Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Textfield mouse support and corrected behaviour for delete key #143

Open
wants to merge 2 commits into
base: dev-2.2.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran into this too. Might be better off deleting it. For anyone unfamiliar, PApplet.setBounds is not present anymore and the project won't compile without this line commented out or deleted.

_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