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