forked from kwikius/quantracker_sim
/
view.cpp
184 lines (147 loc) · 5.01 KB
/
view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "view.hpp"
#include "panel.hpp"
#include "frsky.hpp"
#include "events.hpp"
#include "window_ids.hpp"
#include "document.hpp"
#include <quan/gx/primitives/simple_line.hpp>
#include "aircraft_symbol.hpp"
BEGIN_EVENT_TABLE(view,wxWindow)
EVT_PAINT(view::OnPaint)
EVT_SIZE(view::OnSize)
EVT_SCROLLWIN(view::OnScroll)
EVT_LEFT_DOWN(view::OnMouseLeftDown)
EVT_LEFT_UP(view::OnMouseLeftUp)
EVT_MOTION(view::OnMouseMove)
END_EVENT_TABLE()
view::view(wxWindow* parent)
: wxWindow(parent, wxID_ANY)
,m_aircraft_selected{false}
{
window_ids::view = this->GetId();
this->SetWindowStyle(wxVSCROLL | wxHSCROLL);
this->SetScrollbar(wxVERTICAL,50,10,110);
this->SetScrollbar(wxHORIZONTAL,50,10,110);
this->m_drawing_view.set_scale(0.01);
auto doc = wxGetApp().get_document();
m_last_aircraft_position = doc->get_aircraft_xyz_position();
// update_aircraft_gps_position(doc->get_aircraft_gps_position<
// quan::angle::deg10e7,
// quan::length::m
// >());
}
void view::set_scale(double const & v)
{
m_drawing_view.set_scale(v);
this->Refresh();
}
void view::OnPaint(wxPaintEvent & event)
{
wxPaintDC dc(this);
dc.SetBackground(* wxWHITE_BRUSH); // sets background brush but doesnt clear
dc.Clear(); // need to invoke to clear using current background brush
quan::gx::wxwidgets::graphics_context wc{
&dc,
&this->m_drawing,
&this->m_drawing_view,
&this->m_device_window
};
quan::two_d::box<mm> tracker{mm{-250},mm{250},mm{250},mm{-250}};
wc.draw_box({tracker});
auto doc = wxGetApp().get_document();
auto ap = doc->get_aircraft_xyz_position();
aircraft_symbol sym{ap,doc->get_aircraft_heading()};
sym.draw(wc);
vect2_mm ap1{ap.x,ap.y};
quan::gx::primitives::simple_line<mm> loc {
{mm{0},mm{0}},ap1,mm{1},
quan::gx::rgb::colors::silver
};
wc.draw_line(loc);
}
// update device_window size data when size changes
void view::OnSize(wxSizeEvent & event)
{
this->GetClientSize(
&this->m_device_window.m_size_px.x,
&this->m_device_window.m_size_px.y
);
// find window size in mm by getting size of pixel on display
// may be an ondisplay change function so dont need to continaully update
vect2_i display_size_in_px;
wxDisplaySize(&display_size_in_px.x,&display_size_in_px.y);
wxSize detail_display_size_mm = wxGetDisplaySizeMM();
vect2_mm display_size_in_mm{mm{detail_display_size_mm.x},mm{detail_display_size_mm.y}};
this->m_device_window.m_size_mm.x
= (this->m_device_window.m_size_px.x * display_size_in_mm.x) / display_size_in_px.x;
this->m_device_window.m_size_mm.y
= (this->m_device_window.m_size_px.y * display_size_in_mm.y) / display_size_in_px.y;
this->Refresh();
}
void view::OnScroll(wxScrollWinEvent & event)
{
if (event.GetOrientation() == wxHORIZONTAL){
this->OnHScroll(event);
}else{
this->OnVScroll(event);
}
this->Refresh();
}
void view::OnHScroll(wxScrollWinEvent & event)
{
this->m_drawing_view.set_x_scroll_ratio((event.GetPosition() - 50 )/100.0);
}
void view::OnVScroll(wxScrollWinEvent & event)
{
this->m_drawing_view.set_y_scroll_ratio( -(event.GetPosition() - 50 )/100.0);
}
void view::OnMouseLeftDown(wxMouseEvent & event)
{
event.Skip();
quan::gx::wxwidgets::graphics_info_context ic{
&this->m_drawing,
&this->m_drawing_view,
&this->m_device_window
};
// in drawing
auto mouse_down_pos = ic.device_to_drawing({event.GetX(),event.GetY()});
auto doc = wxGetApp().get_document();
auto aircraft_pos = doc->get_aircraft_xyz_position();
auto diff = abs(ic.scale_drawing_to_view(mouse_down_pos - vect2_mm{aircraft_pos.x,aircraft_pos.y}));
auto click_bucket = doc->get_click_bucket();
if ( (diff.x < click_bucket) && (diff.y < click_bucket)){
m_aircraft_selected = true;
}
}
void view::OnMouseLeftUp(wxMouseEvent & event)
{
m_aircraft_selected = false;
}
void view::OnMouseMove(wxMouseEvent & event)
{
if ( m_aircraft_selected && event.Dragging()){
quan::gx::wxwidgets::graphics_info_context ic{
&this->m_drawing,
&this->m_drawing_view,
&this->m_device_window
};
auto mouse_pos = ic.device_to_drawing({event.GetX(),event.GetY()});
auto & app = wxGetApp();
auto doc = app.get_document();
m_last_aircraft_position = doc->get_aircraft_xyz_position();
vect2_mm last_xy {m_last_aircraft_position.x,m_last_aircraft_position.y};
if ( mouse_pos != last_xy){
doc->set_aircraft_xyz_position({mouse_pos.x,mouse_pos.y,m_last_aircraft_position.z});
auto diff = mouse_pos - last_xy;
auto angle = quan::atan2(diff.y,diff.x);
doc->set_aircraft_heading(quan::angle::deg{90} - angle);
// update_aircraft_gps_position(doc->get_aircraft_gps_position<
// quan::angle::deg10e7,
// quan::length::m
// >());
QuanTrackerSimEvent x{wxEvent_AircraftPositionChanged};
app.get_panel()->AddPendingEvent(x);
this->Refresh();
}
}
}