Fix zero luma corner case
[lsnes.git] / src / plat-wxwidgets / keyentry.cpp
blob09dc8abc46f1e6f946c2aca42b3bbf240612a12a
1 #include "core/keymapper.hpp"
3 #include "plat-wxwidgets/common.hpp"
4 #include "plat-wxwidgets/keyentry.hpp"
6 #define S_DONT_CARE "Ignore"
7 #define S_RELEASED "Released"
8 #define S_PRESSED "Pressed"
10 wx_key_entry::wx_key_entry(wxWindow* parent)
11 : wxDialog(parent, wxID_ANY, wxT("Specify key"), wxDefaultPosition, wxSize(-1, -1))
13 std::vector<wxString> keych;
15 std::set<std::string> mods = modifier::get_set();
16 std::set<std::string> keys = keygroup::get_keys();
17 Centre();
18 wxFlexGridSizer* top_s = new wxFlexGridSizer(2, 1, 0, 0);
19 SetSizer(top_s);
21 wxFlexGridSizer* t_s = new wxFlexGridSizer(mods.size() + 1, 3, 0, 0);
22 for(auto i : mods) {
23 t_s->Add(new wxStaticText(this, wxID_ANY, towxstring(i)), 0, wxGROW);
24 keyentry_mod_data m;
25 t_s->Add(m.pressed = new wxCheckBox(this, wxID_ANY, wxT("Pressed")), 0, wxGROW);
26 t_s->Add(m.unmasked = new wxCheckBox(this, wxID_ANY, wxT("Unmasked")), 0, wxGROW);
27 m.pressed->Disable();
28 modifiers[i] = m;
29 m.pressed->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED,
30 wxCommandEventHandler(wx_key_entry::on_change_setting), NULL, this);
31 m.unmasked->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED,
32 wxCommandEventHandler(wx_key_entry::on_change_setting), NULL, this);
34 for(auto i : keys)
35 keych.push_back(towxstring(i));
36 mainkey = new labeledcombobox(t_s, this, "Key", &keych[0], keych.size(), 0, true, this,
37 (wxObjectEventFunction)(&wx_key_entry::on_change_setting));
38 t_s->Add(new wxStaticText(this, wxID_ANY, wxT("")), 0, wxGROW);
39 top_s->Add(t_s);
41 wxBoxSizer* pbutton_s = new wxBoxSizer(wxHORIZONTAL);
42 pbutton_s->AddStretchSpacer();
43 pbutton_s->Add(ok = new wxButton(this, wxID_OK, wxT("OK")), 0, wxGROW);
44 pbutton_s->Add(cancel = new wxButton(this, wxID_CANCEL, wxT("Cancel")), 0, wxGROW);
45 ok->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
46 wxCommandEventHandler(wx_key_entry::on_ok), NULL, this);
47 cancel->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
48 wxCommandEventHandler(wx_key_entry::on_cancel), NULL, this);
49 top_s->Add(pbutton_s, 0, wxGROW);
51 t_s->SetSizeHints(this);
52 top_s->SetSizeHints(this);
53 Fit();
56 #define TMPFLAG_UNMASKED 65
57 #define TMPFLAG_UNMASKED_LINK_CHILD 2
58 #define TMPFLAG_UNMASKED_LINK_PARENT 68
59 #define TMPFLAG_PRESSED 8
60 #define TMPFLAG_PRESSED_LINK_CHILD 16
61 #define TMPFLAG_PRESSED_LINK_PARENT 32
63 void wx_key_entry::on_change_setting(wxCommandEvent& e)
65 for(auto& i : modifiers)
66 i.second.tmpflags = 0;
67 for(auto& i : modifiers) {
68 modifier* m = NULL;
69 try {
70 m = &modifier::lookup(i.first);
71 } catch(...) {
72 i.second.pressed->Disable();
73 i.second.unmasked->Disable();
74 continue;
76 std::string j = m->linked_name();
77 if(i.second.unmasked->GetValue())
78 i.second.tmpflags |= TMPFLAG_UNMASKED;
79 if(j != "") {
80 if(modifiers[j].unmasked->GetValue())
81 i.second.tmpflags |= TMPFLAG_UNMASKED_LINK_PARENT;
82 if(i.second.unmasked->GetValue())
83 modifiers[j].tmpflags |= TMPFLAG_UNMASKED_LINK_CHILD;
85 if(i.second.pressed->GetValue())
86 i.second.tmpflags |= TMPFLAG_PRESSED;
87 if(j != "") {
88 if(modifiers[j].pressed->GetValue())
89 i.second.tmpflags |= TMPFLAG_PRESSED_LINK_PARENT;
90 if(i.second.pressed->GetValue())
91 modifiers[j].tmpflags |= TMPFLAG_PRESSED_LINK_CHILD;
94 for(auto& i : modifiers) {
95 //Unmasked is to be enabled if neither unmasked link flag is set.
96 if(i.second.tmpflags & ((TMPFLAG_UNMASKED_LINK_CHILD | TMPFLAG_UNMASKED_LINK_PARENT) & ~64)) {
97 i.second.unmasked->SetValue(false);
98 i.second.unmasked->Disable();
99 } else
100 i.second.unmasked->Enable();
101 //Pressed is to be enabled if:
102 //- This modifier is unmasked or parent is unmasked.
103 //- Parent nor child is not pressed.
104 if(((i.second.tmpflags & (TMPFLAG_UNMASKED | TMPFLAG_UNMASKED_LINK_PARENT | TMPFLAG_PRESSED_LINK_CHILD |
105 TMPFLAG_PRESSED_LINK_PARENT)) & 112) == 64)
106 i.second.pressed->Enable();
107 else {
108 i.second.pressed->SetValue(false);
109 i.second.pressed->Disable();
114 void wx_key_entry::on_ok(wxCommandEvent& e)
116 EndModal(wxID_OK);
119 void wx_key_entry::on_cancel(wxCommandEvent& e)
121 EndModal(wxID_CANCEL);
124 std::string wx_key_entry::getkey()
126 std::string x;
127 bool f;
128 f = true;
129 for(auto i : modifiers) {
130 if(i.second.pressed->GetValue()) {
131 if(!f)
132 x = x + ",";
133 f = false;
134 x = x + i.first;
137 x = x + "/";
138 f = true;
139 for(auto i : modifiers) {
140 if(i.second.unmasked->GetValue()) {
141 if(!f)
142 x = x + ",";
143 f = false;
144 x = x + i.first;
147 x = x + "|" + mainkey->get_choice();
148 return x;