|
| 1 | +--[====[ |
| 2 | +
|
| 3 | +gui/autogems |
| 4 | +============ |
| 5 | +
|
| 6 | +A frontend for the `autogems` plugin that allows configuring the gem types to be cut. |
| 7 | +
|
| 8 | +The following controls apply to the gems currently listed: |
| 9 | +
|
| 10 | +- ``s``: Searches for matching gems |
| 11 | +- ``Shift+Enter``: Toggles the status of all listed gems |
| 12 | +
|
| 13 | +The following controls apply to the gems currenty listed, as well as gems listed |
| 14 | +*before* the current search with ``s``, if applicable: |
| 15 | +
|
| 16 | +- ``r``: Displays only "rock crystal" gems |
| 17 | +- ``c``: Displays only gems whose color matches the selected gem |
| 18 | +- ``m``: Displays only gems where at least one rough (uncut) gem is available |
| 19 | + somewhere on the map |
| 20 | +
|
| 21 | +This behavior is intended to allow for things like a search for "lazuli" |
| 22 | +followed by pressing ``c`` to select all gems with the same color as lapis |
| 23 | +lazuli (5 blue gems in vanilla DF), rather than further restricting that to gems |
| 24 | +with "lazuli" in their name (only 1). |
| 25 | +
|
| 26 | +``x`` clears all filters, which is currently the only way to undo filters |
| 27 | +(besides searching), and is useful to verify the gems selected. |
| 28 | +
|
| 29 | +]====] |
| 30 | + |
| 31 | +gui = require "gui" |
| 32 | +dialogs = require "gui.dialogs" |
| 33 | +widgets = require "gui.widgets" |
| 34 | +json = require "json" |
| 35 | + |
| 36 | +CONFIG_KEY = "autogems/config" |
| 37 | +blacklist = {} |
| 38 | + |
| 39 | +gems = {} |
| 40 | +for id, raw in pairs(df.global.world.raws.inorganics) do |
| 41 | + if raw.material.flags.IS_GEM then |
| 42 | + if blacklist[id] == nil then |
| 43 | + blacklist[id] = false |
| 44 | + end |
| 45 | + local name = raw.material.gem_name1 |
| 46 | + local color = raw.material.basic_color[0] + (8 * raw.material.basic_color[1]) |
| 47 | + local entry = { |
| 48 | + id = id, |
| 49 | + text = { |
| 50 | + { |
| 51 | + text = function() return blacklist[id] and 'X' or string.char(251) end, |
| 52 | + pen = function() return blacklist[id] and COLOR_LIGHTRED or COLOR_LIGHTGREEN end, |
| 53 | + }, |
| 54 | + ' ', |
| 55 | + { |
| 56 | + text = string.char(raw.material.tile), |
| 57 | + pen = color |
| 58 | + }, |
| 59 | + ' ', |
| 60 | + name, |
| 61 | + }, |
| 62 | + color = color, |
| 63 | + search_key = name, |
| 64 | + crystal = raw.material.flags.CRYSTAL_GLASSABLE, |
| 65 | + } |
| 66 | + table.insert(gems, entry) |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +local on_map_cache |
| 71 | +function getGemTypesOnMap() |
| 72 | + if not on_map_cache then |
| 73 | + on_map_cache = {} |
| 74 | + for _, item in pairs(df.global.world.items.other.ROUGH) do |
| 75 | + on_map_cache[item.mat_index] = true |
| 76 | + end |
| 77 | + end |
| 78 | + return on_map_cache |
| 79 | +end |
| 80 | + |
| 81 | +function configPath() |
| 82 | + return dfhack.getSavePath() .. '/autogems.json' |
| 83 | +end |
| 84 | + |
| 85 | +function load() |
| 86 | + if dfhack.filesystem.isfile(configPath()) then |
| 87 | + local data = json.decode_file(configPath()) |
| 88 | + if data.blacklist then |
| 89 | + for _, v in pairs(data.blacklist) do |
| 90 | + blacklist[v] = true |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +function save() |
| 97 | + local save_blacklist = {} |
| 98 | + for id in ipairs(df.global.world.raws.inorganics) do |
| 99 | + if blacklist[id] then |
| 100 | + table.insert(save_blacklist, id) |
| 101 | + end |
| 102 | + end |
| 103 | + json.encode_file( |
| 104 | + {blacklist = save_blacklist}, |
| 105 | + configPath(), |
| 106 | + {pretty = false} |
| 107 | + ) |
| 108 | + dfhack.run_command('autogems-reload') |
| 109 | +end |
| 110 | + |
| 111 | +Autogems = defclass(nil, gui.FramedScreen) |
| 112 | + |
| 113 | +Autogems.ATTRS{ |
| 114 | + frame_title = "autogems dashboard", |
| 115 | +} |
| 116 | + |
| 117 | +function Autogems:init() |
| 118 | + load() |
| 119 | + self:addviews{ |
| 120 | + widgets.FilteredList{ |
| 121 | + view_id = 'list', |
| 122 | + frame = {t=1, l=1, b=4, w=38}, |
| 123 | + choices = gems, |
| 124 | + on_submit = self:callback('toggle'), |
| 125 | + on_submit2 = self:callback('toggleAll'), |
| 126 | + edit_key = 'CUSTOM_S', |
| 127 | + }, |
| 128 | + widgets.Label{ |
| 129 | + view_id = 'main_controls', |
| 130 | + frame = {b=1, l=1}, |
| 131 | + text = { |
| 132 | + {key = 'CUSTOM_X', text = ": Clear filters, ", |
| 133 | + on_activate = self:callback('clearFilters'), |
| 134 | + enabled = function() return self.filtered end}, |
| 135 | + {key = 'CUSTOM_R', text = ": Rock crystal, ", |
| 136 | + on_activate = self:callback('showCrystal')}, |
| 137 | + {key = 'CUSTOM_C', text = ": Same color, ", |
| 138 | + on_activate = self:callback('filterByColor')}, |
| 139 | + {key = 'CUSTOM_M', text = ": On map", |
| 140 | + on_activate = self:callback('filterOnMap')}, |
| 141 | + NEWLINE, |
| 142 | + {key = 'LEAVESCREEN', text = ": Back, "}, |
| 143 | + {key = 'SELECT', text = ": Toggle, "}, |
| 144 | + {key = 'SEC_SELECT', text = ": Toggle all"} |
| 145 | + } |
| 146 | + }, |
| 147 | + widgets.Label{ |
| 148 | + view_id = 'edit_controls', |
| 149 | + frame = {b=1, l=1}, |
| 150 | + text = { |
| 151 | + {key = 'LEAVESCREEN', text = ": Clear, "}, |
| 152 | + {key = 'SELECT', text = ": Done"}, |
| 153 | + } |
| 154 | + }, |
| 155 | + } |
| 156 | + self:updateControls() |
| 157 | +end |
| 158 | + |
| 159 | +function Autogems:updateControls() |
| 160 | + self.subviews.main_controls.visible = not self.subviews.list.edit.active |
| 161 | + self.subviews.edit_controls.visible = self.subviews.list.edit.active |
| 162 | + if self.subviews.list.edit.active then |
| 163 | + self.filtered = true |
| 164 | + end |
| 165 | +end |
| 166 | + |
| 167 | +function Autogems:toggle(_, item) |
| 168 | + blacklist[item.id] = not blacklist[item.id] |
| 169 | +end |
| 170 | + |
| 171 | +function Autogems:toggleAll(_, item) |
| 172 | + local new_state = not blacklist[item.id] |
| 173 | + for _, item in pairs(self.subviews.list:getVisibleChoices()) do |
| 174 | + blacklist[item.id] = new_state |
| 175 | + end |
| 176 | +end |
| 177 | + |
| 178 | +function Autogems:clearFilters() |
| 179 | + self.subviews.list:setFilter('') |
| 180 | + self.subviews.list:setChoices(gems) |
| 181 | + self.filtered = false |
| 182 | +end |
| 183 | + |
| 184 | +function Autogems:showCrystal() |
| 185 | + local choices = {} |
| 186 | + for _, c in pairs(self.subviews.list:getChoices()) do |
| 187 | + if c.crystal then |
| 188 | + table.insert(choices, c) |
| 189 | + end |
| 190 | + end |
| 191 | + self.subviews.list:setChoices(choices) |
| 192 | + self.filtered = true |
| 193 | +end |
| 194 | + |
| 195 | +function Autogems:filterByColor() |
| 196 | + if not self.subviews.list:getSelected() then return end |
| 197 | + local choices = {} |
| 198 | + local color = select(2, self.subviews.list:getSelected()).color |
| 199 | + for _, c in pairs(self.subviews.list:getChoices()) do |
| 200 | + if c.color == color then |
| 201 | + table.insert(choices, c) |
| 202 | + end |
| 203 | + end |
| 204 | + self.subviews.list:setChoices(choices) |
| 205 | + self.filtered = true |
| 206 | +end |
| 207 | + |
| 208 | +function Autogems:filterOnMap() |
| 209 | + local choices = {} |
| 210 | + local on_map = getGemTypesOnMap() |
| 211 | + for _, c in pairs(self.subviews.list:getChoices()) do |
| 212 | + if on_map[c.id] then |
| 213 | + table.insert(choices, c) |
| 214 | + end |
| 215 | + end |
| 216 | + self.subviews.list:setChoices(choices) |
| 217 | + self.filtered = true |
| 218 | +end |
| 219 | + |
| 220 | +function Autogems:onInput(keys) |
| 221 | + if self.subviews.list.edit.active and (keys.SELECT or keys.LEAVESCREEN) then |
| 222 | + self.subviews.list.edit.active = false |
| 223 | + if keys.LEAVESCREEN then |
| 224 | + self.subviews.list:setFilter('') |
| 225 | + end |
| 226 | + elseif keys.LEAVESCREEN then |
| 227 | + self:dismiss() |
| 228 | + else |
| 229 | + self:inputToSubviews(keys) |
| 230 | + end |
| 231 | + self:updateControls() |
| 232 | +end |
| 233 | + |
| 234 | +function Autogems:onDismiss() |
| 235 | + save() |
| 236 | +end |
| 237 | + |
| 238 | +Autogems():show() |
0 commit comments