-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaidSelectPanelManager.cs
120 lines (102 loc) · 3.62 KB
/
MaidSelectPanelManager.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
namespace COM3D2.UndressUtil.Plugin
{
public class MaidSelectPanelManager: MonoBehaviour
{
GameObject maidGrid;
Dictionary<Maid, GameObject> maidGameObjectLookup = new Dictionary<Maid, GameObject>();
MaidTracker maidTracker;
public MaidSelectEvent MaidSelected { get; private set; } = new MaidSelectEvent();
public Maid SelectedMaid { get; private set; }
public class MaidSelectEvent : UnityEvent<Maid> { };
public void Start()
{
maidTracker = this.gameObject.GetComponentInParent<MaidTracker>();
maidTracker.MaidActivated.AddListener(this.OnMaidActivated);
maidTracker.MaidDeactivated.AddListener(this.OnMaidDeactivated);
maidGrid = gameObject;
foreach (var maid in maidTracker.GetActiveMaids())
{
OnMaidActivated(maid);
}
}
public void OnDestroy()
{
maidTracker.MaidActivated.RemoveListener(this.OnMaidActivated);
maidTracker.MaidDeactivated.RemoveListener(this.OnMaidDeactivated);
}
public void RepositionIcons()
{
StartCoroutine(RepositionIconsCoroutine());
}
IEnumerator RepositionIconsCoroutine()
{
yield return null;
maidGrid.GetComponent<UIGrid>().Reposition();
}
private void OnMaidActivated(Maid maid)
{
if (maidGameObjectLookup.ContainsKey(maid))
{
maidGameObjectLookup[maid].SetActive(true);
return;
}
var gameObject = Prefabs.CreateMaidIcon(this.maidGrid);
var maidIconComponent = gameObject.GetComponentInChildren<MaidIcon>();
Assert.IsNotNull(maidIconComponent, "Could not get maidIconComponent for [{0}]", gameObject);
maidIconComponent.Setup(maid);
maidIconComponent.Click.AddListener(this.OnMaidIconClick);
maidGameObjectLookup[maid] = gameObject;
if (this.SelectedMaid == null)
{
OnMaidIconClick(maid);
}
RepositionIcons();
}
private void OnMaidDeactivated(Maid maid)
{
if (maidGameObjectLookup.ContainsKey(maid))
{
var obj = maidGameObjectLookup[maid];
var component = obj.GetComponentInChildren<MaidIcon>();
component.SetSelected(false);
obj.SetActive(false);
Destroy(obj);
maidGameObjectLookup.Remove(maid);
if (this.SelectedMaid == maid)
{
SelectNextActiveMaid();
}
}
RepositionIcons();
}
private void SelectNextActiveMaid()
{
this.SelectedMaid = maidTracker.GetActiveMaids().FirstOrDefault();
if (SelectedMaid != null)
{
OnMaidIconClick(SelectedMaid);
}
else
{
MaidSelected.Invoke(SelectedMaid);
}
}
private void OnMaidIconClick(Maid maid)
{
this.SelectedMaid = maid;
foreach (var component in gameObject.GetComponentsInChildren<MaidIcon>())
{
component.SetSelected(component.maid == maid);
}
Log.LogVerbose("Maid selected event [{0}]", maid);
MaidSelected.Invoke(maid);
}
}
}