| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "iundo.h" |
| 4 |
#include "ieclass.h" |
| 5 |
#include "ientity.h" |
| 6 |
#include "string/convert.h" |
| 7 |
#include "util/ScopedBoolLock.h" |
| 8 |
#include <fmt/format.h> |
| 9 |
#include <wx/spinctrl.h> |
| 10 |
#include <wx/panel.h> |
| 11 |
#include <wx/sizer.h> |
| 12 |
|
| 13 |
namespace ui |
| 14 |
{ |
| 15 |
|
| 16 |
/** |
| 17 |
* An enhanced spin button that is updating the named |
| 18 |
* entity property (spawnarg) when toggled. |
| 19 |
* |
| 20 |
* Due to some weird bug that prevented the wxSpinCtrlDouble |
| 21 |
* to be rendered with a wxScrolledWindow as immediate parent, |
| 22 |
* I had to work around it by putting the wxSpinCtrlDouble into |
| 23 |
* a parent wxPanel first. |
| 24 |
*/ |
| 25 |
class SpawnargLinkedSpinButton : |
| 26 |
public wxPanel |
| 27 |
{ |
| 28 |
private: |
| 29 |
wxSpinCtrlDouble* _spinCtrl; |
| 30 |
|
| 31 |
std::string _label; |
| 32 |
|
| 33 |
std::string _propertyName; |
| 34 |
|
| 35 |
Entity* _entity; |
| 36 |
|
| 37 |
bool _updateLock; |
| 38 |
|
| 39 |
public: |
| 40 |
SpawnargLinkedSpinButton(wxWindow* parent, |
| 41 |
const std::string& label, |
| 42 |
const std::string& propertyName, |
| 43 |
double min, |
| 44 |
double max, |
| 45 |
double increment = 1, |
| 46 |
unsigned int digits = 0) : |
| 47 |
wxPanel(parent, wxID_ANY), |
| 48 |
_spinCtrl(new wxSpinCtrlDouble(this, wxID_ANY)), |
| 49 |
_label(label), |
| 50 |
_propertyName(propertyName), |
| 51 |
_entity(NULL), |
| 52 |
_updateLock(false) |
| 53 |
{ |
| 54 |
this->SetSizer(new wxBoxSizer(wxHORIZONTAL)); |
| 55 |
this->GetSizer()->Add(_spinCtrl, 1, wxEXPAND); |
| 56 |
|
| 57 |
_spinCtrl->SetIncrement(increment); |
| 58 |
_spinCtrl->SetRange(min, max); |
| 59 |
_spinCtrl->SetDigits(digits); |
| 60 |
|
| 61 |
// 6 chars wide |
| 62 |
_spinCtrl->SetMaxSize(wxSize(GetCharWidth() * 9, -1)); |
| 63 |
|
| 64 |
_spinCtrl->Connect(wxEVT_SPINCTRLDOUBLE, |
| 65 |
wxSpinDoubleEventHandler(SpawnargLinkedSpinButton::onSpinButtonChanged), NULL, this); |
| 66 |
} |
| 67 |
|
| 68 |
const std::string& getLabel() const |
| 69 |
{ |
| 70 |
return _label; |
| 71 |
} |
| 72 |
|
| 73 |
// Sets the edited Entity object |
| 74 |
void setEntity(Entity* entity) |
| 75 |
{ |
| 76 |
_entity = entity; |
| 77 |
|
| 78 |
if (_entity == NULL) |
| 79 |
{ |
| 80 |
SetToolTip(""); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
std::string desc = _propertyName + ": " |
| 85 |
+ _entity->getEntityClass()->getAttributeDescription(_propertyName); |
| 86 |
_spinCtrl->SetToolTip(desc); |
| 87 |
|
| 88 |
if (_updateLock) return; |
| 89 |
|
| 90 |
util::ScopedBoolLock lock(_updateLock); |
| 91 |
|
| 92 |
_spinCtrl->SetValue(string::convert<float>(_entity->getKeyValue(_propertyName))); |
| 93 |
} |
| 94 |
|
| 95 |
protected: |
| 96 |
|
| 97 |
void onSpinButtonChanged(wxSpinDoubleEvent& ev) |
| 98 |
{ |
| 99 |
ev.Skip(); |
| 100 |
|
| 101 |
// Update the spawnarg if we have a valid entity |
| 102 |
if (!_updateLock && _entity != NULL) |
| 103 |
{ |
| 104 |
util::ScopedBoolLock lock(_updateLock); |
| 105 |
UndoableCommand cmd("editAIProperties"); |
| 106 |
|
| 107 |
double floatVal = _spinCtrl->GetValue(); |
| 108 |
std::string newValue = fmt::format("{0:." + string::to_string(_spinCtrl->GetDigits()) + "f}", floatVal); |
| 109 |
|
| 110 |
// Check if the new value conincides with an inherited one |
| 111 |
const std::string attr = _entity->getEntityClass()->getAttributeValue(_propertyName); |
| 112 |
if (!attr.empty() && string::to_float(attr) == floatVal) |
| 113 |
{ |
| 114 |
// in which case the property just gets removed from the entity |
| 115 |
newValue = ""; |
| 116 |
} |
| 117 |
|
| 118 |
_entity->setKeyValue(_propertyName, newValue); |
| 119 |
} |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
} // namespace |