-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbgtools.pas
More file actions
363 lines (313 loc) · 9.31 KB
/
Copy pathbgtools.pas
File metadata and controls
363 lines (313 loc) · 9.31 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
unit bgTools;
{$mode objfpc}{$H+}
interface
uses
{$IFDEF Windows}Windows,{$ENDIF}Classes, SysUtils, Forms, Controls, Graphics,
uos_flat, Variants;
type
TRectangle = record
x, y, Width, Height: NativeInt;
end;
procedure RecordToVariant(var ARec; Size: integer; var v: olevariant);
procedure VariantToRecord(v: olevariant; var ARec; Size: integer);
procedure PlaySound(channelID: NativeInt; FileName: string);
procedure PlaySoundLoop(channelID: NativeInt; FileName: string; proc: TProc);
function DoRectangle(x, y, Width, Height: NativeInt): TRectangle;
function BoundInt(AValue, AMin, AMax: NativeInt): NativeInt;
function HitTest(r1, r2: TRectangle): boolean;
function HitTestPrecise(r1, r2: TRectangle): boolean;
function IntTowards(AValue, ATarget: NativeInt): NativeInt;
procedure ToggleFullScreen(Form: TForm; ARect: TRect);
procedure CenterControl(Control: TControl);
function CalculateAspectRatioH(const W1, H1, W2: integer): integer; //result H2
function CalculateAspectRatioW(const W1, H1, H2: integer): integer; //result W2
function CalculateDestRect(ImageW, ImageH, DestW, DestH: integer;
Stretch, Proportional, Center: boolean): TRect;
procedure HighDPI(FromDPI: integer);
procedure ScaleDPI(Control: TControl; FromDPI: integer);
procedure ScaleAspectRatio(Control: TControl; OriginalParentW, OriginalParentH: integer);
procedure ScaleAspectRatio(Control: TControl; DestW, DestH: integer;
Stretch, Proportional, Center: boolean);
{$IFDEF MSWINDOWS}
procedure SetScreenResolution(const Width, Height: integer); overload;
procedure SetScreenResolution(const Width, Height, colorDepth: integer); overload;
procedure GetDisplaySettings(s: TStringList);
procedure SetDisplaySettings(Index: integer; Notify: boolean = False);
{$ENDIF}
implementation
{$IFNDEF WINDOWS}
uses
LCLType;
{$ENDIF}
procedure RecordToVariant(var ARec; Size: integer; var v: olevariant);
var
p: PByteArray;
prec: Pointer;
i: integer;
begin
v := VarArrayCreate([0, Size - 1], varByte);
p := VarArrayLock(v);
prec := @ARec;
for i := 0 to Size - 1 do
p^[i] := (PByteArray(prec))^[i];
VarArrayUnLock(v);
end;
procedure VariantToRecord(v: olevariant; var ARec; Size: integer);
var
i: integer;
p: PByteArray;
prec: Pointer;
begin
prec := @ARec;
p := VarArrayLock(v);
for i := 0 to Size - 1 do
(PByteArray(prec))^[i] := p^[i];
VarArrayUnLock(v);
end;
procedure PlaySound(channelID: NativeInt; FileName: string);
begin
uos_CreatePlayer(channelID);
uos_AddFromFile(channelID, PChar(FileName));
uos_AddIntoDevOut(channelID);
uos_Play(channelID);
end;
procedure PlaySoundLoop(channelID: NativeInt; FileName: string; proc: TProc);
begin
uos_CreatePlayer(channelID);
uos_EndProc(channelID, proc);
uos_AddFromFile(channelID, PChar(FileName));
uos_AddIntoDevOut(channelID);
uos_Play(channelID);
end;
function DoRectangle(x, y, Width, Height: NativeInt): TRectangle;
begin
Result.x := x;
Result.y := y;
Result.Width := Width;
Result.Height := Height;
end;
function BoundInt(AValue, AMin, AMax: NativeInt): NativeInt;
begin
if AValue <= AMin then
Result := AMin
else
if AValue >= AMax then
Result := AMax
else
Result := AValue;
end;
function HitTest(r1, r2: TRectangle): boolean;
begin
Result := ((r1.X + r1.Width > r2.X) and (r1.X < r2.X + r2.Width)) and
((r1.Y + r1.Height > r2.Y) and (r1.Y < r2.Y + r2.Height));
end;
function HitTestPrecise(r1, r2: TRectangle): boolean;
begin
Result := ((r1.X + r1.Width >= r2.X) and (r1.X <= r2.X + r2.Width)) and
((r1.Y + r1.Height >= r2.Y) and (r1.Y <= r2.Y + r2.Height));
end;
function IntTowards(AValue, ATarget: NativeInt): NativeInt;
begin
if AValue < ATarget then
Result := AValue + 1
else if AValue > ATarget then
Result := AValue - 1
else
Result := AValue;
end;
procedure ToggleFullScreen(Form: TForm; ARect: TRect);
begin
Form.SetBounds(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
if Form.WindowState <> wsMaximized then
begin
Form.WindowState := wsMaximized;
end
else
begin
Form.WindowState := wsNormal;
end;
end;
procedure CenterControl(Control: TControl);
begin
if not Control.HasParent then
Exit;
Control.SetBounds(
Round((Control.Parent.Width - Control.Width) div 2),
Round((Control.Parent.Height - Control.Height) div 2),
Control.Width, Control.Height);
end;
function CalculateAspectRatioH(const W1, H1, W2: integer): integer;
begin
Result := Round(H1 / W1 * W2);
end;
function CalculateAspectRatioW(const W1, H1, H2: integer): integer;
begin
Result := Round(W1 / H1 * H2);
end;
function CalculateDestRect(ImageW, ImageH, DestW, DestH: integer;
Stretch, Proportional, Center: boolean): TRect;
var
w: integer;
h: integer;
begin
// Stretch or Proportional when Image (Width or Height) is bigger than Destination
if Stretch or (Proportional and ((ImageW > DestW) or (ImageH > DestH))) then
begin
// Proportional when Image (Width or Height) is bigger than 0
if Proportional and (ImageW > 0) and (ImageH > 0) then
begin
w := DestW;
h := CalculateAspectRatioH(ImageW, ImageH, DestW);
if h > DestH then
begin
h := DestH;
w := CalculateAspectRatioW(ImageW, ImageH, DestH);
end;
ImageW := w;
ImageH := h;
end
// Stretch not Proportional or when Image (Width or Height) is 0
else
begin
ImageW := DestW;
ImageH := DestH;
end;
end;
Result := Rect(0, 0, ImageW, ImageH);
// Center: Destination (Width or Height) - Image divided by 2
if Center then
begin
Result.Left := Round((DestW - ImageW) div 2);
Result.Top := Round((DestH - ImageH) div 2);
end;
end;
procedure HighDPI(FromDPI: integer);
var
i: integer;
begin
if Screen.PixelsPerInch = FromDPI then
exit;
for i := 0 to Screen.FormCount - 1 do
ScaleDPI(Screen.Forms[i], FromDPI);
end;
procedure ScaleDPI(Control: TControl; FromDPI: integer);
var
i: integer;
WinControl: TWinControl;
begin
if Screen.PixelsPerInch = FromDPI then
exit;
with Control do
begin
Left := ScaleX(Left, FromDPI);
Top := ScaleY(Top, FromDPI);
Width := ScaleX(Width, FromDPI);
Height := ScaleY(Height, FromDPI);
end;
if Control is TWinControl then
begin
WinControl := TWinControl(Control);
if WinControl.ControlCount = 0 then
exit;
with WinControl.ChildSizing do
begin
HorizontalSpacing := ScaleX(HorizontalSpacing, FromDPI);
LeftRightSpacing := ScaleX(LeftRightSpacing, FromDPI);
TopBottomSpacing := ScaleY(TopBottomSpacing, FromDPI);
VerticalSpacing := ScaleY(VerticalSpacing, FromDPI);
end;
for i := 0 to WinControl.ControlCount - 1 do
ScaleDPI(WinControl.Controls[i], FromDPI);
end;
end;
procedure ScaleAspectRatio(Control: TControl; OriginalParentW, OriginalParentH: integer);
var
l, t, w, h: integer;
begin
l := MulDiv(Control.Left, Control.Parent.Width, OriginalParentW);
t := MulDiv(Control.Top, Control.Parent.Height, OriginalParentH);
w := MulDiv(Control.Width, Control.Parent.Width, OriginalParentW);
h := MulDiv(Control.Height, Control.Parent.Height, OriginalParentH);
Control.SetBounds(l, t, w, h);
end;
procedure ScaleAspectRatio(Control: TControl; DestW, DestH: integer;
Stretch, Proportional, Center: boolean);
var
i: integer;
r: TRect;
WinControl: TWinControl;
w, h: integer;
begin
if Control is TWinControl then
begin
WinControl := TWinControl(Control);
w := WinControl.Width;
h := WinControl.Height;
r := CalculateDestRect(WinControl.Width, WinControl.Height, DestW,
DestH, Stretch, Proportional, Center);
WinControl.SetBounds(r.Left, r.Top, r.Right, r.Bottom);
if WinControl.ControlCount = 0 then
exit;
for i := 0 to WinControl.ControlCount - 1 do
ScaleAspectRatio(WinControl.Controls[i], w, h);
end;
end;
{$IFDEF WINDOWS}
procedure SetScreenResolution(const Width, Height: integer); overload;
var
mode: TDevMode;
begin
zeroMemory(@mode, sizeof(TDevMode));
mode.dmSize := sizeof(TDevMode);
mode.dmPelsWidth := Width;
mode.dmPelsHeight := Height;
mode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;
ChangeDisplaySettings(mode, 0);
end;
procedure SetScreenResolution(const Width, Height, colorDepth: integer); overload;
var
mode: TDevMode;
begin
zeroMemory(@mode, sizeof(TDevMode));
mode.dmSize := sizeof(TDevMode);
mode.dmPelsWidth := Width;
mode.dmPelsHeight := Height;
mode.dmBitsPerPel := colorDepth;
mode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT or DM_BITSPERPEL;
ChangeDisplaySettings(mode, 0);
end;
procedure GetDisplaySettings(s: TStringList);
var
cnt: integer;
DevMode: TDevMode;
t: string;
begin
cnt := 0;
while EnumDisplaySettings(nil, cnt, DevMode) do
begin
with DevMode do
begin
case dmDisplayFixedOutput of
0: t := 'Default';
1: t := 'Stretch';
2: t := 'Center';
end;
s.Add(
IntToStr(dmPelsWidth) + 'x' + IntToStr(dmPelsHeight) + ' ' +
IntToStr(dmBitsPerPel) + ' Bits @' + IntToStr(dmDisplayFrequency) + 'hz ' + t);
end;
Inc(cnt);
end;
end;
procedure SetDisplaySettings(Index: integer; Notify: boolean = False);
var
DevMode: TDeviceMode;
liRetValue: longint;
begin
if EnumDisplaySettings(nil, Index, DevMode) then
liRetValue := ChangeDisplaySettings(DevMode, CDS_UPDATEREGISTRY);
if Notify then
SendMessage(HWND_BROADCAST, WM_DISPLAYCHANGE, SPI_SETNONCLIENTMETRICS, 0);
end;
{$ENDIF}
end.