Newer
Older
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
[RequireComponent(typeof(FactWrapper))]
public class FactFavorisation : MonoBehaviour, IPointerClickHandler
{
#region InspectorVariables
[Header("Prefabs")]
[SerializeField] private GameObject favoriteDisplayPrefab;
#endregion InspectorVariables
#region Static Variables
private static readonly UnityEvent<Fact, bool> ChangeFavoriteEvent = new();
private static readonly List<Fact> favorites = new();
#endregion Static Variables
#region Variables
private GameObject favoriteDisplay;
private Fact fact;
#endregion Variables
#region Properties
private bool isFavorite = false;
public bool IsFavorite
{
get { return isFavorite; }
set { ChangeFavoriteEvent.Invoke(fact, value); }
}
#endregion Properties
#region UnityMethods
public void OnPointerClick(PointerEventData eventData)
{
// TODO: add support for other input systems
if (eventData.button == PointerEventData.InputButton.Middle)
{
// write to property to invoke event
IsFavorite = !IsFavorite;
// update favorites list
if (isFavorite)
favorites.Add(fact);
else
favorites.Remove(fact);
}
}
private void Start()
{
fact = transform.GetComponent<FactWrapper>().fact;
ChangeFavoriteEvent.AddListener(OnFavoriteChange);
// check if fact is currenty a favorite
if (favorites.Contains(fact))
{
isFavorite = true;
UpdateDisplay();
}
}
#endregion UnityMethods
#region Implementation
private void OnFavoriteChange(Fact changedFact, bool isFavourite)
{
if (fact == changedFact)
{
this.isFavorite = isFavourite;
UpdateDisplay();
}
}
private void UpdateDisplay()
{
if (!isFavorite)
Destroy(favoriteDisplay);
else
if (!favoriteDisplay)
favoriteDisplay = Instantiate(favoriteDisplayPrefab, transform);
}
#endregion Implementation
}