-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPAPainter.cs
More file actions
145 lines (136 loc) · 8.16 KB
/
PAPainter.cs
File metadata and controls
145 lines (136 loc) · 8.16 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
using ColossalFramework;
using ColossalFramework.Math;
using ColossalFramework.UI;
using EManagersLib;
using MoveIt;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace PropAnarchy {
internal static class PAPainter {
private const string PAINTERBTN_NAME = @"PAPainterButton";
private const string COLORFIELD_NAME = @"PAPainterColorField";
private const string COLORPICKER_NAME = @"PAPaintercolorPicker";
internal delegate void ActionHandler(HashSet<Instance> selection);
internal delegate void CloneHandler(Dictionary<Instance, Instance> clonedOrigin);
internal static ActionHandler ActionAddHandler;
internal static CloneHandler ActionCloneHandler;
private static System.Action<UIColorPicker, Color> SetPickerColorField;
private static Color GetColor(float x, float y, float width, float height, Color hue) {
float num = x / width;
float num2 = y / height;
num = num < 0f ? 0f : (num > 1f ? 1f : num);
num2 = num2 < 0f ? 0f : (num2 > 1f ? 1f : num2);
Color result = Color.Lerp(Color.white, hue, num) * (1f - num2);
result.a = 1f;
return result;
}
private static void SetPickerColor(UIColorPicker picker, Color color) {
UISprite indicator = picker.m_Indicator;
UITextureSprite HSBField = picker.m_HSBField;
picker.hue = HSBColor.GetHue(color);
SetPickerColorField(picker, color);
HSBColor hSBColor = HSBColor.FromColor(color);
Vector2 a = new Vector2(hSBColor.s * HSBField.width, (1f - hSBColor.b) * HSBField.height);
indicator.relativePosition = a - indicator.size * 0.5f;
if (!(HSBField.renderMaterial is null)) {
HSBField.renderMaterial.color = picker.hue.gamma;
}
Vector2 vector = new Vector2(indicator.relativePosition.x + indicator.size.x * 0.5f, indicator.relativePosition.y + indicator.size.y * 0.5f);
SetPickerColorField(picker, GetColor(vector.x, vector.y, HSBField.width, HSBField.height, picker.hue));
}
internal static void Initialize(SimulationManager smInstance) {
Singleton<LoadingManager>.instance.WaitUntilEssentialScenesLoaded();
EPropInstance[] props = EPropManager.m_props.m_buffer;
SetPickerColorField = PAUtils.CreateSetter<UIColorPicker, Color>(typeof(UIColorPicker).GetField("m_Color", BindingFlags.Instance | BindingFlags.NonPublic));
if (UITemplateManager.Get<UIPanel>("LineTemplate") is UIPanel linePanel && linePanel.Find<UIColorField>("LineColor") is UIColorField field) {
UIColorField colorField = Object.Instantiate(field);
colorField.isVisible = true;
colorField.name = COLORFIELD_NAME;
UIColorPicker picker = Object.Instantiate(field.colorPicker);
picker.color = Color.white;
picker.name = COLORPICKER_NAME;
UIToolOptionPanel.AddMoreButtonCallback += (optionPanel, moreTools, mtpBackGround, mtpContainer) => {
optionPanel.AttachUIComponent(picker.gameObject);
UIPanel pickerPanel = picker.component as UIPanel;
pickerPanel.color = Color.white;
pickerPanel.backgroundSprite = @"InfoPanelBack";
pickerPanel.isVisible = false;
// re-adjust moretools panel
Vector2 containerSize = mtpContainer.size;
containerSize.y += 40f;
optionPanel.m_moreToolsPanel.size = containerSize;
mtpContainer.size = containerSize;
Vector2 backgroundSize = mtpBackGround.size;
backgroundSize.y += 40f;
mtpBackGround.size = backgroundSize;
optionPanel.m_moreToolsPanel.absolutePosition = moreTools.absolutePosition + new Vector3(0, 10 - optionPanel.m_moreToolsPanel.height);
UIMultiStateButton painterBtn = mtpContainer.AddUIComponent<UIMultiStateButton>();
painterBtn.name = PAINTERBTN_NAME;
painterBtn.cachedName = PAINTERBTN_NAME;
painterBtn.tooltip = PALocale.GetLocale(@"PainterTooltip");
painterBtn.playAudioEvents = true;
painterBtn.size = new Vector2(36f, 36f);
painterBtn.atlas = optionPanel.m_picker.atlas;
painterBtn.spritePadding = new RectOffset(2, 2, 2, 2);
painterBtn.backgroundSprites.AddState();
painterBtn.foregroundSprites.AddState();
painterBtn.backgroundSprites[0].normal = "OptionBase";
painterBtn.backgroundSprites[0].focused = "OptionBase";
painterBtn.backgroundSprites[0].hovered = "OptionBaseHovered";
painterBtn.backgroundSprites[0].pressed = "OptionBasePressed";
painterBtn.backgroundSprites[0].disabled = "OptionBaseDisabled";
painterBtn.foregroundSprites[0].normal = "EyeDropper";
painterBtn.backgroundSprites[1].normal = "OptionBaseFocused";
painterBtn.backgroundSprites[1].focused = "OptionBaseFocused";
painterBtn.backgroundSprites[1].hovered = "OptionBaseHovered";
painterBtn.backgroundSprites[1].pressed = "OptionBasePressed";
painterBtn.backgroundSprites[1].disabled = "OptionBaseDisabled";
painterBtn.foregroundSprites[1].normal = "EyeDropper";
painterBtn.activeStateIndex = 0;
Vector2 parentSize = painterBtn.parent.size;
painterBtn.parent.parent.size = new Vector2(parentSize.x, parentSize.y + 40f);
painterBtn.parent.size = painterBtn.parent.parent.size;
painterBtn.eventActiveStateIndexChanged += (_, index) => {
pickerPanel.isVisible = index == 1;
};
pickerPanel.absolutePosition = painterBtn.absolutePosition - new Vector3(pickerPanel.width, pickerPanel.height - 50f);
/* Finally attach all delegates */
IEnumerator ProcessColor(HashSet<Instance> selections) {
yield return new WaitForSeconds(0.1f);
if (!(selections is null) && selections.Count > 0) {
foreach (var selection in selections) {
uint propID = selection.id.GetProp32();
if (selection.isValid && !selection.id.IsEmpty && propID > 0) {
props[propID].m_color = picker.color;
}
}
}
}
picker.eventColorUpdated += (color) => smInstance.AddAction(ProcessColor(Action.selection));
ActionAddHandler = (selections) => {
smInstance.AddAction(() => {
foreach (var selection in selections) {
uint propID = selection.id.GetProp32();
if (selection.isValid && !selection.id.IsEmpty && propID > 0) {
SetPickerColor(picker, props[propID].m_color);
break;
}
}
});
};
ActionCloneHandler = (clonedOrigin) => {
smInstance.AddAction(() => {
if (clonedOrigin == null) return;
foreach (KeyValuePair<Instance, Instance> x in clonedOrigin) {
if (x.Key.id.Type != InstanceType.Prop) return;
props[x.Value.id.GetProp32()].m_color = props[x.Key.id.GetProp32()].m_color;
}
});
};
};
}
}
}
}