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

Update ControlGroup.java #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
108 changes: 35 additions & 73 deletions src/controlP5/ControlGroup.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package controlP5;

/**
* controlP5 is a processing gui library.
*
Expand All @@ -25,6 +23,8 @@
*
*/

package controlP5;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -46,45 +46,36 @@
* @see controlP5.Group
* @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 _myBackgroundColor = 0x00ffffff;

protected boolean isEventActive = false;

protected List< ControlListener > _myControlListener;
protected int _myBackgroundColor = 0x00ffffff;
protected boolean isEventActive;

/**
* Convenience constructor to extend ControlGroup.
*/
public ControlGroup( ControlP5 theControlP5 , String theName ) {
this( theControlP5 , theControlP5.getDefaultTab( ) , theName , 0 , 0 , 100 , 9 );
theControlP5.register( theControlP5.papplet , theName , this );
cp5.register( cp5.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 );
_myControlListener = new ArrayList< ControlListener >( );
_myValueLabel = new Label( cp5 , "" );
_myWidth = theW;
_myWidth = theW;
_myHeight = theH;
}

@ControlP5.Invisible
@ControlP5.Invisible @Override
public void mousePressed( ) {
if ( isBarVisible && isCollapse ) {
if ( !cp5.isAltDown( ) ) {
isOpen = !isOpen;
if ( isEventActive ) {
final ControlEvent myEvent = new ControlEvent( this );
cp5.getControlBroadcaster( ).broadcast( myEvent , ControlP5Constants.METHOD );
for ( ControlListener cl : _myControlListener ) {
cl.controlEvent( myEvent );
}
}
}
if ( !( isBarVisible && isCollapse && !cp5.isAltDown( ) ) ) return;
isOpen = !isOpen;
if ( !isEventActive ) return;
final ControlEvent myEvent = new ControlEvent( this );
cp5.getControlBroadcaster( ).broadcast( myEvent , METHOD );
synchronized ( _myControlListener ) {
for ( final ControlListener cl : _myControlListener ) cl.controlEvent( myEvent );
}
}

Expand All @@ -97,10 +88,10 @@ public T activateEvent( boolean theFlag ) {
return me;
}

@Override
public T setSize( int theWidth , int theHeight ) {
super.setSize( theWidth , theHeight );
setBackgroundHeight( theHeight );
return me;
return setBackgroundHeight( theHeight );
}

public int getBackgroundHeight( ) {
Expand Down Expand Up @@ -128,72 +119,43 @@ public int getBarHeight( ) {

@Override
public T updateInternalEvents( PApplet theApplet ) {
if ( isInside && isBarVisible ) {
cp5.getWindow( ).setMouseOverController( this );
}
if ( isInside && isBarVisible ) getWindow( ).setMouseOverController( this );
return me;
}

@Override
protected void preDraw( PGraphics theGraphics ) {
if ( isOpen ) {
theGraphics.fill( _myBackgroundColor );
theGraphics.rect( 0 , 0 , _myWidth , _myBackgroundHeight - 1 );
}
}

@Override
protected void postDraw( PGraphics theGraphics ) {
if ( isBarVisible ) {
theGraphics.fill( isInside ? color.getForeground( ) : color.getBackground( ) );
theGraphics.rect( 0 , -1 , _myWidth , -_myHeight );
_myLabel.draw( theGraphics , 0 , -_myHeight - 1 , this );
if ( isCollapse && isArrowVisible ) {
theGraphics.fill( _myLabel.getColor( ) );
theGraphics.pushMatrix( );
theGraphics.translate( 2 , 0 );
if ( isOpen ) {
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
if ( !isBarVisible ) return;
theGraphics.fill( isInside ? color.getForeground( ) : color.getBackground( ) );
theGraphics.rect( 0 , -1 , _myWidth , -_myHeight );
_myLabel.draw( theGraphics , 0 , -_myHeight - 1 , this );
if ( !isCollapse || !isArrowVisible ) return;
theGraphics.fill( _myLabel.getColor( ) );
final int w = _myWidth + 2 , h = -( _myHeight >> 1 );
final int h1 = isOpen ? h - 3 : h , h2 = isOpen ? h : h - 3;
theGraphics.triangle( w - 10 , h1 , w - 4 , h1 , w - 7 , h2 );
}

@ControlP5.Invisible @Override
public void controlEvent( ControlEvent theEvent ) {
if ( theEvent.getController( ).getName( ).equals( getName( ) + "close" ) ) {
hide( );
}
if ( theEvent.getController( ).getName( ).equals( getName( ) + "close" ) ) hide( );
}

@ControlP5.Invisible
public String stringValue( ) {
return Float.toString( _myValue );
}

@Override
public String toString( ) {
return super.toString( );
}

@Override
public String 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( );
}

}