1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-19 01:29:49 +00:00

Tidy up preferences code

Use a Preference class instead of a nested array. Lines of code reduced from 461 to 330. Also fixed a minor bug in the Web version where, when the user changed language, "Interface" was being selected instead.
This commit is contained in:
Manolis Papadeas 2022-02-18 01:03:08 +02:00
parent d37d3c5d3d
commit bafcb2620a
2 changed files with 122 additions and 254 deletions

View file

@ -1,224 +1,145 @@
extends AcceptDialog extends AcceptDialog
# Preferences table: [Prop name in Global, relative node path, value type, default value] # Array of Preference(s)
var preferences = [ var preferences := [
[ Preference.new("open_last_project", "Startup/StartupContainer/OpenLastProject", "pressed"),
"open_last_project", Preference.new("quit_confirmation", "Startup/StartupContainer/QuitConfirmation", "pressed"),
"Startup/StartupContainer/OpenLastProject", Preference.new("shrink", "Interface/ShrinkContainer/ShrinkHSlider", "value"),
"pressed", Preference.new("dim_on_popup", "Interface/DimPopup/CheckBox", "pressed"),
Global.open_last_project Preference.new("icon_color_from", "Interface/IconColorFrom/IconColorOptionButton", "selected"),
], Preference.new("custom_icon_color", "Interface/IconColorFrom/IconColorButton", "color"),
[ Preference.new(
"quit_confirmation", "tool_button_size", "Interface/ToolButtonSize/ToolButtonSizeOptionButton", "selected"
"Startup/StartupContainer/QuitConfirmation", ),
"pressed", Preference.new(
Global.quit_confirmation
],
["shrink", "Interface/ShrinkContainer/ShrinkHSlider", "value", Global.shrink],
["dim_on_popup", "Interface/DimPopup/CheckBox", "pressed", Global.dim_on_popup],
[
"icon_color_from",
"Interface/IconColorFrom/IconColorOptionButton",
"selected",
Global.icon_color_from
],
[
"custom_icon_color",
"Interface/IconColorFrom/IconColorButton",
"color",
Global.custom_icon_color
],
[
"tool_button_size",
"Interface/ToolButtonSize/ToolButtonSizeOptionButton",
"selected",
Global.tool_button_size
],
[
"pressure_sensitivity_mode", "pressure_sensitivity_mode",
"Startup/PressureSentivity/PressureSensitivityOptionButton", "Startup/PressureSentivity/PressureSensitivityOptionButton",
"selected", "selected"
Global.pressure_sensitivity_mode ),
], Preference.new(
[ "show_left_tool_icon", "Cursors/CursorsContainer/LeftToolIconCheckbox", "pressed"
"show_left_tool_icon", ),
"Cursors/CursorsContainer/LeftToolIconCheckbox", Preference.new(
"pressed", "show_right_tool_icon", "Cursors/CursorsContainer/RightToolIconCheckbox", "pressed"
Global.show_left_tool_icon ),
], Preference.new(
[ "left_square_indicator_visible", "Cursors/CursorsContainer/LeftIndicatorCheckbox", "pressed"
"show_right_tool_icon", ),
"Cursors/CursorsContainer/RightToolIconCheckbox", Preference.new(
"pressed",
Global.show_right_tool_icon
],
[
"left_square_indicator_visible",
"Cursors/CursorsContainer/LeftIndicatorCheckbox",
"pressed",
Global.left_square_indicator_visible
],
[
"right_square_indicator_visible", "right_square_indicator_visible",
"Cursors/CursorsContainer/RightIndicatorCheckbox", "Cursors/CursorsContainer/RightIndicatorCheckbox",
"pressed", "pressed"
Global.right_square_indicator_visible ),
], Preference.new("native_cursors", "Cursors/CursorsContainer/NativeCursorsCheckbox", "pressed"),
[ Preference.new("cross_cursor", "Cursors/CursorsContainer/CrossCursorCheckbox", "pressed"),
"native_cursors", Preference.new("autosave_interval", "Backup/AutosaveContainer/AutosaveInterval", "value"),
"Cursors/CursorsContainer/NativeCursorsCheckbox", Preference.new("enable_autosave", "Backup/AutosaveContainer/EnableAutosave", "pressed"),
"pressed", Preference.new("default_width", "Image/ImageOptions/ImageDefaultWidth", "value"),
Global.native_cursors Preference.new("default_height", "Image/ImageOptions/ImageDefaultHeight", "value"),
], Preference.new("default_fill_color", "Image/ImageOptions/DefaultFillColor", "color"),
[ Preference.new("smooth_zoom", "Canvas/ZoomOptions/SmoothZoom", "pressed"),
"cross_cursor", Preference.new("grid_type", "Canvas/GridOptions/GridType", "selected"),
"Cursors/CursorsContainer/CrossCursorCheckbox", Preference.new("grid_width", "Canvas/GridOptions/GridWidthValue", "value"),
"pressed", Preference.new("grid_height", "Canvas/GridOptions/GridHeightValue", "value"),
Global.cross_cursor Preference.new(
],
[
"autosave_interval",
"Backup/AutosaveContainer/AutosaveInterval",
"value",
Global.autosave_interval
],
[
"enable_autosave",
"Backup/AutosaveContainer/EnableAutosave",
"pressed",
Global.enable_autosave
],
["default_width", "Image/ImageOptions/ImageDefaultWidth", "value", Global.default_width],
["default_height", "Image/ImageOptions/ImageDefaultHeight", "value", Global.default_height],
[
"default_fill_color",
"Image/ImageOptions/DefaultFillColor",
"color",
Global.default_fill_color
],
["smooth_zoom", "Canvas/ZoomOptions/SmoothZoom", "pressed", Global.smooth_zoom],
["grid_type", "Canvas/GridOptions/GridType", "selected", Global.grid_type],
["grid_width", "Canvas/GridOptions/GridWidthValue", "value", Global.grid_width],
["grid_height", "Canvas/GridOptions/GridHeightValue", "value", Global.grid_height],
[
"grid_isometric_cell_bounds_width", "grid_isometric_cell_bounds_width",
"Canvas/GridOptions/IsometricCellBoundsWidthValue", "Canvas/GridOptions/IsometricCellBoundsWidthValue",
"value", "value"
Global.grid_isometric_cell_bounds_width ),
], Preference.new(
[
"grid_isometric_cell_bounds_height", "grid_isometric_cell_bounds_height",
"Canvas/GridOptions/IsometricCellBoundsHeightValue", "Canvas/GridOptions/IsometricCellBoundsHeightValue",
"value", "value"
Global.grid_isometric_cell_bounds_height ),
], Preference.new("grid_offset_x", "Canvas/GridOptions/GridOffsetXValue", "value"),
["grid_offset_x", "Canvas/GridOptions/GridOffsetXValue", "value", Global.grid_offset_x], Preference.new("grid_offset_y", "Canvas/GridOptions/GridOffsetYValue", "value"),
["grid_offset_y", "Canvas/GridOptions/GridOffsetYValue", "value", Global.grid_offset_y], Preference.new(
[ "grid_draw_over_tile_mode", "Canvas/GridOptions/GridDrawOverTileMode", "pressed"
"grid_draw_over_tile_mode", ),
"Canvas/GridOptions/GridDrawOverTileMode", Preference.new("grid_color", "Canvas/GridOptions/GridColor", "color"),
"pressed", Preference.new("pixel_grid_show_at_zoom", "Canvas/PixelGridOptions/ShowAtZoom", "value"),
Global.grid_draw_over_tile_mode Preference.new("pixel_grid_color", "Canvas/PixelGridOptions/GridColor", "color"),
], Preference.new("guide_color", "Canvas/GuideOptions/GuideColor", "color"),
["grid_color", "Canvas/GridOptions/GridColor", "color", Global.grid_color], Preference.new("checker_size", "Canvas/CheckerOptions/CheckerSizeValue", "value"),
[ Preference.new("checker_color_1", "Canvas/CheckerOptions/CheckerColor1", "color"),
"pixel_grid_show_at_zoom", Preference.new("checker_color_2", "Canvas/CheckerOptions/CheckerColor2", "color"),
"Canvas/PixelGridOptions/ShowAtZoom", Preference.new(
"value", "checker_follow_movement", "Canvas/CheckerOptions/CheckerFollowMovement", "pressed"
Global.pixel_grid_show_at_zoom ),
], Preference.new("checker_follow_scale", "Canvas/CheckerOptions/CheckerFollowScale", "pressed"),
["pixel_grid_color", "Canvas/PixelGridOptions/GridColor", "color", Global.pixel_grid_color], Preference.new("tilemode_opacity", "Canvas/CheckerOptions/TileModeOpacity", "value"),
["guide_color", "Canvas/GuideOptions/GuideColor", "color", Global.guide_color], Preference.new("selection_animated_borders", "Selection/SelectionOptions/Animate", "pressed"),
["checker_size", "Canvas/CheckerOptions/CheckerSizeValue", "value", Global.checker_size], Preference.new("selection_border_color_1", "Selection/SelectionOptions/BorderColor1", "color"),
["checker_color_1", "Canvas/CheckerOptions/CheckerColor1", "color", Global.checker_color_1], Preference.new("selection_border_color_2", "Selection/SelectionOptions/BorderColor2", "color"),
["checker_color_2", "Canvas/CheckerOptions/CheckerColor2", "color", Global.checker_color_2], Preference.new("fps_limit", "Performance/PerformanceContainer/SetFPSLimit", "value"),
[ Preference.new(
"checker_follow_movement", "pause_when_unfocused", "Performance/PerformanceContainer/PauseAppFocus", "pressed"
"Canvas/CheckerOptions/CheckerFollowMovement", ),
"pressed",
Global.checker_follow_movement
],
[
"checker_follow_scale",
"Canvas/CheckerOptions/CheckerFollowScale",
"pressed",
Global.checker_follow_scale
],
["tilemode_opacity", "Canvas/CheckerOptions/TileModeOpacity", "value", Global.tilemode_opacity],
[
"selection_animated_borders",
"Selection/SelectionOptions/Animate",
"pressed",
Global.selection_animated_borders
],
[
"selection_border_color_1",
"Selection/SelectionOptions/BorderColor1",
"color",
Global.selection_border_color_1
],
[
"selection_border_color_2",
"Selection/SelectionOptions/BorderColor2",
"color",
Global.selection_border_color_2
],
["fps_limit", "Performance/PerformanceContainer/SetFPSLimit", "value", Global.fps_limit],
[
"pause_when_unfocused",
"Performance/PerformanceContainer/PauseAppFocus",
"pressed",
Global.pause_when_unfocused
],
] ]
var content_list := []
var selected_item := 0 var selected_item := 0
var restore_default_button_tcsn = preload("res://src/Preferences/RestoreDefaultButton.tscn") var restore_default_button_tcsn = preload("res://src/Preferences/RestoreDefaultButton.tscn")
onready var list: ItemList = $HSplitContainer/List onready var list: ItemList = $HSplitContainer/List
onready var right_side: VBoxContainer = $HSplitContainer/ScrollContainer/VBoxContainer onready var right_side: VBoxContainer = $HSplitContainer/ScrollContainer/VBoxContainer
onready var autosave_interval: SpinBox = right_side.get_node( onready var autosave_container: Container = right_side.get_node("Backup/AutosaveContainer")
"Backup/AutosaveContainer/AutosaveInterval" onready var autosave_interval: SpinBox = autosave_container.get_node("AutosaveInterval")
)
onready var shrink_label: Label = right_side.get_node("Interface/ShrinkContainer/ShrinkLabel") onready var shrink_label: Label = right_side.get_node("Interface/ShrinkContainer/ShrinkLabel")
onready var themes: BoxContainer = right_side.get_node("Interface/Themes") onready var themes: BoxContainer = right_side.get_node("Interface/Themes")
class Preference:
var prop_name: String
var node_path: String
var value_type: String
var default_value
func _init(_prop_name: String, _node_path: String, _value_type: String) -> void:
prop_name = _prop_name
node_path = _node_path
value_type = _value_type
default_value = Global.get(prop_name)
func _ready() -> void: func _ready() -> void:
# Replace OK since preference changes are being applied immediately, not after OK confirmation # Replace OK since preference changes are being applied immediately, not after OK confirmation
get_ok().text = tr("Close") get_ok().text = tr("Close")
for child in right_side.get_children():
content_list.append(child.name)
if OS.get_name() == "HTML5": if OS.get_name() == "HTML5":
content_list.erase("Startup")
right_side.get_node("Startup").queue_free() right_side.get_node("Startup").queue_free()
right_side.get_node("Languages").visible = true right_side.get_node("Language").visible = true
Global.open_last_project = false Global.open_last_project = false
for pref in preferences: for pref in preferences:
var node = right_side.get_node(pref[1]) var node: Node = right_side.get_node(pref.node_path)
var node_position = node.get_index() var node_position := node.get_index()
var restore_default_button: BaseButton = restore_default_button_tcsn.instance() var restore_default_button: BaseButton = restore_default_button_tcsn.instance()
restore_default_button.setting_name = pref[0] restore_default_button.setting_name = pref.prop_name
restore_default_button.value_type = pref[2] restore_default_button.value_type = pref.value_type
restore_default_button.default_value = pref[3] restore_default_button.default_value = pref.default_value
restore_default_button.node = node restore_default_button.node = node
node.get_parent().add_child(restore_default_button) node.get_parent().add_child(restore_default_button)
node.get_parent().move_child(restore_default_button, node_position) node.get_parent().move_child(restore_default_button, node_position)
match pref[2]: match pref.value_type:
"pressed": "pressed":
node.connect( node.connect(
"toggled", "toggled",
self, self,
"_on_Preference_toggled", "_on_Preference_toggled",
[pref[0], pref[3], restore_default_button] [pref.prop_name, pref.default_value, restore_default_button]
) )
"value": "value":
node.connect( node.connect(
"value_changed", "value_changed",
self, self,
"_on_Preference_value_changed", "_on_Preference_value_changed",
[pref[0], pref[3], restore_default_button] [pref.prop_name, pref.default_value, restore_default_button]
) )
"color": "color":
node.get_picker().presets_visible = false node.get_picker().presets_visible = false
@ -226,31 +147,32 @@ func _ready() -> void:
"color_changed", "color_changed",
self, self,
"_on_Preference_color_changed", "_on_Preference_color_changed",
[pref[0], pref[3], restore_default_button] [pref.prop_name, pref.default_value, restore_default_button]
) )
"selected": "selected":
node.connect( node.connect(
"item_selected", "item_selected",
self, self,
"_on_Preference_item_selected", "_on_Preference_item_selected",
[pref[0], pref[3], restore_default_button] [pref.prop_name, pref.default_value, restore_default_button]
) )
if Global.config_cache.has_section_key("preferences", pref[0]): if Global.config_cache.has_section_key("preferences", pref.prop_name):
var value = Global.config_cache.get_value("preferences", pref[0]) var value = Global.config_cache.get_value("preferences", pref.prop_name)
Global.set(pref[0], value) Global.set(pref.prop_name, value)
node.set(pref[2], value) node.set(pref.value_type, value)
var global_value = Global.get(pref.prop_name)
# This is needed because color_changed doesn't fire if the color changes in code # This is needed because color_changed doesn't fire if the color changes in code
if pref[2] == "color": if pref.value_type == "color":
preference_update(pref[0]) preference_update(pref.prop_name)
disable_restore_default_button( disable_restore_default_button(
restore_default_button, Global.get(pref[0]).is_equal_approx(pref[3]) restore_default_button, global_value.is_equal_approx(pref.default_value)
) )
elif pref[2] == "selected": elif pref.value_type == "selected":
preference_update(pref[0]) preference_update(pref.prop_name)
disable_restore_default_button( disable_restore_default_button(
restore_default_button, Global.get(pref[0]) == pref[3] restore_default_button, global_value == pref.default_value
) )
@ -301,63 +223,31 @@ func preference_update(prop: String) -> void:
else: else:
autosave_interval.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN autosave_interval.mouse_default_cursor_shape = Control.CURSOR_FORBIDDEN
if ( if "grid" in prop:
prop
in [
"grid_type",
"grid_width",
"grid_height",
"grid_isometric_cell_bounds_width",
"grid_isometric_cell_bounds_height",
"grid_offset_x",
"grid_offset_y",
"grid_draw_over_tile_mode",
"grid_color"
]
):
Global.canvas.grid.update() Global.canvas.grid.update()
if prop in ["pixel_grid_show_at_zoom", "pixel_grid_color"]: if prop in ["pixel_grid_show_at_zoom", "pixel_grid_color"]:
Global.canvas.pixel_grid.update() Global.canvas.pixel_grid.update()
if ( if "checker" in prop:
prop
in [
"checker_size",
"checker_color_1",
"checker_color_2",
"checker_follow_movement",
"checker_follow_scale"
]
):
Global.transparent_checker.update_rect() Global.transparent_checker.update_rect()
if prop in ["guide_color"]: if prop in ["guide_color"]:
for guide in Global.canvas.get_children(): for guide in Global.canvas.get_children():
if guide is SymmetryGuide: if guide is SymmetryGuide:
# Add a subtle difference to the normal guide color by mixing in some blue # Add a subtle difference to the normal guide color by mixing in some blue
guide.default_color = Global.guide_color.linear_interpolate( guide.default_color = Global.guide_color.linear_interpolate(Color(.2, .2, .65), .6)
Color(0.2, 0.2, .65), .6
)
elif guide is Guide: elif guide is Guide:
guide.default_color = Global.guide_color guide.default_color = Global.guide_color
if prop in ["fps_limit"]: if prop in ["fps_limit"]:
Engine.set_target_fps(Global.fps_limit) Engine.set_target_fps(Global.fps_limit)
if ( if "selection" in prop:
prop var marching_ants: Sprite = Global.canvas.selection.marching_ants_outline
in ["selection_animated_borders", "selection_border_color_1", "selection_border_color_2"] marching_ants.material.set_shader_param("animated", Global.selection_animated_borders)
): marching_ants.material.set_shader_param("first_color", Global.selection_border_color_1)
Global.canvas.selection.marching_ants_outline.material.set_shader_param( marching_ants.material.set_shader_param("second_color", Global.selection_border_color_2)
"animated", Global.selection_animated_borders
)
Global.canvas.selection.marching_ants_outline.material.set_shader_param(
"first_color", Global.selection_border_color_1
)
Global.canvas.selection.marching_ants_outline.material.set_shader_param(
"second_color", Global.selection_border_color_2
)
Global.canvas.selection.update() Global.canvas.selection.update()
if prop in ["icon_color_from", "custom_icon_color"]: if prop in ["icon_color_from", "custom_icon_color"]:
@ -403,19 +293,11 @@ func _on_PreferencesDialog_about_to_show() -> void:
func add_tabs(changed_language := false) -> void: func add_tabs(changed_language := false) -> void:
if OS.get_name() != "HTML5": for item in content_list:
list.add_item(" " + tr("Startup")) list.add_item(" " + tr(item))
list.add_item(" " + tr("Language"))
list.add_item(" " + tr("Interface"))
list.add_item(" " + tr("Canvas"))
list.add_item(" " + tr("Selection"))
list.add_item(" " + tr("Image"))
list.add_item(" " + tr("Shortcuts"))
list.add_item(" " + tr("Backup"))
list.add_item(" " + tr("Performance"))
list.add_item(" " + tr("Cursors"))
list.select(1 if changed_language else selected_item) var language_index := content_list.find("Language")
list.select(language_index if changed_language else selected_item)
autosave_interval.suffix = tr("minute(s)") autosave_interval.suffix = tr("minute(s)")
@ -426,20 +308,6 @@ func _on_PreferencesDialog_popup_hide() -> void:
func _on_List_item_selected(index: int) -> void: func _on_List_item_selected(index: int) -> void:
selected_item = index selected_item = index
for child in right_side.get_children(): for child in right_side.get_children():
var content_list = [
"Startup",
"Languages",
"Interface",
"Canvas",
"Selection",
"Image",
"Shortcuts",
"Backup",
"Performance",
"Cursors"
]
if OS.get_name() == "HTML5":
content_list.erase("Startup")
child.visible = child.name == content_list[index] child.visible = child.name == content_list[index]

View file

@ -111,14 +111,14 @@ text = "Affect Brush's Alpha"
items = [ "None", null, false, 0, null, "Affect Brush's Alpha", null, false, 1, null ] items = [ "None", null, false, 0, null, "Affect Brush's Alpha", null, false, 1, null ]
selected = 1 selected = 1
[node name="Languages" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"] [node name="Language" type="VBoxContainer" parent="HSplitContainer/ScrollContainer/VBoxContainer"]
visible = false visible = false
margin_top = 184.0 margin_top = 184.0
margin_right = 506.0 margin_right = 506.0
margin_bottom = 632.0 margin_bottom = 632.0
script = ExtResource( 4 ) script = ExtResource( 4 )
[node name="System Language" type="CheckBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Languages"] [node name="System Language" type="CheckBox" parent="HSplitContainer/ScrollContainer/VBoxContainer/Language"]
margin_right = 506.0 margin_right = 506.0
margin_bottom = 24.0 margin_bottom = 24.0
mouse_default_cursor_shape = 2 mouse_default_cursor_shape = 2