| 1 | CheatEditor* cheatEditor = nullptr; |
| 2 | |
| 3 | CheatEditor::CheatEditor() { |
| 4 | cheatList.setHeaderText({"Slot", "Code", "Description"}); |
| 5 | cheatList.setHeaderVisible(); |
| 6 | cheatList.setCheckable(); |
| 7 | for(unsigned n = 0; n < Codes; n++) cheatList.append({"", "", ""}); |
| 8 | codeLabel.setText("Code(s):"); |
| 9 | descLabel.setText("Description:"); |
| 10 | findButton.setText("Find Codes ..."); |
| 11 | resetButton.setText("Reset"); |
| 12 | eraseButton.setText("Erase"); |
| 13 | unsigned width = max( |
| 14 | Font::size(program->normalFont, "Codes(s)" ).width, |
| 15 | Font::size(program->normalFont, "Description:").width |
| 16 | ); |
| 17 | |
| 18 | append(cheatList, {~0, ~0}, 5); |
| 19 | append(codeLayout, {~0, 0}, 5); |
| 20 | codeLayout.append(codeLabel, {width, 0}, 5); |
| 21 | codeLayout.append(codeEdit, {~0, 0}); |
| 22 | append(descLayout, {~0, 0}, 5); |
| 23 | descLayout.append(descLabel, {width, 0}, 5); |
| 24 | descLayout.append(descEdit, {~0, 0}); |
| 25 | append(controlLayout, {~0, 0}); |
| 26 | controlLayout.append(findButton, {0, 0}, 5); |
| 27 | controlLayout.append(spacer, {~0, 0}); |
| 28 | controlLayout.append(resetButton, {80, 0}, 5); |
| 29 | controlLayout.append(eraseButton, {80, 0}); |
| 30 | |
| 31 | cheatList.onChange = {&CheatEditor::synchronize, this}; |
| 32 | cheatList.onToggle = [&](unsigned) { update(); }; |
| 33 | codeEdit.onChange = {&CheatEditor::updateCode, this}; |
| 34 | descEdit.onChange = {&CheatEditor::updateDesc, this}; |
| 35 | findButton.onActivate = {&CheatDatabase::findCodes, cheatDatabase}; |
| 36 | resetButton.onActivate = [&] { |
| 37 | if(MessageWindow().setParent(*tools).setText("All codes will be erased. Are you sure you want to do this?") |
| 38 | .question() == MessageWindow::Response::Yes) reset(); |
| 39 | }; |
| 40 | eraseButton.onActivate = {&CheatEditor::erase, this}; |
| 41 | |
| 42 | cheatList.setSelection(0); |
| 43 | synchronize(); |
| 44 | } |
| 45 | |
| 46 | void CheatEditor::synchronize() { |
| 47 | setEnabled(program->active); |
| 48 | |
| 49 | if(cheatList.selected()) { |
| 50 | unsigned n = cheatList.selection(); |
| 51 | codeEdit.setText(cheat[n].code); |
| 52 | descEdit.setText(cheat[n].desc); |
| 53 | codeEdit.setEnabled(true); |
| 54 | descEdit.setEnabled(true); |
| 55 | eraseButton.setEnabled(true); |
| 56 | } else { |
| 57 | codeEdit.setText(""); |
| 58 | descEdit.setText(""); |
| 59 | codeEdit.setEnabled(false); |
| 60 | descEdit.setEnabled(false); |
| 61 | eraseButton.setEnabled(false); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void CheatEditor::refresh() { |
| 66 | for(unsigned n = 0; n < Codes; n++) { |
| 67 | string code = cheat[n].code; |
| 68 | string desc = cheat[n].code.empty() && cheat[n].desc.empty() ? "(empty)" : cheat[n].desc; |
| 69 | lstring codes = code.split("+"); |
| 70 | if(codes.size() > 1) code = {codes[0], "+..."}; |
| 71 | cheatList.setText(n, {format<3>(1 + n), code, desc}); |
| 72 | } |
| 73 | cheatList.autoSizeColumns(); |
| 74 | } |
| 75 | |
| 76 | void CheatEditor::update() { |
| 77 | lstring codes; |
| 78 | for(unsigned n = 0; n < Codes; n++) { |
| 79 | string code = cheat[n].code; |
| 80 | if(cheatList.checked(n) && !code.empty()) codes.append(code); |
| 81 | } |
| 82 | system().cheatSet(codes); |
| 83 | } |
| 84 | |
| 85 | void CheatEditor::reset() { |
| 86 | for(unsigned n = 0; n < Codes; n++) { |
| 87 | cheatList.setChecked(n, false); |
| 88 | cheat[n].code = ""; |
| 89 | cheat[n].desc = ""; |
| 90 | } |
| 91 | codeEdit.setText(""); |
| 92 | descEdit.setText(""); |
| 93 | refresh(); |
| 94 | update(); |
| 95 | } |
| 96 | |
| 97 | void CheatEditor::erase() { |
| 98 | unsigned n = cheatList.selection(); |
| 99 | cheatList.setChecked(n, false); |
| 100 | cheat[n].code = ""; |
| 101 | cheat[n].desc = ""; |
| 102 | codeEdit.setText(""); |
| 103 | descEdit.setText(""); |
| 104 | refresh(); |
| 105 | update(); |
| 106 | } |
| 107 | |
| 108 | void CheatEditor::updateCode() { |
| 109 | unsigned n = cheatList.selection(); |
| 110 | cheat[n].code = codeEdit.text(); |
| 111 | refresh(); |
| 112 | update(); |
| 113 | } |
| 114 | |
| 115 | void CheatEditor::updateDesc() { |
| 116 | unsigned n = cheatList.selection(); |
| 117 | cheat[n].desc = descEdit.text(); |
| 118 | refresh(); |
| 119 | } |
| 120 | |
| 121 | bool CheatEditor::load(string filename) { |
| 122 | string data = string::read(filename); |
| 123 | if(data.empty()) return false; |
| 124 | |
| 125 | unsigned n = 0; |
| 126 | auto document = Markup::Document(data); |
| 127 | for(auto& node : document["cartridge"]) { |
| 128 | if(node.name != "cheat") continue; |
| 129 | cheatList.setChecked(n, node["enabled"].exists()); |
| 130 | cheat[n].code = node["code"].text(); |
| 131 | cheat[n].desc = node["description"].text(); |
| 132 | if(++n >= Codes) break; |
| 133 | } |
| 134 | |
| 135 | refresh(); |
| 136 | update(); |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | bool CheatEditor::save(string filename) { |
| 141 | signed lastSave = -1; |
| 142 | for(signed n = 127; n >= 0; n--) { |
| 143 | if(!cheat[n].code.empty() || !cheat[n].desc.empty()) { |
| 144 | lastSave = n; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if(lastSave == -1) { |
| 150 | file::remove(filename); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | file fp; |
| 155 | if(fp.open(filename, file::mode::write) == false) return false; |
| 156 | |
| 157 | fp.print("cartridge sha256:", system().sha256(), "\n"); |
| 158 | for(unsigned n = 0; n <= lastSave; n++) { |
| 159 | fp.print(" cheat", cheatList.checked(n) ? " enabled\n" : "\n"); |
| 160 | fp.print(" description:", cheat[n].desc, "\n"); |
| 161 | fp.print(" code:", cheat[n].code, "\n"); |
| 162 | } |
| 163 | fp.close(); |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool CheatEditor::import(string code, string desc) { |
| 169 | for(unsigned n = 0; n < Codes; n++) { |
| 170 | if(cheat[n].code.empty() && cheat[n].desc.empty()) { |
| 171 | cheatList.setChecked(n, false); |
| 172 | cheat[n].code = code; |
| 173 | cheat[n].desc = desc; |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | return false; |
| 178 | } |