mirror of
https://github.com/LazyDuchess/OpenTS2.git
synced 2025-01-23 08:41:47 -05:00
Initial button support
This commit is contained in:
parent
e560fb62da
commit
dccffa1da7
5 changed files with 140 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
|||
using OpenTS2.Content;
|
||||
using OpenTS2.Files;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
@ -89,12 +90,23 @@ namespace OpenTS2.UI {
|
|||
if (Application.isFocused)
|
||||
HardwareCursors.SetCurrentCursor((int)Cursor);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void Update()
|
||||
{
|
||||
#if !UNITY_EDITOR
|
||||
SetCursorInternal();
|
||||
}
|
||||
#endif
|
||||
if (UIManager.HeldComponent != null)
|
||||
{
|
||||
if (!UIManager.HeldComponent.isActiveAndEnabled)
|
||||
UIManager.HeldComponent = null;
|
||||
}
|
||||
if (Input.GetMouseButtonUp(0))
|
||||
{
|
||||
UIManager.HeldComponent = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,95 @@
|
|||
using System;
|
||||
using OpenTS2.Engine;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace OpenTS2.UI
|
||||
{
|
||||
public class UIButtonComponent : MonoBehaviour
|
||||
public class UIButtonComponent : UIComponent, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Triggers when this button is clicked. Boolean argument specifies whether it was a double click.
|
||||
/// </summary>
|
||||
public Action OnClick;
|
||||
public bool GreyedOut = false;
|
||||
private bool _hovering = false;
|
||||
private Texture2D[] _textures;
|
||||
public RawImage RawImageComponent => GetComponent<RawImage>();
|
||||
public void SetTexture(Texture2D texture)
|
||||
{
|
||||
_textures = UIUtils.SplitTextureHorizontalSequence(texture, texture.width / 4);
|
||||
UpdateTexture();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (UIManager.HeldComponent == this && GreyedOut)
|
||||
UIManager.HeldComponent = null;
|
||||
UpdateTexture();
|
||||
}
|
||||
|
||||
void UpdateTexture()
|
||||
{
|
||||
if (GreyedOut)
|
||||
RawImageComponent.texture = _textures[0];
|
||||
else
|
||||
{
|
||||
RawImageComponent.texture = _textures[1];
|
||||
if (UIManager.HeldComponent == this)
|
||||
{
|
||||
RawImageComponent.texture = _textures[2];
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (UIManager.HeldComponent != null)
|
||||
return;
|
||||
}
|
||||
if (_hovering)
|
||||
RawImageComponent.texture = _textures[3];
|
||||
}
|
||||
}
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (UIManager.HeldComponent == this)
|
||||
UIManager.HeldComponent = null;
|
||||
foreach (var texture in _textures)
|
||||
{
|
||||
texture.Free();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
_hovering = true;
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
_hovering = false;
|
||||
}
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
UIManager.HeldComponent = this;
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
if (UIManager.HeldComponent == this && _hovering)
|
||||
{
|
||||
Click();
|
||||
}
|
||||
}
|
||||
|
||||
void Click()
|
||||
{
|
||||
OnClick?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,25 +1,35 @@
|
|||
using System;
|
||||
using OpenTS2.Common;
|
||||
using OpenTS2.Content;
|
||||
using OpenTS2.Content.DBPF;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OpenTS2.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// UI Button.
|
||||
/// </summary>
|
||||
public class UIButtonElement : UIBMPElement
|
||||
public class UIButtonElement : UIElement
|
||||
{
|
||||
public ResourceKey Image = default;
|
||||
protected override Type UIComponentType => typeof(UIButtonComponent);
|
||||
public override void ParseProperties(UIProperties properties)
|
||||
{
|
||||
base.ParseProperties(properties);
|
||||
Image = properties.GetImageKeyProperty("image");
|
||||
}
|
||||
public override UIComponent Instantiate(Transform parent)
|
||||
{
|
||||
var uiComponent = base.Instantiate(parent) as UIBMPComponent;
|
||||
var rawImage = uiComponent.RawImageComponent;
|
||||
if (rawImage.texture != null)
|
||||
{
|
||||
rawImage.texture = UIUtils.SplitTextureHorizontalSequence(rawImage.texture as Texture2D, rawImage.texture.width / 4)[1];
|
||||
}
|
||||
var contentProvider = ContentProvider.Get();
|
||||
var imageAsset = contentProvider.GetAsset<TextureAsset>(Image);
|
||||
var uiComponent = base.Instantiate(parent) as UIButtonComponent;
|
||||
var rawImage = uiComponent.gameObject.AddComponent<RawImage>();
|
||||
uiComponent.SetTexture(imageAsset.Texture);
|
||||
return uiComponent;
|
||||
}
|
||||
}
|
||||
|
|
13
Assets/Scripts/OpenTS2/UI/UIManager.cs
Normal file
13
Assets/Scripts/OpenTS2/UI/UIManager.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OpenTS2.UI
|
||||
{
|
||||
public static class UIManager
|
||||
{
|
||||
public static UIComponent HeldComponent = null;
|
||||
}
|
||||
}
|
11
Assets/Scripts/OpenTS2/UI/UIManager.cs.meta
generated
Normal file
11
Assets/Scripts/OpenTS2/UI/UIManager.cs.meta
generated
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 792515d980a86cb4db8d82ada56497b3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in a new issue