7
7
8
8
namespace FlashDevelop . Managers
9
9
{
10
- class ImageManager
10
+ static class ImageManager
11
11
{
12
- public static Int32 X ;
13
- public static Int32 Y ;
14
- public static Int32 Size ;
15
- public static Int32 Icon ;
16
- public static Int32 Bullet ;
17
- public static Int32 Padding ;
18
- public static Bitmap Source ;
19
- public static Dictionary < String , Bitmap > Cache ;
12
+ static readonly int Size ;
13
+ static readonly int Padding ;
14
+ static readonly Bitmap Source ;
15
+ static readonly Dictionary < string , ImagePair > Cache ;
16
+ static readonly List < ImagePair > AutoAdjusted ;
20
17
21
18
/// <summary>
22
19
/// Static constructor
23
20
/// </summary>
24
21
static ImageManager ( )
25
22
{
26
- Double scale = ScaleHelper . GetScale ( ) ;
27
- Cache = new Dictionary < String , Bitmap > ( ) ;
23
+ double scale = ScaleHelper . GetScale ( ) ;
24
+ Cache = new Dictionary < string , ImagePair > ( ) ;
25
+ AutoAdjusted = new List < ImagePair > ( ) ;
26
+
28
27
if ( scale >= 1.5 )
29
28
{
30
29
Size = 32 ;
@@ -37,80 +36,180 @@ static ImageManager()
37
36
Padding = 0 ;
38
37
Source = new Bitmap ( FileNameHelper . Images ) ;
39
38
}
40
- Source = ( Bitmap ) AdjustImage ( Source ) ;
41
39
}
42
40
43
41
/// <summary>
44
- /// Adjusts the image for different themes
42
+ /// Composes an icon from image data.
45
43
/// </summary>
46
- public static Image AdjustImage ( Image image )
44
+ public static Image GetComposedBitmap ( string data , bool autoAdjusted )
47
45
{
48
- String style = Globals . MainForm . GetThemeValue ( "ImageManager.ImageSet" ) ;
49
- if ( style == "Bright" ) return ImageKonverter . ImageAdjust ( image , 20 , 0 ) ;
50
- else if ( style == "Dim" ) return ImageKonverter . ImageAdjust ( image , - 5 , - 2 ) ;
51
- else if ( style == "Dark" ) return ImageKonverter . ImageAdjust ( image , - 5 , - 10 ) ;
52
- else if ( style == "Darker" ) return ImageKonverter . ImageAdjust ( image , - 20 , - 20 ) ;
53
- else if ( style == "Black" ) return ImageKonverter . ImageAdjust ( image , - 50 , - 25 ) ;
54
- else return image ;
46
+ if ( ! Cache . ContainsKey ( data ) )
47
+ {
48
+ int x , y , icon , bullet , rx , ry ;
49
+ var original = new Bitmap ( Size , Size ) ;
50
+ var graphics = Graphics . FromImage ( original ) ;
51
+ var destRect = new Rectangle ( Padding , Padding , Size - ( Padding * 2 ) , Size - ( Padding * 2 ) ) ;
52
+
53
+ ProcessImageData ( data , out x , out y , out icon , out bullet ) ;
54
+ graphics . Clear ( Color . Transparent ) ;
55
+
56
+ if ( icon >= 0 )
57
+ {
58
+ rx = ( icon % 16 ) * Size ;
59
+ ry = ( icon / 16 ) * Size ;
60
+ graphics . DrawImage ( Source , destRect , new Rectangle ( rx , ry , Size , Size ) , GraphicsUnit . Pixel ) ;
61
+ }
62
+ if ( bullet >= 0 )
63
+ {
64
+ rx = ( bullet % 16 ) * Size ;
65
+ ry = ( bullet / 16 ) * Size ;
66
+ destRect . X += ( Size == 32 ) ? x * 2 : x ;
67
+ destRect . Y += ( Size == 32 ) ? y * 2 : y ;
68
+ graphics . DrawImage ( Source , destRect , new Rectangle ( rx , ry , Size , Size ) , GraphicsUnit . Pixel ) ;
69
+ }
70
+
71
+ graphics . Dispose ( ) ;
72
+ original = ScaleHelper . Scale ( original ) ;
73
+ Cache [ data ] = new ImagePair ( original ) ;
74
+ }
75
+
76
+
77
+ if ( autoAdjusted )
78
+ {
79
+ var imagePair = Cache [ data ] ;
80
+ return imagePair . Adjusted ?? AddAutoAdjustImage ( imagePair ) ;
81
+ }
82
+ return Cache [ data ] . Original ;
55
83
}
56
84
57
85
/// <summary>
58
- /// Composes an icon from Image data
86
+ /// Gets an adjusted copy of the specified image.
59
87
/// </summary>
60
- public static Bitmap GetComposedBitmap ( String data )
88
+ public static Image SetImageAdjustment ( Image original )
61
89
{
62
- if ( Cache . ContainsKey ( data ) )
90
+ int saturation , brightness ;
91
+ if ( GetImageAdjustments ( out saturation , out brightness ) )
63
92
{
64
- return Cache [ data ] ;
93
+ return ImageKonverter . ImageAdjust ( original , saturation , brightness ) ;
65
94
}
66
- ProcessImageData ( data ) ;
67
- Bitmap composed = new Bitmap ( Size , Size ) ;
68
- Graphics destination = Graphics . FromImage ( composed ) ;
69
- destination . Clear ( Color . Transparent ) ;
70
- Int32 rx ; Int32 ry ;
71
- if ( Icon >= 0 )
72
- {
73
- rx = ( Icon % 16 ) * Size ;
74
- ry = ( Icon / 16 ) * Size ;
75
- destination . DrawImage ( Source , new Rectangle ( Padding , Padding , Size - ( Padding * 2 ) , Size - ( Padding * 2 ) ) , new Rectangle ( rx , ry , Size , Size ) , GraphicsUnit . Pixel ) ;
76
- }
77
- if ( Bullet >= 0 )
95
+ return new Bitmap ( original ) ;
96
+ }
97
+
98
+ /// <summary>
99
+ /// Gets a copy of the image that changes color according to the theme.
100
+ /// </summary>
101
+ public static Image GetAutoAdjustedImage ( Image image )
102
+ {
103
+ return AddAutoAdjustImage ( new ImagePair ( image ) ) ;
104
+ }
105
+
106
+ /// <summary>
107
+ /// Adjust colors of all cached images.
108
+ /// </summary>
109
+ public static void AdjustAllImages ( )
110
+ {
111
+ int saturation , brightness ;
112
+ GetImageAdjustments ( out saturation , out brightness ) ;
113
+
114
+ for ( int i = 0 , length = AutoAdjusted . Count ; i < length ; i ++ )
78
115
{
79
- rx = ( Bullet % 16 ) * Size ;
80
- ry = ( Bullet / 16 ) * Size ;
81
- X = ( Size == 32 ) ? X * 2 : X ;
82
- Y = ( Size == 32 ) ? Y * 2 : Y ;
83
- destination . DrawImage ( Source , new Rectangle ( X + Padding , Y + Padding , Size - ( Padding * 2 ) , Size - ( Padding * 2 ) ) , new Rectangle ( rx , ry , Size , Size ) , GraphicsUnit . Pixel ) ;
116
+ var imagePair = AutoAdjusted [ i ] ;
117
+ var adjusted = imagePair . Adjusted ;
118
+ if ( adjusted == null )
119
+ {
120
+ AutoAdjusted . RemoveAt ( i -- ) ;
121
+ length -- ;
122
+ }
123
+ else ImageKonverter . ImageAdjust ( imagePair . Original , adjusted , saturation , brightness ) ;
84
124
}
85
- composed = ScaleHelper . Scale ( composed ) ;
86
- Cache [ data ] = composed ;
87
- return composed ;
88
125
}
89
126
90
127
/// <summary>
91
128
/// Processes data from "icon|bullet|x|y" or just index
92
129
/// </summary>
93
- private static void ProcessImageData ( String data )
130
+ static void ProcessImageData ( string data , out int x , out int y , out int icon , out int bullet )
94
131
{
95
- X = Y = 0 ;
96
- Icon = Bullet = - 1 ;
132
+ x = y = 0 ;
133
+ icon = bullet = - 1 ;
97
134
if ( string . IsNullOrEmpty ( data ) ) return ;
98
- String [ ] par = data . Split ( '|' ) ;
99
- if ( par . Length > 0 )
135
+
136
+ string [ ] args = data . Split ( '|' ) ;
137
+ if ( args . Length == 0 || ! int . TryParse ( args [ 0 ] , out icon ) ) return ;
138
+ if ( args . Length == 1 || ! int . TryParse ( args [ 1 ] , out bullet ) ) return ;
139
+ if ( bullet < 0 || args . Length < 4 ) return ;
140
+ int . TryParse ( args [ 2 ] , out x ) ;
141
+ int . TryParse ( args [ 3 ] , out y ) ;
142
+ }
143
+
144
+ /// <summary>
145
+ /// Adds a pair to the update list.
146
+ /// </summary>
147
+ static Image AddAutoAdjustImage ( ImagePair pair )
148
+ {
149
+ AutoAdjusted . Add ( pair ) ;
150
+ return pair . Adjusted = SetImageAdjustment ( pair . Original ) ;
151
+ }
152
+
153
+ /// <summary>
154
+ /// Gets the appropriate color adjustment components.
155
+ /// </summary>
156
+ static bool GetImageAdjustments ( out int saturation , out int brightness )
157
+ {
158
+ switch ( Globals . MainForm . GetThemeValue ( "ImageManager.ImageSet" ) )
100
159
{
101
- Int32 . TryParse ( par [ 0 ] , out Icon ) ;
102
- if ( par . Length > 1 )
103
- {
104
- Int32 . TryParse ( par [ 1 ] , out Bullet ) ;
105
- if ( Bullet >= 0 && par . Length == 4 )
106
- {
107
- Int32 . TryParse ( par [ 2 ] , out X ) ;
108
- Int32 . TryParse ( par [ 3 ] , out Y ) ;
109
- }
110
- }
160
+ case "Bright" : saturation = 20 ; brightness = 0 ; return true ;
161
+ case "Dim" : saturation = - 5 ; brightness = - 2 ; return true ;
162
+ case "Dark" : saturation = - 5 ; brightness = - 10 ; return true ;
163
+ case "Darker" : saturation = - 20 ; brightness = - 20 ; return true ;
164
+ case "Black" : saturation = - 50 ; brightness = - 25 ; return true ;
165
+ default : saturation = 0 ; brightness = 0 ; return false ;
111
166
}
112
167
}
113
168
169
+ /// <summary>
170
+ /// A pair of images used for tracking original and adjusted.
171
+ /// </summary>
172
+ class ImagePair
173
+ {
174
+ Image original ;
175
+ WeakReference adjusted ;
176
+
177
+ /// <summary>
178
+ /// The original image.
179
+ /// </summary>
180
+ public Image Original
181
+ {
182
+ get { return original ; }
183
+ }
184
+
185
+ /// <summary>
186
+ /// The copy of <see cref="Original"/> that changes color according to the theme.
187
+ /// </summary>
188
+ public Image Adjusted
189
+ {
190
+ get { return adjusted . Target as Image ; }
191
+ set { adjusted . Target = value ; }
192
+ }
193
+
194
+ /// <summary>
195
+ /// Creates an instance of <see cref="ImagePair"/>.
196
+ /// </summary>
197
+ /// <param name="original"><see cref="Original"/></param>
198
+ public ImagePair ( Image original ) : this ( original , null )
199
+ {
200
+ }
201
+
202
+ /// <summary>
203
+ /// Creates an instance of <see cref="ImagePair"/>.
204
+ /// </summary>
205
+ /// <param name="original"><see cref="Original"/></param>
206
+ /// <param name="adjusted"><see cref="Adjusted"/></param>
207
+ public ImagePair ( Image original , Image adjusted )
208
+ {
209
+ this . original = original ;
210
+ this . adjusted = new WeakReference ( adjusted ) ;
211
+ }
212
+ }
114
213
}
115
214
116
215
}
0 commit comments