-
Notifications
You must be signed in to change notification settings - Fork 1
/
UIElement.cs
69 lines (53 loc) · 2.63 KB
/
UIElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//EVERY button should have this so the style is consistent
public class UIElement : TooltipableGameObject, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler {
AudioSource audioSource;
public string tooltipMessage = "";
public override string TooltipMessage => tooltipMessage;
readonly Vector3 transformScaleChange = new(0.2f, 0.2f, 0.2f);
Vector3 originalScale;
public static Actions ActionBeforeEnteringSettings { get; private set; }
public bool playSounds;
public bool isCancelButton;
public bool ExpandOnHover;
public bool SetActionToNothingOnEnter = true;
private static AudioClip hoverSound, clickSound, clickSoundCancel;
public void Start() {
if (!TryGetComponent(out audioSource)) audioSource = gameObject.AddComponent<AudioSource>();
originalScale = GetComponent<RectTransform>().localScale;
if (hoverSound == null) {
hoverSound = Resources.Load<AudioClip>("SoundEffects/ButtonHover");
clickSoundCancel = Resources.Load<AudioClip>("SoundEffects/BackButtonSound");
clickSound = Resources.Load<AudioClip>("SoundEffects/ClickButton");
}
}
public new void OnPointerEnter(PointerEventData eventData) {
base.OnPointerEnter(eventData);
if (SetActionToNothingOnEnter) {
if (!MoveablePanel.panelWithNoActionRequirementIsOpen) {
ActionBeforeEnteringSettings = BuildingController.CurrentAction;
BuildingController.SetCurrentAction(Actions.DO_NOTHING);
}
}
if (ExpandOnHover) StartCoroutine(ObjectMover.ScaleUIObjectInConstantTime(transform, GetComponent<RectTransform>().localScale, originalScale + transformScaleChange, 0.1f));
if (!playSounds) return;
audioSource.clip = hoverSound;
audioSource.Play();
}
public void OnPointerExit(PointerEventData eventData) {
if (SetActionToNothingOnEnter && BuildingController.CurrentAction == Actions.DO_NOTHING) {
if (!MoveablePanel.panelWithNoActionRequirementIsOpen) BuildingController.SetCurrentAction(ActionBeforeEnteringSettings);
}
if (ExpandOnHover) StartCoroutine(ObjectMover.ScaleUIObjectInConstantTime(transform, GetComponent<RectTransform>().localScale, originalScale, 0.1f));
}
public void OnPointerClick(PointerEventData eventData) {
if (playSounds) {
audioSource.clip = isCancelButton ? clickSoundCancel : clickSound;
audioSource.Play();
}
}
}