-
Notifications
You must be signed in to change notification settings - Fork 5
/
scaffolder_draw_utils.gd
1104 lines (979 loc) · 32.6 KB
/
scaffolder_draw_utils.gd
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
tool
class_name ScaffolderDrawUtils
extends Node
const STRIKE_THROUGH_ANGLE := -PI / 3.0
const EXCLAMATION_MARK_GAP_LENGTH_TO_WIDTH_RATIO := 0.5
const EXCLAMATION_MARK_BODY_LOWER_END_WIDTH_RATIO := 0.5
const EXCLAMATION_MARK_DOT_WIDTH_RATIO := 1.0
func _init() -> void:
Sc.logger.on_global_init(self, "ScaffolderDrawUtils")
# Godot's CanvasItem.draw_polyline function draws the ends of the polyline with
# a flat cap that is perpendicular to the adjacent segment.
#
# If you try to use draw_polyline to draw a closed polygon outline by including
# the same point in the first and last position of the collection, then there
# will be a small gap between the "adjacent" start and end segments.
#
# We can close this gap by ensuring that the first and last segments are
# collinear. We can make the first and last segments collinear by introducing
# additional vertices that are just slightly offset from the original ends.
func draw_closed_polyline(
canvas: CanvasItem,
points: PoolVector2Array,
color: Color,
stroke_width := 1.0,
antialiased := false) -> void:
var original_size := points.size()
assert(original_size >= 2)
if original_size == 2:
# Degenerate case: A line segment.
canvas.draw_line(
points[0],
points[1],
color,
stroke_width,
antialiased)
points.insert(0, points[0])
if points[0] == points[original_size]:
# The given points collection already starts and ends with the same
# point.
points.push_back(points[0])
else:
# The given points collection starts and ends with a different point.
points.resize(original_size + 3)
points[original_size] = points[0]
points[original_size + 1] = points[0]
var new_size := points.size()
var tangentish_direction := points[2] - points[1]
var offset := tangentish_direction * 0.00001
points[1] = points[0] + offset
points[new_size - 2] = points[0] - offset
canvas.draw_polyline(
points,
color,
stroke_width,
antialiased)
func draw_dashed_line(
canvas: CanvasItem,
from: Vector2,
to: Vector2,
color: Color,
dash_length: float,
dash_gap: float,
dash_offset := 0.0,
width := 1.0,
antialiased := false) -> void:
var segment_length := from.distance_to(to)
var direction_normalized: Vector2 = (to - from).normalized()
var current_length := dash_offset
while current_length < segment_length:
var current_dash_length := \
dash_length if \
current_length + dash_length <= segment_length else \
segment_length - current_length
var current_from := from + direction_normalized * current_length
var current_to := from + direction_normalized * \
(current_length + current_dash_length)
canvas.draw_line(
current_from,
current_to,
color,
width,
antialiased)
current_length += dash_length + dash_gap
# TODO: Update this to honor gaps across vertices.
func draw_dashed_polyline(
canvas: CanvasItem,
vertices: PoolVector2Array,
color: Color,
dash_length: float,
dash_gap: float,
dash_offset := 0.0,
width := 1.0,
antialiased := false) -> void:
for i in vertices.size() - 1:
var from := vertices[i]
var to := vertices[i + 1]
draw_dashed_line(
canvas,
from,
to,
color,
dash_length,
dash_gap,
dash_offset,
width,
antialiased)
func draw_dashed_rectangle(
canvas: CanvasItem,
center: Vector2,
half_width_height: Vector2,
is_rotated_90_degrees: bool,
color: Color,
dash_length: float,
dash_gap: float,
dash_offset := 0.0,
stroke_width := 1.0,
antialiased := false) -> void:
var half_width := \
half_width_height.y if \
is_rotated_90_degrees else \
half_width_height.x
var half_height := \
half_width_height.x if \
is_rotated_90_degrees else \
half_width_height.y
var top_left := center + Vector2(-half_width, -half_height)
var top_right := center + Vector2(half_width, -half_height)
var bottom_right := center + Vector2(half_width, half_height)
var bottom_left := center + Vector2(-half_width, half_height)
draw_dashed_line(
canvas,
top_left,
top_right,
color,
dash_length,
dash_gap,
dash_offset,
stroke_width,
antialiased)
draw_dashed_line(
canvas,
top_right,
bottom_right,
color,
dash_length,
dash_gap,
dash_offset,
stroke_width,
antialiased)
draw_dashed_line(
canvas,
bottom_right,
bottom_left,
color,
dash_length,
dash_gap,
dash_offset,
stroke_width,
antialiased)
draw_dashed_line(
canvas,
bottom_left,
top_left,
color,
dash_length,
dash_gap,
dash_offset,
stroke_width,
antialiased)
func draw_dashed_circle(
canvas: CanvasItem,
center: Vector2,
radius: float,
color: Color,
dash_length: float,
dash_gap: float,
dash_offset := 0.0,
width := 1.0,
antialiased := false) -> void:
draw_dashed_arc(
canvas,
center,
radius,
0.0,
2.0 * PI,
color,
dash_length,
dash_gap,
dash_offset,
width,
antialiased)
func draw_dashed_arc(
canvas: CanvasItem,
center: Vector2,
radius: float,
start_angle: float,
end_angle: float,
color: Color,
dash_length: float,
dash_gap: float,
dash_offset := 0.0,
width := 1.0,
antialiased := false) -> void:
assert(dash_length > 0.0)
assert(dash_gap > 0.0)
var offset_angle := asin(dash_offset / radius / 2.0) * 2.0
start_angle += offset_angle
var dash_angle_diff := asin(dash_length / radius / 2.0) * 2.0
var gap_angle_diff := asin(dash_gap / radius / 2.0) * 2.0
var angle_diff := end_angle - start_angle
var sector_count := ceil(2.0 * PI / (dash_angle_diff + gap_angle_diff))
var theta := start_angle
while theta < end_angle - 0.0001:
var from := Vector2(cos(theta), sin(theta)) * radius + center
theta += dash_angle_diff
var to := Vector2(cos(theta), sin(theta)) * radius + center
theta += gap_angle_diff
theta = min(theta, end_angle)
canvas.draw_line(
from,
to,
color,
width,
antialiased)
func draw_dashed_capsule(
canvas: CanvasItem,
center: Vector2,
radius: float,
height: float,
is_rotated_90_degrees: bool,
color: Color,
dash_length: float,
dash_gap: float,
dash_offset := 0.0,
thickness := 1.0,
antialiased := false) -> void:
var capsule_end_start_angle: float
var capsule_end_end_angle: float
var capsule_end_offset: Vector2
if is_rotated_90_degrees:
capsule_end_start_angle = PI
capsule_end_end_angle = TAU
capsule_end_offset = Vector2(0.0, height / 2.0)
else:
capsule_end_start_angle = PI / 2.0
capsule_end_end_angle = PI / 2.0 + PI
capsule_end_offset = Vector2(height / 2.0, 0.0)
var end_center := center - capsule_end_offset
draw_dashed_arc(
canvas,
end_center,
radius,
capsule_end_start_angle,
capsule_end_end_angle,
color,
dash_length,
dash_gap,
dash_offset,
thickness,
antialiased)
end_center = center + capsule_end_offset
capsule_end_start_angle += PI
capsule_end_end_angle += PI
draw_dashed_arc(
canvas,
end_center,
radius,
capsule_end_start_angle,
capsule_end_end_angle,
color,
dash_length,
dash_gap,
dash_offset,
thickness,
antialiased)
var from: Vector2
var to: Vector2
if is_rotated_90_degrees:
from = Vector2(-radius, height / 2.0)
to = Vector2(-radius, -height / 2.0)
else:
from = Vector2(-height / 2.0, -radius)
to = Vector2(height / 2.0, -radius)
draw_dashed_line(
canvas,
from,
to,
color,
dash_length,
dash_gap,
dash_offset,
thickness,
antialiased)
if is_rotated_90_degrees:
from = Vector2(radius, -height / 2.0)
to = Vector2(radius, height / 2.0)
else:
from = Vector2(height / 2.0, radius)
to = Vector2(-height / 2.0, radius)
draw_dashed_line(
canvas,
from,
to,
color,
dash_length,
dash_gap,
dash_offset,
thickness,
antialiased)
func draw_x(
canvas: CanvasItem,
center: Vector2,
width: float,
height: float,
color: Color,
stroke_width: float) -> void:
var half_width := width / 2.0
var half_height := height / 2.0
canvas.draw_line(
center + Vector2(-half_width, -half_height),
center + Vector2(half_width, half_height),
color,
stroke_width)
canvas.draw_line(
center + Vector2(half_width, -half_height),
center + Vector2(-half_width, half_height),
color,
stroke_width)
func draw_plus(
canvas: CanvasItem,
center: Vector2,
width: float,
height: float,
color: Color,
stroke_width: float) -> void:
var half_width := width / 2.0
var half_height := height / 2.0
canvas.draw_line(
center + Vector2(-half_width, 0),
center + Vector2(half_width, 0),
color,
stroke_width)
canvas.draw_line(
center + Vector2(0, -half_height),
center + Vector2(0, half_height),
color,
stroke_width)
func draw_asterisk(
canvas: CanvasItem,
center: Vector2,
width: float,
height: float,
color: Color,
stroke_width: float) -> void:
var plus_width := width
var plus_height := height
var x_width := plus_width * 0.8
var x_height := plus_height * 0.8
draw_x(
canvas,
center,
x_width,
x_height,
color,
stroke_width)
draw_plus(
canvas,
center,
plus_width,
plus_height,
color,
stroke_width)
func draw_checkmark(
canvas: CanvasItem,
position: Vector2,
width: float,
color: Color,
stroke_width: float) -> void:
# We mostly treat the check mark as 90 degrees, divide the check mark into
# thirds horizontally, and then position it so that the bottom-most point
# of the checkmark is slightly below the target position. However, we then
# give the right-right corner a slight adjustment upward, which makes the
# checkmark slightly more accute than 90.
var top_left_point := position + Vector2(-width / 3.0, -width / 6.0)
var bottom_mid_point := position + Vector2(0, width / 6.0)
var top_right_point := \
position + Vector2(width * 2.0 / 3.0, -width / 2.0 * 1.33)
# TODO: Replace this with outline vertices and draw_colored_polygon.
var slight_horizontal_offset := Vector2(0.001, 0.0)
var vertices := [
top_left_point,
bottom_mid_point - slight_horizontal_offset,
bottom_mid_point + slight_horizontal_offset,
top_right_point,
]
canvas.draw_polyline(
vertices,
color,
stroke_width)
func draw_exclamation_mark(
canvas: CanvasItem,
center: Vector2,
width: float,
length: float,
color: Color,
is_filled: bool,
stroke_width: float,
sector_arc_length := 4.0) -> void:
var half_width := width / 2.0
var half_length := length / 2.0
var body_top_radius := half_width
var body_bottom_radius := \
body_top_radius * EXCLAMATION_MARK_BODY_LOWER_END_WIDTH_RATIO
var dot_radius := body_top_radius * EXCLAMATION_MARK_DOT_WIDTH_RATIO
var gap_length := width * EXCLAMATION_MARK_GAP_LENGTH_TO_WIDTH_RATIO
var body_length := length - gap_length - dot_radius * 2.0
var body_top_center := \
center + Vector2(0.0, -half_length + body_top_radius)
var body_bottom_center := \
body_top_center + \
Vector2(0.0,
body_length - \
body_top_radius - \
body_bottom_radius)
var dot_center := \
center + Vector2(0.0, half_length - dot_radius)
# Draw the dot.
if is_filled:
canvas.draw_circle(
dot_center,
dot_radius,
color)
else:
draw_circle_outline(
canvas,
dot_center,
dot_radius,
color,
stroke_width,
sector_arc_length)
# Draw the body.
draw_smooth_segment_with_two_circular_ends(
canvas,
body_top_center,
body_top_radius,
body_bottom_center,
body_bottom_radius,
color,
is_filled,
stroke_width,
sector_arc_length)
func draw_arrow(
canvas: CanvasItem,
start: Vector2,
end: Vector2,
head_length: float,
head_width: float,
color: Color,
stroke_width: float) -> void:
draw_strike_through_arrow(
canvas,
start,
end,
head_length,
head_width,
INF,
color,
stroke_width)
func draw_strike_through_arrow(
canvas: CanvasItem,
start: Vector2,
end: Vector2,
head_length: float,
head_width: float,
strike_through_length: float,
color: Color,
stroke_width: float) -> void:
# Calculate points in the arrow head.
var start_to_end_angle := start.angle_to_point(end)
var head_diff_1 := Vector2(head_length, -head_width * 0.5) \
.rotated(start_to_end_angle)
var head_diff_2 := Vector2(head_length, head_width * 0.5) \
.rotated(start_to_end_angle)
var head_end_1 := end + head_diff_1
var head_end_2 := end + head_diff_2
# Draw the arrow head.
canvas.draw_line(
end,
head_end_1,
color,
stroke_width)
canvas.draw_line(
end,
head_end_2,
color,
stroke_width)
# Draw the arrow body.
canvas.draw_line(
start,
end,
color,
stroke_width)
# Draw the strike through.
if !is_inf(strike_through_length):
var strike_through_angle := start_to_end_angle + STRIKE_THROUGH_ANGLE
var strike_through_middle := start.linear_interpolate(
end,
0.5)
var strike_through_half_length := strike_through_length / 2.0
var strike_through_offset := Vector2(
cos(strike_through_angle) * strike_through_half_length,
sin(strike_through_angle) * strike_through_half_length)
var strike_through_start := \
strike_through_middle - strike_through_offset
var strike_through_end := \
strike_through_middle + strike_through_offset
canvas.draw_line(
strike_through_start,
strike_through_end,
color,
stroke_width)
func draw_diamond_outline(
canvas: CanvasItem,
center: Vector2,
width: float,
height: float,
color: Color,
stroke_width: float) -> void:
var half_width := width / 2.0
var half_height := height / 2.0
canvas.draw_line(
center + Vector2(-half_width, 0),
center + Vector2(0, -half_height),
color,
stroke_width)
canvas.draw_line(
center + Vector2(0, -half_height),
center + Vector2(half_width, 0),
color,
stroke_width)
canvas.draw_line(
center + Vector2(half_width, 0),
center + Vector2(0, half_height),
color,
stroke_width)
canvas.draw_line(
center + Vector2(0, half_height),
center + Vector2(-half_width, 0),
color,
stroke_width)
func draw_shape_outline(
canvas: CanvasItem,
position: Vector2,
shape: RotatedShape,
color: Color,
thickness: float) -> void:
if shape.shape is CircleShape2D:
draw_circle_outline(
canvas,
position,
shape.shape.radius,
color,
thickness)
elif shape.shape is CapsuleShape2D:
draw_capsule_outline(
canvas,
position,
shape.shape.radius,
shape.shape.height,
shape.is_rotated_90_degrees,
color,
thickness)
elif shape.shape is RectangleShape2D:
draw_rectangle_outline(
canvas,
position,
shape.shape.extents,
shape.is_rotated_90_degrees,
color,
thickness)
else:
Sc.logger.error(
"Invalid Shape2D provided for draw_shape_outline: %s. The " +
"supported shapes are: CircleShape2D, CapsuleShape2D, " +
"RectangleShape2D." % shape.shape)
func draw_dashed_shape(
canvas: CanvasItem,
position: Vector2,
shape: RotatedShape,
color: Color,
dash_length: float,
dash_gap: float,
dash_offset: float = 0.0,
thickness: float = 1.0) -> void:
if shape.shape is CircleShape2D:
draw_dashed_circle(
canvas,
position,
shape.shape.radius,
color,
dash_length,
dash_gap,
dash_offset,
thickness)
elif shape.shape is CapsuleShape2D:
draw_dashed_capsule(
canvas,
position,
shape.shape.radius,
shape.shape.height,
shape.is_rotated_90_degrees,
color,
dash_length,
dash_gap,
dash_offset,
thickness)
elif shape.shape is RectangleShape2D:
draw_dashed_rectangle(
canvas,
position,
shape.shape.extents,
shape.is_rotated_90_degrees,
color,
dash_length,
dash_gap,
dash_offset,
thickness)
else:
Sc.logger.error(
"Invalid Shape2D provided for draw_shape_dashed_outline: " +
"%s. The supported shapes are: CircleShape2D, " +
"CapsuleShape2D, RectangleShape2D." % shape.shape)
func draw_circle_outline(
canvas: CanvasItem,
center: Vector2,
radius: float,
color: Color,
border_width := 1.0,
sector_arc_length := 4.0) -> void:
var points := compute_arc_points(
center,
radius,
0.0,
2.0 * PI,
sector_arc_length)
# Even though the points ended and began at the same position, Godot would
# render a gap, because the "adjacent" segments aren't collinear, and thus
# their end caps don't align. We introduce two vertices, at very slight
# offsets, so that we can force the end caps to line up.
points.insert(0, points[0])
points.push_back(points[0])
points[points.size() - 2].y -= 0.0001
points[1].y += 0.0001
canvas.draw_polyline(
points,
color,
border_width)
func draw_arc(
canvas: CanvasItem,
center: Vector2,
radius: float,
start_angle: float,
end_angle: float,
color: Color,
border_width := 1.0,
sector_arc_length := 4.0) -> void:
var points := compute_arc_points(
center,
radius,
start_angle,
end_angle,
sector_arc_length)
canvas.draw_polyline(
points,
color,
border_width)
func compute_arc_points(
center: Vector2,
radius: float,
start_angle: float,
end_angle: float,
sector_arc_length := 4.0) -> PoolVector2Array:
assert(sector_arc_length > 0.0)
var angle_diff := end_angle - start_angle
var sector_count := floor(abs(angle_diff) * radius / sector_arc_length)
var delta_theta := sector_arc_length / radius
var theta := start_angle
if angle_diff == 0:
return PoolVector2Array([
Vector2(cos(start_angle), sin(start_angle)) * radius + center])
elif angle_diff < 0:
delta_theta = -delta_theta
var should_include_partial_sector_at_end := \
abs(angle_diff) - sector_count * delta_theta > 0.01
var vertex_count := sector_count + 1
if should_include_partial_sector_at_end:
vertex_count += 1
var points := PoolVector2Array()
points.resize(vertex_count)
for i in sector_count + 1:
points[i] = Vector2(cos(theta), sin(theta)) * radius + center
theta += delta_theta
# Handle the fence-post problem.
if should_include_partial_sector_at_end:
points[vertex_count - 1] = \
Vector2(cos(end_angle), sin(end_angle)) * radius + center
return points
func draw_rectangle_outline(
canvas: CanvasItem,
center: Vector2,
half_width_height: Vector2,
is_rotated_90_degrees: bool,
color: Color,
thickness := 1.0) -> void:
var x_offset: float = \
half_width_height.y if \
is_rotated_90_degrees else \
half_width_height.x
var y_offset: float = \
half_width_height.x if \
is_rotated_90_degrees else \
half_width_height.y
var polyline := PoolVector2Array()
polyline.resize(6)
polyline[1] = center + Vector2(-x_offset, -y_offset)
polyline[2] = center + Vector2(x_offset, -y_offset)
polyline[3] = center + Vector2(x_offset, y_offset)
polyline[4] = center + Vector2(-x_offset, y_offset)
# By having the polyline start and end in the middle of a segment, we can
# ensure the end caps line up and don't show a gap.
polyline[5] = lerp(polyline[4], polyline[1], 0.5)
polyline[0] = polyline[5]
canvas.draw_polyline(
polyline,
color,
thickness)
func draw_capsule_outline(
canvas: CanvasItem,
center: Vector2,
radius: float,
height: float,
is_rotated_90_degrees: bool,
color: Color,
thickness := 1.0,
sector_arc_length := 4.0) -> void:
var sector_count := ceil((PI * radius / sector_arc_length) / 2.0) * 2.0
var delta_theta := PI / sector_count
var theta := \
PI / 2.0 if \
is_rotated_90_degrees else \
0.0
var capsule_end_offset := \
Vector2(height / 2.0, 0.0) if \
is_rotated_90_degrees else \
Vector2(0.0, height / 2.0)
var end_center := center - capsule_end_offset
var vertices := PoolVector2Array()
var vertex_count := (sector_count + 1) * 2 + 2
vertices.resize(vertex_count)
for i in sector_count + 1:
vertices[i + 1] = Vector2(cos(theta), sin(theta)) * radius + end_center
theta += delta_theta
end_center = center + capsule_end_offset
theta -= delta_theta
for i in range(sector_count + 1, (sector_count + 1) * 2):
vertices[i + 1] = Vector2(cos(theta), sin(theta)) * radius + end_center
theta += delta_theta
# By having the polyline start and end in the middle of a segment, we can
# ensure the end caps line up and don't show a gap.
vertices[vertex_count - 1] = lerp(
vertices[vertex_count - 2],
vertices[1],
0.5)
vertices[0] = vertices[vertex_count - 1]
canvas.draw_polyline(
vertices,
color,
thickness)
# This applies Thales's theorem to find the points of tangency between the line
# segments from the triangular portion and the circle:
# https://en.wikipedia.org/wiki/Thales%27s_theorem
func draw_ice_cream_cone(
canvas: CanvasItem,
cone_end_point: Vector2,
circle_center: Vector2,
circle_radius: float,
color: Color,
is_filled: bool,
border_width := 1.0,
sector_arc_length := 4.0) -> void:
assert(circle_radius >= 0.0)
var distance_from_cone_end_point_to_circle_center := \
cone_end_point.distance_to(circle_center)
if circle_radius <= 0.0:
# Degenerate case: A line segment.
canvas.draw_line(
circle_center,
cone_end_point,
color,
border_width,
false)
return
elif distance_from_cone_end_point_to_circle_center <= circle_radius:
# Degenerate case: A circle (the cone-end-point lies within the
# circle).
if is_filled:
canvas.draw_circle(
circle_center,
circle_radius,
color)
return
else:
draw_circle_outline(
canvas,
circle_center,
circle_radius,
color,
border_width,
sector_arc_length)
return
var angle_from_circle_center_to_point_of_tangency := \
acos(circle_radius / distance_from_cone_end_point_to_circle_center)
var angle_from_circle_center_to_cone_end_point := \
cone_end_point.angle_to_point(circle_center)
var start_angle := angle_from_circle_center_to_cone_end_point + \
angle_from_circle_center_to_point_of_tangency
var end_angle := angle_from_circle_center_to_cone_end_point + \
2.0 * PI - \
angle_from_circle_center_to_point_of_tangency
var points := compute_arc_points(
circle_center,
circle_radius,
start_angle,
end_angle,
sector_arc_length)
# These extra points prevent the stroke width from shrinking around the
# cone end point.
var extra_cone_end_point_1 := \
cone_end_point + \
(points[points.size() - 1] - cone_end_point) * 0.000001
var extra_cone_end_point_2 := \
cone_end_point + \
(points[0] - cone_end_point) * 0.000001
points.push_back(extra_cone_end_point_1)
points.push_back(cone_end_point)
points.push_back(extra_cone_end_point_2)
points.push_back(points[0])
if is_filled:
canvas.draw_colored_polygon(
points,
color)
else:
canvas.draw_polyline(
points,
color,
border_width)
# - This applies Thales's theorem to find the points of tangency between the
# line segments from the triangular portion and the circle:
# https://en.wikipedia.org/wiki/Thales%27s_theorem
# - Also see:
# https://en.wikipedia.org/wiki/Tangent_lines_to_circles#Tangent_lines_to_two_circles
func draw_smooth_segment_with_two_circular_ends(
canvas: CanvasItem,
center_1: Vector2,
radius_1: float,
center_2: Vector2,
radius_2: float,
color: Color,
is_filled: bool,
stroke_width: float,
sector_arc_length := 4.0) -> void:
assert(radius_1 >= 0.0)
assert(radius_2 >= 0.0)
var larger_radius: float
var larger_center: Vector2
var smaller_radius: float
var smaller_center: Vector2
if radius_1 > radius_2:
larger_radius = radius_1
larger_center = center_1
smaller_radius = radius_2
smaller_center = center_2
else:
larger_radius = radius_2