-
Notifications
You must be signed in to change notification settings - Fork 2
/
ControlSectorArea.cs
541 lines (440 loc) · 15.5 KB
/
ControlSectorArea.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#region ================== Copyright (c) 2014 Boris Iwanski
/*
* Copyright (c) 2014 Boris Iwanski
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
using System;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using CodeImp.DoomBuilder.Geometry;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Editing;
using CodeImp.DoomBuilder.Map;
namespace CodeImp.DoomBuilder.ThreeDFloorMode
{
public class ControlSectorArea
{
#region ================== Enums
public enum Highlight
{
None,
OuterLeft,
OuterRight,
OuterTop,
OuterBottom,
OuterTopLeft,
OuterTopRight,
OuterBottomLeft,
OuterBottomRight,
Body
};
#endregion
#region ================== Variables
private RectangleF outerborder;
private PixelColor bordercolor = new PixelColor(255, 0, 192, 0);
private PixelColor fillcolor = new PixelColor(128, 0, 128, 0);
private PixelColor borderhighlightcolor = new PixelColor(255, 0, 192, 0);
private PixelColor fillhighlightcolor = new PixelColor(128, 0, 192, 0);
private Dictionary<Highlight, Line2D> lines;
private Dictionary<Highlight, Vector2D> points;
private float gridsize;
private float gridsizeinv;
private float sectorsize;
private float outerleft;
private float outerright;
private float outertop;
private float outerbottom;
private bool usecustomtagrange;
private int firsttag;
private int lasttag;
#endregion
#region ================== Properties
public float GridSize { get { return gridsize; } }
public float SectorSize { get { return sectorsize; } }
public RectangleF OuterBorder { get { return outerborder; } }
public float OuterLeft
{
get { return outerleft; }
set { outerleft = value; UpdateLinesAndPoints(); }
}
public float OuterRight
{
get { return outerright; }
set { outerright = value; UpdateLinesAndPoints(); }
}
public float OuterTop
{
get { return outertop; }
set { outertop = value; UpdateLinesAndPoints(); }
}
public float OuterBottom
{
get { return outerbottom; }
set { outerbottom = value; UpdateLinesAndPoints(); }
}
public bool UseCustomTagRnage { get { return usecustomtagrange; } set { usecustomtagrange = value; } }
public int FirstTag { get { return firsttag; } set { firsttag = value; } }
public int LastTag { get { return lasttag; } set { lasttag = value; } }
#endregion
#region ================== Constructor / Disposer
public ControlSectorArea(float outerleft, float outerright, float outertop, float outerbottom, float gridsize, float sectorsize)
{
this.outerleft = outerleft;
this.outerright = outerright;
this.outertop = outertop;
this.outerbottom = outerbottom;
lines = new Dictionary<Highlight, Line2D>();
points = new Dictionary<Highlight, Vector2D>();
this.gridsize = gridsize;
gridsizeinv = 1.0f / gridsize;
this.sectorsize = sectorsize;
UpdateLinesAndPoints();
}
#endregion
#region ================== Methods
public void UpdateLinesAndPoints()
{
lines[Highlight.OuterLeft] = new Line2D(outerleft, outertop, outerleft, outerbottom);
lines[Highlight.OuterRight] = new Line2D(outerright, outertop, outerright, outerbottom);
lines[Highlight.OuterTop] = new Line2D(outerleft, outertop, outerright, outertop);
lines[Highlight.OuterBottom] = new Line2D(outerleft, outerbottom, outerright, outerbottom);
points[Highlight.OuterTopLeft] = new Vector2D(outerleft, outertop);
points[Highlight.OuterTopRight] = new Vector2D(outerright, outertop);
points[Highlight.OuterBottomLeft] = new Vector2D(outerleft, outerbottom);
points[Highlight.OuterBottomRight] = new Vector2D(outerright, outerbottom);
outerborder = new RectangleF(outerleft, outertop, outerright - outerleft, outerbottom - outertop);
}
public void Draw(IRenderer2D renderer, Highlight highlight)
{
PixelColor fcolor = highlight == Highlight.Body ? fillhighlightcolor : fillcolor;
renderer.RenderRectangleFilled(
new RectangleF(outerleft, outertop, outerright - outerleft, outerbottom - outertop),
fcolor,
true
);
// Draw the borders
renderer.RenderRectangle(outerborder, 1.0f, bordercolor, true);
// Highlight a border if necessary
if (highlight >= Highlight.OuterLeft && highlight <= Highlight.OuterBottom)
renderer.RenderLine(lines[highlight].v1, lines[highlight].v2, 1.0f, borderhighlightcolor, true);
else
{
// Highlight the corners
switch (highlight)
{
// Outer corners
case Highlight.OuterTopLeft:
renderer.RenderLine(lines[Highlight.OuterTop].v1, lines[Highlight.OuterTop].v2, 1.0f, borderhighlightcolor, true);
renderer.RenderLine(lines[Highlight.OuterLeft].v1, lines[Highlight.OuterLeft].v2, 1.0f, borderhighlightcolor, true);
break;
case Highlight.OuterTopRight:
renderer.RenderLine(lines[Highlight.OuterTop].v1, lines[Highlight.OuterTop].v2, 1.0f, borderhighlightcolor, true);
renderer.RenderLine(lines[Highlight.OuterRight].v1, lines[Highlight.OuterRight].v2, 1.0f, borderhighlightcolor, true);
break;
case Highlight.OuterBottomLeft:
renderer.RenderLine(lines[Highlight.OuterBottom].v1, lines[Highlight.OuterBottom].v2, 1.0f, borderhighlightcolor, true);
renderer.RenderLine(lines[Highlight.OuterLeft].v1, lines[Highlight.OuterLeft].v2, 1.0f, borderhighlightcolor, true);
break;
case Highlight.OuterBottomRight:
renderer.RenderLine(lines[Highlight.OuterBottom].v1, lines[Highlight.OuterBottom].v2, 1.0f, borderhighlightcolor, true);
renderer.RenderLine(lines[Highlight.OuterRight].v1, lines[Highlight.OuterRight].v2, 1.0f, borderhighlightcolor, true);
break;
}
}
}
public Highlight CheckHighlight(Vector2D pos, float scale)
{
float distance = float.MaxValue;
float d;
Highlight highlight = Highlight.None;
// Find a line to highlight
foreach (Highlight h in (Highlight[])Enum.GetValues(typeof(Highlight)))
{
if (h >= Highlight.OuterLeft && h <= Highlight.OuterBottom)
{
d = Line2D.GetDistanceToLine(lines[h].v1, lines[h].v2, pos, true);
if (d <= BuilderModes.BuilderPlug.Me.HighlightRange / scale && d < distance)
{
distance = d;
highlight = h;
}
}
}
distance = float.MaxValue;
// Find a corner to highlight
foreach (Highlight h in (Highlight[])Enum.GetValues(typeof(Highlight)))
{
if (h >= Highlight.OuterTopLeft && h <= Highlight.OuterBottomRight)
{
d = Vector2D.Distance(pos, points[h]);
if (d <= BuilderModes.BuilderPlug.Me.HighlightRange / scale && d < distance)
{
distance = d;
highlight = h;
}
}
}
if (highlight != Highlight.None)
return highlight;
if (OuterLeft < pos.x && OuterRight > pos.x && OuterTop > pos.y && OuterBottom < pos.y)
return Highlight.Body;
return Highlight.None;
}
public void SnapToGrid(Highlight highlight, Vector2D pos, Vector2D lastpos)
{
Vector2D newpos = GridSetup.SnappedToGrid(pos, gridsize, gridsizeinv);
switch (highlight)
{
case Highlight.Body:
Vector2D diff = GridSetup.SnappedToGrid(pos, gridsize, gridsizeinv) - GridSetup.SnappedToGrid(lastpos, gridsize, gridsizeinv);
Debug.WriteLine("diff: " + (diff).ToString());
outerleft += diff.x;
outerright += diff.x;
outertop += diff.y;
outerbottom += diff.y;
break;
// Outer border
case Highlight.OuterLeft:
if (newpos.x < outerright) outerleft = newpos.x;
break;
case Highlight.OuterRight:
if(newpos.x > outerleft) outerright = newpos.x;
break;
case Highlight.OuterTop:
if (newpos.y > outerbottom) outertop = newpos.y;
break;
case Highlight.OuterBottom:
if (newpos.y < outertop) outerbottom = newpos.y;
break;
// Outer corners
case Highlight.OuterTopLeft:
if (newpos.x < outerright) outerleft = newpos.x;
if (newpos.y > outerbottom) outertop = newpos.y;
break;
case Highlight.OuterTopRight:
if (newpos.x > outerleft) outerright = newpos.x;
if (newpos.y > outerbottom) outertop = newpos.y;
break;
case Highlight.OuterBottomLeft:
if (newpos.x < outerright) outerleft = newpos.x;
if (newpos.y < outertop) outerbottom = newpos.y;
break;
case Highlight.OuterBottomRight:
if (newpos.x > outerleft) outerright = newpos.x;
if (newpos.y < outertop) outerbottom = newpos.y;
break;
}
UpdateLinesAndPoints();
}
public List<Vector2D> GetRelocatePositions(int numsectors)
{
List<Vector2D> positions = new List<Vector2D>();
BlockMap<BlockEntry> blockmap = CreateBlockmap(true);
int margin = (int)((gridsize - sectorsize) / 2);
for (int x = (int)outerleft; x < (int)outerright; x += (int)gridsize)
{
for (int y = (int)outertop; y > (int)outerbottom; y -= (int)gridsize)
{
List<BlockEntry> blocks = blockmap.GetLineBlocks(
new Vector2D(x + 1, y - 1),
new Vector2D(x + gridsize - 1, y - gridsize + 1)
);
// The way our blockmap is built and queried we will always get exactly one block
if (blocks[0].Sectors.Count == 0)
{
positions.Add(new Vector2D(x + margin, y - margin));
numsectors--;
}
if (numsectors == 0)
return positions;
}
}
throw new Exception("Not enough space for control sector relocation");
}
public List<DrawnVertex> GetNewControlSectorVertices()
{
BlockMap<BlockEntry> blockmap = CreateBlockmap();
int margin = (int)((gridsize - sectorsize) / 2);
// find position for new control sector
for (int x = (int)outerleft; x < (int)outerright; x += (int)gridsize)
{
for (int y = (int)outertop; y > (int)outerbottom; y -= (int)gridsize)
{
List<BlockEntry> blocks = blockmap.GetLineBlocks(
new Vector2D(x + 1, y - 1),
new Vector2D(x + gridsize - 1, y - gridsize + 1)
);
// The way our blockmap is built and queried we will always get exactly one block
if (blocks[0].Sectors.Count == 0)
{
List<DrawnVertex> dv = new List<DrawnVertex>();
Point p = new Point(x + margin, y - margin);
dv.Add(SectorVertex(p.X, p.Y));
dv.Add(SectorVertex(p.X + BuilderPlug.Me.ControlSectorArea.SectorSize, p.Y));
dv.Add(SectorVertex(p.X + BuilderPlug.Me.ControlSectorArea.SectorSize, p.Y - BuilderPlug.Me.ControlSectorArea.SectorSize));
dv.Add(SectorVertex(p.X, p.Y - BuilderPlug.Me.ControlSectorArea.SectorSize));
dv.Add(SectorVertex(p.X, p.Y));
return dv;
}
}
}
throw new Exception("No space left for control sectors");
}
public bool Inside(float x, float y)
{
return Inside(new Vector2D(x, y));
}
public bool Inside(Vector2D pos)
{
if (pos.x > outerleft && pos.x < outerright && pos.y < outertop && pos.y > outerbottom)
return true;
return false;
}
public bool OutsideOuterBounds(float x, float y)
{
return OutsideOuterBounds(new Vector2D(x, y));
}
public bool OutsideOuterBounds(Vector2D pos)
{
if(pos.x < outerleft || pos.x > outerright || pos.y > outertop || pos.y < outerbottom)
return true;
return false;
}
// Aligns the area to the grid, expanding the area if necessary
private RectangleF AlignAreaToGrid(RectangleF area)
{
List<float> f = new List<float>
{
area.Left,
area.Top,
area.Right,
area.Bottom
};
for (int i = 0; i < f.Count; i++)
{
if (f[i] < 0)
f[i] = (float)Math.Floor(f[i] / gridsize) * gridsize;
else
f[i] = (float)Math.Ceiling(f[i] / gridsize) * gridsize;
}
float l = f[0];
float t = f[1];
float r = f[2];
float b = f[3];
return new RectangleF(l, t, r - l, b - t);
}
private BlockMap<BlockEntry> CreateBlockmap()
{
return CreateBlockmap(false);
}
private BlockMap<BlockEntry> CreateBlockmap(bool ignorecontrolsectors)
{
// Make blockmap
RectangleF area = MapSet.CreateArea(General.Map.Map.Vertices);
area = MapSet.IncreaseArea(area, new Vector2D(outerleft, outertop));
area = MapSet.IncreaseArea(area, new Vector2D(outerright, outerbottom));
area = AlignAreaToGrid(area);
BlockMap<BlockEntry> blockmap = new BlockMap<BlockEntry>(area, (int)gridsize);
if (ignorecontrolsectors)
{
foreach (Sector s in General.Map.Map.Sectors)
{
// Managed control sectors have the custom UDMF field "user_managed_3d_floor" set to true
// So if the field is NOT set, add the sector to the blockmap
if (s.Fields.GetValue("user_managed_3d_floor", false) == false)
blockmap.AddSector(s);
}
}
else
{
blockmap.AddSectorsSet(General.Map.Map.Sectors);
}
return blockmap;
}
public void Edit()
{
ControlSectorAreaConfig csacfg = new ControlSectorAreaConfig(this);
csacfg.ShowDialog((Form)General.Interface);
}
public void SaveConfig()
{
ListDictionary config = new ListDictionary();
config.Add("usecustomtagrange", usecustomtagrange);
if (usecustomtagrange)
{
config.Add("firsttag", firsttag);
config.Add("lasttag", lasttag);
}
config.Add("outerleft", outerleft);
config.Add("outerright", outerright);
config.Add("outertop", outertop);
config.Add("outerbottom", outerbottom);
General.Map.Options.WritePluginSetting("controlsectorarea", config);
}
public void LoadConfig()
{
ListDictionary config = (ListDictionary)General.Map.Options.ReadPluginSetting("controlsectorarea", new ListDictionary());
usecustomtagrange = General.Map.Options.ReadPluginSetting("controlsectorarea.usecustomtagrange", false);
firsttag = General.Map.Options.ReadPluginSetting("controlsectorarea.firsttag", 0);
lasttag = General.Map.Options.ReadPluginSetting("controlsectorarea.lasttag", 0);
outerleft = General.Map.Options.ReadPluginSetting("controlsectorarea.outerleft", outerleft);
outerright = General.Map.Options.ReadPluginSetting("controlsectorarea.outerright", outerright);
outertop = General.Map.Options.ReadPluginSetting("controlsectorarea.outertop", outertop);
outerbottom = General.Map.Options.ReadPluginSetting("controlsectorarea.outerbottom", outerbottom);
UpdateLinesAndPoints();
}
public int GetNewSectorTag(List<int> tagblacklist)
{
List<int> usedtags = new List<int>();
if (usecustomtagrange)
{
for (int i = firsttag; i <= lasttag; i++)
{
if (!tagblacklist.Contains(i) && General.Map.Map.GetSectorsByTag(i).Count == 0)
return i;
}
throw new Exception("No free tags in the custom range between " + firsttag.ToString() + " and " + lasttag.ToString() + ".");
}
return General.Map.Map.GetNewTag(tagblacklist);
}
public int GetNewLineID()
{
return General.Map.Map.GetNewTag();
}
// Turns a position into a DrawnVertex and returns it
private DrawnVertex SectorVertex(float x, float y)
{
DrawnVertex v = new DrawnVertex();
v.stitch = true;
v.stitchline = true;
v.pos = new Vector2D((float)Math.Round(x, General.Map.FormatInterface.VertexDecimals), (float)Math.Round(y, General.Map.FormatInterface.VertexDecimals));
return v;
}
private DrawnVertex SectorVertex(Vector2D v)
{
return SectorVertex(v.x, v.y);
}
static int GCD(int[] numbers)
{
return numbers.Aggregate(GCD);
}
static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
#endregion
}
}