-
Notifications
You must be signed in to change notification settings - Fork 8
/
RuleGroup.cs
231 lines (199 loc) · 7.27 KB
/
RuleGroup.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System.Collections.Generic;
using System.Linq;
using ExileCore;
using ExileCore.Shared.Helpers;
using ImGuiNET;
using Newtonsoft.Json;
using ReAgent.SideEffects;
using ReAgent.State;
using SharpDX;
namespace ReAgent;
public class RuleGroup
{
private bool _expand;
private int _deleteIndex = -1;
public List<Rule> Rules = new();
public bool Enabled;
public bool EnabledInTown;
public bool EnabledInHideout;
public bool EnabledInPeacefulAreas;
public bool EnabledInMaps = true;
public string Name;
public RuleGroup(string name)
{
Name = name;
}
public void DrawSettings(RuleState state, ReAgentSettings settings)
{
using (settings.PluginSettings.ColorEnableToggles ? ImGuiHelpers.UseStyleColor(ImGuiCol.Text, Color.Lime.ToImguiVec4()) : null)
ImGui.Checkbox("Enable", ref Enabled);
if (settings.PluginSettings.KeepEnableTogglesOnASingleLine)
{
ImGui.SameLine();
}
using (settings.PluginSettings.ColorEnableToggles ? ImGuiHelpers.UseStyleColor(ImGuiCol.Text, Color.LightBlue.ToImguiVec4()) : null)
ImGui.Checkbox("Enable in town", ref EnabledInTown);
if (settings.PluginSettings.KeepEnableTogglesOnASingleLine)
{
ImGui.SameLine();
}
using (settings.PluginSettings.ColorEnableToggles ? ImGuiHelpers.UseStyleColor(ImGuiCol.Text, Color.Salmon.ToImguiVec4()) : null)
ImGui.Checkbox("Enable in hideout", ref EnabledInHideout);
if (settings.PluginSettings.KeepEnableTogglesOnASingleLine)
{
ImGui.SameLine();
}
using (settings.PluginSettings.ColorEnableToggles ? ImGuiHelpers.UseStyleColor(ImGuiCol.Text, Color.Orange.ToImguiVec4()) : null)
ImGui.Checkbox("Enable in maps", ref EnabledInMaps);
if (settings.PluginSettings.KeepEnableTogglesOnASingleLine)
{
ImGui.SameLine();
}
using (settings.PluginSettings.ColorEnableToggles ? ImGuiHelpers.UseStyleColor(ImGuiCol.Text, Color.LightGoldenrodYellow.ToImguiVec4()) : null)
ImGui.Checkbox("Enable in other peaceful areas", ref EnabledInPeacefulAreas);
ImGui.InputText("Name", ref Name, 20);
if (Rules.Any())
{
if (ImGui.Button($"{(_expand ? "Collapse" : "Expand")}###ExpandHideButton"))
{
_expand = !_expand;
}
ImGui.SameLine();
}
if (ImGui.Button("Export group"))
{
ImGui.SetClipboardText(DataExporter.ExportDataBase64(this, "reagent_group_v1", new JsonSerializerSettings()));
}
using var groupReg = state?.InternalState.SetCurrentGroup(this);
for (var i = 0; i < Rules.Count; i++)
{
ImGui.PushID($"Rule{i}");
if (i != 0)
{
ImGui.Separator();
}
var dropTargetStart = ImGui.GetCursorPos();
ImGui.PushStyleColor(ImGuiCol.Button, 0);
ImGui.Button("=");
ImGui.PopStyleColor();
ImGui.SameLine();
if (ImGui.BeginDragDropSource())
{
ImguiExt.SetDragDropPayload("RuleIndex", i);
Rules[i].Display(state, false);
ImGui.EndDragDropSource();
}
else if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Drag me");
}
if (ImGui.Button("Delete"))
{
if (ImGui.IsKeyDown(ImGuiKey.ModShift))
{
RemoveAt(i);
ImGui.PopID();
break;
}
_deleteIndex = i;
}
else if (ImGui.IsItemHovered())
{
ImGui.SetTooltip("Hold Shift to delete without confirmation");
}
ImGui.SameLine();
Rules[i].Display(state, _expand);
ImguiExt.DrawLargeTransparentSelectable("##DragTarget", dropTargetStart);
if (ImGui.BeginDragDropTarget())
{
var sourceId = ImguiExt.AcceptDragDropPayload<int>("RuleIndex");
if (sourceId != null)
{
if (ImGui.IsMouseReleased(ImGuiMouseButton.Left))
{
MoveRule(sourceId.Value, i);
}
}
ImGui.EndDragDropTarget();
}
ImGui.PopID();
}
if (ImGui.Button("Add New Rule"))
{
Rules.Add(new Rule("false", 1));
}
if (ImGui.TreeNode("State"))
{
var flags = state.InternalState.CurrentGroupState.Flags;
var timers = state.InternalState.CurrentGroupState.Timers;
var numbers = state.InternalState.CurrentGroupState.Numbers;
if ((flags.Any() || timers.Any() || numbers.Any()) && ImGui.Button("Clear"))
{
flags.Clear();
timers.Clear();
numbers.Clear();
}
ImGui.Text("Timers:");
foreach (var (name, timer) in timers)
{
ImGui.TextColored(timer.IsRunning ? Color.Green.ToImguiVec4() : Color.Yellow.ToImguiVec4(), $"{name}: {timer.Elapsed.TotalSeconds}");
}
ImGui.Text("Flags:");
foreach (var (name, flag) in flags)
{
ImGui.TextColored(flag ? Color.Green.ToImguiVec4() : Color.Yellow.ToImguiVec4(), name);
}
ImGui.Text("Numbers:");
foreach (var (name, value) in flags)
{
ImGui.TextColored(Color.Green.ToImguiVec4(), $"{name}: {value}");
}
ImGui.TreePop();
}
if (_deleteIndex != -1)
{
ImGui.OpenPopup("RuleDeleteConfirmation");
}
var deleteResult = ImguiExt.DrawDeleteConfirmationPopup("RuleDeleteConfirmation", _deleteIndex == -1 ? null : $"rule with index {_deleteIndex}");
if (deleteResult == true)
{
RemoveAt(_deleteIndex);
}
if (deleteResult != null)
{
_deleteIndex = -1;
}
}
public IEnumerable<SideEffectContainer> Evaluate(RuleState state)
{
if (Enabled &&
(state.IsInHideout, state.IsInTown, state.IsInPeacefulArea) switch
{
(true, _, _) => EnabledInHideout,
(_, true, _) => EnabledInTown,
(_, _, true) => EnabledInPeacefulAreas,
(false, false, false) => EnabledInMaps,
})
{
using var groupReg = state.InternalState.SetCurrentGroup(this);
foreach (var rule in Rules)
{
using var ruleReg = state.InternalState.CurrentGroupState.SetCurrentRule(rule);
foreach (var effect in rule.Evaluate(state))
{
yield return new SideEffectContainer(effect, this, rule);
}
}
}
}
private void RemoveAt(int index)
{
Rules.RemoveAt(index);
}
private void MoveRule(int sourceIndex, int targetIndex)
{
var movedItem = Rules[sourceIndex];
Rules.RemoveAt(sourceIndex);
Rules.Insert(targetIndex, movedItem);
}
}