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"
14 #include <wx/control.h>
15 #include <wx/combobox.h>
17 void do_flush_slotinfo();
19 class wxeditor_authors
: public wxDialog
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
);
34 void reorder_scripts(int delta
);
35 wxTextCtrl
* projectname
;
37 wxTextCtrl
* directory
;
39 wxTextCtrl
* projectpfx
;
40 wxListBox
* luascripts
;
41 wxCheckBox
* autorunlua
;
43 wxButton
* removebutton
;
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();
56 wxFlexGridSizer
* top_s
= new wxFlexGridSizer(proj
? 12 : 5, 1, 0, 0);
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
);
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
);
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);
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
);
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
);
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,
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);
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,
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);
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();
128 top_s
->Add(autorunlua
= new wxCheckBox(this, wxID_ANY
, wxT("Autorun added Lua scripts")), 0, wxGROW
);
129 autorunlua
->SetValue(true);
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);
153 std::list
<std::string
> luascriptlist
;
154 std::string gamename
;
156 lsnes_instance
.iqueue
.run([&gamename
, &x
, &luascriptlist
, proj
]() {
158 luascriptlist
= proj
->luascripts
;
159 gamename
= proj
->gamename
;
160 for(auto i
: proj
->authors
)
161 x
= x
+ i
.first
+ "|" + i
.second
+ "\n";
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
180 void wxeditor_authors::on_authors_change(wxCommandEvent
& e
)
183 size_t lines
= authors
->GetNumberOfLines();
184 for(size_t i
= 0; i
< lines
; i
++) {
185 std::string l
= tostdstring(authors
->GetLineText(i
));
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
));
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
;
223 for(auto i
: proj
->luascripts
)
224 oldscripts
.insert(i
);
225 proj
->gamename
= gamename
;
226 proj
->authors
= newauthors
;
228 proj
->directory
= dir
;
229 proj
->name
= prjname
;
230 proj
->luascripts
= luascriptlist
;
232 //For save status to immediately update.
234 update_movie_state();
235 notify_title_change();
237 lsnes_instance
.mlogic
.get_mfile().gamename
= gamename
;
238 lsnes_instance
.mlogic
.get_mfile().authors
= newauthors
;
239 set_mprefix_for_project(pfx
);
242 for(auto i
: luascriptlist
)
243 if(!oldscripts
.count(i
))
244 lsnes_instance
.command
.invoke("run-lua " + i
);
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
) {
257 directory
->SetValue(d
->GetPath());
261 void wxeditor_authors::on_add(wxCommandEvent
& e
)
264 std::string luascript
= choose_file_load(this, "Pick lua script", ".", filetype_lua_script
);
266 auto& p
= zip::openrel(luascript
, "");
268 } catch(std::exception
& e
) {
269 show_message_ok(this, "File not found", "File '" + luascript
+ "' can't be opened",
273 luascripts
->Append(towxstring(luascript
));
278 void wxeditor_authors::on_remove(wxCommandEvent
& e
)
280 int sel
= luascripts
->GetSelection();
281 int count
= luascripts
->GetCount();
282 luascripts
->Delete(sel
);
284 luascripts
->SetSelection(sel
);
286 luascripts
->SetSelection(count
- 2);
288 luascripts
->SetSelection(wxNOT_FOUND
);
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)
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
)
311 void wxeditor_authors::on_down(wxCommandEvent
& 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
);
335 editor
= new wxeditor_authors(parent
);