-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddonDialog.cpp
More file actions
305 lines (267 loc) · 7.68 KB
/
AddonDialog.cpp
File metadata and controls
305 lines (267 loc) · 7.68 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "AddonDialog.h"
#include "cbproject.h"
#include "configmanager.h"
#include "projectmanager.h"
#include <map>
AddonDialog::AddonDialog(wxWindow* pParent,wxWindowID nID, const wxString& sTitle):wxDialog(pParent, nID, sTitle) {
int max_w = 420;
SetSize(wxSize(max_w,500));
max_w -= 10;
ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("openframeworks"));
wxString saved_path = _T("");
if (cfg) {
saved_path = cfg->Read(_T("of_dir"),_T("You need to select the openFrameworks directory."));
}
of_dir_box = new wxStaticBox(
this
,wxNewId()
,_T("openFrameworks location")
,wxPoint(10,10)
,wxSize(385, 60)
);
of_dir_txt = new wxTextCtrl(
this
,wxNewId()
,saved_path
,wxPoint(20,35)
,wxSize(330,20)
);
of_dir_txt->SetEditable(false);
of_dir_btn = new wxButton(
this
,OF_DIR_BUTTON
,_T("...")
,wxPoint(355,35)
,wxSize(25,20)
);
Connect(
OF_DIR_BUTTON
,wxEVT_COMMAND_BUTTON_CLICKED
,wxCommandEventHandler(AddonDialog::onOFDirButtonClick)
);
of_addon_box = new wxStaticBox(
this
,wxNewId()
,_T("Select addons for your project")
,wxPoint(10,80)
,wxSize(385, 330)
);
of_addons_list = new wxCheckListBox(
this
,wxNewId()
,wxPoint(20,105)
,wxSize(360,280)
);
// Add selected addons to project.
of_ok_btn = new wxButton(
this
,OF_OK_BUTTON
,_T("Add addons to project")
,wxPoint(10,420)
);
Connect(
OF_OK_BUTTON
,wxEVT_COMMAND_BUTTON_CLICKED
,wxCommandEventHandler(AddonDialog::onOkButtonClick)
);
of_cancel_btn = new wxButton(
this
,OF_CANCEL_BUTTON
,_T("Close")
,wxPoint(GetSize().GetWidth()-100,of_ok_btn->GetPosition().y)
);
Connect(
OF_CANCEL_BUTTON
,wxEVT_COMMAND_BUTTON_CLICKED
,wxCommandEventHandler(AddonDialog::onCancelButtonClick)
);
setOFDir(saved_path);
// setOFDir fills and checks the addons checkboxes;
// when they're filled we get the currently selected ones.
prev_selected_addons = getSelectedAddons();
}
AddonDialog::~AddonDialog() {
delete of_dir_txt;
delete of_dir_btn;
delete of_ok_btn;
delete of_cancel_btn;
delete of_dir_box;
delete of_addon_box;
delete of_addons_list;
}
void AddonDialog::onOFDirButtonClick(wxCommandEvent & WXUNUSED(oEv)) {
Manager::Get()->GetLogManager()->Log(_T("ofDirSelect clicked"));
showOFDirDialog();
}
void AddonDialog::onOkButtonClick(wxCommandEvent & WXUNUSED(oEv)) {
Manager::Get()->GetLogManager()->Log(_T("OK clicked."));
Manager::Get()->GetLogManager()->Log(wxString::Format(wxT("total %i"),of_addons_list->GetCount()));
// close dialog.
EndModal(wxID_OK);
}
void AddonDialog::onCancelButtonClick(wxCommandEvent & WXUNUSED(oEv)) {
EndModal(wxID_CLOSE);
}
wxString AddonDialog::getAddonInstallFile(wxString sForAddon) {
for (int i = 0; i < addons.size(); ++i) {
if (addons[i].name == sForAddon) {
return addons[i].install_file;
}
}
return _T("");
}
void AddonDialog::showOFDirDialog() {
// get last selected directory.
ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("openframeworks"));
wxString saved_path = _T("");
if (cfg) {
saved_path = cfg->Read(_T("of_dir"),_T(""));
}
// select new directory.
wxDirDialog dir_dlg(this, _T("Select the openFramworks directory"), saved_path);
if (dir_dlg.ShowModal() == wxID_OK) {
wxString selected_path = dir_dlg.GetPath();
wxDir dir(selected_path);
// did the user select the correct oF directory?
if (dir.HasSubDirs(_T("addons"))
&& dir.HasSubDirs(_T("apps"))
&& dir.HasSubDirs(_T("libs"))
)
{
setOFDir(selected_path);
}
else {
// incorrect oF directory, show error.
wxMessageDialog *err = new wxMessageDialog(
NULL
,wxT("Incorrect openFrameworks location.\nPlease select the 'base' directory where you unpakked openFrameworks.\nThis directory contains the addons, libs, apps, etc. directories.")
,wxT("Error")
,wxOK | wxICON_ERROR
);
err->ShowModal();
delete err;
}
// store the selected path.
if (cfg) {
Manager::Get()->GetLogManager()->Log(_T("Saving ofdir"));
cfg->Write(_T("of_dir"), selected_path, false);
}
}
}
void AddonDialog::setOFDir(wxString sDir) {
of_dir = sDir;
addons.clear();
// find addons in given directory.
wxString addon_path = sDir
+ wxFileName::GetPathSeparator()
+_T("addons")
+wxFileName::GetPathSeparator();
findAddons(addon_path);
wxArrayString addons_array;
for(int i = 0; i < addons.size(); ++i) {
addons_array.Add(addons[i].name);
}
// clear the checkbox list and insert the new items.
of_addons_list->Clear();
of_addons_list->InsertItems(addons_array,0);
// find addons which are used in the project alreay.
std::vector<AddonFile> used_addons = getAddonsUsedInCurrentProject();
for (int i = 0; i < used_addons.size(); ++i) {
wxString used_addon = used_addons.at(i).name;
for(int j = 0; j < of_addons_list->GetCount();++j) {
if (of_addons_list->GetString(j) == used_addon) {
of_addons_list->Check(j);
}
}
}
}
std::vector<AddonFile> AddonDialog::findAddons(wxString inDir) {
wxDir dir(inDir);
Manager::Get()->GetLogManager()->Log(_T("Finding addons in"));
Manager::Get()->GetLogManager()->Log(inDir);
wxString file;
bool cont = dir.GetFirst(&file, wxEmptyString, wxDIR_DIRS);
while(cont) {
wxString addon_dir_name = inDir;
addon_dir_name.append(file);
wxDir addon_dir(addon_dir_name);
if (addon_dir.HasFiles(_T("install.xml"))) {
AddonFile addon_info = {
file
,addon_dir_name
,addon_dir_name
+wxFileName::GetPathSeparator()
+_T("install.xml")
};
addons.push_back(addon_info );
}
cont = dir.GetNext(&file);
}
return addons;
}
std::vector<AddonFile> AddonDialog::getFoundAddons() {
return addons;
}
std::vector<AddonFile> AddonDialog::getDeselectedAddons() {
std::vector<AddonFile> prev_selected = getPreviouslySelectedAddons();
std::vector<AddonFile> new_selected = getSelectedAddons();
std::vector<AddonFile> deselected;
for(std::vector<AddonFile>::const_iterator p = prev_selected.begin(); p != prev_selected.end(); ++p) {
bool still_selected = false;
for(std::vector<AddonFile>::const_iterator k = new_selected.begin(); k != new_selected.end(); ++k) {
if (p->name == k->name) {
still_selected = true;
break;
}
}
if (!still_selected) {
deselected.push_back(*p);
}
}
return deselected;
}
std::vector<AddonFile> AddonDialog::getNewSelectedAddons() {
std::vector<AddonFile> selected = getSelectedAddons();
std::vector<AddonFile> used = getAddonsUsedInCurrentProject();
std::vector<AddonFile> new_selected;
for(int i = 0; i < selected.size(); ++i) {
bool is_new_addon = true;
for(int j = 0; j < used.size(); ++j) {
if (used.at(j).name == selected.at(i).name) {
is_new_addon = false;
break;
}
}
if (is_new_addon) {
new_selected.push_back(selected.at(i));
}
}
return new_selected;
}
std::vector<AddonFile> AddonDialog::getPreviouslySelectedAddons() {
return prev_selected_addons;
}
// returns a vector with all the selected addons in the checkbox list.
std::vector<AddonFile> AddonDialog::getSelectedAddons() {
std::vector<AddonFile> sel_addons;
for(int j = 0; j < of_addons_list->GetCount();++j) {
if (of_addons_list->IsChecked(j)) {
AddonFile addon_to_add = {
of_addons_list->GetString(j)
,_T("")
,getAddonInstallFile(of_addons_list->GetString(j))
};
sel_addons.push_back(addon_to_add);
}
}
return sel_addons;
}
std::vector<AddonFile> AddonDialog::getAddonsUsedInCurrentProject() {
ProjectManager* project_mng = Manager::Get()->GetProjectManager();
cbProject* project = project_mng->GetActiveProject();
ProjectAnalyzer* analyzer = new ProjectAnalyzer();
analyzer->analyze(project);
std::vector<AddonFile> used_addons = analyzer->getUsedAddons();
delete analyzer;
return used_addons;
}