Skip to content

Commit

Permalink
Keybindings in tooltips & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrlabs committed Aug 28, 2024
1 parent 87024b2 commit ab6579b
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 118 deletions.
1 change: 1 addition & 0 deletions lorien/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ edit_alpha = false
presets_visible = false

[node name="MainMenu" parent="." instance=ExtResource("5")]
visible = false
submenu_popup_delay = 0.1
file_dialog_path = NodePath("../FileDialog")

Expand Down
1 change: 1 addition & 0 deletions lorien/Misc/GlobalSignals.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ extends Node

# -------------------------------------------------------------------------------------------------
signal language_changed
signal keybinding_changed(action: KeybindingsManager.Action)
49 changes: 36 additions & 13 deletions lorien/Misc/KeybindingsManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ class Action:
var name: String
var display_name: String
var event: InputEventKey

func event_label() -> String:
return event.as_text_keycode()

# -------------------------------------------------------------------------------------------------
var _actions: Array[Action]

# -------------------------------------------------------------------------------------------------
func _ready() -> void:
Expand All @@ -14,19 +20,36 @@ func _ready() -> void:

# -------------------------------------------------------------------------------------------------
func get_actions() -> Array[Action]:
var actions: Array[Action]
for action_name: String in InputMap.get_actions():
if !action_name.begins_with("ui_") && !action_name.begins_with("player_"):
var events := InputMap.action_get_events(action_name)
if events.size() > 0:
var event := events[0]
event = Settings.get_keybind_value(action_name, event)
var action := Action.new()
action.event = event
action.name = action_name
action.display_name = tr("ACTION_" + action_name)
actions.append(action)
return actions
if _actions.is_empty():
for action_name: String in InputMap.get_actions():
if !action_name.begins_with("ui_") && !action_name.begins_with("player_"):
var events := InputMap.action_get_events(action_name)
if events.size() > 0:
var event := events[0]
event = Settings.get_keybind_value(action_name, event)

var action := Action.new()
action.event = event
action.name = action_name
action.display_name = tr("ACTION_" + action_name)
_actions.append(action)

return _actions

# -------------------------------------------------------------------------------------------------
func get_action(name: String) -> Action:
for action: Action in _actions:
if action.name == name:
return action
return null

# -------------------------------------------------------------------------------------------------
func get_action_map() -> Dictionary:
var dict: Dictionary
for action: Action in _actions:
dict[action.name] = action

return dict

# -------------------------------------------------------------------------------------------------
func rebind_action(action: Action, event: InputEventKey) -> void:
Expand Down
2 changes: 1 addition & 1 deletion lorien/UI/Dialogs/SettingsDialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,4 @@ func _on_constant_pressure_toggled(button_pressed: bool):
func _on_action_keybinding_changed(action: KeybindingsManager.Action, event: InputEventKey) -> void:
KeybindingsManager.rebind_action(action, event)
Settings.set_keybind_value(action.name, action.event)
print("Rebind done")
GlobalSignals.keybinding_changed.emit(action)
58 changes: 24 additions & 34 deletions lorien/UI/MainMenu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ extends PopupMenu
# -------------------------------------------------------------------------------------------------
signal open_about_dialog
signal open_settings_dialog
signal open_url(url)
signal open_project(filepath)
signal open_url(url: String)
signal open_project(filepath: String)
signal save_project
signal save_project_as
signal export_svg
Expand All @@ -20,51 +20,46 @@ const ITEM_MANUAL := 5
const ITEM_BUG_TRACKER := 6
const ITEM_ABOUT := 7

const ITEM_VIEW_1 := 100
const ITEM_VIEW_2 := 101
const ITEM_VIEW_3 := 102

# -------------------------------------------------------------------------------------------------
@export var file_dialog_path: NodePath
@onready var _submenu_views: PopupMenu = $ViewsMenu

# -------------------------------------------------------------------------------------------------
func _ready() -> void:
id_pressed.connect(_on_MainMenu_id_pressed)
_set_items()

# Views submenu
_submenu_views.name = "Views"
_submenu_views.add_item("View 1", ITEM_VIEW_1)
_submenu_views.add_item("View 2", ITEM_VIEW_2)

# main menu
_apply_language()
GlobalSignals.language_changed.connect(_apply_language)
id_pressed.connect(_on_item_pressed)
GlobalSignals.language_changed.connect(_set_items)
GlobalSignals.keybinding_changed.connect(func(action): _set_items())

# -------------------------------------------------------------------------------------------------
func _apply_language() -> void:
func _set_items() -> void:
clear()
add_item(tr("MENU_OPEN"), ITEM_OPEN)
add_item(tr("MENU_SAVE"), ITEM_SAVE)

var open_action := KeybindingsManager.get_action("shortcut_open_project")
var save_action := KeybindingsManager.get_action("shortcut_save_project")
var export_action := KeybindingsManager.get_action("shortcut_export_project")

add_item(tr("MENU_OPEN"), ITEM_OPEN, open_action.event.get_keycode_with_modifiers())
add_item(tr("MENU_SAVE"), ITEM_SAVE, save_action.event.get_keycode_with_modifiers())
add_item(tr("MENU_SAVE_AS"), ITEM_SAVE_AS)
add_item(tr("MENU_EXPORT"), ITEM_EXPORT)
add_item(tr("MENU_EXPORT"), ITEM_EXPORT, export_action.event.get_keycode_with_modifiers())
add_item(tr("MENU_SETTINGS"), ITEM_SETTINGS)
add_separator()
add_item(tr("MENU_MANUAL"), ITEM_MANUAL)
add_item(tr("MENU_BUG_TRACKER"), ITEM_BUG_TRACKER)
add_item(tr("MENU_ABOUT"), ITEM_ABOUT)

# -------------------------------------------------------------------------------------------------
func _on_MainMenu_id_pressed(id: int):
func _on_item_pressed(id: int):
match id:
ITEM_OPEN: _on_open_project()
ITEM_SAVE: emit_signal("save_project")
ITEM_SAVE_AS: emit_signal("save_project_as")
ITEM_EXPORT: emit_signal("export_svg")
ITEM_SETTINGS: emit_signal("open_settings_dialog")
ITEM_MANUAL: emit_signal("open_url", "https://github.com/mbrlabs/lorien/blob/main/docs/manuals/manual_v0.6.0.md")
ITEM_BUG_TRACKER: emit_signal("open_url", "https://github.com/mbrlabs/lorien/issues")
ITEM_ABOUT: emit_signal("open_about_dialog")
ITEM_SAVE: save_project.emit()
ITEM_SAVE_AS: save_project_as.emit()
ITEM_EXPORT: export_svg.emit()
ITEM_SETTINGS: open_settings_dialog.emit()
ITEM_MANUAL: open_url.emit("https://github.com/mbrlabs/lorien/blob/main/docs/manuals/manual_v0.6.0.md")
ITEM_BUG_TRACKER: open_url.emit("https://github.com/mbrlabs/lorien/issues")
ITEM_ABOUT: open_about_dialog.emit()

# -------------------------------------------------------------------------------------------------
func _on_open_project():
Expand All @@ -77,15 +72,10 @@ func _on_open_project():

# -------------------------------------------------------------------------------------------------
func _on_project_selected_to_open(filepath: String) -> void:
emit_signal("open_project", filepath)
open_project.emit(filepath)

# -------------------------------------------------------------------------------------------------
func _on_file_dialog_closed() -> void:
var file_dialog: FileDialog = get_node(file_dialog_path)
Utils.remove_signal_connections(file_dialog, "file_selected")
Utils.remove_signal_connections(file_dialog, "close_requested")

# -------------------------------------------------------------------------------------------------
func add_item_with_shortcut(target: PopupMenu, p_name: String, id: int, shortcut_action: String) -> void:
var shortcut = InputMap.action_get_events(shortcut_action)[0].get_keycode_with_modifiers()
target.add_item(p_name, id, shortcut)
5 changes: 2 additions & 3 deletions lorien/UI/MainMenu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
[ext_resource type="Script" path="res://UI/MainMenu.gd" id="2"]

[node name="MainMenu" type="PopupMenu"]
visible = true
unfocusable = true
theme = ExtResource("1")
submenu_popup_delay = 0.01
script = ExtResource("2")

[node name="ViewsMenu" type="PopupMenu" parent="."]
submenu_popup_delay = 0.01
111 changes: 59 additions & 52 deletions lorien/UI/Toolbar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,34 @@ var _last_active_tool_button: FlatTextureButton

# -------------------------------------------------------------------------------------------------
func _ready():
# Set inintial values
var brush_size: int = Settings.get_general_value(Settings.GENERAL_DEFAULT_BRUSH_SIZE, Config.DEFAULT_BRUSH_SIZE)
_brush_size_label.text = str(brush_size)
_brush_size_slider.value = brush_size
_last_active_tool_button = _tool_btn_brush

_new_button.pressed.connect(_on_NewFileButton_pressed)
_undo_button.pressed.connect(_on_UndoButton_pressed)
_redo_button.pressed.connect(_on_RedoButton_pressed)
_open_button.pressed.connect(_on_OpenFileButton_pressed)
_save_button.pressed.connect(_on_SaveFileButton_pressed)
_color_button.pressed.connect(_on_ColorButton_pressed)
_brush_size_slider.value_changed.connect(_on_BrushSizeSlider_value_changed)
_tool_btn_brush.pressed.connect(_on_BrushToolButton_pressed)
_tool_btn_rectangle.pressed.connect(_on_RectangleToolButton_pressed)
_tool_btn_circle.pressed.connect(_on_CircleToolButton_pressed)
_tool_btn_line.pressed.connect(_on_LineToolButton_pressed)
_tool_btn_eraser.pressed.connect(_on_EraserToolButton_pressed)
_tool_btn_selection.pressed.connect(_on_SelectToolButton_pressed)
# Tooltips (dynamic because they include keybindings)
for action: KeybindingsManager.Action in KeybindingsManager.get_actions():
_on_keybinding_changed(action)

# Signals
ProjectManager.active_project_changed.connect(_on_active_project_changed)
GlobalSignals.keybinding_changed.connect(_on_keybinding_changed)

_new_button.pressed.connect(func(): new_project.emit())
_undo_button.pressed.connect(func(): undo_action.emit())
_redo_button.pressed.connect(func(): redo_action.emit())
_open_button.pressed.connect(_on_open_project_pressed)
_save_button.pressed.connect(func(): save_project.emit())
_color_button.pressed.connect(func(): toggle_brush_color_picker.emit())
_brush_size_slider.value_changed.connect(_on_brush_size_changed)
_tool_btn_brush.pressed.connect(_on_brush_tool_pressed)
_tool_btn_rectangle.pressed.connect(_on_rectangle_tool_pressed)
_tool_btn_circle.pressed.connect(_on_circle_tool_pressed)
_tool_btn_line.pressed.connect(_on_line_tool_pressed)
_tool_btn_eraser.pressed.connect(_on_eraser_tool_pressed)
_tool_btn_selection.pressed.connect(_on_select_tool_pressed)

# Button clicked callbacks
# -------------------------------------------------------------------------------------------------
func _on_NewFileButton_pressed(): emit_signal("new_project")
func _on_UndoButton_pressed(): emit_signal("undo_action")
func _on_RedoButton_pressed(): emit_signal("redo_action")

# -------------------------------------------------------------------------------------------------
func enable_tool(tool_type: int) -> void:
var btn: TextureButton
Expand All @@ -77,7 +79,7 @@ func enable_tool(tool_type: int) -> void:

btn.toggle()
_change_active_tool_button(btn)
emit_signal("tool_changed", tool_type)
tool_changed.emit(tool_type)

# -------------------------------------------------------------------------------------------------
func set_brush_color(color: Color) -> void:
Expand All @@ -91,7 +93,28 @@ func set_brush_color(color: Color) -> void:
_color_button.text = "#" + color.to_html(false)

# -------------------------------------------------------------------------------------------------
func _on_OpenFileButton_pressed():
func get_brush_color_button() -> Control:
return _color_button

# -------------------------------------------------------------------------------------------------
func _on_keybinding_changed(action: KeybindingsManager.Action) -> void:
var label := action.event_label()
var fmt := "%s (%s)"
match action.name:
"shortcut_new_project": _new_button.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_NEW_FILE"), label]
"shortcut_open_project": _open_button.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_OPEN_FILE"), label]
"shortcut_save_project": _save_button.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_SAVE_FILE"), label]
"shortcut_undo": _undo_button.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_UNDO"), label]
"shortcut_redo": _redo_button.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_REDO"), label]
"shortcut_brush_tool": _tool_btn_brush.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_BRUSH_TOOL"), label]
"shortcut_rectangle_tool": _tool_btn_rectangle.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_RECTANGLE_TOOL"), label]
"shortcut_circle_tool": _tool_btn_circle.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_CIRCLE_TOOL"), label]
"shortcut_line_tool": _tool_btn_line.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_LINE_TOOL"), label]
"shortcut_eraser_tool": _tool_btn_eraser.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_ERASER_TOOL"), label]
"shortcut_select_tool": _tool_btn_selection.tooltip_text = fmt % [tr("TOOLBAR_TOOLTIP_SELECT_TOOL"), label]

# -------------------------------------------------------------------------------------------------
func _on_open_project_pressed():
var file_dialog: FileDialog = get_node(file_dialog_path)
file_dialog.file_mode = FileDialog.FILE_MODE_OPEN_FILE
file_dialog.file_selected.connect(_on_project_selected_to_open)
Expand All @@ -101,11 +124,7 @@ func _on_OpenFileButton_pressed():

# -------------------------------------------------------------------------------------------------
func _on_project_selected_to_open(filepath: String) -> void:
emit_signal("open_project", filepath)

# -------------------------------------------------------------------------------------------------
func _on_SaveFileButton_pressed():
emit_signal("save_project")
open_project.emit(filepath)

# -------------------------------------------------------------------------------------------------
func _on_file_dialog_closed() -> void:
Expand All @@ -114,59 +133,47 @@ func _on_file_dialog_closed() -> void:
Utils.remove_signal_connections(file_dialog, "close_requested")

# -------------------------------------------------------------------------------------------------
func _on_ColorButton_pressed():
emit_signal("toggle_brush_color_picker")

# -------------------------------------------------------------------------------------------------
func _on_background_color_changed(color: Color) -> void:
emit_signal("canvas_background_changed", color)

# -------------------------------------------------------------------------------------------------
func _on_BrushSizeSlider_value_changed(value: float):
func _on_brush_size_changed(value: float):
var new_size := int(value)
_brush_size_label.text = "%d" % new_size
emit_signal("brush_size_changed", new_size)
brush_size_changed.emit(new_size)

# -------------------------------------------------------------------------------------------------
func _on_BrushToolButton_pressed():
func _on_brush_tool_pressed():
_change_active_tool_button(_tool_btn_brush)
emit_signal("tool_changed", Types.Tool.BRUSH)
tool_changed.emit(Types.Tool.BRUSH)

# -------------------------------------------------------------------------------------------------
func _on_RectangleToolButton_pressed() -> void:
func _on_rectangle_tool_pressed() -> void:
_change_active_tool_button(_tool_btn_rectangle)
emit_signal("tool_changed", Types.Tool.RECTANGLE)
tool_changed.emit(Types.Tool.RECTANGLE)

# -------------------------------------------------------------------------------------------------
func _on_CircleToolButton_pressed():
func _on_circle_tool_pressed():
_change_active_tool_button(_tool_btn_circle)
emit_signal("tool_changed", Types.Tool.CIRCLE)
tool_changed.emit(Types.Tool.CIRCLE)

# -------------------------------------------------------------------------------------------------
func _on_LineToolButton_pressed():
func _on_line_tool_pressed():
_change_active_tool_button(_tool_btn_line)
emit_signal("tool_changed", Types.Tool.LINE)
tool_changed.emit(Types.Tool.LINE)

# -------------------------------------------------------------------------------------------------
func _on_EraserToolButton_pressed():
func _on_eraser_tool_pressed():
_change_active_tool_button(_tool_btn_eraser)
emit_signal("tool_changed", Types.Tool.ERASER)
tool_changed.emit(Types.Tool.ERASER)

# -------------------------------------------------------------------------------------------------
func _on_SelectToolButton_pressed():
func _on_select_tool_pressed():
_change_active_tool_button(_tool_btn_selection)
emit_signal("tool_changed", Types.Tool.SELECT)
tool_changed.emit(Types.Tool.SELECT)

# -------------------------------------------------------------------------------------------------
func _change_active_tool_button(btn: TextureButton) -> void:
if _last_active_tool_button != null:
_last_active_tool_button.toggle()
_last_active_tool_button = btn

# -------------------------------------------------------------------------------------------------
func get_brush_color_button() -> Control:
return _color_button

# -------------------------------------------------------------------------------------------------
func _on_active_project_changed(previous_project: Project, current_project: Project) -> void:
_update_undo_redo_buttons()
Expand Down
Loading

0 comments on commit ab6579b

Please sign in to comment.