-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayerToolbar.cs
More file actions
187 lines (160 loc) · 6.1 KB
/
PlayerToolbar.cs
File metadata and controls
187 lines (160 loc) · 6.1 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using avaness.ToolSwitcherPlugin.Definitions;
using avaness.ToolSwitcherPlugin.Slot;
using Sandbox.Game.Screens.Helpers;
using System;
using VRage.Game;
namespace avaness.ToolSwitcherPlugin
{
public class PlayerToolbar
{
private readonly MyToolbar toolbar;
private readonly ToolDefinitions defs;
private int? activeToolPage;
private readonly ToolSlot[] toolPages = new ToolSlot[9];
public bool NeedsTool { get; private set; }
public PlayerToolbar(MyToolbar toolbar, ToolDefinitions defs)
{
this.toolbar = toolbar;
this.defs = defs;
ToolSlot firstTool = null;
for(int i = 80; i >= 0; i--)
{
ToolSlot slot = CreateSlot(toolbar[i], i);
if (slot != null)
{
if (firstTool == null)
firstTool = slot;
ToolSlot current = toolPages[slot.Page];
if (current != null)
{
current.Clear(toolbar);
if (firstTool == current)
firstTool = slot;
}
toolPages[slot.Page] = slot;
}
}
ToolSlot initialTool = toolPages[toolbar.CurrentPage];
if (initialTool != null)
activeToolPage = toolbar.CurrentPage;
if (initialTool == null)
{
if (firstTool == null)
return;
initialTool = firstTool;
}
CopyFrom(initialTool);
toolbar.SlotActivated += Toolbar_SlotActivated;
toolbar.Unselected += Toolbar_Unselected;
//toolbar.ItemChanged += Toolbar_ItemChanged; Handled by CopyFrom
toolbar.ItemUpdated += Toolbar_ItemUpdated;
}
public void Unload()
{
toolbar.SlotActivated -= Toolbar_SlotActivated;
toolbar.Unselected -= Toolbar_Unselected;
toolbar.ItemChanged -= Toolbar_ItemChanged;
toolbar.ItemUpdated -= Toolbar_ItemUpdated;
}
private void Toolbar_ItemUpdated(MyToolbar toolbar, MyToolbar.IndexArgs index, MyToolbarItem.ChangeInfo change)
{
if(change == MyToolbarItem.ChangeInfo.Enabled)
{
MyToolbarItemDefinition itemDef = toolbar[index.ItemIndex] as MyToolbarItemDefinition;
if (itemDef?.Definition != null && !itemDef.Enabled && defs.ContainsPhysical(itemDef.Definition.Id))
NeedsTool = true;
}
}
private void Toolbar_ItemChanged(MyToolbar toolbar, MyToolbar.IndexArgs index, bool arg3)
{
ToolSlot slot = CreateSlot(toolbar.GetItemAtIndex(index.ItemIndex), index.ItemIndex);
if (slot != null)
{
ToolSlot current = toolPages[slot.Page];
if (current != null && current.Slot != slot.Slot)
current.Clear(toolbar);
toolPages[slot.Page] = slot;
CopyFrom(slot);
}
}
private void CopyFrom(ToolSlot slot)
{
toolbar.ItemChanged -= Toolbar_ItemChanged;
foreach(ToolSlot s in toolPages)
{
if(s != null && s != slot)
s.CopyFrom(toolbar, slot);
}
NeedsTool = false;
toolbar.ItemChanged += Toolbar_ItemChanged;
}
private void Toolbar_SlotActivated(MyToolbar toolbar, MyToolbar.SlotArgs slot, bool arg3)
{
if (!slot.SlotNumber.HasValue)
return;
ToolSlot newSlot = CreateSlot(toolbar.GetSlotItem(slot.SlotNumber.Value), toolbar.CurrentPage, slot.SlotNumber.Value);
if (newSlot != null)
{
ToolSlot current = toolPages[newSlot.Page];
if (current == null || current.Slot != newSlot.Slot)
toolPages[newSlot.Page] = newSlot;
activeToolPage = newSlot.Page;
NeedsTool = false;
}
}
private void Toolbar_Unselected(MyToolbar toolbar)
{
activeToolPage = null;
}
public ToolSlot GetHandSlot()
{
if (!activeToolPage.HasValue)
return null;
int currentPage = toolbar.CurrentPage;
ToolSlot currentSlot = toolPages[currentPage];
if (currentSlot != null)
return currentSlot;
return toolPages[activeToolPage.Value];
}
public ToolSlot GetToolSlot()
{
ToolSlot pageSlot = toolPages[toolbar.CurrentPage];
if (pageSlot != null)
return pageSlot;
if (activeToolPage.HasValue)
return toolPages[activeToolPage.Value];
foreach (ToolSlot s in toolPages)
{
if (s != null)
return s;
}
return null;
}
private ToolSlot CreateSlot(MyToolbarItem item, int page, int slot)
{
MyToolbarItemDefinition itemDef = item as MyToolbarItemDefinition;
if (itemDef?.Definition != null && defs.ContainsPhysical(itemDef.Definition.Id))
return new ToolSlot(page, slot, itemDef);
return null;
}
private ToolSlot CreateSlot(MyToolbarItem item, int index)
{
MyToolbarItemDefinition itemDef = item as MyToolbarItemDefinition;
if (itemDef?.Definition != null && defs.ContainsPhysical(itemDef.Definition.Id))
return new ToolSlot(index, itemDef);
return null;
}
public void SetTool(MyDefinitionId physicalId)
{
ToolSlot refer = GetToolSlot();
if (refer == null)
return;
toolbar.Unselect(false);
refer.SetTo(toolbar, physicalId);
CopyFrom(refer);
refer.Activate(toolbar);
activeToolPage = refer.Page;
NeedsTool = false;
}
}
}