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