forked from A2TC-YT/afk-class-item-double-chest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay_class.ahk
480 lines (405 loc) · 18.5 KB
/
overlay_class.ahk
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
CoordMode, Pixel
global overlay_var
global OVERLAY_OFFSET_X := 0
global OVERLAY_OFFSET_Y := 0
; Get the DPI scaling factor
global dpiScaling := GetDPIScaling()
; Adjust the scaling factor if necessary
global dpiInverse := 1 / dpiScaling
class Overlay
{
__New(name, content, x_pos, y_pos, font_num:=3, font_size:=16, is_bold:=0, text_color:="White", has_background:=false, background_color:="Black", round_corners:=0)
{
x_pos := OVERLAY_OFFSET_X + x_pos
y_pos := OVERLAY_OFFSET_Y + y_pos
font_size := font_size * dpiInverse
this.name := name
this.content := content
this.x_pos := x_pos
this.y_pos := y_pos
this.font_num := font_num
this.font_size := font_size
this.is_bold := is_bold
this.text_color := text_color
this.has_background := has_background
this.background_showing := false
this.background_opacity := 100
this.background_padding := 7
this.round_corners := round_corners
this.dimensions := [0, 0]
this.paused := false
fonts := ["Arial", "Verdana", "Helvetica", "Courier New", "Small Fonts", "Impact"]
Gui, % this.name ": Color", 0x0101FF
Gui, % this.name ": +E0x20 +E0x02000000 -caption +hWndoverlayHwnd +AlwaysOnTop +ToolWindow"
this.hwnd := overlayHwnd
font_choice := (font_num >= 1 && font_num <= 6) ? fonts[font_num] : font_num
Gui, % this.name ": Font", q3 s%font_size% c%text_color%, % font_choice
if (is_bold)
Gui, % this.name ": Font", Bold
this.has_timer := false
this.has_countdown := false
temp_content := content
; Handle regular timer
if (RegExMatch(content, "!timer(\d{5})", matchTimer))
{
this.timer_format := [SubStr(matchTimer1, 1, 1), SubStr(matchTimer1, 2, 1), SubStr(matchTimer1, 3, 1), SubStr(matchTimer1, 4, 1), SubStr(matchTimer1, 5, 1)]
temp_content := RegExReplace(content, "!timer\d{5}", format_time(0, this.timer_format[1], this.timer_format[2], this.timer_format[3], this.timer_format[4], this.timer_format[5]))
this.has_timer := true
}
; Handle countdown timer
else if (RegExMatch(content, "!countdown(\d+),(\d+),(\d+),(\d+)", matchCountdown))
{
this.countdown_time := (matchCountdown1 * 3600 + matchCountdown2 * 60 + matchCountdown3) * 1000 ; Convert to milliseconds
show_hours := matchCountdown1 > 0
show_minutes := matchCountdown2 > 0 || show_hours
show_seconds := matchCountdown3 > 0 || show_minutes
this.timer_format := [show_hours, show_minutes, show_seconds, true, matchCountdown4]
temp_content := RegExReplace(content, "!countdown\d+,\d+,\d+,\d+", format_time(this.countdown_time, this.timer_format[1], this.timer_format[2], this.timer_format[3], this.timer_format[4], this.timer_format[5], "countdown"))
this.has_countdown := true
}
Gui, % this.name ": Add", Text, % " BackGroundTrans HwndcontrolHwnd x0 y0 w3000 +" pos " +0x200 voverlay_var", % temp_content
this.controlHwnd := controlHwnd
if (has_background || true)
{
this.__create_background(background_color)
this.__update_background_position(temp_content)
}
Gui, % this.name ": Show", % "x" x_pos " y" y_pos " NoActivate AutoSize", % name " overlay"
WinSet, TransColor, 0101FF, % "ahk_id " this.hwnd
Gui, % this.name ": Hide"
Gui, % this.name "_bg: Hide"
}
toggle_visibility(force:="none")
{
if ((this.showing && force == "none") || force == "hide")
{
this.showing := false
Gui, % this.name ": Hide"
}
else if (force == "none" || force == "show")
{
this.showing := true
Gui, % this.name ": Show", NA
}
return this.showing
}
toggle_timer(force:="none")
{
if (!this.has_timer && !this.has_countdown)
return false
timer_function := Func("timer_function").bind(this)
if (this.timer_running && force == "pause")
{
SetTimer, % timer_function, Off
this.timer_running := false
this.paused := true
}
else if ((this.timer_running && (force == "none" || force == "stop")) || this.paused && force == "stop")
{
SetTimer, % timer_function, Off
this.start_time := 0
timer_function(this)
this.update_content(this.content)
this.timer_running := false
this.paused := false
}
else if (force == "none" || force == "start")
{
SetTimer, % timer_function, 25
this.start_time := A_TickCount
this.timer_running := true
this.paused := false
}
return
}
add_time(time_to_add, in_seconds:=true) ; only works while timer is running
{
if (!this.has_timer)
return false
if (this.has_timer)
{
if (in_seconds)
this.start_time -= time_to_add * 1000
else
this.start_time -= time_to_add
}
return
}
update_content(new_content)
{
this.has_timer := false
this.has_countdown := false
temp_content := new_content
; Handle regular timer
if (RegExMatch(new_content, "!timer(\d{5})", matchTimer))
{
this.timer_format := [SubStr(matchTimer1, 1, 1), SubStr(matchTimer1, 2, 1), SubStr(matchTimer1, 3, 1), SubStr(matchTimer1, 4, 1), SubStr(matchTimer1, 5, 1)]
temp_content := RegExReplace(new_content, "!timer\d{5}", format_time(0, this.timer_format[1], this.timer_format[2], this.timer_format[3], this.timer_format[4], this.timer_format[5]))
this.has_timer := true
}
; Handle countdown timer
else if (RegExMatch(new_content, "!countdown(\d+),(\d+),(\d+),(\d+)", matchCountdown))
{
this.countdown_time := (matchCountdown1 * 3600 + matchCountdown2 * 60 + matchCountdown3) * 1000 ; Convert to milliseconds
show_hours := matchCountdown1 > 0
show_minutes := matchCountdown2 > 0 || show_hours
show_seconds := matchCountdown3 > 0 || show_minutes
this.timer_format := [show_hours, show_minutes, show_seconds, true, matchCountdown4]
temp_content := RegExReplace(new_content, "!countdown\d+,\d+,\d+,\d+", format_time(this.countdown_time, this.timer_format[1], this.timer_format[2], this.timer_format[3], this.timer_format[4], this.timer_format[5], "countdown"))
this.has_countdown := true
}
this.content := new_content
this.__update_background_position(temp_content)
if (!this.timer_running)
GuiControl % this.name ":", overlay_var, % temp_content
Sleep, 0
if (this.showing)
Gui, % this.name ": Show", NA
return
}
update_position(new_x_pos, new_y_pos)
{
this.x_pos := new_x_pos
this.y_pos := new_y_pos
temp_content := this.content
if (RegExMatch(temp_content, "!timer(\d{5})", matchTimer))
temp_content := RegExReplace(temp_content, "!timer\d{5}", format_time(0, this.timer_format[1], this.timer_format[2], this.timer_format[3], this.timer_format[4], this.timer_format[5]))
else if (RegExMatch(temp_content, "!countdown(\d+),(\d+),(\d+),(\d+)", matchCountdown))
temp_content := RegExReplace(temp_content, "!countdown\d+,\d+,\d+,\d+", format_time(this.countdown_time, this.timer_format[1], this.timer_format[2], this.timer_format[3], this.timer_format[4], this.timer_format[5], "countdown"))
this.__update_background_position(temp_content)
if (!this.showing)
{
Gui, % this.name ": Show", % "x" this.x_pos " y" this.y_pos " NoActivate HIDE"
WinMove, % "ahk_id " this.hwnd ,, % this.x_pos, % this.y_pos
Gui, % this.name ": Hide"
}
else
WinMove, % "ahk_id " this.hwnd ,, % this.x_pos, % this.y_pos
return
}
change_color(new_color)
{
this.text_color := new_color
fonts := ["Arial", "Verdana", "Helvetica", "Courier New", "Small Fonts", "Impact"]
Gui, % this.name ": Font", % "q3 s" this.font_size " c" new_color, % fonts[font_num]
GuiControl % this.name ": Font", overlay_var
}
__create_background(background_color)
{
Gui, % this.name "_bg: +E0x20 -caption +AlwaysOnTop +ToolWindow +HwndbackgroundHwnd"
this.background_hwnd := backgroundHwnd
Gui, % this.name "_bg: Color", % background_color
Gui, % this.name "_bg: Show", % "x" this.x_pos " y" this.y_pos " NA", % this.name " BG"
this.background_transparency(100)
this.toggle_background_visibility("hide")
return
}
toggle_background_visibility(force:="none")
{
if ((this.background_showing && force == "none") || force == "hide")
{
this.background_showing := false
Gui, % this.name "_bg: Hide"
}
else if ((force == "none" || force == "show") && this.has_background)
{
this.background_showing := true
Gui, % this.name "_bg: Show", NA
if (this.showing)
Gui, % this.name ": Show", NA
}
return this.background_showing
}
change_background_color(new_color)
{
Gui, % this.name "_bg: Color", % new_color
return
}
background_transparency(percent)
{
percent := (percent < 0) ? 0 : (percent > 100) ? 100 : percent
this.background_opacity := percent
WinSet, Transparent, % Round(percent * 255 / 100), % "ahk_id " this.background_hwnd
return
}
__update_background_position(content)
{
if (!this.has_background)
return
; Use LOGFONT to calculate text dimensions
fontMetric := New LOGFONT(this.controlHwnd) ; Assuming 'hwnd' is the control handle of the text
dimensions := fontMetric.GetDimensionsInPixels(content)
; Update background GUI size and position
; if ((this.has_timer || this.has_countdown) && this.font_num >= 5)
; dimensions.w += this.font_size//5
if (!this.background_showing)
{
WinSet, Transparent, 0, % "ahk_id " this.background_hwnd
Gui, % this.name "_bg: Show", % "w" dimensions.w+(2*this.background_padding) " h" dimensions.h+(2*this.background_padding) " x" this.x_pos-this.background_padding " y" this.y_pos-this.background_padding " NA"
Winset, Region, % " 0-0 w" dimensions.w+(2*this.background_padding) " h" dimensions.h+(2*this.background_padding) " R" this.round_corners "-" this.round_corners , % "ahk_id " this.background_hwnd
; WinMove, % "ahk_id " this.background_hwnd,, % this.x_pos, % this.y_pos
Gui, % this.name "_bg: Hide"
WinSet, Transparent, % Round(this.background_opacity * 255 / 100) , % "ahk_id " this.background_hwnd
}
else
{
Gui, % this.name "_bg: Show", % "w" dimensions.w+(2*this.background_padding) " h" dimensions.h+(2*this.background_padding) " x" this.x_pos-this.background_padding " y" this.y_pos-this.background_padding " NA"
Winset, Region, % " 0-0 w" dimensions.w+(2*this.background_padding) " h" dimensions.h+(2*this.background_padding) " R" this.round_corners "-" this.round_corners , % "ahk_id " this.background_hwnd
}
if (this.showing)
WinSet, Top, , % "ahk_id " this.hwnd
this.dimensions := [dimensions.w+(2*this.background_padding), dimensions.h+(2*this.background_padding)]
return
}
get_dimensions()
{
return this.dimensions
}
destroy_overlay()
{
Gui, % this.name ": Hide"
Gui, % this.name ": Destroy"
return
}
}
timer_function(overlay)
{
if (overlay.timer_running)
{
; Check for regular timer
if (overlay.has_timer)
{
new_time := format_time(overlay.start_time, overlay.timer_format[1], overlay.timer_format[2], overlay.timer_format[3], overlay.timer_format[4], overlay.timer_format[5])
temp_content := RegExReplace(overlay.content, "!timer\d{5}", new_time)
}
; Check for countdown timer
else if (overlay.has_countdown)
{
remainingTime := overlay.start_time + overlay.countdown_time - A_TickCount
if (remainingTime < 0)
{
overlay.timer_running := false
new_time := format_time(overlay.countdown_time, overlay.timer_format[1], overlay.timer_format[2], overlay.timer_format[3], overlay.timer_format[4], overlay.timer_format[5], "countdown")
}
Else
{
new_time := format_time(remainingTime, overlay.timer_format[1], overlay.timer_format[2], overlay.timer_format[3], overlay.timer_format[4], overlay.timer_format[5], "countdown")
}
temp_content := RegExReplace(overlay.content, "!countdown\d+,\d+,\d+,\d+", new_time)
}
GuiControl, % overlay.name ":", overlay_var, % temp_content
}
return
}
format_time(start_time, show_hours, show_minutes, show_seconds, show_ms, round_ms, timer_type:="elapsed")
{
currentTick := A_TickCount ; Get current tick count
elapsedMS := (timer_type = "elapsed") ? (currentTick - start_time) : start_time
if (start_time == 0)
elapsedMS := 0
numSeconds := Floor(elapsedMS / 1000)
numHours := Floor(numSeconds / 3600)
numMinutes := Mod(Floor(numSeconds / 60), 60)
numSeconds := Mod(numSeconds, 60)
numMS := Mod(elapsedMS, 1000) ; Representing milliseconds
; Determine the highest and lowest units to be shown
highestUnit := show_hours ? 1 : (show_minutes ? 2 : (show_seconds ? 3 : 4))
lowestUnit := show_ms ? 4 : (show_seconds ? 3 : (show_minutes ? 2 : 1))
; Adjust visibility flags based on the highest and lowest units
show_hours := (highestUnit <= 1 && lowestUnit >= 1)
show_minutes := (highestUnit <= 2 && lowestUnit >= 2)
show_seconds := (highestUnit <= 3 && lowestUnit >= 3)
show_ms := (highestUnit <= 4 && lowestUnit >= 4)
formattedTime := ""
if (show_hours) {
formattedTime .= Format("{:02}", numHours)
if (show_minutes || show_seconds || show_ms)
formattedTime .= ":"
}
if (show_minutes) {
formattedTime .= Format("{:02}", numMinutes)
if (show_seconds || show_ms)
formattedTime .= ":"
}
if (show_seconds) {
formattedTime .= Format("{:02}", numSeconds)
if (show_ms)
formattedTime .= "."
}
if (show_ms)
formattedTime .= SubStr(Format("{:03}", numMS), 1, round_ms)
return formattedTime
}
; class LOGFONT by Capn Odin
Class LOGFONT{
Static WM_GETFONT := 0x31, LONG := 4, BYTE := 1
HFONT := 0, Hwnd
Height := 0, Width := 0, Escapement := 0, Orientation := 0, Weight := 0
Italic := 0, Underline := 0, StrikeOut := 0, CharSet := 0, OutPrecision := 0, ClipPrecision := 0, Quality := 0, PitchAndFamily := 0
FaceName := ""
__New(Hwnd){
this.Hwnd := Hwnd
this.UpdateFont()
}
UpdateFont(){
this.HFONT := DllCall("SendMessage", "Ptr", this.Hwnd, "UInt", this.WM_GETFONT, "Ptr", 0, "Ptr", 0, "Ptr")
amount := DllCall("GetObject", "Ptr", this.HFONT, "Int", 0, "Ptr", 0)
VarSetCapacity(buff, amount)
amount := DllCall("GetObject", "Ptr", this.HFONT, "Int", amount, "Ptr", &buff)
this.GetData(buff, amount)
}
GetData(ByRef buff, amount){
; Of Type LONG
this.Height := NumGet(buff, this.LONG * 0, "Int") ; Verified I think
this.Width := NumGet(buff, this.LONG * 1, "Int")
this.Escapement := NumGet(buff, this.LONG * 2, "Int")
this.Orientation:= NumGet(buff, this.LONG * 3, "Int")
this.Weight := NumGet(buff, this.LONG * 4, "Int") ; Verified
offset := this.LONG * 4
; Of Type BYTE
this.Italic := NumGet(buff, this.BYTE * 4 + offset, "UChar") ; Verified
this.Underline := NumGet(buff, this.BYTE * 5 + offset, "UChar") ; Verified
this.StrikeOut := NumGet(buff, this.BYTE * 6 + offset, "UChar") ; Verified
this.CharSet := NumGet(buff, this.BYTE * 7 + offset, "UChar")
this.OutPrecision := NumGet(buff, this.BYTE * 1 + offset, "UChar")
this.ClipPrecision := NumGet(buff, this.BYTE * 2 + offset, "UChar")
this.Quality := NumGet(buff, this.BYTE * 3 + offset, "UChar")
this.PitchAndFamily := NumGet(buff, this.BYTE * 0 + offset, "UChar")
offset += this.BYTE * 7 - 1
this.FaceName := ""
; Of Type Char Array
While (offset < amount){
this.FaceName .= Chr(NumGet(buff, offset += 1, "Char"))
}
}
PixelWidth(str){
return this.GetDimensionsInPixels(str)["w"]
}
PixelHeight(str){
return this.GetDimensionsInPixels(str)["h"]
}
GetDimensionsInPixels(str){
hDC := DllCall("GetDC", "Uint", this.Hwnd)
hFold := DllCall("SelectObject", "Uint", hDC, "Uint", this.HFONT)
DllCall("GetTextExtentPoint32", "Uint", hDC, "str", str, "int", StrLen(str), "int64P", nSize)
DllCall("SelectObject", "Uint", hDC, "Uint", hFold)
DllCall("ReleaseDC", "Uint", this.Hwnd, "Uint", hDC)
nWidth := nSize & 0xFFFFFFFF
nHeight := nSize >> 32 & 0xFFFFFFFF
Return {"w" : nWidth, "h" : nHeight}
}
Print(){
LONG := "Height:`t`t" this.Height "`nWidth:`t`t" this.Width "`nEscapement:`t" this.Escapement "`nOrientation:`t" this.Orientation "`nWeight:`t`t" this.Weight
BYTE := "Italic:`t`t" this.Italic "`nUnderline:`t`t" this.Underline "`nStrikeOut:`t`t" this.StrikeOut "`nCharSet:`t`t" this.CharSet "`nOutPrecision:`t" this.OutPrecision "`nClipPrecision:`t" this.ClipPrecision "`nQuality:`t`t" this.Quality "`nPitchAndFamily:`t" this.PitchAndFamily
return "-" RegExReplace(this.FaceName, "[^a-zA-Z ]") "-`n" LONG "`n" BYTE
}
}
; Function to get the DPI scaling factor
GetDPIScaling() {
hDC := DllCall("GetDC", "Ptr", 0, "Ptr")
if !hDC
return
dpiX := DllCall("GetDeviceCaps", "Ptr", hDC, "Int", 88) ; 88 is the index for LOGPIXELSX
DllCall("ReleaseDC", "Ptr", 0, "Ptr", hDC)
return dpiX / 96.0 ; 96 is the default DPI
}