-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDrawTextMode.cs
636 lines (539 loc) · 24.2 KB
/
DrawTextMode.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using CodeImp.DoomBuilder;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Editing;
using CodeImp.DoomBuilder.Actions;
using CodeImp.DoomBuilder.Geometry;
using CodeImp.DoomBuilder.Map;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;
using CodeImp.DoomBuilder.Windows;
using TriDelta.OpenType;
using CodeImp.DoomBuilder.Controls;
namespace TriDelta.DrawTextMode {
internal delegate void ModeChangedEvent(DrawTextMode mode);
[EditMode(
DisplayName = "Draw Text Mode",
SwitchAction = "drawtextmode",
ButtonImage = "icon.png",
ButtonOrder = 502,
ButtonGroup = "000_editing",
UseByDefault = true,
SafeStartMode = false,
Volatile = true
)]
public class DrawTextMode : ClassicMode {
private const float LINE_THICKNESS = 0.8f;
private const float GRIP_SIZE = 9.0f;
BuilderPlug plug;
List<List<DrawnVertex>> shapecache;
List<PointF> shapecontrols;
Docker dockerDrawText;
DrawTextPanel panel;
private LineLengthLabel labelGuideLength;
private ControlHandle handleInner;
private ControlHandle handleOuter;
private ControlHandle handleCurrent;
private PixelColor pcLineSnap = General.Colors.Selection;
private PixelColor pcLineFree = General.Colors.Highlight;
private PixelColor pcHandleFill = General.Colors.Background;
private bool snapguidetogrid;
private bool isCreating;
internal event ModeChangedEvent ModeChanged;
int HighlightMode = 0;
ControlHandle HighlightHandle;
public BuilderPlug Plug {
get { return plug; }
}
public DrawTextMode() {
plug = BuilderPlug.Me;
plug.EditMode = this;
labelGuideLength = new LineLengthLabel(true);
shapecache = new List<List<DrawnVertex>>();
shapecontrols = new List<PointF>();
panel = new DrawTextPanel(this);
dockerDrawText = new Docker("drawtextmode", "Draw Text", panel);
}
public string Text {
get { return plug.DisplayText; }
set {
if (value != plug.DisplayText) {
plug.DisplayText = value;
Update();
}
}
}
public float TextSize {
get { return plug.Size; }
set {
if (value != plug.Size) {
plug.Size = value;
if (plug.Size < 0)
plug.Size = 0;
if (plug.Font != null)
plug.Font.PointSize = value;
Update();
if (ModeChanged != null)
ModeChanged(this);
}
}
}
public OpenFont TextFont {
get { return plug.Font; }
set {
if (value != plug.Font) {
plug.Font = value;
if (value != null) {
value.PointSize = plug.Size;
value.Quality = plug.CurveQuality;
}
Update();
}
}
}
public int Quality {
get { return plug.CurveQuality; }
set {
if (value != plug.CurveQuality) {
plug.CurveQuality = value;
if (plug.Font != null)
plug.Font.Quality = value;
Update();
}
}
}
public float Tolerance {
get { return plug.Tolerance; }
set {
if (value != plug.Tolerance) {
plug.Tolerance = value;
if (plug.Font != null)
plug.Font.Tolerance = value;
Update();
}
}
}
public PlotMode DrawMode {
get { return plug.PlotMode; }
set {
if (value != plug.PlotMode) {
plug.PlotMode = value;
Update();
}
}
}
public bool DebugMode {
get { return plug.DebugMode; }
set {
if (value != plug.DebugMode) {
plug.DebugMode = value;
Update();
}
}
}
public TextAlignment Alignment {
get { return plug.TextAlignment; }
set {
if (value != plug.TextAlignment) {
plug.TextAlignment = value;
Update();
}
}
}
public float TextSpacing {
get { return plug.TextSpacing; }
set {
if (value != plug.TextSpacing) {
plug.TextSpacing = value;
Update();
}
}
}
[BeginAction("incfontsize")]
public void IncreaseFontSize() {
if (handleInner != null && handleOuter != null)
TextSize++;
}
[BeginAction("decfontsize")]
public void DecreaseFontSize() {
if (handleInner != null && handleOuter != null)
TextSize--;
}
public override void OnEngage() {
base.OnEngage();
General.Interface.AddDocker(dockerDrawText);
renderer.SetPresentation(Presentation.Standard);
}
public override void OnDisengage() {
plug.Save();
General.Interface.RemoveDocker(dockerDrawText);
base.OnDisengage();
}
public override void OnRedrawDisplay() {
renderer.RedrawSurface();
if (renderer.StartPlotter(true)) {
renderer.PlotLinedefSet(General.Map.Map.Linedefs);
renderer.PlotVerticesSet(General.Map.Map.Vertices);
renderer.Finish();
}
if (renderer.StartThings(true)) {
renderer.RenderThingSet(General.Map.Map.Things, 1.0f);
renderer.Finish();
}
Render();
}
public void Update() {
snapguidetogrid = General.Interface.ShiftState ^ General.Interface.SnapToGrid;
//only create geometry if both control handles have been set and a font has been selected
if (handleInner != null && handleOuter != null && plug.Font != null) {
PointF origin = new PointF(handleInner.Position.x, handleInner.Position.y);
PointF target = new PointF(handleOuter.Position.x, handleOuter.Position.y);
//plot the string and convert the shape points into something doombuilder can use
shapecache.Clear();
List<PointF[]> lines = plug.Font.PlotString(plug.DisplayText, origin, target, plug.PlotMode, plug.TextAlignment, plug.TextSpacing);
if (lines != null)
shapecache.AddRange(ConvertPoints(lines));
shapecontrols = plug.Font.LastControlList;
}
Render();
}
public void Render() {
if (renderer.StartOverlay(true)) {
float vsize = ((float)renderer.VertexSize + 1.0f) / renderer.Scale;
//draw a guide circle when drawing circular text
if (plug.PlotMode == PlotMode.Circle && handleInner != null && handleOuter != null) {
//Calculate the angle and circle starting point
Vector2D delta = handleOuter.Position - handleInner.Position;
int sides = 32;
float length = delta.GetLength();
float originRads = (float)Math.Atan2(handleInner.Position.y - handleOuter.Position.y, handleInner.Position.x - handleOuter.Position.x);
float pointRads = (float)((Math.PI / (double)sides) * 2);
Vector2D first, last, current;
first = last = new Vector2D(
handleInner.Position.x - (float)(Math.Cos(originRads) * length),
handleInner.Position.y - (float)(Math.Sin(originRads) * length)
);
for (int i = 0; i < sides; i++) {
//calculate where the vertex should go, based on the number of segments
float rads = originRads + (pointRads * (float)i);
current = new Vector2D(
handleInner.Position.x - (float)(Math.Cos(rads) * length),
handleInner.Position.y - (float)(Math.Sin(rads) * length)
);
//measure the segment for the total length display
renderer.RenderLine(last, current, LINE_THICKNESS, pcLineFree, true);
last = current;
}
renderer.RenderLine(last, first, LINE_THICKNESS, pcLineFree, true);
}
//Render the text glyph outlines
if (shapecache.Count > 0) {
Vector2D first, last;
float heading;
float arrowsize = GRIP_SIZE / renderer.Scale;
bool debugmode = plug.DebugMode;
foreach (List<DrawnVertex> shape in shapecache) {
if (shape.Count > 0) {
//draw the preview linedefs
first = last = shape[0].pos;
for (int i = 1; i < shape.Count - 1; i++) {
renderer.RenderLine(last, shape[i].pos, LINE_THICKNESS, pcLineFree, true);
//DEBUG MODE: draw an arrow head to indicate the draw direction
if (debugmode) {
heading = (float)Math.Atan2(shape[i].pos.y - last.y, shape[i].pos.x - last.x); //calculate the line angle
RenderArrowHead(shape[i].pos, heading, arrowsize);
}
last = shape[i].pos;
}
renderer.RenderLine(last, first, LINE_THICKNESS, pcLineFree, true);
//draw the preview vertices
foreach(DrawnVertex p in shape)
RenderPoint(p.pos, vsize, pcLineSnap);
//DEBUG MODE: mark the shape start
if (debugmode)
RenderPoint(shape[0].pos, vsize * 1.3f, PixelColor.FromColor(Color.Green));
}
}
//DEBUG MODE: draw the bezier curve control handle locations
if (debugmode && shapecontrols.Count > 0) {
float controlsize = GRIP_SIZE / renderer.Scale;
foreach (PointF point in shapecontrols) {
RectangleF rect = new RectangleF(point.X - controlsize * 0.5f, point.Y - controlsize * 0.5f, controlsize, controlsize);
renderer.RenderRectangleFilled(rect, General.Colors.Colors[ColorCollection.VERTICES], true);
renderer.RenderRectangle(rect, 2, General.Colors.DarkColors[ColorCollection.VERTICES], true);
}
}
}
//Render Guide
if (handleInner != null) {
PixelColor handlecolor = snapguidetogrid ? pcLineSnap : pcLineFree;
PixelColor highlightcolor = General.Colors.Vertices; //snapguidetogrid ? General.Colors.BrightColors[ColorCollection.HIGHLIGHT] : General.Colors.BrightColors[ColorCollection.SELECTION];
//guide line
renderer.RenderLine(handleInner.Position, handleOuter.Position, LINE_THICKNESS, handlecolor, true);
float gripsize = GRIP_SIZE / renderer.Scale;
//size text
labelGuideLength.Start = handleInner.Position;
labelGuideLength.End = handleOuter.Position;
renderer.RenderText(labelGuideLength.TextLabel);
//control handles
RectangleF handleRect = new RectangleF(handleInner.Position.x - gripsize * 0.5f, handleInner.Position.y - gripsize * 0.5f, gripsize, gripsize);
renderer.RenderRectangleFilled(handleRect, pcHandleFill, true);
renderer.RenderRectangle(handleRect, 2, HighlightMode == 1 && HighlightHandle == handleInner || handleCurrent == handleInner ? highlightcolor : handlecolor, true);
handleRect = new RectangleF(handleOuter.Position.x - gripsize * 0.5f, handleOuter.Position.y - gripsize * 0.5f, gripsize, gripsize);
renderer.RenderRectangleFilled(handleRect, pcHandleFill, true);
renderer.RenderRectangle(handleRect, 2, HighlightMode == 1 && HighlightHandle == handleOuter || handleCurrent == handleOuter ? highlightcolor : handlecolor, true);
}
renderer.Finish();
}
renderer.Present();
}
private void RenderPoint(DrawnVertex p, float size, PixelColor color) {
RenderPoint(p.pos, size, color);
}
private void RenderPoint(Vector2D p, float size, PixelColor color) {
renderer.RenderRectangleFilled(new RectangleF(p.x - size, p.y - size, size * 2.0f, size * 2.0f), color, true);
}
private void RenderArrowHead(Vector2D p, float Heading, float Size) {
float x2, y2;
//draw arrow head
y2 = (float)(Math.Sin(Heading + 10f) * Size);
x2 = (float)(Math.Cos(Heading + 10f) * Size);
renderer.RenderLine(p, new Vector2D(p.x + x2, p.y + y2), LINE_THICKNESS, pcLineFree, true);
y2 = (float)(Math.Sin(Heading - 10f) * Size);
x2 = (float)(Math.Cos(Heading - 10f) * Size);
renderer.RenderLine(p, new Vector2D(p.x + x2, p.y + y2), LINE_THICKNESS, pcLineFree, true);
}
private List<List<DrawnVertex>> ConvertPoints(List<PointF[]> shapeList) {
DrawnVertex v;
List<List<DrawnVertex>> shapes = new List<List<DrawnVertex>>();
List<DrawnVertex> shape;
foreach (PointF[] points in shapeList) {
shape = new List<DrawnVertex>();
for (int i = 0; i < points.Length; i++) {
v = new DrawnVertex();
v.stitch = true;
v.stitchline = true;
v.pos.x = points[i].X;
v.pos.y = points[i].Y;
shape.Add(v);
}
shape.Add(shape[0]); //close
shapes.Add(shape);
}
return shapes;
}
protected override void OnEditBegin() {
base.OnEditBegin();
if (shapecache.Count == 0) {
//this is a brand new circle, so setup the control handles and do initial shape creation and rendering
isCreating = true;
snapguidetogrid = General.Interface.ShiftState ^ General.Interface.SnapToGrid; //if control is held then only flip the circle
//initialize the guide line
handleInner = new ControlHandle();
handleOuter = new ControlHandle();
handleInner.Position = handleOuter.Position = GetCurrentPosition();
Update();
} else if (handleInner != null) {
//if we are already in preview mode, then only allow the control handles to be dragged
float gripsize = GRIP_SIZE / renderer.Scale;
if (handleOuter.isHovered(MouseMapPos, gripsize)) {
handleCurrent = handleOuter;
General.Interface.SetCursor(Cursors.Cross);
return;
} else if (handleInner.isHovered(MouseMapPos, gripsize)) {
handleCurrent = handleInner;
General.Interface.SetCursor(Cursors.Cross);
return;
}
}
}
protected override void OnEditEnd() {
base.OnEditEnd();
if (isCreating) {
//if the opening edit was cancelled before letting go of the mouse then do no further processing
isCreating = false;
handleOuter.Position = GetCurrentPosition();
if ((handleOuter.Position - handleInner.Position).GetLength() == 0) {
handleInner = null;
handleOuter = null;
return;
}
Update();
} else {
//since we were just dragging an existing handle, just let go of it
General.Interface.SetCursor(Cursors.Default);
handleCurrent = null;
}
}
protected override void OnDragStart(MouseEventArgs e) {
base.OnDragStart(e);
//this differs from "EditStart", as this is a PRIMARY mouse click, rather than a SECONDARY one.
if (handleInner != null) {
float gripsize = GRIP_SIZE / renderer.Scale;
if (handleOuter.isHovered(MouseDownMapPos, gripsize)) {
handleCurrent = handleOuter;
General.Interface.SetCursor(Cursors.Cross);
return;
} else if (handleInner.isHovered(MouseDownMapPos, gripsize)) {
handleCurrent = handleInner;
General.Interface.SetCursor(Cursors.Cross);
return;
}
}
handleCurrent = null;
}
protected override void OnDragStop(MouseEventArgs e) {
base.OnDragStop(e);
General.Interface.SetCursor(Cursors.Default);
handleCurrent = null;
}
public override void OnMouseMove(MouseEventArgs e) {
base.OnMouseMove(e);
float gripsize = GRIP_SIZE / renderer.Scale;
if (handleCurrent != null) {
handleCurrent.Position = GetCurrentPosition();
Update();
} else if (isCreating) {
handleOuter.Position = GetCurrentPosition();
Update();
} else if (handleOuter != null && handleOuter.isHovered(MouseMapPos, gripsize)) {
if (HighlightHandle != handleOuter) {
General.Interface.SetCursor(Cursors.Hand);
HighlightMode = 1;
HighlightHandle = handleOuter;
Render();
}
} else if (handleInner != null && handleInner.isHovered(MouseMapPos, gripsize)) {
if (HighlightHandle != handleInner) {
General.Interface.SetCursor(Cursors.Hand);
HighlightMode = 1;
HighlightHandle = handleInner;
Render();
}
} else {
if (HighlightMode > 0) {
General.Interface.SetCursor(Cursors.Default);
HighlightMode = 0;
HighlightHandle = null;
Render();
}
}
}
public override void OnKeyUp(KeyEventArgs e) {
base.OnKeyUp(e);
Update();
}
public override void OnKeyDown(KeyEventArgs e) {
base.OnKeyDown(e);
Update();
if (handleInner != null && e.KeyCode == Keys.Enter)
OnAccept();
}
public override void OnCancel() {
base.OnCancel();
isCreating = false;
handleInner = null;
handleOuter = null;
shapecache.Clear();
Render();
}
public override void OnAccept() {
Cursor.Current = Cursors.AppStarting;
General.Settings.FindDefaultDrawSettings();
// When points have been drawn
if (shapecache.Count > 0) {
// Make undo for the draw
General.Map.UndoRedo.CreateUndo("Draw Text");
// Clear selection
General.Map.Map.ClearAllSelected();
// Make the drawing
foreach (List<DrawnVertex> shape in shapecache) {
//if the user holds down ALT while creating, assume they want a "guide" for positioning other map elements. A guide will not split linedefs.
if (!General.Interface.AutoMerge || General.Interface.AltState) {
int j = shape.Count;
DrawnVertex v;
while (j-- > 0) {
v = shape[j];
v.stitchline = false; //don't split any linedefs
//don't complete the shape if ALT mode, to prevent sector creation and background fill
if (General.Interface.AltState)
v.stitch = false;
shape[j] = v;
}
}
// Make the drawing
CodeImp.DoomBuilder.Geometry.Tools.DrawLines(shape);
General.Map.Map.SelectMarkedLinedefs(true, true);
}
// Snap to map format accuracy
General.Map.Map.SnapAllToAccuracy();
// Update cached values
General.Map.Map.Update();
// Update the used textures
General.Map.Data.UpdateUsedTextures();
// Map is changed
General.Map.IsChanged = true;
}
// Done
Cursor.Current = Cursors.Default;
isCreating = false;
handleInner = null;
handleOuter = null;
shapecache.Clear();
OnRedrawDisplay();
}
private Vector2D GetCurrentPosition() {
Vector2D vm = MouseMapPos;
float vrange = 20f / renderer.Scale;
// Try the nearest vertex
Vertex nv = General.Map.Map.NearestVertexSquareRange(vm, vrange);
if (nv != null)
return nv.Position;
// Try the nearest linedef
Linedef nl = General.Map.Map.NearestLinedefRange(vm, vrange);
if (nl != null) {
// Snap to grid?
if (snapguidetogrid) {
// Get grid intersection coordinates
List<Vector2D> coords = nl.GetGridIntersections();
// Find nearest grid intersection
bool found = false;
float found_distance = float.MaxValue;
Vector2D found_coord = new Vector2D();
foreach (Vector2D v in coords) {
Vector2D delta = vm - v;
if (delta.GetLengthSq() < found_distance) {
found_distance = delta.GetLengthSq();
found_coord = v;
found = true;
}
}
if (found)
return found_coord;
} else {
return nl.NearestOnLine(vm);
}
}
//Just get the current mouse location instead
if (snapguidetogrid)
return General.Map.Grid.SnappedToGrid(vm);
return vm;
}
}
//holder class for the guide drag handles
internal class ControlHandle {
public Vector2D Position;
public ControlHandle() { }
public ControlHandle(Vector2D position) {
this.Position = position;
}
public bool isHovered(Vector2D position, float size) {
return position.x <= this.Position.x + size && position.x >= this.Position.x - size && position.y <= this.Position.y + size && position.y >= this.Position.y - size;
}
}
}