-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldWorkspace.cs
More file actions
351 lines (311 loc) · 11.9 KB
/
WorldWorkspace.cs
File metadata and controls
351 lines (311 loc) · 11.9 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
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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WorldEditor
{
public partial class WorldWorkspace : Control
{
#pragma warning disable 0414
// Constants
private int levelMargin = 24;
// Variables
private int pWidth, pHeight, pArrSize, pWSWidth, pWSHeight;
private ushort[] pLevelGrid = null;
private Tile[] pLevelPalette = new Tile[512];
private string openedFile = null;
private bool pShowGrid = false;
private ushort curSelectedIndex = 0;
// Drawing Lines, Rectangles variable
private int pDBeginX, pDBeginY, pDEndX, pDEndY;
private bool pDPreview; // Action not commited/ended yet
// For Selection Rectangle Tool
private int pSelectBeginX, pSelectBeginY, pSelectWidth, pSelectHeight;
// private bool pDSelect
// Optimized Drawing
private int pCurTileWidth, pCurTileHeight;
public WorldWorkspace()
{
InitializeComponent();
this.Controls.Add(hScrollBar1);
this.Controls.Add(vScrollBar1);
this.Controls.Add(panel1);
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
hScrollBar1.Enabled = false;
vScrollBar1.Enabled = false;
}
// Get stuff
public int LGridWidth
{
get { return pWidth; }
}
public int LGridWSWidth
{
get { return pWSWidth; }
}
public int LGridHeight
{
get { return pHeight; }
}
public int LGridWSHeight
{
get { return pWSHeight; }
}
public int LGridXOffset
{
get
{
if (hScrollBar1.Enabled)
return hScrollBar1.Value;
else
return 0;
}
}
public int LGridYOffset
{
get
{
if (vScrollBar1.Enabled)
return vScrollBar1.Value;
else
return 0;
}
}
public bool ShowGridLines
{
get { return pShowGrid; }
set { pShowGrid = value; this.Invalidate(); }
}
// Functions
public void CreateNewLevel(int width, int height)
{
pWidth = width;
pHeight = height;
pArrSize = pWidth * pHeight;
pWSWidth = (pWidth * 32) + (levelMargin * 2);
pWSHeight = (pHeight * 32) + (levelMargin * 2);
pLevelGrid = new ushort[pArrSize];
int tempSBX = pWSWidth - (this.Bounds.Width - vScrollBar1.Width);
int tempSBY = pWSHeight - (this.Bounds.Height - hScrollBar1.Height);
if (tempSBX > 0)
{
hScrollBar1.Minimum = 0;
hScrollBar1.Maximum = tempSBX;
if (tempSBX < 128)
hScrollBar1.LargeChange = tempSBX;
else
hScrollBar1.LargeChange = 128;
if (tempSBX < 32)
hScrollBar1.SmallChange = tempSBX;
else
hScrollBar1.SmallChange = 32;
hScrollBar1.Maximum += hScrollBar1.LargeChange;
hScrollBar1.Enabled = true;
}
else
{
hScrollBar1.Enabled = false;
}
if (tempSBY > 0)
{
vScrollBar1.Minimum = 0;
vScrollBar1.Maximum = tempSBY;
if (tempSBY < 128)
vScrollBar1.LargeChange = tempSBY;
else
vScrollBar1.LargeChange = 128;
if (tempSBY < 32)
vScrollBar1.SmallChange = tempSBY;
else
vScrollBar1.SmallChange = 32;
vScrollBar1.Maximum += vScrollBar1.LargeChange;
vScrollBar1.Enabled = true;
}
else
{
vScrollBar1.Enabled = false;
}
hScrollBar1.Value = 0;
vScrollBar1.Value = 0;
pCurTileWidth = (this.Bounds.Width / 32) + 1;
pCurTileHeight = (this.Bounds.Height / 32) + 1;
this.Invalidate();
}
public bool UpdateSelectedTile(Tile updateTile)
{
bool found = false;
for (ushort i = 1; i < pLevelPalette.Length; i++)
{
if (pLevelPalette[i] != null && pLevelPalette[i].TileName == updateTile.TileName)
{
found = true;
curSelectedIndex = i;
break;
}
}
if (!found)
{
// Find empty slot
ushort emptyIndex = 0;
for (ushort i = 1; i < pLevelPalette.Length; i++)
{
if (pLevelPalette[i] == null)
{
emptyIndex = i;
break;
}
}
if (emptyIndex != 0)
{
pLevelPalette[emptyIndex] = updateTile;
curSelectedIndex = emptyIndex;
return true;
}
else
{
return false; // The palette is full
}
}
else
{
return true;
}
}
public void PaintTile(int tX, int tY)
{
// Pencil Tool
if (pLevelGrid[(tY * pWidth) + tX] != curSelectedIndex)
{
pLevelGrid[(tY * pWidth) + tX] = curSelectedIndex;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pe)
{
// Calling the base class OnPaint
base.OnPaint(pe);
pe.Graphics.FillRectangle(Brushes.DarkGray, 0, 0, this.Bounds.Width, this.Bounds.Height);
if (pLevelGrid != null)
{
int tmpOffsetX = 0;
int tmpOffsetY = 0;
int tmpXWidth = 0;
int tmpYHeight = 0;
int gOffsetX = 0;
int gOffsetY = 0;
// This code is for if user maximizes and scrollbars are disabled
if (hScrollBar1.Enabled)
tmpOffsetX = hScrollBar1.Value;
if (vScrollBar1.Enabled)
tmpOffsetY = vScrollBar1.Value;
// Draw Outline Border
pe.Graphics.DrawRectangle(Pens.Black, levelMargin - 1 - tmpOffsetX, levelMargin - 1 - tmpOffsetY, (pWidth * 32) + 1, (pHeight * 32) + 1);
if (tmpOffsetX > 56)
gOffsetX = (tmpOffsetX - 24) / 32;
if (tmpOffsetY > 56)
gOffsetY = (tmpOffsetY - 24) / 32;
tmpXWidth = gOffsetX + pCurTileWidth;
tmpYHeight = gOffsetY + pCurTileHeight;
if (tmpXWidth > pWidth)
tmpXWidth = pWidth;
if (tmpYHeight > pHeight)
tmpYHeight = pHeight;
for (int y = gOffsetY; y < tmpYHeight; y++)
{
for (int x = gOffsetX; x < tmpXWidth; x++)
{
int tmpGridIndex = (y * pWidth) + x;
if (pLevelGrid[tmpGridIndex] == 0)
{
pe.Graphics.FillRectangle(Brushes.White, (x << 5) + levelMargin - tmpOffsetX, (y << 5) + levelMargin - tmpOffsetY, 32, 32);
pe.Graphics.FillRectangle(Brushes.LightGray, (x << 5) + levelMargin - tmpOffsetX, (y << 5) + levelMargin - tmpOffsetY, 16, 16);
pe.Graphics.FillRectangle(Brushes.LightGray, (x << 5) + levelMargin - tmpOffsetX + 16, (y << 5) + levelMargin - tmpOffsetY + 16, 16, 16);
}
else
{
pe.Graphics.DrawImageUnscaled(pLevelPalette[pLevelGrid[tmpGridIndex]].TileImage, (x << 5) + levelMargin - tmpOffsetX, (y << 5) + levelMargin - tmpOffsetY);
}
// Draw Grid?
if (pShowGrid)
{
pe.Graphics.DrawRectangle(Pens.DarkGray, (x << 5) + levelMargin - tmpOffsetX, (y << 5) + levelMargin - tmpOffsetY, 31, 31);
}
}
}
//if (curSelectedIndex != 0)
//{
// Font tmpFont = new Font(FontFamily.GenericSerif, 8, FontStyle.Bold);
// pe.Graphics.DrawString("Current Tile: " + pLevelPalette[curSelectedIndex].TileName, tmpFont, Brushes.Black, 2.0f, 2.0f);
// pe.Graphics.DrawString("Current Palette Index: " + curSelectedIndex, tmpFont, Brushes.Black, 2.0f, 20.0f);
// tmpFont.Dispose();
//}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Rectangle tempRect = new Rectangle(this.Bounds.Width - vScrollBar1.Width, 0, vScrollBar1.Width, this.Bounds.Height - hScrollBar1.Height);
vScrollBar1.Bounds = tempRect;
tempRect = new Rectangle(0, this.Bounds.Height - hScrollBar1.Height, this.Bounds.Width - vScrollBar1.Width, hScrollBar1.Height);
hScrollBar1.Bounds = tempRect;
tempRect = new Rectangle(vScrollBar1.Bounds.X, hScrollBar1.Bounds.Y, vScrollBar1.Width, hScrollBar1.Height);
panel1.Bounds = tempRect;
if (pLevelGrid != null)
{
int tempSBX = pWSWidth - (this.Bounds.Width - vScrollBar1.Width);
int tempSBY = pWSHeight - (this.Bounds.Height - hScrollBar1.Height);
if (tempSBX > 0)
{
hScrollBar1.Minimum = 0;
hScrollBar1.Maximum = tempSBX;
if (tempSBX < 128)
hScrollBar1.LargeChange = tempSBX;
else
hScrollBar1.LargeChange = 128;
if (tempSBX < 32)
hScrollBar1.SmallChange = tempSBX;
else
hScrollBar1.SmallChange = 32;
hScrollBar1.Maximum += hScrollBar1.LargeChange;
hScrollBar1.Enabled = true;
}
else
{
hScrollBar1.Enabled = false;
}
if (tempSBY > 0)
{
vScrollBar1.Minimum = 0;
vScrollBar1.Maximum = tempSBY;
if (tempSBY < 128)
vScrollBar1.LargeChange = tempSBY;
else
vScrollBar1.LargeChange = 128;
if (tempSBY < 32)
vScrollBar1.SmallChange = tempSBY;
else
vScrollBar1.SmallChange = 32;
vScrollBar1.Maximum += vScrollBar1.LargeChange;
vScrollBar1.Enabled = true;
}
else
{
vScrollBar1.Enabled = false;
}
}
pCurTileWidth = (this.Bounds.Width / 32) + 1;
pCurTileHeight = (this.Bounds.Height / 32) + 1;
this.Invalidate();
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
this.Invalidate();
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
this.Invalidate();
}
}
}