/
MultiList.java
executable file
·270 lines (240 loc) · 7.24 KB
/
MultiList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package controlP5;
/**
* controlP5 is a processing gui library.
*
* 2006-2015 by Andreas Schlegel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* @author Andreas Schlegel (http://www.sojamo.de)
* @modified ##date##
* @version ##version##
*
*/
import processing.core.PApplet;
import processing.core.PGraphics;
/**
* A Multilist is a multi-menu-tree controller. see the example for more information and how to use.
*
* @example controllers/ControlP5multiList
*
* TODO is currently broken, is this due to replacing PVector with float[]?
*
*/
public class MultiList extends Controller< MultiList > implements MultiListInterface , ControlListener {
/* TODO reflection does not work properly. TODO add an option to remove MultiListButtons */
protected Tab _myTab;
protected boolean isVisible = true;
private int cnt;
protected boolean isOccupied;
protected boolean isUpdateLocation = false;
protected MultiListInterface mostRecent;
protected int[] _myRect = new int[ 4 ];
protected int _myDirection = ControlP5Constants.RIGHT;
public int closeDelay = 30;
protected int _myDefaultButtonHeight = 10;
protected boolean isUpperCase = true;
/**
* Convenience constructor to extend MultiList.
*
* @example use/ControlP5extendController
* @param theControlP5
* @param theName
*/
public MultiList( ControlP5 theControlP5 , String theName ) {
this( theControlP5 , theControlP5.getDefaultTab( ) , theName , 0 , 0 , 99 , 19 );
theControlP5.register( theControlP5.papplet , theName , this );
}
public MultiList( ControlP5 theControlP5 , Tab theParent , String theName , int theX , int theY , int theWidth , int theHeight ) {
super( theControlP5 , theParent , theName , theX , theY , theWidth , 0 );
_myDefaultButtonHeight = theHeight;
setup( );
}
public MultiList toUpperCase( boolean theValue ) {
isUpperCase = theValue;
for ( Controller c : getSubelements( ) ) {
c.getCaptionLabel( ).toUpperCase( isUpperCase );
}
return this;
}
@ControlP5.Invisible public void setup( ) {
mostRecent = this;
isVisible = true;
updateRect( x( position ) , y( position ) , getWidth( ) , _myDefaultButtonHeight );
}
protected void updateRect( float theX , float theY , float theW , float theH ) {
_myRect = new int[] { ( int ) theX , ( int ) theY , ( int ) theW , ( int ) theH };
}
public int getDirection( ) {
return _myDirection;
}
/**
* TODO does not work.
*
* @param theDirection
*/
void setDirection( int theDirection ) {
_myDirection = ( theDirection == LEFT ) ? LEFT : RIGHT;
for ( int i = 0 ; i < getSubelements( ).size( ) ; i++ ) {
( ( MultiListButton ) getSubelements( ).get( i ) ).setDirection( _myDirection );
}
}
/**
* @param theX
* float
* @param theY
* float
*/
@ControlP5.Invisible public void updateLocation( float theX , float theY ) {
set( position , theX , theY );
updateRect( x( position ) , y( position ) , getWidth( ) , _myDefaultButtonHeight );
for ( int i = 0 ; i < getSubelements( ).size( ) ; i++ ) {
( ( MultiListInterface ) getSubelements( ).get( i ) ).updateLocation( theX , theY );
}
}
/**
* removes the multilist.
*/
public void remove( ) {
super.remove( );
for ( int i = 0 ; i < getSubelements( ).size( ) ; i++ ) {
getSubelements( ).get( i ).removeListener( this );
getSubelements( ).get( i ).remove( );
}
}
/**
* adds multilist buttons to the multilist.
*
* @param theName
* String
* @param theValue
* int
* @return MultiListButton
*/
public MultiListButton add( String theName , int theValue ) {
int x = ( int ) x( position );
int yy = 0;
for ( Controller< ? > c : getSubelements( ) ) {
yy += c.getHeight( ) + 1;
}
int y = ( int ) y( position ) + yy;// (_myDefaultButtonHeight + 1) * _myChildren.size();
MultiListButton b = new MultiListButton( cp5 , theName , theValue , x , y , getWidth( ) , _myDefaultButtonHeight , this , this );
b.toUpperCase( isUpperCase );
b.isMoveable = false;
cp5.register( null , "" , b );
b.addListener( this );
getSubelements( ).add( b );
b.show( );
updateRect( x( position ) , y( position ) , getWidth( ) , ( _myDefaultButtonHeight + 1 ) * getSubelements( ).size( ) );
return b;
}
/**
* @param theEvent
*/
@Override @ControlP5.Invisible public void controlEvent( ControlEvent theEvent ) {
if ( theEvent.getController( ) instanceof MultiListButton ) {
_myValue = theEvent.getController( ).getValue( );
ControlEvent myEvent = new ControlEvent( this );
cp5.getControlBroadcaster( ).broadcast( myEvent , ControlP5Constants.FLOAT );
}
}
/**
* {@inheritDoc}
*/
@Override @ControlP5.Invisible public void draw( PGraphics theGraphics ) {
super.draw( theGraphics );
// TODO update( theGraphics );
}
/**
*
* @param theApplet
* @return boolean
*/
@ControlP5.Invisible public boolean update( PApplet theApplet ) {
if ( !isOccupied ) {
cnt++;
if ( cnt == closeDelay ) {
close( );
}
}
if ( isUpdateLocation ) {
updateLocation( ( _myControlWindow.mouseX - _myControlWindow.pmouseX ) , ( _myControlWindow.mouseY - _myControlWindow.pmouseY ) );
isUpdateLocation = theApplet.mousePressed;
}
if ( isOccupied ) {
if ( theApplet.keyPressed && theApplet.mousePressed ) {
if ( theApplet.keyCode == PApplet.ALT ) {
isUpdateLocation = true;
return true;
}
}
}
return false;
}
/**
*
* @param theFlag
* boolean
*/
@ControlP5.Invisible public void occupied( boolean theFlag ) {
isOccupied = theFlag;
cnt = 0;
}
/**
* @return boolean
*/
@ControlP5.Invisible public boolean observe( ) {
return CP.inside( _myRect , _myControlWindow.mouseX , _myControlWindow.mouseY );
}
/**
* @param theInterface
* MultiListInterface
*/
public void close( MultiListInterface theInterface ) {
for ( int i = 0 ; i < getSubelements( ).size( ) ; i++ ) {
if ( theInterface != ( MultiListInterface ) getSubelements( ).get( i ) ) {
( ( MultiListInterface ) getSubelements( ).get( i ) ).close( );
}
}
}
/**
* {@inheritDoc}
*/
@Override public void close( ) {
for ( int i = 0 ; i < getSubelements( ).size( ) ; i++ ) {
( ( MultiListInterface ) getSubelements( ).get( i ) ).close( );
}
}
/**
* {@inheritDoc}
*/
@Override public void open( ) {
for ( int i = 0 ; i < getSubelements( ).size( ) ; i++ ) {
( ( MultiListInterface ) getSubelements( ).get( i ) ).open( );
}
}
/**
* {@inheritDoc}
*/
@Override public MultiList setValue( float theValue ) {
return this;
}
/**
* {@inheritDoc}
*/
@Override public MultiList update( ) {
return setValue( _myValue );
}
}