Debian Codegraph @

plugins/dm.objectives/ce/DistanceComponentEditor.cpp

raw
1 #include "DistanceComponentEditor.h"
2 #include "../SpecifierType.h"
3 #include "../Component.h"
4
5 #include "string/convert.h"
6
7 #include "i18n.h"
8
9 #include <wx/spinctrl.h>
10 #include <wx/textctrl.h>
11 #include <wx/stattext.h>
12
13 namespace objectives
14 {
15
16 namespace ce
17 {
18
19 // Registration helper, will register this editor in the factory
20 DistanceComponentEditor::RegHelper DistanceComponentEditor::regHelper;
21
22 // Constructor
23 DistanceComponentEditor::DistanceComponentEditor(wxWindow* parent, Component& component) :
24 ComponentEditorBase(parent),
25 _component(&component),
26 _entity(new wxTextCtrl(_panel, wxID_ANY)),
27 _location(new wxTextCtrl(_panel, wxID_ANY))
28 {
29 _entity->Bind(wxEVT_TEXT, [&] (wxCommandEvent& ev) { writeToComponent(); });
30 _location->Bind(wxEVT_TEXT, [&] (wxCommandEvent& ev) { writeToComponent(); });
31
32 // Allow for one digit of the distance, everything below this step size is insane
33 _distance = new wxSpinCtrl(_panel, wxID_ANY);
34 _distance->SetValue(1);
35 _distance->SetRange(0, 132000);
36 _distance->SetMinClientSize(wxSize(_distance->GetCharWidth()*9, -1));
37 _distance->Bind(wxEVT_SPINCTRL, [&] (wxSpinEvent& ev) { writeToComponent(); });
38
39 _interval = new wxSpinCtrlDouble(_panel, wxID_ANY);
40 _interval->SetValue(1);
41 _interval->SetRange(0, 65535);
42 _interval->SetIncrement(0.1);
43 _interval->SetDigits(1);
44 _interval->SetMinClientSize(wxSize(_interval->GetCharWidth()*9, -1));
45 _interval->Bind(wxEVT_SPINCTRLDOUBLE, [&] (wxSpinDoubleEvent& ev) { writeToComponent(); });
46
47 wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
48 _panel->GetSizer()->Add(hbox, 0, wxBOTTOM | wxEXPAND, 6);
49
50 hbox->Add(new wxStaticText(_panel, wxID_ANY, _("Entity:")), 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 6);
51 hbox->Add(_entity, 1, wxEXPAND);
52
53 wxBoxSizer* hbox2 = new wxBoxSizer(wxHORIZONTAL);
54 _panel->GetSizer()->Add(hbox2, 0, wxBOTTOM | wxEXPAND, 6);
55
56 hbox2->Add(new wxStaticText(_panel, wxID_ANY, _("Location Entity:")), 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 6);
57 hbox2->Add(_location, 1, wxEXPAND);
58
59 wxBoxSizer* hbox3 = new wxBoxSizer(wxHORIZONTAL);
60 _panel->GetSizer()->Add(hbox3, 0, wxBOTTOM | wxEXPAND, 6);
61
62 hbox3->Add(new wxStaticText(_panel, wxID_ANY, _("Distance:")), 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 6);
63 hbox3->Add(_distance, 0);
64
65 wxBoxSizer* hbox4 = new wxBoxSizer(wxHORIZONTAL);
66 _panel->GetSizer()->Add(hbox4, 0, wxBOTTOM | wxEXPAND, 6);
67
68 hbox4->Add(new wxStaticText(_panel, wxID_ANY, _("Clock interval:")), 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 6);
69 hbox4->Add(_interval, 0);
70 hbox4->Add(new wxStaticText(_panel, wxID_ANY, _("seconds:")), 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, 6);
71
72 // Load the initial values from the component arguments
73 _entity->SetValue(component.getArgument(0));
74 _location->SetValue(component.getArgument(1));
75
76 _distance->SetValue(string::convert<int>(component.getArgument(2)));
77 float interval = component.getClockInterval();
78 _interval->SetValue(interval >= 0 ? interval : 1.0);
79 }
80
81 // Write to component
82 void DistanceComponentEditor::writeToComponent() const
83 {
84 if (!_active) return; // still under construction
85
86 assert(_component);
87
88 _component->clearArguments();
89 _component->setArgument(0, _entity->GetValue().ToStdString());
90 _component->setArgument(1, _location->GetValue().ToStdString());
91 _component->setArgument(2, string::to_string(_distance->GetValue()));
92 _component->setClockInterval(static_cast<float>(_interval->GetValue()));
93 }
94
95 } // namespace ce
96
97 } // namespace objectives