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; using BangerTypes; namespace UITypes { public class GameElement : VisualElement { [UnityEngine.Scripting.Preserve] public new class UxmlFactory : UxmlFactory { } 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 bool leftActive = false; private bool rightActive = false; private bool downActive = false; private bool upActive = false; private Label score; private Label songTime; private VisualElement leftBackgroundImage; private VisualElement rightBackgroundImage; Dictionary 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(OnKeyDown, TrickleDown.TrickleDown); RegisterCallback(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; } public void UpdateNotes(Note[] notes) { leftActive = notes.Where(note => note.a == "left").Count() > 0; rightActive = notes.Where(note => note.a == "right").Count() > 0; upActive = notes.Where(note => note.a == "up").Count() > 0; downActive = notes.Where(note => note.a == "down").Count() > 0; leftUpcomingColumn.EnableInClassList("ColumnActive", leftActive); rightUpcomingColumn.EnableInClassList("ColumnActive", rightActive); downUpcomingColumn.EnableInClassList("ColumnActive", downActive); upUpcomingColumn.EnableInClassList("ColumnActive", upActive); } 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"); } } }