From c1f30a2905d7a44439c2c0797ddd87b58f79d724 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 25 Dec 2023 13:10:22 +0100 Subject: level select input support --- UI/HUD/CancelButton.gd | 5 +++++ UI/LevelSelect.gd | 35 +++++++++++++++++++++++++++++++++++ UI/LevelSelectCell.gd | 17 +++++++++++++++++ UI/LevelSelectCell.tscn | 3 --- 4 files changed, 57 insertions(+), 3 deletions(-) (limited to 'UI') diff --git a/UI/HUD/CancelButton.gd b/UI/HUD/CancelButton.gd index 51b146b..a40d594 100644 --- a/UI/HUD/CancelButton.gd +++ b/UI/HUD/CancelButton.gd @@ -3,3 +3,8 @@ extends Button func _on_CancelButton_pressed(): Global.cancel_level() + + +func _input(event): + if event.is_action_pressed("BACK"): + Global.cancel_level() diff --git a/UI/LevelSelect.gd b/UI/LevelSelect.gd index 368d338..dae69bb 100644 --- a/UI/LevelSelect.gd +++ b/UI/LevelSelect.gd @@ -1,29 +1,64 @@ extends Control +var selected_idx = 0 + + func _ready(): Global.connect("level_map_updated", Callable(self, "draw")) draw() +func _input(event): + if event.is_action_pressed("DIRECTION_LEFT") and event.get_action_strength("DIRECTION_LEFT") == 1.0: + selected_idx = max(selected_idx - 1, 0) + set_cell_selected() + elif event.is_action_pressed("DIRECTION_RIGHT") and event.get_action_strength("DIRECTION_RIGHT") == 1.0: + selected_idx = min(selected_idx + 1, Global.Level_Map.size() - 1) + set_cell_selected() + elif event.is_action_pressed("UP") and (selected_idx - 3 >= 0) and event.get_action_strength("UP") == 1.0: + selected_idx = max(selected_idx - 3, 0) + set_cell_selected() + elif event.is_action_pressed("DOWN") and (selected_idx + 3 <= Global.Level_Map.size() - 1) and event.get_action_strength("DOWN") == 1.0: + selected_idx = min(selected_idx + 3, Global.Level_Map.size() - 1) + set_cell_selected() + elif event.is_action_pressed("ACCEPT"): + Global.start_level(selected_idx) + + func draw(): var Cell = load("res://UI/LevelSelectCell.tscn") for idx in range(Global.Level_Map.size()): # size = 9 var cell = Cell.instantiate() cell.level_idx = idx + + if idx == selected_idx: + cell.set_selected(true) + cell.set_rect_size( get_viewport_rect().size.x/3, get_viewport_rect().size.y/3 ) + var column = idx % 3 var row = idx / 3 cell.position.x = cell.size.x*column cell.position.y = cell.size.y*row + cell.connect("gui_input", Callable(self, '_button_pressed').bind(idx)) + self.add_child(cell) +func set_cell_selected(): + var cells = get_children() + for cell in cells: + cell.set_selected(false) + + cells[selected_idx].set_selected(true) + + func _button_pressed(event, idx): if event is InputEventMouseButton or event is InputEventKey: if event.pressed: diff --git a/UI/LevelSelectCell.gd b/UI/LevelSelectCell.gd index a756210..cd59593 100644 --- a/UI/LevelSelectCell.gd +++ b/UI/LevelSelectCell.gd @@ -3,10 +3,16 @@ extends Panel var level_idx = 0 +var stylebox = StyleBoxFlat.new() +var background_color = "#225f30" +var background_color_selected = "#3c9f54" + func _ready(): var level = Global.get_level(self.level_idx) + stylebox = self.get("theme_override_styles/panel") + $ClearMark.text = "" if level.cleared_by.idx == Enum.PLAYER.FIRST: $ClearMark.text = "X" @@ -31,3 +37,14 @@ func set_rect_size(x, y): $Time.position = Vector2(3, 3) $Name.position = Vector2(3, y - $Name.size.y) + + +func set_selected(is_selected = true): + if is_selected: + var box = stylebox.duplicate() + box.bg_color = Color.from_string(self.background_color_selected, self.background_color) + self.set("theme_override_styles/panel", box) + else: + var box = stylebox.duplicate() + box.bg_color = Color.from_string(self.background_color, self.background_color) + self.set("theme_override_styles/panel", box) diff --git a/UI/LevelSelectCell.tscn b/UI/LevelSelectCell.tscn index b76111b..2d26c47 100644 --- a/UI/LevelSelectCell.tscn +++ b/UI/LevelSelectCell.tscn @@ -13,9 +13,6 @@ border_width_bottom = 1 [sub_resource type="FontFile" id="3"] fallbacks = Array[Font]([ExtResource("2")]) -face_index = null -embolden = null -transform = null cache/0/16/0/ascent = 0.0 cache/0/16/0/descent = 0.0 cache/0/16/0/underline_position = 0.0 -- cgit v1.2.3