-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropitizeButton.cs
More file actions
166 lines (132 loc) · 6.33 KB
/
PropitizeButton.cs
File metadata and controls
166 lines (132 loc) · 6.33 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
using System.IO;
using System.Reflection;
using ColossalFramework.UI;
using UnityEngine;
using MoveIt;
using UIUtils = SamsamTS.UIUtils;
namespace Propitize
{
public class PropitizeButton : UIPanel
{
static public UIButton m_propitize_button;
// Copied over from Move it
static public UIButton CreateSubButton(UIToolOptionPanel parent, string name, string tooltip, string fgSprite)
{
m_propitize_button = parent.m_viewOptions.AddUIComponent<UIButton>();
m_propitize_button.name = name;
m_propitize_button.atlas = GetIconsAtlas();
m_propitize_button.tooltip = tooltip;
m_propitize_button.playAudioEvents = true;
m_propitize_button.size = new Vector2(36, 36);
m_propitize_button.normalBgSprite = "OptionBase";
m_propitize_button.hoveredBgSprite = "OptionBaseHovered";
m_propitize_button.pressedBgSprite = "OptionBasePressed";
m_propitize_button.disabledBgSprite = "OptionBaseDisabled";
m_propitize_button.normalFgSprite = fgSprite;
m_propitize_button.relativePosition = new Vector3(4f, 112f);
m_propitize_button.eventClicked += (c, p) =>
{
PropitizeTool.instance.StartConvertAction();
};
parent.m_viewOptions.height += 36;
parent.m_viewOptions.absolutePosition += new Vector3(0, -36);
return m_propitize_button;
}
static internal UITextureAtlas GetIconsAtlas()
{
UITextureAtlas atlas = UIUtils.GetAtlas("Ingame");
Texture2D[] textures =
{
atlas["OptionBase"].texture,
atlas["OptionBaseHovered"].texture,
atlas["OptionBasePressed"].texture,
atlas["OptionBaseDisabled"].texture,
atlas["OptionBaseFocused"].texture,
atlas["OptionsDropboxListbox"].texture,
atlas["OptionsDropboxListboxHovered"].texture,
atlas["OptionsDropboxListboxFocused"].texture
};
string[] spriteNames = new string[]
{
"Propitize"
};
UITextureAtlas loadedAtlas = CreateTextureAtlas("Propitize", spriteNames, "Propitize.icon.");
AddTexturesInAtlas(loadedAtlas, textures);
return loadedAtlas;
}
private static UITextureAtlas CreateTextureAtlas(string atlasName, string[] spriteNames, string assemblyPath)
{
int maxSize = 1024;
Texture2D texture2D = new Texture2D(maxSize, maxSize, TextureFormat.ARGB32, false);
Texture2D[] textures = new Texture2D[spriteNames.Length];
Rect[] regions;// = new Rect[spriteNames.Length];
for (int i = 0; i < spriteNames.Length; i++)
textures[i] = LoadTextureFromAssembly(assemblyPath + spriteNames[i] + ".png");
regions = texture2D.PackTextures(textures, 4, maxSize);
UITextureAtlas textureAtlas = ScriptableObject.CreateInstance<UITextureAtlas>();
Material material = Object.Instantiate(UIView.GetAView().defaultAtlas.material);
material.mainTexture = texture2D;
textureAtlas.material = material;
textureAtlas.name = atlasName;
for (int i = 0; i < spriteNames.Length; i++)
{
UITextureAtlas.SpriteInfo item = new UITextureAtlas.SpriteInfo
{
name = spriteNames[i],
texture = textures[i],
region = regions[i],
};
textureAtlas.AddSprite(item);
}
return textureAtlas;
}
private static void AddTexturesInAtlas(UITextureAtlas atlas, Texture2D[] newTextures, bool locked = false)
{
Texture2D[] textures = new Texture2D[atlas.count + newTextures.Length];
for (int i = 0; i < atlas.count; i++)
{
Texture2D texture2D = atlas.sprites[i].texture;
if (locked)
{
// Locked textures workaround
RenderTexture renderTexture = RenderTexture.GetTemporary(texture2D.width, texture2D.height, 0);
Graphics.Blit(texture2D, renderTexture);
RenderTexture active = RenderTexture.active;
texture2D = new Texture2D(renderTexture.width, renderTexture.height);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0f, 0f, (float)renderTexture.width, (float)renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = active;
RenderTexture.ReleaseTemporary(renderTexture);
}
textures[i] = texture2D;
textures[i].name = atlas.sprites[i].name;
}
for (int i = 0; i < newTextures.Length; i++)
textures[atlas.count + i] = newTextures[i];
Rect[] regions = atlas.texture.PackTextures(textures, atlas.padding, 4096, false);
atlas.sprites.Clear();
for (int i = 0; i < textures.Length; i++)
{
UITextureAtlas.SpriteInfo spriteInfo = atlas[textures[i].name];
atlas.sprites.Add(new UITextureAtlas.SpriteInfo
{
texture = textures[i],
name = textures[i].name,
border = (spriteInfo != null) ? spriteInfo.border : new RectOffset(),
region = regions[i]
});
}
atlas.RebuildIndexes();
}
private static Texture2D LoadTextureFromAssembly(string path)
{
Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
byte[] array = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(array, 0, array.Length);
Texture2D texture2D = new Texture2D(2, 2, TextureFormat.ARGB32, false);
texture2D.LoadImage(array);
return texture2D;
}
}
}