You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.5 KiB
75 lines
2.5 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Game;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class GameUI : MonoBehaviour {
|
|
|
|
public delegate void ButtonDown(Side verticalSide, string horizontalSide);
|
|
public delegate void ButtonUp(Side side, string direction);
|
|
public static GameUI Singleton;
|
|
public ButtonDown buttonDown;
|
|
public ButtonUp buttonUp;
|
|
|
|
private UIDocument document;
|
|
|
|
private List<VisualElement> PlayerPanels => document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel")).ToList();
|
|
|
|
private void Awake() {
|
|
Singleton = this;
|
|
document = GetComponent<UIDocument>();
|
|
PreparePlayerPanels();
|
|
}
|
|
|
|
public VisualElement PlayerPanel(Side side) {
|
|
return side switch {
|
|
Side.Top => PlayerPanels[0],
|
|
Side.Bottom => PlayerPanels[1],
|
|
_ => throw new ArgumentOutOfRangeException(nameof(side), side, null)
|
|
};
|
|
}
|
|
|
|
public static VisualElement AddModification(VisualElement playerPanel, ModificationProperties properties) {
|
|
var listElement = ModsList(playerPanel, properties.type);
|
|
|
|
VisualElement newElement = new() {
|
|
style = {
|
|
backgroundImage = Background.FromSprite(properties.image)
|
|
}
|
|
};
|
|
listElement.Add(newElement);
|
|
|
|
return newElement;
|
|
}
|
|
|
|
private static VisualElement ModsList(VisualElement player, ModType type) {
|
|
return type switch {
|
|
ModType.Buff => player.Children().First(e => e.ClassListContains("mods_list")),
|
|
ModType.Nerf => player.Children().Last(e => e.ClassListContains("mods_list")),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
|
};
|
|
}
|
|
|
|
private void PreparePlayerPanels() {
|
|
var pixelHeight = Dimensions.Singleton.PanelHeightPixels;
|
|
PlayerPanel(Side.Top).style.height = PlayerPanel(Side.Bottom).style.height = new Length(pixelHeight, LengthUnit.Pixel);
|
|
|
|
SetupGoButton(Side.Top, "left");
|
|
SetupGoButton(Side.Top, "right");
|
|
SetupGoButton(Side.Bottom, "left");
|
|
SetupGoButton(Side.Bottom, "right");
|
|
}
|
|
|
|
public Label GetGoButton(Side sideVertical, string sideHorizontal) {
|
|
return PlayerPanel(sideVertical).Q<Label>("go_" + sideHorizontal);
|
|
}
|
|
|
|
private void SetupGoButton(Side sideVertical, string sideHorizontal) {
|
|
var element = GetGoButton(sideVertical, sideHorizontal);
|
|
element.RegisterCallback<PointerDownEvent>(_ => buttonDown(sideVertical, sideHorizontal));
|
|
element.RegisterCallback<PointerUpEvent>(_ => buttonUp(sideVertical, sideHorizontal));
|
|
element.RegisterCallback<PointerLeaveEvent>(_ => buttonUp(sideVertical, sideHorizontal));
|
|
}
|
|
}
|
|
|