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.

76 lines
2.5 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using Game;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UIElements;
public class GameUI : MonoBehaviour {
private UIDocument document;
public static GameUI Singleton;
private void Awake() {
Singleton = this;
document = GetComponent<UIDocument>();
PreparePlayerPanels();
}
private List<VisualElement> PlayerPanels => document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel")).ToList();
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, ActiveModification modification) {
VisualElement listElement = ModsList(playerPanel, modification.Properties.type);
VisualElement newElement = new() {
style = {
backgroundImage = Background.FromSprite(modification.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() {
float heightPercentage = Dimensions.Singleton.panelHeightPercentage;
PlayerPanel(Side.Top).style.height = PlayerPanel(Side.Bottom).style.height = Length.Percent(heightPercentage);
SetupButtons(Side.Top);
SetupButtons(Side.Bottom);
}
public delegate void ButtonDown(Side side, string direction);
public delegate void ButtonUp(Side side, string direction);
public ButtonDown buttonDown;
public ButtonUp buttonUp;
private void SetupButtons(Side side) {
Button btnLeft = PlayerPanel(side).Q<Button>("btn_left");
btnLeft.clickable.activators.Clear();
btnLeft.RegisterCallback<MouseDownEvent>(_ => buttonDown(side, "left"));
btnLeft.RegisterCallback<MouseUpEvent>(_ => buttonUp(side, "left"));
Button btnRight = PlayerPanel(side).Q<Button>("btn_right");
btnRight.clickable.activators.Clear();
btnRight.RegisterCallback<MouseDownEvent>(_ => buttonDown(side, "right"));
btnRight.RegisterCallback<MouseUpEvent>(_ => buttonUp(side, "right"));
}
}