@@ -35,11 +35,12 @@ class WidgetManager {
3535 currentContainerLayout = 4 ; // default layout ... tall container left and 2 shorter containers stacked on the right
3636 sessionSettings. currentLayout = 4 ;
3737 }
38-
39- setNewContainerLayout(currentContainerLayout); // sets and fills layout with widgets in order of widget index, to reorganize widget index, reorder the creation in setupWidgets()
38+
39+ // Set and fill layout with widgets in order of widget index
40+ setNewContainerLayout(currentContainerLayout);
4041 }
4142
42- void setupWidgets () {
43+ private void setupWidgets () {
4344
4445 widgets. add(new W_TimeSeries ());
4546
@@ -95,174 +96,181 @@ class WidgetManager {
9596 widgets. add(new W_Template ());
9697 }
9798
98- void setupWidgetSelectorDropdowns () {
99- // create the widgetSelector dropdown of each widget
100- // println("widgets.size() = " + widgets.size());
101- // create list of WidgetTitles.. we will use this to populate the dropdown (widget selector) of each widget
102- for (int i = 0 ; i < widgets. size(); i++ ) {
103- widgetOptions. add(widgets. get(i). widgetTitle);
99+ private void setupWidgetSelectorDropdowns () {
100+ // Populate the dropdown options with widget titles
101+ for (Widget widget : widgets) {
102+ widgetOptions. add(widget. widgetTitle);
104103 }
105- // println("widgetOptions.size() = " + widgetOptions.size());
106- for (int i = 0 ; i < widgetOptions. size(); i++ ) {
107- widgets. get(i). setupWidgetSelectorDropdown(widgetOptions);
108- widgets. get(i). setupNavDropdowns();
104+
105+ // Setup the dropdown for each widget
106+ for (Widget widget : widgets) {
107+ widget. setupWidgetSelectorDropdown(widgetOptions);
108+ widget. setupNavDropdowns();
109109 }
110110 }
111111
112- void update () {
113- for (int i = 0 ; i < widgets. size(); i++ ) {
114- if (widgets. get(i). getIsActive()) {
115- widgets. get(i). update();
116- // if the widgets are not mapped to containers correctly, remap them..
117- // if (widgets.get(i).x != container[widgets.get(i).currentContainer].x || widgets.get(i).y != container[widgets.get(i).currentContainer].y || widgets.get(i).w != container[widgets.get(i).currentContainer].w || widgets.get(i).h != container[widgets.get(i).currentContainer].h) {
118- if (widgets. get(i). x0 != (int )container[widgets. get(i). currentContainer]. x || widgets. get(i). y0 != (int )container[widgets. get(i). currentContainer]. y || widgets. get(i). w0 != (int )container[widgets. get(i). currentContainer]. w || widgets. get(i). h0 != (int )container[widgets. get(i). currentContainer]. h) {
119- screenResized();
120- println (" WidgetManager.pde: Remapping widgets to container layout..." );
121- }
112+ public void update () {
113+ for (Widget currentWidget : widgets) {
114+ if (! currentWidget. getIsActive()) {
115+ continue ;
116+ }
117+
118+ currentWidget. update();
119+
120+ // Check if widget position or dimensions have changed relative to its container
121+ boolean positionChanged = currentWidget. x0 != (int )container[currentWidget. currentContainer]. x;
122+ boolean yPositionChanged = currentWidget. y0 != (int )container[currentWidget. currentContainer]. y;
123+ boolean widthChanged = currentWidget. w0 != (int )container[currentWidget. currentContainer]. w;
124+ boolean heightChanged = currentWidget. h0 != (int )container[currentWidget. currentContainer]. h;
125+
126+ if (positionChanged || yPositionChanged || widthChanged || heightChanged) {
127+ screenResized();
128+ println (" WidgetManager.pde: Remapping widgets to container layout..." );
122129 }
123130 }
124131 }
125132
126- void draw () {
127- for (int i = 0 ; i < widgets. size(); i ++ ) {
128- if (widgets . get(i) . getIsActive()) {
129- widgets . get(i) . draw();
130- widgets . get(i) . drawDropdowns();
133+ public void draw () {
134+ for (Widget widget : widgets) {
135+ if (widget . getIsActive()) {
136+ widget . draw();
137+ widget . drawDropdowns();
131138 }
132139 }
133140 }
134141
135- void screenResized () {
136- for (int i = 0 ; i < widgets. size(); i ++ ) {
137- widgets . get(i) . screenResized();
142+ public void screenResized () {
143+ for (Widget widget : widgets) {
144+ widget . screenResized();
138145 }
139146 }
140147
141- void mousePressed () {
142- for (int i = 0 ; i < widgets. size(); i ++ ) {
143- if (widgets . get(i) . getIsActive()) {
144- widgets . get(i) . mousePressed();
148+ public void mousePressed () {
149+ for (Widget widget : widgets) {
150+ if (widget . getIsActive()) {
151+ widget . mousePressed();
145152 }
146-
147153 }
148154 }
149155
150- void mouseReleased () {
151- for (int i = 0 ; i < widgets. size(); i ++ ) {
152- if (widgets . get(i) . getIsActive()) {
153- widgets . get(i) . mouseReleased();
156+ public void mouseReleased () {
157+ for (Widget widget : widgets) {
158+ if (widget . getIsActive()) {
159+ widget . mouseReleased();
154160 }
155161 }
156162 }
157163
158- void mouseDragged () {
159- for (int i = 0 ; i < widgets. size(); i ++ ) {
160- if (widgets . get(i) . getIsActive()) {
161- widgets . get(i) . mouseDragged();
164+ public void mouseDragged () {
165+ for (Widget widget : widgets) {
166+ if (widget . getIsActive()) {
167+ widget . mouseDragged();
162168 }
163169 }
164170 }
165171
166- void setupLayouts () {
167- // refer to [PUT_LINK_HERE] for layouts/numbers image
168- // note that the order you create/add these layouts matters... if you reorganize these, the LayoutSelector will be out of order
169- layouts. add(new Layout (new int []{5 })); // layout 1
170- layouts. add(new Layout (new int []{1 ,3 ,7 ,9 })); // layout 2
171- layouts. add(new Layout (new int []{4 ,6 })); // layout 3
172- layouts. add(new Layout (new int []{2 ,8 })); // etc.
173- layouts. add(new Layout (new int []{4 ,3 ,9 }));
174- layouts. add(new Layout (new int []{1 ,7 ,6 }));
175- layouts. add(new Layout (new int []{1 ,3 ,8 }));
176- layouts. add(new Layout (new int []{2 ,7 ,9 }));
177- layouts. add(new Layout (new int []{4 ,11 ,12 ,13 ,14 }));
178- layouts. add(new Layout (new int []{4 ,15 ,16 ,17 ,18 }));
179- layouts. add(new Layout (new int []{1 ,7 ,11 ,12 ,13 ,14 }));
180- layouts. add(new Layout (new int []{1 ,7 ,15 ,16 ,17 ,18 }));
172+ private void setupLayouts () {
173+ // Reference for layouts: [PUT_LINK_HERE] for layouts/numbers image
174+ // Note: Order matters for the LayoutSelector UI
175+ layouts. add(new Layout (new int []{5 })); // layout 1: Single container
176+ layouts. add(new Layout (new int []{1 ,3 ,7 ,9 })); // layout 2: Four equal containers
177+ layouts. add(new Layout (new int []{4 ,6 })); // layout 3: Left/right split
178+ layouts. add(new Layout (new int []{2 ,8 })); // layout 4: Top/bottom split
179+ layouts. add(new Layout (new int []{4 ,3 ,9 })); // layout 5
180+ layouts. add(new Layout (new int []{1 ,7 ,6 })); // layout 6
181+ layouts. add(new Layout (new int []{1 ,3 ,8 })); // layout 7
182+ layouts. add(new Layout (new int []{2 ,7 ,9 })); // layout 8
183+ layouts. add(new Layout (new int []{4 ,11 ,12 ,13 ,14 })); // layout 9
184+ layouts. add(new Layout (new int []{4 ,15 ,16 ,17 ,18 })); // layout 10
185+ layouts. add(new Layout (new int []{1 ,7 ,11 ,12 ,13 ,14 })); // layout 11
186+ layouts. add(new Layout (new int []{1 ,7 ,15 ,16 ,17 ,18 })); // layout 12
187+
188+ if (isVerbose) {
189+ printLayouts();
190+ }
181191 }
182192
183- void printLayouts () {
193+ private void printLayouts () {
184194 for (int i = 0 ; i < layouts. size(); i++ ) {
185195 println (" Widget Manager:printLayouts: " + layouts. get(i));
186- String layoutString = " " ;
196+ StringBuilder layoutString = new StringBuilder ();
197+
187198 for (int j = 0 ; j < layouts. get(i). myContainers. length; j++ ) {
188- // println("Widget Manager:layoutContainers: " + layouts.get(i).myContainers[j]);
189- layoutString += layouts. get(i). myContainers[j]. x + " , " ;
190- layoutString += layouts. get(i). myContainers[j]. y + " , " ;
191- layoutString += layouts. get(i). myContainers[j]. w + " , " ;
192- layoutString += layouts. get(i). myContainers[j]. h;
199+ layoutString. append(layouts. get(i). myContainers[j]. x). append(" , " );
200+ layoutString. append(layouts. get(i). myContainers[j]. y). append(" , " );
201+ layoutString. append(layouts. get(i). myContainers[j]. w). append(" , " );
202+ layoutString. append(layouts. get(i). myContainers[j]. h);
203+
204+ if (j < layouts. get(i). myContainers. length - 1 ) {
205+ layoutString. append(" | " );
206+ }
193207 }
194- println (" Widget Manager:printLayouts: " + layoutString);
208+ println (" Widget Manager:printLayouts: " + layoutString. toString() );
195209 }
196210 }
197211
198- void setNewContainerLayout (int _newLayout ) {
199-
200- // find out how many active widgets we need...
212+ public void setNewContainerLayout (int _newLayout ) {
213+ // Determine how many widgets are needed for the new layout
201214 int numActiveWidgetsNeeded = layouts. get(_newLayout). myContainers. length;
202- // calculate the number of current active widgets & keep track of which widgets are active
215+
216+ // Count currently active widgets
203217 int numActiveWidgets = 0 ;
204- // ArrayList<int> activeWidgets = new ArrayList<int>();
205- for (int i = 0 ; i < widgets. size(); i++ ) {
206- if (widgets. get(i). getIsActive()) {
207- numActiveWidgets++ ; // increment numActiveWidgets
208- // activeWidgets.add(i); //keep track of the active widget
218+ for (Widget widget : widgets) {
219+ if (widget. getIsActive()) {
220+ numActiveWidgets++ ;
209221 }
210222 }
211223
212- if (numActiveWidgets > numActiveWidgetsNeeded) { // if there are more active widgets than needed
213- // shut some down
224+ if (numActiveWidgets > numActiveWidgetsNeeded) {
225+ // Need to deactivate some widgets
214226 int numToShutDown = numActiveWidgets - numActiveWidgetsNeeded;
215227 int counter = 0 ;
216228 println (" Widget Manager: Powering " + numToShutDown + " widgets down, and remapping." );
217- for (int i = widgets. size()- 1 ; i >= 0 ; i-- ) {
218- if (widgets. get(i). getIsActive() && counter < numToShutDown) {
229+
230+ // Deactivate widgets starting from the end
231+ for (int i = widgets. size()- 1 ; i >= 0 && counter < numToShutDown; i-- ) {
232+ if (widgets. get(i). getIsActive()) {
219233 verbosePrint(" Widget Manager: Deactivating widget [" + i + " ]" );
220234 widgets. get(i). setIsActive(false );
221235 counter++ ;
222236 }
223237 }
224238
225- // and map active widgets
226- counter = 0 ;
227- for (int i = 0 ; i < widgets. size(); i++ ) {
228- if (widgets. get(i). getIsActive()) {
229- widgets. get(i). setContainer(layouts. get(_newLayout). containerInts[counter]);
230- counter++ ;
231- }
232- }
239+ // Map active widgets to containers
240+ mapActiveWidgetsToContainers(_newLayout);
233241
234- } else if (numActiveWidgetsNeeded > numActiveWidgets) { // if there are less active widgets than needed
235- // power some up
242+ } else if (numActiveWidgetsNeeded > numActiveWidgets) {
243+ // Need to activate more widgets
236244 int numToPowerUp = numActiveWidgetsNeeded - numActiveWidgets;
237245 int counter = 0 ;
238246 verbosePrint(" Widget Manager: Powering " + numToPowerUp + " widgets up, and remapping." );
239- for (int i = 0 ; i < widgets. size(); i++ ) {
240- if (! widgets. get(i). getIsActive() && counter < numToPowerUp) {
247+
248+ // Activate widgets from the beginning
249+ for (int i = 0 ; i < widgets. size() && counter < numToPowerUp; i++ ) {
250+ if (! widgets. get(i). getIsActive()) {
241251 verbosePrint(" Widget Manager: Activating widget [" + i + " ]" );
242252 widgets. get(i). setIsActive(true );
243253 counter++ ;
244254 }
245255 }
246256
247- // and map active widgets
248- counter = 0 ;
249- for (int i = 0 ; i < widgets. size(); i++ ) {
250- if (widgets. get(i). getIsActive()) {
251- widgets. get(i). setContainer(layouts. get(_newLayout). containerInts[counter]);
252- // widgets.get(i).screenResized(); // do this to make sure the container is updated
253- counter++ ;
254- }
255- }
257+ // Map active widgets to containers
258+ mapActiveWidgetsToContainers(_newLayout);
256259
257- } else { // if there are the same amount
258- // simply remap active widgets
260+ } else {
261+ // Same number of active widgets as needed, just remap
259262 verbosePrint(" Widget Manager: Remapping widgets." );
260- int counter = 0 ;
261- for (int i = 0 ; i < widgets. size(); i++ ) {
262- if (widgets. get(i). getIsActive()) {
263- widgets. get(i). setContainer(layouts. get(_newLayout). containerInts[counter]);
264- counter++ ;
265- }
263+ mapActiveWidgetsToContainers(_newLayout);
264+ }
265+ }
266+
267+ // Helper method to map active widgets to containers
268+ private void mapActiveWidgetsToContainers (int layoutIndex ) {
269+ int counter = 0 ;
270+ for (Widget widget : widgets) {
271+ if (widget. getIsActive()) {
272+ widget. setContainer(layouts. get(layoutIndex). containerInts[counter]);
273+ counter++ ;
266274 }
267275 }
268276 }
@@ -275,31 +283,28 @@ class WidgetManager {
275283 // Useful in places like TopNav which overlap widget dropdowns
276284 public void lockCp5ObjectsInAllWidgets (boolean lock ) {
277285 for (int i = 0 ; i < widgets. size(); i++ ) {
278- for (int j = 0 ; j < widgets. get(i). cp5_widget. getAll(). size(); j++ ) {
279- ControlP5 cp5Instance = widgets. get(i). cp5_widget;
280- String widgetAddress = cp5Instance. getAll(). get(j). getAddress();
281- cp5Instance. getController(widgetAddress). setLock(lock);
286+ ControlP5 cp5Instance = widgets. get(i). cp5_widget;
287+ List controllerList = cp5Instance. getAll();
288+
289+ for (int j = 0 ; j < controllerList. size(); j++ ) {
290+ controlP5. Controller controller = (controlP5. Controller )controllerList. get(j);
291+ controller. setLock(lock);
282292 }
283293 }
284294 }
285295
286296 public Widget getWidget (String className ) {
287- for (int i = 0 ; i < widgets. size(); i++ ) {
288- Widget widget = widgets. get(i);
289- // Get the class name of the widget
297+ for (Widget widget : widgets) {
290298 String widgetClassName = widget. getClass(). getSimpleName();
291- // Check if it matches the requested class name
292299 if (widgetClassName. equals(className)) {
293300 return widget;
294301 }
295302 }
296- // Return null if no widget of the specified class is found
297303 return null ;
298304 }
299305
300306 public boolean getWidgetExists (String className ) {
301- Widget widget = getWidget(className);
302- return widget != null ;
307+ return getWidget(className) != null ;
303308 }
304309
305310 public W_TimeSeries getTimeSeriesWidget () {
0 commit comments