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
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
);
29 void reorder_scripts(int delta
);
30 emulator_instance
& inst
;
33 wxTextCtrl
* directory
;
35 wxTextCtrl
* projectpfx
;
36 wxListBox
* luascripts
;
37 wxCheckBox
* autorunlua
;
39 wxButton
* removebutton
;
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)),
52 project_author_info ainfo
= UI_load_author_info(inst
);
54 wxFlexGridSizer
* top_s
= new wxFlexGridSizer(ainfo
.is_project
? 12 : 5, 1, 0, 0);
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
);
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
);
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);
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
);
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
);
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
);
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,
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);
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();
125 top_s
->Add(autorunlua
= new wxCheckBox(this, wxID_ANY
, wxT("Autorun added Lua scripts")), 0, wxGROW
);
126 autorunlua
->SetValue(true);
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);
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
163 void wxeditor_authors::on_authors_change(wxCommandEvent
& e
)
167 size_t lines
= authors
->GetNumberOfLines();
168 for(size_t i
= 0; i
< lines
; i
++) {
169 std::string l
= tostdstring(authors
->GetLineText(i
));
179 void wxeditor_authors::on_cancel(wxCommandEvent
& e
)
182 EndModal(wxID_CANCEL
);
185 void wxeditor_authors::on_ok(wxCommandEvent
& e
)
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
));
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
);
210 void wxeditor_authors::on_dir_select(wxCommandEvent
& e
)
213 wxDirDialog
* d
= new wxDirDialog(this, wxT("Select project directory"), directory
->GetValue(),
214 wxDD_DIR_MUST_EXIST
);
215 if(d
->ShowModal() == wxID_CANCEL
) {
219 directory
->SetValue(d
->GetPath());
223 void wxeditor_authors::on_add(wxCommandEvent
& e
)
227 std::string luascript
= choose_file_load(this, "Pick lua script", ".", filetype_lua_script
);
229 auto& p
= zip::openrel(luascript
, "");
231 } catch(std::exception
& e
) {
232 show_message_ok(this, "File not found", "File '" + luascript
+ "' can't be opened",
236 luascripts
->Append(towxstring(luascript
));
241 void wxeditor_authors::on_remove(wxCommandEvent
& e
)
244 int sel
= luascripts
->GetSelection();
245 int count
= luascripts
->GetCount();
246 luascripts
->Delete(sel
);
248 luascripts
->SetSelection(sel
);
250 luascripts
->SetSelection(count
- 2);
252 luascripts
->SetSelection(wxNOT_FOUND
);
256 void wxeditor_authors::reorder_scripts(int delta
)
259 int sel
= luascripts
->GetSelection();
260 int count
= luascripts
->GetCount();
261 if(sel
== wxNOT_FOUND
|| sel
+ delta
>= count
|| sel
+ delta
< 0)
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
)
276 void wxeditor_authors::on_down(wxCommandEvent
& e
)
282 void wxeditor_authors::on_luasel(wxCommandEvent
& e
)
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
)
295 modal_pause_holder hld
;
297 show_message_ok(parent
, "No movie", "Can't edit authors of nonexistent movie", wxICON_EXCLAMATION
);
302 editor
= new wxeditor_authors(parent
, inst
);