Skip to content

Commit

Permalink
Changed behavior of the delete key
Browse files Browse the repository at this point in the history
Added click selection support for Textfields
  • Loading branch information
Viproz committed Nov 7, 2018
1 parent 1f7cb64 commit 2e47b17
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/controlP5/ControlWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ public ControlWindow setUndecorated( boolean theFlag ) {
_myApplet.frame.removeNotify( );
_myApplet.frame.setUndecorated( isUndecorated );
_myApplet.setSize( _myApplet.width , _myApplet.height );
_myApplet.setBounds( 0 , 0 , _myApplet.width , _myApplet.height );
//_myApplet.setBounds( 0 , 0 , _myApplet.width , _myApplet.height );
_myApplet.frame.setSize( _myApplet.width , _myApplet.height );
_myApplet.frame.addNotify( );
}
Expand Down
40 changes: 33 additions & 7 deletions src/controlP5/Textfield.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public Textfield( ControlP5 theControlP5 , ControllerGroup< ? > theParent , Stri
keyMapping = new HashMap< Integer , TextfieldCommand >( );
keyMapping.put( ENTER , new Enter( ) );
keyMapping.put( DEFAULT , new InsertCharacter( ) );
keyMapping.put( DELETE , new DeleteCharacter( ) );
keyMapping.put( BACKSPACE , new DeleteCharacter( ) );
keyMapping.put( DELETE , new DeleteCharacterRight( ) );
keyMapping.put( BACKSPACE , new DeleteCharacterLeft( ) );
keyMapping.put( LEFT , new MoveLeft( ) );
keyMapping.put( RIGHT , new MoveRight( ) );
keyMapping.put( UP , new MoveUp( ) );
Expand Down Expand Up @@ -261,11 +261,27 @@ public boolean isAutoClear( ) {
@Override protected void mousePressed( ) {
if ( isActive ) {
// TODO System.out.println("adjust cursor");
// Multiline not supported
}
int x = ( int ) ( getControlWindow( ).mouseX - x( getAbsolutePosition( ) ) );
int y = ( int ) ( getControlWindow( ).mouseY - y( getAbsolutePosition( ) ) );

// TODO System.out.println(x + ":" + y);
int x = ( int ) ( getControlWindow( ).mouseX - x( getPosition( ) ) );
int y = ( int ) ( getControlWindow( ).mouseY - y( getPosition( ) ) );

int i = 0;
int textWidth;
for(i = 0 ; i <= _myTextBuffer.length() ; i++) {
textWidth = ControlFont.getWidthFor( _myTextBuffer.substring( 0 , i ) , _myValueLabel , buffer );
if(textWidth > x) {
if(i != 0 && textWidth-x > ControlFont.getWidthFor( _myTextBuffer.substring( i-1 , i ) , _myValueLabel , buffer )/2) {
i--;
}
break;
}
}
if(i > _myTextBuffer.length())
i = _myTextBuffer.length();

setIndex(i);

setFocus( true );
}

Expand Down Expand Up @@ -424,7 +440,7 @@ public void execute( ) {
}
}

class DeleteCharacter implements TextfieldCommand {
class DeleteCharacterLeft implements TextfieldCommand {

public void execute( ) {
if ( _myTextBuffer.length( ) > 0 && _myTextBufferIndex > 0 ) {
Expand All @@ -434,6 +450,16 @@ public void execute( ) {
}
}

class DeleteCharacterRight implements TextfieldCommand {

public void execute( ) {
if ( _myTextBuffer.length( ) > 0 && _myTextBufferIndex < _myTextBuffer.length( ) ) {
_myTextBuffer.deleteCharAt( _myTextBufferIndex );
setIndex( _myTextBufferIndex );
}
}
}

class MoveLeft implements TextfieldCommand {

public void execute( ) {
Expand Down

0 comments on commit 2e47b17

Please sign in to comment.