mirror of
https://github.com/bryanthaboi/gen1recomp.git
synced 2026-08-26 07:21:22 +02:00
b796c30278
Inset save-editor chrome and pickers into SafeArea so Save stays tappable on punch-hole phones (#917), replace move cycling with a ranked search modal that reflows on phones/RGxxx, and omit the engine version from window titles on stamped release builds.
148 lines
5.6 KiB
Lua
148 lines
5.6 KiB
Lua
-- Type-to-search item picker: the items panel's answer to the species
|
|
-- picker (panels/SpeciesPicker.lua), and built to the same shape on purpose.
|
|
--
|
|
-- Adding an item used to mean an inline catalog card wedged into the Items
|
|
-- tab: a search field and a scrolling list competing for height with the bag
|
|
-- and PC lists beside it, which on a phone left about three rows visible.
|
|
-- Adding a Pokemon was already a full-screen modal with the whole window to
|
|
-- work with, and there was no reason for the two to differ.
|
|
--
|
|
-- 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 PickerChrome = require("PickerChrome")
|
|
local PAL = Theme.PAL
|
|
|
|
local Picker = {}
|
|
|
|
local FIELD_ID = "item-picker"
|
|
|
|
function Picker.results(S)
|
|
local p = S.itemPicker
|
|
return Ops.itemSearch(S, p and p.query or "")
|
|
end
|
|
|
|
-- Enter commits the top match into whichever destination the picker was
|
|
-- opened for, 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 item matches that") end
|
|
return Picker.commit(S, Kit, hits[1])
|
|
end
|
|
|
|
-- One funnel for both destinations. The picker stays OPEN after a commit:
|
|
-- stocking a save means adding several items in a row, and reopening the
|
|
-- modal per item is the kind of friction the inline card at least did not
|
|
-- have. Escape / Close / tap-outside is the way out.
|
|
function Picker.commit(S, Kit, id)
|
|
local p = S.itemPicker
|
|
local dest = (p and p.dest) or "bag"
|
|
if dest == "pc" then return Ops.addToPc(S, id) end
|
|
return Ops.addToBag(S, id)
|
|
end
|
|
|
|
function Picker.draw(S, Kit, width, height)
|
|
local p = S.itemPicker
|
|
if not p then return end
|
|
local s = Kit.scale
|
|
|
|
-- The click that opened the picker is still the frame's click: the panel
|
|
-- 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.
|
|
if p.opened then
|
|
p.opened = nil
|
|
Kit.blockClicks = true
|
|
end
|
|
|
|
-- the scrim doubles as the "tap outside to cancel" target; it covers the
|
|
-- full window so unsafe bands (notch / home indicator) stay dimmed too
|
|
Theme.col(PAL.bg, 0.82)
|
|
love.graphics.rectangle("fill", 0, 0, width, height)
|
|
|
|
-- Card fills / centres in SafeArea so phones and RGxxx landscapes keep a
|
|
-- usable list (#917 / #715).
|
|
local x, y, w, h, pad = PickerChrome.card(Kit, width, height)
|
|
if Kit.press(0, 0, width, height) and not Kit.hit(x, y, w, h) then
|
|
Ops.closeItemPicker(S, Kit)
|
|
return
|
|
end
|
|
|
|
Kit.card(x, y, w, h)
|
|
local cx, cy = x + pad, y + pad
|
|
local inner = w - 2 * pad
|
|
|
|
local closeW = PickerChrome.closeSize(Kit)
|
|
local captionH = Kit.textHeight("caption")
|
|
local headH = math.max(captionH, closeW)
|
|
Kit.caption(cx, cy + (headH - captionH) / 2, "ADD AN ITEM")
|
|
if Kit.button(x + w - pad - closeW, cy + (headH - closeW) / 2, closeW, closeW, "x",
|
|
{ font = "small" }) then
|
|
Ops.closeItemPicker(S, Kit)
|
|
return
|
|
end
|
|
cy = cy + headH + 10 * s
|
|
|
|
-- Destination toggle. Which list an item lands in is the only real choice
|
|
-- here, so it is a pair of chips at the top rather than two buttons at the
|
|
-- bottom that each mean "commit, and also pick a destination".
|
|
local half = (inner - 8 * s) / 2
|
|
local destH = math.max(PickerChrome.tapMin(Kit), math.floor(30 * s))
|
|
if Kit.chip(cx, cy, half, destH, "-> BAG", p.dest ~= "pc", PAL.green, PAL.steel) then
|
|
p.dest = "bag"
|
|
end
|
|
if Kit.chip(cx + half + 8 * s, cy, half, destH, "-> PC", p.dest == "pc",
|
|
PAL.green, PAL.steel) then
|
|
p.dest = "pc"
|
|
end
|
|
cy = cy + destH + 10 * s
|
|
|
|
local fieldH = PickerChrome.fieldH(Kit)
|
|
p.query = Kit.textfield(FIELD_ID, cx, cy, inner, fieldH, p.query,
|
|
"type an item id")
|
|
cy = cy + fieldH + 10 * s
|
|
|
|
local hits = Picker.results(S)
|
|
local listH, rowH, rowGap, pagerH = PickerChrome.listMetrics(Kit, y, h, pad, 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))
|
|
-- wheel / touch drag scroll the modal list too; the shield is already
|
|
-- lowered for this layer, so Kit.scroll works here and only here
|
|
p.offset = Kit.scroll(cx, cy, inner, listH, p.offset, #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)
|
|
if Kit.row(cx, ry, inner, rowH, false, PAL.green, 9 * s) then
|
|
Picker.commit(S, Kit, id)
|
|
end
|
|
-- how many the save already holds, so a second add is an informed one
|
|
local have = (S.save.inventory and S.save.inventory[id])
|
|
or (Ops.pcItems(S) or {})[id]
|
|
local tail = have and ("x%d"):format(have) or ""
|
|
local tailW = Kit.textWidth("tiny", tail)
|
|
Kit.text("monoRow",
|
|
Kit.ellipsize("monoRow", id, inner - tailW - 30 * s), cx + 10 * s,
|
|
ry + (rowH - Kit.textHeight("monoRow")) / 2, PAL.text)
|
|
if tail ~= "" then
|
|
Kit.textRight("tiny", tail, cx + inner - 10 * s,
|
|
ry + (rowH - Kit.textHeight("tiny")) / 2, PAL.caption)
|
|
end
|
|
end
|
|
Kit.popClip()
|
|
Kit.scrollbar(cx, cy, inner, listH, p.offset, #hits, perPage)
|
|
end
|
|
|
|
p.offset = Kit.pager(cx, y + h - pad - pagerH, inner, p.offset, #hits, perPage)
|
|
end
|
|
|
|
return Picker
|