[WIP] Song selection via touchscreen

This commit is contained in:
Luke Hubmayer-Werner 2019-11-25 18:39:35 +10:30
parent a42781b8fd
commit 2a36f572ad
1 changed files with 55 additions and 23 deletions

78
Menu.gd
View File

@ -8,12 +8,16 @@ enum ChartDifficulty {EASY, BASIC, ADV, EXPERT, MASTER}
var selected_genre: int = 0 var selected_genre: int = 0
var selected_song: int = 0 var selected_song: int = 0
var selected_song_vis: int = 0
var selected_song_delta: float = 0.0 # For floaty display scrolling var selected_song_delta: float = 0.0 # For floaty display scrolling
var selected_song_speed: float = 0.25 # For floaty display scrolling var selected_song_speed: float = 0.25 # For floaty display scrolling
var selected_difficulty = ChartDifficulty.ADV var selected_difficulty = ChartDifficulty.ADV
var touchrects = []
var TitleFont := preload("res://assets/MenuTitleFont.tres") var TitleFont := preload("res://assets/MenuTitleFont.tres")
var GenreFont := preload("res://assets/MenuGenreFont.tres") var GenreFont := preload("res://assets/MenuGenreFont.tres")
var snd_interact := preload("res://assets/softclap.wav")
func scan_library(): func scan_library():
print("Scanning library") print("Scanning library")
@ -47,44 +51,46 @@ func _ready():
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): func _process(delta):
var diff = selected_song - (selected_song_vis + selected_song_delta)
selected_song_speed = sign(diff)*ease(abs(diff), 2)*10
selected_song_delta += selected_song_speed * delta selected_song_delta += selected_song_speed * delta
if selected_song_delta > 0.5: if selected_song_delta > 0.5:
selected_song_delta -= 1.0 selected_song_delta -= 1.0
selected_song += 1 selected_song_vis += 1
elif selected_song_delta < -0.5: elif selected_song_delta < -0.5:
selected_song_delta += 1.0 selected_song_delta += 1.0
selected_song -= 1 selected_song_vis -= 1
update() update()
func draw_string_centered(font, position, string, color := Color.white): func draw_string_centered(font, position, string, color := Color.white):
draw_string(font, Vector2(position.x - font.get_string_size(string).x/2.0, position.y + font.get_ascent()), string, color) draw_string(font, Vector2(position.x - font.get_string_size(string).x/2.0, position.y + font.get_ascent()), string, color)
func draw_songtile(song_key, position, size, title_text:=false, difficulty=selected_difficulty, outline_px:=3): func draw_songtile(song_key, position, size, title_text:=false, difficulty=selected_difficulty, outline_px:=3):
# Draws from top left-corner. # Draws from top left-corner. Returns Rect2 of the image (not the outline).
# Draw difficulty-colored outline # Draw difficulty-colored outline
var diff_color := GameTheme.COLOR_DIFFICULTY[difficulty*2] var diff_color := GameTheme.COLOR_DIFFICULTY[difficulty*2]
var rect := Rect2(position.x, position.y, size, size)
draw_rect(Rect2(position.x - outline_px, position.y - outline_px, size + outline_px*2, size + outline_px*2), diff_color) draw_rect(Rect2(position.x - outline_px, position.y - outline_px, size + outline_px*2, size + outline_px*2), diff_color)
draw_texture_rect(song_images[song_key], Rect2(position.x, position.y, size, size), false) draw_texture_rect(song_images[song_key], rect, false)
# Draw track difficulty rating # Draw track difficulty rating
draw_string_centered(GenreFont, Vector2(position.x+size-24, position.y+size-56), diffstr(song_defs[song_key]["chart_difficulties"][difficulty]), diff_color) draw_string_centered(GenreFont, Vector2(position.x+size-24, position.y+size-56), diffstr(song_defs[song_key]["chart_difficulties"][difficulty]), diff_color)
if title_text: if title_text:
draw_string_centered(TitleFont, Vector2(position.x + size/2.0, position.y+size), song_defs[song_key]["title"], Color(0.95, 0.95, 1.0)) draw_string_centered(TitleFont, Vector2(position.x+size/2.0, position.y+size), song_defs[song_key]["title"], Color(0.95, 0.95, 1.0))
return rect
func diffstr(difficulty: float): func diffstr(difficulty: float):
# Convert .5 to + # Convert .5 to +
return str(int(floor(difficulty))) + ("+" if fmod(difficulty, 1.0)>0.4 else "") return str(int(floor(difficulty))) + ("+" if fmod(difficulty, 1.0)>0.4 else "")
func _draw():
var songs = len(song_defs) func _draw_song_select(center_x):
var size = 216 var size = 216
var spacer_x = 12 var spacer_x = 12
var spacer_y = 64 var spacer_y = 64
var outline_px = 3
var center_x = 0.0
var sel_scales := [1.0, 0.8, 0.64, 0.512, 0.4096] var sel_scales := [1.0, 0.8, 0.64, 0.512, 0.4096]
var bg_scales := [0.64, 0.64, 0.64, 0.512, 0.4096] var bg_scales := [0.64, 0.64, 0.64, 0.512, 0.4096]
var gy := -160.0 var gy := -160.0
for g in len(genres): for g in len(genres):
var selected: bool = (g == selected_genre) var selected: bool = (g == selected_genre)
var base_scales = sel_scales if selected else bg_scales var base_scales = sel_scales if selected else bg_scales
@ -107,33 +113,59 @@ func _draw():
draw_string_centered(GenreFont, Vector2(0, gy), genre) draw_string_centered(GenreFont, Vector2(0, gy), genre)
var songslist = genres[genre] var songslist = genres[genre]
var s = len(songslist) var s = len(songslist)
var key = songslist[selected_song % s] var key = songslist[selected_song_vis % s]
var y = gy + spacer_y var y = gy + spacer_y
var x = -subsize/2.0 var x = -subsize/2.0
draw_songtile(key, Vector2(gx+x, y), subsize, selected, selected_difficulty) var r = draw_songtile(key, Vector2(gx+x, y), subsize, selected, selected_difficulty)
for i in [1, 2, 3, 4]: touchrects.append({rect=r, song_idx=selected_song_vis, genre_idx=g})
for i in range(1, len(base_scales)):
x += subsize + spacer_x x += subsize + spacer_x
subsize = size * scales[i] subsize = size * scales[i]
draw_songtile(songslist[(selected_song+i) % s], Vector2(gx+x, y), subsize) r = draw_songtile(songslist[(selected_song_vis+i) % s], Vector2(gx+x, y), subsize)
touchrects.append({rect=r, song_idx=selected_song_vis+i, genre_idx=g})
subsize = size * scales[0] subsize = size * scales[0]
x = -subsize/2.0 x = -subsize/2.0
for i in [1, 2, 3, 4]: for i in range(1, len(base_scales)):
x += subsize + spacer_x x += subsize + spacer_x
subsize = size * scales[-i] subsize = size * scales[-i]
draw_songtile(songslist[(selected_song-i) % s], Vector2(gx-x - subsize, y), subsize) r = draw_songtile(songslist[(selected_song_vis-i) % s], Vector2(gx-x - subsize, y), subsize)
gy += size + (spacer_y * 2) touchrects.append({rect=r, song_idx=selected_song_vis-i, genre_idx=g})
gy += size*base_scales[0] + (spacer_y * 2)
func _draw():
var songs = len(song_defs)
var size = 216
var outline_px = 3
var center_x = 0.0
touchrects = []
_draw_song_select(center_x)
func touch_select_song(touchdict):
self.selected_genre = touchdict.genre_idx
self.selected_song = touchdict.song_idx
SFXPlayer.play(SFXPlayer.Type.NON_POSITIONAL, self, snd_interact)
func _input(event): func _input(event):
if event.is_action_pressed("ui_right"): if event is InputEventScreenTouch:
if event.pressed:
var pos = event.position - get_global_transform_with_canvas().get_origin()
for d in touchrects:
if d.rect.has_point(pos):
touch_select_song(d)
elif event.is_action_pressed("ui_right"):
selected_song += 1 selected_song += 1
if event.is_action_pressed("ui_left"): elif event.is_action_pressed("ui_left"):
selected_song -= 1 selected_song -= 1
if event.is_action_pressed("ui_up"): elif event.is_action_pressed("ui_up"):
selected_genre = int(max(0, selected_genre - 1)) selected_genre = int(max(0, selected_genre - 1))
if event.is_action_pressed("ui_down"): elif event.is_action_pressed("ui_down"):
selected_genre = int(min(1, selected_genre + 1)) selected_genre = int(min(1, selected_genre + 1))
if event.is_action_pressed("ui_page_up"): elif event.is_action_pressed("ui_page_up"):
selected_difficulty = int(max(0, selected_difficulty - 1)) selected_difficulty = int(max(0, selected_difficulty - 1))
if event.is_action_pressed("ui_page_down"): elif event.is_action_pressed("ui_page_down"):
selected_difficulty = int(min(4, selected_difficulty + 1)) selected_difficulty = int(min(4, selected_difficulty + 1))