-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmarkup.lua
3051 lines (2559 loc) · 72.4 KB
/
markup.lua
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
local gfx = (...) or _G.gfx
local META = prototype.CreateTemplate("markup")
META.tags = {}
META:GetSet("Table", {})
META:GetSet("MaxWidth", math.huge)
META:GetSet("ControlDown", false)
META:GetSet("LineWrap", true)
META:GetSet("ShiftDown", false)
META:GetSet("Editable", false)
META:GetSet("Multiline", true)
META:GetSet("MousePosition", Vec2())
META:GetSet("SelectionColor", Color(1, 1, 1, 0.5))
META:GetSet("CaretColor", Color(1, 1, 1, 1))
META:IsSet("Selectable", true)
META:GetSet("MinimumHeight", 0)
META:GetSet("HeightSpacing", 15)
META:GetSet("LightMode", false)
META:GetSet("SuperLightMode", false)
META:GetSet("CopyTags", true)
META:GetSet("PreserveTabsOnEnter", true)
if SERVER then
META:GetSet("FixedSize", 14) -- sigh
else
META:GetSet("FixedSize", 0)
end
function gfx.CreateMarkup(str, skip_invalidate)
local self = prototype.CreateObject(
META,
{
w = 0,
h = 0,
chunks = {},
cull_x = 0,
cull_y = 0,
cull_w = math.huge,
cull_h = math.huge,
blink_offset = 0,
remove_these = {},
started_tags = {},
undo = {},
}
)
if str then self:SetText(str) end
if not skip_invalidate then self:Invalidate() end
return self
end
function META:SetMaxWidth(w)
self.MaxWidth = w
if self.lastmw ~= w then
self.need_layout = true
self.lastmw = w
end
end
function META:SetLineWrap(b)
self.LineWrap = b
self.need_layout = true
end
function META:SetEditable(b)
self.Editable = b
self:Unselect()
end
function META:Clear(skip_invalidate)
table.clear(self.chunks)
table.clear(self.remove_these)
table.clear(self.started_tags)
if not skip_invalidate then self:Invalidate() end
end
function META:SetTable(tbl, tags)
self.Table = tbl
self:Clear()
for _, var in ipairs(tbl) do
self:Add(var, tags)
end
end
function META:AddTable(tbl, tags)
for _, var in ipairs(tbl) do
self:Add(var, tags)
end
end
function META:BeginLifeTime(time, fade_time)
fade_time = fade_time or 2
list.insert(
self.chunks,
{
type = "start_fade",
val = system.GetElapsedTime() + time + fade_time,
fade_time = fade_time,
time = time,
}
)
end
function META:EndLifeTime()
list.insert(self.chunks, {type = "end_fade", val = true})
end
function META:AddTagStopper()
list.insert(self.chunks, {type = "tag_stopper", val = true})
end
function META:AddColor(color)
list.insert(self.chunks, {type = "color", val = color})
self.need_layout = true
end
function META:AddString(str, tags)
str = tostring(str)
if tags then
for _, chunk in ipairs(self:StringTagsToTable(str)) do
list.insert(self.chunks, chunk)
end
else
list.insert(self.chunks, {type = "string", val = str})
end
self.need_layout = true
end
function META:AddFont(font)
list.insert(self.chunks, {type = "font", val = font})
self.need_layout = true
end
function META:Add(var, tags)
local t = typex(var)
if t == "color" then
self:AddColor(var)
elseif t == "string" or t == "number" then
self:AddString(var, tags)
elseif t == "table" and var.type and var.val then
list.insert(self.chunks, var)
elseif t ~= "cdata" then
llog("tried to parse unknown type %q", t)
end
self.need_layout = true
end
function META:TagPanic()
for _, v in ipairs(self.chunks) do
if v.type == "custom" then v.panic = true end
end
end
function META:CallTagFunction(chunk, name, ...)
if not chunk.val.tag then return end
if chunk.type == "custom" and not chunk.panic then
local func = chunk.val.tag and chunk.val.tag[name]
if func then
local args = {self, chunk, ...}
for i, t in ipairs(chunk.val.tag.arg_types) do
local val = chunk.val.args[i]
if type(val) == "function" then
local ok, v = pcall(val, chunk.exp_env)
if ok then val = v else wlog(v) end
end
-- type isn't right? revert to default!
if type(val) ~= t then
val = chunk.val.tag.arguments[i]
if type(val) == "table" then val = val.default end
end
list.insert(args, val)
end
args = {pcall(func, unpack(args))}
if not args[1] then llog("tag error %s", args[2]) end
return unpack(args)
end
end
end
function META:GetNextCharacterClassPosition(delta, next_space)
if next_space == nil then next_space = not self.caret_shift_pos end
local pos = self.caret_pos.i
if delta > 0 then pos = pos + 1 end
if delta > 0 then
if pos > 0 and self.chars[pos - 1] then
local type = string.get_char_type(self.chars[pos - 1].str)
while pos > 0 and self.chars[pos] and string.get_char_type(self.chars[pos].str) == type do
pos = pos + 1
end
end
if pos >= #self.chars then return pos, self.chars[#self.chars].y end
if next_space then
while
pos > 0 and
self.chars[pos] and
string.get_char_type(self.chars[pos].str) == "space" and
self.chars[pos].str ~= "\n"
do
pos = pos + 1
end
end
return self.chars[pos - 1].x, self.chars[pos - 1].y
else
-- this isn't really scintilla behaviour but I think it makes sense
if next_space then
while
pos > 1 and
string.get_char_type(self.chars[pos - 1].str) == "space" and
self.chars[pos - 1].str ~= "\n"
do
pos = pos - 1
end
end
if self.chars[pos - 1] then
local type = string.get_char_type(self.chars[pos - 1].str)
while pos > 1 and string.get_char_type(self.chars[pos - 1].str) == type do
pos = pos - 1
end
end
if pos == 1 then return 0, 1 end
return self.chars[pos + 1].x, self.chars[pos + 1].y
end
end
function META:InsertString(str, skip_move, start_offset, stop_offset)
start_offset = start_offset or 0
stop_offset = stop_offset or 0
local sub_pos = self:GetCaretSubPosition()
self:DeleteSelection(true)
do
local x, y = self.caret_pos.x, self.caret_pos.y
for _ = 1, start_offset do
x = x - 1
if x <= 0 then
y = y - 1
x = utf8.length(self.lines[y])
end
end
self:SelectStart(x, y)
x, y = self.caret_pos.x, self.caret_pos.y
for _ = 1, stop_offset do
x = x + 1
if x >= utf8.length(self.lines[y]) then
y = y + 1
x = 0
end
end
self:SelectStop(x, y)
self:DeleteSelection(true)
end
self.text = utf8.sub(self.text, 1, sub_pos - 1) .. str .. utf8.sub(self.text, sub_pos)
do -- fix chunks
local sub_pos = self.caret_pos.char.data.i
local chunk = self.caret_pos.char.chunk
-- if we're in a sea of non strings we need to make one
if
(
chunk.type ~= "string" or
chunk.internal
)
and
(
(
self.chunks[chunk.i - 1] and
self.chunks[chunk.i - 1].type ~= "string"
)
or
(
self.chunks[chunk.i + 1] and
self.chunks[chunk.i + 1].type ~= "string"
)
)
then
list.insert(self.chunks, chunk.i, {type = "string", val = str})
else
if chunk.internal then
local chunk = self.chunks[chunk.i - 1]
sub_pos = #chunk.chars + 1
chunk.val = utf8.sub(chunk.val, 1, sub_pos - 1) .. str .. utf8.sub(chunk.val, sub_pos)
else
do
local pos = chunk.i
while chunk.type ~= "string" and pos > 1 do
pos = pos - 1
chunk = self.chunks[pos]
end
end
if chunk.type == "string" then
if not sub_pos then sub_pos = #chunk.chars + 1 end
chunk.val = utf8.sub(chunk.val, 1, sub_pos - 1) .. str .. utf8.sub(chunk.val, sub_pos)
else
list.remove(self.chunks, chunk.i)
end
end
end
self:Invalidate()
end
if not skip_move then
local x = self.caret_pos.x + utf8.length(str)
local y = self.caret_pos.y + string.count(str, "\n")
if self.caret_pos.char.str == "\n" then x = 0 end
self:SetCaretPosition(x, y)
end
self:InvalidateEditedText()
self.caret_shift_pos = nil
end
function META:InvalidateEditedText()
if self.text ~= self.last_text and self.OnTextChanged then
self:OnTextChanged(self.text)
self.last_text = self.text
end
end
function META:GetSubPosFromPosition(x, y)
if x == math.huge and y == math.huge then return #self.chars end
if x == 0 and y == 0 then return 0 end
for sub_pos, char in ipairs(self.chars) do
if char.x == x and char.y == y then return sub_pos end
end
if x == math.huge then
for sub_pos, char in ipairs(self.chars) do
if char.y == y and char.str == "\n" then return sub_pos - 1 end
end
return self.chars[#self.chars]
end
if y == math.huge then
for i = 1, self.chars do
i = -i + #self.chars
local char = self.chars[i]
if char.x == x then return 1 end
end
end
return 0
end
do -- tags
local function set_font(self, font)
if self.FixedSize == 0 then gfx.SetFont(font) end
end
META.tags.click = {
arguments = {},
mouse = function(markup, self, button, press, x, y)
if button == "button_1" and press then
local str = ""
for i = self.i + 1, math.huge do
local chunk = markup.chunks[i]
if chunk.type == self.type or i > #markup.chunks then
system.OpenURL(str)
break
elseif chunk.type == "string" then
str = str .. chunk.val
end
end
return false
end
end,
post_draw_chunks = function(markup, self, chunk)
local y_offset = markup.HeightSpacing + 1
gfx.DrawLine(chunk.x - 2, chunk.top - y_offset, chunk.right + 2, chunk.top - y_offset)
end,
}
META.tags.console = {
arguments = {},
mouse = function(markup, self, button, press, x, y)
if button == "button_1" and press then
local str = ""
for i = self.i + 1, math.huge do
local chunk = markup.chunks[i]
if chunk.type == self.type or i > #markup.chunks then
commands.RunString(str)
break
elseif chunk.type == "string" then
str = str .. chunk.val
end
end
return false
end
end,
post_draw_chunks = function(markup, self, chunk)
local y_offset = markup.HeightSpacing + 1
gfx.DrawLine(chunk.x - 2, chunk.top - y_offset, chunk.right + 2, chunk.top - y_offset)
end,
}
META.tags.nolinebreak = {
arguments = {},
post_init = function(markup, self)
local ok = false
for i = 1, #markup.chunks do
local chunk = markup.chunks[i]
if ok then chunk.nolinebreak = true end
if chunk.type == "custom" and chunk.val.type == "nolinebreak" then
if not chunk.val.stop_tag then
ok = true
else
ok = false
end
end
end
end,
}
if string.anime then
META.tags.anime = {
arguments = {},
modify_text = function(markup, self, str)
return str:anime()
end,
}
end
META.tags.wrong = {
arguments = {},
post_draw_chunks = function(markup, self, chunk)
render2d.PushColor(1, 0, 0, 1)
local y_offset = markup.HeightSpacing + 1
for x = chunk.x, chunk.right do
gfx.DrawLine(x, chunk.top + math.sin(x) - y_offset, x + 1, chunk.top + math.sin(x) - y_offset)
end
render2d.PopColor()
end,
}
META.tags.background = {
arguments = {1, 1, 1, 1},
pre_draw = function(markup, self, x, y, r, g, b, a)
render2d.PushColor(r, g, b, a)
local w, h = self.tag_width, self.tag_height
if h > self.h then y = y - h end
render2d.SetTexture()
render2d.DrawRect(x, y, w, h)
render2d.PopColor()
end,
post_draw = function() -- if we don't have this we don't get tag_center_x and stuff due to performance reasons
end,
}
META.tags.mark = {
arguments = {},
post_draw_chunks = function(markup, self, chunk)
render2d.PushColor(1, 1, 0, 0.25)
render2d.SetTexture()
render2d.DrawRect(chunk.x, chunk.y, chunk.w, chunk.h)
render2d.PopColor()
end,
}
META.tags.hsv = {
arguments = {0, 1, 1},
pre_draw = function(markup, self, x, y, h, s, v)
local c = ColorHSV(h, s, v)
local r, g, b = c:Unpack()
render2d.PushColor(r, g, b, 1)
for i = self.i + 1, math.huge do
local chunk = markup.chunks[i]
if
not chunk or
(
chunk.type == "custom" and
chunk.val.type == "hsv"
)
or
chunk.type == "tag_stopper"
then
break
end
if chunk.color then chunk.color = c end
end
end,
post_draw = function()
render2d.PopColor()
end,
}
META.tags.color = {
arguments = {1, 1, 1, 1},
pre_draw = function(markup, self, x, y, r, g, b, a)
local c = Color(r, g, b, a)
render2d.PushColor(r, g, b, 1)
for i = self.i + 1, math.huge do
local chunk = markup.chunks[i]
if
not chunk or
(
chunk.type == "custom" and
chunk.val.type == "hsv"
)
or
chunk.type == "tag_stopper"
then
break
end
if chunk.color then chunk.color = c end
end
end,
post_draw = function()
render2d.PopColor()
end,
}
META.tags.alpha = {
arguments = {1},
pre_draw = function(markup, self, x, y, alpha)
render2d.SetAlphaMultiplier(alpha)
end,
post_draw = function(markup, self)
render2d.SetAlphaMultiplier(1)
end,
}
META.tags.blackhole = {
arguments = {1},
pre_draw = function(markup, self, x, y, force)
local delta = system.GetFrameTime() * 2
for _, v in ipairs(markup.chunks) do
if v ~= self and v.w > 0 and v.h > 0 then
if not v.phys then
v.phys = {
pos = {x = v.x, y = v.y},
vel = {x = 0, y = 0},
}
end
local phys = v.phys
phys.vel.x = phys.vel.x + ((self.x - phys.pos.x) * 0.01 * force)
phys.vel.y = phys.vel.y + ((self.y - phys.pos.y) * 0.01 * force)
-- velocity
phys.pos.x = phys.pos.x + (phys.vel.x * delta)
phys.pos.y = phys.pos.y + (phys.vel.y * delta)
-- friction
phys.vel.x = phys.vel.x * 0.97
phys.vel.y = phys.vel.y * 0.97
v.x = phys.pos.x
v.y = phys.pos.y
end
end
end,
}
META.tags.physics = {
arguments = {1, 0, 0, 0, 0.997, 0.1},
draw_init = function(markup, self, gx, gy, vx, vy, drag, rand_mult)
local part = {}
part = {
pos = {x = 0, y = 0},
vel = {x = vx, y = vy},
siz = {x = self.tag_width, y = self.tag_height},
rand_mult = rand_mult,
drag = drag,
}
self.part = part
end,
pre_draw = function(markup, self, x, y, gravity_y, gravity_x, vx, vy, drag, rand_mult)
local delta = system.GetFrameTime() * 2
local part = self.part
local W, H = markup.width, markup.height
W = W - self.x
H = H - self.y + part.siz.y
--local xvel = (self.last_world_x or markup.current_x) - markup.current_x
--local yvel = (self.last_world_y or markup.current_y) - markup.current_y
--self.last_world_x = markup.current_x or 0
--self.last_world_y = markup.current_y or 0
-- random velocity for some variation
part.vel.y = part.vel.y + gravity_y + (math.randomf(-1, 1) * rand_mult) --+ yvel
part.vel.x = part.vel.x + gravity_x + (math.randomf(-1, 1) * rand_mult) --+ xvel
-- velocity
part.pos.x = part.pos.x + (part.vel.x * delta)
part.pos.y = part.pos.y + (part.vel.y * delta)
-- friction
part.vel.x = part.vel.x * part.drag
part.vel.y = part.vel.y * part.drag
-- collision
if part.pos.x + part.siz.x < 0 then
part.pos.x = -part.siz.x
part.vel.x = part.vel.x * -part.drag
end
if part.pos.x + part.siz.x > W then
part.pos.x = W - part.siz.x
part.vel.x = part.vel.x * -part.drag
end
if part.pos.y + part.siz.y < 0 then
part.pos.y = -part.siz.y
part.vel.y = part.vel.y * -part.drag
end
if part.pos.y + part.siz.y > H then
part.pos.y = H - part.siz.y
part.vel.y = part.vel.y * -part.drag
end
render2d.PushMatrix()
local center_x = self.tag_center_x
local center_y = self.tag_center_y
render2d.Translate(part.pos.x, part.pos.y)
render2d.Translate(center_x, center_y)
render2d.Rotate(math.deg(math.atan2(part.vel.y, part.vel.x)))
render2d.Translate(-center_x, -center_y)
end,
post_draw = function()
render2d.PopMatrix()
end,
}
META.tags.font = {
arguments = {},
pre_draw = function(markup, self, x, y, font)
if not self.font then return end
for i = self.i + 1, math.huge do
local chunk = markup.chunks[i]
if not chunk or chunk.type == "tag_stopper" then break end
if chunk.font then chunk.font = self.font end
end
end,
init = function(markup, self, font)
self.font = fonts.FindFont(font)
end,
}
META.tags.createfont = {
arguments = {"roboto black", 18, 0, 0, 0, 0, 1, 0},
pre_draw = function(markup, self, x, y, font)
for i = self.i + 1, math.huge do
local chunk = markup.chunks[i]
if not chunk or chunk.type == "tag_stopper" then break end
if chunk.font then chunk.font = self.font end
end
end,
init = function(markup, self, font, size, blur_size, bgr, bgg, bgb, bga, blur_overdraw)
self.font = fonts.CreateFont(
{
font = font,
size = size,
blur_size = blur_size,
background_color = Color(bgr, bgg, bgb, bga),
blur_overdraw = blur_overdraw,
}
)
end,
}
META.tags.texture = {
arguments = {"error", {min = 4, max = 128}, {min = 4, max = 128}},
init = function(markup, self, path)
self.mat = render.CreateTextureFromPath(path)
end,
get_size = function(markup, self, path, size_x, size_y)
size_x = tonumber(size_x)
size_y = tonumber(size_y) or size_x
if self.mat:IsLoading() then return size_x or 16, size_y or 16 end
size_x = size_x or self.mat:GetSize().x
size_y = size_y or self.mat:GetSize().y
return size_x, size_y
end,
pre_draw = function(markup, self, x, y, path, size_x, size_y)
size_x = tonumber(size_x)
size_y = tonumber(size_y) or size_x
size_x = size_x or self.mat:GetSize().x
size_y = size_y or self.mat:GetSize().y
render2d.SetTexture(self.mat)
render2d.DrawRect(x, y, size_x, size_y)
end,
}
end
do -- tags matrix
local function set_cull_clockwise() -- ???
end
local function detM2x2(m11, m12, m21, m22)
return m11 * m22 - m12 * m21
end
local function mulM2x2V2(m11, m12, m21, m22, v1, v2)
return v1 * m11 + v2 * m12, v1 * m21 + v2 * m22
end
local function normalizeV2(x, y)
local length = math.sqrt(x * x + y * y)
return x / length, y / length
end
local function scaleV2(v1, v2, k)
return v1 * k, v2 * k
end
local function eigenvector2(l, a, d)
-- (a - ?) u1 + d u2 = 0
if a - l == 0 then return 1, 0 end
if d == 0 then return 0, 1 end
return normalizeV2(-d / (a - l), 1)
end
local function orthonormalM2x2ToVMatrix(m11, m12, m21, m22)
local det = detM2x2(m11, m12, m21, m22)
if det < 0 then render2d.Scale(1, -1) end
local angle = math.atan2(m21, m11)
render2d.Rotate(math.deg(angle))
end
META.tags.translate = {
arguments = {0, 0},
pre_draw = function(markup, self, x, y, dx, dy)
render2d.PushMatrix()
render2d.Translate(dx, dy)
end,
post_draw = function()
render2d.PopMatrix()
end,
}
META.tags.scale = {
arguments = {1, 1},
init = function() end,
pre_draw = function(markup, self, x, y, scaleX, scaleY)
render2d.PushMatrix()
self.matrixDeterminant = scaleX * scaleY
if math.abs(self.matrixDeterminant) > 10 then
scaleX, scaleY = normalizeV2(scaleX, scaleY)
scaleX, scaleY = scaleV2(scaleX, scaleY, 10)
end
local centerY = y - self.tag_height / 2
render2d.Translate(x, centerY)
render2d.Scale(scaleX, scaleY)
if scaleX < 0 then render2d.Translate(-self.tag_width, 0) end
render2d.Translate(-x, -centerY)
set_cull_clockwise(self.matrixDeterminant < 0)
end,
post_draw = function(markup, self)
if self.matrixDeterminant < 0 then set_cull_clockwise(false) end
render2d.PopMatrix()
end,
}
META.tags.size = {
arguments = {1},
pre_draw = function(markup, self, x, y, size)
markup.tags.scale.pre_draw(markup, self, x, y, size, size)
end,
post_draw = function(markup, self)
markup.tags.scale.post_draw(markup, self)
end,
}
META.tags.rotate = {
arguments = {45},
pre_draw = function(markup, self, x, y, deg)
render2d.PushMatrix()
local center_x = self.tag_center_x
local center_y = self.tag_center_y
render2d.Translate(center_x, center_y)
render2d.Rotate(math.rad(deg))
render2d.Translate(-center_x, -center_y)
end,
post_draw = function()
render2d.PopMatrix()
end,
}
META.tags.matrixez = {
arguments = {0, 0, 1, 1, 0},
pre_draw = function(markup, self, x, y, X, Y, scaleX, scaleY, angleInDegrees)
self.matrixDeterminant = scaleX * scaleY
if math.abs(self.matrixDeterminant) > 10 then
scaleX, scaleY = normalizeV2(scaleX, scaleY)
scaleX, scaleY = scaleV2(scaleX, scaleY, 10)
end
local centerX = self.tag_center_x
local centerY = self.tag_center_y
render2d.PushMatrix()
render2d.Translate(x, centerY)
render2d.Translate(X, Y)
render2d.Scale(scaleX, scaleY)
if scaleX < 0 then render2d.Translate(-self.tag_width, 0) end
if angleInDegrees ~= 0 then
render2d.Translate(centerX)
render2d.Rotate(angleInDegrees)
render2d.Translate(-centerX)
end
render2d.Translate(x, -centerY)
set_cull_clockwise(self.matrixDeterminant < 0)
end,
post_draw = function(markup, self)
if self.matrixDeterminant < 0 then set_cull_clockwise(false) end
render2d.PopMatrix()
end,
}
META.tags.matrix = {
arguments = {1, 0, 0, 1, 0, 0},
pre_draw = function(markup, self, x, y, a11, a12, a21, a22, dx, dy)
-- Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn
-- A = Q1 ? Q2
-- B = transpose (A) * A
local b11 = a11 * a11 + a21 * a21
local b12 = a11 * a12 + a21 * a22
local b21 = a12 * a11 + a22 * a21
local b22 = a12 * a12 + a22 * a22
local trB = b11 + b22
local detB = detM2x2(b11, b12, b21, b22)
-- Finding eigenvalues of B...
-- det (B - ?I) = 0
-- | a - ? b | = 0
-- | c d - ? |
--
-- (a - ?) (d - ?) - bc = 0
-- ?² + (-a - d) ? + ad - bc = 0
--
-- a + d ± sqrt ((a + d)² - 4 (ad - bc))
-- ? = -------------------------------------
-- 2
-- This is never negative
local sqrtInside = trB * trB - 4 * detB
local eigenvalue1 = 0.5 * (trB + math.sqrt(sqrtInside))
local eigenvalue2 = 0.5 * (trB - math.sqrt(sqrtInside))
-- (B - ?I) u = 0
--
-- [ a - ? b ] [ u1 ] = [ 0 ]
-- [ c d - ? ] [ u2 ] [ 0 ]
--
-- (a - ?) u1 + b u2 = 0
local q211, q221 = eigenvector2(eigenvalue1, b11, b12)
local q212, q222 = eigenvector2(eigenvalue2, b11, b12)
if eigenvalue1 == eigenvalue2 then
-- Make up an eigenvector
q212, q222 = q221, -q211
end
-- Those will never be negative as well #yolo
local scaleX = math.sqrt(eigenvalue1)
local scaleY = math.sqrt(eigenvalue2)
local q111, q121 = mulM2x2V2(a11, a12, a21, a22, q211, q221)
local q112, q122 = mulM2x2V2(a11, a12, a21, a22, q212, q222)
q111, q121 = scaleV2(q111, q121, (scaleX ~= 0) and (1 / scaleX) or 0)
if scaleY == 0 then
q112, q122 = q121, -q111
else
-- DOES THIS WORK LOL
q112, q122 = scaleV2(q112, q122, (scaleY ~= 0) and (1 / scaleY) or 0)
end
-- transpose Q2
q212, q221 = q221, q212
-- End of Cthulhu summoning
self.matrixDeterminant = detM2x2(a11, a12, a21, a22)
render2d.PushMatrix()
render2d.Translate(x, y)
render2d.Translate(dx, dy)
orthonormalM2x2ToVMatrix(q211, q212, q221, q222)
render2d.Scale(scaleX, scaleY)
orthonormalM2x2ToVMatrix(q111, q112, q121, q122)
render2d.Translate(-x, -y)
set_cull_clockwise(self.matrixDeterminant < 0)
end,
post_draw = function(markup, self)
if self.matrixDeterminant < 0 then set_cull_clockwise(false) end
render2d.PopMatrix()
end,
}
end
do -- parse tags
local function parse_tag_arguments(self, arg_line)
local out = {}
local str = {}
local in_lua = false
for _, char in ipairs(utf8.to_list(arg_line)) do
if char == "[" then
in_lua = true
elseif in_lua and char == "]" then -- todo: longest match
in_lua = false
local exp = list.concat(str, "")
local ok, func = expression.Compile(exp)
if ok then
list.insert(out, func)
else
logf("%s\n", exp)
logf("markup expression error: %s", func)
system.OnError(func)
end
str = {}
elseif char == "," and not in_lua then
if #str > 0 then
list.insert(out, list.concat(str, ""))
str = {}
end
else
list.insert(str, char)
end
end
if #str > 0 then
list.insert(out, list.concat(str, ""))
str = {}
end
for k, v in ipairs(out) do
if tonumber(v) then out[k] = tonumber(v) end
end
return out
end
function META:StringTagsToTable(str)
str = tostring(str)
str = str:gsub("<rep=(%d+)>(.-)</rep>", function(count, str)
count = math.min(math.max(tonumber(count), 1), 500)