| 1 | StateManager* stateManager = nullptr; |
| 2 | |
| 3 | StateManager::StateManager() { |
| 4 | stateList.setHeaderText({"Slot", "Description"}); |
| 5 | stateList.setHeaderVisible(); |
| 6 | for(unsigned n = 0; n < Slots; n++) stateList.append({format<2>(1 + n), "(empty)"}); |
| 7 | stateList.autoSizeColumns(); |
| 8 | descLabel.setText("Description:"); |
| 9 | saveButton.setText("Save"); |
| 10 | loadButton.setText("Load"); |
| 11 | resetButton.setText("Reset"); |
| 12 | eraseButton.setText("Erase"); |
| 13 | |
| 14 | append(stateList, {~0, ~0}, 5); |
| 15 | append(descLayout, {~0, 0}, 5); |
| 16 | descLayout.append(descLabel, {0, 0}, 5); |
| 17 | descLayout.append(descEdit, {~0, 0}); |
| 18 | append(controlLayout, {~0, 0}); |
| 19 | controlLayout.append(saveButton, {80, 0}, 5); |
| 20 | controlLayout.append(loadButton, {80, 0}, 5); |
| 21 | controlLayout.append(spacer, {~0, 0}); |
| 22 | controlLayout.append(resetButton, {80, 0}, 5); |
| 23 | controlLayout.append(eraseButton, {80, 0}); |
| 24 | |
| 25 | stateList.onChange = {&StateManager::synchronize, this}; |
| 26 | stateList.onActivate = {&StateManager::slotLoad, this}; |
| 27 | descEdit.onChange = {&StateManager::slotSaveDescription, this}; |
| 28 | saveButton.onActivate = {&StateManager::slotSave, this}; |
| 29 | loadButton.onActivate = {&StateManager::slotLoad, this}; |
| 30 | resetButton.onActivate = [&] { |
| 31 | if(MessageWindow().setParent(*tools).setText("All states will be erased. Are you sure you want to do this?") |
| 32 | .question() == MessageWindow::Response::Yes) reset(); |
| 33 | }; |
| 34 | eraseButton.onActivate = {&StateManager::slotErase, this}; |
| 35 | |
| 36 | stateList.setSelection(0); |
| 37 | synchronize(); |
| 38 | } |
| 39 | |
| 40 | void StateManager::synchronize() { |
| 41 | setEnabled(program->active); |
| 42 | |
| 43 | descEdit.setText(""); |
| 44 | descEdit.setEnabled(false); |
| 45 | controlLayout.setEnabled(stateList.selected()); |
| 46 | if(stateList.selected() == false) return; |
| 47 | |
| 48 | if(slot[stateList.selection()].capacity() > 0) { |
| 49 | descEdit.setText(slotLoadDescription(stateList.selection())); |
| 50 | descEdit.setEnabled(true); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void StateManager::refresh() { |
| 55 | for(unsigned n = 0; n < Slots; n++) { |
| 56 | stateList.setText(n, {format<2>(1 + n), slotLoadDescription(n)}); |
| 57 | } |
| 58 | stateList.autoSizeColumns(); |
| 59 | } |
| 60 | |
| 61 | void StateManager::reset() { |
| 62 | for(auto& slot : this->slot) slot = serializer(); |
| 63 | synchronize(); |
| 64 | refresh(); |
| 65 | } |
| 66 | |
| 67 | bool StateManager::load(string filename, unsigned revision) { |
| 68 | for(auto& slot : this->slot) slot = serializer(); |
| 69 | synchronize(); |
| 70 | |
| 71 | file fp; |
| 72 | if(fp.open(filename, file::mode::read) == false) return false; |
| 73 | |
| 74 | if(fp.readl(4) == 0x31415342 /* 'BSA1' */ && fp.readl(4) == revision) { |
| 75 | for(auto &slot : this->slot) { |
| 76 | if(fp.read() == false) continue; //slot is empty |
| 77 | unsigned size = fp.readl(4); |
| 78 | uint8_t* data = new uint8_t[size]; |
| 79 | fp.read(data, size); |
| 80 | slot = serializer(data, size); |
| 81 | delete[] data; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | refresh(); |
| 86 | synchronize(); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool StateManager::save(string filename, unsigned revision) { |
| 91 | bool hasSave = false; |
| 92 | for(auto& slot : this->slot) hasSave |= slot.capacity() > 0; |
| 93 | if(hasSave == false) { |
| 94 | file::remove(filename); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | directory::create(dir(filename)); |
| 99 | |
| 100 | file fp; |
| 101 | if(fp.open(filename, file::mode::write) == false) return false; |
| 102 | |
| 103 | fp.writel(0x31415342, 4); //'BSA1' |
| 104 | fp.writel(revision, 4); |
| 105 | for(auto& slot : this->slot) { |
| 106 | fp.write(slot.capacity() > 0); |
| 107 | if(slot.capacity()) { |
| 108 | fp.writel(slot.capacity(), 4); |
| 109 | fp.write(slot.data(), slot.capacity()); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void StateManager::slotLoad() { |
| 115 | if(stateList.selected() == false) return; |
| 116 | serializer s(slot[stateList.selection()].data(), slot[stateList.selection()].capacity()); |
| 117 | system().unserialize(s); |
| 118 | } |
| 119 | |
| 120 | void StateManager::slotSave() { |
| 121 | if(stateList.selected()) { |
| 122 | slot[stateList.selection()] = system().serialize(); |
| 123 | } |
| 124 | refresh(); |
| 125 | synchronize(); |
| 126 | descEdit.setFocused(); |
| 127 | } |
| 128 | |
| 129 | void StateManager::slotErase() { |
| 130 | if(stateList.selected()) { |
| 131 | slot[stateList.selection()] = serializer(); |
| 132 | } |
| 133 | refresh(); |
| 134 | synchronize(); |
| 135 | } |
| 136 | |
| 137 | string StateManager::slotLoadDescription(unsigned id) { |
| 138 | if(slot[id].capacity() == 0) return "(empty)"; |
| 139 | char text[DescriptionLength]; |
| 140 | strmcpy(text, (const char*)slot[id].data() + HeaderLength, DescriptionLength); |
| 141 | return text; |
| 142 | } |
| 143 | |
| 144 | void StateManager::slotSaveDescription() { |
| 145 | if(stateList.selected() == false) return; |
| 146 | string text = descEdit.text(); |
| 147 | if(slot[stateList.selection()].capacity() > 0) { |
| 148 | strmcpy((char*)slot[stateList.selection()].data() + HeaderLength, (const char*)text, DescriptionLength); |
| 149 | } |
| 150 | refresh(); |
| 151 | } |