source: trunk/tests/gui_test/nv_gui_test.cc

Last change on this file was 351, checked in by epyon, 10 years ago
  • gui hover and selected support
  • minor changes
File size: 3.4 KB
RevLine 
[127]1#include <nv/gl/gl_device.hh>
2#include <nv/gui/gui_environment.hh>
[328]3#include <nv/sdl/sdl_window_manager.hh>
[239]4#include <nv/interface/context.hh>
[328]5#include <nv/interface/window_manager.hh>
[319]6#include <nv/core/logging.hh>
7#include <nv/core/logger.hh>
[321]8#include <nv/core/random.hh>
[127]9
10class application
11{
12public:
13        application();
14        bool initialize();
15        bool run();
16        void kill_window();
17        void spawn_window();
18        void recolor_window();
[267]19//      void bring_to_front( int i );
[127]20        ~application();
21protected:
[328]22        nv::window_manager* m_wm;
[127]23        nv::device* m_device;
24        nv::window* m_window;
25        nv::clear_state m_clear_state;
26        nv::gui::environment* m_guienv;
[269]27        std::vector<nv::gui::handle>   m_windows;
[127]28};
29
30application::application()
31{
32        m_device = new nv::gl_device();
[328]33        m_wm     = new nv::sdl::window_manager;
34        m_window = m_wm->create_window( m_device, 800, 600, false );
[127]35        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
36        m_guienv = new nv::gui::environment( m_window );
37        m_guienv->load_style( "test.style.lua" );
38}
39
40bool application::initialize()
41{
42        return true;
43}
44
45bool application::run()
46{
47        int keypress = 0;
48
49        while(!keypress)
50        {
51                m_guienv->update();
52
53                m_window->get_context()->clear( m_clear_state );
54                m_guienv->draw();
55                m_window->swap_buffers();
56
57                nv::io_event event;
58                while(m_window->poll_event(event))
59                {     
60                        switch (event.type)
61                        {
62                        case nv::EV_QUIT:
63                                keypress = 1;
64                                break;
65                        case nv::EV_KEY:
66                                if (event.key.pressed)
67                                {
68                                        switch (event.key.code)
69                                        {
[267]70//                                      case nv::KEY_1      : bring_to_front(0); break;
71//                                      case nv::KEY_2      : bring_to_front(1); break;
72//                                      case nv::KEY_3      : bring_to_front(2); break;
73//                                      case nv::KEY_4      : bring_to_front(3); break;
74//                                      case nv::KEY_5      : bring_to_front(4); break;
[127]75                                        case nv::KEY_N      : spawn_window(); break;
76                                        case nv::KEY_K      : kill_window(); break;
77                                        case nv::KEY_ESCAPE : keypress = 1; break;
78                                        }
79                                }
80                                break;
81                        }
[351]82                        m_guienv->process_io_event( event );
[127]83                }
84        }
85        return true;
86}
87
88void application::spawn_window()
89{
[321]90        nv::ivec2 a = nv::random::get().range( nv::ivec2(), nv::ivec2( 600, 400 ) );
91        nv::ivec2 b = nv::random::get().range( nv::ivec2( 40, 40 ), nv::ivec2( 240, 240 ) );
[269]92        nv::gui::handle e = m_guienv->create_element( nv::rectangle(a).dim(b) );
[268]93        m_guienv->set_class( e, "window" );
94        m_guienv->set_text( e, "Window "+nv::to_string(m_windows.size()+1) );
[127]95        NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" );
96        m_windows.push_back( e );
97}
98
99void application::kill_window()
100{
101        if ( m_windows.size() == 0 ) return;
[321]102        size_t index = nv::random::get().urand( m_windows.size() );
[267]103        m_guienv->destroy_element( m_windows[index] );
[127]104        m_windows.erase( m_windows.begin() + index );
105}
106
107application::~application()
108{
109        m_windows.clear();
110        delete m_window;
111        delete m_device;
112}
113
[267]114// void application::bring_to_front( int i )
115// {
116//      if ( i >= 0 && i < m_windows.size() )
117//      {
118//              m_guienv->bring_to_front( m_windows[i] );
119//      }
120// }
121
[127]122int main(int, char* [])
123{
[321]124        nv::random::get().randomize();
[127]125        nv::logger log(nv::LOG_TRACE);
126        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
127        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
128       
129        NV_LOG( nv::LOG_NOTICE, "Logging started" );
130        application app;
131        if ( app.initialize() )
132        {
133                app.run();
134        }
135        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
136
137        return 0;
138}
139
Note: See TracBrowser for help on using the repository browser.