-
Notifications
You must be signed in to change notification settings - Fork 6
/
Editor.cs
394 lines (348 loc) · 10.8 KB
/
Editor.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Linq;
using System.Collections.Generic;
namespace Noxico
{
#if DEBUG
public class Editor : Form
{
private ComboBox combo;
private TabControl tabs;
public TokenCarrier Carrier { get; private set; }
public Editor()
{
Text = "Noxico Debug Editor 0.5";
ClientSize = new Size(388, 442);
tabs = new TabControl();
tabs.Dock = DockStyle.Fill;
Controls.Add(tabs);
combo = new ComboBox();
combo.DropDownStyle = ComboBoxStyle.DropDownList;
combo.Dock = DockStyle.Top;
combo.SelectedIndexChanged += new EventHandler(combo_SelectedIndexChanged);
Controls.Add(combo);
}
public void LoadBoard(Board board)
{
combo.Items.Clear();
combo.Items.AddRange(board.Entities.ToArray());
combo.Items.Add(board);
combo.SelectedIndex = 0;
ShowDialog();
}
private void combo_SelectedIndexChanged(object sender, EventArgs e)
{
Carrier = null;
tabs.TabPages.Clear();
AddTab(combo.SelectedItem);
combo.Select();
}
private void AddTab(object o)
{
var page = new TabPage(o.GetType().ToString());
var prop = new PropertyGrid();
prop.Dock = DockStyle.Fill;
prop.SelectedObject = o;
prop.ToolbarVisible = false;
prop.HelpVisible = false;
prop.PropertySort = PropertySort.CategorizedAlphabetical;
page.Controls.Add(prop);
tabs.TabPages.Add(page);
if (o is Token)
Carrier = (Token)o;
else if (o is TokenCarrier)
Carrier = (TokenCarrier)o;
if (o is BoardChar)
AddTab(((BoardChar)o).Character);
else if (o is Container)
AddTab(((Container)o).Token);
else if (o is DroppedItem)
AddTab(((DroppedItem)o).Token);
}
}
public class GlyphSelectorControl : UserControl
{
private Brush highlight;
public int Value { get; set; }
public IWindowsFormsEditorService EdSvc { get; set; }
public GlyphSelectorControl()
{
GlyphSelector.Init();
highlight = new SolidBrush(Color.FromArgb(128, 64, 64, 255));
this.ClientSize = new Size(256, 512);
//this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
Paint += new PaintEventHandler(GlyphSelectorForm_Paint);
MouseUp += new MouseEventHandler(GlyphSelectorForm_MouseUp);
}
void GlyphSelectorForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(GlyphSelector.Sheet, 0, 0, 256, 512);
var val = Value - 32;
e.Graphics.FillRectangle(highlight, (val % 32) * 8, (val / 32) * 16, 8, 16);
e.Graphics.FillRectangle(highlight, 0, (val / 32) * 16, 256, 16);
e.Graphics.FillRectangle(highlight, (val % 32) * 8, 0, 8, 512);
}
void GlyphSelectorForm_MouseUp(object sender, MouseEventArgs e)
{
var x = e.X / 8;
var y = e.Y / 16;
if (x < 0 || y < 0 || x >= 32 || y >= 32)
return;
Value = ((y * 32) + x) + 32;
EdSvc.CloseDropDown(); //Close();
}
}
public class GlyphSelector : UITypeEditor
{
public static Bitmap Sheet;
public static void Init()
{
if (Sheet == null)
Sheet = Mix.GetBitmap("fonts\\8x16-bold.png");
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null && provider != null)
{
var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
var editor = new GlyphSelectorControl();
editor.Value = (int)Convert.ChangeType(value, context.PropertyDescriptor.PropertyType);
editor.EdSvc = edSvc;
//edSvc.ShowDialog(editor);
edSvc.DropDownControl(editor);
return editor.Value;
}
}
return value;
}
public override void PaintValue(PaintValueEventArgs e)
{
Init();
var val = (int)e.Value - 32;
var dest = e.Bounds;
var src = new System.Drawing.Rectangle((val % 32) * 8, (val / 32) * 16, 8, 16);
e.Graphics.DrawImage(GlyphSelector.Sheet, dest, src, GraphicsUnit.Pixel);
}
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
}
public class TokenEditorForm : Form
{
public List<Token> Value { get; set; }
private TokenCarrier carrier;
private TextBox textBox;
private ToolStrip toolStrip;
public TokenEditorForm()
{
this.Text = "Token editor";
this.ClientSize = new Size(512, 512);
textBox = new TextBox()
{
Multiline = true,
Dock = DockStyle.Fill,
AcceptsTab = true,
ScrollBars = ScrollBars.Both,
Font = new Font(FontFamily.GenericMonospace, 10),
};
Controls.Add(textBox);
toolStrip = new ToolStrip()
{
Dock = DockStyle.Top,
RenderMode = ToolStripRenderMode.System,
};
Controls.Add(toolStrip);
var checkButton = new ToolStripButton()
{
Text = "Check"
};
checkButton.Click += CheckButton_Click;
toolStrip.Items.Add(checkButton);
var revertButton = new ToolStripButton()
{
Text = "Revert"
};
revertButton.Click += RevertButton_Click;
toolStrip.Items.Add(revertButton);
Load += new EventHandler(TokenEditorForm_Load);
FormClosed += new FormClosedEventHandler(TokenEditorForm_FormClosed);
}
void TokenEditorForm_Load(object sender, EventArgs e)
{
carrier = new TokenCarrier();
textBox.Text = carrier.DumpTokens(Value, 0);
textBox.SelectionStart = textBox.SelectionLength = 0;
}
void TokenEditorForm_FormClosed(object sender, FormClosedEventArgs e)
{
carrier.Tokenize(textBox.Text);
Value = carrier.Tokens;
}
void CheckButton_Click(object sender, EventArgs e)
{
try
{
var checker = new TokenCarrier();
checker.Tokenize(textBox.Text);
var newValue = checker.Tokens;
foreach (var form in Application.OpenForms)
{
if (form is Editor)
{
var carrier = ((Editor)form).Carrier;
if (carrier is Character)
{
var root = new Token("character", "<check>");
root.AddSet(checker.Tokens);
root.CheckSchema("unique", ((Character)carrier).ID);
}
break;
}
}
textBox.Text = checker.DumpTokens(newValue, 0);
textBox.SelectionStart = textBox.SelectionLength = 0;
}
catch (Exception x)
{
System.Windows.Forms.MessageBox.Show(x.Message, this.Text);
return;
}
}
void RevertButton_Click(object sender, EventArgs e)
{
textBox.Text = carrier.DumpTokens(Value, 0);
textBox.SelectionStart = textBox.SelectionLength = 0;
}
}
public class TokenEditor : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null && provider != null)
{
var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
var editor = new TokenEditorForm();
var tokens = value as List<Token>;
editor.Value = tokens;
edSvc.ShowDialog(editor);
//return editor.Value;
tokens.Clear();
tokens.AddRange(editor.Value);
value = tokens;
}
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}
public class ColorEditorControl : UserControl
{
public Color Value { get; set; }
public IWindowsFormsEditorService EdSvc { get; set; }
private static List<Tuple<string, SolidBrush>> colors;
private TextBox hexField;
private ListBox knownList;
public ColorEditorControl(Color value)
{
Value = value;
hexField = new TextBox();
knownList = new ListBox();
hexField.Text = value.ToHex();
hexField.Dock = DockStyle.Top;
if (colors == null)
{
colors = new List<Tuple<string, SolidBrush>>();
var knownColors = Mix.GetTokenTree("knowncolors.tml", true);
foreach (var knownColor in knownColors)
{
var name = knownColor.Name;
var brush = new SolidBrush(System.Drawing.Color.FromArgb((int)((long)knownColor.Value | 0xFF000000)));
colors.Add(new Tuple<string, SolidBrush>(name, brush));
}
}
knownList.Items.AddRange(colors.ToArray());
for (var i = 0; i < colors.Count; i++)
{
var colorHere = colors[i].Item2.Color.ToArgb();
if ((colorHere & 0xFFFFFF) == (value.ArgbValue & 0xFFFFFF))
{
knownList.SelectedIndex = i;
break;
}
}
knownList.DrawMode = DrawMode.OwnerDrawFixed;
knownList.DrawItem += new DrawItemEventHandler(knownList_DrawItem);
knownList.SelectedIndexChanged += new EventHandler(knownList_SelectedIndexChanged);
knownList.Dock = DockStyle.Fill;
Controls.Add(knownList);
Controls.Add(hexField);
}
void knownList_SelectedIndexChanged(object sender, EventArgs e)
{
var color = ((Tuple<string, SolidBrush>)knownList.SelectedItem).Item2.Color.ToArgb();
var foo = color.ToString("X");
hexField.Text = "#" + foo.Substring(foo.Length - 8);
Value = Color.FromArgb(color);
}
void knownList_DrawItem(object sender, DrawItemEventArgs e)
{
var item = (Tuple<string, SolidBrush>)knownList.Items[e.Index];
var name = item.Item1;
var brush = item.Item2;
var rect = new System.Drawing.Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, e.Bounds.Height - 2, e.Bounds.Height - 2);
e.DrawBackground();
e.Graphics.FillRectangle(brush, rect);
e.Graphics.DrawRectangle(Pens.Black, rect);
e.DrawFocusRectangle();
}
}
public class ColorEditor : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null && provider != null)
{
var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
var editor = new ColorEditorControl((Color)Convert.ChangeType(value, context.PropertyDescriptor.PropertyType));
editor.EdSvc = edSvc;
edSvc.DropDownControl(editor);
return editor.Value;
}
}
return value;
}
public override void PaintValue(PaintValueEventArgs e)
{
var val = (Color)e.Value;
e.Graphics.FillRectangle(new SolidBrush(val), e.Bounds);
}
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
}
#endif
}