mirror of
https://github.com/bryanthaboi/gen1recomp.git
synced 2026-08-15 07:41:21 +02:00
CLOSES #415, CLOSES #484, CLOSES #488, CLOSES #492, CLOSES #497, CLOSES #541, CLOSES #559, CLOSES #562, CLOSES #563, CLOSES #564, CLOSES #565, CLOSES #566, CLOSES #567, CLOSES #568, CLOSES #569, CLOSES #570, CLOSES #571, CLOSES #572
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
-- Type-to-search species picker (#541). The inspector used to change species
|
||||
-- with a pair of arrows, which walked the catalog one entry at a time -- 151
|
||||
-- taps to cross the dex -- and ran a full MonOps recalculation on whatever
|
||||
-- record happened to be next, including records the Gen1 formulas cannot use.
|
||||
-- This is the replacement: one modal list, filtered as you type, committing
|
||||
-- through Ops.setSpecies so an unusable record refuses instead of crashing.
|
||||
--
|
||||
-- Modal is literal. Kit hit-tests without a z-order, so App.draw raises
|
||||
-- Kit.blockClicks over the chrome and the panel while this is open and lowers
|
||||
-- it only for this overlay; nothing underneath can take the same tap.
|
||||
|
||||
local Theme = require("Theme")
|
||||
local Ops = require("Ops")
|
||||
local MonEditor = require("MonEditor")
|
||||
local PAL = Theme.PAL
|
||||
|
||||
local Picker = {}
|
||||
|
||||
local FIELD_ID = "species-picker"
|
||||
|
||||
function Picker.results(S)
|
||||
local p = S.speciesPicker
|
||||
return Ops.speciesSearch(S, p and p.query or "")
|
||||
end
|
||||
|
||||
-- Enter commits the top match, which is the whole point of a search field.
|
||||
function Picker.commitFirst(S, Kit)
|
||||
local hits = Picker.results(S)
|
||||
if not hits[1] then return Ops.say(S, "No species matches that") end
|
||||
local ok = Ops.setSpecies(S, S.editingMon, hits[1])
|
||||
if ok then Ops.closeSpeciesPicker(S, Kit) end
|
||||
return ok
|
||||
end
|
||||
|
||||
function Picker.draw(S, Kit, width, height)
|
||||
local p = S.speciesPicker
|
||||
if not p then return end
|
||||
local s = Kit.scale
|
||||
|
||||
-- The click that opened the picker is still the frame's click: the
|
||||
-- inspector dispatches earlier in App.draw than this overlay does, so
|
||||
-- without swallowing it the scrim below would read it as a tap outside and
|
||||
-- shut the picker in the same frame it went up. App re-raises the shield
|
||||
-- at the top of the next frame, so leaving it up here is safe.
|
||||
if p.opened then
|
||||
p.opened = nil
|
||||
Kit.blockClicks = true
|
||||
end
|
||||
|
||||
-- the scrim doubles as the "tap outside to cancel" target
|
||||
Theme.col(PAL.bgBot, 0.72)
|
||||
love.graphics.rectangle("fill", 0, 0, width, height)
|
||||
|
||||
local w = math.min(width - 32 * s, 520 * s)
|
||||
local h = math.min(height - 32 * s, 560 * s)
|
||||
local x = (width - w) / 2
|
||||
local y = (height - h) / 2
|
||||
if Kit.press(0, 0, width, height) and not Kit.hit(x, y, w, h) then
|
||||
Ops.closeSpeciesPicker(S, Kit)
|
||||
return
|
||||
end
|
||||
|
||||
Kit.card(x, y, w, h)
|
||||
local pad = 18 * s
|
||||
local cx, cy = x + pad, y + pad
|
||||
local inner = w - 2 * pad
|
||||
|
||||
Kit.caption(cx, cy, "CHOOSE A SPECIES")
|
||||
local closeW = 30 * s
|
||||
if Kit.button(x + w - pad - closeW, cy - 4 * s, closeW, 26 * s, "x",
|
||||
{ font = "small", radius = 7 * s }) then
|
||||
Ops.closeSpeciesPicker(S, Kit)
|
||||
return
|
||||
end
|
||||
cy = cy + Kit.textHeight("caption") + 10 * s
|
||||
|
||||
local fieldH = 34 * s
|
||||
p.query = Kit.textfield(FIELD_ID, cx, cy, inner, fieldH, p.query,
|
||||
"type a name, an id, or a dex number")
|
||||
cy = cy + fieldH + 10 * s
|
||||
|
||||
local hits = Picker.results(S)
|
||||
local rowH = 40 * s
|
||||
local rowGap = 6 * s
|
||||
local pagerH = 30 * s
|
||||
local listH = (y + h - pad - pagerH - 10 * s) - cy
|
||||
local perPage = math.max(1, math.floor((listH + rowGap) / (rowH + rowGap)))
|
||||
p.offset = Theme.clamp(p.offset or 0, 0, math.max(0, #hits - perPage))
|
||||
|
||||
if #hits == 0 then
|
||||
Kit.emptyBox(cx, cy, inner, listH, "Nothing matches that.")
|
||||
else
|
||||
Kit.pushClip(cx, cy, inner, listH)
|
||||
for i = 1, perPage do
|
||||
local id = hits[p.offset + i]
|
||||
if not id then break end
|
||||
local ry = cy + (i - 1) * (rowH + rowGap)
|
||||
local def = S.data.pokemon[id]
|
||||
-- A record the formulas cannot use still lists, greyed: hiding it would
|
||||
-- make a modded species look like it never registered (#541).
|
||||
local usable = Ops.speciesUsable(S, id)
|
||||
local current = (S.editingMon and S.editingMon.species == id)
|
||||
if Kit.row(cx, ry, inner, rowH, current, PAL.green, 9 * s) then
|
||||
if Ops.setSpecies(S, S.editingMon, id) then
|
||||
Ops.closeSpeciesPicker(S, Kit)
|
||||
Kit.popClip()
|
||||
return
|
||||
end
|
||||
end
|
||||
local icon = rowH - 4 * s
|
||||
MonEditor.drawSprite(S, Kit, id, cx + 6 * s, ry + 2 * s, icon)
|
||||
local tx = cx + 6 * s + icon + 8 * s
|
||||
local tail = usable and ("#%03d"):format(tonumber(def and def.dex) or 0)
|
||||
or "no data"
|
||||
local tailW = Kit.textWidth("tiny", tail)
|
||||
Kit.text("monoRow",
|
||||
Kit.ellipsize("monoRow", id, inner - (tx - cx) - tailW - 20 * s), tx,
|
||||
ry + (rowH - Kit.textHeight("monoRow")) / 2,
|
||||
usable and PAL.text or PAL.faint)
|
||||
Kit.textRight("tiny", tail, cx + inner - 10 * s,
|
||||
ry + (rowH - Kit.textHeight("tiny")) / 2, PAL.caption)
|
||||
end
|
||||
Kit.popClip()
|
||||
end
|
||||
|
||||
p.offset = Kit.pager(cx, y + h - pad - pagerH, inner, p.offset, #hits, perPage)
|
||||
end
|
||||
|
||||
return Picker
|
||||
Reference in New Issue
Block a user