Changeset 267
- Timestamp:
- 06/19/14 19:03:25 (11 years ago)
- Location:
- trunk
- Files:
-
- 1 deleted
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/gui/gui_common.hh
r108 r267 14 14 #define NV_GUI_COMMON_HH 15 15 16 #include <nv/ object.hh>16 #include <nv/common.hh> 17 17 18 18 namespace nv -
trunk/nv/gui/gui_element.hh
r266 r267 28 28 29 29 /** 30 * Creates a new GUI element with the give root node and pos tioning data.30 * Creates a new GUI element with the give root node and positioning data. 31 31 * 32 32 * @param r The rectangle representing the position and size of this element. 33 33 */ 34 explicit element( const rectangle& r ); 34 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 ) {} 35 36 36 37 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 38 66 39 /** … … 70 43 * @returns True if the point is within the region of the element, false otherwise. 71 44 */ 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 ); 45 virtual bool contains( const position& p ) const 46 { 47 return m_absolute.contains( p ); 48 } 87 49 88 50 /** … … 99 61 */ 100 62 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 63 109 64 /** … … 177 132 virtual void set_class( const string& class_ ) { m_class = class_; m_dirty = true; } 178 133 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 /**190 * Moves element to back of child list (top of stack)191 *192 * @returns true if object found, false otherwise193 */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 otherwise200 */201 bool move_to_bottom( element* child );202 203 134 protected: 204 135 /** 205 136 * Destroys the element. 206 137 */ 207 virtual ~element() ;138 virtual ~element() { delete m_render_data; } 208 139 protected: 209 140 friend class environment; -
trunk/nv/gui/gui_environment.hh
r264 r267 17 17 #include <nv/gui/gui_style.hh> 18 18 #include <nv/lua/lua_state.hh> 19 #include <nv/io_event.hh> 19 20 #include <nv/interface/window.hh> 20 #include <nv/ root.hh>21 #include <nv/array.hh> 21 22 22 23 namespace nv … … 25 26 namespace gui 26 27 { 27 class screen : public element28 {29 public:30 screen( const rectangle& r ) : element( r ) {}31 virtual bool on_event( const io_event& ) { return false; }32 virtual void recalculate_absolute() { recalculate_absolute_children(); }33 };34 28 35 29 class environment … … 40 34 void load_style( const std::string& filename ); 41 35 element* create_element( element* parent, const rectangle& r ); 36 void update(); 37 void draw(); 38 void destroy_element( element* e ); 39 bool process_io_event( const io_event& ev ); 40 virtual ~environment(); 41 protected: 42 element* get_element( const position& p ); 43 void add_child( element* child ); 44 void destroy_children( element* e ); 42 45 void update( element* e, uint32 elapsed ); 43 46 void draw( element* e ); 44 void update();45 void draw();46 void add_child( element* child );47 void destroy_element( element* e);48 void destroy_children( element* e);49 v irtual ~environment();50 protected:51 47 bool process_io_event( element* e, const io_event& ev ); 48 element* get_deepest_child( element* e, const position& p ); 49 void move_to_top( element* child ); 50 void move_to_bottom( element* child ); 51 void set_relative( element* e, const rectangle& r ); 52 void set_relative( element* e, const position& p ); 53 void recalculate_absolute( element* e ); 54 52 55 renderer* m_renderer; 53 56 window* m_window; 54 screen*m_screen;57 element* m_screen; 55 58 rectangle m_area; 56 59 }; -
trunk/nv/gui/gui_renderer.hh
r234 r267 22 22 #include <nv/interface/device.hh> 23 23 #include <nv/interface/context.hh> 24 #include <nv/interface/camera.hh> 24 25 #include <nv/gfx/sliced_buffer.hh> 25 26 #include <nv/gfx/texture_atlas.hh> … … 68 69 bool m_reupload; 69 70 71 scene_state m_scene_state; 70 72 render_state m_render_state; 71 73 render_data* m_render_data; -
trunk/nv/interface/camera.hh
r235 r267 33 33 { 34 34 m_projection = glm::perspective( fov, aspect, near, far ); 35 } 36 void set_ortho( f32 left, f32 right, f32 bottom, f32 top, f32 near = -1.0f, f32 far = 1.0f ) 37 { 38 m_projection = glm::ortho( left, right, bottom, top, near, far ); 35 39 } 36 40 const mat4& get_projection() const -
trunk/src/gui/gui_environment.cc
r264 r267 25 25 #include "nv/gfx/texture_atlas.hh" 26 26 27 #include <vector> 28 29 using namespace nv; 30 using namespace nv::gui; 31 32 environment::environment( window* w, const std::string& shader_path ) 27 nv::gui::environment::environment( window* w, const std::string& shader_path ) 33 28 : m_renderer( nullptr ), m_window( w ), m_screen( nullptr ) 34 29 { 35 30 m_area.dim( dimension( w->get_width(), w->get_height() ) ); 36 m_screen = new screen( m_area );31 m_screen = new element( m_area ); 37 32 m_renderer = new renderer( w, shader_path ); 38 33 } … … 43 38 } 44 39 45 element* nv::gui::environment::create_element( element* parent, const rectangle& r )40 nv::gui::element* nv::gui::environment::create_element( element* parent, const rectangle& r ) 46 41 { 47 42 element* result = new element( r ); … … 58 53 } 59 54 60 void environment::update( element* e, uint32 elapsed )61 { 62 e->on_update( elapsed );55 void nv::gui::environment::update( element* e, uint32 elapsed ) 56 { 57 // e->on_update( elapsed ); 63 58 if ( e->is_visible() ) 64 59 { … … 75 70 } 76 71 77 void environment::draw( element* e )72 void nv::gui::environment::draw( element* e ) 78 73 { 79 74 if ( e->is_visible() ) 80 75 { 81 e->on_draw();76 // e->on_draw(); 82 77 m_renderer->draw( e ); 83 78 for ( object* i : *e ) … … 88 83 } 89 84 90 void environment::update()85 void nv::gui::environment::update() 91 86 { 92 87 update( m_screen, 0 ); 93 88 } 94 89 95 void environment::draw()90 void nv::gui::environment::draw() 96 91 { 97 92 draw( m_screen ); … … 99 94 } 100 95 101 void environment::add_child( element* child )96 void nv::gui::environment::add_child( element* child ) 102 97 { 103 98 m_screen->add_child( child ); 104 99 } 105 100 106 void environment::destroy_children( element* e )101 void nv::gui::environment::destroy_children( element* e ) 107 102 { 108 103 while ( !e->m_children.empty() ) … … 113 108 114 109 115 environment::~environment()110 nv::gui::environment::~environment() 116 111 { 117 112 destroy_element( m_screen ); … … 119 114 } 120 115 116 bool nv::gui::environment::process_io_event( const io_event& ) 117 { 118 return false; 119 } 120 121 bool nv::gui::environment::process_io_event( element* e, const io_event& ev ) 122 { 123 return e->m_parent ? process_io_event( (element*)e->m_parent, ev ) : false; 124 } 125 126 nv::gui::element* nv::gui::environment::get_element( const position& p ) 127 { 128 return get_deepest_child( m_screen, p ); 129 } 130 131 nv::gui::element* nv::gui::environment::get_deepest_child( element* e, const position& p ) 132 { 133 if ( !e->is_visible() ) return nullptr; 134 135 element* result = nullptr; 136 element::list::reverse_iterator it = e->m_children.rbegin(); 137 138 while ( it != e->m_children.rend() ) 139 { 140 result = get_deepest_child( (element*)(*it), p ); 141 if ( result ) return result; 142 ++it; 143 } 144 145 if ( e->contains( p ) ) return e; 146 return nullptr; 147 } 148 149 void nv::gui::environment::move_to_top( element* child ) 150 { 151 element* parent = (element*)child->m_parent; 152 if ( parent ) 153 { 154 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child ); 155 if ( it != parent->m_children.end() ) 156 { 157 parent->m_children.erase( it ); 158 parent->m_children.push_back( child ); 159 parent->m_dirty = true; 160 } 161 } 162 } 163 164 void nv::gui::environment::move_to_bottom( element* child ) 165 { 166 element* parent = (element*)child->m_parent; 167 if ( parent ) 168 { 169 auto it = std::find( parent->m_children.begin(), parent->m_children.end(), (object*)child ); 170 if ( it != parent->m_children.end() ) 171 { 172 parent->m_children.erase( it ); 173 parent->m_children.push_front( child ); 174 parent->m_dirty = true; 175 } 176 } 177 } 178 179 void nv::gui::environment::set_relative( element* e, const rectangle& r ) 180 { 181 e->m_dirty = true; 182 e->m_relative = r; 183 recalculate_absolute( e ); 184 } 185 186 void nv::gui::environment::set_relative( element* e, const position& p ) 187 { 188 set_relative( e, rectangle( p, p + e->m_relative.get_size() ) ); 189 } 190 191 void nv::gui::environment::recalculate_absolute( element* e ) 192 { 193 rectangle pabsolute; 194 195 if ( e->m_parent ) 196 { 197 pabsolute = ((element*)(e->m_parent))->m_absolute; 198 } 199 200 e->m_absolute = e->m_relative + pabsolute.ul; 201 202 for ( object* o : e->m_children ) 203 { 204 recalculate_absolute( ((element*)o) ); 205 } 206 } 207 -
trunk/src/gui/gui_renderer.cc
r234 r267 112 112 sr->varray = m_window->get_device()->create_vertex_array(); 113 113 sr->shader = m_window->get_device()->create_program( nv::slurp( shader_path + ".vert" ), nv::slurp( shader_path + ".frag" ) ); 114 sr->shader->set_uniform( "tex", 0 ); 115 glm::mat4 projection = glm::ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f, -1.0f, 1.0f ); 116 sr->shader->set_uniform( "nv_projection", projection ); 114 m_scene_state.get_camera().set_ortho( 0.0f, float( m_window->get_width() ), float( m_window->get_height() ), 0.0f ); 117 115 118 116 vertex_buffer* vb = (vertex_buffer*)sr->buffer.get_buffer(); … … 274 272 sr->varray->update_vertex_buffer( nv::COLOR, vb, false ); 275 273 } 276 sr->texture->bind( 0 ); 277 sr->shader->set_uniform( "tex", 0 ); 278 m_window->get_context()->draw( TRIANGLES, m_render_state, sr->shader, sr->varray, sr->buffer.get_size() * 6 ); 274 sr->texture->bind( nv::TEX_DIFFUSE ); 275 m_window->get_context()->draw( TRIANGLES, m_render_state, m_scene_state, sr->shader, sr->varray, sr->buffer.get_size() * 6 ); 279 276 } 280 277 -
trunk/tests/gui_test/gui.frag
r161 r267 2 2 varying vec4 v_color; 3 3 varying vec2 v_texcoord; 4 uniform sampler2D tex;4 uniform sampler2D nv_t_diffuse; 5 5 6 6 void main(void) { 7 vec4 texture = texture2D( tex,v_texcoord);7 vec4 texture = texture2D(nv_t_diffuse,v_texcoord); 8 8 gl_FragColor = v_color * texture; 9 9 } -
trunk/tests/gui_test/gui.vert
r161 r267 3 3 attribute vec2 nv_texcoord; 4 4 attribute vec4 nv_color; 5 uniform mat4 nv_ projection;5 uniform mat4 nv_m_projection; 6 6 varying vec4 v_color; 7 7 varying vec2 v_texcoord; … … 10 10 v_color = nv_color; 11 11 v_texcoord = nv_texcoord; 12 gl_Position = nv_ projection * vec4( nv_position.x, nv_position.y, 0.0, 1.0 );12 gl_Position = nv_m_projection * vec4( nv_position.x, nv_position.y, 0.0, 1.0 ); 13 13 } -
trunk/tests/gui_test/nv_gui_test.cc
r239 r267 16 16 void spawn_window(); 17 17 void recolor_window(); 18 // void bring_to_front( int i ); 18 19 ~application(); 19 20 protected: … … 64 65 switch (event.key.code) 65 66 { 67 // case nv::KEY_1 : bring_to_front(0); break; 68 // case nv::KEY_2 : bring_to_front(1); break; 69 // case nv::KEY_3 : bring_to_front(2); break; 70 // case nv::KEY_4 : bring_to_front(3); break; 71 // case nv::KEY_5 : bring_to_front(4); break; 66 72 case nv::KEY_N : spawn_window(); break; 67 73 case nv::KEY_K : kill_window(); break; … … 80 86 glm::ivec2 a( std::rand() % 600, std::rand() % 400 ); 81 87 glm::ivec2 b( std::rand() % 200 + 40, std::rand() % 200 + 40 ); 82 nv::gui::element* e = new nv::gui::element( m_guienv, nv::rectangle(a).dim(b) );88 nv::gui::element* e = m_guienv->create_element( nullptr, nv::rectangle(a).dim(b) ); 83 89 e->set_class( "window" ); 84 90 e->set_text("Window "+nv::to_string(m_windows.size()+1) ); 85 m_guienv->add_child( e );86 91 NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" ); 87 92 m_windows.push_back( e ); … … 92 97 if ( m_windows.size() == 0 ) return; 93 98 size_t index = rand() % m_windows.size(); 94 delete m_windows[ index ];99 m_guienv->destroy_element( m_windows[index] ); 95 100 m_windows.erase( m_windows.begin() + index ); 96 101 } … … 102 107 delete m_device; 103 108 } 109 110 // void application::bring_to_front( int i ) 111 // { 112 // if ( i >= 0 && i < m_windows.size() ) 113 // { 114 // m_guienv->bring_to_front( m_windows[i] ); 115 // } 116 // } 104 117 105 118 int main(int, char* [])
Note: See TracChangeset
for help on using the changeset viewer.