-- Smart Pickup — hjælp: manualen i spillet (brugerønske 20/10) -- "(?)" efter titlen på hver række med et hjælpeafsnit (klik på titlen), knappen Hjælp (hjælp til den markerede -- række, ellers indholdsfortegnelsen), hele manualen som indhold med links og søgning. Hjælpen vises i listen -- eller i redigeringsvinduet, hvor den blev åbnet; Tilbage går et skridt tilbage, til sidst ud af hjælpen. -- Data og ren logik: adHelp.lua. Indlæses efter de øvrige gui-filer: lægger sig om rebuild/fillEditPanel/ -- refreshLive/refreshButtons/switchTab/onFrameClose/openEditPanel/hideEditPanel. SmartPickupFrame.HELP_SUFFIX = " (?)" local INDENT = {[2] = "", [3] = " ", [4] = " "} function SmartPickupFrame:initHelpButtons(i18n) self.helpButtonInfo = {inputAction = InputAction.MENU_EXTRA_1, text = i18n:getText("spu_help_button"), callback = function() self:onClickHelp() end} self.helpBackButtonInfo = {inputAction = InputAction.MENU_BACK, text = i18n:getText("spu_help_back"), callback = function() self:helpBack() end} self.helpSearchButtonInfo = {inputAction = InputAction.MENU_EXTRA_1, text = i18n:getText("spu_help_search"), callback = function() self:onClickHelpSearch() end} self.helpTocButtonInfo = {inputAction = InputAction.MENU_EXTRA_2, text = i18n:getText("spu_help_toc"), callback = function() self:openHelpView({mode = "toc"}) end} self.helpViewButtons = {self.helpBackButtonInfo, self.helpSearchButtonInfo, self.helpTocButtonInfo} -- Hjælp-knappen hvor MENU_EXTRA_1 er fri: Måned/Marksiloer/Kæder/Indstillinger, lagerfanerne, guidens trin -- (ikke oversigten — dér er den "Gem uden at starte") og redigeringsvinduet for _, list in ipairs({self.monthButtons, self.storageButtons, self.guideButtons, self.panelButtons, self.panelButtonsNoDelete}) do if list ~= nil then table.insert(list, self.helpButtonInfo) end end end -- ---- tilstand ---- -- Hjælpen vises igen: i redigeringsvinduet kun vinduet (listen bagved bygges ikke om — review 20/10), ellers listen local function showHelp(self) if self.editPanelOpen and self:editPanelHooks() ~= nil then self:fillEditPanel(self.getAdEnv()) self:refreshButtons() self:restoreFocusIfLost() -- den fokuserede række blev slettet: fokus i vinduet else self:requestRebuild() end end function SmartPickupFrame:openHelpView(view) if self.helpView ~= nil then self.helpStack = self.helpStack or {} table.insert(self.helpStack, self.helpView) end self.helpView = view showHelp(self) end -- Tilbage: forrige hjælpevisning, ellers ud af hjælpen. ESC og knappen kan komme for samme tryk (panelClosedAt) function SmartPickupFrame:helpBack() if g_time ~= nil and self.panelClosedAt ~= nil and g_time - self.panelClosedAt < SmartPickupFrame.PANEL_ESC_DEBOUNCE_MS then return end self.panelClosedAt = g_time local stack = self.helpStack or {} self.helpView = table.remove(stack) self.helpStack = stack showHelp(self) end function SmartPickupFrame:closeHelp() self.helpView, self.helpStack = nil, {} end function SmartPickupFrame:openHelpAlias(alias) if alias == nil then return end self:openHelpView({mode = "section", id = alias.ref, alias = alias}) end function SmartPickupFrame:helpContext() local direction = nil if self.guide ~= nil and self.guide.draft ~= nil and self.guideKind == "flow" then direction = self.guide.draft.kind == "outbound" and "out" or "in" end return {guideKind = self.guideKind, direction = direction} end function SmartPickupFrame:aliasForData(data) local ctx = self:helpContext() return ADHelp.findAlias(ADHelp.get(), ADHelp.rowId(data, ctx), ctx.direction) end -- Knappen Hjælp: den markerede række, ellers guidens trin, ellers indholdsfortegnelsen function SmartPickupFrame:onClickHelp() self:safe(function(frame) local focused = SmartPickupFrame.getFocusedElement() local alias = focused ~= nil and frame:aliasForData(frame.rowData[focused]) or nil if alias == nil and frame.guide ~= nil then local ctx = frame:helpContext() alias = ADHelp.findAlias(ADHelp.get(), ADHelp.stepId(frame.guideKind, frame.guide.step), ctx.direction) end if alias ~= nil then frame:openHelpAlias(alias) else frame:openHelpView({mode = "toc"}) end end) end function SmartPickupFrame:onClickHelpSearch() self:safe(function(frame) local current = frame.helpView ~= nil and frame.helpView.query or "" TextInputDialog.show(SmartPickupFrame.onHelpSearchResult, frame, current, frame.i18n:getText("spu_help_searchTitle"), nil, 40, g_i18n:getText("button_ok")) end) end function SmartPickupFrame:onHelpSearchResult(value, clickOk) self:safe(function(frame) if not clickOk or value == nil or not tostring(value):match("%S") then return end frame:openHelpView({mode = "search", query = tostring(value)}) end) end -- Overskrift på et guide-trin med "(?)", klikbar når trinnet har et hjælpeafsnit function SmartPickupFrame:addHelpHeader(text, stepId) local ctx = self:helpContext() local alias = ADHelp.findAlias(ADHelp.get(), stepId, ctx.direction) if alias == nil then return self:addTextRow(text) end return self:addLinkRow(text .. SmartPickupFrame.HELP_SUFFIX, function(frame) frame:openHelpAlias(alias) end) end -- Klik på titlen (ikke hele rækken: et fejlklik ved valgpilene må ikke åbne hjælpen — review 20/10) local MOUSE_LEFT = Input ~= nil and Input.MOUSE_BUTTON_LEFT or 1 local function inside(element, x, y) local position, size = element.absPosition, element.absSize return position ~= nil and size ~= nil and x >= position[1] and x <= position[1] + size[1] and y >= position[2] and y <= position[2] + size[2] end function SmartPickupFrame:makeTitleClickable(row, title, action) local frame = self local baseMouseEvent = row.mouseEvent row.mouseEvent = function(element, posX, posY, isDown, isUp, button, eventUsed) local used = baseMouseEvent(element, posX, posY, isDown, isUp, button, eventUsed) local layout = element.spuLayout or frame.boxLayout local blockedByPanel = frame.editPanelOpen and layout == frame.boxLayout if not used and not eventUsed and not blockedByPanel and isUp and button == MOUSE_LEFT and inside(title, posX, posY) and inside(layout, posX, posY) then frame.pendingClick = action return true end return used end end -- "(?)" på rækker med et hjælpeafsnit; klik på titlen åbner det (valget selv virker som før). Deler flere rækker -- samme afsnit (én pr. stald, én pr. vare), får kun den første "(?)" — ryddeligt function SmartPickupFrame:decorateHelpRows(rows) local shown = {} for _, row in ipairs(rows or {}) do if not row.spuHelpDone then row.spuHelpDone = true local option = row:getDescendantByName("option") local alias = option ~= nil and self:aliasForData(self.rowData[option]) or nil if alias ~= nil and shown[alias.id] then alias = nil end local title = alias ~= nil and row:getDescendantByName("title") or nil if title ~= nil then shown[alias.id] = true title:setText(tostring(title.text or "") .. SmartPickupFrame.HELP_SUFFIX) self:makeTitleClickable(row, title, function(frame) frame:openHelpAlias(alias) end) end end end end -- ---- visning ---- local function addParagraphs(frame, paragraphs) for _, paragraph in ipairs(paragraphs or {}) do frame:addTextRow(paragraph) end end function SmartPickupFrame:fillHelpRows() local help, view, i18n = ADHelp.get(), self.helpView, self.i18n self:addTextRow(i18n:getText("spu_help_title")) if #help.sections == 0 then self:addTextRow(i18n:getText("spu_help_missing")) elseif view.mode == "search" then local hits = ADHelp.search(help.sections, view.query, ADHelp.SEARCH_MAX) self:addTextRow(string.format(i18n:getText("spu_help_results"), tostring(view.query), #hits)) for _, hit in ipairs(hits) do local id = hit.section.id self:addLinkRow(" " .. hit.section.title, function(frame) frame:openHelpView({mode = "section", id = id}) end) if hit.snippet ~= "" then self:addTextRow(" " .. hit.snippet) end end elseif view.mode == "section" and help.byId[view.id] ~= nil then local section, alias = help.byId[view.id], view.alias if alias ~= nil and #(alias.paragraphs or {}) > 0 then -- en indstilling (tabelrække): dens egen tekst, så link til hele afsnittet self:addTextRow(tostring(alias.title or section.title)) addParagraphs(self, alias.paragraphs) self:addLinkRow(string.format(i18n:getText("spu_help_whole"), section.title), function(frame) frame:openHelpView({mode = "section", id = section.id}) end) else self:addTextRow(section.title) addParagraphs(self, section.paragraphs) local index = help.indexOf[section.id] for _, child in ipairs(ADHelp.children(help.sections, index)) do local id = child.id self:addLinkRow(" → " .. child.title, function(frame) frame:openHelpView({mode = "section", id = id}) end) end local nextSection = help.sections[(index or 0) + 1] if nextSection ~= nil then local id = nextSection.id self:addLinkRow(string.format(i18n:getText("spu_help_next"), nextSection.title), function(frame) frame:openHelpView({mode = "section", id = id}) end) end end else -- indholdsfortegnelsen: alle afsnit som links self:addLinkRow(i18n:getText("spu_help_searchLink"), function(frame) frame:onClickHelpSearch() end) for _, section in ipairs(help.sections) do local id = section.id self:addLinkRow((INDENT[section.level] or "") .. section.title, function(frame) frame:openHelpView({mode = "section", id = id}) end) end end self:addLinkRow(i18n:getText("spu_help_backLink"), function(frame) frame:helpBack() end) end -- ---- krogene ---- local baseRebuild = SmartPickupFrame.rebuild function SmartPickupFrame:rebuild() if self.helpView ~= nil and not self.editPanelOpen then self:clearRows() self:setStaticRowsVisible(self.currentTab, nil) self:fillHelpRows() self:colorRows() self.boxLayout:invalidateLayout() self:restoreFocusIfLost() self:refreshButtons() return end baseRebuild(self) self:decorateHelpRows(self.clonedRows) end local baseFillEditPanel = SmartPickupFrame.fillEditPanel function SmartPickupFrame:fillEditPanel(adEnv) if self.helpView ~= nil and self:editPanelHooks() ~= nil then self:clearPanelRows() self:layoutEditPanel() self:withRowTarget(self.editLayout, function(frame) frame:fillHelpRows() end) self:colorLayout(self.editLayout) self.editLayout:invalidateLayout() return end baseFillEditPanel(self, adEnv) self:decorateHelpRows(self.panelRows) end -- mens hjælpen er åben: ingen tidsstyret genopbygning (rullepositionen bevares) local baseRefreshLive = SmartPickupFrame.refreshLive function SmartPickupFrame:refreshLive() if self.helpView ~= nil then if self.editPanelOpen then self:layoutEditPanel() end self:refreshButtons() return end baseRefreshLive(self) end local baseRefreshButtons = SmartPickupFrame.refreshButtons function SmartPickupFrame:refreshButtons() if self.helpView ~= nil then if self.menuButtonInfo ~= self.helpViewButtons then self.menuButtonInfo = self.helpViewButtons self:setMenuButtonInfo(self.helpViewButtons) end if self.setMenuButtonInfoDirty ~= nil then self:setMenuButtonInfoDirty() end return end baseRefreshButtons(self) end -- ESC i hjælpen: et skridt tilbage (ikke luk menuen/vinduet — review 20/10) local baseRequestClose = SmartPickupFrame.requestClose function SmartPickupFrame:requestClose(callback) if self.helpView ~= nil then self:helpBack() return false end return baseRequestClose(self, callback) end for _, name in ipairs({"switchTab", "onFrameClose", "openEditPanel", "hideEditPanel"}) do local base = SmartPickupFrame[name] SmartPickupFrame[name] = function(self, ...) self:closeHelp() return base(self, ...) end end