forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageManager.cs
More file actions
306 lines (274 loc) · 10.5 KB
/
Copy pathImageManager.cs
File metadata and controls
306 lines (274 loc) · 10.5 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using FlashDevelop.Helpers;
using PluginCore.Helpers;
using PluginCore.Utilities;
namespace FlashDevelop.Managers
{
static class ImageManager
{
const int Size16 = 16;
const int Size32 = 32;
static readonly int Size;
static readonly int Padding;
static readonly Bitmap Source;
static readonly Dictionary<string, ImagePair> Cache;
static readonly List<ImagePair> AutoAdjusted;
/// <summary>
/// Static constructor
/// </summary>
static ImageManager()
{
double scale = ScaleHelper.GetScale();
Cache = new Dictionary<string, ImagePair>();
AutoAdjusted = new List<ImagePair>();
if (scale >= 1.5)
{
Size = Size32;
Padding = scale > 1.5 ? 2 : 1;
Source = new Bitmap(FileNameHelper.Images32);
}
else
{
Size = Size16;
Padding = 0;
Source = new Bitmap(FileNameHelper.Images);
}
}
/// <summary>
/// Composes an icon from image data.
/// </summary>
public static Image GetComposedBitmap(string data, bool autoAdjusted)
{
var c = Components.Parse(data);
string key = c.Key;
if (!Cache.ContainsKey(key))
{
var original = new Bitmap(Size, Size);
var srcRect = new Rectangle(0, 0, Size, Size);
var destRect = new Rectangle(Padding, Padding, Size - (Padding * 2), Size - (Padding * 2));
using (var graphics = Graphics.FromImage(original))
{
graphics.Clear(Color.Transparent);
if (c.Icon >= 0)
{
srcRect.X = (c.Icon % Size16) * Size;
srcRect.Y = (c.Icon / Size16) * Size;
graphics.DrawImage(Source, destRect, srcRect, GraphicsUnit.Pixel);
}
if (c.Bullet >= 0)
{
srcRect.X = (c.Bullet % Size16) * Size;
srcRect.Y = (c.Bullet / Size16) * Size;
destRect.X += (Size == Size32) ? c.X * 2 : c.X;
destRect.Y += (Size == Size32) ? c.Y * 2 : c.Y;
graphics.DrawImage(Source, destRect, srcRect, GraphicsUnit.Pixel);
}
}
original = ScaleHelper.Scale(original);
Cache[key] = new ImagePair(original);
}
if (autoAdjusted)
{
var imagePair = Cache[key];
return imagePair.Adjusted ?? AddAutoAdjustImage(imagePair);
}
return Cache[key].Original;
}
/// <summary>
/// Composes an icon from image data with the size of 16x16
/// </summary>
public static Image GetComposedBitmapSize16(string data, bool autoAdjusted)
{
if (Size == Size16) return GetComposedBitmap(data, autoAdjusted);
string key = Components.Parse(data, Size16).Key;
if (!Cache.ContainsKey(key))
{
var image32 = GetComposedBitmap(data, false);
int size = ScaleHelper.Scale(Size16);
var image16 = new Bitmap(size, size);
using (var graphics = Graphics.FromImage(image16))
{
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.DrawImage(image32, 0, 0, image16.Width, image16.Height);
}
Cache[key] = new ImagePair(image16);
}
if (autoAdjusted)
{
var imagePair = Cache[key];
return imagePair.Adjusted ?? AddAutoAdjustImage(imagePair);
}
return Cache[key].Original;
}
/// <summary>
/// Gets an adjusted copy of the specified image.
/// </summary>
public static Image SetImageAdjustment(Image original)
{
int saturation, brightness;
if (GetImageAdjustments(out saturation, out brightness))
{
return ImageKonverter.ImageAdjust(original, saturation, brightness);
}
return new Bitmap(original);
}
/// <summary>
/// Gets a copy of the image that changes color according to the theme.
/// </summary>
public static Image GetAutoAdjustedImage(Image image)
{
for (int i = 0, length = AutoAdjusted.Count; i < length; i++)
{
var imagePair = AutoAdjusted[i];
if (imagePair.Original == image)
{
return imagePair.Adjusted ?? (imagePair.Adjusted = SetImageAdjustment(imagePair.Original));
}
if (imagePair.Adjusted == null)
{
AutoAdjusted.RemoveAt(i--);
length--;
}
}
return AddAutoAdjustImage(new ImagePair(image));
}
/// <summary>
/// Adjust colors of all cached images.
/// </summary>
public static void AdjustAllImages()
{
int saturation, brightness;
GetImageAdjustments(out saturation, out brightness);
for (int i = 0, length = AutoAdjusted.Count; i < length; i++)
{
var imagePair = AutoAdjusted[i];
var adjusted = imagePair.Adjusted;
if (adjusted == null)
{
AutoAdjusted.RemoveAt(i--);
length--;
}
else ImageKonverter.ImageAdjust(imagePair.Original, adjusted, saturation, brightness);
}
}
/// <summary>
/// Adds a pair to the update list.
/// </summary>
static Image AddAutoAdjustImage(ImagePair pair)
{
if (!AutoAdjusted.Contains(pair)) AutoAdjusted.Add(pair);
return pair.Adjusted = SetImageAdjustment(pair.Original);
}
/// <summary>
/// Gets the appropriate color adjustment components.
/// </summary>
static bool GetImageAdjustments(out int saturation, out int brightness)
{
switch (Globals.MainForm.GetThemeValue("ImageManager.ImageSet"))
{
default:
case "Default": saturation = 0; brightness = 0; return false;
case "Bright": saturation = 20; brightness = 0; return true;
case "Dim": saturation = -5; brightness = -2; return true;
case "Dark": saturation = -5; brightness = -10; return true;
case "Darker": saturation = -20; brightness = -20; return true;
case "Black": saturation = -50; brightness = -25; return true;
}
}
/// <summary>
/// Contains information parsed from an image data string.
/// </summary>
struct Components
{
public int Size;
public int Icon;
public int Bullet;
public int X;
public int Y;
/// <summary>
/// Returns a string to use as a dictionary key.
/// </summary>
public string Key
{
get { return Size + "_" + Icon + "|" + Bullet + "|" + X + "|" + Y; }
}
/// <summary>
/// Parses an image data string with default size.
/// </summary>
public static Components Parse(string value)
{
return Parse(value, ImageManager.Size);
}
/// <summary>
/// Parses an image data string with the specified size.
/// </summary>
public static Components Parse(string value, int size)
{
Components c;
c.Size = size;
ProcessImageData(value, out c.Icon, out c.Bullet, out c.X, out c.Y);
return c;
}
/// <summary>
/// Processes data from "icon|bullet|x|y"
/// </summary>
static void ProcessImageData(string data, out int icon, out int bullet, out int x, out int y)
{
icon = bullet = -1;
x = y = 0;
if (string.IsNullOrEmpty(data)) return;
string[] args = data.Split('|');
if (args.Length == 0 || !int.TryParse(args[0], out icon)) return;
if (args.Length == 1 || !int.TryParse(args[1], out bullet)) return;
if (bullet < 0 || args.Length < 4) return;
int.TryParse(args[2], out x);
int.TryParse(args[3], out y);
}
}
/// <summary>
/// A pair of images used for tracking original and adjusted.
/// </summary>
class ImagePair
{
Image original;
WeakReference adjusted;
/// <summary>
/// Creates an instance of <see cref="ImagePair"/>.
/// </summary>
/// <param name="original"><see cref="Original"/></param>
public ImagePair(Image original) : this(original, null)
{
}
/// <summary>
/// Creates an instance of <see cref="ImagePair"/>.
/// </summary>
/// <param name="original"><see cref="Original"/></param>
/// <param name="adjusted"><see cref="Adjusted"/></param>
public ImagePair(Image original, Image adjusted)
{
this.original = original;
this.adjusted = new WeakReference(adjusted);
}
/// <summary>
/// The original image.
/// </summary>
public Image Original
{
get { return original; }
}
/// <summary>
/// The copy of <see cref="Original"/> that changes color according to the theme.
/// </summary>
public Image Adjusted
{
get { return adjusted.Target as Image; }
set { adjusted.Target = value; }
}
}
}
}