Update ControlGroup.java

Given ControlGroup is just a small addendum to ControllerGroup, there was some need to control its loop, add() and remove() codes.
While at it, erased many redundant members being re-implemented w/o any modifications to the inherited originals.
This commit is contained in:
GoToLoop 2016-07-11 03:08:56 -03:00 committed by GitHub
parent 1f7cb64986
commit ce9b919750

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.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -46,45 +46,36 @@ import processing.core.PGraphics;
* @see controlP5.Group * @see controlP5.Group
* @example controllers/ControlP5group * @example controllers/ControlP5group
*/ */
public class ControlGroup< T > extends ControllerGroup< T > implements ControlListener { public class ControlGroup< T extends ControlGroup< T > > extends ControllerGroup< T > {
protected int _myBackgroundHeight = 0; protected int _myBackgroundHeight = 0;
protected int _myBackgroundColor = 0x00ffffff; protected int _myBackgroundColor = 0x00ffffff;
protected boolean isEventActive;
protected boolean isEventActive = false;
protected List< ControlListener > _myControlListener;
/** /**
* Convenience constructor to extend ControlGroup. * Convenience constructor to extend ControlGroup.
*/ */
public ControlGroup( ControlP5 theControlP5 , String theName ) { public ControlGroup( ControlP5 theControlP5 , String theName ) {
this( theControlP5 , theControlP5.getDefaultTab( ) , theName , 0 , 0 , 100 , 9 ); this( theControlP5 , theControlP5.getDefaultTab( ) , theName , 0 , 0 , 100 , 9 );
theControlP5.register( theControlP5.papplet , theName , this ); cp5.register( theControlP5.papplet , theName , this );
} }
public ControlGroup( ControlP5 theControlP5 , ControllerGroup< ? > theParent , String theName , int theX , int theY , int theW , int theH ) { public ControlGroup( ControlP5 theControlP5 , ControllerGroup< ? extends ControllerGroup< ? > > theParent ,
String theName , int theX , int theY , int theW , int theH ) {
super( theControlP5 , theParent , theName , theX , theY ); super( theControlP5 , theParent , theName , theX , theY );
_myControlListener = new ArrayList< ControlListener >( );
_myValueLabel = new Label( cp5 , "" ); _myValueLabel = new Label( cp5 , "" );
_myWidth = theW; _myWidth = theW;
_myHeight = theH; _myHeight = theH;
} }
@ControlP5.Invisible @ControlP5.Invisible @Override
public void mousePressed( ) { public void mousePressed( ) {
if ( isBarVisible && isCollapse ) { if ( !( isBarVisible && isCollapse && !cp5.isAltDown( ) ) ) return;
if ( !cp5.isAltDown( ) ) {
isOpen = !isOpen; isOpen = !isOpen;
if ( isEventActive ) { if ( !isEventActive ) return;
final ControlEvent myEvent = new ControlEvent( this ); final ControlEvent myEvent = new ControlEvent( this );
cp5.getControlBroadcaster( ).broadcast( myEvent , ControlP5Constants.METHOD ); cp5.getControlBroadcaster( ).broadcast( myEvent , METHOD );
for ( ControlListener cl : _myControlListener ) { if ( !_myControlListener.isEmpty( ) ) synchronized ( _myControlListener ) {
cl.controlEvent( myEvent ); for ( final ControlListener cl : _myControlListener ) cl.controlEvent( myEvent );
}
}
}
} }
} }
@ -97,10 +88,10 @@ public class ControlGroup< T > extends ControllerGroup< T > implements ControlLi
return me; return me;
} }
@Override
public T setSize( int theWidth , int theHeight ) { public T setSize( int theWidth , int theHeight ) {
super.setSize( theWidth , theHeight ); super.setSize( theWidth , theHeight );
setBackgroundHeight( theHeight ); return setBackgroundHeight( theHeight );
return me;
} }
public int getBackgroundHeight( ) { public int getBackgroundHeight( ) {
@ -128,12 +119,11 @@ public class ControlGroup< T > extends ControllerGroup< T > implements ControlLi
@Override @Override
public T updateInternalEvents( PApplet theApplet ) { public T updateInternalEvents( PApplet theApplet ) {
if ( isInside && isBarVisible ) { if ( isInside && isBarVisible ) getWindow( ).setMouseOverController( this );
cp5.getWindow( ).setMouseOverController( this );
}
return me; return me;
} }
@Override
protected void preDraw( PGraphics theGraphics ) { protected void preDraw( PGraphics theGraphics ) {
if ( isOpen ) { if ( isOpen ) {
theGraphics.fill( _myBackgroundColor ); theGraphics.fill( _myBackgroundColor );
@ -141,30 +131,22 @@ public class ControlGroup< T > extends ControllerGroup< T > implements ControlLi
} }
} }
@Override
protected void postDraw( PGraphics theGraphics ) { protected void postDraw( PGraphics theGraphics ) {
if ( isBarVisible ) { if ( !isBarVisible ) return;
theGraphics.fill( isInside ? color.getForeground( ) : color.getBackground( ) ); theGraphics.fill( isInside ? color.getForeground( ) : color.getBackground( ) );
theGraphics.rect( 0 , -1 , _myWidth , -_myHeight ); theGraphics.rect( 0 , -1 , _myWidth , -_myHeight );
_myLabel.draw( theGraphics , 0 , -_myHeight - 1 , this ); _myLabel.draw( theGraphics , 0 , -_myHeight - 1 , this );
if ( isCollapse && isArrowVisible ) { if ( !isCollapse || !isArrowVisible ) return;
theGraphics.fill( _myLabel.getColor( ) ); theGraphics.fill( _myLabel.getColor( ) );
theGraphics.pushMatrix( ); final int w = _myWidth + 2 , h = -( _myHeight >> 1 );
theGraphics.translate( 2 , 0 ); final int h1 = isOpen ? h - 3 : h , h2 = isOpen ? h : h - 3;
if ( isOpen ) { theGraphics.triangle( w - 10 , h1 , w - 4 , h1 , w - 7 , h2 );
theGraphics.triangle( _myWidth - 10 , -_myHeight / 2 - 3 , _myWidth - 4 , -_myHeight / 2 - 3 , _myWidth - 7 , -_myHeight / 2 );
} else {
theGraphics.triangle( _myWidth - 10 , -_myHeight / 2 , _myWidth - 4 , -_myHeight / 2 , _myWidth - 7 , -_myHeight / 2 - 3 );
}
theGraphics.popMatrix( );
}
}
} }
@ControlP5.Invisible @ControlP5.Invisible @Override
public void controlEvent( ControlEvent theEvent ) { public void controlEvent( ControlEvent theEvent ) {
if ( theEvent.getController( ).getName( ).equals( getName( ) + "close" ) ) { if ( theEvent.getController( ).getName( ).equals( getName( ) + "close" ) ) hide( );
hide( );
}
} }
@ControlP5.Invisible @ControlP5.Invisible
@ -172,28 +154,8 @@ public class ControlGroup< T > extends ControllerGroup< T > implements ControlLi
return Float.toString( _myValue ); return Float.toString( _myValue );
} }
@Override
public String toString( ) {
return super.toString( );
}
@Override @Override
public String getInfo( ) { public String getInfo( ) {
return "type:\tControlGroup\n" + super.getInfo( ); return "type:\tControlGroup\n" + super.getInfo( );
} }
public T addListener( final ControlListener theListener ) {
_myControlListener.add( theListener );
return me;
}
public T removeListener( final ControlListener theListener ) {
_myControlListener.remove( theListener );
return me;
}
public int listenerSize( ) {
return _myControlListener.size( );
}
} }