source: trunk/tests/gui_test/nv_gui_test.cc @ 269

Last change on this file since 269 was 269, checked in by epyon, 11 years ago
  • gui::environment uses a single vector of static elements
  • gui::environment operates on gui::handle's explicitly
  • indexes via gui::handle are reusable, but counted!
  • full DOD achieved (making a note here, huge success!)
File size: 3.2 KB
Line 
1#include <nv/gl/gl_device.hh>
2#include <nv/gui/gui_environment.hh>
3#include <nv/interface/context.hh>
4#include <nv/logging.hh>
5#include <nv/logger.hh>
6#include <cstdlib> // rand
7#include <ctime> // time
8
9class application
10{
11public:
12        application();
13        bool initialize();
14        bool run();
15        void kill_window();
16        void spawn_window();
17        void recolor_window();
18//      void bring_to_front( int i );
19        ~application();
20protected:
21        nv::device* m_device;
22        nv::window* m_window;
23        nv::clear_state m_clear_state;
24        nv::gui::environment* m_guienv;
25        std::vector<nv::gui::handle>   m_windows;
26};
27
28application::application()
29{
30        m_device = new nv::gl_device();
31        m_window = m_device->create_window( 800, 600, false );
32        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
33        m_guienv = new nv::gui::environment( m_window );
34        m_guienv->load_style( "test.style.lua" );
35}
36
37bool application::initialize()
38{
39        return true;
40}
41
42bool application::run()
43{
44        int keypress = 0;
45
46        while(!keypress)
47        {
48                m_guienv->update();
49
50                m_window->get_context()->clear( m_clear_state );
51                m_guienv->draw();
52                m_window->swap_buffers();
53
54                nv::io_event event;
55                while(m_window->poll_event(event))
56                {     
57                        switch (event.type)
58                        {
59                        case nv::EV_QUIT:
60                                keypress = 1;
61                                break;
62                        case nv::EV_KEY:
63                                if (event.key.pressed)
64                                {
65                                        switch (event.key.code)
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;
72                                        case nv::KEY_N      : spawn_window(); break;
73                                        case nv::KEY_K      : kill_window(); break;
74                                        case nv::KEY_ESCAPE : keypress = 1; break;
75                                        }
76                                }
77                                break;
78                        }
79                }
80        }
81        return true;
82}
83
84void application::spawn_window()
85{
86        glm::ivec2 a( std::rand() % 600, std::rand() % 400 );
87        glm::ivec2 b( std::rand() % 200 + 40, std::rand() % 200 + 40 );
88        nv::gui::handle e = m_guienv->create_element( nv::rectangle(a).dim(b) );
89        m_guienv->set_class( e, "window" );
90        m_guienv->set_text( e, "Window "+nv::to_string(m_windows.size()+1) );
91        NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" );
92        m_windows.push_back( e );
93}
94
95void application::kill_window()
96{
97        if ( m_windows.size() == 0 ) return;
98        size_t index = rand() % m_windows.size();
99        m_guienv->destroy_element( m_windows[index] );
100        m_windows.erase( m_windows.begin() + index );
101}
102
103application::~application()
104{
105        m_windows.clear();
106        delete m_window;
107        delete m_device;
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// }
117
118int main(int, char* [])
119{
120        std::srand((unsigned int) std::time(0) );
121        nv::logger log(nv::LOG_TRACE);
122        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
123        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
124       
125        NV_LOG( nv::LOG_NOTICE, "Logging started" );
126        application app;
127        if ( app.initialize() )
128        {
129                app.run();
130        }
131        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
132
133        return 0;
134}
135
Note: See TracBrowser for help on using the repository browser.