/
AIHeadChooserDialog.cpp
205 lines (156 loc) · 5.3 KB
/
AIHeadChooserDialog.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "AIHeadChooserDialog.h"
#include "i18n.h"
#include "ui/imainframe.h"
#include "ieclass.h"
#include "eclass.h"
#include <wx/splitter.h>
#include <wx/button.h>
namespace ui
{
namespace
{
const char* const WINDOW_TITLE = N_("Choose AI Head");
}
AIHeadChooserDialog::AIHeadChooserDialog() :
DialogBase(_(WINDOW_TITLE)),
_headStore(new wxutil::TreeModel(_columns, true)),
_headsView(NULL),
_description(NULL)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxSP_3D | wxSP_LIVE_UPDATE);
splitter->SetMinimumPaneSize(10); // disallow unsplitting
GetSizer()->Add(splitter, 1, wxEXPAND | wxALL, 12);
GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxBOTTOM | wxRIGHT, 12);
_headsView = wxutil::TreeView::CreateWithModel(splitter, _headStore.get(), wxDV_NO_HEADER);
_headsView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(AIHeadChooserDialog::onHeadSelectionChanged), NULL, this);
// Head Name column
_headsView->AppendTextColumn("", _columns.name.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
// Allow searching for the name
_headsView->AddSearchColumn(_columns.name);
FitToScreen(0.7f, 0.6f);
wxPanel* previewPanel = new wxPanel(splitter, wxID_ANY);
// Set the default rotation to something better suitable for the head models
_preview.reset(new wxutil::ModelPreview(previewPanel));
_preview->setDefaultCamDistanceFactor(9.0f);
_description = new wxTextCtrl(previewPanel, wxID_ANY, "",
wxDefaultPosition, wxDefaultSize, wxTE_LEFT | wxTE_MULTILINE | wxTE_READONLY | wxTE_WORDWRAP);
_description->SetMinClientSize(wxSize(-1, 60));
previewPanel->SetSizer(new wxBoxSizer(wxVERTICAL));
previewPanel->GetSizer()->Add(_description, 0, wxEXPAND | wxBOTTOM, 6);
previewPanel->GetSizer()->Add(_preview->getWidget(), 1, wxEXPAND);
splitter->SplitVertically(_headsView, previewPanel);
// Set the default size of the window
splitter->SetSashPosition(static_cast<int>(GetSize().GetWidth() * 0.3f));
// Check if the liststore is populated
findAvailableHeads();
// Load the found heads into the GtkListStore
populateHeadStore();
Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &AIHeadChooserDialog::_onItemActivated, this );
}
void AIHeadChooserDialog::_onItemActivated( wxDataViewEvent& ev ) {
EndModal( wxID_OK );
}
void AIHeadChooserDialog::setSelectedHead(const std::string& headDef)
{
_selectedHead = headDef;
if (_selectedHead.empty())
{
_headsView->UnselectAll();
return;
}
wxDataViewItem found = _headStore->FindString(headDef, _columns.name);
// Lookup the model path in the treemodel
if (found.IsOk())
{
_headsView->Select(found);
_headsView->EnsureVisible(found);
handleSelectionChanged();
}
}
std::string AIHeadChooserDialog::getSelectedHead()
{
return _selectedHead;
}
void AIHeadChooserDialog::handleSelectionChanged()
{
// Prepare to check for a selection
wxDataViewItem item = _headsView->GetSelection();
// Add button is enabled if there is a selection and it is not a folder.
if (item.IsOk())
{
// Make the OK button active
FindWindowById(wxID_OK, this)->Enable(true);
_description->Enable(true);
// Set the panel text with the usage information
wxutil::TreeModel::Row row(item, *_headStore);
_selectedHead = row[_columns.name];
// Lookup the IEntityClass instance
IEntityClassPtr ecls = GlobalEntityClassManager().findClass(_selectedHead);
if (ecls)
{
_preview->setModel(ecls->getAttributeValue("model"));
_preview->setSkin(ecls->getAttributeValue("skin"));
// Update the usage panel
_description->SetValue(eclass::getUsage(*ecls));
}
}
else
{
_selectedHead = "";
_preview->setModel("");
FindWindowById(wxID_OK, this)->Enable(false);
_description->Enable(false);
}
}
void AIHeadChooserDialog::onHeadSelectionChanged(wxDataViewEvent& ev)
{
handleSelectionChanged();
}
void AIHeadChooserDialog::populateHeadStore()
{
// Clear the head list to be safe
_headStore->Clear();
for (HeadList::const_iterator i = _availableHeads.begin(); i != _availableHeads.end(); ++i)
{
// Add the entity to the list
wxutil::TreeModel::Row row = _headStore->AddItem();
row[_columns.name] = *i;
row.SendItemAdded();
}
}
namespace
{
class HeadEClassFinder :
public EntityClassVisitor
{
AIHeadChooserDialog::HeadList& _list;
public:
HeadEClassFinder(AIHeadChooserDialog::HeadList& list) :
_list(list)
{}
void visit(const IEntityClassPtr& eclass)
{
if (eclass->getAttributeValue("editor_head") == "1")
{
_list.insert(eclass->getName());
}
}
};
} // namespace
void AIHeadChooserDialog::findAvailableHeads()
{
if (!_availableHeads.empty())
{
return;
}
// Instantiate a finder class and traverse all eclasses
HeadEClassFinder visitor(_availableHeads);
GlobalEntityClassManager().forEachEntityClass(visitor);
}
// Init static class member
AIHeadChooserDialog::HeadList AIHeadChooserDialog::_availableHeads;
} // namespace ui