Skip to content

Commit

Permalink
Merge pull request #19 from SkyJumper409x/config_v2
Browse files Browse the repository at this point in the history
Config V2 (yes)
  • Loading branch information
raphaelgoulart authored Jun 23, 2024
2 parents 7163283 + 77d9787 commit 858d4bf
Show file tree
Hide file tree
Showing 29 changed files with 996 additions and 192 deletions.
57 changes: 57 additions & 0 deletions Binding.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extends Node

# mostly a convenience class

var current_input_source: int = InputSources.NONE
var kb_btn: int = -1
var gp_btn: int = -1
var gp_axis: int = -1
var gp_axis_device: int = -2 # cant use -1 because it is a constant for emulated mouse inputs for InputEvent.device
var gp_axis_positive: bool = true

# device and axis_positive are only needed if an axis binding is being set
func set_binding(input_source: int, binding: int, device: int=- 2, axis_positive: bool=true):
clear_bindings()

if input_source == InputSources.GP_AXIS:
gp_axis = binding
gp_axis_device = device
gp_axis_positive = axis_positive

elif input_source == InputSources.GP:
gp_btn = binding

elif input_source == InputSources.KB:
kb_btn = binding

else:
print_debug("invalid input source was supplied at BindingSet.set_binding:")
print("set_binding(" + str(input_source) + ", " + str(binding) + ", " + str(device) + ", " + str(axis_positive) + ")")

current_input_source = input_source

func set_binding_from_ev(ev: InputEvent):
if ev is InputEventJoypadMotion:
set_binding(InputSources.GP_AXIS, ev.axis, ev.device, ev.axis_value > 0)
elif ev is InputEventJoypadButton:
set_binding(InputSources.GP, ev.button_index)
elif ev is InputEventKey:
set_binding(InputSources.KB, ev.keycode)

func clear_bindings():
current_input_source = InputSources.NONE
kb_btn = -1
gp_btn = -1
gp_axis = -1
gp_axis_device = -2
gp_axis_positive = true

func get_current_binding():
if current_input_source == InputSources.KB:
return kb_btn
elif current_input_source == InputSources.GP:
return gp_btn
elif current_input_source == InputSources.GP_AXIS:
return gp_axis
else:
return InputSources.NONE
42 changes: 42 additions & 0 deletions ButtonHamburger.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extends TextureButton

const config_interface_scene = preload ("res://ConfigInterface/ConfigInterface.tscn")

var current_alpha: float
var target_alpha: float = 1
const anim_speed: float = 3 # higher value = faster fade in/out

func _ready():
pressed.connect(Singleton.show_config_window)

if not ConfigHandler.current_config.always_show_hamburger:
target_alpha = 0
current_alpha = target_alpha
modulate.a = target_alpha

mouse_entered.connect(_on_mouse_entered)
mouse_exited.connect(_on_mouse_exited)

func _process(delta):
anim_step(delta)

func anim_step(delta):
# there is no need for checking if current_alpha is equal to target_alpha,
# because if they are, (target_alpha - current_alpha) * delta = 0 * delta = 0
# It is done anyways for clarity :3
if current_alpha != target_alpha:
current_alpha += (target_alpha - current_alpha) * delta * anim_speed
current_alpha = clampf(current_alpha, 0, 1) # could be combined into the previous line but it's more readable like this

# 2.2 is ease in, 0.2 is ease out.
# Using ease in if target alpha is 1 or vice versa feels very slow.
modulate.a = ease(current_alpha, 2.2 - target_alpha * 2)

func change_to_alpha(new_alpha: float):
target_alpha = new_alpha

func _on_mouse_entered():
change_to_alpha(1)
func _on_mouse_exited():
if not ConfigHandler.current_config.always_show_hamburger:
change_to_alpha(0)
Binary file added ButtonHamburgerIcons/Click_mask.bmp
Binary file not shown.
16 changes: 16 additions & 0 deletions ButtonHamburgerIcons/Click_mask.bmp.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[remap]

importer="bitmap"
type="BitMap"
uid="uid://cnj1o7mv0758k"
path="res://.godot/imported/Click_mask.bmp-ef08cc1f9d05d7c756d714ed7aa634bc.res"

[deps]

source_file="res://ButtonHamburgerIcons/Click_mask.bmp"
dest_files=["res://.godot/imported/Click_mask.bmp-ef08cc1f9d05d7c756d714ed7aa634bc.res"]

[params]

create_from=0
threshold=0.5
Binary file added ButtonHamburgerIcons/Hover.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 ButtonHamburgerIcons/Hover.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://ds1xh0grlep6b"
path="res://.godot/imported/Hover.png-6fa8e000c33daf0e90259baf33d65873.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://ButtonHamburgerIcons/Hover.png"
dest_files=["res://.godot/imported/Hover.png-6fa8e000c33daf0e90259baf33d65873.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
Binary file added ButtonHamburgerIcons/Idle.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 ButtonHamburgerIcons/Idle.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://hfjxlldsajma"
path="res://.godot/imported/Idle.png-1ee98660680919ba7ac7aea079d528ad.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://ButtonHamburgerIcons/Idle.png"
dest_files=["res://.godot/imported/Idle.png-1ee98660680919ba7ac7aea079d528ad.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
Binary file added ButtonHamburgerIcons/Inbetween.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 ButtonHamburgerIcons/Inbetween.png.import
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[remap]

importer="texture"
type="CompressedTexture2D"
uid="uid://bb1lgvbgvt6ok"
path="res://.godot/imported/Inbetween.png-c1dae802336d430899742637bac7fc6e.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://ButtonHamburgerIcons/Inbetween.png"
dest_files=["res://.godot/imported/Inbetween.png-c1dae802336d430899742637bac7fc6e.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
10 changes: 10 additions & 0 deletions Config.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extends Object

var version: float = 2

var scroll_rate: float = 400.0
var input_bar_width: int = 50
var always_show_hamburger: bool = true

# TODO: assign these to the inputbtns instead of storing them here
var colors = [Color.GREEN, Color.RED, Color.YELLOW, Color.BLUE, Color(1, 0.5, 0), Color(0.5, 0, 1), Color(0.5, 0, 1)]
141 changes: 141 additions & 0 deletions ConfigHandler.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
extends Node

# this script is used for all config file reading and writing

const Binding = preload ("res://Binding.gd")
const InputBtn = preload ("res://InputBtn.gd")
const Config = preload ("res://Config.gd")

const valid_config_versions = [1, 1.1, 2]

const legacy_color_section_keys = ["green", "red", "yellow", "blue", "orange", "up", "down"]
const btn_config_names = ["fret_0", "fret_1", "fret_2", "fret_3", "fret_4", "strum_up", "strum_down"]

var current_config: Config

func _ready():
load_cfg()

var config_file: ConfigFile
func load_cfg():
current_config = Config.new()
config_file = ConfigFile.new()
var err = config_file.load("user://yaid_settings.cfg")
if err != OK:
print("error reading settings file; using default values...")
config_file = null
return

if config_file.has_section("Meta"):
current_config.version = config_file.get_value("Meta", "config_version")
else:
current_config.version = 1

current_config.scroll_rate = config_file.get_value("Settings", "scroll_rate", 400.0)
if current_config.version < 2:
current_config.input_bar_width = config_file.get_value("Settings", "width", 50)
else:
current_config.input_bar_width = config_file.get_value("Settings", "input_bar_width", 50)

current_config.always_show_hamburger = config_file.get_value("Settings", "always_show_hamburger", true)
# colors
load_colors()

func load_colors():
for i in 7:
if config_file.has_section_key("Colors", color_index_to_config_key(i)):
current_config.colors[i] = Color(config_file.get_value("Colors", color_index_to_config_key(i)), 1)

func load_binding(btn: InputBtn): # called by an inputbtn when it's _ready() function is called
var section_name = btn.get_config_name(current_config.version)
var result = Binding.new()
if config_file == null:
result.set_binding(InputSources.GP, btn.get_meta("gp_btn"))
return result

# default to v2 keys, if none are found, the default values will prevent any errors
result.set_binding(
config_file.get_value(section_name, "input_source", InputSources.GP),
config_file.get_value(section_name, "binding", btn.get_meta("gp_btn")),
config_file.get_value(section_name, "gp_axis_device", -2), # we can just attempt to supply the arguments for an axis binding
config_file.get_value(section_name, "gp_axis_positive", true) # because they will be ignored anyways if current_input_source isn't GP_AXIS
)

if current_config.version < 2:
var input_source: int = InputSources.NONE
for i in 3: # iterates over all InputSources (except InputSources.NONE)
if config_file.has_section_key(section_name, input_source_to_config_key(i)):
input_source = i
# dont break once a binding was found, so if multiple bindings exist in the config_file, the priority is gp_axis > gp_btn > kb_btn
if input_source > InputSources.NONE: # legacy binding was found in the config_file, so use it
result.set_binding(
input_source,
config_file.get_value(section_name, input_source_to_config_key(input_source)),
config_file.get_value(section_name, "gp_axis_device", -2),
config_file.get_value(section_name, "gp_axis_positive", true)
)

return result

var new_config_file # this are declared here so all config file saving methods can easily access it
var new_config_file_version
func save_config(version=2): # version should be either 1, 1.1 or 2

new_config_file_version = version
# Create new config_file file
new_config_file = ConfigFile.new()

# config_file version
new_config_file.set_value("Meta", "config_version", new_config_file_version)

# misc settings
new_config_file.set_value("Settings", "scroll_rate", current_config.scroll_rate)

if new_config_file_version < 2:
new_config_file.set_value("Settings", "width", current_config.input_bar_width)
else:
new_config_file.set_value("Settings", "input_bar_width", current_config.input_bar_width)
new_config_file.set_value("Settings", "always_show_hamburger", current_config.always_show_hamburger)
# colors
save_colors()

# bindings
save_bindings()

# Save it to a file (overwrite if already exists).
new_config_file.save("user://yaid_settings.cfg")

func save_colors():
for i in 7:
new_config_file.set_value("Colors", color_index_to_config_key(i, new_config_file_version), "#" + current_config.colors[i].to_html(false))

func save_bindings():
for btn: InputBtn in Singleton.btns:
var config_name = btn.get_config_name(new_config_file_version)

var binding = btn.binding

if new_config_file_version < 2:
new_config_file.set_value(config_name, input_source_to_config_key(binding.current_input_source, new_config_file_version), binding.get_current_binding())
else:
new_config_file.set_value(config_name, "input_source", binding.current_input_source)
new_config_file.set_value(config_name, "binding", binding.get_current_binding())
if binding.current_input_source == InputSources.GP_AXIS:
new_config_file.set_value(config_name, "gp_axis_device", binding.gp_axis_device)
new_config_file.set_value(config_name, "gp_axis_positive", binding.gp_axis_positive)

func color_index_to_config_key(index: int, version: float=current_config.version):
if version < 2:
return legacy_color_section_keys[index]
else:
return btn_config_names[index]

func input_source_to_config_key(input_source: int, version: float=current_config.version):
if input_source == InputSources.NONE:
return "NONE"
elif input_source == InputSources.KB:
return "kb_btn" if version > 1 else "btn"
elif input_source == InputSources.GP:
return "gp_btn"
elif input_source == InputSources.GP_AXIS:
return "gp_axis"
11 changes: 11 additions & 0 deletions ConfigInterface/ConfigInterface.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extends Window

func _input(ev):
if ev is InputEventKey:
if not ev.echo and (ev.pressed and ev.keycode == KEY_ESCAPE):
Singleton.update_spinboxes()
hide()

func _notification(what):
if what == NOTIFICATION_WM_CLOSE_REQUEST:
hide()
Loading

0 comments on commit 858d4bf

Please sign in to comment.