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();
178 for(unsigned i
= 0;; i
++) {
179 auto pcid
= controls
.lcid_to_pcid(i
);
182 const port_type
& pt
= pts
.port_type(pcid
.first
);
183 const port_controller_set
& pci
= *(pt
.controller_info
);
184 if((ssize_t
)pci
.controllers
.size() <= pcid
.second
)
186 const port_controller
& pc
= pci
.controllers
[pcid
.second
];
187 //First check that this has non-hidden buttons.
188 bool has_buttons
= false;
189 for(unsigned k
= 0; k
< pc
.buttons
.size(); k
++) {
190 const port_controller_button
& pcb
= pc
.buttons
[k
];
191 if(pcb
.type
== port_controller_button::TYPE_BUTTON
&& !pcb
.shadow
)
196 //Okay, a valid controller.
197 if(!next_in_class
.count(pc
.cclass
))
198 next_in_class
[pc
.cclass
] = 1;
199 uint32_t cnum
= next_in_class
[pc
.cclass
]++;
200 _controller_labels
.push_back((stringfmt() << pc
.cclass
<< "-" << cnum
).str());
201 for(unsigned k
= 0; k
< pc
.buttons
.size(); k
++) {
202 const port_controller_button
& pcb
= pc
.buttons
[k
];
203 if(pcb
.type
!= port_controller_button::TYPE_BUTTON
|| pcb
.shadow
)
205 struct control_triple t
;
207 t
.controller
= pcid
.second
;
209 t
.status
= controls
.autohold2(pcid
.first
, pcid
.second
, k
);
210 auto h
= controls
.autofire2(pcid
.first
, pcid
.second
, k
);
211 t
.afstatus
= (h
.first
> 0);
214 _autoholds
.push_back(t
);
219 int next_id
= wxID_HIGHEST
+ 1;
220 unsigned last_logical
= 0xFFFFFFFFUL
;
221 wxSizer
* current
= NULL
;
222 wxPanel
* current_p
= NULL
;
223 wxSizer
* current_t
= NULL
;
224 for(auto i
: _autoholds
) {
225 if(i
.logical
!= last_logical
) {
226 //New controller starts.
228 hsizer
->Add(current_p
);
229 current_t
->SetSizeHints(current_p
);
230 current_t
->Fit(current_p
);
233 current_p
= d
.panel
= new wxPanel(this, wxID_ANY
);
234 current_t
= d
.rtop
= new wxBoxSizer(wxVERTICAL
);
235 d
.panel
->SetSizer(d
.rtop
);
236 d
.box
= new wxStaticBox(d
.panel
, wxID_ANY
, towxstring(_controller_labels
[i
.logical
]));
237 d
.top
= new wxStaticBoxSizer(d
.box
, wxVERTICAL
);
239 d
.label
= new wxStaticText(d
.panel
, wxID_ANY
, towxstring(_controller_labels
[i
.logical
]));
242 current
= d
.grid
= new wxFlexGridSizer(0, 3, 0, 0);
246 last_logical
= i
.logical
;
248 wxStaticText
* label
= new wxStaticText(current_p
, wxID_ANY
, towxstring(i
.name
));
249 wxCheckBox
* check
= new wxCheckBox(current_p
, next_id
, wxT("Hold"));
250 wxCheckBox
* afcheck
= new wxCheckBox(current_p
, next_id
+ 1, wxT("Rapid"));
251 struct control_triple t
;
253 t
.controller
= i
.controller
;
258 t
.afid
= next_id
+ 1;
259 check
->SetValue(i
.status
);
260 check
->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED
, wxCommandEventHandler(wxeditor_autohold::on_checkbox
),
262 afcheck
->SetValue(i
.afstatus
);
263 afcheck
->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED
,
264 wxCommandEventHandler(wxeditor_autohold::on_checkbox
), NULL
, this);
267 current
->Add(afcheck
);
268 autoholds
[next_id
++] = t
;
269 autoholds
[next_id
++] = t
;
272 hsizer
->Add(current_p
);
273 current_t
->SetSizeHints(current_p
);
274 current_t
->Fit(current_p
);
276 if(_autoholds
.empty()) {
277 throw std::runtime_error("No controlers");
279 hsizer
->SetSizeHints(this);
285 bool wxeditor_autohold::ShouldPreventAppExit() const { return false; }
287 void wxeditor_autohold::on_wclose(wxCloseEvent
& e
)
291 autohold_open
= NULL
;
296 void wxeditor_autohold_display(wxWindow
* parent
)
300 wxeditor_autohold
* v
;
302 v
= new wxeditor_autohold(parent
);
303 } catch(std::runtime_error
& e
) {
304 wxMessageBox(_T("No controllers present"), _T("Error"), wxICON_EXCLAMATION
| wxOK
, parent
);