-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUshortEditorForm.cs
422 lines (370 loc) · 14.7 KB
/
UshortEditorForm.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SM64DSe
{
public partial class RawEditorForm : Form
{
private LevelEditorForm m_parent;
private bool m_displayInBinary;
private RawUshortEdit[] m_ParameterControls = new RawUshortEdit[5];
private Label m_NothingCtrl;
public RawEditorForm(LevelEditorForm levelEditor)
{
m_parent = levelEditor;
m_displayInBinary = false;
InitializeComponent();
for (int i = 0; i < m_ParameterControls.Length; i++)
{
m_ParameterControls[i] = new RawUshortEdit(0, valueInput, m_displayInBinary) { ValueChanged = SendValueToLevelForm };
m_ParameterControls[i].MouseDown += DeselectOthers;
m_ParameterControls[i].Tag = "Parameter " + (i + 1);
}
//Noting
m_NothingCtrl = new Label() { Text = "No Raw Editing available for this Object", Width = 300 };
}
public void ShowForObject(LevelObject obj)
{
if (!Visible)
Show(m_parent);
else
Activate();
UpdateForObject(obj);
}
public void UpdateParameter(string name, ushort value)
{
foreach (Control ctrl in panControls.Controls)
{
if ((ctrl is RawUshortEdit) && ((string)ctrl.Tag == name))
((RawUshortEdit)ctrl).SetValue(value);
}
}
public void UpdateForObject(LevelObject obj)
{
panControls.Controls.Clear();
int nextSnapY = 7;
int nParameters;
int firstParameterIndex;
switch (obj.m_Type)
{
case LevelObject.Type.STANDARD:
nParameters = 3;
firstParameterIndex = 0;
break;
case LevelObject.Type.SIMPLE:
nParameters = 1;
firstParameterIndex = 0;
break;
case LevelObject.Type.PATH:
nParameters = 3;
firstParameterIndex = 2;
break;
default:
nParameters = -1;
firstParameterIndex = -1;
break;
}
if (nParameters > -1)
{
for (int i = 0; i < nParameters; i++)
{
RawUshortEdit control = m_ParameterControls[i + firstParameterIndex];
panControls.Controls.Add(control);
nextSnapY = Helper.snapControlVertically(control, nextSnapY) + 7;
control.SetValue(obj.Parameters[i + firstParameterIndex]);
}
}
else
{
panControls.Controls.Add(m_NothingCtrl);
Helper.snapControlVertically(m_NothingCtrl, nextSnapY);
}
}
private void DeselectOthers(object sender, MouseEventArgs e)
{
foreach (Control ctrl in panControls.Controls)
{
if ((ctrl is RawUshortEdit) && (ctrl != sender))
((RawUshortEdit)ctrl).Deselect();
}
}
private void RawEditorForm_FormClosing(object sender, FormClosingEventArgs e)
{
m_parent.m_rawEditor = null;
}
private void btnToogleBinary_Click(object sender, EventArgs e)
{
m_displayInBinary = !m_displayInBinary;
foreach (Control ctrl in panControls.Controls)
{
if (ctrl is RawUshortEdit)
((RawUshortEdit)ctrl).SetBinary(m_displayInBinary);
}
((Button)sender).Text = "Display in " + (m_displayInBinary ? "Hex" : "Binary");
}
private void SendValueToLevelForm(RawUshortEdit sender)
{
m_parent.SetProperty((string)sender.Tag, sender.GetValue());
m_parent.InitializePropertyInterface();
}
private void btnToggleHex_Click(object sender, EventArgs e)
{
if (valueInput.Hexadecimal)
{
valueInput.Hexadecimal = false;
btnToggleHex.Text = "Hex";
}
else
{
valueInput.Hexadecimal = true;
btnToggleHex.Text = "Dec";
}
}
}
public partial class RawUshortEdit : UserControl
{
int m_FieldWidth;
ushort m_ValueToEdit;
NumericUpDown m_ValueInput;
string m_StringRepresentation;
int m_SelectionStartIndex;
int m_SelectionEndIndex;
bool m_InBinary;
bool m_SettingValues;
public delegate void OnValueChanged(RawUshortEdit sender);
public OnValueChanged ValueChanged = null;
public RawUshortEdit(ushort value, NumericUpDown valueInput, bool displayBinary = false)
{
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer,
true);
m_ValueInput = valueInput;
m_ValueInput.ValueChanged += M_valueInput_ValueChanged;
m_InBinary = displayBinary;
m_SelectionStartIndex = -1;
m_SelectionEndIndex = 0;
SetValue(value);
}
private void M_valueInput_ValueChanged(object sender, EventArgs e)
{
InsertValue((ushort)((NumericUpDown)sender).Value);
if (ValueChanged != null)
{
ValueChanged.Invoke(this);
}
}
public void Deselect()
{
m_SelectionStartIndex = -1;
Refresh();
}
public bool IsSelected()
{
return (m_SelectionStartIndex != -1);
}
public bool InsertValue(ushort value)
{
if ((m_SelectionStartIndex == -1)|| m_SettingValues)
return false;
int selectionStart = m_SelectionStartIndex;
int selectionLength = (m_SelectionEndIndex - m_SelectionStartIndex) + 1;
string stringValue = Convert.ToString(value, m_InBinary?2:16).PadLeft(selectionLength,'0');
if (stringValue.Length<= selectionLength)
{
string stringWithGap = m_StringRepresentation.Remove(selectionStart, selectionLength);
m_StringRepresentation = stringWithGap.Insert(selectionStart,stringValue);
Refresh();
RefreshValue();
return true;
}
return false;
}
public ushort GetValue()
{
return m_ValueToEdit;
}
public void SetValue(ushort value)
{
m_ValueToEdit = value;
if (m_InBinary)
{
Width = 320;
Height = 20;
m_FieldWidth = 20;
m_StringRepresentation = Convert.ToString(m_ValueToEdit, 2).PadLeft(16, '0');
if (m_SelectionStartIndex != -1)
{
int selectionStart = m_SelectionStartIndex;
int selectionLength = (m_SelectionEndIndex - m_SelectionStartIndex) + 1;
m_SettingValues = true; //prevent the valueChanged event
m_ValueInput.Value = Convert.ToUInt16(m_StringRepresentation.Substring(selectionStart, selectionLength), 2);
m_SettingValues = false;
}
}
else
{
Width = 160;
Height = 20;
m_FieldWidth = 40;
m_StringRepresentation = Convert.ToString(m_ValueToEdit, 16).PadLeft(4, '0').ToUpper();
if (m_SelectionStartIndex != -1)
{
int selectionStart = m_SelectionStartIndex;
int selectionLength = (m_SelectionEndIndex - m_SelectionStartIndex) + 1;
m_SettingValues = true; //prevent the valueChanged event
m_ValueInput.Value = Convert.ToUInt16(m_StringRepresentation.Substring(selectionStart, selectionLength), 16);
m_SettingValues = false;
}
}
Refresh();
}
public void SetBinary(bool value)
{
if (value == m_InBinary) return;
if (value)
{
Width = 320;
Height = 20;
m_InBinary = true;
m_FieldWidth = 20;
m_StringRepresentation = Convert.ToString(m_ValueToEdit, 2).PadLeft(16, '0');
if (m_SelectionStartIndex != -1)
{
m_SelectionStartIndex *= 4;
m_SelectionEndIndex *= 4;
m_SelectionEndIndex += 3;
}
}
else
{
Width = 160;
Height = 20;
m_InBinary = false;
m_FieldWidth = 40;
m_StringRepresentation = Convert.ToString(m_ValueToEdit, 16).PadLeft(4, '0');
if (m_SelectionStartIndex != -1)
{
m_SelectionStartIndex /= 4;
m_SelectionEndIndex /= 4;
int selectionStart = m_SelectionStartIndex;
int selectionLength = (m_SelectionEndIndex - m_SelectionStartIndex) + 1;
m_SettingValues = true; //prevent the valueChanged event
m_ValueInput.Maximum = (ushort)(Math.Pow(m_InBinary ? 2 : 16, selectionLength) - 1);
m_ValueInput.Value = Convert.ToUInt16(m_StringRepresentation.Substring(selectionStart, selectionLength), 16);
m_SettingValues = false;
}
}
Refresh();
}
public void RefreshValue()
{
if (m_InBinary)
{
m_ValueToEdit = Convert.ToUInt16(m_StringRepresentation, 2);
}
else
{
m_ValueToEdit = Convert.ToUInt16(m_StringRepresentation, 16);
}
}
protected override void OnPaint(PaintEventArgs e)
{
Color backColor = SystemColors.ButtonShadow;
Brush selBackColor = new SolidBrush(SystemColors.Highlight);
Brush fieldColor = new SolidBrush(SystemColors.ButtonFace);
Brush selFieldColor = new SolidBrush(SystemColors.GradientActiveCaption);
Brush textColor = new SolidBrush(SystemColors.ControlText);
Font textFont = new Font(SystemFonts.DefaultFont, FontStyle.Bold);
int selectionStart;
int selectionLength;
if (m_SelectionStartIndex <= m_SelectionEndIndex)
{
selectionStart = m_SelectionStartIndex;
selectionLength = (m_SelectionEndIndex - m_SelectionStartIndex) + 1;
}
else
{
selectionStart = m_SelectionEndIndex;
selectionLength = (m_SelectionStartIndex - m_SelectionEndIndex) + 1;
}
e.Graphics.Clear(backColor);
if (m_SelectionStartIndex!=-1)
{
Rectangle selectionRect = new Rectangle(
selectionStart * m_FieldWidth, 0,
selectionLength * m_FieldWidth, Height
);
e.Graphics.FillRectangle(selBackColor, selectionRect);
}
for (int i = 0; i < (m_InBinary?16:4); i++)
{
Rectangle fieldRect = new Rectangle(1 + i*m_FieldWidth, 1, m_FieldWidth - 2, Height - 2);
if ((i>= selectionStart) && (i< selectionStart + selectionLength) && (m_SelectionStartIndex != -1))
e.Graphics.FillRectangle(selFieldColor, fieldRect);
else
e.Graphics.FillRectangle(fieldColor, fieldRect);
if ( m_InBinary && (Math.Floor(i/4.0)%2==0) )
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, 100, 100, 50)), fieldRect);
e.Graphics.DrawString(m_StringRepresentation[i].ToString(), textFont, textColor, fieldRect.Left+(m_InBinary?1:3),fieldRect.Top+2);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
m_SelectionStartIndex = (int)Math.Floor(e.X /(float)m_FieldWidth);
m_SelectionEndIndex = m_SelectionStartIndex;
Refresh();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
m_SelectionEndIndex = Math.Min(Math.Max(0,(int)Math.Floor(e.X / (float)m_FieldWidth)), m_InBinary ? 15 : 3);
Refresh();
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (e.Button == MouseButtons.Left)
{
if(m_SelectionStartIndex > m_SelectionEndIndex)
{
int startIndexBefore = m_SelectionStartIndex;
m_SelectionStartIndex = m_SelectionEndIndex;
m_SelectionEndIndex = startIndexBefore;
}
int selectionStart = m_SelectionStartIndex;
int selectionLength = (m_SelectionEndIndex - m_SelectionStartIndex) + 1;
m_SettingValues = true; //prevent the valueChanged event
m_ValueInput.Maximum = (ushort)(Math.Pow(m_InBinary ? 2 : 16, selectionLength)-1);
m_ValueInput.Value = Convert.ToUInt16(m_StringRepresentation.Substring(selectionStart, selectionLength), m_InBinary ? 2 : 16);
m_SettingValues = false;
}
}
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
m_SelectionStartIndex = 0;
int selectionLength = m_InBinary ? 16 : 4;
m_SelectionEndIndex = selectionLength - 1;
m_SettingValues = true; //prevent the valueChanged event
m_ValueInput.Maximum = (ushort)(Math.Pow(m_InBinary ? 2 : 16, selectionLength) - 1);
m_ValueInput.Value = Convert.ToUInt16(m_StringRepresentation.Substring(m_SelectionStartIndex, selectionLength), m_InBinary ? 2 : 16);
m_SettingValues = false;
Refresh();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
//TODO implement a Direct Digit Writing
}
}
}