If initsram/initstate points to LSS file, pull the matching member
[lsnes.git] / src / platform / wxwidgets / editor-authors.cpp
blobf910e1ddca46f50c49fcd81e97bd03bac972a5c3
1 #include <wx/wx.h>
2 #include <wx/event.h>
3 #include <wx/control.h>
4 #include <wx/combobox.h>
6 #include "core/instance.hpp"
7 #include "core/moviedata.hpp"
8 #include "core/ui-services.hpp"
9 #include "library/zip.hpp"
11 #include "platform/wxwidgets/platform.hpp"
12 #include "platform/wxwidgets/loadsave.hpp"
14 class wxeditor_authors : public wxDialog
16 public:
17 wxeditor_authors(wxWindow* parent, emulator_instance& _inst);
18 bool ShouldPreventAppExit() const;
19 void on_authors_change(wxCommandEvent& e);
20 void on_cancel(wxCommandEvent& e);
21 void on_ok(wxCommandEvent& e);
22 void on_dir_select(wxCommandEvent& e);
23 void on_add(wxCommandEvent& e);
24 void on_remove(wxCommandEvent& e);
25 void on_down(wxCommandEvent& e);
26 void on_up(wxCommandEvent& e);
27 void on_luasel(wxCommandEvent& e);
28 private:
29 void reorder_scripts(int delta);
30 emulator_instance& inst;
31 wxTextCtrl* gamename;
32 wxTextCtrl* pname;
33 wxTextCtrl* directory;
34 wxTextCtrl* authors;
35 wxTextCtrl* projectpfx;
36 wxListBox* luascripts;
37 wxCheckBox* autorunlua;
38 wxButton* addbutton;
39 wxButton* removebutton;
40 wxButton* upbutton;
41 wxButton* downbutton;
42 wxButton* okbutton;
43 wxButton* cancel;
47 wxeditor_authors::wxeditor_authors(wxWindow* parent, emulator_instance& _inst)
48 : wxDialog(parent, wxID_ANY, wxT("lsnes: Edit game name & authors"), wxDefaultPosition, wxSize(-1, -1)),
49 inst(_inst)
51 CHECK_UI_THREAD;
52 project_author_info ainfo = UI_load_author_info(inst);
53 Centre();
54 wxFlexGridSizer* top_s = new wxFlexGridSizer(ainfo.is_project ? 12 : 5, 1, 0, 0);
55 SetSizer(top_s);
57 if(ainfo.is_project) {
58 wxFlexGridSizer* c4_s = new wxFlexGridSizer(1, 2, 0, 0);
59 c4_s->Add(new wxStaticText(this, wxID_ANY, wxT("Project:")), 0, wxGROW);
60 c4_s->Add(pname = new wxTextCtrl(this, wxID_ANY, towxstring(ainfo.projectname),
61 wxDefaultPosition, wxSize(400, -1)), 1, wxGROW);
62 top_s->Add(c4_s);
64 wxFlexGridSizer* c2_s = new wxFlexGridSizer(1, 3, 0, 0);
65 c2_s->Add(new wxStaticText(this, wxID_ANY, wxT("Directory:")), 0, wxGROW);
66 c2_s->Add(directory = new wxTextCtrl(this, wxID_ANY, towxstring(ainfo.directory),
67 wxDefaultPosition, wxSize(350, -1)), 1, wxGROW);
68 wxButton* pdir;
69 c2_s->Add(pdir = new wxButton(this, wxID_ANY, wxT("...")), 1, wxGROW);
70 pdir->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
71 wxCommandEventHandler(wxeditor_authors::on_dir_select), NULL, this);
72 top_s->Add(c2_s);
74 wxFlexGridSizer* c3_s = new wxFlexGridSizer(1, 2, 0, 0);
75 c3_s->Add(new wxStaticText(this, wxID_ANY, wxT("Prefix:")), 0, wxGROW);
76 c3_s->Add(projectpfx = new wxTextCtrl(this, wxID_ANY, towxstring(ainfo.prefix),
77 wxDefaultPosition, wxSize(300, -1)), 1, wxGROW);
78 top_s->Add(c3_s);
79 } else {
80 directory = NULL;
81 pname = NULL;
82 wxFlexGridSizer* c2_s = new wxFlexGridSizer(1, 2, 0, 0);
83 c2_s->Add(new wxStaticText(this, wxID_ANY, wxT("Save slot prefix:")), 0, wxGROW);
84 c2_s->Add(projectpfx = new wxTextCtrl(this, wxID_ANY, towxstring(ainfo.prefix),
85 wxDefaultPosition, wxSize(300, -1)), 1, wxGROW);
86 top_s->Add(c2_s);
89 wxFlexGridSizer* c_s = new wxFlexGridSizer(1, 2, 0, 0);
90 c_s->Add(new wxStaticText(this, wxID_ANY, wxT("Game name:")), 0, wxGROW);
91 c_s->Add(gamename = new wxTextCtrl(this, wxID_ANY, towxstring(ainfo.gamename), wxDefaultPosition,
92 wxSize(400, -1)), 1, wxGROW);
93 top_s->Add(c_s);
95 top_s->Add(new wxStaticText(this, wxID_ANY, wxT("Authors (one per line):")), 0, wxGROW);
96 top_s->Add(authors = new wxTextCtrl(this, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize,
97 wxTE_MULTILINE), 0, wxGROW);
98 authors->Connect(wxEVT_COMMAND_TEXT_UPDATED,
99 wxCommandEventHandler(wxeditor_authors::on_authors_change), NULL, this);
101 if(ainfo.is_project) {
102 top_s->Add(new wxStaticText(this, wxID_ANY, wxT("Autoload lua scripts:")), 0, wxGROW);
103 top_s->Add(luascripts = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxSize(400, 100)), 1,
104 wxEXPAND);
105 luascripts->Connect(wxEVT_COMMAND_LISTBOX_SELECTED,
106 wxCommandEventHandler(wxeditor_authors::on_luasel), NULL, this);
108 wxFlexGridSizer* c3_s = new wxFlexGridSizer(1, 4, 0, 0);
109 c3_s->Add(addbutton = new wxButton(this, wxID_ANY, wxT("Add")), 1, wxGROW);
110 addbutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
111 wxCommandEventHandler(wxeditor_authors::on_add), NULL, this);
112 c3_s->Add(removebutton = new wxButton(this, wxID_ANY, wxT("Remove")), 1, wxGROW);
113 removebutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
114 wxCommandEventHandler(wxeditor_authors::on_remove), NULL, this);
115 removebutton->Disable();
116 c3_s->Add(upbutton = new wxButton(this, wxID_ANY, wxT("Up")), 1, wxGROW);
117 upbutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
118 wxCommandEventHandler(wxeditor_authors::on_up), NULL, this);
119 upbutton->Disable();
120 c3_s->Add(downbutton = new wxButton(this, wxID_ANY, wxT("Down")), 1, wxGROW);
121 downbutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
122 wxCommandEventHandler(wxeditor_authors::on_down), NULL, this);
123 downbutton->Disable();
124 top_s->Add(c3_s);
125 top_s->Add(autorunlua = new wxCheckBox(this, wxID_ANY, wxT("Autorun added Lua scripts")), 0, wxGROW);
126 autorunlua->SetValue(true);
127 } else {
128 luascripts = NULL;
129 addbutton = NULL;
130 removebutton = NULL;
131 upbutton = NULL;
132 downbutton = NULL;
133 autorunlua = NULL;
136 wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL);
137 pbutton_s->AddStretchSpacer();
138 pbutton_s->Add(okbutton = new wxButton(this, wxID_OK, wxT("OK")), 0, wxGROW);
139 pbutton_s->Add(cancel = new wxButton(this, wxID_CANCEL, wxT("Cancel")), 0, wxGROW);
140 okbutton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
141 wxCommandEventHandler(wxeditor_authors::on_ok), NULL, this);
142 cancel->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
143 wxCommandEventHandler(wxeditor_authors::on_cancel), NULL, this);
144 top_s->Add(pbutton_s, 0, wxGROW);
146 c_s->SetSizeHints(this);
147 top_s->SetSizeHints(this);
148 Fit();
150 std::string x;
151 for(auto i : ainfo.authors)
152 x = x + i.first + "|" + i.second + "\n";
153 for(auto i : ainfo.luascripts)
154 luascripts->Append(towxstring(i));
155 authors->SetValue(towxstring(x));
158 bool wxeditor_authors::ShouldPreventAppExit() const
160 return false;
163 void wxeditor_authors::on_authors_change(wxCommandEvent& e)
165 CHECK_UI_THREAD;
166 try {
167 size_t lines = authors->GetNumberOfLines();
168 for(size_t i = 0; i < lines; i++) {
169 std::string l = tostdstring(authors->GetLineText(i));
170 if(l == "|")
171 throw 43;
173 okbutton->Enable();
174 } catch(...) {
175 okbutton->Disable();
179 void wxeditor_authors::on_cancel(wxCommandEvent& e)
181 CHECK_UI_THREAD;
182 EndModal(wxID_CANCEL);
185 void wxeditor_authors::on_ok(wxCommandEvent& e)
187 CHECK_UI_THREAD;
188 project_author_info ainfo;
189 ainfo.gamename = tostdstring(gamename->GetValue());
190 ainfo.prefix = tostdstring(projectpfx->GetValue());
191 ainfo.directory = directory ? tostdstring(directory->GetValue()) : std::string("");
192 ainfo.projectname = pname ? tostdstring(pname->GetValue()) : std::string("");
194 size_t lines = authors->GetNumberOfLines();
195 for(size_t i = 0; i < lines; i++) {
196 std::string l = tostdstring(authors->GetLineText(i));
197 if(l != "" && l != "|")
198 ainfo.authors.push_back(split_author(l));
201 if(luascripts)
202 for(unsigned i = 0; i < luascripts->GetCount(); i++)
203 ainfo.luascripts.push_back(tostdstring(luascripts->GetString(i)));
204 ainfo.autorunlua = autorunlua ? autorunlua->GetValue() : false;
206 UI_save_author_info(inst, ainfo);
207 EndModal(wxID_OK);
210 void wxeditor_authors::on_dir_select(wxCommandEvent& e)
212 CHECK_UI_THREAD;
213 wxDirDialog* d = new wxDirDialog(this, wxT("Select project directory"), directory->GetValue(),
214 wxDD_DIR_MUST_EXIST);
215 if(d->ShowModal() == wxID_CANCEL) {
216 d->Destroy();
217 return;
219 directory->SetValue(d->GetPath());
220 d->Destroy();
223 void wxeditor_authors::on_add(wxCommandEvent& e)
225 CHECK_UI_THREAD;
226 try {
227 std::string luascript = choose_file_load(this, "Pick lua script", ".", filetype_lua_script);
228 try {
229 auto& p = zip::openrel(luascript, "");
230 delete &p;
231 } catch(std::exception& e) {
232 show_message_ok(this, "File not found", "File '" + luascript + "' can't be opened",
233 wxICON_EXCLAMATION);
234 return;
236 luascripts->Append(towxstring(luascript));
237 } catch(...) {
241 void wxeditor_authors::on_remove(wxCommandEvent& e)
243 CHECK_UI_THREAD;
244 int sel = luascripts->GetSelection();
245 int count = luascripts->GetCount();
246 luascripts->Delete(sel);
247 if(sel < count - 1)
248 luascripts->SetSelection(sel);
249 else if(count > 1)
250 luascripts->SetSelection(count - 2);
251 else
252 luascripts->SetSelection(wxNOT_FOUND);
253 on_luasel(e);
256 void wxeditor_authors::reorder_scripts(int delta)
258 CHECK_UI_THREAD;
259 int sel = luascripts->GetSelection();
260 int count = luascripts->GetCount();
261 if(sel == wxNOT_FOUND || sel + delta >= count || sel + delta < 0)
262 return;
263 wxString a = luascripts->GetString(sel);
264 wxString b = luascripts->GetString(sel + delta);
265 luascripts->SetString(sel, b);
266 luascripts->SetString(sel + delta, a);
267 luascripts->SetSelection(sel + delta);
270 void wxeditor_authors::on_up(wxCommandEvent& e)
272 reorder_scripts(-1);
273 on_luasel(e);
276 void wxeditor_authors::on_down(wxCommandEvent& e)
278 reorder_scripts(1);
279 on_luasel(e);
282 void wxeditor_authors::on_luasel(wxCommandEvent& e)
284 CHECK_UI_THREAD;
285 int sel = luascripts->GetSelection();
286 int count = luascripts->GetCount();
287 removebutton->Enable(sel != wxNOT_FOUND);
288 upbutton->Enable(sel != wxNOT_FOUND && sel > 0);
289 downbutton->Enable(sel != wxNOT_FOUND && sel < count - 1);
292 void wxeditor_authors_display(wxWindow* parent, emulator_instance& inst)
294 CHECK_UI_THREAD;
295 modal_pause_holder hld;
296 if(!*inst.mlogic) {
297 show_message_ok(parent, "No movie", "Can't edit authors of nonexistent movie", wxICON_EXCLAMATION);
298 return;
300 wxDialog* editor;
301 try {
302 editor = new wxeditor_authors(parent, inst);
303 editor->ShowModal();
304 } catch(...) {
305 return;
307 editor->Destroy();