Debian Codegraph @

plugins/dm.editing/AIHeadPropertyEditor.cpp

raw
1 #include "AIHeadPropertyEditor.h"
2
3 #include "i18n.h"
4 #include "ieclass.h"
5 #include "ientity.h"
6
7 #include <wx/panel.h>
8 #include <wx/button.h>
9 #include "wxutil/Bitmap.h"
10 #include <wx/sizer.h>
11
12 #include "AIHeadChooserDialog.h"
13
14 namespace ui
15 {
16
17 AIHeadPropertyEditor::AIHeadPropertyEditor(wxWindow* parent, IEntitySelection& entities, const ITargetKey::Ptr& key) :
18 _entities(entities),
19 _key(key)
20 {
21 // Construct the main widget (will be managed by the base class)
22 _widget = new wxPanel(parent, wxID_ANY);
23 _widget->SetSizer(new wxBoxSizer(wxHORIZONTAL));
24
25 // Create the browse button
26 wxButton* browseButton = new wxButton(_widget, wxID_ANY, _("Choose AI head..."));
27 browseButton->SetBitmap(wxutil::GetLocalBitmap("icon_model.png"));
28 browseButton->Bind(wxEVT_BUTTON, &AIHeadPropertyEditor::onChooseButton, this);
29
30 _widget->GetSizer()->Add(browseButton, 0, wxALIGN_CENTER_VERTICAL);
31 }
32
33 AIHeadPropertyEditor::~AIHeadPropertyEditor()
34 {
35 if (_widget != nullptr)
36 {
37 _widget->Destroy();
38 }
39 }
40
41 wxPanel* AIHeadPropertyEditor::getWidget()
42 {
43 return _widget;
44 }
45
46 void AIHeadPropertyEditor::updateFromEntities()
47 {
48 // nothing to do
49 }
50
51 sigc::signal<void(const std::string&, const std::string&)>& AIHeadPropertyEditor::signal_keyValueApplied()
52 {
53 return _sigKeyValueApplied;
54 }
55
56 IPropertyEditor::Ptr AIHeadPropertyEditor::CreateNew(wxWindow* parent, IEntitySelection& entities,
57 const ITargetKey::Ptr& key)
58 {
59 return std::make_shared<AIHeadPropertyEditor>(parent, entities, key);
60 }
61
62 void AIHeadPropertyEditor::onChooseButton(wxCommandEvent& ev)
63 {
64 // Construct a new head chooser dialog
65 AIHeadChooserDialog* dialog = new AIHeadChooserDialog;
66
67 dialog->setSelectedHead(_entities.getSharedKeyValue(DEF_HEAD_KEY, true));
68
69 // Show and block
70 if (dialog->ShowModal() == wxID_OK)
71 {
72 auto selectedHead = dialog->getSelectedHead();
73
74 _entities.foreachEntity([&](const IEntityNodePtr& entity)
75 {
76 entity->getEntity().setKeyValue(DEF_HEAD_KEY, selectedHead);
77 });
78
79 signal_keyValueApplied().emit(DEF_HEAD_KEY, selectedHead);
80 }
81
82 dialog->Destroy();
83 }
84
85 std::string AIHeadEditorDialogWrapper::runDialog(Entity* entity, const std::string& key)
86 {
87 // Construct a new head chooser dialog
88 AIHeadChooserDialog* dialog = new AIHeadChooserDialog;
89
90 std::string prevHead = entity->getKeyValue(key);
91 dialog->setSelectedHead(prevHead);
92
93 // Show and block
94 std::string selected = prevHead;
95
96 if (dialog->ShowModal() == wxID_OK)
97 {
98 selected = dialog->getSelectedHead();
99 }
100
101 dialog->Destroy();
102
103 return selected;
104 }
105
106 } // namespace ui