[66] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * @file gui_element.hh
|
---|
| 9 | * @author Kornel Kisielewicz
|
---|
| 10 | * @brief GUI Element
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #ifndef NV_GUI_ELEMENT_HH
|
---|
| 14 | #define NV_GUI_ELEMENT_HH
|
---|
| 15 |
|
---|
| 16 | #include <nv/object.hh>
|
---|
| 17 | #include <nv/position.hh>
|
---|
[69] | 18 | #include <nv/io_event.hh>
|
---|
| 19 | #include <nv/gui/gui_common.hh>
|
---|
[66] | 20 |
|
---|
| 21 | namespace nv
|
---|
| 22 | {
|
---|
| 23 | namespace gui
|
---|
| 24 | {
|
---|
| 25 | class element : public object
|
---|
| 26 | {
|
---|
[257] | 27 | protected:
|
---|
[110] | 28 |
|
---|
| 29 | /**
|
---|
[132] | 30 | * Creates a new GUI element with the give root node and postioning data.
|
---|
[110] | 31 | *
|
---|
[132] | 32 | * @param r The rectangle representing the position and size of this element.
|
---|
[110] | 33 | */
|
---|
[257] | 34 | explicit element( const rectangle& r );
|
---|
[110] | 35 |
|
---|
[257] | 36 | public:
|
---|
| 37 |
|
---|
[110] | 38 | /**
|
---|
[132] | 39 | * Gets the deepest child element that contains the indicated point.
|
---|
[110] | 40 | *
|
---|
[132] | 41 | * @param p The position to check.
|
---|
| 42 | * @returns The last child element that contains the given point.
|
---|
[110] | 43 | */
|
---|
[66] | 44 | element* get_element( const position& p );
|
---|
[110] | 45 |
|
---|
| 46 | /**
|
---|
[257] | 47 | * Event handler for update events.
|
---|
[110] | 48 | *
|
---|
[132] | 49 | * @param elapsed Time since last update.
|
---|
[110] | 50 | */
|
---|
[257] | 51 | virtual void on_update( uint32 /*elapsed*/ ) {}
|
---|
[110] | 52 |
|
---|
| 53 | /**
|
---|
[257] | 54 | * Event handler for draw events.
|
---|
[110] | 55 | */
|
---|
[257] | 56 | virtual void on_draw() {}
|
---|
[110] | 57 |
|
---|
| 58 | /**
|
---|
[132] | 59 | * Event handler for IO events. Calls on_event for all affected children.
|
---|
[110] | 60 | *
|
---|
[132] | 61 | * @param event The event data.
|
---|
| 62 | * @returns ???
|
---|
[110] | 63 | */
|
---|
[69] | 64 | virtual bool on_event( const io_event& event );
|
---|
[110] | 65 |
|
---|
| 66 | /**
|
---|
[132] | 67 | * Checks if this element contains the given point.
|
---|
[110] | 68 | *
|
---|
[132] | 69 | * @param p The point to check.
|
---|
| 70 | * @returns True if the point is within the region of the element, false otherwise.
|
---|
[110] | 71 | */
|
---|
[69] | 72 | virtual bool contains( const position& p ) const;
|
---|
[110] | 73 |
|
---|
| 74 | /**
|
---|
[132] | 75 | * Sets the position and size of this element relative to its parent.
|
---|
[110] | 76 | *
|
---|
[132] | 77 | * @param r The new position and size of the element.
|
---|
[110] | 78 | */
|
---|
[66] | 79 | virtual void set_relative( const rectangle& r );
|
---|
[110] | 80 |
|
---|
| 81 | /**
|
---|
[132] | 82 | * Sets the position of this element relative to its parent.
|
---|
[110] | 83 | *
|
---|
[132] | 84 | * @param p The new position of the element.
|
---|
[110] | 85 | */
|
---|
[66] | 86 | virtual void set_relative( const position& p );
|
---|
[110] | 87 |
|
---|
| 88 | /**
|
---|
[132] | 89 | * Gets the position and size of the element relative to its parent.
|
---|
[110] | 90 | *
|
---|
[132] | 91 | * @returns The element's position and size relative to its parent.
|
---|
[110] | 92 | */
|
---|
[66] | 93 | virtual const rectangle& get_relative() const { return m_relative; }
|
---|
[110] | 94 |
|
---|
| 95 | /**
|
---|
[132] | 96 | * Gets the position and size of the element relative to the root (window).
|
---|
[110] | 97 | *
|
---|
[132] | 98 | * @returns The element's position and size relative to the root (window).
|
---|
[110] | 99 | */
|
---|
[66] | 100 | virtual const rectangle& get_absolute() const { return m_absolute; }
|
---|
[110] | 101 |
|
---|
| 102 | /**
|
---|
[132] | 103 | * Gets the parent element of this element.
|
---|
[110] | 104 | *
|
---|
[132] | 105 | * @returns The parent element.
|
---|
[110] | 106 | */
|
---|
[66] | 107 | virtual element* get_parent() const { return (element*)m_parent; }
|
---|
[110] | 108 |
|
---|
| 109 | /**
|
---|
[132] | 110 | * Gets whether this element is currently accepting events.
|
---|
[110] | 111 | *
|
---|
[132] | 112 | * @returns True if the element is receiving events, false otherwise.
|
---|
[110] | 113 | */
|
---|
[66] | 114 | virtual bool is_enabled() const { return m_enabled; }
|
---|
[110] | 115 |
|
---|
| 116 | /**
|
---|
[132] | 117 | * Gets whether this element is currently visible.
|
---|
[110] | 118 | *
|
---|
[132] | 119 | * @returns True if the element is visible, false otherwise.
|
---|
[110] | 120 | */
|
---|
[66] | 121 | virtual bool is_visible() const { return m_visible; }
|
---|
[110] | 122 |
|
---|
| 123 | /**
|
---|
[132] | 124 | * Gets whether this element needs to be redrawn.
|
---|
[110] | 125 | *
|
---|
[132] | 126 | * @returns True if the element needs to be redrawn, false if not.
|
---|
[110] | 127 | */
|
---|
[69] | 128 | virtual bool is_dirty() const { return m_dirty; }
|
---|
[110] | 129 |
|
---|
| 130 | /**
|
---|
[132] | 131 | * Sets whether this element is currently accepting events.
|
---|
[110] | 132 | *
|
---|
[132] | 133 | * @param value True to allow the element and its children to receive events, false to disable.
|
---|
[110] | 134 | */
|
---|
[66] | 135 | virtual void set_enabled( bool value ) { m_enabled = value; }
|
---|
[110] | 136 |
|
---|
| 137 | /**
|
---|
[132] | 138 | * Sets whether this element is visible on the screen.
|
---|
[110] | 139 | *
|
---|
[132] | 140 | * @param value True to display the element and its children, false to hide it and its children.
|
---|
[110] | 141 | */
|
---|
[66] | 142 | virtual void set_visible( bool value ) { m_visible = value; }
|
---|
[110] | 143 |
|
---|
| 144 | /**
|
---|
[132] | 145 | * Sets whether this element needs to be redrawn.
|
---|
[110] | 146 | *
|
---|
[132] | 147 | * @param value True to request that the element and its children be redrawn, false if not.
|
---|
[110] | 148 | */
|
---|
[69] | 149 | virtual void set_dirty( bool value ) { m_dirty = value; }
|
---|
[110] | 150 |
|
---|
| 151 | /**
|
---|
[132] | 152 | * Gets the text associated with this element.
|
---|
[110] | 153 | *
|
---|
[132] | 154 | * @returns A string containing the associated text.
|
---|
[110] | 155 | */
|
---|
[69] | 156 | virtual const string& get_text() const { return m_text; }
|
---|
[110] | 157 |
|
---|
| 158 | /**
|
---|
[132] | 159 | * Sets the text associated with this element.
|
---|
[110] | 160 | *
|
---|
[132] | 161 | * @param text The new text to associate with this element.
|
---|
[110] | 162 | */
|
---|
[69] | 163 | virtual void set_text( const string& text ) { m_text = text; m_dirty = true; }
|
---|
[110] | 164 |
|
---|
| 165 | /**
|
---|
[132] | 166 | * Gets the class name associated with this element.
|
---|
[110] | 167 | *
|
---|
[132] | 168 | * @returns A string containing the class name of this element.
|
---|
[110] | 169 | */
|
---|
[69] | 170 | virtual const string& get_class() const { return m_class; }
|
---|
[110] | 171 |
|
---|
| 172 | /**
|
---|
[132] | 173 | * sets the class name associated with this element.
|
---|
[110] | 174 | *
|
---|
[132] | 175 | * @param class_ The new class name.
|
---|
[110] | 176 | */
|
---|
[69] | 177 | virtual void set_class( const string& class_ ) { m_class = class_; m_dirty = true; }
|
---|
[110] | 178 |
|
---|
| 179 | /**
|
---|
[132] | 180 | * Recalcuates the absolute position of the element and the element's children.
|
---|
[110] | 181 | */
|
---|
[66] | 182 | virtual void recalculate_absolute();
|
---|
[110] | 183 |
|
---|
| 184 | /**
|
---|
[132] | 185 | * Recalculates the aboslute position of the element's children.
|
---|
[110] | 186 | */
|
---|
[99] | 187 | virtual void recalculate_absolute_children();
|
---|
[110] | 188 |
|
---|
[266] | 189 | /**
|
---|
| 190 | * Moves element to back of child list (top of stack)
|
---|
| 191 | *
|
---|
| 192 | * @returns true if object found, false otherwise
|
---|
| 193 | */
|
---|
| 194 | bool move_to_top( element* child );
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | * Moves element to front of child list (bottom of stack)
|
---|
| 198 | *
|
---|
| 199 | * @returns true if object found, false otherwise
|
---|
| 200 | */
|
---|
| 201 | bool move_to_bottom( element* child );
|
---|
| 202 |
|
---|
[257] | 203 | protected:
|
---|
[110] | 204 | /**
|
---|
[132] | 205 | * Destroys the element.
|
---|
[110] | 206 | */
|
---|
[99] | 207 | virtual ~element();
|
---|
[66] | 208 | protected:
|
---|
[99] | 209 | friend class environment;
|
---|
[126] | 210 | friend class renderer;
|
---|
[99] | 211 |
|
---|
[110] | 212 | string m_class; ///< Class name.
|
---|
| 213 | string m_text; ///< Displayed label or text.
|
---|
| 214 | rectangle m_relative; ///< Position relative to parent.
|
---|
| 215 | rectangle m_absolute; ///< Position relative to window/screen.
|
---|
| 216 | bool m_enabled; ///< Whether the element accepts events.
|
---|
| 217 | bool m_visible; ///< Whether the element is drawn.
|
---|
| 218 | bool m_dirty; ///< Whether the element needs updating.
|
---|
| 219 | render_data* m_render_data; ///< -?-
|
---|
[66] | 220 | };
|
---|
| 221 |
|
---|
| 222 | } // namespace gui
|
---|
| 223 |
|
---|
| 224 | } // namespace nv
|
---|
| 225 |
|
---|
| 226 | #endif // NV_GUI_ELEMENT_HH
|
---|