Changeset 268
- Timestamp:
- 06/19/14 20:22:54 (11 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/gui/gui_element.hh
r267 r268 14 14 #define NV_GUI_ELEMENT_HH 15 15 16 #include <nv/ object.hh>16 #include <nv/common.hh> 17 17 #include <nv/position.hh> 18 18 #include <nv/io_event.hh> … … 23 23 namespace gui 24 24 { 25 class element : public object25 class element 26 26 { 27 27 protected: 28 /// List type 29 typedef std::list<element*> list; 28 30 29 /** 30 * Creates a new GUI element with the give root node and positioning data. 31 * 32 * @param r The rectangle representing the position and size of this element. 33 */ 31 34 32 explicit element( const rectangle& r ) 35 : object( "" ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true ), m_render_data( nullptr ) {} 36 37 public: 38 39 /** 40 * Checks if this element contains the given point. 41 * 42 * @param p The point to check. 43 * @returns True if the point is within the region of the element, false otherwise. 44 */ 45 virtual bool contains( const position& p ) const 46 { 47 return m_absolute.contains( p ); 48 } 49 50 /** 51 * Gets the position and size of the element relative to its parent. 52 * 53 * @returns The element's position and size relative to its parent. 54 */ 55 virtual const rectangle& get_relative() const { return m_relative; } 56 57 /** 58 * Gets the position and size of the element relative to the root (window). 59 * 60 * @returns The element's position and size relative to the root (window). 61 */ 62 virtual const rectangle& get_absolute() const { return m_absolute; } 63 64 /** 65 * Gets whether this element is currently accepting events. 66 * 67 * @returns True if the element is receiving events, false otherwise. 68 */ 69 virtual bool is_enabled() const { return m_enabled; } 70 71 /** 72 * Gets whether this element is currently visible. 73 * 74 * @returns True if the element is visible, false otherwise. 75 */ 76 virtual bool is_visible() const { return m_visible; } 77 78 /** 79 * Gets whether this element needs to be redrawn. 80 * 81 * @returns True if the element needs to be redrawn, false if not. 82 */ 83 virtual bool is_dirty() const { return m_dirty; } 84 85 /** 86 * Sets whether this element is currently accepting events. 87 * 88 * @param value True to allow the element and its children to receive events, false to disable. 89 */ 90 virtual void set_enabled( bool value ) { m_enabled = value; } 91 92 /** 93 * Sets whether this element is visible on the screen. 94 * 95 * @param value True to display the element and its children, false to hide it and its children. 96 */ 97 virtual void set_visible( bool value ) { m_visible = value; } 98 99 /** 100 * Sets whether this element needs to be redrawn. 101 * 102 * @param value True to request that the element and its children be redrawn, false if not. 103 */ 104 virtual void set_dirty( bool value ) { m_dirty = value; } 105 106 /** 107 * Gets the text associated with this element. 108 * 109 * @returns A string containing the associated text. 110 */ 111 virtual const string& get_text() const { return m_text; } 112 113 /** 114 * Sets the text associated with this element. 115 * 116 * @param text The new text to associate with this element. 117 */ 118 virtual void set_text( const string& text ) { m_text = text; m_dirty = true; } 119 120 /** 121 * Gets the class name associated with this element. 122 * 123 * @returns A string containing the class name of this element. 124 */ 125 virtual const string& get_class() const { return m_class; } 126 127 /** 128 * sets the class name associated with this element. 129 * 130 * @param class_ The new class name. 131 */ 132 virtual void set_class( const string& class_ ) { m_class = class_; m_dirty = true; } 33 : m_id( "" ) 34 , m_parent( nullptr ) 35 , m_children() 36 , m_child_count(0) 37 , m_class("") 38 , m_relative( r ) 39 , m_absolute( r ) 40 , m_enabled( true ) 41 , m_visible( true ) 42 , m_dirty( true ) 43 , m_render_data( nullptr ) {} 133 44 134 45 protected: 135 /**136 * Destroys the element.137 */138 46 virtual ~element() { delete m_render_data; } 139 47 protected: 140 48 friend class environment; 141 49 friend class renderer; 50 friend class style; 142 51 52 string m_id; ///< id type of the object 53 element* m_parent; ///< pointer to parent 54 list m_children; ///< children objects 55 size_t m_child_count; ///< number of children 143 56 string m_class; ///< Class name. 144 57 string m_text; ///< Displayed label or text. -
trunk/nv/gui/gui_environment.hh
r267 r268 26 26 namespace gui 27 27 { 28 class handle 29 { 30 31 }; 28 32 29 33 class environment … … 34 38 void load_style( const std::string& filename ); 35 39 element* create_element( element* parent, const rectangle& r ); 40 void set_text( element* e, const string& text ); 41 void set_class( element* e, const string& text ); 36 42 void update(); 37 43 void draw(); … … 42 48 element* get_element( const position& p ); 43 49 void add_child( element* child ); 50 void add_child( element* parent, element* child ); 51 void remove_child( element* parent, element* child ); 44 52 void destroy_children( element* e ); 45 53 void update( element* e, uint32 elapsed ); -
trunk/src/gui/gui_environment.cc
r267 r268 42 42 element* result = new element( r ); 43 43 if ( parent == nullptr ) parent = m_screen; 44 parent->add_child(result );44 add_child( parent, result ); 45 45 return result; 46 46 } … … 49 49 { 50 50 destroy_children( e ); 51 e->detach(); 51 if ( e->m_parent ) 52 { 53 remove_child( e->m_parent, e ); 54 } 52 55 delete e; 53 56 } … … 56 59 { 57 60 // e->on_update( elapsed ); 58 if ( e-> is_visible())59 { 60 for ( object* i : *e)61 { 62 update( ((element*)i), elapsed );63 } 64 } 65 if ( e-> is_dirty()|| e->m_render_data == nullptr )61 if ( e->m_visible ) 62 { 63 for ( element* i : e->m_children ) 64 { 65 update( i, elapsed ); 66 } 67 } 68 if ( e->m_dirty || e->m_render_data == nullptr ) 66 69 { 67 70 m_renderer->redraw( e, elapsed ); 68 e-> set_dirty( false );71 e->m_dirty = false; 69 72 } 70 73 } … … 72 75 void nv::gui::environment::draw( element* e ) 73 76 { 74 if ( e-> is_visible())77 if ( e->m_visible ) 75 78 { 76 79 // e->on_draw(); 77 80 m_renderer->draw( e ); 78 for ( object* i : *e)79 { 80 draw( (element*)i);81 for ( element* i : e->m_children ) 82 { 83 draw(i); 81 84 } 82 85 } … … 96 99 void nv::gui::environment::add_child( element* child ) 97 100 { 98 m_screen->add_child( child ); 101 add_child( m_screen, child ); 102 } 103 104 void nv::gui::environment::add_child( element* parent, element* child ) 105 { 106 if ( child ) 107 { 108 if ( child->m_parent ) 109 { 110 remove_child( child->m_parent, child ); 111 } 112 child->m_parent = parent; 113 parent->m_children.push_back( child ); 114 parent->m_child_count++; 115 } 99 116 } 100 117 … … 103 120 while ( !e->m_children.empty() ) 104 121 { 105 destroy_element( (element*)e->m_children.front() );122 destroy_element( e->m_children.front() ); 106 123 } 107 124 } … … 121 138 bool nv::gui::environment::process_io_event( element* e, const io_event& ev ) 122 139 { 123 return e->m_parent ? process_io_event( (element*)e->m_parent, ev ) : false;140 return e->m_parent ? process_io_event( e->m_parent, ev ) : false; 124 141 } 125 142 … … 131 148 nv::gui::element* nv::gui::environment::get_deepest_child( element* e, const position& p ) 132 149 { 133 if ( !e-> is_visible()) return nullptr;150 if ( !e->m_visible ) return nullptr; 134 151 135 152 element* result = nullptr; … … 138 155 while ( it != e->m_children.rend() ) 139 156 { 140 result = get_deepest_child( (element*)(*it), p );157 result = get_deepest_child( *it, p ); 141 158 if ( result ) return result; 142 159 ++it; 143 160 } 144 161 145 if ( e-> contains( p) ) return e;162 if ( e->m_absolute.contains(p) ) return e; 146 163 return nullptr; 147 164 } … … 149 166 void nv::gui::environment::move_to_top( element* child ) 150 167 { 151 element* parent = (element*)child->m_parent;168 element* parent = child->m_parent; 152 169 if ( parent ) 153 170 { 154 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child );171 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), child ); 155 172 if ( it != parent->m_children.end() ) 156 173 { … … 164 181 void nv::gui::environment::move_to_bottom( element* child ) 165 182 { 166 element* parent = (element*)child->m_parent;183 element* parent = child->m_parent; 167 184 if ( parent ) 168 185 { 169 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child );186 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), child ); 170 187 if ( it != parent->m_children.end() ) 171 188 { … … 195 212 if ( e->m_parent ) 196 213 { 197 pabsolute = ((element*)(e->m_parent))->m_absolute;214 pabsolute = e->m_parent->m_absolute; 198 215 } 199 216 200 217 e->m_absolute = e->m_relative + pabsolute.ul; 201 218 202 for ( object* o : e->m_children ) 203 { 204 recalculate_absolute( ((element*)o) ); 205 } 206 } 207 219 for ( element* o : e->m_children ) 220 { 221 recalculate_absolute( o ); 222 } 223 } 224 225 void nv::gui::environment::set_class( element* e, const string& text ) 226 { 227 e->m_class = text; 228 e->m_dirty = true; 229 } 230 231 void nv::gui::environment::set_text( element* e, const string& text ) 232 { 233 e->m_text = text; 234 e->m_dirty = true; 235 } 236 237 void nv::gui::environment::remove_child( element* parent, element* child ) 238 { 239 if ( child->m_parent != parent ) 240 { 241 return; // signal error? 242 } 243 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), child ); 244 if ( it != parent->m_children.end() ) 245 { 246 (*it)->m_parent = nullptr; 247 parent->m_children.erase(it); 248 } 249 250 } 251 -
trunk/src/gui/gui_renderer.cc
r267 r268 198 198 199 199 qvec.clear(); 200 rectangle abs = e-> get_absolute();201 if ( e-> get_absolute()!= m_area )200 rectangle abs = e->m_absolute; 201 if ( e->m_absolute != m_area ) 202 202 { 203 203 int border; … … 220 220 } 221 221 222 text = e-> get_text();222 text = e->m_text; 223 223 if ( !text.empty() ) 224 224 { -
trunk/src/gui/gui_style.cc
r204 r268 67 67 { 68 68 const char* centry = entry.c_str(); 69 const char* cid = e-> get_id().c_str();70 const char* cclass = e-> get_class().c_str();69 const char* cid = e->m_id.c_str(); 70 const char* cclass = e->m_class.c_str(); 71 71 lua_getglobal( m_lua, "default" ); 72 72 int global = lua_gettop( m_lua ); -
trunk/tests/gui_test/nv_gui_test.cc
r267 r268 87 87 glm::ivec2 b( std::rand() % 200 + 40, std::rand() % 200 + 40 ); 88 88 nv::gui::element* e = m_guienv->create_element( nullptr, nv::rectangle(a).dim(b) ); 89 e->set_class("window" );90 e->set_text("Window "+nv::to_string(m_windows.size()+1) );89 m_guienv->set_class( e, "window" ); 90 m_guienv->set_text( e, "Window "+nv::to_string(m_windows.size()+1) ); 91 91 NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" ); 92 92 m_windows.push_back( e );
Note: See TracChangeset
for help on using the changeset viewer.