cirnogodot/addons/weapon_creator/BaseViewer.gd

175 lines
5.4 KiB
GDScript

@tool
class_name BaseViewer
extends PanelContainer
# Base class for all viewer panels (WeaponViewer, BulletViewer, ItemViewer, EnemyViewer)
# Provides common functionality for resource loading, filtering, and UI building
var _editor_interface: EditorInterface
var _grid_container: HFlowContainer
var _dock: PanelContainer
var _show_2d_checkbox: CheckBox
var _show_3d_checkbox: CheckBox
func setup(editor_interface: EditorInterface, dock: PanelContainer = null) -> void:
_editor_interface = editor_interface
_dock = dock
_load_filter_settings()
call_deferred("refresh")
func _ready() -> void:
_build_ui()
call_deferred("refresh")
func _build_ui() -> void:
var margin = MarginContainer.new()
margin.add_theme_constant_override("margin_left", 8)
margin.add_theme_constant_override("margin_top", 8)
margin.add_theme_constant_override("margin_right", 8)
margin.add_theme_constant_override("margin_bottom", 8)
add_child(margin)
var vbox = VBoxContainer.new()
vbox.add_theme_constant_override("separation", 8)
margin.add_child(vbox)
# Header with create buttons and filters
var header_hbox = HBoxContainer.new()
vbox.add_child(header_hbox)
_build_header_buttons(header_hbox)
var spacer = Control.new()
spacer.size_flags_horizontal = Control.SIZE_EXPAND_FILL
header_hbox.add_child(spacer)
if _supports_2d_3d_filter():
_show_2d_checkbox = CheckBox.new()
_show_2d_checkbox.text = "2D"
_show_2d_checkbox.button_pressed = true
_show_2d_checkbox.toggled.connect(_on_filter_changed)
header_hbox.add_child(_show_2d_checkbox)
_show_3d_checkbox = CheckBox.new()
_show_3d_checkbox.text = "3D"
_show_3d_checkbox.button_pressed = true
_show_3d_checkbox.toggled.connect(_on_filter_changed)
header_hbox.add_child(_show_3d_checkbox)
var refresh_button = Button.new()
refresh_button.text = "Refresh"
refresh_button.pressed.connect(refresh)
header_hbox.add_child(refresh_button)
vbox.add_child(HSeparator.new())
# Scroll container for grid
var scroll = ScrollContainer.new()
scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
vbox.add_child(scroll)
_grid_container = HFlowContainer.new()
_grid_container.size_flags_horizontal = Control.SIZE_EXPAND_FILL
_grid_container.add_theme_constant_override("h_separation", 8)
_grid_container.add_theme_constant_override("v_separation", 8)
scroll.add_child(_grid_container)
# Override in derived classes to add create buttons
func _build_header_buttons(header_hbox: HBoxContainer) -> void:
pass
# Override in derived classes if they support 2D/3D filtering
func _supports_2d_3d_filter() -> bool:
return false
# Override in derived classes to implement refresh logic
func refresh() -> void:
if not _grid_container:
return
_clear_grid()
func _clear_grid() -> void:
if not _grid_container:
return
for child in _grid_container.get_children():
child.queue_free()
func _on_filter_changed(_toggled: bool) -> void:
_save_filter_settings()
refresh()
# Override these in derived classes for specific filter settings
func _get_filter_setting_2d_key() -> String:
return ""
func _get_filter_setting_3d_key() -> String:
return ""
func _load_filter_settings() -> void:
if not _supports_2d_3d_filter():
return
if _show_2d_checkbox:
_show_2d_checkbox.button_pressed = _load_filter_setting(_get_filter_setting_2d_key(), true)
if _show_3d_checkbox:
_show_3d_checkbox.button_pressed = _load_filter_setting(_get_filter_setting_3d_key(), true)
func _save_filter_settings() -> void:
if not _supports_2d_3d_filter():
return
if not _editor_interface:
return
var editor_settings = _editor_interface.get_editor_settings()
if editor_settings:
if _show_2d_checkbox:
editor_settings.set_setting(_get_filter_setting_2d_key(), _show_2d_checkbox.button_pressed)
if _show_3d_checkbox:
editor_settings.set_setting(_get_filter_setting_3d_key(), _show_3d_checkbox.button_pressed)
func _load_filter_setting(key: String, default_value: bool) -> bool:
if not _editor_interface or key.is_empty():
return default_value
var editor_settings = _editor_interface.get_editor_settings()
if editor_settings and editor_settings.has_setting(key):
return editor_settings.get_setting(key)
return default_value
# Utility functions for adding labels to the grid
func _add_error_label(text: String) -> void:
var label = Label.new()
label.text = text
label.add_theme_color_override("font_color", Color.RED)
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
if _grid_container:
_grid_container.add_child(label)
func _add_info_label(text: String) -> void:
var label = Label.new()
label.text = text
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
if _grid_container:
_grid_container.add_child(label)
func _add_warning_label(text: String) -> void:
var label = Label.new()
label.text = text
label.add_theme_color_override("font_color", Color.ORANGE)
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
if _grid_container:
_grid_container.add_child(label)
# Check if 2D should be shown based on filter
func _should_show_2d() -> bool:
return _show_2d_checkbox == null or _show_2d_checkbox.button_pressed
# Check if 3D should be shown based on filter
func _should_show_3d() -> bool:
return _show_3d_checkbox == null or _show_3d_checkbox.button_pressed
# Utility to log to dock
func _log_to_dock(message: String, color: Color = Color.WHITE) -> void:
if _dock and _dock.has_method("add_log"):
_dock.call("add_log", message, color)