Update Textarea.java

Fix & cleanup **_ControllerGroup_** inheritance in **_Textarea_**.
This commit is contained in:
GoToLoop 2016-07-15 06:28:25 -03:00 committed by GitHub
parent 1f7cb64986
commit dc953a7c75
1 changed files with 65 additions and 89 deletions

View File

@ -1,5 +1,3 @@
package controlP5;
/** /**
* controlP5 is a processing gui library. * controlP5 is a processing gui library.
* *
@ -25,6 +23,8 @@ package controlP5;
* *
*/ */
package controlP5;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -39,7 +39,7 @@ import processing.core.PGraphics;
* *
* @example controllers/ControlP5textarea * @example controllers/ControlP5textarea
*/ */
public class Textarea extends ControllerGroup< Textarea > implements ControlListener { public class Textarea extends ControllerGroup< Textarea > {
protected String _myText; protected String _myText;
protected Slider _myScrollbar; protected Slider _myScrollbar;
@ -81,7 +81,8 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
* @param theH * @param theH
* int * int
*/ */
protected Textarea( ControlP5 theControlP5 , ControllerGroup< ? > theGroup , String theName , String theText , int theX , int theY , int theW , int theH ) { protected Textarea( ControlP5 theControlP5 , ControllerGroup< ? extends ControllerGroup< ? > > theGroup ,
String theName , String theText , int theX , int theY , int theW , int theH ) {
super( theControlP5 , theGroup , theName , theX , theY ); super( theControlP5 , theGroup , theName , theX , theY );
_myWidth = theW; _myWidth = theW;
_myHeight = theH; _myHeight = theH;
@ -109,8 +110,8 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
_myValueLabel.setFont( cp5.controlFont == cp5.defaultFont ? cp5.defaultFontForText : cp5.controlFont ); _myValueLabel.setFont( cp5.controlFont == cp5.defaultFont ? cp5.defaultFontForText : cp5.controlFont );
_myValueLabel.setWidth( ( int ) _myWidth ); _myValueLabel.setWidth( _myWidth );
_myValueLabel.setHeight( ( int ) _myHeight ); _myValueLabel.setHeight( _myHeight );
_myValueLabel.setMultiline( true ); _myValueLabel.setMultiline( true );
_myValueLabel.toUpperCase( false ); _myValueLabel.toUpperCase( false );
_myValueLabel.setColor( color.getValueLabel( ) ); _myValueLabel.setColor( color.getValueLabel( ) );
@ -136,8 +137,8 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
* @param theEvent * @param theEvent
* ControlEvent * ControlEvent
*/ */
public void controlEvent( ControlEvent theEvent ) { @Override public void controlEvent( ControlEvent theEvent ) {
_myScrollValue = - ( 1 - theEvent.getValue( ) ); _myScrollValue = theEvent.getValue( ) - 1;
scroll( ); scroll( );
} }
@ -149,10 +150,8 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
public Textarea showScrollbar( ) { public Textarea showScrollbar( ) {
isScrollbarVisible = true; isScrollbarVisible = true;
boolean isScrollbar = _myHeight < ( _myValueLabel.getTextHeight( ) + _myValueLabel.getLineHeight( ) ); boolean isScrollbar = _myHeight < _myValueLabel.getTextHeight( ) + _myValueLabel.getLineHeight( );
if ( isScrollbar ) { if ( isScrollbar ) _myScrollbar.show( );
_myScrollbar.show( );
}
return this; return this;
} }
@ -160,8 +159,12 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
return _myScrollbar.isVisible( ); return _myScrollbar.isVisible( );
} }
@Override public Textarea setColorBackground( int theColor ) { @Override public T setColorBackground( int theColor ) {
_myColorBackground = theColor; _myColorBackground = theColor;
return enableColorBackground( );
}
public Textarea enableColorBackground( ) {
isColorBackground = true; isColorBackground = true;
return this; return this;
} }
@ -171,11 +174,6 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
return this; return this;
} }
public Textarea enableColorBackground( ) {
isColorBackground = true;
return this;
}
/** /**
* scroll the Textarea remotely. values must range from 0 to 1. * scroll the Textarea remotely. values must range from 0 to 1.
* *
@ -189,25 +187,20 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
/** /**
* private update method for the scrollbar. * private update method for the scrollbar.
*/ */
private void scroll( ) { private Textarea scroll( ) {
_myScrollValue = PApplet.min( PApplet.max( -1 , _myScrollValue ) , 0 ); _myScrollValue = PApplet.constrain( _myScrollValue , -1 , 0 );
float myLen = _myValueLabel.getTextHeight( ) + _myValueLabel.getLineHeight( ); float myLen = _myValueLabel.getTextHeight( ) + _myValueLabel.getLineHeight( );
float myOffset = 0;
boolean isScrollbar = _myHeight < myLen; boolean isScrollbar = _myHeight < myLen;
if ( isScrollbar ) { //float myOffset = isScrollbar ? _myScrollValue * ( myLen - _myHeight + _myBottomOffset ) : 0;
myOffset = _myScrollValue * ( myLen - _myHeight + _myBottomOffset ); _myScrollbar.setVisible( isScrollbarVisible ? isScrollbar : false );
}
isScrollbar = ( isScrollbarVisible ) ? isScrollbar : false;
_myScrollbar.setVisible( isScrollbar );
_myValueLabel.setOffsetYratio( _myScrollValue ); _myValueLabel.setOffsetYratio( _myScrollValue );
return this;
} }
@ControlP5.Invisible public void scrolled( int theStep ) { @ControlP5.Invisible public void scrolled( int theStep ) {
if ( _myScrollbar.isVisible( ) ) { if ( _myScrollbar.isVisible( ) ) {
int lines = ( _myValueLabel.getTextHeight( ) / _myValueLabel.getLineHeight( ) ); float lines = _myValueLabel.getTextHeight( ) / _myValueLabel.getLineHeight( );
float step = 1.0f / lines; scroll( theStep/lines - getScrollPosition( ) + 1f );
scroll( ( 1 - getScrollPosition( ) ) + ( theStep * step ) );
} }
} }
@ -218,34 +211,32 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
/** /**
* set the width of the textarea. * set the width of the textarea.
* *
* @param theValue * @param theWidth
* int * int
*/ */
@Override public Textarea setWidth( int theValue ) { @Override public T setWidth( int theWidth ) {
theValue = ( theValue < 10 ) ? 10 : theValue; _myWidth = Math.max( 10 , theWidth );
_myWidth = theValue;
_myValueLabel.setWidth( _myWidth - _myScrollbarWidth - 10 ); _myValueLabel.setWidth( _myWidth - _myScrollbarWidth - 10 );
return this; return me;
} }
/** /**
* set the height of the textarea. * set the height of the textarea.
* *
* @param theValue * @param theHeight
* int * int
*/ */
@Override public Textarea setHeight( int theValue ) { @Override public T setHeight( int theHeight ) {
theValue = ( theValue < 10 ) ? 10 : theValue; _myHeight = Math.max( 10 , theHeight );
_myHeight = theValue;
_myValueLabel.setHeight( _myHeight - 2 ); _myValueLabel.setHeight( _myHeight - 2 );
_myScrollbar.setHeight( theValue ); _myScrollbar.setHeight( theHeight );
return this; return me;
} }
public Textarea setSize( int theWidth , int theHeight ) { @Override public T setSize( int theWidth , int theHeight ) {
setWidth( theWidth ); setWidth( theWidth );
setHeight( theHeight ); setHeight( theHeight );
return this; return me;
} }
/** /**
@ -256,8 +247,7 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
*/ */
public Textarea setLineHeight( int theLineHeight ) { public Textarea setLineHeight( int theLineHeight ) {
_myValueLabel.setLineHeight( theLineHeight ); _myValueLabel.setLineHeight( theLineHeight );
scroll( ); return scroll( );
return this;
} }
/** /**
@ -271,15 +261,6 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
return this; return this;
} }
/**
* returns the instance of the textarea's label.
*
* @return
*/
public Label getValueLabel( ) {
return _myValueLabel;
}
/** /**
* set the text of the textarea. * set the text of the textarea.
* *
@ -288,8 +269,9 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
*/ */
public Textarea setText( String theText ) { public Textarea setText( String theText ) {
_myValueLabel.set( theText ); _myValueLabel.set( theText );
_myScrollValue = ( float ) ( _myHeight ) / ( float ) ( _myValueLabel.getTextHeight( ) ); _myScrollValue = ( float ) _myHeight / _myValueLabel.getTextHeight( );
_myScrollbar.setHeight( _myHeight + _myValueLabel.getStyle( ).paddingTop + _myValueLabel.getStyle( ).paddingBottom ); final ControllerStyle style = _myValueLabel.getStyle( );
_myScrollbar.setHeight( _myHeight + style.paddingTop + style.paddingBottom );
return this; return this;
} }
@ -303,13 +285,10 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
public Textarea append( String theText , int max ) { public Textarea append( String theText , int max ) {
String str = getText( ) + theText; String str = getText( ) + theText;
if ( max == -1 ) return setText( str );
if ( max == -1 ) {
return setText( str );
}
List< String > strs = Arrays.asList( str.split( "\n" ) ); List< String > strs = Arrays.asList( str.split( "\n" ) );
return setText( CP.join( strs.subList( Math.max( 0 , strs.size( ) - max ) , strs.size( ) ) , "\n" ) ); int len = strs.size( );
return setText( CP.join( strs.subList( Math.max( 0 , len - max ) , len ) , "\n" ) );
} }
/** /**
@ -321,45 +300,42 @@ public class Textarea extends ControllerGroup< Textarea > implements ControlList
return getStringValue( ); return getStringValue( );
} }
@Override protected void preDraw( PGraphics theGraphics ) { @Override protected void preDraw( final PGraphics theGraphics ) {
if ( isScrollbarVisible ) { if ( isScrollbarVisible ) _myScrollbar.setVisible( _myValueLabel.getOverflow( ) > 1 );
_myScrollbar.setVisible( _myValueLabel.getOverflow( ) > 1 ); if ( !isColorBackground && !_myScrollbar.isVisible( ) ) return;
} final ControllerStyle style = _myValueLabel.getStyle( );
if ( _myScrollbar.isVisible( ) || isColorBackground ) { float x = _myWidth - _myScrollbarWidth + style.paddingLeft + style.paddingRight;
float x = _myWidth - _myScrollbarWidth + _myValueLabel.getStyle( ).paddingLeft + _myValueLabel.getStyle( ).paddingRight; set( _myScrollbar.getPosition( ) , x , y( _myScrollbar.getPosition( ) ) );
float y = y( _myScrollbar.getPosition( ) ); if ( isColorBackground ) {
set( _myScrollbar.getPosition( ) , x , y ); int a = _myColorBackground >> 24 & 0xff;
if ( !isColorBackground ) { theGraphics.fill( _myColorBackground , a > 0 ? a : 0xff );
theGraphics.noFill( ); } else theGraphics.noFill( );
} else { int ww = _myWidth + style.paddingLeft + style.paddingRight;
int a = _myColorBackground >> 24 & 0xff; int hh = _myHeight + style.paddingTop + style.paddingBottom;
theGraphics.fill( _myColorBackground , a > 0 ? a : 255 ); theGraphics.rect( 0 , 0 , ww , hh );
}
int ww = _myWidth + _myValueLabel.getStyle( ).paddingLeft + _myValueLabel.getStyle( ).paddingRight;
int hh = _myHeight + _myValueLabel.getStyle( ).paddingTop + _myValueLabel.getStyle( ).paddingBottom;
theGraphics.rect( 0 , 0 , ww , hh );
}
} }
// !!! add padding to the box. // !!! add padding to the box.
// padding and margin doesnt work nicely with textarea yet! // padding and margin doesn't work nicely with Textarea yet!
protected boolean inside( ) { @Override protected boolean inside( ) {
return ( cp5.getWindow( ).mouseX > x( position ) + x( _myParent.absolutePosition ) && cp5.getWindow( ).mouseX < x( position ) + x( _myParent.absolutePosition ) + _myWidth final int mx = getWindow( ).mouseX, my = getWindow( ).mouseY;
&& cp5.getWindow( ).mouseY > y( position ) + y( _myParent.absolutePosition ) && cp5.getWindow( ).mouseY < y( position ) + y( _myParent.absolutePosition ) + _myHeight ); final int x = Math.round( x( position ) + x( _myParent.absolutePosition ) );
final int y = Math.round( y( position ) + y( _myParent.absolutePosition ) );
return mx > x && mx < x + _myWidth && my > y && my < y + _myHeight;
} }
public String getStringValue( ) { @Override public String getStringValue( ) {
return _myValueLabel.getText( ); return _myValueLabel.getText( );
} }
public Textarea setFont( ControlFont theFont ) { @Override public T setFont( ControlFont theFont ) {
getValueLabel( ).setFont( theFont ); getValueLabel( ).setFont( theFont );
return this; return me;
} }
public Textarea setFont( PFont thePFont ) { @Override public T setFont( PFont thePFont ) {
getValueLabel( ).setFont( thePFont ); getValueLabel( ).setFont( thePFont );
return this; return me;
} }
public Textarea setFont( int theFontIndex ) { public Textarea setFont( int theFontIndex ) {