Make various instance stuff to take references to other instance objs
[lsnes.git] / src / platform / wxwidgets / editor-authors.cpp
blob638ed2e7a05a33b872e2f6a3ea2a8322694ebae7
1 #include "core/command.hpp"
2 #include "core/dispatch.hpp"
3 #include "core/instance.hpp"
4 #include "core/mainloop.hpp"
5 #include "core/moviedata.hpp"
6 #include "core/project.hpp"
7 #include "library/zip.hpp"
9 #include "platform/wxwidgets/platform.hpp"
10 #include "platform/wxwidgets/loadsave.hpp"
12 #include <wx/wx.h>
13 #include <wx/event.h>
14 #include <wx/control.h>
15 #include <wx/combobox.h>
17 void do_flush_slotinfo();
19 class wxeditor_authors : public wxDialog
21 public:
22 wxeditor_authors(wxWindow* parent);
23 bool ShouldPreventAppExit() const;
24 void on_authors_change(wxCommandEvent& e);
25 void on_cancel(wxCommandEvent& e);
26 void on_ok(wxCommandEvent& e);
27 void on_dir_select(wxCommandEvent& e);
28 void on_add(wxCommandEvent& e);
29 void on_remove(wxCommandEvent& e);
30 void on_down(wxCommandEvent& e);
31 void on_up(wxCommandEvent& e);
32 void on_luasel(wxCommandEvent& e);
33 private:
34 void reorder_scripts(int delta);
35 wxTextCtrl* projectname;
36 wxTextCtrl* pname;
37 wxTextCtrl* directory;
38 wxTextCtrl* authors;
39 wxTextCtrl* projectpfx;
40 wxListBox* luascripts;
41 wxCheckBox* autorunlua;
42 wxButton* addbutton;
43 wxButton* removebutton;
44 wxButton* upbutton;
45 wxButton* downbutton;
46 wxButton* okbutton;
47 wxButton* cancel;
51 wxeditor_authors::wxeditor_authors(wxWindow* parent)
52 : wxDialog(parent, wxID_ANY, wxT("lsnes: Edit game name & authors"), wxDefaultPosition, wxSize(-1, -1))
54 project_info* proj = lsnes_instance.project.get();
55 Centre();
56 wxFlexGridSizer* top_s = new wxFlexGridSizer(proj ? 12 : 5, 1, 0, 0);
57 SetSizer(top_s);
59 if(proj) {
60 wxFlexGridSizer* c4_s = new wxFlexGridSizer(1, 2, 0, 0);
61 c4_s->Add(new wxStaticText(this, wxID_ANY, wxT("Project:")), 0, wxGROW);
62 c4_s->Add(pname = new wxTextCtrl(this, wxID_ANY, towxstring(proj->name),
63 wxDefaultPosition, wxSize(400, -1)), 1, wxGROW);
64 top_s->Add(c4_s);
66 wxFlexGridSizer* c2_s = new wxFlexGridSizer(1, 3, 0, 0);
67 c2_s->Add(new wxStaticText(this, wxID_ANY, wxT("Directory:")), 0, wxGROW);
68 c2_s->Add(directory = new wxTextCtrl(this, wxID_ANY, towxstring(proj->directory),
69 wxDefaultPosition, wxSize(350, -1)), 1, wxGROW);
70 wxButton* pdir;
71 c2_s->Add(pdir = new wxButton(this, wxID_ANY, wxT("...")), 1, wxGROW);
72 pdir->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
73 wxCommandEventHandler(wxeditor_authors::on_dir_select), NULL, this);
74 top_s->Add(c2_s);
76 wxFlexGridSizer* c3_s = new wxFlexGridSizer(1, 2, 0, 0);
77 c3_s->Add(new wxStaticText(this, wxID_ANY, wxT("Prefix:")), 0, wxGROW);
78 c3_s->Add(projectpfx = new wxTextCtrl(this, wxID_ANY, towxstring(proj->prefix),
79 wxDefaultPosition, wxSize(300, -1)), 1, wxGROW);
80 top_s->Add(c3_s);
82 } else {
83 directory = NULL;
84 pname = NULL;
85 wxFlexGridSizer* c2_s = new wxFlexGridSizer(1, 2, 0, 0);
86 c2_s->Add(new wxStaticText(this, wxID_ANY, wxT("Save slot prefix:")), 0, wxGROW);
87 c2_s->Add(projectpfx = new wxTextCtrl(this, wxID_ANY, towxstring(get_mprefix_for_project()),
88 wxDefaultPosition, wxSize(300, -1)), 1, wxGROW);
89 top_s->Add(c2_s);
92 wxFlexGridSizer* c_s = new wxFlexGridSizer(1, 2, 0, 0);
93 c_s->Add(new wxStaticText(this, wxID_ANY, wxT("Game name:")), 0, wxGROW);
94 c_s->Add(projectname = new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(400, -1)), 1,
95 wxGROW);
96 top_s->Add(c_s);
98 top_s->Add(new wxStaticText(this, wxID_ANY, wxT("Authors (one per line):")), 0, wxGROW);
99 top_s->Add(authors = new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize,
100 wxTE_MULTILINE), 0, wxGROW);
101 authors->Connect(wxEVT_COMMAND_TEXT_UPDATED,
102 wxCommandEventHandler(wxeditor_authors::on_authors_change), NULL, this);
104 if(proj) {
105 top_s->Add(new wxStaticText(this, wxID_ANY, wxT("Autoload lua scripts:")), 0, wxGROW);
106 top_s->Add(luascripts = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxSize(400, 100)), 1,
107 wxEXPAND);
108 luascripts->Connect(wxEVT_COMMAND_LISTBOX_SELECTED,
109 wxCommandEventHandler(wxeditor_authors::on_luasel), NULL, this);
111 wxFlexGridSizer* c3_s = new wxFlexGridSizer(1, 4, 0, 0);
112 c3_s->Add(addbutton = new wxButton(this, wxID_ANY, wxT("Add")), 1, wxGROW);
113 addbutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
114 wxCommandEventHandler(wxeditor_authors::on_add), NULL, this);
115 c3_s->Add(removebutton = new wxButton(this, wxID_ANY, wxT("Remove")), 1, wxGROW);
116 removebutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
117 wxCommandEventHandler(wxeditor_authors::on_remove), NULL, this);
118 removebutton->Disable();
119 c3_s->Add(upbutton = new wxButton(this, wxID_ANY, wxT("Up")), 1, wxGROW);
120 upbutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
121 wxCommandEventHandler(wxeditor_authors::on_up), NULL, this);
122 upbutton->Disable();
123 c3_s->Add(downbutton = new wxButton(this, wxID_ANY, wxT("Down")), 1, wxGROW);
124 downbutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
125 wxCommandEventHandler(wxeditor_authors::on_down), NULL, this);
126 downbutton->Disable();
127 top_s->Add(c3_s);
128 top_s->Add(autorunlua = new wxCheckBox(this, wxID_ANY, wxT("Autorun added Lua scripts")), 0, wxGROW);
129 autorunlua->SetValue(true);
130 } else {
131 luascripts = NULL;
132 addbutton = NULL;
133 removebutton = NULL;
134 upbutton = NULL;
135 downbutton = NULL;
136 autorunlua = NULL;
139 wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL);
140 pbutton_s->AddStretchSpacer();
141 pbutton_s->Add(okbutton = new wxButton(this, wxID_OK, wxT("OK")), 0, wxGROW);
142 pbutton_s->Add(cancel = new wxButton(this, wxID_CANCEL, wxT("Cancel")), 0, wxGROW);
143 okbutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
144 wxCommandEventHandler(wxeditor_authors::on_ok), NULL, this);
145 cancel->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
146 wxCommandEventHandler(wxeditor_authors::on_cancel), NULL, this);
147 top_s->Add(pbutton_s, 0, wxGROW);
149 c_s->SetSizeHints(this);
150 top_s->SetSizeHints(this);
151 Fit();
153 std::list<std::string> luascriptlist;
154 std::string gamename;
155 std::string x;
156 lsnes_instance.iqueue.run([&gamename, &x, &luascriptlist, proj]() {
157 if(proj) {
158 luascriptlist = proj->luascripts;
159 gamename = proj->gamename;
160 for(auto i : proj->authors)
161 x = x + i.first + "|" + i.second + "\n";
162 } else {
163 gamename = lsnes_instance.mlogic.get_mfile().gamename;
164 for(auto i : lsnes_instance.mlogic.get_mfile().authors)
165 x = x + i.first + "|" + i.second + "\n";
169 for(auto i : luascriptlist)
170 luascripts->Append(towxstring(i));
171 projectname->SetValue(towxstring(gamename));
172 authors->SetValue(towxstring(x));
175 bool wxeditor_authors::ShouldPreventAppExit() const
177 return false;
180 void wxeditor_authors::on_authors_change(wxCommandEvent& e)
182 try {
183 size_t lines = authors->GetNumberOfLines();
184 for(size_t i = 0; i < lines; i++) {
185 std::string l = tostdstring(authors->GetLineText(i));
186 if(l == "|")
187 throw 43;
189 okbutton->Enable();
190 } catch(...) {
191 okbutton->Disable();
195 void wxeditor_authors::on_cancel(wxCommandEvent& e)
197 EndModal(wxID_CANCEL);
200 void wxeditor_authors::on_ok(wxCommandEvent& e)
202 project_info* proj = lsnes_instance.project.get();
203 std::string gamename = tostdstring(projectname->GetValue());
204 std::string pfx = tostdstring(projectpfx->GetValue());
205 std::string dir = directory ? tostdstring(directory->GetValue()) : std::string("");
206 std::string prjname = pname ? tostdstring(pname->GetValue()) : std::string("");
207 std::vector<std::pair<std::string, std::string>> newauthors;
208 std::list<std::string> luascriptlist;
209 size_t lines = authors->GetNumberOfLines();
210 for(size_t i = 0; i < lines; i++) {
211 std::string l = tostdstring(authors->GetLineText(i));
212 if(l != "" && l != "|")
213 newauthors.push_back(split_author(l));
215 if(luascripts)
216 for(unsigned i = 0; i < luascripts->GetCount(); i++)
217 luascriptlist.push_back(tostdstring(luascripts->GetString(i)));
218 bool run_new = autorunlua ? autorunlua->GetValue() : false;
220 lsnes_instance.iqueue.run([gamename, newauthors, pfx, dir, prjname, luascriptlist, run_new, proj]() {
221 std::set<std::string> oldscripts;
222 if(proj) {
223 for(auto i : proj->luascripts)
224 oldscripts.insert(i);
225 proj->gamename = gamename;
226 proj->authors = newauthors;
227 proj->prefix = pfx;
228 proj->directory = dir;
229 proj->name = prjname;
230 proj->luascripts = luascriptlist;
231 proj->flush();
232 //For save status to immediately update.
233 do_flush_slotinfo();
234 update_movie_state();
235 notify_title_change();
236 } else {
237 lsnes_instance.mlogic.get_mfile().gamename = gamename;
238 lsnes_instance.mlogic.get_mfile().authors = newauthors;
239 set_mprefix_for_project(pfx);
241 if(run_new)
242 for(auto i : luascriptlist)
243 if(!oldscripts.count(i))
244 lsnes_instance.command.invoke("run-lua " + i);
246 EndModal(wxID_OK);
249 void wxeditor_authors::on_dir_select(wxCommandEvent& e)
251 wxDirDialog* d = new wxDirDialog(this, wxT("Select project directory"), directory->GetValue(),
252 wxDD_DIR_MUST_EXIST);
253 if(d->ShowModal() == wxID_CANCEL) {
254 d->Destroy();
255 return;
257 directory->SetValue(d->GetPath());
258 d->Destroy();
261 void wxeditor_authors::on_add(wxCommandEvent& e)
263 try {
264 std::string luascript = choose_file_load(this, "Pick lua script", ".", filetype_lua_script);
265 try {
266 auto& p = zip::openrel(luascript, "");
267 delete &p;
268 } catch(std::exception& e) {
269 show_message_ok(this, "File not found", "File '" + luascript + "' can't be opened",
270 wxICON_EXCLAMATION);
271 return;
273 luascripts->Append(towxstring(luascript));
274 } catch(...) {
278 void wxeditor_authors::on_remove(wxCommandEvent& e)
280 int sel = luascripts->GetSelection();
281 int count = luascripts->GetCount();
282 luascripts->Delete(sel);
283 if(sel < count - 1)
284 luascripts->SetSelection(sel);
285 else if(count > 1)
286 luascripts->SetSelection(count - 2);
287 else
288 luascripts->SetSelection(wxNOT_FOUND);
289 on_luasel(e);
292 void wxeditor_authors::reorder_scripts(int delta)
294 int sel = luascripts->GetSelection();
295 int count = luascripts->GetCount();
296 if(sel == wxNOT_FOUND || sel + delta >= count || sel + delta < 0)
297 return;
298 wxString a = luascripts->GetString(sel);
299 wxString b = luascripts->GetString(sel + delta);
300 luascripts->SetString(sel, b);
301 luascripts->SetString(sel + delta, a);
302 luascripts->SetSelection(sel + delta);
305 void wxeditor_authors::on_up(wxCommandEvent& e)
307 reorder_scripts(-1);
308 on_luasel(e);
311 void wxeditor_authors::on_down(wxCommandEvent& e)
313 reorder_scripts(1);
314 on_luasel(e);
317 void wxeditor_authors::on_luasel(wxCommandEvent& e)
319 int sel = luascripts->GetSelection();
320 int count = luascripts->GetCount();
321 removebutton->Enable(sel != wxNOT_FOUND);
322 upbutton->Enable(sel != wxNOT_FOUND && sel > 0);
323 downbutton->Enable(sel != wxNOT_FOUND && sel < count - 1);
326 void wxeditor_authors_display(wxWindow* parent)
328 modal_pause_holder hld;
329 if(!lsnes_instance.mlogic) {
330 show_message_ok(parent, "No movie", "Can't edit authors of nonexistent movie", wxICON_EXCLAMATION);
331 return;
333 wxDialog* editor;
334 try {
335 editor = new wxeditor_authors(parent);
336 editor->ShowModal();
337 } catch(...) {
338 return;
340 editor->Destroy();