-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecoratorDefaultBitmapBase.cs
350 lines (284 loc) · 11.9 KB
/
DecoratorDefaultBitmapBase.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using CSharpDecorator.Framework;
using GME.MGA;
using GME.MGA.Meta;
using GME.Util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using ISIS.GME.Dsml.SignLanguage;
namespace GenericDecorator
{
enum ModifierType { NONE, INSTANCE, TYPE, SUBTYPE };
class DefaultBitmapDecorator : DecoratorUiBase
{
protected string GetStreetSignIcon(IMgaFCO ss)
{
string icon_map = ss.StrAttrByName["StreetSignIconMap"].Trim();
string street_sign = ss.StrAttrByName["StreetSignTypes"].Trim();
// expect list of form <key string> : <value string>
var splitResult = icon_map.Split(new string[] { System.Environment.NewLine, "\n" },
System.StringSplitOptions.RemoveEmptyEntries);
foreach (string line in splitResult)
{
string[] halves = line.Split(new Char[] { ':' }, 2);
if (halves[0].Trim() == street_sign)
return halves[1].Trim();
}
return ss.RegistryValue["icon"]; // default icon
}
protected string GetFloodSignIcon(IMgaFCO fs)
{
string icon_map = fs.StrAttrByName["FloodIconMap"].Trim();
double flood_level = fs.FloatAttrByName["FloodLevel"];
// expect list of form <key string> : <value string>
var splitResult = icon_map.Split(new string[] { System.Environment.NewLine, "\n" },
System.StringSplitOptions.RemoveEmptyEntries);
string iconStr = "";
foreach (string line in splitResult.Reverse()) // go in decreasing order
{
var halves = line.Split(new Char[] { ':' }, 2);
double level = Convert.ToDouble(halves[0].Trim());
iconStr = halves[1].Trim();
if (flood_level >= level )
return iconStr;
}
return iconStr; // if flood_level is smaller than them all
}
protected string GetParkingSignIcon(IMgaFCO ps)
{
string icon_map = ps.StrAttrByName["ParkingIconMap"].Trim();
bool parking_val = ps.BoolAttrByName["Parking"];
// expect list of form <key string> : <value string>
var splitResult = icon_map.Split(new string[] { System.Environment.NewLine, "\n" },
System.StringSplitOptions.RemoveEmptyEntries);
foreach (string line in splitResult)
{
var halves = line.Split(new Char[] { ':' }, 2);
var lhs_bool = Convert.ToBoolean(halves[0].Trim());
if (parking_val == lhs_bool)
{
return halves[1].Trim();
}
}
return ps.RegistryValue["icon"]; // default icon
}
protected readonly string defaultIconStr = string.Format("GenericDecorator.Images.{0}", "generalicon.emf");
protected enum EmbellishType
{
None,
Instance,
Reference,
SubType,
Type,
NullReference,
NullReferenceWarning
}
protected Image image { get; set; }
protected Image default_image { get; set; }
protected EmbellishType Embellish;
protected string fcoName;
protected IBackroundDraw backgrounddraw;
Image refImage=null;
private Type formType;
Image LoadImage(string resourceName)
{
Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
if (imageStream != null)
{
return Image.FromStream(imageStream);
}
return null;
}
~DefaultBitmapDecorator()
{
}
string[] search_paths;
public DefaultBitmapDecorator(GenericDecorator decorator,
IntPtr parentHwnd, MgaFCO fco, MgaMetaFCO metafco, MgaProject project, IBackroundDraw backgrounddraw, Type formType = null)
: base(parentHwnd, fco, project, formType != null)
{
string iconName;
decorator.GetPreference(out iconName, "icon");
string defaultIconName = iconName;
if (fco != null)
{
string kind = fco.Meta.Name;
if (kind == "ParkingSign")
{
iconName = GetParkingSignIcon(fco);
}
else if (kind == "FloodSign")
{
iconName = GetFloodSignIcon(fco);
}
else if (kind == "StreetSign")
{
iconName = GetStreetSignIcon(fco);
}
// else fall through to the default icon
}
this.backgrounddraw = backgrounddraw;
this.formType = formType;
string projectDir = Path.GetDirectoryName(project.ProjectConnStr.Substring(4));
string paradigmDir = Path.GetDirectoryName(project.ParadigmConnStr.Substring(4));
MgaRegistrar registrar = new MgaRegistrar();
StringBuilder iconPath = new StringBuilder(registrar.IconPath[regaccessmode_enum.REGACCESS_BOTH]);
iconPath.Replace("$PROJECTDIR", projectDir);
iconPath.Replace("$PARADIGMDIR", paradigmDir);
search_paths = iconPath.ToString().Split(';');
foreach (string path in search_paths)
{
string fileName = Path.Combine(path, iconName);
if (File.Exists(fileName))
{
image = Image.FromFile(fileName);
break;
}
}
if (image == null) // look at the default
{
foreach (string path in search_paths)
{
string fileName = Path.Combine(path, defaultIconName);
if (File.Exists(fileName))
{
image = Image.FromFile(fileName);
break;
}
}
}
if (image == null)
{
image = LoadImage(defaultIconStr);
default_image = image;
}
if (fco != null)
{
fcoName = fco.Name;
//fcoName = String.IsNullOrEmpty(metafco.DisplayedName) ? metafco.Name : metafco.DisplayedName;
if (fco.ObjType == objtype_enum.OBJTYPE_REFERENCE)
{
MgaReference reference = fco as MgaReference;
if (reference.Referred == null)
{
Embellish = EmbellishType.NullReference;
}
else
{
Embellish = EmbellishType.Reference;
}
} else if (fco.IsInstance)
{
// instance
Embellish = EmbellishType.Instance;
}
else if (fco.DerivedFrom != null)
{
// Subtype
Embellish = EmbellishType.SubType;
}
else if (fco.DerivedObjects.Count != 0)
{
Embellish = EmbellishType.Type;
}
else
{
Embellish = EmbellishType.None;
}
}
else
{
fcoName = String.IsNullOrEmpty(metafco.DisplayedName) ? metafco.Name : metafco.DisplayedName;
}
}
public override void SetParam(string Name, object value)
{
}
protected override void DrawNormal(Graphics g)
{
if (backgrounddraw != null)
backgrounddraw.DrawBackground(g, new Point(DecoratorArea.X+DecoratorArea.Width/2, DecoratorArea.Y+DecoratorArea.Height/2), Active);
g.DrawImage(image, new Rectangle(DecoratorArea.X + (DecoratorArea.Width-image.Width)/2, DecoratorArea.Y + (DecoratorArea.Height-image.Height)/2, image.Width, image.Height));
Rectangle textArea = new Rectangle(new Point(DecoratorArea.Left, backgrounddraw == null ? DecoratorArea.Bottom : (DecoratorArea.Y + DecoratorArea.Height / 2 + backgrounddraw.Dimensions.Height / 2)), new Size(DecoratorArea.Width, 0));
RectangleF textAreaF = textArea;
Font font = new Font("Arial", 10);
SizeF textSizeF = g.MeasureString(fcoName, font);
textAreaF.Height = textSizeF.Height;
textAreaF.X += (textAreaF.Width - textSizeF.Width) / 2;
textAreaF.Width = textSizeF.Width;
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Near;
g.DrawString(fcoName, font, new SolidBrush(Color.Black), textAreaF, format);
DrawEmbellishments(g, DecoratorArea);
}
protected void DrawMarker(Graphics g, string s, Point p, Color bgColor)
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Font f = new Font("Century", 8, FontStyle.Bold);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
Rectangle area = new Rectangle(p.X - 13 / 2, p.Y - 13 / 2, 13, 13);
Rectangle shadow = area;
shadow.X -= 1;
shadow.Y += 1;
shadow.Width += 1;
shadow.Height += 1;
Brush b = new SolidBrush(Color.FromArgb(128, Color.Black));
g.FillEllipse(b, shadow);
b = new SolidBrush(bgColor);
g.FillEllipse(b, area);
g.DrawEllipse(Pens.DarkBlue, area);
g.DrawString(s, f, new SolidBrush(Color.Black), area, format);
}
protected void DrawEmbellishments(Graphics g, Rectangle area)
{
if (refImage != null)
{
g.DrawImage(refImage, new Rectangle(area.X, area.Y, 13, 13));
}
if (Embellish != EmbellishType.None)
{
Rectangle iconArea = new Rectangle(area.X + area.Width - 13, area.Y - 13/2, 13, 13);
if (Embellish != EmbellishType.Reference)
{
if (Embellish != EmbellishType.NullReference)
{
string s = "";
switch (Embellish)
{
case EmbellishType.Instance:
s = "I";
break;
case EmbellishType.SubType:
s = "S";
break;
case EmbellishType.Type:
s = "T";
break;
}
DrawMarker(g, s, new Point((iconArea.Right + iconArea.Left) / 2, (iconArea.Bottom + iconArea.Top) / 2), Active ? Color.LightBlue : Color.LightGray);
}
else if (Embellish == EmbellishType.NullReference)
{
DrawMarker(g, "!", new Point((iconArea.Right + iconArea.Left) / 2, (iconArea.Bottom + iconArea.Top) / 2), Active ? Color.Red : Color.LightGray);
}
}
}
}
protected override void DrawSelected(Graphics g)
{
DrawNormal(g);
}
public override void GetPreferredSize(out int sizex, out int sizey)
{
sizex = image.Width;
sizey = image.Height;
}
}
}