aboutsummaryrefslogtreecommitdiff
path: root/src/Java/binnie/craftgui/core/Widget.java
blob: 448bcbaed65613893ef131284bb65fc9357bf45d (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package binnie.craftgui.core;

import binnie.craftgui.core.geometry.IArea;
import binnie.craftgui.core.geometry.IPoint;
import binnie.craftgui.core.renderer.Renderer;
import binnie.craftgui.events.Event;
import binnie.craftgui.events.EventHandler;
import binnie.craftgui.events.EventHandler.Origin;
import binnie.craftgui.events.EventWidget.ChangeColour;
import binnie.craftgui.events.EventWidget.ChangeOffset;
import binnie.craftgui.events.EventWidget.ChangePosition;
import binnie.craftgui.events.EventWidget.ChangeSize;
import binnie.craftgui.events.EventWidget.Disable;
import binnie.craftgui.events.EventWidget.Enable;
import binnie.craftgui.events.EventWidget.Hide;
import binnie.craftgui.events.EventWidget.Show;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.List;

public class Widget
  implements IWidget
{
  public Widget(IWidget parent)
  {
    this.parent = parent;
    if (parent != null) {
      parent.addWidget(this);
    }
  }
  
  private IWidget parent = null;
  private List<IWidget> subWidgets = new ArrayList();
  private List<IWidgetAttribute> attributes = new ArrayList();
  
  public List<IWidgetAttribute> getAttributes()
  {
    return this.attributes;
  }
  
  public boolean hasAttribute(IWidgetAttribute attribute)
  {
    return this.attributes.contains(attribute);
  }
  
  public boolean addAttribute(IWidgetAttribute attribute)
  {
    return this.attributes.add(attribute);
  }
  
  public final void deleteChild(IWidget child)
  {
    if (child == null) {
      return;
    }
    child.delete();
    this.subWidgets.remove(child);
  }
  
  public final void deleteAllChildren()
  {
    while (!this.subWidgets.isEmpty()) {
      deleteChild((IWidget)this.subWidgets.get(0));
    }
  }
  
  public final IWidget getParent()
  {
    return this.parent;
  }
  
  public final ITopLevelWidget getSuperParent()
  {
    return isTopLevel() ? (ITopLevelWidget)this : this.parent.getSuperParent();
  }
  
  public final IWidget addWidget(IWidget widget)
  {
    if ((this.subWidgets.size() != 0) && (((IWidget)this.subWidgets.get(this.subWidgets.size() - 1)).hasAttribute(Attribute.AlwaysOnTop))) {
      this.subWidgets.add(this.subWidgets.size() - 1, widget);
    } else {
      this.subWidgets.add(widget);
    }
    onAddChild(widget);
    return widget;
  }
  
  protected void onAddChild(IWidget widget) {}
  
  public final List<IWidget> getWidgets()
  {
    return this.subWidgets;
  }
  
  public final boolean isTopLevel()
  {
    return this instanceof ITopLevelWidget;
  }
  
  private IPoint position = new IPoint(0.0F, 0.0F);
  private IPoint size = new IPoint(0.0F, 0.0F);
  private IPoint offset = new IPoint(0.0F, 0.0F);
  IArea cropArea;
  IWidget cropWidget;
  
  public final IPoint pos()
  {
    return this.position.add(this.offset);
  }
  
  public final IPoint size()
  {
    return this.size;
  }
  
  public final IArea area()
  {
    return getArea();
  }
  
  public final IPoint getPosition()
  {
    return pos();
  }
  
  public final IArea getArea()
  {
    return new IArea(IPoint.ZERO, size());
  }
  
  public final IPoint getOriginalPosition()
  {
    return this.position;
  }
  
  boolean cropped = false;
  
  public IArea getCroppedZone()
  {
    return this.cropArea;
  }
  
  public void setCroppedZone(IWidget relative, IArea area)
  {
    this.cropArea = area;
    this.cropped = true;
    this.cropWidget = relative;
  }
  
  public final IPoint getAbsolutePosition()
  {
    return isTopLevel() ? getPosition() : getParent().getAbsolutePosition().add(getPosition());
  }
  
  public final IPoint getOriginalAbsolutePosition()
  {
    return isTopLevel() ? getOriginalPosition() : getParent().getOriginalPosition().sub(getOriginalPosition());
  }
  
  public final IPoint getSize()
  {
    return size();
  }
  
  public final IPoint getOffset()
  {
    return this.offset;
  }
  
  public final void setPosition(IPoint vector)
  {
    if (!vector.equals(this.position))
    {
      this.position = new IPoint(vector);
      callEvent(new EventWidget.ChangePosition(this));
    }
  }
  
  public final void setSize(IPoint vector)
  {
    if (!vector.equals(this.size))
    {
      this.size = new IPoint(vector);
      callEvent(new EventWidget.ChangeSize(this));
    }
  }
  
  public final void setOffset(IPoint vector)
  {
    if (vector != this.offset)
    {
      this.offset = new IPoint(vector);
      callEvent(new EventWidget.ChangeOffset(this));
    }
  }
  
  int colour = 16777215;
  
  public final void setColour(int colour)
  {
    if (this.colour != colour)
    {
      this.colour = colour;
      callEvent(new EventWidget.ChangeColour(this));
    }
  }
  
  public final int getColour()
  {
    return this.colour;
  }
  
  public boolean canMouseOver()
  {
    return hasAttribute(Attribute.MouseOver);
  }
  
  public boolean canFocus()
  {
    return hasAttribute(Attribute.CanFocus);
  }
  
  private Collection<EventHandler> globalEventHandlers = new ArrayList();
  
  public void addEventHandler(EventHandler handler)
  {
    this.globalEventHandlers.add(handler);
  }
  
  public void addSelfEventHandler(EventHandler handler)
  {
    addEventHandler(handler.setOrigin(EventHandler.Origin.Self, this));
  }
  
  public final void callEvent(Event event)
  {
    getSuperParent().recieveEvent(event);
  }
  
  public final void recieveEvent(Event event)
  {
    for (EventHandler handler : this.globalEventHandlers) {
      if (handler.handles(event)) {
        handler.onEvent(event);
      }
    }
    try
    {
      for (IWidget child : getWidgets()) {
        child.recieveEvent(event);
      }
    }
    catch (ConcurrentModificationException e) {}
  }
  
  public final IPoint getMousePosition()
  {
    return getSuperParent().getAbsoluteMousePosition();
  }
  
  public final IPoint getRelativeMousePosition()
  {
    return isTopLevel() ? getMousePosition() : getParent().getRelativeMousePosition().sub(getPosition());
  }
  
  public boolean isCroppedWidet()
  {
    return this.cropped;
  }
  
  public final IWidget getCropWidget()
  {
    return this.cropWidget == null ? this : this.cropWidget;
  }
  
  public final void render()
  {
    if (isVisible())
    {
      CraftGUI.Render.preRender(this);
      onRender(RenderStage.PreChildren);
      for (IWidget widget : getWidgets()) {
        widget.render();
      }
      for (IWidget widget : getWidgets())
      {
        CraftGUI.Render.preRender(widget);
        widget.onRender(RenderStage.PostSiblings);
        CraftGUI.Render.postRender(widget);
      }
      onRender(RenderStage.PostChildren);
      CraftGUI.Render.postRender(this);
    }
  }
  
  public final void updateClient()
  {
    if (!isVisible()) {
      return;
    }
    if (getSuperParent() == this) {
      ((ITopLevelWidget)this).updateTopLevel();
    }
    onUpdateClient();
    
    List<IWidget> deletedWidgets = new ArrayList();
    for (IWidget widget : getWidgets()) {
      if (widget.hasAttribute(Attribute.NeedsDeletion)) {
        deletedWidgets.add(widget);
      } else {
        widget.updateClient();
      }
    }
    for (IWidget widget : deletedWidgets) {
      deleteChild(widget);
    }
  }
  
  public final boolean calculateIsMouseOver()
  {
    IPoint mouse = getRelativeMousePosition();
    if (!this.cropped) {
      return isMouseOverWidget(mouse);
    }
    IWidget cropRelative = this.cropWidget != null ? this.cropWidget : this;
    IPoint pos = IPoint.sub(cropRelative.getAbsolutePosition(), getAbsolutePosition());
    IPoint size = new IPoint(this.cropArea.size().x(), this.cropArea.size().y());
    boolean inCrop = (mouse.x() > pos.x()) && (mouse.y() > pos.y()) && (mouse.x() < pos.x() + size.x()) && (mouse.y() < pos.y() + size.y());
    
    return (inCrop) && (isMouseOverWidget(mouse));
  }
  
  public boolean isMouseOverWidget(IPoint relativeMouse)
  {
    return getArea().contains(relativeMouse);
  }
  
  private boolean enabled = true;
  private boolean visible = true;
  
  public final void enable()
  {
    this.enabled = true;
    callEvent(new EventWidget.Enable(this));
  }
  
  public final void disable()
  {
    this.enabled = false;
    callEvent(new EventWidget.Disable(this));
  }
  
  public final void show()
  {
    this.visible = true;
    callEvent(new EventWidget.Show(this));
  }
  
  public final void hide()
  {
    this.visible = false;
    callEvent(new EventWidget.Hide(this));
  }
  
  public boolean isEnabled()
  {
    return (this.enabled) && ((isTopLevel()) || ((getParent().isEnabled()) && (getParent().isChildEnabled(this))));
  }
  
  public final boolean isVisible()
  {
    return (this.visible) && ((isTopLevel()) || ((getParent().isVisible()) && (getParent().isChildVisible(this))));
  }
  
  public final boolean isFocused()
  {
    return getSuperParent().isFocused(this);
  }
  
  public final boolean isDragged()
  {
    return getSuperParent().isDragged(this);
  }
  
  public final boolean isMouseOver()
  {
    return getSuperParent().isMouseOver(this);
  }
  
  public boolean isChildVisible(IWidget child)
  {
    return true;
  }
  
  public boolean isChildEnabled(IWidget child)
  {
    return true;
  }
  
  public void onRender(RenderStage stage)
  {
    if (stage == RenderStage.PreChildren) {
      onRenderBackground();
    }
    if (stage == RenderStage.PostChildren) {
      onRenderForeground();
    }
    if (stage == RenderStage.PostSiblings) {
      onRenderOverlay();
    }
  }
  
  public void onRenderBackground() {}
  
  public void onRenderForeground() {}
  
  public void onRenderOverlay() {}
  
  public void onUpdateClient() {}
  
  public final void delete()
  {
    getSuperParent().widgetDeleted(this);
    onDelete();
  }
  
  public void onDelete() {}
  
  public <T> T getWidget(Class<T> x)
  {
    for (IWidget child : getWidgets())
    {
      if (x.isInstance(child)) {
        return child;
      }
      T found = child.getWidget(x);
      if (found != null) {
        return found;
      }
    }
    return null;
  }
  
  public final boolean contains(IPoint position)
  {
    return getArea().contains(position);
  }
  
  public void scheduleDeletion()
  {
    addAttribute(Attribute.NeedsDeletion);
  }
  
  public int getLevel()
  {
    int level = getParent() == null ? 0 : getParent().getLevel();
    int index = getParent() == null ? 0 : getParent().getWidgets().indexOf(this);
    return level + index;
  }
  
  public boolean isDescendant(IWidget widget)
  {
    IWidget clss = this;
    do
    {
      if (clss == widget) {
        return true;
      }
      clss = clss.getParent();
    } while (clss != null);
    return false;
  }
  
  public float x()
  {
    return pos().x();
  }
  
  public float y()
  {
    return pos().y();
  }
  
  public float w()
  {
    return size().x();
  }
  
  public float h()
  {
    return size().y();
  }
  
  public IWidget getWidget()
  {
    return this;
  }
}