/
Vector3PropertyEditor.cpp
117 lines (93 loc) · 3.39 KB
/
Vector3PropertyEditor.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
#include "Vector3PropertyEditor.h"
#include "i18n.h"
#include "ientity.h"
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/button.h>
#include <wx/spinctrl.h>
#include <wx/stattext.h>
#include "string/convert.h"
#include <sstream>
#include <vector>
namespace ui
{
static const int RANGE_MAX = 32767;
static const int RANGE_MIN = -32767;
wxSpinCtrl* makeSpinCtrl(wxPanel* parent)
{
wxSpinCtrl* ctrl = new wxSpinCtrl(parent, wxID_ANY);
// Set an appropriate minimum size based on the expected contents
static const wxSize minSize = ctrl->GetSizeFromTextSize(
ctrl->GetTextExtent(std::to_string(RANGE_MIN)).GetWidth()
);
ctrl->SetMinSize(minSize);
// Set value and range
ctrl->SetValue(0);
ctrl->SetRange(RANGE_MIN, RANGE_MAX);
return ctrl;
}
// Blank ctor
Vector3PropertyEditor::Vector3PropertyEditor() {}
// Constructor. Create the widgets here
Vector3PropertyEditor::Vector3PropertyEditor(wxWindow* parent, Entity* entity,
const std::string& name)
: PropertyEditor(entity),
_key(name)
{
// Construct the main widget (will be managed by the base class)
wxPanel* mainVBox = new wxPanel(parent, wxID_ANY);
mainVBox->SetSizer(new wxBoxSizer(wxHORIZONTAL));
// Register the main widget in the base class
setMainWidget(mainVBox);
// Create the spin buttons
_xValue = makeSpinCtrl(mainVBox);
_yValue = makeSpinCtrl(mainVBox);
_zValue = makeSpinCtrl(mainVBox);
// Add the spin buttons to the HBox with labels
mainVBox->GetSizer()->Add(new wxStaticText(mainVBox, wxID_ANY, _("X: ")), 0, wxALIGN_CENTER_VERTICAL | wxALL, 3);
mainVBox->GetSizer()->Add(_xValue, 0, wxALIGN_CENTER_VERTICAL | wxALL, 3);
mainVBox->GetSizer()->Add(new wxStaticText(mainVBox, wxID_ANY, _(" Y: ")), 0, wxALIGN_CENTER_VERTICAL | wxALL, 3);
mainVBox->GetSizer()->Add(_yValue, 0, wxALIGN_CENTER_VERTICAL | wxALL, 3);
mainVBox->GetSizer()->Add(new wxStaticText(mainVBox, wxID_ANY, _(" Z: ")), 0, wxALIGN_CENTER_VERTICAL | wxALL, 3);
mainVBox->GetSizer()->Add(_zValue, 0, wxALIGN_CENTER_VERTICAL | wxALL, 3);
// Create the apply button
wxButton* applyButton = new wxButton(mainVBox, wxID_APPLY, _("Apply..."));
applyButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(Vector3PropertyEditor::_onApply), NULL, this);
mainVBox->GetSizer()->Add(applyButton, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
// Populate the spin boxes from the keyvalue
updateFromEntity();
}
void Vector3PropertyEditor::updateFromEntity()
{
setWidgetsFromKey(_entity->getKeyValue(_key));
}
void Vector3PropertyEditor::setWidgetsFromKey(const std::string& val)
{
// Stream the given string into a temporary buffer to compile a vector
// of 3 components (separated by spaces in the input).
std::stringstream stream(val);
std::vector<float> values;
float buf;
while (stream >> buf)
values.push_back(buf);
// Set the Gtk widgets
if (values.size() == 3)
{
_xValue->SetValue(values[0]);
_yValue->SetValue(values[1]);
_zValue->SetValue(values[2]);
}
}
void Vector3PropertyEditor::_onApply(wxCommandEvent& ev)
{
// Construct a text value out of the vector components
std::string value =
string::to_string(_xValue->GetValue())
+ " "
+ string::to_string(_yValue->GetValue())
+ " "
+ string::to_string(_zValue->GetValue());
// Set the key on the entity
setKeyValue(_key, value);
}
}