A node in the 2D [scene graph](https://en.wikipedia.org/wiki/Scene_graph). The bounding box of a Widget is a rectangle specified by `box` relative to their parent. The appearance is defined by overriding `draw()`, and the behavior is defined by overriding `step()` and `on*()` event handlers. */
| 19 | The appearance is defined by overriding `draw()`, and the behavior is defined by overriding `step()` and `on*()` event handlers. |
| 20 | */ |
| 21 | struct Widget : WeakBase { |
| 22 | /** Position relative to parent and size of widget. */ |
| 23 | math::Rect box = math::Rect(math::Vec(), math::Vec(INFINITY, INFINITY)); |
| 24 | /** Automatically set when Widget is added as a child to another Widget */ |
| 25 | Widget* parent = NULL; |
| 26 | std::list<Widget*> children; |
| 27 | /** Disables rendering but allow stepping. |
| 28 | Use isVisible(), setVisible(), show(), or hide() instead of using this variable directly. |
| 29 | */ |
| 30 | bool visible = true; |
| 31 | /** If set to true, parent will delete Widget in the next step(). |
| 32 | Use requestDelete() instead of using this variable directly. |
| 33 | */ |
| 34 | bool requestedDelete = false; |
| 35 | |
| 36 | virtual ~Widget(); |
| 37 | |
| 38 | math::Rect getBox(); |
| 39 | /** Calls setPosition() and then setSize(). */ |
| 40 | void setBox(math::Rect box); |
| 41 | math::Vec getPosition(); |
| 42 | /** Sets position and triggers RepositionEvent if position changed. */ |
| 43 | void setPosition(math::Vec pos); |
| 44 | math::Vec getSize(); |
| 45 | /** Sets size and triggers ResizeEvent if size changed. */ |
| 46 | void setSize(math::Vec size); |
| 47 | widget::Widget* getParent(); |
| 48 | bool isVisible(); |
| 49 | /** Sets `visible` and triggers ShowEvent or HideEvent if changed. */ |
| 50 | void setVisible(bool visible); |
| 51 | /** Makes Widget visible and triggers ShowEvent if changed. */ |
| 52 | void show() { |
| 53 | setVisible(true); |
| 54 | } |
| 55 | /** Makes Widget not visible and triggers HideEvent if changed. */ |
| 56 | void hide() { |
| 57 | setVisible(false); |
| 58 | } |
| 59 | |
| 60 | /** Requests this Widget's parent to delete it in the next step(). */ |
| 61 | void requestDelete(); |
| 62 | |
| 63 | /** Returns the smallest rectangle containing this widget's children (visible and invisible) in its local coordinates. |
| 64 | Returns `Rect(Vec(inf, inf), Vec(-inf, -inf))` if there are no children. |
| 65 | */ |
| 66 | virtual math::Rect getChildrenBoundingBox(); |
| 67 | virtual math::Rect getVisibleChildrenBoundingBox(); |
| 68 | /** Returns whether `ancestor` is a parent or distant parent of this widget. |
| 69 | */ |
| 70 | bool isDescendantOf(Widget* ancestor); |
| 71 | /** Returns `v` (given in local coordinates) transformed into the coordinate system of `ancestor`. |
| 72 | */ |
| 73 | virtual math::Vec getRelativeOffset(math::Vec v, Widget* ancestor); |
| 74 | /** Returns `v` transformed into world/root/global/absolute coordinates. |
| 75 | */ |
| 76 | math::Vec getAbsoluteOffset(math::Vec v) { |
| 77 | return getRelativeOffset(v, NULL); |
| 78 | } |