|
| 1 | +# Copyright (c) 2024 Liam Sherwin, All rights reserved. |
| 2 | +# This file is part of the Spectrum Lighting Engine, licensed under the GPL v3. |
| 3 | + |
| 4 | +class_name UICorePrimarySideBar extends Control |
| 5 | +## The primary side bar for UICore |
| 6 | + |
| 7 | + |
| 8 | +## Maximun number of visable tabs |
| 9 | +@export var max_visable_tabs: int = 20 |
| 10 | + |
| 11 | +## Minuimun Height in px for tab buttons |
| 12 | +@export var button_min_height: int = 40 |
| 13 | + |
| 14 | +## Font size for tab buttons |
| 15 | +@export var button_font_size: int = 11 |
| 16 | + |
| 17 | +## Separation in px for tab buttons |
| 18 | +@export var button_separation: int = 4 |
| 19 | + |
| 20 | +## The modulate color for tab buttons |
| 21 | +@export var tab_button_modulate: Color = Color.WHITE |
| 22 | + |
| 23 | +## The modulate color for disabled tab buttons |
| 24 | +@export var disabled_tab_button_modulate: Color = Color.WHITE |
| 25 | + |
| 26 | +## The name for the default tab |
| 27 | +@export var default_tab_name: String = "TAB#" |
| 28 | + |
| 29 | + |
| 30 | +@export_group("Nodes") |
| 31 | + |
| 32 | +## The BoxContainer for tab Buttons |
| 33 | +@export var _tab_button_container: BoxContainer |
| 34 | + |
| 35 | +## The ScrollContainer that contains the _tab_button_container |
| 36 | +@export var _tab_button_scroll: ScrollContainer |
| 37 | + |
| 38 | +## The container for tab Control nodes |
| 39 | +@export var _tab_control_container: Control |
| 40 | + |
| 41 | +## The Menu Button |
| 42 | +@export var _menu_button: Button |
| 43 | + |
| 44 | +## The Edit Button |
| 45 | +@export var _edit_button: Button |
| 46 | + |
| 47 | +## The PanelTypeOption menu |
| 48 | +@export var _panel_type_option: PanelContainer |
| 49 | + |
| 50 | + |
| 51 | +## All tab Buttons stored by tab number |
| 52 | +var _tab_buttons: Array[Button] |
| 53 | + |
| 54 | +## RefMap for TabID: UIPanel |
| 55 | +var _tab_controls: RefMap = RefMap.new() |
| 56 | + |
| 57 | +## The current tab number |
| 58 | +var _current_tab: int = 0 |
| 59 | + |
| 60 | +## The current visable tabs control node |
| 61 | +var _current_visable_panel: UIPanel |
| 62 | + |
| 63 | +## The Button Group to asign all tab buttons to |
| 64 | +var _button_group: ButtonGroup = ButtonGroup.new() |
| 65 | + |
| 66 | + |
| 67 | +func _ready() -> void: |
| 68 | + for i in range(max_visable_tabs + 1): |
| 69 | + var new_button: Button = _create_tab_button("", i) |
| 70 | + |
| 71 | + _tab_buttons.append(new_button) |
| 72 | + _tab_button_container.add_child(new_button) |
| 73 | + |
| 74 | + var new_desk: UIDesk = Interface.panels.Desk.instantiate() |
| 75 | + |
| 76 | + new_desk.set_menu_bar_visable(false) |
| 77 | + _tab_controls.map(_current_tab, new_desk) |
| 78 | + _current_visable_panel = new_desk |
| 79 | + |
| 80 | + _tab_control_container.add_child(new_desk) |
| 81 | + _tab_buttons[_current_tab].self_modulate = tab_button_modulate |
| 82 | + _tab_buttons[_current_tab].set_pressed_no_signal(true) |
| 83 | + _tab_buttons[_current_tab].get_child(0).set_text(default_tab_name.replace("#", "0")) |
| 84 | + |
| 85 | + _tab_button_container.add_theme_constant_override("separation", button_separation) |
| 86 | + |
| 87 | + |
| 88 | +## Changes to a tab |
| 89 | +func change_to_tab(p_tab_id: int) -> bool: |
| 90 | + if p_tab_id == _current_tab or p_tab_id > len(_tab_buttons): |
| 91 | + return false |
| 92 | + |
| 93 | + if _tab_controls.has_left(p_tab_id): |
| 94 | + if _current_visable_panel: |
| 95 | + _current_visable_panel.hide() |
| 96 | + |
| 97 | + _panel_type_option.hide() |
| 98 | + |
| 99 | + _current_visable_panel = _tab_controls.left(p_tab_id) |
| 100 | + _current_visable_panel.show() |
| 101 | + |
| 102 | + _edit_button.disabled = false |
| 103 | + _edit_button.set_pressed_no_signal(_current_visable_panel.get_edit_mode()) |
| 104 | + |
| 105 | + _tab_buttons[p_tab_id].set_pressed_no_signal(true) |
| 106 | + |
| 107 | + else: |
| 108 | + if _current_visable_panel: |
| 109 | + _current_visable_panel.hide() |
| 110 | + |
| 111 | + _panel_type_option.show() |
| 112 | + |
| 113 | + _current_visable_panel = null |
| 114 | + _edit_button.disabled = true |
| 115 | + |
| 116 | + _tab_buttons[p_tab_id].set_pressed_no_signal(true) |
| 117 | + |
| 118 | + _current_tab = p_tab_id |
| 119 | + return true |
| 120 | + |
| 121 | + |
| 122 | +## Updates the visable buttons to match the current size |
| 123 | +func match_visable_to_size() -> void: |
| 124 | + var container_height: int = _tab_button_scroll.size.y |
| 125 | + |
| 126 | + for i: int in range(len(_tab_buttons)): |
| 127 | + var button: Button = _tab_buttons[i] |
| 128 | + |
| 129 | + button.visible = (i + 1) * (button_min_height + button_separation) <= container_height |
| 130 | + |
| 131 | + |
| 132 | +## Adds a desk on the current tab |
| 133 | +func create_desk() -> bool: |
| 134 | + if _tab_controls.has_left(_current_tab): |
| 135 | + return false |
| 136 | + |
| 137 | + var new_desk: UIDesk = Interface.panels.Desk.instantiate() |
| 138 | + new_desk.set_menu_bar_visable(false) |
| 139 | + |
| 140 | + _tab_controls.map(_current_tab, new_desk) |
| 141 | + _tab_control_container.add_child(new_desk) |
| 142 | + _current_visable_panel = new_desk |
| 143 | + |
| 144 | + _tab_buttons[_current_tab].get_child(0).set_text(default_tab_name.replace("#", str(_current_tab))) |
| 145 | + _tab_buttons[_current_tab].self_modulate = tab_button_modulate |
| 146 | + _panel_type_option.hide() |
| 147 | + |
| 148 | + return true |
| 149 | + |
| 150 | + |
| 151 | +## Sets the edit mode state on the current selected tab |
| 152 | +func set_tab_edit_mode(p_edit_mode: bool) -> bool: |
| 153 | + if not _current_visable_panel: |
| 154 | + return false |
| 155 | + |
| 156 | + _current_visable_panel.set_edit_mode(p_edit_mode) |
| 157 | + return true |
| 158 | + |
| 159 | + |
| 160 | +## Creates a tab button |
| 161 | +func _create_tab_button(p_text: String, p_index: int) -> Button: |
| 162 | + var button: Button = Button.new() |
| 163 | + var label: Label = Label.new() |
| 164 | + |
| 165 | + label.text = p_text |
| 166 | + label.add_theme_font_size_override("font_size", button_font_size) |
| 167 | + label.set_anchors_preset(Control.PRESET_FULL_RECT) |
| 168 | + label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER |
| 169 | + label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER |
| 170 | + |
| 171 | + button.add_child(label) |
| 172 | + button.custom_minimum_size = Vector2(0, button_min_height) |
| 173 | + button.self_modulate = disabled_tab_button_modulate |
| 174 | + button.toggle_mode = true |
| 175 | + button.size_flags_vertical = Control.SIZE_EXPAND_FILL |
| 176 | + button.button_group = _button_group |
| 177 | + button.action_mode = BaseButton.ACTION_MODE_BUTTON_PRESS |
| 178 | + |
| 179 | + button.pressed.connect(change_to_tab.bind(p_index)) |
| 180 | + |
| 181 | + return button |
0 commit comments