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"
19 #include <wx/control.h>
20 #include <wx/combobox.h>
21 #include <wx/statline.h>
23 class wxeditor_autohold
: public wxDialog
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
);
32 struct dispatch_target
<unsigned, unsigned, unsigned, bool> ahupdate
;
33 struct dispatch_target
<unsigned, unsigned, unsigned, unsigned, unsigned> afupdate
;
34 struct dispatch_target
<> ahreconfigure
;
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
59 std::map
<int, control_triple
> autoholds
;
60 std::vector
<controller_double
> panels
;
61 void update_controls();
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))
78 hsizer
= new wxBoxSizer(wxHORIZONTAL
);
80 Connect(wxEVT_CLOSE_WINDOW
, wxCloseEventHandler(wxeditor_autohold::on_wclose
));
82 hsizer
->SetSizeHints(this);
85 ahupdate
.set(notify_autohold_update
, [this](unsigned port
, unsigned controller
, unsigned ctrlnum
,
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
);
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]() {
110 this->update_controls();
111 } catch(std::runtime_error
& e
) {
115 autohold_open
= NULL
;
123 void wxeditor_autohold::on_checkbox(wxCommandEvent
& e
)
126 if(!autoholds
.count(id
))
128 auto t
= autoholds
[id
];
129 bool isaf
= (t
.afid
== id
);
130 bool newstate
= isaf
? t
.afcheck
->IsChecked() : t
.check
->IsChecked();
132 runemufn([t
, newstate
, &state
, 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" :
139 controls
.autofire2(t
.port
, t
.controller
, t
.index
, newstate
? 1 : 0, newstate
? 2 : 1);
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"))
145 controls
.autohold2(t
.port
, t
.controller
, t
.index
, newstate
);
150 t
.afcheck
->SetValue(state
);
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
);
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();
179 for(unsigned i
= 0;; i
++) {
180 auto pcid
= controls
.lcid_to_pcid(i
);
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
)
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
)
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
)
206 struct control_triple t
;
208 t
.controller
= pcid
.second
;
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);
215 _autoholds
.push_back(t
);
220 int next_id
= wxID_HIGHEST
+ 1;
221 unsigned last_logical
= 0xFFFFFFFFUL
;
224 wxSizer
* current_t
= NULL
;
225 for(auto i
: _autoholds
) {
226 if(i
.logical
!= last_logical
) {
227 //New controller starts.
229 hsizer
->Add(current_p
);
230 current_t
->SetSizeHints(current_p
);
231 current_t
->Fit(current_p
);
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
);
240 d
.label
= new wxStaticText(d
.panel
, wxID_ANY
, towxstring(_controller_labels
[i
.logical
]));
243 current
= d
.grid
= new wxFlexGridSizer(0, 3, 0, 0);
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
;
254 t
.controller
= i
.controller
;
259 t
.afid
= next_id
+ 1;
260 check
->SetValue(i
.status
);
261 check
->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED
, wxCommandEventHandler(wxeditor_autohold::on_checkbox
),
263 afcheck
->SetValue(i
.afstatus
);
264 afcheck
->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED
,
265 wxCommandEventHandler(wxeditor_autohold::on_checkbox
), NULL
, this);
268 current
->Add(afcheck
);
269 autoholds
[next_id
++] = t
;
270 autoholds
[next_id
++] = 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);
286 bool wxeditor_autohold::ShouldPreventAppExit() const { return false; }
288 void wxeditor_autohold::on_wclose(wxCloseEvent
& e
)
292 autohold_open
= NULL
;
297 void wxeditor_autohold_display(wxWindow
* parent
)
301 wxeditor_autohold
* v
;
303 v
= new wxeditor_autohold(parent
);
304 } catch(std::runtime_error
& e
) {
305 wxMessageBox(_T("No controllers present"), _T("Error"), wxICON_EXCLAMATION
| wxOK
, parent
);