class_name UpgradePanel extends CenterContainer ## A shop's menu (Upgrades): one row per upgrade or item sold by this instance's shop (shop, ## Upgrades.shop_of), by track, with what its next level does and costs ("Rapid fire II" for one ## with levels). A done one says so ("Max" at the last level); one that can't be bought yet says ## why: what's missing, "Level 2 opens on day 8", or, for a shed item not in stock yet, just its ## name and "Arrives on day 6". The work area's instance (shop "workbench": player/weapon ## stat and buildable upgrades) opens with interact at the workbench by day; the supply shed's ## (shop "shed": weapons and other purchasable items) opens with interact at the shed's counter by ## day (Interactions). Buy with a click, or the arrows / d-pad and Enter / A. Close it with Close, ## Escape / B, interact again, by walking off, or at nightfall. The scarecrow stands still and ## can't shoot while either is open. Hand-drawn like the HUD's pouch panel (UiTheme.sketch): the ## title over a pen rule, the tracks' names dim like the pouch's column headings, a slot per row. ## On a phone (UiTheme's panels) it sits in the safe area: the rows are a list that scrolls ## (UiTheme.TouchScroll: a finger that drags the list scrolls it and buys nothing, a tap on a row ## buys), each row at least the touch target tall with its text wrapped to the room's width (up ## to PHONE_ROW), and Close stays below the list, always in view. ## A purchase's result text. Nothing shows it on screen since #23 (the rows say what's missing); ## tests listen to it. signal message(text: String, color: Color) signal bought(id: String) const ROW := Vector2(860.0, 46.0) const CLOSE := Vector2(200.0, 48.0) const MARGIN := 8 ## A row's widest on a phone (an iPad's room is wider): about the workbench's longest. const PHONE_ROW := 1150.0 var campaign: CampaignController var structures: StructureSystem var player: PlayerCharacter var is_open := false ## Which shop this instance lists ("workbench" or "shed"); set by setup(). var shop := "workbench" ## Co-op partner: ask the host to buy (upgrade id); the result comes back as a message, and a ## purchase that went through as confirm_bought. var buy_request: Callable ## The avatar slot of this machine's scarecrow (FarmSim.set_local): what's affordable is its own ## pouch plus the shed. var slot: int = 0 var _rows := {} # upgrade id -> Button var _title: Label var _close: Button var _safe: MarginContainer var _panel: PanelContainer var _scroll: UiTheme.TouchScroll var _list: VBoxContainer var _fit_queued := false func setup(p_campaign: CampaignController, p_structures: StructureSystem, p_player: PlayerCharacter, p_shop: String = "workbench") -> void: campaign = p_campaign structures = p_structures player = p_player shop = p_shop var box := _list var track := "" for id in Upgrades.ORDER: var u: Dictionary = campaign.config.upgrades.get(id, {}) if u.is_empty() or Upgrades.shop_of(campaign.config, id) != shop: continue if String(u.get("track", "")) != track: track = String(u.get("track", "")) var head := Label.new() head.text = track head.add_theme_font_size_override("font_size", 24) head.add_theme_color_override("font_color", Sketch.DIM) box.add_child(head) var b := Button.new() b.alignment = HORIZONTAL_ALIGNMENT_LEFT b.custom_minimum_size = ROW b.add_theme_font_size_override("font_size", 25) b.pressed.connect(func() -> void: _buy(id)) box.add_child(b) _rows[id] = b func _ready() -> void: set_anchors_preset(Control.PRESET_FULL_RECT) theme = UiTheme.sketch() _safe = MarginContainer.new() # the safe area's margins on a phone (none on desktop) _safe.name = "Safe" add_child(_safe) _panel = PanelContainer.new() _panel.name = "Panel" _safe.add_child(_panel) var margin := MarginContainer.new() margin.name = "Margin" for side in ["left", "right", "top", "bottom"]: margin.add_theme_constant_override("margin_" + side, MARGIN) _panel.add_child(margin) var box := VBoxContainer.new() box.name = "Box" box.add_theme_constant_override("separation", 6) margin.add_child(box) _title = Label.new() _title.add_theme_font_size_override("font_size", 36) box.add_child(_title) box.add_child(HSeparator.new()) _scroll = UiTheme.TouchScroll.new(Sketch.TEXT, 4.0) # the rows (setup), scrolling on a phone box.add_child(_scroll) _list = VBoxContainer.new() _list.name = "List" _list.size_flags_horizontal = Control.SIZE_EXPAND_FILL _list.add_theme_constant_override("separation", 6) _scroll.add_child(_list) _close = Button.new() _close.text = "Close" _close.custom_minimum_size = CLOSE _close.add_theme_font_size_override("font_size", 28) _close.size_flags_horizontal = Control.SIZE_SHRINK_CENTER _close.pressed.connect(close) box.add_child(_close) visible = false mouse_filter = Control.MOUSE_FILTER_IGNORE _list.minimum_size_changed.connect(_queue_fit) # rows rewritten or reflowed get_viewport().size_changed.connect(_queue_fit) func open() -> void: if is_open: return is_open = true visible = true mouse_filter = Control.MOUSE_FILTER_STOP player.start_channel(&"Idle_A") refresh() _fit() _scroll.scroll_vertical = 0 var first: Button = _close for id in Upgrades.ORDER: if _rows.has(id) and not (_rows[id] as Button).disabled: first = _rows[id] break first.grab_focus.call_deferred() func close() -> void: if not is_open: return is_open = false visible = false mouse_filter = Control.MOUSE_FILTER_IGNORE player.stop_channel() ## Lays the menu out: on desktop as it always was; on a phone (UiTheme's panels) inside the safe ## area, the rows wrapped to the room's width and at least the touch target tall, and the list ## scrolling past what the height leaves for it. func _fit() -> void: var phone := MobilePort.active() _scroll.set_phone(phone) var target := MobilePort.minimum_target(self) if phone else 0.0 _close.custom_minimum_size = Vector2(CLOSE.x, maxf(CLOSE.y, target)) var room := UiTheme.panel_room(self) if phone else Rect2() UiTheme.fit_margins(_safe, room) var width := ROW.x if phone: var frame := _panel.get_theme_stylebox("panel").get_minimum_size().x + 2.0 * MARGIN + _scroll.frame_width() width = floorf(minf(PHONE_ROW, room.size.x - frame)) for id: String in _rows: var b := _rows[id] as Button b.custom_minimum_size = Vector2(width, maxf(ROW.y, target)) b.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART if phone else TextServer.AUTOWRAP_OFF if phone: _scroll.fit(room.size.y - (_panel.get_combined_minimum_size().y - _scroll.get_combined_minimum_size().y)) ## Fits again once this frame's changes are in (rows rewritten, the screen changed size). func _queue_fit() -> void: if _fit_queued or not is_open: return _fit_queued = true _refit.call_deferred() func _refit() -> void: _fit_queued = false if is_open: _fit() ## Rewrites every row for what's owned and affordable now. func refresh() -> void: var cfg := campaign.config var s := campaign.state var done := 0 for id: String in _rows: var u: Dictionary = cfg.upgrades[id] var b := _rows[id] as Button var why := Upgrades.blocker(campaign, id, slot) var lvl := Upgrades.level(s, id) var top := Upgrades.max_level(cfg, id) b.disabled = why != "" if lvl >= top: done += 1 b.text = "%s — %s (%s)" % [Upgrades.name_at(cfg, id, top), Upgrades.text_at(cfg, id, top), "Max" if top > 1 else "Done"] continue var arrival := "" if Upgrades.arrived(s, cfg, id) else "Arrives on day %d" % Upgrades.level_day(cfg, id, 1) if not Upgrades.revealed(s, cfg, id): # Hidden: no name, description, or cost until its reveal condition is met. b.text = "?" if arrival == "" else "? %s" % arrival continue if arrival != "": b.text = "%s %s" % [u["name"], arrival] # (greyed: disabled) continue b.text = "%s — %s (%s)" % [Upgrades.name_at(cfg, id, lvl + 1), Upgrades.text_at(cfg, id, lvl + 1), ResourceStore.describe(Upgrades.cost(cfg, id, lvl + 1))] if why != "": b.text += " %s" % why var place := "Upgrades at the work area" if shop == "workbench" else "Shop at the supply shed" _title.text = "%s (%d of %d done)" % [place, done, _rows.size()] func _buy(id: String) -> void: if buy_request.is_valid(): buy_request.call(id) return var was := Upgrades.level(campaign.state, id) var text := Upgrades.buy(campaign, id, structures, slot) if Upgrades.level(campaign.state, id) > was: # a level went through (every one jingles) bought.emit(id) message.emit(text, Color(0.85, 0.75, 1.0) if text.contains("done:") else Color(1.0, 0.8, 0.5)) _after_buy(id) ## Co-op partner: the host bought id for this machine's scarecrow (buy_request), and this ## machine's farm state has it now. It counts as bought here, as a purchase in solo does (bought: ## a new weapon goes into this scarecrow's hand, the jingle plays) (#22). func confirm_bought(id: String) -> void: bought.emit(id) if is_open and _rows.has(id): _after_buy(id) func _after_buy(id: String) -> void: refresh() if (_rows[id] as Button).has_focus() and (_rows[id] as Button).disabled: _close.grab_focus() func _unhandled_input(event: InputEvent) -> void: if is_open and event.is_action_pressed("ui_cancel"): close() get_viewport().set_input_as_handled()