125 lines
3.1 KiB
C#
125 lines
3.1 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Runtime.InteropServices.WindowsRuntime;
|
||
|
using Unity.VisualScripting;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UIElements;
|
||
|
|
||
|
|
||
|
namespace UITypes
|
||
|
{
|
||
|
|
||
|
public class GameElement : VisualElement
|
||
|
{
|
||
|
[UnityEngine.Scripting.Preserve]
|
||
|
public new class UxmlFactory : UxmlFactory<GameElement> { }
|
||
|
|
||
|
public Button Left { get; set; }
|
||
|
public Button Right { get; set; }
|
||
|
public Button Up { get; set; }
|
||
|
public Button Down { get; set; }
|
||
|
|
||
|
private Visualizer leftVisualizer;
|
||
|
private Visualizer rightVisualizer;
|
||
|
|
||
|
|
||
|
private VisualElement leftUpcomingColumn;
|
||
|
private VisualElement rightUpcomingColumn;
|
||
|
private VisualElement upUpcomingColumn;
|
||
|
public VisualElement downUpcomingColumn;
|
||
|
|
||
|
private Label score;
|
||
|
private Label songTime;
|
||
|
|
||
|
private VisualElement leftBackgroundImage;
|
||
|
private VisualElement rightBackgroundImage;
|
||
|
|
||
|
Dictionary<KeyCode, Button> buttonDictionary;
|
||
|
|
||
|
Game game;
|
||
|
|
||
|
public void InitGame(Game game)
|
||
|
{
|
||
|
this.game = game;
|
||
|
leftVisualizer = this.Q("LeftVisualizer") as Visualizer;
|
||
|
rightVisualizer = this.Q("RightVisualizer") as Visualizer;
|
||
|
|
||
|
Left = this.Q("GameLeftButton") as Button;
|
||
|
Right = this.Q("GameRightButton") as Button;
|
||
|
Up = this.Q("GameUpButton") as Button;
|
||
|
Down = this.Q("GameDownButton") as Button;
|
||
|
|
||
|
score = this.Q("Score") as Label;
|
||
|
songTime = this.Q("TimeLeft") as Label;
|
||
|
|
||
|
leftBackgroundImage = this.Q("GameBackgroundImageLeft");
|
||
|
rightBackgroundImage = this.Q("GameBackgroundImageRight");
|
||
|
|
||
|
leftUpcomingColumn = this.Q("LeftArrowUpcomingColumn");
|
||
|
rightUpcomingColumn = this.Q("RightArrowUpcomingColumn");
|
||
|
upUpcomingColumn = this.Q("UpArrowUpcomingColumn");
|
||
|
downUpcomingColumn = this.Q("DownArrowUpcomingColumn");
|
||
|
|
||
|
RegisterCallback<KeyDownEvent>(OnKeyDown, TrickleDown.TrickleDown);
|
||
|
RegisterCallback<KeyUpEvent>(OnKeyUp, TrickleDown.TrickleDown);
|
||
|
|
||
|
leftVisualizer.InitVisualizer("LeftBar");
|
||
|
rightVisualizer.InitVisualizer("RightBar");
|
||
|
|
||
|
buttonDictionary = new()
|
||
|
{
|
||
|
{ KeyCode.LeftArrow, Left },
|
||
|
{ KeyCode.RightArrow, Right },
|
||
|
{ KeyCode.UpArrow, Up },
|
||
|
{ KeyCode.DownArrow, Down },
|
||
|
};
|
||
|
}
|
||
|
|
||
|
public void SetColumnColor(Color32 color)
|
||
|
{
|
||
|
leftVisualizer.SetBarColor(color);
|
||
|
rightVisualizer.SetBarColor(color);
|
||
|
}
|
||
|
|
||
|
public void UpdateVisualizers(float[] specData, float bassStrength)
|
||
|
{
|
||
|
leftVisualizer.UpdateBars(specData);
|
||
|
rightVisualizer.UpdateBars(specData);
|
||
|
|
||
|
leftBackgroundImage.style.opacity = new StyleFloat(bassStrength);
|
||
|
rightBackgroundImage.style.opacity = new StyleFloat(bassStrength);
|
||
|
}
|
||
|
|
||
|
public void UpdateSongTime(string time)
|
||
|
{
|
||
|
songTime.text = time;
|
||
|
}
|
||
|
|
||
|
void OnKeyDown(KeyDownEvent ev)
|
||
|
{
|
||
|
if (ev.keyCode == KeyCode.Escape)
|
||
|
{
|
||
|
game.EscapeClicked();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
buttonDictionary.TryGetValue(ev.keyCode, out Button button);
|
||
|
|
||
|
if (button == null) return;
|
||
|
|
||
|
button.AddToClassList("GameButtonClicked");
|
||
|
}
|
||
|
|
||
|
void OnKeyUp(KeyUpEvent ev)
|
||
|
{
|
||
|
buttonDictionary.TryGetValue(ev.keyCode, out Button button);
|
||
|
|
||
|
if (button == null) return;
|
||
|
|
||
|
button.RemoveFromClassList("GameButtonClicked");
|
||
|
}
|
||
|
}
|
||
|
}
|