Skip to content

Commit

Permalink
Changes in preparation for global menu support (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke authored Apr 9, 2024
1 parent 006830b commit 0f39192
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 53 deletions.
120 changes: 84 additions & 36 deletions project.godot

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions src/SVG.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@
## The SVG text, and the native [TagSVG] representation.
extends Node


signal parsing_finished(error_id: SVGParser.ParseError)
signal svg_text_changed()

const GoodFileDialogType = preload("res://src/ui_parts/good_file_dialog.gd")

const AlertDialog := preload("res://src/ui_parts/alert_dialog.tscn")
const ImportWarningDialog = preload("res://src/ui_parts/import_warning_dialog.tscn")
const GoodFileDialog = preload("res://src/ui_parts/good_file_dialog.tscn")
const ExportDialog = preload("res://src/ui_parts/export_dialog.tscn")

const default = '<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg"></svg>'
const DEFAULT = '<svg width="16" height="16" xmlns="http://www.w3.org/2000/svg"></svg>'

var text := "":
set(value):
text = value
svg_text_changed.emit()

var text := ""
var root_tag := TagSVG.new()

var UR := UndoRedo.new()

signal parsing_finished(error_id: SVGParser.ParseError)

func _ready() -> void:
UR.version_changed.connect(_on_undo_redo)
root_tag.changed_unknown.connect(update_text.bind(false))
Expand All @@ -38,7 +44,7 @@ func _ready() -> void:
if not GlobalSettings.save_data.svg_text.is_empty():
apply_svg_text(GlobalSettings.save_data.svg_text)
else:
apply_svg_text(default)
apply_svg_text(DEFAULT)

if load_cmdl:
apply_svg_from_path(cmdline_args[0])
Expand Down Expand Up @@ -196,6 +202,10 @@ func finish_export(file_path: String, extension: String, upscale_amount := 1.0)
HandlerGUI.remove_overlay()


func does_svg_data_match_disk_contents() -> bool:
return text == FileAccess.get_file_as_string(GlobalSettings.save_data.current_file_path)


func save_svg_to_file(path: String) -> void:
var FA := FileAccess.open(path, FileAccess.WRITE)
FA.store_string(text)
Expand Down
3 changes: 3 additions & 0 deletions src/ui_elements/context_popup.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
## The standard context menu popup.
extends BetterPopup


@onready var panel: PanelContainer = $PanelContainer
@onready var main_container: VBoxContainer = $PanelContainer/MainContainer


func add_button(btn: Button, align_left: bool) -> void:
if not btn is CheckBox:
btn.theme_type_variation = "ContextButton"
Expand Down Expand Up @@ -67,5 +69,6 @@ min_width := -1, separator_indices: Array[int] = []) -> void:
min_size.x = ceili(min_width)
panel.custom_minimum_size.x = min_width


func get_button_count() -> int:
return main_container.get_child_count()
44 changes: 36 additions & 8 deletions src/ui_parts/code_editor.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
extends VBoxContainer


signal optimize_button_enable_updated(is_optimize_enabled: bool)

const ContextPopup = preload("res://src/ui_elements/context_popup.tscn")

@onready var code_edit: TextEdit = $ScriptEditor/SVGCodeEdit
Expand All @@ -11,6 +14,7 @@ const ContextPopup = preload("res://src/ui_elements/context_popup.tscn")
@onready var options_button: Button = $PanelContainer/CodeButtons/MetaActions/OptionsButton
@onready var optimize_button: Button = $PanelContainer/CodeButtons/OptimizeButton


func _ready() -> void:
SVG.parsing_finished.connect(update_error)
auto_update_text()
Expand All @@ -25,6 +29,29 @@ func _ready() -> void:
SVG.root_tag.changed_unknown.connect(auto_update_text)
GlobalSettings.save_data.current_file_path_changed.connect(update_file_button)


func _notification(what: int) -> void:
if what == Utils.CustomNotification.HIGHLIGHT_COLORS_CHANGED:
setup_highlighter()


func _unhandled_input(input_event: InputEvent) -> void:
if input_event.is_action_pressed(&"import"):
_on_import_button_pressed()
elif input_event.is_action_pressed(&"export"):
_on_export_button_pressed()
elif input_event.is_action_pressed(&"copy_svg_text"):
_on_copy_button_pressed()
elif input_event.is_action_pressed(&"clear_svg"):
clear_svg()
elif input_event.is_action_pressed(&"optimize_svg"):
_on_optimize_button_pressed()
elif input_event.is_action_pressed(&"clear_file_path"):
clear_file_path()
elif input_event.is_action_pressed(&"reset_svg"):
reset_svg()


func auto_update_text() -> void:
if not code_edit.has_focus():
code_edit.text = SVG.text
Expand Down Expand Up @@ -102,10 +129,14 @@ func update_file_button() -> void:
else:
get_window().title = "GodSVG"


func update_optimize_button() -> void:
optimize_button.disabled = not SVG.root_tag.optimize(false)
var enabled: bool = SVG.root_tag.optimize(false)
optimize_button.disabled = not enabled
optimize_button.mouse_default_cursor_shape = Control.CURSOR_ARROW if\
optimize_button.disabled else Control.CURSOR_POINTING_HAND
optimize_button_enable_updated.emit(enabled)


func _on_svg_code_edit_focus_exited() -> void:
code_edit.text = SVG.text
Expand All @@ -116,12 +147,13 @@ func _on_svg_code_edit_focus_exited() -> void:
func _on_optimize_button_pressed() -> void:
SVG.root_tag.optimize()


func _on_file_button_pressed() -> void:
var btn_array: Array[Button] = []
btn_array.append(Utils.create_btn(tr("Clear association"), clear_file_path,
false, load("res://visual/icons/Clear.svg")))
btn_array.append(Utils.create_btn(tr("Reset SVG"), reset_svg,
SVG.text == FileAccess.get_file_as_string(GlobalSettings.save_data.current_file_path),
SVG.does_svg_data_match_disk_contents(),
load("res://visual/icons/Reload.svg")))
var context_popup := ContextPopup.instantiate()
add_child(context_popup)
Expand All @@ -135,7 +167,7 @@ func _on_options_button_pressed() -> void:
btn_array.append(Utils.create_btn(tr("Copy All Text"), _on_copy_button_pressed,
false, load("res://visual/icons/Copy.svg")))
btn_array.append(Utils.create_btn(tr("Clear SVG"), clear_svg,
SVG.text == SVG.default, load("res://visual/icons/Clear.svg")))
SVG.text == SVG.DEFAULT, load("res://visual/icons/Clear.svg")))
var context_popup := ContextPopup.instantiate()
add_child(context_popup)
context_popup.setup(btn_array, true, options_button.size.x)
Expand All @@ -150,11 +182,7 @@ func reset_svg() -> void:
SVG.apply_svg_from_path(GlobalSettings.save_data.current_file_path)

func clear_svg() -> void:
SVG.apply_svg_text(SVG.default)

func _notification(what: int) -> void:
if what == Utils.CustomNotification.HIGHLIGHT_COLORS_CHANGED:
setup_highlighter()
SVG.apply_svg_text(SVG.DEFAULT)

func setup_highlighter() -> void:
if code_edit != null:
Expand Down
49 changes: 47 additions & 2 deletions src/ui_parts/display.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
extends VBoxContainer


signal view_settings_updated(show_grid: bool, show_handles: bool, rasterized_svg: bool)
signal snap_settings_updated(snap_enabled: bool, snap_amount: float)

const settings_menu = preload("settings_menu.tscn")
const about_menu = preload("about_menu.tscn")
const donate_menu = preload("res://src/ui_parts/donate_menu.tscn")
Expand All @@ -15,28 +19,55 @@ const NumberField = preload("res://src/ui_elements/number_field.tscn")
@onready var grid_visuals: Control = %Camera
@onready var visuals_button: Button = %LeftMenu/Visuals
@onready var more_button: Button = %LeftMenu/MoreOptions
@onready var snapper: NumberEditType = %LeftMenu/Snapping/NumberEdit
@onready var snapper: NumberEditType = %LeftMenu/Snapping/SnapNumberEdit
@onready var snap_button: BetterToggleButtonType = %LeftMenu/Snapping/SnapButton


func _ready() -> void:
update_snap_config()
view_settings_updated.emit(grid_visuals.visible, controls.visible, viewport.display_texture.rasterized)


func _unhandled_input(input_event: InputEvent) -> void:
if input_event.is_action_pressed(&"open_settings"):
_on_settings_pressed()
elif input_event.is_action_pressed(&"view_show_grid"):
toggle_grid_visuals()
elif input_event.is_action_pressed(&"view_show_handles"):
toggle_handles_visuals()
elif input_event.is_action_pressed(&"view_rasterized_svg"):
toggle_rasterization()
elif input_event.is_action_pressed(&"snap_toggle"):
toggle_snap()
elif input_event.is_action_pressed(&"about_repo"):
open_godsvg_repo()
elif input_event.is_action_pressed(&"about_info"):
open_about()
elif input_event.is_action_pressed(&"about_donate"):
open_sponsor()


func _notification(what: int) -> void:
if what == Utils.CustomNotification.NUMBER_PRECISION_CHANGED:
update_snap_config()
if what == NOTIFICATION_WM_ABOUT:
open_about.call_deferred()


func update_snap_config() -> void:
var snap_config := GlobalSettings.save_data.snap
var snap_enabled := snap_config > 0.0
snap_button.button_pressed = snap_enabled
snapper.editable = snap_enabled
snapper.set_value(absf(snap_config))
snap_settings_updated.emit(snap_enabled, absf(snap_config))


func _on_settings_pressed() -> void:
var settings_menu_instance := settings_menu.instantiate()
HandlerGUI.add_overlay(settings_menu_instance)


func _on_visuals_button_pressed() -> void:
var btn_arr: Array[Button] = [
Utils.create_checkbox(tr("Show Grid"), toggle_grid_visuals, grid_visuals.visible),
Expand Down Expand Up @@ -80,22 +111,36 @@ func open_sponsor() -> void:
var donate_menu_instance := donate_menu.instantiate()
HandlerGUI.add_overlay(donate_menu_instance)


func toggle_grid_visuals() -> void:
grid_visuals.visible = not grid_visuals.visible
view_settings_updated.emit(grid_visuals.visible, controls.visible, viewport.display_texture.rasterized)


func toggle_handles_visuals() -> void:
controls.visible = not controls.visible
view_settings_updated.emit(grid_visuals.visible, controls.visible, viewport.display_texture.rasterized)


func toggle_rasterization() -> void:
viewport.display_texture.rasterized = not viewport.display_texture.rasterized
view_settings_updated.emit(grid_visuals.visible, controls.visible, viewport.display_texture.rasterized)


func toggle_snap() -> void:
snap_button.button_pressed = not snap_button.button_pressed


func set_snap_amount(snap_value: float) -> void:
snapper.set_value(snap_value)


func _on_snap_button_toggled(toggled_on: bool) -> void:
GlobalSettings.modify_save_data("snap",
absf(GlobalSettings.save_data.snap) * (1 if toggled_on else -1))
update_snap_config()

func _on_number_edit_value_changed(new_value: float) -> void:
func _on_snap_number_edit_value_changed(new_value: float) -> void:
GlobalSettings.modify_save_data("snap",
new_value * signf(GlobalSettings.save_data.snap))
update_snap_config()
4 changes: 2 additions & 2 deletions src/ui_parts/display.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ icon = ExtResource("5_1k2cq")
script = ExtResource("6_3v3ve")
hover_pressed_stylebox = SubResource("StyleBoxFlat_eujxa")

[node name="NumberEdit" parent="PanelContainer/HBoxContainer/LeftMenu/Snapping" instance=ExtResource("7_wrrfr")]
[node name="SnapNumberEdit" parent="PanelContainer/HBoxContainer/LeftMenu/Snapping" instance=ExtResource("7_wrrfr")]
custom_minimum_size = Vector2(46, 22)
layout_mode = 2
tooltip_text = "Snap size"
Expand Down Expand Up @@ -183,7 +183,7 @@ script = ExtResource("15_hevpa")
[connection signal="pressed" from="PanelContainer/HBoxContainer/LeftMenu/Settings" to="." method="_on_settings_pressed"]
[connection signal="pressed" from="PanelContainer/HBoxContainer/LeftMenu/Visuals" to="." method="_on_visuals_button_pressed"]
[connection signal="toggled" from="PanelContainer/HBoxContainer/LeftMenu/Snapping/SnapButton" to="." method="_on_snap_button_toggled"]
[connection signal="value_changed" from="PanelContainer/HBoxContainer/LeftMenu/Snapping/NumberEdit" to="." method="_on_number_edit_value_changed"]
[connection signal="value_changed" from="PanelContainer/HBoxContainer/LeftMenu/Snapping/SnapNumberEdit" to="." method="_on_snap_number_edit_value_changed"]
[connection signal="zoom_changed" from="PanelContainer/HBoxContainer/ZoomMenu" to="ViewportPanel/ViewportContainer/Viewport" method="_on_zoom_changed"]
[connection signal="zoom_reset_pressed" from="PanelContainer/HBoxContainer/ZoomMenu" to="ViewportPanel/ViewportContainer/Viewport" method="center_frame"]
[connection signal="size_changed" from="ViewportPanel/ViewportContainer/Viewport" to="ViewportPanel/ViewportContainer/Viewport" method="_on_size_changed"]
9 changes: 9 additions & 0 deletions src/ui_parts/zoom_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ signal zoom_reset_pressed
var _zoom_level: float


func _unhandled_input(input_event: InputEvent) -> void:
if input_event.is_action_pressed(&"zoom_in"):
zoom_in()
elif input_event.is_action_pressed(&"zoom_out"):
zoom_out()
elif input_event.is_action_pressed(&"zoom_reset"):
zoom_reset()


func set_zoom(new_value: float, offset := Vector2(0.5, 0.5)) -> void:
new_value = clampf(new_value, MIN_ZOOM, MAX_ZOOM)
if _zoom_level != new_value:
Expand Down

0 comments on commit 0f39192

Please sign in to comment.