Skip to content

Commit

Permalink
Add shortcuts for suggested actions, list shortcuts on the settings s…
Browse files Browse the repository at this point in the history
…creen

Not ideal to show them on the settings screen when
they are not configurable, but the about/help screen
is very saturated as it is.
  • Loading branch information
YuriSizov committed Oct 30, 2024
1 parent e007b8b commit 13bb90a
Show file tree
Hide file tree
Showing 11 changed files with 681 additions and 26 deletions.
7 changes: 7 additions & 0 deletions globals/Controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func _notification(what: int) -> void:
_file_dialog.queue_free()


func _shortcut_input(event: InputEvent) -> void:
if event.is_action_pressed("glasan_randomize_voice", false, true):
voice_manager.randomize_current_voice()
elif event.is_action_pressed("glasan_play_note", false, true):
voice_manager.play_sample()


# Dialog management.

func get_file_dialog() -> FileDialog:
Expand Down
7 changes: 7 additions & 0 deletions globals/VoiceManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ func change_voice_type(type: int) -> void:
voice_changed.emit()


func randomize_current_voice() -> void:
if not _current_voice:
return

_current_voice.randomize_voice()


# Sample management.

func get_sample_params() -> Sample:
Expand Down
60 changes: 60 additions & 0 deletions gui/components/help/PianoOverlay.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
###################################################
# Part of Glasan FX #
# Copyright (c) 2024 Yuri Sizov and contributors #
# Provided under MIT #
###################################################

@tool
class_name PianoOverlay extends Control

@export var piano_container: PianoContainer = null:
set = set_piano_container

var _shortcut_buffers: Array[TextLine] = []


func _ready() -> void:
_update_shortcut_buffers()


func _draw() -> void:
if not is_instance_valid(piano_container):
return

for i in _shortcut_buffers.size():
var text_buffer := _shortcut_buffers[i]
var text_color := PianoKey.LIGHT_COLOR if Note.is_note_sharp(i) else PianoKey.DARK_COLOR

var key_rect := piano_container._valid_children[i].get_rect()
var text_position := key_rect.position + Vector2(key_rect.size.x / 2.0, key_rect.size.y)
text_position.x -= text_buffer.get_size().x / 2.0
text_position.y -= text_buffer.get_size().y + 6.0

text_buffer.draw(get_canvas_item(), text_position, text_color)


func _update_shortcut_buffers() -> void:
_shortcut_buffers.clear()
if not is_instance_valid(piano_container):
return

var font := get_theme_font("font")
var font_size := get_theme_font_size("font_size")

for i in piano_container.number_of_keys:
var text_buffer := TextLine.new()
var shortcut_name := &"glasan_play_key%d" % [ i ]
var shortcut_text := ShortcutLine.get_action_as_string(shortcut_name)

text_buffer.add_string(shortcut_text, font, font_size)
_shortcut_buffers.push_back(text_buffer)

queue_redraw()


func set_piano_container(value: PianoContainer) -> void:
if piano_container == value:
return

piano_container = value
_update_shortcut_buffers()
90 changes: 90 additions & 0 deletions gui/components/help/ShortcutLine.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
###################################################
# Part of Glasan FX #
# Copyright (c) 2024 Yuri Sizov and contributors #
# Provided under MIT #
###################################################

@tool
class_name ShortcutLine extends HBoxContainer

@export var key_text: String = "KEY":
set = set_key_text
@export var key_is_action: bool = true:
set = set_key_is_action
@export_multiline var description_text: String = "Description":
set = set_description_text

@onready var _key_label: Label = $KeyLabel
@onready var _description_label: Label = $DescriptionLabel


func _ready() -> void:
_update_labels()


func _update_labels() -> void:
if not is_inside_tree():
return

if key_is_action:
_key_label.text = get_action_as_string(key_text)
else:
_key_label.text = key_text

_description_label.text = description_text


func set_key_text(value: String) -> void:
key_text = value
_update_labels()


func set_key_is_action(value: bool) -> void:
key_is_action = value
_update_labels()


func set_description_text(value: String) -> void:
description_text = value
_update_labels()


static func get_action_as_string(action_text: String) -> String:
var bound_events := InputMap.action_get_events(action_text)
if bound_events.size() <= 0:
return "[UNBOUND]"

var first_event := bound_events[0]
if first_event is InputEventKey:
var key_event := first_event as InputEventKey
var key_bits := PackedStringArray()

if key_event.shift_pressed:
key_bits.push_back("SHIFT")
if key_event.ctrl_pressed:
key_bits.push_back("CTRL")
if key_event.meta_pressed:
if OS.has_feature("macos") || OS.has_feature("web_macos"):
key_bits.push_back("CMD")
elif OS.has_feature("windows") || OS.has_feature("web_windows"):
key_bits.push_back("WIN")
else:
key_bits.push_back("META")
if key_event.alt_pressed:
key_bits.push_back("ALT")

var keycode := key_event.keycode
if keycode == KEY_NONE:
keycode = key_event.physical_keycode

var keycode_string := OS.get_keycode_string(keycode)
if keycode in [ KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT ]:
keycode_string = "ARROW " + keycode_string

if keycode_string.is_empty():
keycode_string = "[UNKNOWN]"
key_bits.append(keycode_string)

return " + ".join(key_bits)

return first_event.as_text()
32 changes: 32 additions & 0 deletions gui/components/help/ShortcutLine.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[gd_scene load_steps=2 format=3 uid="uid://cinwiem08gkak"]

[ext_resource type="Script" path="res://gui/components/help/ShortcutLine.gd" id="1_06v85"]

[node name="ShortcutLine" type="HBoxContainer"]
auto_translate_mode = 1
anchors_preset = 10
anchor_right = 1.0
offset_bottom = 28.0
grow_horizontal = 2
script = ExtResource("1_06v85")

[node name="KeyLabel" type="Label" parent="."]
auto_translate_mode = 1
layout_mode = 2
size_flags_horizontal = 0
theme_type_variation = &"ShortcutLabelAccented"
text = "[UNBOUND]"
uppercase = true

[node name="Filler" type="Control" parent="."]
auto_translate_mode = 1
custom_minimum_size = Vector2(12, 0)
layout_mode = 2
size_flags_horizontal = 3

[node name="DescriptionLabel" type="Label" parent="."]
auto_translate_mode = 1
layout_mode = 2
size_flags_horizontal = 0
theme_type_variation = &"ShortcutLabel"
text = "Description"
9 changes: 6 additions & 3 deletions gui/components/preview/PianoContainer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const KEY_SCENE := preload("res://gui/components/preview/PianoKey.tscn")
@export var reset_keys: bool:
set = set_reset_keys

var _valid_children: int = 0
var _valid_children: Array[Control] = []
var _light_children: Array[Control] = []
var _dark_children: Array[Control] = []

Expand Down Expand Up @@ -86,6 +86,7 @@ func _get_minimum_size() -> Vector2:
var min_size := Vector2.ZERO
var child_index := 0

_valid_children.clear()
_light_children.clear()
_dark_children.clear()

Expand All @@ -107,9 +108,9 @@ func _get_minimum_size() -> Vector2:
else:
_dark_children.push_back(child_control)

_valid_children.push_back(child_control)
child_index += 1

_valid_children = child_index
return min_size


Expand Down Expand Up @@ -141,7 +142,7 @@ func _sort_children() -> void:
child_rect.position.x -= child_rect.size.x / 2.0

# Give the last child the remainder.
if child_index == (_valid_children - 1):
if child_index == (_valid_children.size() - 1):
child_rect.size.x = size.x - child_offset.x

fit_child_in_rect(key, child_rect)
Expand All @@ -157,6 +158,8 @@ func _sort_children() -> void:
child_index += 1


# Key management.

func set_number_of_keys(value: int) -> void:
if number_of_keys == value:
return
Expand Down
15 changes: 15 additions & 0 deletions gui/components/preview/PianoRoll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ func _ready() -> void:
_octave_down_button.pressed.connect(_shift_octave.bind(-1))


func _shortcut_input(event: InputEvent) -> void:
if event.is_action_pressed("glasan_octave_up", false, true):
_shift_octave(1)
elif event.is_action_pressed("glasan_octave_down", false, true):
_shift_octave(-1)

for i in Note.MAX:
var action_name := &"glasan_play_key%d" % [ i ]
if event.is_action_pressed(action_name, false, true):
_change_note(i)
break


# Property management.

func set_note_value(value: int) -> void:
if note_value == value:
return
Expand Down
12 changes: 8 additions & 4 deletions gui/panels/SettingsPanel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ func _ready() -> void:
super()
_update_settings_controls()

_scale_100_button.pressed.connect(Controller.settings_manager.set_gui_scale.bind(100))
_scale_125_button.pressed.connect(Controller.settings_manager.set_gui_scale.bind(125))
_scale_150_button.pressed.connect(Controller.settings_manager.set_gui_scale.bind(150))
_scale_200_button.pressed.connect(Controller.settings_manager.set_gui_scale.bind(200))
if not Engine.is_editor_hint():
_scale_100_button.pressed.connect(Controller.settings_manager.set_gui_scale.bind(100))
_scale_125_button.pressed.connect(Controller.settings_manager.set_gui_scale.bind(125))
_scale_150_button.pressed.connect(Controller.settings_manager.set_gui_scale.bind(150))
_scale_200_button.pressed.connect(Controller.settings_manager.set_gui_scale.bind(200))


# Settings management.

func _update_settings_controls() -> void:
if Engine.is_editor_hint():
return

var gui_scale := Controller.settings_manager.get_gui_scale()

_scale_100_button.button_pressed = (gui_scale == 100)
Expand Down
Loading

0 comments on commit 13bb90a

Please sign in to comment.