-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTextRender.ahk
More file actions
3511 lines (2937 loc) · 157 KB
/
TextRender.ahk
File metadata and controls
3511 lines (2937 loc) · 157 KB
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
; Script: TextRender.ahk
; License: MIT License
; Author: Edison Hua (iseahound)
; Github: https://github.com/iseahound/TextRender
; Date: 2026-04-18
; Version: 2.1.8
#Requires AutoHotkey v2.0-beta.13+
; TextRender() - Display custom text on screen.
class TextRender {
static call(text:="✿sentinel✿", background_style:="", text_style:="") {
; super is used to overshadow "this" to instantiate without infinite recursion.
this := super()
this.style1 := background_style
this.style2 := text_style
; Don't measure, just assume style1 and style2 are to be set when text is blank.
if (text == "✿sentinel✿")
return this
else
return this.Render(text, background_style, text_style)
}
; Sections
; Recipe Functions - Modifies recipe states only
; Window Functions - Modifies window states only
; Bitmap Functions - Modifies bitmap states only
; Main Functions - Modifies recipe, window, and bitmap states
; recipestate
; 0 - "noideas" | object
; 1 - "recorded" | object + recipe
; bitmapstate
; 0 - "freed" | object
; 1 - "allocated" | object + bitmap
; 2 - "filled" | object + bitmap + bitmap coordinates
; 3 - "copied" | object + bitmap + bitmap coordinates + pending deletion
; windowstate
; 0 - "notexist" | object
; 1 - "invalid" | object + window
; 2 - "updated" | object + window + window coordinates
; 3 - "rendered" | object + window + window coordinates + active timers
__Delete() {
this.Forget() ; recipestate ? ← x
this.Free() ; bitmapstate ? ← x
this.Destroy() ; windowstate ? ← x
this.CallEvent("Delete")
TextRender.gdiplusShutdown()
}
__New(OffsetLeft := 0, OffsetTop := 0) {
TextRender.gdiplusStartup()
; UpdateLayeredWindow uses these offsets to optionally isolate drawing on a single monitor.
this.OffsetLeft := OffsetLeft
this.OffsetTop := OffsetTop
; Initalize default events.
this.events := {}
this.OnEvent("LeftMouseDown", this.EventMoveWindow)
this.OnEvent("MiddleMouseDown", this.EventShowCoordinates)
this.OnEvent("RightMouseDown", this.EventCopyData)
; These are global variables. Can cause infinite loops otherwise.
this.TimeStamp() ; Sets this.t0 to the current time
this.status := 0xFFFF0001 ; Used to block timers
this.layers := []
this.data := ""
this.style1 := ""
this.style2 := ""
; Allows WinExist!
this.hwnd := 0
; Each function occupies a vector on this rank-3 tensor.
this.recipestate := 0 ; recipestate ? → 0
this.bitmapstate := 0 ; bitmapstate ? → 0
this.windowstate := 0 ; windowstate ? → 0
return this
}
; Recipe Functions
Remember(data := "", style1 := "", style2 := "") {
this.RememberRecipe(data, style1, style2)
this.recipestate := 1 ; recipestate x → 1
this.CallEvent("Remember")
return this
}
RememberRecipe(data := "", style1 := "", style2 := "") {
; Use previous styles if and only if both styles are blank.
if (style1 = "" && style2 = "") {
style1 := this.style1
style2 := this.style2
}
; Global Variables
this.data := data
this.style1 := style1
this.style2 := style2
this.layers.push([data, style1, style2])
}
Forget(n := "✿sentinel✿") {
this.ForgetRecipe(n)
this.recipestate := !!this.layers.length
this.CallEvent("Forget") ; recipestate 0 ← x (1 of 2)
return this ; recipestate 0|1 ← x (2 of 2)
}
ForgetRecipe(n := "✿sentinel✿") {
; Redraws are no longer possible!
if (n == "✿sentinel✿")
this.layers := []
else
loop min(this.layers.length, n)
this.layers.pop()
}
; Window Functions
Destroy() {
; windowstate (x → 0) → ∅ - Ignore current and lower states
if (this.windowstate <= 0)
return this
; windowstate 1 ← x - Previous state applies all previous changes
this.Invalidate()
; windowstate 0 ← 1
this.DestroyWindow()
this.windowstate := 0 ; windowstate 0 ← x
this.CallEvent("Destroy")
return this
}
DestroyWindow() {
DllCall("DestroyWindow", "ptr", this.hwnd) ; Sends WM_DESTROY
this.hwnd := 0 ; Don't delete the property, just set it to 0.
}
Create(title := "", style := 0x80000000, styleEx := 0x80088, parent := 0) {
; windowstate (1 ← x) → ∅ - Ignore current and higher states
if (this.windowstate >= 1)
return this
; windowstate 0 → 1
this.CreateWindow(title, style, styleEx, parent)
this.windowstate := 1 ; windowstate x → 1
this.CallEvent("Create")
return this
}
CreateWindow(title := "", style := 0x80000000, styleEx := 0x80088, parent := 0) {
; Window Styles - https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles
WS_POPUP := 0x80000000 ; Allow small windows.
; Extended Window Styles - https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles
WS_EX_TOPMOST := 0x8 ; Always on top.
WS_EX_TOOLWINDOW := 0x80 ; Hides from Alt+Tab menu. Removes small icon.
WS_EX_LAYERED := 0x80000 ; For UpdateLayeredWindow.
; Start off hidden with WS_VISIBLE off and zero width/height coordinates.
try dpi := DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr")
hwnd := DllCall("CreateWindowEx"
, "uint", styleEx ; dwExStyle
, "str", this.WindowClass() ; lpClassName
, "str", title ; lpWindowName
, "uint", style ; dwStyle
, "int", 0 ; X
, "int", 0 ; Y
, "int", 0 ; nWidth
, "int", 0 ; nHeight
, "ptr", parent ; hWndParent
, "ptr", 0 ; hMenu
, "ptr", 0 ; hInstance
, "ptr", 0 ; lpParam
, "ptr")
try DllCall("SetThreadDpiAwarenessContext", "ptr", dpi, "ptr")
if (hwnd == 0)
throw Error("CreateWindow failed. If #MaxThreads is set to 1, increase it to at least 2.")
; Show the window without activating it. (WS_VISIBLE wouldn't work here. Also WS_EX_NOACTIVATE does something else.)
DllCall("ShowWindow", "ptr", hwnd, "int", 4) ; SW_SHOWNOACTIVATE
; Save a reference to this object to process window messages.
DllCall("SetWindowLong" (A_PtrSize=8 ? "Ptr":""), "ptr", hwnd, "int", 0, "ptr", ObjPtr(this))
; Set the window property.
this.hwnd := hwnd
; Check if an actual parent was supplied and set the parent property.
if DllCall("GetAncestor", "ptr", hwnd, "uint", 1, "ptr") != DllCall("GetDesktopWindow", "ptr")
this.parent := parent
}
Invalidate() {
; windowstate (x → 1) → ∅ - Ignore current and lower states
if (this.windowstate <= 1)
return this
; windowstate 2 ← x - Previous state applies all previous changes
this.Stop()
; windowstate 1 ← 2
this.InvalidateWindow()
this.windowstate := 1 ; windowstate 1 ← x
this.CallEvent("Invalidate")
return this
}
InvalidateWindow() {
this.DeleteProp("WindowTime")
this.DeleteProp("WindowLeft")
this.DeleteProp("WindowTop")
this.DeleteProp("WindowRight")
this.DeleteProp("WindowBottom")
this.DeleteProp("WindowWidth")
this.DeleteProp("WindowHeight")
}
Validate() {
; windowstate (2 ← x) → ∅ - Ignore current and higher states
if (this.windowstate >= 2)
return this
; windowstate x → 1 - Previous state applies all previous changes
this.Create()
; windowstate 1 → 2
this.ValidateWindow()
this.windowstate := 2 ; windowstate x → 2
this.CallEvent("Validate")
return this
}
ValidateWindow() {
WinGetPos &x, &y, &w, &h, this.hwnd
this.HasProp("WindowTime") || this.WindowTime := 0 ; Use a dummy variable here
this.WindowLeft := x
this.WindowTop := y
this.WindowRight := x + w
this.WindowBottom := y + h
this.WindowWidth := w
this.WindowHeight := h
}
Stop() {
; windowstate (x → 2) → ∅ - Ignore current and lower states
if (this.windowstate <= 2)
return this
; windowstate 2 ← 3
this.StopWindow()
this.windowstate := 2 ; windowstate 2 ← x
this.CallEvent("Stop")
return this
}
StopWindow() {
this.StopTimer()
}
Start() {
; windowstate x → 2 - Previous state applies all previous changes
this.Validate()
; windowstate 2 → 3
this.StartWindow()
this.windowstate := 3 ; windowstate x → 3
this.CallEvent("Start")
return this
}
StartWindow() {
this.TimeStamp() ; Sets this.t0 to the current time
this.StartTimer() ; Starts timer using this.WindowTime
}
Resume() {
; windowstate (x → 1) → ∅ - Invalidated windows do not have a WindowTime
if (this.windowstate <= 1)
return this
; windowstate 2 → 3
this.ResumeWindow()
this.windowstate := 3 ; windowstate 2 → 3
this.CallEvent("Resume")
return this
}
ResumeWindow() {
this.ResumeTimer()
}
Restart() {
this.Stop()
this.Start()
}
UpdateLayered(alpha := 255) {
; bitmapstate (x → 1) → ∅ - The bitmap and canvas coordinates must be defined
if (this.bitmapstate <= 1)
return this
; windowstate x → 1 - Previous state applies all previous changes
if (this.windowstate <= 0)
this.Create()
; windowstate 1 → 2
this.UpdateLayeredWindow(alpha)
; windowstate 2 ← x - Block existing timers
this.Stop()
f := "UpdateLayered"
this.windowstate := 2 ; windowstate x → 2 ← x
this.CallEvent(f) ; bitmapstate 2|3
return this
}
UpdateLayeredWindow(alpha := 255) {
; Transfer the canvas time as the window duration.
t := this.WindowTime := this.t
; Define the smaller of canvas and bitmap coordinates.
x := this.WindowLeft := max(this.BitmapLeft, this.x)
y := this.WindowTop := max(this.BitmapTop, this.y)
x2 := this.WindowRight := min(this.BitmapRight, this.x2)
y2 := this.WindowBottom := min(this.BitmapBottom, this.y2)
w := this.WindowWidth := this.WindowRight - this.WindowLeft
h := this.WindowHeight := this.WindowBottom - this.WindowTop
if !(w && h) { ; If the width and height are zero, render a blank screen.
this.FadeWindow(0) ; windowstate is still 2, even though nothing is on screen.
return
}
; Changing x, y, w, h to be stationary does not provide a speed boost.
; Nor does making the window opaque.
pptDst := x - this.OffsetLeft << 32 >>> 32 | y - this.OffsetTop << 32
pptSrc := x - this.BitmapLeft << 32 >>> 32 | y - this.BitmapTop << 32
; Reminder: Only the visible screen area will be rendered. Clipping will occur.
DllCall("UpdateLayeredWindow"
, "ptr", this.hwnd ; hWnd
, "ptr", 0 ; hdcDst
,"uint64*", pptDst ; *pptDst
,"uint64*", w | h << 32 ; *psize
, "ptr", this.hdc ; hdcSrc
,"uint64*", pptSrc ; *pptSrc
, "uint", 0 ; crKey
, "uint*", alpha << 16 | 0x01 << 24 ; *pblend
, "uint", 2 ; dwFlags
, "int") ; Success = 1
; Fixes a long standing bug where Windows forgets which windows are on top.
; Seems to happen mostly when connecting a laptop to an external monitor.
if WinGetExStyle(this.hwnd) & 0x8 ; WS_EX_TOPMOST
WinSetAlwaysOnTop True, this.hwnd ; Set always on top again.
}
FadeWindow(alpha) {
DllCall("UpdateLayeredWindow"
, "ptr", this.hwnd ; hWnd
, "ptr", 0 ; hdcDst
, "ptr", 0 ; *pptDst
, "ptr", 0 ; *psize
, "ptr", 0 ; hdcSrc
, "ptr", 0 ; *pptSrc
, "uint", 0 ; crKey
, "uint*", alpha << 16 | 0x01 << 24 ; *pblend
, "uint", 2 ; dwFlags
, "int") ; Success = 1
}
Hide() {
; windowstate (x → 0) → ∅ - Ignore invalid states
if (this.windowstate < 1)
return this
; windowstate 1 ← x - Removes window coordinates and stops timers
this.Invalidate()
; Make the window completely invisible but preserves what was already on screen.
this.FadeWindow(0)
this.CallEvent("Hide")
return this ; windowstate 1 ← x
}
Show() {
; windowstate (x → 0) → ∅ - Ignore invalid states
if (this.windowstate < 1)
return this
; windowstate x → 2 - Adds window coordinates and creates window
this.Validate()
; Sets the alpha of the window back to 255 to restore visibility.
this.FadeWindow(255)
this.CallEvent("Show")
return this ; windowstate x → 2|3
}
ShowHide() {
if (this.windowstate <= 1)
return this.Show()
if (this.windowstate >= 2)
return this.Hide()
}
Screenshot(filepath := "", quality := "") {
if (this.windowstate <= 1)
return this
; Takes a picture even when the window is fully invisible!
this.ValidateWindow() ; Refresh window coordinates
pBitmap := TextRender.ScreenshotToBitmap([this.WindowLeft, this.WindowTop, this.WindowWidth, this.WindowHeight])
TextRender.BitmapToFile(pBitmap, filepath, quality)
DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
this.CallEvent("Screenshot")
return this
}
; Bitmap Functions
Free() {
; bitmapstate (x → 0) → ∅ - Ignore current and lower states
if (this.bitmapstate <= 0)
return this
; bitmapstate 1 ← x - Previous state applies all previous changes
this.Erase()
; bitmapstate 0 ← 1
this.FreeBitmap()
this.bitmapstate := 0 ; bitmapstate 0 ← x
this.CallEvent("Free")
return this
}
FreeBitmap() {
this.DeleteProp("ViewportLeft")
this.DeleteProp("ViewportTop")
this.DeleteProp("ViewportWidth")
this.DeleteProp("ViewportHeight")
this.DeleteProp("BitmapWidth")
this.DeleteProp("BitmapHeight")
this.DeleteProp("BitmapLeft")
this.DeleteProp("BitmapTop")
this.DeleteProp("BitmapRight")
this.DeleteProp("BitmapBottom")
DllCall("gdiplus\GdipDeleteGraphics", "ptr", this.Graphics)
obm := DllCall("CreateBitmap", "int", 0, "int", 0, "uint", 1, "uint", 1, "ptr", 0, "ptr")
DllCall("SelectObject", "ptr", this.hdc, "ptr", obm)
DllCall("DeleteObject", "ptr", this.hbm)
DllCall("DeleteDC", "ptr", this.hdc)
this.DeleteProp("hdc")
this.DeleteProp("hbm")
this.DeleteProp("ptr")
this.DeleteProp("size")
this.DeleteProp("width")
this.DeleteProp("height")
this.DeleteProp("Graphics")
}
Allocate(left := 0, top := 0, width := 0, height := 0) {
; bitmapstate (1 ← x) → ∅ - Ignore current and higher states
if (this.bitmapstate >= 1)
return this
; bitmapstate 0 → 1
this.AllocateBitmap(left := 0, top := 0, width := 0, height := 0)
this.bitmapstate := 1 ; bitmapstate x → 1
this.CallEvent("Allocate")
return this
}
AllocateBitmap(left := 0, top := 0, width := 0, height := 0) {
; Update the viewport to the new primary monitor!
try dpi := DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr")
this.ViewportLeft := 0
this.ViewportTop := 0
this.ViewportWidth := A_ScreenWidth
this.ViewportHeight := A_ScreenHeight
try DllCall("SetThreadDpiAwarenessContext", "ptr", dpi, "ptr")
if !(width && height)
this.GetParentCoordinates(&left, &top, &width, &height)
this.BitmapLeft := left
this.BitmapTop := top
this.BitmapRight := left + width
this.BitmapBottom := top + height
this.BitmapWidth := width
this.BitmapHeight := height
; struct BITMAPINFOHEADER - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
bi := Buffer(40, 0) ; sizeof(bi) = 40
NumPut( "uint", 40, bi, 0) ; Size
NumPut( "int", width, bi, 4) ; Width
NumPut( "int", -height, bi, 8) ; Height - Negative so (0, 0) is top-left.
NumPut("ushort", 1, bi, 12) ; Planes
NumPut("ushort", 32, bi, 14) ; BitCount / BitsPerPixel
hbm := DllCall("CreateDIBSection", "ptr", hdc, "ptr", bi, "uint", 0, "ptr*", &pBits:=0, "ptr", 0, "uint", 0, "ptr")
obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
DllCall("gdiplus\GdipCreateFromHDC", "ptr", hdc, "ptr*", &Graphics:=0)
DllCall("gdiplus\GdipTranslateWorldTransform", "ptr", Graphics, "float", -left, "float", -top, "int", 0)
this.hdc := hdc
this.hbm := hbm
this.ptr := pBits
this.size := 4 * width * height
this.width := width
this.height := height
this.Graphics := Graphics
}
Erase() {
; bitmapstate (x → 1) → ∅ - Ignore current and lower states
if (this.bitmapstate <= 1)
return this
; bitmapstate 2 ← x - Previous state applies all previous changes
this.Recycle()
; bitmapstate 1 ← 2
this.EraseBitmap()
this.bitmapstate := 1 ; bitmapstate 1 ← x
this.CallEvent("Erase")
return this
}
EraseBitmap() {
; Restricting the area to the canvas coordinates is faster than clearing the entire memory.
DllCall("gdiplus\GdipSetClipRect", "ptr", this.Graphics, "float", this.x, "float", this.y, "float", this.w, "float", this.h, "int", 0)
DllCall("gdiplus\GdipGraphicsClear", "ptr", this.Graphics, "uint", 0x00FFFFFF) ; All colors are the same speed.
DllCall("gdiplus\GdipResetClip", "ptr", this.Graphics)
; Invalidate canvas coordinates.
this.DeleteProp("t")
this.DeleteProp("x")
this.DeleteProp("y")
this.DeleteProp("x2")
this.DeleteProp("y2")
this.DeleteProp("w")
this.DeleteProp("h")
this.DeleteProp("chars")
this.DeleteProp("words")
this.DeleteProp("lines")
}
Fill() {
; bitmapstate (2 ← x) → ∅ - Ignore current and higher states
if (this.bitmapstate >= 2)
return this
; bitmapstate x → 1 - Previous state applies all previous changes
this.Allocate()
; recipestate 0 → ∅ - Filling is not possible if no layers are present
if (this.recipestate <= 0)
return this
; bitmapstate 1 → 2
this.FillBitmap()
this.bitmapstate := 2 ; bitmapstate x → 2 (if recipestate = 1)
this.CallEvent("Fill") ; bitmapstate x → 1 (if recipestate = 0)
return this
}
FillBitmap() {
for _, layer in this.layers
this.DrawBitmap(layer*)
}
DrawBitmap(data := "", style1 := "", style2 := "") {
; (Pull) Corrects the drawing while the user is moving the window.
dx := dy := 0
if this.HasProp("OriginLeft") && this.HasProp("OriginTop") {
DllCall("GetCursorPos", "ptr", point := Buffer(8))
dx := NumGet(point, 0, "int") - this.OriginLeft
dy := NumGet(point, 4, "int") - this.OriginTop
}
; Draw relative to the viewport coordinates.
that := this.DrawOnGraphics(this.Graphics
, data
, style1
, style2
, this.ViewportWidth
, this.ViewportHeight
, this.ViewportLeft + dx
, this.ViewportTop + dy)
; Set canvas coordinates. Ensure the starting coordinates are blank.
this.HasProp("t") && that.HasProp("t") ? this.t := max(this.t, that.t) : that.HasProp("t") && this.t := that.t
this.HasProp("x") && that.HasProp("x") ? this.x := min(this.x, that.x) : that.HasProp("x") && this.x := that.x
this.HasProp("y") && that.HasProp("y") ? this.y := min(this.y, that.y) : that.HasProp("y") && this.y := that.y
this.HasProp("x2") && that.HasProp("x2") ? this.x2 := max(this.x2, that.x2) : that.HasProp("x2") && this.x2 := that.x2
this.HasProp("y2") && that.HasProp("y2") ? this.y2 := max(this.y2, that.y2) : that.HasProp("y2") && this.y2 := that.y2
this.HasProp("x2") && this.HasProp("x") && this.w := this.x2 - this.x
this.HasProp("y2") && this.HasProp("y") && this.h := this.y2 - this.y
that.HasProp("chars") && this.chars := that.chars
that.HasProp("words") && this.words := that.words
that.HasProp("lines") && this.lines := that.lines
; Disable blank Draw() when called before any other drawings have been done...
if !this.HasProp("w") || !this.HasProp("h")
throw Error("Contract Broken: User refused to cooperate with drawing something...")
}
Recycle() {
; bitmapstate (x → 2) → ∅ - Ignore current and lower states
if (this.bitmapstate <= 2)
return this
this.bitmapstate := 2 ; bitmapstate 2 ← x
this.CallEvent("Recycle")
return this
}
Resolve() {
; bitmapstate x → 2 (if recipestate = 1)
; bitmapstate x → 1 (if recipestate = 0)
this.Fill()
; bitmapstate 1 → 1 - The bitmap has not been filled so return early
if (this.bitmapstate == 1)
return this
this.bitmapstate := 3 ; bitmapstate x → 3 (if recipestate = 1)
this.CallEvent("Resolve") ; bitmapstate x → 1 (if recipestate = 0)
return this
}
Save(filepath := "", quality := "") {
; Can recover out of bounds bitmap drawings by drawing to a separate bitmap buffer.
if (this.recipestate >= 1) {
; bitmapstate x → 2|3 ← x
this.Redraw()
; recipestate 1
; bitmapstate 2|3 - InBounds uses canvas coordinates
pBitmap := this.InBounds() ? this.CopyToBitmap() : this.RenderToBitmap()
}
; Can only save what's currently present on the bitmap.
if (this.recipestate <= 0) {
; bitmapstate (x → 1) → ∅ - Guard against saving a blank bitmap
if (this.bitmapstate <= 1)
return this
; bitmapstate 2|3 - Underlying bitmap must be filled with drawings
pBitmap := this.CopyToBitmap()
}
TextRender.BitmapToFile(pBitmap, filepath, quality)
DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
; If the memory has been freed it is now reallocated.
this.CallEvent("Save") ; bitmapstate 2|3 if recipestate = 0 (1 of 2)
return this ; bitmapstate x → 2|3 ← x if recipestate = 1 (2 of 2)
}
; Main Functions - Modifies recipe, window, and bitmap states
Clear() {
this.Forget() ; recipestate 0 ← x
this.Erase() ; bitmapstate 1 ← x
this.Hide() ; windowstate 1 ← x
this.CallEvent("Clear")
return this
}
Timeout(status) {
; Blocks timers from acting when bitmaps have changed.
if (this.status != status) ; windowstate a:x = b:x
return this
this.Destroy() ; windowstate 0 ← x
this.CallEvent("Timeout")
return this
}
Flush() {
; recipestate 0 → ∅ - Do nothing if this.layers is blank
if (this.recipestate <= 0)
return this
; bitmapstate x → 1|2|3 ← x - Check if screen size has changed
this.Reallocate()
; bitmapstate 1 ← x - Clear the bitmap and canvas and expire timers
this.Erase()
; bitmapstate 1 → 2 - Fill the bitmap with queued drawings
this.Fill()
; recipestate 0 ← 1 - Deletes this.layers to prevent redrawing
this.Forget()
this.recipestate := 0 ; recipestate 0 ← x
this.bitmapstate := 2 ; bitmapstate x → 2 ← x
this.CallEvent("Flush")
return this
}
Paint() {
; recipestate 0 → ∅ - Can't paint if there are no recipes!
if (this.recipestate <= 0)
return this
; recipestate 0 ← x - Flushes all pending events and draws them immediately
; bitmapstate x → 2 ← x - The bitmap is filled with new drawings!
this.Flush()
; bitmapstate 2 → 3 - There are recipes so the bitmap has been drawn!
this.Resolve()
; windowstate x → 2 ← x - Requires bitmapstate at 2|3
this.UpdateLayered()
; windowstate 2 → 3 - Starts any timers
this.Start()
this.recipestate := 0 ; recipestate 0 ← x
this.bitmapstate := 3 ; bitmapstate x → 3 ← x
this.windowstate := 3 ; windowstate x → 3 ← x
this.CallEvent("Paint")
return this
}
Draw(data := "", style1 := "", style2 := "") {
; Prepare the bitmap for new drawings.
if (this.bitmapstate >= 3) {
; recipestate 0 ← x - Deletes this.layers to prevent redrawing
this.Forget()
; bitmapstate 1 ← 3 - Clear the bitmap and canvas and expire timers
this.Erase()
}
; bitmapstate x → 1|2 ← x - Recover intermediate steps when screen changes
this.Redraw()
; recipestate x → 1 - Sets default styles and data, use before DrawBitmap
this.Remember(data, style1, style2)
; bitmapstate 1 → 2 - Use previous styles iff both style1 and style2 are blank
this.DrawBitmap(this.data, this.style1, this.style2)
this.bitmapstate := 2 ; bitmapstate x → 2 ← x
this.CallEvent("Draw") ; recipestate x → 1
return this
}
Render(data := "✿sentinel✿", styles*) {
if !(data == "✿sentinel✿")
; recipestate x → 1 - Saves the data and styles to the layers
; bitmapstate x → 2 ← x - Allocates memory and fills it with drawings
this.Draw(data, styles*)
; Enforce contract to prevent fork in Resolve().
if (this.recipestate = 0)
throw Error("RecipeState is 0: Please pass drawing parameters here")
; bitmapstate x → 1|2|3 ← x - This is normally called within Draw()
this.Reallocate()
; bitmapstate x → 3 (if recipestate = 1) - Marks the memory for clearing after rendering
this.Resolve()
; windowstate x → 2 ← x - Renders to bitmap data (2|3) to screen
this.UpdateLayered()
; windowstate 2 → 3 - Starts any timers
this.Start()
; By not clearing any memory early, calls to Save() will not encounter a blank bitmap.
this.recipestate := 1 ; recipestate x → 1
this.bitmapstate := 3 ; bitmapstate x → 3
this.windowstate := 3 ; windowstate x → 3 ← x
this.CallEvent("Render")
return this
}
Reallocate() {
; bitmapstate x → 1 - Initalize memory buffer
this.Allocate()
; Check if bitmap coordinates have changed.
this.GetParentCoordinates(&left, &top, &width, &height)
if !(width = this.BitmapWidth && height = this.BitmapHeight) {
; bitmapstate 0 ← x - Delete memory
this.Free()
; bitmapstate 0 → 1 - Allocate again
this.Allocate(left, top, width, height)
}
f := "Reallocate" ; bitmapstate x → 1|2|3 ← x (conditions 1 and 2 combined)
this.CallEvent(f) ; bitmapstate x → 1 ← x if screen size has changed (1 of 2)
return this ; bitmapstate x → 1|2|3 if screen size is the same (2 of 2)
}
Redraw() {
; bitmapstate x → 1|2|3 ← x - The bitmap memory could be reallocated or remain the same
this.Reallocate()
; recipestate 0 → ∅ - Dividing this into 2 conditional branches allows a better proof
if (this.recipestate == 0)
return this
; bitmapstate x → 2 (if recipestate = 1) - Fills bitmap with drawings from layers
this.Fill()
; Often called after the screen size changes during a drawing so bitmapstate is not 3 yet.
this.CallEvent("Redraw") ; bitmapstate x → 1|2|3 ← x if recipestate = 0 (1 of 2)
return this ; bitmapstate x → 2|3 ← x if recipestate = 1 (2 of 2)
}
Rerender() {
; recipestate 0 → ∅ - Does nothing if this.layers is blank
if (this.recipestate <= 0)
return this
; bitmapstate 0|1|2 → ∅ - There was nothing shown on screen
if (this.bitmapstate <= 2)
return this
; windowstate 0 → ∅ - Assume the window has been destroyed on purpose
if (this.windowstate <= 0)
return this
; bitmapstate 2|3 ← x - With recipestate = 1, bitmapstate 1 can be omitted
this.Redraw()
; bitmapstate 2|3 → 3 - Mark the bitmap buffer as rendered.
this.Resolve()
; windowstate 1|2|3 → 2 - Show the renders to the user's screen
this.UpdateLayered()
; windowstate 2 → 3 - Resume any timers with the remaning time
this.Resume()
this.windowstate := 3 ; windowstate 1|2|3 → 3
this.CallEvent("Rerender") ; recipestate 1
return this ; bitmapstate 3
}
; Timekeeping
TimeStamp() {
DllCall("QueryPerformanceCounter", "int64*", &start:=0)
return this.t0 := start
}
TimeElapsed() {
DllCall("QueryPerformanceFrequency", "int64*", &frequency:=0)
DllCall("QueryPerformanceCounter", "int64*", &end:=0)
return 1000 * (end - this.t0) / frequency
}
TimeRemaining() {
return max(0, this.WindowTime - this.TimeElapsed())
}
TimeExceeded() {
return min(0, this.TimeElapsed() - this.WindowTime)
}
; Timers!!!
StartTimer() {
this.Timer(this.WindowTime)
}
ResumeTimer() {
this.Timer(this.TimeRemaining())
}
StopTimer() {
; By having a random and a deterministic component the probablility of collisions drops significantly.
this.status += 1
if 0xFFFF & this.status == 0xFFFF {
h := Random(0x1000, 0xFFFF)
l := 0
this.status := h << 32 | l
}
}
Timer(t, method := "Timeout") {
; Create a timer that eventually clears the canvas.
if (t > 0) {
; Create a reference to the object held by a timer.
f := ObjBindMethod(this, method, this.status) ; Calls Timeout()
SetTimer f, -t ; Calls __Delete.
}
}
; Time Functions
Wait(t := 0) {
this.Cooldown() ; windowstate 2 ← x
this.Suspend(t) ; windowstate x
this.CallEvent("Wait")
return this
}
Cooldown() {
; windowstate (x → 1) → ∅ - Ignore invalid states
if (this.windowstate <= 1)
return this
; windowstate 2 ← x - Block existing timers
this.Stop()
; Waits the time set originally as the "t" parameter.
this.CooldownWindow()
this.windowstate := 2 ; windowstate 1|2 ← x
this.CallEvent("Cooldown")
return this
}
CooldownWindow() {
this.SuspendWindow(this.TimeRemaining())
}
Suspend(t := 0) {
; Simply suspends the window for a brief duration.
this.SuspendWindow(t)
this.CallEvent("Suspend")
return this ; windowstate x
}
SuspendWindow(t := 0) {
if (t <= 0)
return
; Always use QPC over GetTickCount for finer time intervals.
DllCall("QueryPerformanceFrequency", "int64*", &frequency:=0)
DllCall("QueryPerformanceCounter", "int64*", &start:=0)
loop {
DllCall("QueryPerformanceCounter", "int64*", &now:=0)
elapsed_time := (now - start) / frequency * 1000
remaining_time := t - elapsed_time
if (remaining_time > 30)
Sleep 10
if (remaining_time <= 0)
break
}
}
; Animation Functions
Animate(f, t, keyframes := "") {
; bitmapstate (x → 1) → ∅ - Cannot render empty bitmap
if (this.bitmapstate <= 1)
return this
; bitmapstate 2|3 → 3 - Prep bitmap for overwriting with Draw()