Skip to content

Commit b5c55cb

Browse files
author
JasonNumberThirteen
committed
Add detecting hovering UI panels to prevent triggering events by overlapping
1 parent 6d3acbd commit b5c55cb

16 files changed

Lines changed: 366 additions & 29 deletions

Assets/Project/Scenes/AStarVisualiserScene.unity

Lines changed: 110 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Project/Scripts/Enums/VisualiserEventType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ public enum VisualiserEventType
22
{
33
MapTileHoverStateWasChanged,
44
MapTileSelectionStateWasChanged,
5-
MapTileWeightWasChanged
5+
MapTileWeightWasChanged,
6+
PanelUIHoverStateWasChanged
67
}

Assets/Project/Scripts/Events/Bool Events.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Project/Scripts/Events/BoolVisualiserEvent.cs renamed to Assets/Project/Scripts/Events/Bool Events/BoolVisualiserEvent.cs

File renamed without changes.

Assets/Project/Scripts/Events/BoolVisualiserEvent.cs.meta renamed to Assets/Project/Scripts/Events/Bool Events/BoolVisualiserEvent.cs.meta

File renamed without changes.

Assets/Project/Scripts/Events/MapTileBoolVisualiserEvent.cs renamed to Assets/Project/Scripts/Events/Bool Events/MapTileBoolVisualiserEvent.cs

File renamed without changes.

Assets/Project/Scripts/Events/MapTileBoolVisualiserEvent.cs.meta renamed to Assets/Project/Scripts/Events/Bool Events/MapTileBoolVisualiserEvent.cs.meta

File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class PanelUIBoolVisualiserEvent : BoolVisualiserEvent
2+
{
3+
private readonly PanelUI panelUI;
4+
5+
public PanelUIBoolVisualiserEvent(PanelUI panelUI, VisualiserEventType visualiserEventType, bool stateIsEnabled) : base(visualiserEventType, stateIsEnabled)
6+
{
7+
this.panelUI = panelUI;
8+
}
9+
10+
public PanelUI GetPanelUI() => panelUI;
11+
}

Assets/Project/Scripts/Events/Bool Events/PanelUIBoolVisualiserEvent.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Project/Scripts/Game Objects/HoveredMapTileIndicator.cs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
using UnityEngine;
22

3-
public class HoveredMapTileIndicator : MonoBehaviour, IMapEditingElement
3+
public class HoveredMapTileIndicator : MonoBehaviour, IPrimaryWindowElement, IMapEditingElement
44
{
55
[SerializeField, Min(0f)] private float animationTransitionDuration = 0.5f;
66

77
private static readonly float ANIMATION_TRANSITION_MINIMUM_SCALE = 0.8f;
88

9+
private MapTile hoveredMapTile;
10+
private MapTile selectedMapTile;
911
private MapTile currentMapTile;
10-
private MapTile previousMapTile;
12+
private bool indicatorWasHidden;
13+
private bool panelUIHoverWasDetected;
1114
private HoveredMapTileManager hoveredMapTileManager;
1215
private SelectedMapTileManager selectedMapTileManager;
16+
private PanelUIHoverDetectionManager panelUIHoverDetectionManager;
17+
18+
public void SetPrimaryWindowElementActive(bool active)
19+
{
20+
indicatorWasHidden = !active;
21+
panelUIHoverWasDetected = false;
22+
23+
UpdateActiveState();
24+
}
1325

1426
public void SetMapEditingElementActive(bool active)
1527
{
16-
gameObject.SetActive(active);
28+
indicatorWasHidden = !active;
29+
30+
UpdateActiveState();
1731
}
1832

1933
private void Awake()
2034
{
2135
hoveredMapTileManager = FindFirstObjectByType<HoveredMapTileManager>();
2236
selectedMapTileManager = FindFirstObjectByType<SelectedMapTileManager>();
37+
panelUIHoverDetectionManager = FindFirstObjectByType<PanelUIHoverDetectionManager>();
2338

2439
RegisterToListeners(true);
2540
}
@@ -50,6 +65,11 @@ private void RegisterToListeners(bool register)
5065
{
5166
selectedMapTileManager.selectedMapTileWasChangedEvent.AddListener(OnSelectedMapTileWasChanged);
5267
}
68+
69+
if(panelUIHoverDetectionManager != null)
70+
{
71+
panelUIHoverDetectionManager.panelUIHoverDetectionStateWasChangedEvent.AddListener(OnPanelUIHoverDetectionStateWasChanged);
72+
}
5373
}
5474
else
5575
{
@@ -62,6 +82,11 @@ private void RegisterToListeners(bool register)
6282
{
6383
selectedMapTileManager.selectedMapTileWasChangedEvent.RemoveListener(OnSelectedMapTileWasChanged);
6484
}
85+
86+
if(panelUIHoverDetectionManager != null)
87+
{
88+
panelUIHoverDetectionManager.panelUIHoverDetectionStateWasChangedEvent.RemoveListener(OnPanelUIHoverDetectionStateWasChanged);
89+
}
6590
}
6691
}
6792

@@ -72,30 +97,36 @@ private void Start()
7297

7398
private void OnHoveredMapTileWasChanged(MapTile mapTile)
7499
{
75-
if(previousMapTile != null)
100+
hoveredMapTile = mapTile;
101+
102+
if(selectedMapTile != null)
76103
{
77104
return;
78105
}
106+
107+
currentMapTile = hoveredMapTile;
79108

80-
currentMapTile = mapTile;
81-
82-
gameObject.SetActive(currentMapTile != null);
109+
UpdateActiveState();
83110
}
84111

85112
private void OnSelectedMapTileWasChanged(MapTile mapTile)
86113
{
87-
if(mapTile != null)
88-
{
89-
previousMapTile = mapTile;
90-
currentMapTile = null;
91-
}
92-
else
93-
{
94-
currentMapTile = previousMapTile;
95-
previousMapTile = null;
96-
}
114+
selectedMapTile = mapTile;
115+
currentMapTile = selectedMapTile == null ? hoveredMapTile : null;
97116

98-
gameObject.SetActive(currentMapTile != null);
117+
UpdateActiveState();
118+
}
119+
120+
private void OnPanelUIHoverDetectionStateWasChanged(bool detected)
121+
{
122+
panelUIHoverWasDetected = detected;
123+
124+
UpdateActiveState();
125+
}
126+
127+
private void UpdateActiveState()
128+
{
129+
gameObject.SetActive(!indicatorWasHidden && !panelUIHoverWasDetected && currentMapTile != null);
99130
}
100131

101132
private void Update()

0 commit comments

Comments
 (0)