1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #include <nv/wx/wx_canvas.hh>
|
---|
8 |
|
---|
9 | #include <nv/core/time.hh>
|
---|
10 |
|
---|
11 | wxBEGIN_EVENT_TABLE( nv::wx_gl_canvas, wxWindow )
|
---|
12 | EVT_PAINT( nv::wx_gl_canvas::on_paint )
|
---|
13 | wxEND_EVENT_TABLE()
|
---|
14 |
|
---|
15 | nv::wx_gl_canvas::wx_gl_canvas( wxWindow *parent, input* in )
|
---|
16 | : wxWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
|
---|
17 | wxFULL_REPAINT_ON_RESIZE )
|
---|
18 | {
|
---|
19 | nv::load_gl_no_context();
|
---|
20 | wxClientDC dc( this );
|
---|
21 | m_render = true;
|
---|
22 | m_wm = new nv::sdl::window_manager;
|
---|
23 | m_device = new nv::gl_device();
|
---|
24 | if ( !in ) in = new nv::sdl::input;
|
---|
25 | m_window = new nv::gl_window( m_device, m_wm, in, GetHWND(), dc.GetHDC() );
|
---|
26 | m_context = m_window->get_context();
|
---|
27 | m_main = true;
|
---|
28 | }
|
---|
29 |
|
---|
30 | nv::wx_gl_canvas::wx_gl_canvas( wxWindow *parent, wx_gl_canvas *sibling, input* in )
|
---|
31 | : wxWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
|
---|
32 | wxFULL_REPAINT_ON_RESIZE )
|
---|
33 | {
|
---|
34 | wxClientDC dc( this );
|
---|
35 | m_render = true;
|
---|
36 | m_wm = sibling->m_wm;
|
---|
37 | m_device = new nv::gl_device();
|
---|
38 | if ( !in ) in = new nv::sdl::input;
|
---|
39 | m_window = new nv::gl_window( m_device, m_wm, in, GetHWND(), dc.GetHDC() );
|
---|
40 | m_context = m_window->get_context();
|
---|
41 | make_current();
|
---|
42 | m_main = false;
|
---|
43 |
|
---|
44 | }
|
---|
45 |
|
---|
46 | nv::wx_gl_canvas::~wx_gl_canvas()
|
---|
47 | {
|
---|
48 | make_current();
|
---|
49 | delete m_window;
|
---|
50 | delete m_device;
|
---|
51 | }
|
---|
52 |
|
---|
53 | void nv::wx_gl_canvas::on_paint( wxPaintEvent& )
|
---|
54 | {
|
---|
55 | make_current();
|
---|
56 | const wxSize client_size = GetClientSize();
|
---|
57 | m_context->set_viewport( nv::ivec4( nv::ivec2(), client_size.x, client_size.y ) );
|
---|
58 | on_update();
|
---|
59 | }
|
---|
60 |
|
---|
61 | void nv::wx_gl_canvas::stop()
|
---|
62 | {
|
---|
63 | Disconnect( wxEVT_IDLE, wxIdleEventHandler( wx_gl_canvas::on_idle ) );
|
---|
64 | //m_update_timer->Stop();
|
---|
65 | }
|
---|
66 |
|
---|
67 | void nv::wx_gl_canvas::start()
|
---|
68 | {
|
---|
69 | Connect( wxID_ANY, wxEVT_IDLE, wxIdleEventHandler( wx_gl_canvas::on_idle ) );
|
---|
70 | //m_update_timer->Start(20);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void nv::wx_gl_canvas::on_idle( wxIdleEvent& evt )
|
---|
74 | {
|
---|
75 | nv::sleep( 1 );
|
---|
76 | if ( m_render )
|
---|
77 | {
|
---|
78 | make_current();
|
---|
79 | on_update();
|
---|
80 | evt.RequestMore(); // render continuously, not only once on idle
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | void nv::wx_gl_canvas::make_current()
|
---|
85 | {
|
---|
86 | m_window->make_current();
|
---|
87 | }
|
---|
88 |
|
---|
89 | nv::wx_log_text_ctrl_sink::wx_log_text_ctrl_sink( wxTextCtrl* a_text_ctrl ) : m_text_ctrl( a_text_ctrl )
|
---|
90 | {
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | static wxColor wx_log_color[] =
|
---|
95 | {
|
---|
96 | wxColor( *wxWHITE ),
|
---|
97 | wxColor( *wxRED ),
|
---|
98 | wxColor( *wxRED ),
|
---|
99 | wxColor( *wxRED ),
|
---|
100 | wxColor( *wxYELLOW ),
|
---|
101 | wxColor( *wxGREEN ),
|
---|
102 | wxColor( *wxLIGHT_GREY ),
|
---|
103 | wxColor( *wxLIGHT_GREY ),
|
---|
104 | wxColor( *wxLIGHT_GREY ),
|
---|
105 | wxColor( *wxLIGHT_GREY ),
|
---|
106 | wxColor( *wxLIGHT_GREY ),
|
---|
107 | };
|
---|
108 |
|
---|
109 | void nv::wx_log_text_ctrl_sink::log( nv::log_level level, const nv::string_view& message )
|
---|
110 | {
|
---|
111 | wxString str;
|
---|
112 | char stamp[16];
|
---|
113 | size_t ssize = timestamp( stamp );
|
---|
114 | m_text_ctrl->SetDefaultStyle( wxTextAttr( wx_log_color[ level / 10 ] ) );
|
---|
115 | str << "[" << padded_level_name( level ).data() << "] " << message.data() << "\n";
|
---|
116 | m_text_ctrl->AppendText( str );
|
---|
117 | }
|
---|
118 |
|
---|
119 | nv::wx_app_base::wx_app_base()
|
---|
120 | : m_logger( nv::LOG_TRACE )
|
---|
121 | {
|
---|
122 | nv::log_sink* sink = new nv::log_file_sink( "log.txt" );
|
---|
123 | m_logger.add_sink( sink, nv::LOG_TRACE );
|
---|
124 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
125 | }
|
---|
126 |
|
---|
127 | bool nv::wx_app_base::OnInit()
|
---|
128 | {
|
---|
129 | if ( !wxApp::OnInit() )
|
---|
130 | return false;
|
---|
131 |
|
---|
132 | return initialize();
|
---|
133 | }
|
---|
134 |
|
---|
135 | int nv::wx_app_base::OnExit()
|
---|
136 | {
|
---|
137 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
138 | return wxApp::OnExit();
|
---|
139 | }
|
---|