JSON-based controller descriptions
[lsnes.git] / src / platform / wxwidgets / editor-autohold.cpp
blob20dd7e6ef323aa88107e401e3c7f1f6c9d49462f
1 #include "core/controller.hpp"
2 #include "core/movie.hpp"
3 #include "core/moviedata.hpp"
4 #include "core/dispatch.hpp"
5 #include "core/window.hpp"
7 #include "interface/controller.hpp"
8 #include "core/mainloop.hpp"
9 #include "platform/wxwidgets/platform.hpp"
10 #include "platform/wxwidgets/textrender.hpp"
11 #include "library/minmax.hpp"
12 #include "library/string.hpp"
13 #include "library/utf8.hpp"
15 #include <algorithm>
16 #include <cstring>
17 #include <wx/wx.h>
18 #include <wx/event.h>
19 #include <wx/control.h>
20 #include <wx/combobox.h>
21 #include <wx/statline.h>
23 class wxeditor_autohold : public wxDialog
25 public:
26 wxeditor_autohold(wxWindow* parent);
27 ~wxeditor_autohold() throw();
28 bool ShouldPreventAppExit() const;
29 void on_wclose(wxCloseEvent& e);
30 void on_checkbox(wxCommandEvent& e);
31 private:
32 struct dispatch_target<unsigned, unsigned, unsigned, bool> ahupdate;
33 struct dispatch_target<unsigned, unsigned, unsigned, unsigned, unsigned> afupdate;
34 struct dispatch_target<> ahreconfigure;
36 struct control_triple
38 unsigned port;
39 unsigned controller;
40 unsigned index;
41 int afid;
42 wxStaticText* label; //Used only by UI version.
43 wxCheckBox* check; //Used only by UI version.
44 wxCheckBox* afcheck; //Used only by UI version.
45 bool status; //Used only by internal version.
46 bool afstatus; //Used only by internal version.
47 unsigned logical; //Logical controller. Internal only.
48 std::string name; //Name. Internal only.
50 struct controller_double
52 wxPanel* panel;
53 wxStaticBox* box;
54 wxStaticText* label;
55 wxSizer* rtop;
56 wxSizer* top;
57 wxSizer* grid;
59 std::map<int, control_triple> autoholds;
60 std::vector<controller_double> panels;
61 void update_controls();
62 bool closing;
63 wxBoxSizer* hsizer;
66 namespace
68 wxeditor_autohold* autohold_open;
71 wxeditor_autohold::~wxeditor_autohold() throw() {}
73 wxeditor_autohold::wxeditor_autohold(wxWindow* parent)
74 : wxDialog(parent, wxID_ANY, wxT("lsnes: Autohold/Autofire"), wxDefaultPosition, wxSize(-1, -1))
76 closing = false;
77 Centre();
78 hsizer = new wxBoxSizer(wxHORIZONTAL);
79 SetSizer(hsizer);
80 Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(wxeditor_autohold::on_wclose));
81 update_controls();
82 hsizer->SetSizeHints(this);
83 Fit();
85 ahupdate.set(notify_autohold_update, [this](unsigned port, unsigned controller, unsigned ctrlnum,
86 bool newstate) {
87 runuifun([this, port, controller, ctrlnum, newstate]() {
88 for(auto i : this->autoholds) {
89 if(i.second.port != port) continue;
90 if(i.second.controller != controller) continue;
91 if(i.second.index != ctrlnum) continue;
92 i.second.check->SetValue(newstate);
94 });
95 });
96 afupdate.set(notify_autofire_update, [this](unsigned port, unsigned controller, unsigned ctrlnum,
97 unsigned duty, unsigned cyclelen) {
98 runuifun([this, port, controller, ctrlnum, duty]() {
99 for(auto i : this->autoholds) {
100 if(i.second.port != port) continue;
101 if(i.second.controller != controller) continue;
102 if(i.second.index != ctrlnum) continue;
103 i.second.afcheck->SetValue(duty != 0);
107 ahreconfigure.set(notify_autohold_reconfigure, [this]() {
108 runuifun([this]() {
109 try {
110 this->update_controls();
111 } catch(std::runtime_error& e) {
112 //Close the window.
113 bool wasc = closing;
114 closing = true;
115 autohold_open = NULL;
116 if(!wasc)
117 Destroy();
123 void wxeditor_autohold::on_checkbox(wxCommandEvent& e)
125 int id = e.GetId();
126 if(!autoholds.count(id))
127 return;
128 auto t = autoholds[id];
129 bool isaf = (t.afid == id);
130 bool newstate = isaf ? t.afcheck->IsChecked() : t.check->IsChecked();
131 bool state = false;
132 runemufn([t, newstate, &state, isaf]() {
133 if(isaf) {
134 auto _state = controls.autofire2(t.port, t.controller, t.index);
135 state = (_state.first != 0);
136 if(lua_callback_do_button(t.port, t.controller, t.index, newstate ? "autofire 1 2" :
137 "autofire"))
138 return;
139 controls.autofire2(t.port, t.controller, t.index, newstate ? 1 : 0, newstate ? 2 : 1);
140 state = newstate;
141 } else {
142 state = controls.autohold2(t.port, t.controller, t.index);
143 if(lua_callback_do_button(t.port, t.controller, t.index, newstate ? "hold" : "unhold"))
144 return;
145 controls.autohold2(t.port, t.controller, t.index, newstate);
146 state = newstate;
149 if(isaf)
150 t.afcheck->SetValue(state);
151 else
152 t.check->SetValue(state);
155 void wxeditor_autohold::update_controls()
157 for(auto i : autoholds) {
158 if(i.first != i.second.afid)
159 i.second.label->Destroy();
160 if(i.first != i.second.afid)
161 i.second.check->Destroy();
162 if(i.first == i.second.afid)
163 i.second.afcheck->Destroy();
165 for(auto i : panels) {
166 hsizer->Detach(i.panel);
167 i.panel->Destroy();
169 autoholds.clear();
170 panels.clear();
171 std::vector<control_triple> _autoholds;
172 std::vector<std::string> _controller_labels;
173 runemufn([&_autoholds, &_controller_labels](){
174 std::map<std::string, unsigned> next_in_class;
175 controller_frame model = controls.get_blank();
176 const port_type_set& pts = model.porttypes();
177 unsigned pcnt = pts.ports();
178 unsigned cnum_g = 0;
179 for(unsigned i = 0;; i++) {
180 auto pcid = controls.lcid_to_pcid(i);
181 if(pcid.first < 0)
182 break;
183 const port_type& pt = pts.port_type(pcid.first);
184 const port_controller_set& pci = *(pt.controller_info);
185 if(pci.controllers.size() <= pcid.second)
186 continue;
187 const port_controller& pc = pci.controllers[pcid.second];
188 //First check that this has non-hidden buttons.
189 bool has_buttons = false;
190 for(unsigned k = 0; k < pc.buttons.size(); k++) {
191 const port_controller_button& pcb = pc.buttons[k];
192 if(pcb.type == port_controller_button::TYPE_BUTTON && !pcb.shadow)
193 has_buttons = true;
195 if(!has_buttons)
196 continue;
197 //Okay, a valid controller.
198 if(!next_in_class.count(pc.cclass))
199 next_in_class[pc.cclass] = 1;
200 uint32_t cnum = next_in_class[pc.cclass]++;
201 _controller_labels.push_back((stringfmt() << pc.cclass << "-" << cnum).str());
202 for(unsigned k = 0; k < pc.buttons.size(); k++) {
203 const port_controller_button& pcb = pc.buttons[k];
204 if(pcb.type != port_controller_button::TYPE_BUTTON || pcb.shadow)
205 continue;
206 struct control_triple t;
207 t.port = pcid.first;
208 t.controller = pcid.second;
209 t.index = k;
210 t.status = controls.autohold2(pcid.first, pcid.second, k);
211 auto h = controls.autofire2(pcid.first, pcid.second, k);
212 t.afstatus = (h.first > 0);
213 t.logical = cnum_g;
214 t.name = pcb.name;
215 _autoholds.push_back(t);
217 cnum_g++;
220 int next_id = wxID_HIGHEST + 1;
221 unsigned last_logical = 0xFFFFFFFFUL;
222 wxSizer* current;
223 wxPanel* current_p;
224 wxSizer* current_t = NULL;
225 for(auto i : _autoholds) {
226 if(i.logical != last_logical) {
227 //New controller starts.
228 if(current_t) {
229 hsizer->Add(current_p);
230 current_t->SetSizeHints(current_p);
231 current_t->Fit(current_p);
233 controller_double d;
234 current_p = d.panel = new wxPanel(this, wxID_ANY);
235 current_t = d.rtop = new wxBoxSizer(wxVERTICAL);
236 d.panel->SetSizer(d.rtop);
237 d.box = new wxStaticBox(d.panel, wxID_ANY, towxstring(_controller_labels[i.logical]));
238 d.top = new wxStaticBoxSizer(d.box, wxVERTICAL);
239 #ifdef __WXMAC__
240 d.label = new wxStaticText(d.panel, wxID_ANY, towxstring(_controller_labels[i.logical]));
241 d.top->Add(d.label);
242 #endif
243 current = d.grid = new wxFlexGridSizer(0, 3, 0, 0);
244 d.top->Add(d.grid);
245 d.rtop->Add(d.top);
246 panels.push_back(d);
247 last_logical = i.logical;
249 wxStaticText* label = new wxStaticText(current_p, wxID_ANY, towxstring(i.name));
250 wxCheckBox* check = new wxCheckBox(current_p, next_id, wxT("Hold"));
251 wxCheckBox* afcheck = new wxCheckBox(current_p, next_id + 1, wxT("Rapid"));
252 struct control_triple t;
253 t.port = i.port;
254 t.controller = i.controller;
255 t.index = i.index;
256 t.label = label;
257 t.check = check;
258 t.afcheck = afcheck;
259 t.afid = next_id + 1;
260 check->SetValue(i.status);
261 check->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(wxeditor_autohold::on_checkbox),
262 NULL, this);
263 afcheck->SetValue(i.afstatus);
264 afcheck->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED,
265 wxCommandEventHandler(wxeditor_autohold::on_checkbox), NULL, this);
266 current->Add(label);
267 current->Add(check);
268 current->Add(afcheck);
269 autoholds[next_id++] = t;
270 autoholds[next_id++] = t;
272 if(current_t) {
273 hsizer->Add(current_p);
274 current_t->SetSizeHints(current_p);
275 current_t->Fit(current_p);
277 if(_autoholds.empty()) {
278 throw std::runtime_error("No controlers");
280 hsizer->SetSizeHints(this);
281 hsizer->Layout();
282 hsizer->Fit(this);
283 Fit();
286 bool wxeditor_autohold::ShouldPreventAppExit() const { return false; }
288 void wxeditor_autohold::on_wclose(wxCloseEvent& e)
290 bool wasc = closing;
291 closing = true;
292 autohold_open = NULL;
293 if(!wasc)
294 Destroy();
297 void wxeditor_autohold_display(wxWindow* parent)
299 if(autohold_open)
300 return;
301 wxeditor_autohold* v;
302 try {
303 v = new wxeditor_autohold(parent);
304 } catch(std::runtime_error& e) {
305 wxMessageBox(_T("No controllers present"), _T("Error"), wxICON_EXCLAMATION | wxOK, parent);
306 return;
308 v->Show();
309 autohold_open = v;