Skip to content

Commit

Permalink
Improve the example project around visuals and drumkits
Browse files Browse the repository at this point in the history
Closes #8.
  • Loading branch information
YuriSizov committed Jun 25, 2024
1 parent ad8ebe4 commit cb03af2
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 40 deletions.
2 changes: 2 additions & 0 deletions example/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const TEST_TUNE := "t100 l8 [ ccggaag4 ffeeddc4 | [ggffeed4]2 ]2"


func _ready() -> void:
get_window().min_size = Vector2(640, 360)

_play_button.pressed.connect(Controller.music_player.play_tune.bind(TEST_TUNE))
_kenney_label.meta_clicked.connect(_handle_meta_clicked)

Expand Down
25 changes: 20 additions & 5 deletions example/Main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=10 format=3 uid="uid://btlcavlirkxet"]
[gd_scene load_steps=11 format=3 uid="uid://btlcavlirkxet"]

[ext_resource type="Script" path="res://Main.gd" id="1_8sk4m"]
[ext_resource type="Script" path="res://gui/Visualizer.gd" id="2_3u74r"]
Expand All @@ -9,6 +9,7 @@
[ext_resource type="Texture2D" uid="uid://c37ayws6ebi88" path="res://icons/arrow_left.png" id="4_btwgs"]
[ext_resource type="Script" path="res://gui/FilterSelector.gd" id="4_uavpo"]
[ext_resource type="Texture2D" uid="uid://bp1awbe5naawb" path="res://icons/arrow_right.png" id="5_ururd"]
[ext_resource type="Texture2D" uid="uid://d4darly7q0o0o" path="res://icons/level.png" id="7_6h62v"]

[node name="Main" type="Control"]
layout_mode = 3
Expand Down Expand Up @@ -48,6 +49,9 @@ script = ExtResource("3_71u1t")
layout_mode = 1
anchors_preset = -1
anchor_right = 1.0
offset_left = 10.0
offset_top = -44.0
offset_right = -10.0
offset_bottom = -8.0
grow_horizontal = 2
grow_vertical = 0
Expand All @@ -57,7 +61,7 @@ auto_translate_mode = 1
layout_mode = 2
theme_override_colors/font_color = Color(0.503461, 0.593425, 0.670281, 1)
theme_override_font_sizes/font_size = 16
text = "Filter:"
text = "Effect:"
horizontal_alignment = 2
vertical_alignment = 1

Expand All @@ -79,7 +83,7 @@ expand_icon = true
auto_translate_mode = 1
layout_mode = 2
theme_override_font_sizes/font_size = 20
text = "[FILTER NAME]"
text = "[EFFECT NAME]"

[node name="RightButton" type="Button" parent="PianoRoll/Controls/FilterSelector"]
auto_translate_mode = 1
Expand All @@ -90,10 +94,21 @@ flat = true
icon_alignment = 1
expand_icon = true

[node name="PowerSlider" type="HSlider" parent="PianoRoll/Controls/FilterSelector"]
custom_minimum_size = Vector2(240, 0)
[node name="Control" type="Control" parent="PianoRoll/Controls/FilterSelector"]
layout_mode = 2

[node name="PowerIcon" type="TextureRect" parent="PianoRoll/Controls/FilterSelector"]
custom_minimum_size = Vector2(32, 32)
layout_mode = 2
size_flags_vertical = 4
texture = ExtResource("7_6h62v")
expand_mode = 1
stretch_mode = 5

[node name="PowerSlider" type="HSlider" parent="PianoRoll/Controls/FilterSelector"]
custom_minimum_size = Vector2(200, 0)
layout_mode = 2
size_flags_vertical = 1

[node name="Filler" type="Control" parent="PianoRoll/Controls"]
layout_mode = 2
Expand Down
2 changes: 1 addition & 1 deletion example/globals/MusicPlayer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const AVAILABLE_INSTRUMENTS := [
{ "category": "CHIPTUNE", "name": "Square Wave" },
{ "category": "CHIPTUNE", "name": "Triangle LO-FI" },

{ "category": "DRUMKIT", "name": "Midi Drumkit" },
{ "category": "DRUMKIT", "name": "Simple Drumkit" },
]

const AVAILABLE_FILTERS := [
Expand Down
94 changes: 61 additions & 33 deletions example/gui/PianoRoll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# Provided under MIT #
###################################################

@tool
extends Control

const FONT := preload("res://fonts/Kenney Future Narrow.ttf")
const NOTE_LENGTH := 4

var _keys: Array = []
var _notes: PackedStringArray = PackedStringArray()
Expand All @@ -30,9 +32,10 @@ func _init() -> void:

func _ready() -> void:
_update_instrument()
Controller.music_player.instrument_changed.connect(_update_instrument)

_update_positions()

if not Engine.is_editor_hint():
Controller.music_player.instrument_changed.connect(_update_instrument)


func _draw() -> void:
Expand Down Expand Up @@ -79,34 +82,47 @@ func _update_notes() -> void:


func _update_keys() -> void:
_keys.resize(256)
_keys.fill(-1)
if is_drumkit:
var instrument := Controller.music_player.get_active_instrument()
var active_drumkit := Controller.voice_manager.get_drumkit(instrument.type)

_keys.resize(active_drumkit.size)
for i in active_drumkit.size:
_keys[i] = i
else:
_keys.resize(256)
_keys.fill(-1)

const scale_layout := [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
var scale_size := scale_layout.size()
const scale_layout := [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]
var scale_size := scale_layout.size()

var key_count := 0
var key_idx := 0
var scale_idx := 0
var last_note := 0
var key_count := 0
var key_idx := 0
var scale_idx := 0
var last_note := 0

while last_note < 104:
_keys[key_idx] = last_note
key_count += 1
while last_note < 104:
_keys[key_idx] = last_note
key_count += 1

last_note += scale_layout[scale_idx]
last_note += scale_layout[scale_idx]

key_idx += 1
scale_idx += 1
if scale_idx >= scale_size:
scale_idx -= scale_size
key_idx += 1
scale_idx += 1
if scale_idx >= scale_size:
scale_idx -= scale_size

_keys.resize(key_count)
_keys.resize(key_count)


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

var instrument := Controller.music_player.get_active_instrument()
is_drumkit = instrument.type != 0

_update_keys()
_update_positions()
queue_redraw()

Expand All @@ -117,14 +133,19 @@ func _update_positions() -> void:
_normal_key_boxes.clear()
_sharp_key_boxes.clear()

var active_drumkit: Drumkit = null
var normal_key_count := 0
for i in _keys.size():
var key: int = _keys[i]
var note_name := _notes[key]

var sharp_note := (is_drumkit && i % 2 == 0) || (not is_drumkit && note_name.ends_with("#"))
if not sharp_note:
normal_key_count += 1

if is_drumkit:
var instrument := Controller.music_player.get_active_instrument()
active_drumkit = Controller.voice_manager.get_drumkit(instrument.type)
normal_key_count = _keys.size()
else:
for i in _keys.size():
var key: int = _keys[i]
var note_name := _notes[key]
if not note_name.ends_with("#"):
normal_key_count += 1

# Split available space evenly, but don't make resulting keys too wide.
var key_width: int = min(size.x / normal_key_count, 120)
Expand All @@ -133,9 +154,15 @@ func _update_positions() -> void:
var offset_idx := -1
for i in _keys.size():
var key: int = _keys[i]
var note_name := _notes[key]
var note_name := ""
var sharp_note := false

if is_drumkit:
note_name = active_drumkit.voice_name[key]
else:
note_name = _notes[key]
sharp_note = note_name.ends_with("#")

var sharp_note := (is_drumkit && i % 2 == 0) || (not is_drumkit && note_name.ends_with("#"))
if not sharp_note:
offset_idx += 1

Expand All @@ -145,10 +172,7 @@ func _update_positions() -> void:
if sharp_note:
key_size.x *= 0.75
key_size.y /= 2
if is_drumkit:
key_position.x += key_width + (key_width - key_size.x) / 2
else:
key_position.x += key_width - key_size.x / 2
key_position.x += key_width - key_size.x / 2
key_position.y += 2

var key_box := KeyBox.new(Rect2(key_position, key_size), note_name, i)
Expand Down Expand Up @@ -200,7 +224,11 @@ func _update_active_key(key: int) -> void:
return

_active_key = key
Controller.music_player.play_note(_active_key, 4)
var note_length := NOTE_LENGTH
if is_drumkit:
note_length = NOTE_LENGTH * 2

Controller.music_player.play_note(_active_key, note_length)
queue_redraw()

func _clear_active_key() -> void:
Expand Down
Binary file added example/icons/level.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions example/icons/level.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://d4darly7q0o0o"
path="res://.godot/imported/level.png-f7a55b75d8936d2b808d4d0c370ea7ab.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://icons/level.png"
dest_files=["res://.godot/imported/level.png-f7a55b75d8936d2b808d4d0c370ea7ab.ctex"]

[params]

compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
1 change: 1 addition & 0 deletions example/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ theme/custom="res://project_theme.tres"
renderer/rendering_method="gl_compatibility"
renderer/rendering_method.mobile="gl_compatibility"
textures/vram_compression/import_etc2_astc=true
limits/global_shader_variables/buffer_size=4096
environment/defaults/default_clear_color=Color(0.0603977, 0.0700237, 0.101998, 1)
12 changes: 11 additions & 1 deletion example/project_theme.tres
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
[gd_resource type="Theme" load_steps=2 format=3 uid="uid://bm402itx0nq0t"]
[gd_resource type="Theme" load_steps=3 format=3 uid="uid://bm402itx0nq0t"]

[ext_resource type="FontFile" uid="uid://y70lqerdtol1" path="res://fonts/Kenney Future Narrow.ttf" id="1_8d45h"]

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_iv47y"]
content_margin_top = 4.0
content_margin_bottom = 4.0
bg_color = Color(0.248965, 0.270927, 0.316075, 1)
corner_radius_top_left = 4
corner_radius_top_right = 4
corner_radius_bottom_right = 4
corner_radius_bottom_left = 4

[resource]
default_font = ExtResource("1_8d45h")
default_font_size = 30
HSlider/styles/slider = SubResource("StyleBoxFlat_iv47y")

0 comments on commit cb03af2

Please sign in to comment.