-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometer.c
2073 lines (1860 loc) · 66.6 KB
/
geometer.c
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _CRT_SECURE_NO_WARNINGS
#include "geometer.h"
#include <fonts.c>
#include "geometer_debug.c"
// TODO:
// =====
//
// - Find (and colour) lines intersecting at a given point
// - Undos
// - should saves remove 'superfluous' actions - i.e. take out remove points/indirections?
// - undo history (show overall/with layers)
// - undo by absolute and layer order
// - Change storage of intersections, so they don't all need to be recomputed on changes
// - only compute those from shapesnear mouse?
// - Spatially partition(?) shapes
// - Layers
// - Togglable layers
// - Infinite layers
// - Scrollable layers
// - For fast movements, make sweep effect, rather than ugly multiple line effect
// - Deal with perfect overlaps that aren't identical (i.e. one line/arc is longer)
// - Resizable windo (maintain centre vs maintain absolute position)
// - Constraint system? Macros? Paid version?
// - Make custom cursors
// - F keys to open toolbars (layers/minimap/action history/...)
// - ... or have them pop up by 'slamming' mouse to canvas edge... or both
// - Copy and paste (between running apps - clipboard?) without NEEDING clipboard
// - What else between apps - lengths?
// - Consolidate history
// - Cancel actions with other mouseclick e.g. LMB while extending line with RMB
// - Select shapes, then move that shape's points - extend/arc
// - select via intersection with line allows weird angle selections...
// - Add arc creation behaviour to dragging
// - Regression test actions by comparing end state to version reconstructed by actions
// UI that allows modification:
// - LMB-drag - quick seg
// - LMB - start drawing
// (Alt + anything for point version)
// - LMB on pt - quick circle (allows double click)
// - LMB drag on pt - set length
// - LMB - circle
// - LMB-drag - arc
// - RMB drag on point - set perpendicular
// - RMB - seg
// - RMB-drag - extend seg
// - RMB - select point/shape
// - RMB-drag marquee-select points/shapes
// - ??? - move to background layer/another layer...
// Once points are selected:
// - Alt-RMB for +/- selection
// OR
// - RMB for + selection, Alt-RMB for - selection
// ...
// CONTROLS: ////////////////////////////
#define C_Cancel Keyboard.Esc
#define C_StartShape Mouse.LMB
#define C_Arc Mouse.LMB
#define C_Line Mouse.RMB
#define C_Length Mouse.LMB
// divide length 1-0
// mult length Alt + 1-0
// get store length a-z,A-Z
// set store length Alt + a-z,A-Z
#define C_NoSnap Keyboard.Shift
#define C_ShapeLock Keyboard.Ctrl
// IDEA (ui): would this be better as full stop? obvious semantic connection
// further from resting position, but probably used infrequently..?
#define C_PointOnly Keyboard.Alt
#define C_Drag Mouse.LMB
#define C_Select Mouse.RMB
#define C_SelectMod Keyboard.Alt
#define C_BasisSet Mouse.RMB
#define C_BasisMod Keyboard.Space
#define C_Pan Mouse.MMB
#define C_PanDown Keyboard.Down
#define C_PanUp Keyboard.Up
#define C_PanLeft Keyboard.Left
#define C_PanRight Keyboard.Right
#define C_PanMod Keyboard.Space
#define C_Zoom Mouse.ScrollV
#define C_ZoomIn Keyboard.PgUp
#define C_ZoomOut Keyboard.PgDn
#define C_Delete Keyboard.Del
#define C_Reset Keyboard.Backspace
#define C_CanvasHome Keyboard.Home
#define C_DebugInfo Keyboard.End
#define C_PrevLength Keyboard.Tab
// TODO: ctrl-tab? mouse special buttons?
#define C_LayerMod Keyboard.Ctrl
#define C_LayerRev Keyboard.Shift
#define C_LayerChange Keyboard.Tab // TODO: change to 1?
#define C_LayerDrawer Keyboard.T
/////////////////////////////////////////
#define Col_DrawPreview BLUE
#define Col_Preview LIGHT_GREY
#define Col_ArcCircle LIGHT_GREY
#define Col_ArcLines LIGHT_GREY
#define Col_LineExtend LIGHT_GREY
#define Col_Shape BLACK
#define Col_ShapeOffLayer LIGHT_GREY
#define Col_Perp LIGHT_GREY
#define Col_SetLength LIGHT_GREY
#define Col_BasisLine RED
#define Col_ThumbOutline LIGHT_GREY
#define Col_ThumbSelected BLUE
#define Col_Text BLACK
#define Col_Pt LIGHT_GREY
#define Col_ActivePt RED
#define Col_SelectedPt ORANGE
#define Col_UnselectedPt MAGENTA
#define Col_SelectBox GREY
v2 gDebugV2;
v2 gDebugPoint;
#define DRAW_FN(name)\
fn_##name *name;
DRAW_FNS
#undef DRAW_FN
internal inline void
#define DrawCrosshair GeoDrawCrosshair
DrawCrosshair(draw_buffer *Draw, v2 Centre, f32 Radius, colour Colour)
{
v2 X1 = {2.f, 0.f};
v2 Y1 = {0.f, 2.f};
v2 XRad = {Radius, 0.f};
v2 YRad = {0.f, Radius};
draw_buffer tDraw = *Draw;
tDraw.StrokeWidth = 1.f;
DrawSeg(&tDraw, V2Sub(Centre, X1), V2Sub(Centre, XRad), Colour);
DrawSeg(&tDraw, V2Add(Centre, X1), V2Add(Centre, XRad), Colour);
DrawSeg(&tDraw, V2Add(Centre, Y1), V2Add(Centre, YRad), Colour);
DrawSeg(&tDraw, V2Sub(Centre, Y1), V2Sub(Centre, YRad), Colour);
}
internal inline void
DrawClosestPtOnSegment(draw_buffer *Draw, v2 po, v2 lipoA, v2 lipoB)
{
BEGIN_TIMED_BLOCK;
v2 po1 = ClosestPtOnSegment(po, lipoA, V2Sub(lipoB, lipoA));
DrawCrosshair(Draw, po1, ACTIVE_POINT_RADIUS, RED);
END_TIMED_BLOCK;
}
internal inline void
DrawClosestPtOnCircle(draw_buffer *Draw, v2 po, v2 poFocus, f32 Radius)
{
BEGIN_TIMED_BLOCK;
v2 po1 = ClosestPtOnCircle(po, poFocus, Radius);
DrawCrosshair(Draw, po1, ACTIVE_POINT_RADIUS, RED);
END_TIMED_BLOCK;
}
internal inline void
DrawClosestPtOnArc(draw_buffer *Draw, v2 po, v2 poFocus, v2 poStart, v2 poEnd)
{
BEGIN_TIMED_BLOCK;
v2 po1 = ClosestPtOnArc(po, poFocus, poStart, poEnd);
DrawCrosshair(Draw, po1, ACTIVE_POINT_RADIUS, RED);
END_TIMED_BLOCK;
}
internal inline void
DrawActivePoint(draw_buffer *Draw, v2 po, colour Col)
{
BEGIN_TIMED_BLOCK;
draw_buffer tDraw = *Draw;
tDraw.StrokeWidth = 1.f;
DrawCircleFill(&tDraw, po, POINT_RADIUS, Col);
DrawCircleLine(&tDraw, po, ACTIVE_POINT_RADIUS, Col);
END_TIMED_BLOCK;
}
internal inline void
DrawArcFromPoints(draw_buffer *Draw, v2 Centre, v2 A, v2 B, colour Colour)
{
DrawArcLine(Draw, Centre, Dist(Centre, A), V2Sub(A, Centre), V2Sub(B, Centre), Colour);
}
internal inline b32
SameAngle(v2 A, v2 B)
{
b32 Result = WithinEpsilon(Dot(A, B), 1.f, POINT_EPSILON);
return Result;
}
internal inline void
SimpleRedo(state *State)
{
BEGIN_TIMED_BLOCK;
Assert(State->iCurrentAction < State->iLastAction);
ApplyAction(State, Pull(State->maActions, ++State->iCurrentAction));
Assert(State->iCurrentAction <= State->iLastAction);
END_TIMED_BLOCK;
}
internal void
SimpleUndo(state *State)
{
BEGIN_TIMED_BLOCK;
Assert(State->iCurrentAction > 0);
action *Actions = State->maActions.Items;
action Action = Actions[State->iCurrentAction];
switch(USERIFY_ACTION(Action.Kind)) // whether or not it is user-initiated is dealt with by UserUndo
{
case ACTION_Reset:
{ // reapply all actions from scratch
// TODO: add checkpoints so it doesn't have to start right from beginning
uint iCurrentAction = State->iCurrentAction;
for(uint i = Action.Reset.i; i < iCurrentAction; ++i)
{ ApplyAction(State, Actions[i]); }
} break;
case ACTION_RemovePt:
{
POINTSTATUS(Action.Point.ipo) = 1;
} break;
case ACTION_RemoveShape:
{
shape *Shape = &Pull(State->maShapes, Action.Shape.i);
Assert(Shape->Kind < SHAPE_Free);
Shape->Kind = Shape->Kind < SHAPE_Free ? -Shape->Kind : Shape->Kind;
} break;
case ACTION_Basis:
{ // find the previous basis and apply that
basis PrevBasis = DefaultBasis; // in case no previous basis found
for(uint i = State->iCurrentAction; i > 0; --i)
{ // find previous basis
if(Actions[i].Kind == ACTION_Basis)
{
PrevBasis = Actions[i].Basis;
break;
}
}
SetBasis(State, PrevBasis);
} break;
case ACTION_Segment:
case ACTION_Circle:
case ACTION_Arc:
{
Pull(State->maShapes, Action.Shape.i).Kind = SHAPE_Free;
if(Action.Shape.i == State->iLastShape)
{
--State->iLastShape;
PopDiscard(&State->maShapes);
}
else
{ Assert(!"TODO: undo shape additions mid-array"); }
// does anything actually need to be done?
} break;
case ACTION_Point:
{
Pull(State->maPointLayer, Action.Point.ipo) = 0;
if(Action.Point.ipo == State->iLastPoint)
{
--State->iLastPoint;
PopDiscard(&State->maPoints);
PopDiscard(&State->maPointStatus);
PopDiscard(&State->maPointLayer);
}
else
{ Assert(!"TODO: undo point additions mid-array"); }
// does anything actually need to be done?
} break;
case ACTION_Move:
{
/* POINTS(Action.Move.ipo[0]) = V2Sub(POINTS(Action.Move.ipo[0]), Action.Move.Dir); */
/* if(Action.Move.ipo[1]) */
/* { POINTS(Action.Move.ipo[1]) = V2Sub(POINTS(Action.Move.ipo[1]), Action.Move.Dir); } */
} break;
default:
{
Assert(!"Unknown/invalid action type");
}
}
--State->iCurrentAction;
Assert(State->iCurrentAction <= State->iLastAction);
#if INTERNAL && DEBUG_LOG_ACTIONS
LogActionsToFile(State, "ActionLog.txt");
#endif
END_TIMED_BLOCK;
}
internal inline v2
ExtendSegment(v2 poStart, v2 poDir, v2 poLength)
{
v2 LineAxis = Norm(V2Sub(poDir, poStart));
v2 RelLength = V2Sub(poLength, poStart);
// Project RelLength onto LineAxis
f32 ExtendLength = Dot(LineAxis, RelLength);
v2 Result = V2WithDist(poStart, poDir, ExtendLength);
return Result;
}
internal uint
ClosestPtIntersectingCircle(v2 *Points, shape *Shapes, uint iLastShape, v2 P, v2 TestFocus, f32 TestRadius, v2 *poClosest)
{
b32 IsSet = 0;
v2 poIntersect1 = ZeroV2, poIntersect2 = ZeroV2;
uint cTotalIntersects = 0;
for(uint iShape = 1; iShape <= iLastShape; ++iShape)
{
shape Shape = Shapes[iShape];
uint cIntersects = 0;
switch(Shape.Kind)
{ // find the intersects with a circle from poSaved to mouse
case SHAPE_Circle:
{
v2 poFocus = Points[Shape.Circle.ipoFocus];
f32 Radius = Dist(poFocus, Points[Shape.Circle.ipoRadius]);
cIntersects = IntersectCircles(TestFocus, TestRadius, poFocus, Radius,
&poIntersect1, &poIntersect2);
} break;
case SHAPE_Arc:
{
v2 poFocus = Points[Shape.Arc.ipoFocus];
v2 poStart = Points[Shape.Arc.ipoStart];
v2 poEnd = Points[Shape.Arc.ipoEnd];
f32 Radius = Dist(poFocus, poStart);
cIntersects = IntersectCircleArc(TestFocus, TestRadius, poFocus, Radius, poStart, poEnd,
&poIntersect1, &poIntersect2);
} break;
case SHAPE_Segment:
{
v2 po = Points[Shape.Line.P1];
v2 Dir = V2Sub(Points[Shape.Line.P2], po);
cIntersects = IntersectSegmentCircle(po, Dir, TestFocus, TestRadius,
&poIntersect1, &poIntersect2);
} break;
default: { /* do nothing */ }
}
cTotalIntersects += cIntersects;
// update closest candidate
if((cIntersects && DistSq(P, poIntersect1) < DistSq(P, *poClosest)) || !IsSet)
{ *poClosest = poIntersect1; IsSet = 1; }
if(cIntersects == 2 && DistSq(P, poIntersect2) < DistSq(P, *poClosest))
{ *poClosest = poIntersect2; }
}
// only possibly true for first shape
if(!IsSet) { *poClosest = ClosestPtOnCircle(P, TestFocus, TestRadius); }
return cTotalIntersects;
}
internal uint
ClosestPtIntersectingLine(v2 *Points, shape *Shapes, uint iLastShape, v2 P, v2 TestStart, v2 TestDir, v2 *poClosest)
{
*poClosest = TestStart;
v2 poIntersect1 = ZeroV2, poIntersect2 = ZeroV2;
uint cTotalIntersects = 0;
// TODO (opt): extract to function with function params
for(uint iShape = 1; iShape <= iLastShape; ++iShape)
{
shape Shape = Shapes[iShape];
uint cIntersects = 0;
switch(Shape.Kind)
{ // find the intersects with a line going through poExtend
case SHAPE_Circle:
{
v2 poFocus = Points[Shape.Circle.ipoFocus];
f32 Radius = Dist(poFocus, Points[Shape.Circle.ipoRadius]);
cIntersects = IntersectLineCircle(TestStart, TestDir, poFocus, Radius,
&poIntersect1, &poIntersect2);
} break;
case SHAPE_Arc:
{
v2 poFocus = Points[Shape.Arc.ipoFocus];
v2 poStart = Points[Shape.Arc.ipoStart];
v2 poEnd = Points[Shape.Arc.ipoEnd];
f32 Radius = Dist(poFocus, poStart);
cIntersects = IntersectLineArc(TestStart, TestDir, poFocus, Radius, poStart, poEnd,
&poIntersect1, &poIntersect2);
} break;
case SHAPE_Segment:
{
v2 po = Points[Shape.Line.P1];
v2 Dir = V2Sub(Points[Shape.Line.P2], po);
cIntersects = IntersectLineSegment(TestStart, TestDir, po, Dir, &poIntersect1);
} break;
default: { Assert(!"Unknown shape type"); }
}
cTotalIntersects += cIntersects;
// update closest candidate
if(cIntersects && DistSq(P, poIntersect1) < DistSq(P, *poClosest))
{ *poClosest = poIntersect1; }
if(cIntersects == 2 && DistSq(P, poIntersect2) < DistSq(P, *poClosest))
{ *poClosest = poIntersect2; }
}
return cTotalIntersects;
}
/* internal uint */
/* ClosestPtIntersectingShape(v2 *Points, shape *Shapes, uint iLastShape, v2 P, shape TestShape, v2 *poClosest) */
/* { */
/* *poClosest = TestShape.P[0]; // either focus or line start */
/* v2 poIntersect1 = ZeroV2, poIntersect2 = ZeroV2; */
/* uint cTotalIntersects = 0; */
/* // TODO (opt): extract to function with function params */
/* for(uint iShape = 1; iShape <= iLastShape; ++iShape) */
/* { */
/* shape Shape = Shapes[iShape]; */
/* uint cIntersects = 0; */
/* switch(Shape.Kind) */
/* { // find the intersects with a line going through poExtend */
/* case SHAPE_Circle: */
/* { */
/* v2 poFocus = Points[Shape.Circle.ipoFocus]; */
/* f32 Radius = Dist(poFocus, Points[Shape.Circle.ipoRadius]); */
/* if(TestShape.Kind == SHAPE_Line) */
/* { cIntersects = IntersectLineCircle(TestStart, TestDir, poFocus, Radius, */
/* &poIntersect1, &poIntersect2); } */
/* else if(TestShape.Kind == SHAPE_Circle) */
/* { cIntersects = IntersectCircles(TestFocus, TestRadius, poFocus, Radius, */
/* &poIntersect1, &poIntersect2); } */
/* else { Assert(0); } */
/* } break; */
/* case SHAPE_Arc: */
/* { */
/* v2 poFocus = Points[Shape.Arc.ipoFocus]; */
/* v2 poStart = Points[Shape.Arc.ipoStart]; */
/* v2 poEnd = Points[Shape.Arc.ipoEnd]; */
/* f32 Radius = Dist(poFocus, poStart); */
/* if(TestShape.Kind == SHAPE_Line) */
/* { cIntersects = IntersectLineArc(TestStart, TestDir, poFocus, Radius, poStart, poEnd, */
/* &poIntersect1, &poIntersect2); } */
/* else if(TestShape.Kind == SHAPE_Circle) */
/* { cIntersects = IntersectCircleArc(TestFocus, TestRadius, poFocus, Radius, poStart, poEnd, */
/* &poIntersect1, &poIntersect2); } */
/* else { Assert(0); } */
/* } break; */
/* case SHAPE_Segment: */
/* { */
/* v2 po = Points[Shape.Line.P1]; */
/* v2 Dir = V2Sub(Points[Shape.Line.P2], po); */
/* if(TestShape.Kind == SHAPE_Line) */
/* { cIntersects = IntersectLineSegment(TestStart, TestDir, po, Dir, &poIntersect1); } */
/* else if(TestShape.Kind == SHAPE_Circle) */
/* { cIntersects = IntersectCircles(TestFocus, TestRadius, poFocus, Radius, */
/* &poIntersect1, &poIntersect2); } */
/* else { Assert(0); } */
/* } break; */
/* default: { /1* do nothing *1/ } */
/* } */
/* cTotalIntersects += cIntersects; */
/* // update closest candidate */
/* if(cIntersects && DistSq(P, poIntersect1) < DistSq(P, *poClosest)) */
/* { *poClosest = poIntersect1; } */
/* if(cIntersects == 2 && DistSq(P, poIntersect2) < DistSq(P, *poClosest)) */
/* { *poClosest = poIntersect2; } */
/* } */
/* return cTotalIntersects; */
/* } */
internal inline v2
ChooseCirclePoint(state *State, v2 MouseP, v2 SnapMouseP, b32 ShapeLock)
{ // find point on shape closest to mouse along circumference
v2 poFocus = State->poSelect;
f32 Radius = State->Length;
v2 Result;
if(ShapeLock)
{
uint cIntersects = ClosestPtIntersectingCircle(State->maPoints.Items, State->maShapes.Items,
State->iLastShape, MouseP, poFocus, Radius, &Result);
// TODO: remove? (inc assert)
if(cIntersects == 0) { Result = ClosestPtOnCircle(MouseP, poFocus, Radius); }
/* v2 TestPoint = ClosestPtOnCircle(MouseP, poFocus, Radius); */
/* Assert(cIntersects || V2WithinEpsilon(Result, TestPoint, POINT_EPSILON)); */
DebugReplace("cIntersects: %u\n", cIntersects);
}
else
{
v2 P = V2Equals(SnapMouseP, poFocus) ? MouseP : SnapMouseP;
Result = ClosestPtOnCircle(P, poFocus, Radius);
}
Assert( ! V2WithinEpsilon(Result, poFocus, POINT_EPSILON));
return Result;
}
internal void
DrawAABB(draw_buffer *Draw, aabb AABB, colour Col)
{
// TODO (fix): missing pixel in bottom right
v2 TopLeft = V2(AABB.MinX, AABB.MaxY);
v2 TopRight = V2(AABB.MaxX, AABB.MaxY);
v2 BottomLeft = V2(AABB.MinX, AABB.MinY);
v2 BottomRight = V2(AABB.MaxX, AABB.MinY);
DrawSeg(Draw, TopLeft, TopRight, Col);
DrawSeg(Draw, TopLeft, BottomLeft, Col);
DrawSeg(Draw, TopRight, BottomRight, Col);
DrawSeg(Draw, BottomLeft, BottomRight, Col);
}
internal inline aabb
AABBCanvasToScreen(basis Basis, aabb AABB, v2 ScreenCentre)
{
aabb Result;
Result.Min = V2CanvasToScreen(Basis, AABB.Min, ScreenCentre);
Result.Max = V2CanvasToScreen(Basis, AABB.Max, ScreenCentre);
return Result;
}
internal inline b32
CurrentActionIsByUser(state *State)
{
action Action = Pull(State->maActions, State->iCurrentAction);
b32 Result = Action.Kind >= 0;
return Result;
}
internal inline b32
PointIsSelected(state *State, uint ipo)
{
b32 Result = 0;
foreachf(uint, ipoSelect, State->maSelectedPoints)
{
if(ipoSelect == ipo)
{ Result = 1; break; }
}
return Result;
}
internal void
AdjustMatchingArcPoint(shape_arena Shapes, v2_arena Points, uint ipo)
{
foreachf(shape, Shape, Shapes)
{
if(Shape.Kind == SHAPE_Arc)
{
if(Shape.Arc.ipoStart == ipo)
{
v2 Focus = Pull(Points,Shape.Arc.ipoFocus);
v2 Start = Pull(Points,Shape.Arc.ipoStart);
v2 *End = &Pull(Points,Shape.Arc.ipoEnd);
*End = ClosestPtOnCircle(*End, Focus, Dist(Focus, Start));
}
else if(Shape.Arc.ipoEnd == ipo)
{
v2 Focus = Pull(Points, Shape.Arc.ipoFocus);
v2 *Start = &Pull(Points, Shape.Arc.ipoStart);
v2 End = Pull(Points, Shape.Arc.ipoEnd);
*Start = ClosestPtOnCircle(*Start, Focus, Dist(Focus, End));
}
}
}
}
// this is for the occasions when an arc/circle is fully around the screen
// trying to draw the massive shape is very expensive, and nothing gets drawn
internal b32
ScreenIsInsideCircle(aabb ScreenBB, v2 poSSFocus, f32 SSRadiusSq)
{
b32 Result = 0;
if( DistSq(poSSFocus, ScreenBB.Min) < SSRadiusSq &&
DistSq(poSSFocus, ScreenBB.Max) < SSRadiusSq &&
DistSq(poSSFocus, V2(ScreenBB.MinX, ScreenBB.MaxY)) < SSRadiusSq &&
DistSq(poSSFocus, V2(ScreenBB.MaxX, ScreenBB.MinY)) < SSRadiusSq)
{ Result = 1; }
return Result;
}
#define ToScreen(p) V2CanvasToScreen(Basis, p, ScreenCentre)
internal void
RenderDrawing(draw_buffer Draw, state *State, basis Basis, v2 AreaOffset, v2 AreaSize, uint iLayer, f32 PtRadius)
{
enum { MainCanvasDrawing };
LOG("\tDRAW SHAPES");
shape_arena maShapesNearScreen = State->maShapesNearScreen;
// changes buffer dimensions if software rendering, otherwise does OpenGL scissor
Draw.Buffer = ClipBuffer(Draw.Buffer, AreaOffset, AreaSize);
v2 ScreenCentre = V2Add(AreaOffset, V2Mult(0.5, AreaSize));
AreaSize = V2( (f32)Draw.Buffer.Width, (f32)Draw.Buffer.Height );
if(Draw.Kind == DRAW_Software)
{ ScreenCentre = V2Mult(0.5, AreaSize); }
if(iLayer != MainCanvasDrawing)
{ Draw.StrokeWidth = 1.0f; }
foreachf(shape, Shape, maShapesNearScreen)
{ // DRAW SHAPES
uint ShapeLayer = POINTLAYER(Shape.P[0]);
Assert(ShapeLayer != 0);
if(iLayer == MainCanvasDrawing || ShapeLayer == iLayer)
{
colour LayerColour = BLACK;
if(ShapeLayer != State->iCurrentLayer)
{ LayerColour = PreMultiplyColour(LayerColour, 0.25f); }
switch(Shape.Kind)
{
case SHAPE_Segment:
{
v2 poA = ToScreen(POINTS_OS(Shape.Line.P1));
v2 poB = ToScreen(POINTS_OS(Shape.Line.P2));
DrawSeg(&Draw, poA, poB, LayerColour);
} break;
case SHAPE_Circle:
{
v2 poFocus = ToScreen(POINTS_OS(Shape.Circle.ipoFocus));
v2 poRadius = ToScreen(POINTS_OS(Shape.Circle.ipoRadius));
f32 Radius = Dist(poFocus, poRadius);
Assert(Radius);
DrawCircleLine(&Draw, poFocus, Radius, LayerColour);
} break;
case SHAPE_Arc:
{
v2 poFocus = ToScreen(POINTS_OS(Shape.Arc.ipoFocus));
v2 poStart = ToScreen(POINTS_OS(Shape.Arc.ipoStart));
v2 poEnd = ToScreen(POINTS_OS(Shape.Arc.ipoEnd));
DrawArcFromPoints(&Draw, poFocus, poStart, poEnd, LayerColour);
} break;
default:
{ Assert(! "Tried to draw unknown shape"); }
}
}
}
v2_arena maPointsOnScreen = State->maPointsOnScreen;
LOG("\tDRAW POINTS");
char PointIndex[32] = {0};
foreachf1(v2, po, maPointsOnScreen)
if(POINTSTATUS(ipo) && (! iLayer || POINTLAYER(ipo) == iLayer))
{ // draw on-screen points
v2 SSPoint = ToScreen(po);
DrawCircleFill(&Draw, SSPoint, PtRadius, Col_Pt);
}
if(iLayer == MainCanvasDrawing)
DEBUG_LIVE_if(Points_Numbering)
{ // write index number next to points and intersections
foreachf(v2, po, State->maPoints)
{
v2 SSPoint = ToScreen(po);
ssnprintf(PointIndex, sizeof(PointIndex), "%u (L%u)", ipo, POINTLAYER(ipo));
DrawString(&Draw.Buffer, &State->DefaultFont, PointIndex, 15.f, SSPoint.X + 5.f, SSPoint.Y - 5.f, 0, BLACK);
}
foreachf(v2, P, State->maIntersects)
{
v2 SSP = ToScreen(P);
ssnprintf(PointIndex, sizeof(PointIndex), "%u", iP);
DrawCrosshair(&Draw, SSP, 6.f, GREEN);
DrawString(&Draw.Buffer, &State->DefaultFont, PointIndex, 15.f, SSP.X + 5.f, SSP.Y - 5.f, 0, GREEN);
}
foreachf(shape, Shape, State->maShapes)
{
v2 SSP = ToScreen(POINTS(Shape.P[1]));
ssnprintf(PointIndex, sizeof(PointIndex), "%u (L%u)", Shape.P[1], POINTLAYER(Shape.P[2]));
DrawString(&Draw.Buffer, &State->DefaultFont, PointIndex, 15.f, SSP.X + 5.f, SSP.Y + 5.f, 0, MAGENTA);
}
}
// reset scissor for OpenGL
ClipBuffer(Draw.Buffer, ZeroV2, AreaSize);
}
UPDATE_AND_RENDER(UpdateAndRender)
{
BEGIN_TIMED_BLOCK;
OPEN_LOG("geometer_log",".txt");
state *State = (state *)Memory->PermanentStorage;
memory_arena Arena;
v2 Origin;
Origin.X = 0;
Origin.Y = 0;
v2 ScreenSize;
ScreenSize.X = (f32)Draw->Buffer.Width;
ScreenSize.Y = (f32)Draw->Buffer.Height;
v2 ScreenCentre = V2Mult(0.5f, ScreenSize);
platform_request File = {0};
{
#define DRAW_FN(name)\
name = Draw->name;
DRAW_FNS
#undef DRAW_FN
}
memory_arena TempArena;
InitArena(&TempArena, Memory->TransientStorage, Memory->TransientStorageSize);
if(!Memory->IsInitialized)
{
InitArena(&Arena, (state *)Memory->PermanentStorage + 1, Memory->PermanentStorageSize - sizeof(state));
State->iSaveAction = State->iCurrentAction;
State->iCurrentLayer = 1;
State->FX[FX_Smear] = 1;
Memory->IsInitialized = 1;
}
{ // DEBUG
Debug.Buffer = Draw->Buffer;
Debug.Print = DrawString;
Debug.Font = State->DefaultFont;
Debug.FontSize = 11.f;
Debug.P = V2(2.f, ScreenSize.Y-(2.f*Debug.FontSize));
}
Assert(State->OverflowTest == 0);
if(V2InvalidDir(BASIS.XAxis))
{
LOG("Invalid basis");
BASIS.XAxis.X = 1.f;
BASIS.XAxis.Y = 0.f;
BASIS.Zoom = BASIS_DEFAULT_ZOOM;
// TODO (UI): should I reset zoom here as well?
}
// Clear BG
BEGIN_NAMED_TIMED_BLOCK(ClearBG);
ClearBuffer(Draw->Buffer);
END_NAMED_TIMED_BLOCK(ClearBG);
/* DrawRectangleFilled(Draw.Buffer, Origin, ScreenSize, WHITE); */
if(State->tBasis < 1.f) { State->tBasis += State->dt*BASIS_ANIMATION_SPEED; }
else { State->tBasis = 1.f; }
basis Basis = AnimateBasis(pBASIS, State->tBasis, BASIS);
keyboard_state Keyboard = Input.New->Keyboard;
mouse_state Mouse = Input.New->Mouse;
mouse_state pMouse = Input.Old->Mouse;
v2 SnapMouseP = State->pSnapMouseP, poClosest;
v2 poAtDist = ZeroV2;
v2 poOnLine = ZeroV2;
/* v2 DragDir = ZeroV2; */
v2 poArcStart = ZeroV2;
v2 poArcEnd = ZeroV2;
b32 IsSnapped = 0;
uint ipoClosest = 0;
uint ipoClosestIntersect = 0;
uint ipoSnap = 0;
aabb SelectionAABB = {0};
b32 RecalcNeeded = 0;
#if 1
b32 IsTakingDebugInput = 0;
for(int i = 0; i < 9; ++i) {
if(Keyboard.Num[i].EndedDown)
{ IsTakingDebugInput = 1; break; }
}
if(!IsTakingDebugInput)
#endif
{ LOG("INPUT");
// Pan with arrow keys
b32 BasisIsChanged = 0;
basis NewBasis = Basis;
b32 Down = C_PanDown.EndedDown;
b32 Up = C_PanUp.EndedDown;
b32 Left = C_PanLeft.EndedDown;
b32 Right = C_PanRight.EndedDown;
f32 PanSpeed = 15.f * Basis.Zoom;
if(Down != Up)
{
if (Down) { NewBasis.Offset = V2Add(NewBasis.Offset, V2Mult(-PanSpeed, Perp(NewBasis.XAxis))); }
else/*Up*/ { NewBasis.Offset = V2Add(NewBasis.Offset, V2Mult( PanSpeed, Perp(NewBasis.XAxis))); }
BasisIsChanged = 1;
}
if(Left != Right)
{
if (Left) { NewBasis.Offset = V2Add(NewBasis.Offset, V2Mult(-PanSpeed, NewBasis.XAxis )); }
else/*Right*/ { NewBasis.Offset = V2Add(NewBasis.Offset, V2Mult( PanSpeed, NewBasis.XAxis )); }
BasisIsChanged = 1;
}
// Zoom with PgUp/PgDn
b32 ZoomIn = C_ZoomIn.EndedDown;
b32 ZoomOut = C_ZoomOut.EndedDown;
// TODO: Make these constants?
if(ZoomIn != ZoomOut)
{
f32 ZoomFactor = 0.9f;
f32 invZoomFactor = 1.f/ZoomFactor;
if(ZoomIn) { NewBasis.Zoom *= ZoomFactor; }
else if(ZoomOut) { NewBasis.Zoom *= invZoomFactor; }
BasisIsChanged = 1;
}
if(C_Zoom)
{ // scroll to change zoom level
f32 ScrollFactor = 0.8f;
f32 invScrollFactor = 1.f/ScrollFactor;
v2 dMouseP = V2Sub(Mouse.P, ScreenCentre);
if(C_Zoom < 0)
{ // Zooming out
C_Zoom = -C_Zoom;
ScrollFactor = invScrollFactor;
}
// NOTE: keep canvas under pointer in same screen location
v2 RotatedOffset = V2RotateToAxis(NewBasis.XAxis, V2Mult((1.f-ScrollFactor) * NewBasis.Zoom, dMouseP));
NewBasis.Offset = V2Add(NewBasis.Offset, RotatedOffset);
// NOTE: wheel delta is in multiples of 120
for(int i = 0; i < C_Zoom/120; ++i)
{ NewBasis.Zoom *= ScrollFactor; }
BasisIsChanged = 1;
}
if(BasisIsChanged) { SetBasis(State, NewBasis); }
// SNAPPING
v2 CanvasMouseP = V2ScreenToCanvas(Basis, Mouse.P, ScreenCentre);
{
f32 ClosestDistSq;
f32 ClosestIntersectDistSq = 0.f;
b32 ClosestPtOrIntersect = 0;
SnapMouseP = CanvasMouseP;
poClosest = CanvasMouseP;
ipoClosest = ClosestPointIndex(State, CanvasMouseP, &ClosestDistSq);
// TODO (ui): consider ignoring intersections while selecting
/* if( ! (MODE_START_Select <= State->InputMode && State->InputMode <= MODE_END_Select)) */
{ ipoClosestIntersect = ClosestIntersectIndex(State, CanvasMouseP, &ClosestIntersectDistSq); }
ClosestPtOrIntersect = ipoClosest || ipoClosestIntersect;
DebugReplace("Pt: %u, Isct: %u, Drawing: %u\n", ipoClosest, ipoClosestIntersect, IsDrawing(State));
if(ClosestPtOrIntersect || IsDrawing(State))
{
// decide whether to use point or intersect
if(ipoClosest && ipoClosestIntersect)
{
if(ClosestDistSq <= ClosestIntersectDistSq)
{
poClosest = POINTS(ipoClosest);
ipoSnap = ipoClosest;
DebugAdd("PI - Point\n");
}
else
{
poClosest = Pull(State->maIntersects, ipoClosestIntersect);
ClosestDistSq = ClosestIntersectDistSq;
DebugAdd("PI - Intersect\n");
}
}
else if(ipoClosest)
{
poClosest = POINTS(ipoClosest);
ipoSnap = ipoClosest;
DebugAdd("Point\n");
}
else if(ipoClosestIntersect)
{
poClosest = Pull(State->maIntersects, ipoClosestIntersect);
ClosestDistSq = ClosestIntersectDistSq;
DebugAdd("Intersect\n");
}
// compare against state-based temporary points
// assumes that IsDrawingArc is implied by IsDrawing
if(State->InputMode == MODE_ExtendArc)
{
f32 ArcStartDistSq = DistSq(State->poArcStart, CanvasMouseP);
DebugAdd("Test: %f, Current: %f\n", ArcStartDistSq, ClosestDistSq);
// NOTE: very slight preference for arc start
DebugAdd("poClosest: %.2f, %.2f\n", poClosest.X, poClosest.Y);
if(ArcStartDistSq < ClosestDistSq)
{
poClosest = State->poArcStart;
ClosestDistSq = ArcStartDistSq;
DebugAdd("poClosest: %.2f, %.2f\n", poClosest.X, poClosest.Y);
ipoSnap = 0;
}
}
// don't snap to focus while extending arc
else if(IsDrawing(State))
{
f32 SelectDistSq = DistSq(State->poSelect, CanvasMouseP);
DebugAdd("Test: %f, Current: %f\n", SelectDistSq, ClosestDistSq);
if( ! ClosestPtOrIntersect || SelectDistSq < ClosestDistSq)
{
poClosest = State->poSelect;
DebugAdd("Adding poClosest: %.2f, %.2f\n", poClosest.X, poClosest.Y);
ClosestDistSq = SelectDistSq;
ipoSnap = 0;
}
}
DebugAdd("poClosest: %.2f, %.2f\n", poClosest.X, poClosest.Y);
/* gDebugPoint = poClosest; */
#define POINT_SNAP_DIST 5000.f
// NOTE: BASIS->Zoom needs to be squared to match ClosestDistSq
if(ClosestDistSq/(Basis.Zoom * Basis.Zoom) < POINT_SNAP_DIST && // closest point is within range
! C_NoSnap.EndedDown && // point snapping is still on
! V2Equals(poClosest, CanvasMouseP)) // found something to snap to
{
SnapMouseP = poClosest;
DebugAdd("SnapMouseP: %.2f, %.2f\n", SnapMouseP.X, SnapMouseP.Y);
IsSnapped = 1;
}
}
if(C_ShapeLock.EndedDown)
{
for(uint iShape = 1; iShape <= State->iLastShape; ++iShape)
{
v2 TestP = ZeroV2;
shape Shape = Pull(State->maShapes, iShape);
switch(Shape.Kind)
{
case SHAPE_Circle:
{
circle Circle = Shape.Circle;
v2 poFocus = POINTS(Circle.ipoFocus);
f32 Radius = Dist(poFocus, POINTS(Circle.ipoRadius));
TestP = ClosestPtOnCircle(CanvasMouseP, poFocus, Radius);
} break;
case SHAPE_Arc:
{
arc Arc = Shape.Arc;
v2 poFocus = POINTS(Arc.ipoFocus);
v2 poStart = POINTS(Arc.ipoStart);
v2 poEnd = POINTS(Arc.ipoEnd);
TestP = ClosestPtOnArc(CanvasMouseP, poFocus, poStart, poEnd);
} break;
case SHAPE_Segment:
{
line Seg = Shape.Line;
v2 po1 = POINTS(Seg.P1);
v2 Dir = V2Sub(POINTS(Seg.P2), po1);
TestP = ClosestPtOnSegment(CanvasMouseP, po1, Dir);
} break;
default:
{
// do nothing
}
}
if(iShape == 1 || DistSq(TestP, CanvasMouseP) < DistSq(CanvasMouseP, SnapMouseP))
// TODO: IsSnapped == 0? bring
{ SnapMouseP = TestP; }
}
}
if(IsSnapped == 0) { ipoSnap = 0; }
else if(! V2Equals(SnapMouseP, State->pSnapMouseP))
{ State->tSnapMouseP = 0.f; }
}
// TODO IMPORTANT (fix): stop unwanted clicks from registering. e.g. on open/save
// TODO: fix the halftransitioncount - when using released(button), it fires twice per release
b32 MouseInScreenBounds = IsInScreenBounds(Draw->Buffer, Mouse.P);
#define DEBUGClick(button) (MouseInScreenBounds && DEBUGPress(button))
#define DEBUGRelease(button) (Input.Old->button.EndedDown && !Input.New->button.EndedDown)
#define DEBUGPress(button) (!Input.Old->button.EndedDown && Input.New->button.EndedDown)
#define DEBUGdMouse() (V2Sub(Input.New->Mouse.P, Input.Old->Mouse.P))
if(DEBUGPress(C_DebugInfo)) // toggle debug info
{ DEBUG_LIVE_VAR_Debug_ShowInfo = !DEBUG_LIVE_VAR_Debug_ShowInfo; }
if(DEBUGPress(C_CanvasHome))
{ // reset canvas position
NewBasis.Offset = ZeroV2;
NewBasis.Zoom = BASIS_DEFAULT_ZOOM;
SetBasis(State, NewBasis);
}