Skip to content

Commit

Permalink
add dpiFIX() method to Forms
Browse files Browse the repository at this point in the history
scale the custombutton by default
add some extra safety checks to mono/dotnet info
trainers now accept high dpi and replace the default buttons with custombuttons
  • Loading branch information
cheat-engine committed Feb 19, 2021
1 parent 9782e42 commit 0e30e50
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 69 deletions.
13 changes: 13 additions & 0 deletions Cheat Engine/LuaForm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ function createForm(L: Plua_State): integer; cdecl;

end;


function customform_fixDPI(L: PLua_State): integer; cdecl;
var
c: TCustomForm;
begin
c:=luaclass_getClassObject(L);
c.AutoAdjustLayout(lapAutoAdjustForDPI,c.DesignTimePPI,screen.PixelsPerInch,c.width,scaley(c.width,c.DesignTimePPI));
result:=0;
end;


function customform_getOnClose(L: PLua_State): integer; cdecl;
var
c: TCustomForm;
Expand Down Expand Up @@ -315,6 +326,7 @@ function customform_loadFormPosition(L: Plua_State): integer; cdecl;

end;


function createFormFromFile(L: Plua_State): integer; cdecl;
var filename: string;
f: TCEForm;
Expand Down Expand Up @@ -711,6 +723,7 @@ procedure customform_addMetaData(L: PLua_state; metatable: integer; userdata: in
luaclass_addClassFunctionToTable(L, metatable, userdata, 'saveFormPosition', customform_saveFormPosition);
luaclass_addClassFunctionToTable(L, metatable, userdata, 'loadFormPosition', customform_loadFormPosition);

luaclass_addClassFunctionToTable(L, metatable, userdata, 'fixDPI', customform_fixDPI);


luaclass_addPropertyToTable(L, metatable, userdata, 'OnClose', customform_getOnClose, customform_setOnClose);
Expand Down
3 changes: 1 addition & 2 deletions Cheat Engine/MainUnit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3952,8 +3952,6 @@ procedure TMainForm.MenuItem9Click(Sender: TObject);
exit;
end;



frmTrainerGenerator.Show;
{$endif}
end;
Expand Down Expand Up @@ -4230,6 +4228,7 @@ procedure TMainForm.miCreateLuaFormClick(Sender: TObject);
br: TRect;
begin
f := tceform.CreateNew(nil);
f.DesignTimePPI:=screen.PixelsPerInch;
f.autosize := False;

j := 1;
Expand Down
63 changes: 48 additions & 15 deletions Cheat Engine/betterControls/cecustombutton.pas
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ TCECustomButton=class(TCustomControl)
//state: T
fAlignment : TAlignment;

fScaled: boolean;
fDrawFocusRect: boolean;
fFocusElipseColor: TColor;

hasSetRounding: boolean;
froundingX: integer;
froundingY: integer;
fCustomDrawn: boolean;
Expand Down Expand Up @@ -71,22 +73,24 @@ TCECustomButton=class(TCustomControl)
procedure setFocusedSize(size: integer);
procedure setButtonColor(c: TColor);
procedure setDrawFocusRect(state: boolean);

protected

procedure SetFocus; override;

procedure ChildHandlesCreated; override;

procedure Paint; override;
procedure MouseEnter; override;
procedure MouseLeave; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;

procedure DoAutoSize; override;
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer; WithThemeSpace: Boolean); override;

function getCaption: string; virtual;
procedure setCaption(c: string); virtual;
public
procedure SetFocus; override;

procedure startAnimatorTimer; //starts the timer based on the current fps. note: does not stop if CustomDrawnn is true
procedure stopAnimatorTimer;
constructor Create(AOwner: TComponent); override;
Expand All @@ -110,6 +114,7 @@ TCECustomButton=class(TCustomControl)
property DrawFocusRect: boolean read fDrawFocusRect write setDrawFocusRect;
property FocusedSize: integer read fFocusedSize write setFocusedSize;
property FocusElipseColor: tcolor read fFocusElipseColor write fFocusElipseColor;
property Scaled: boolean read fScaled write fScaled default true;

property Align;
property Anchors;
Expand Down Expand Up @@ -173,6 +178,8 @@ TCECustomButton=class(TCustomControl)

implementation

uses forms;

procedure TCECustomButton.timertimer(sender: TObject);
begin
Invalidate;
Expand Down Expand Up @@ -212,6 +219,7 @@ procedure TCECustomButton.setRoundingX(x: integer);
begin
if x>=0 then
begin
hasSetRounding:=true;
froundingX:=x;

if AutoSize then DoAutoSize;
Expand All @@ -223,6 +231,7 @@ procedure TCECustomButton.setRoundingY(y: integer);
begin
if y>=0 then
begin
hasSetRounding:=true;
froundingY:=y;

if AutoSize then DoAutoSize;
Expand Down Expand Up @@ -323,10 +332,34 @@ procedure TCECustomButton.MouseLeave;
inherited MouseLeave;
end;

procedure TCECustomButton.ChildHandlesCreated;
var
p: twincontrol;
f: TCustomForm absolute p;
begin
if scaled and hasSetRounding then
begin
p:=parent;
while (p<>nil) and (not (p is TCustomForm)) do
p:=p.Parent;

if p<>nil then
begin
fRoundingX:=scalex(froundingx, f.DesignTimePPI);
fRoundingY:=scaley(froundingy, f.DesignTimePPI);
end;

Invalidate;
end;

inherited ChildHandlesCreated;
end;

procedure TCECustomButton.SetFocus;
begin
inherited SetFocus;
invalidate;

end;

procedure TCECustomButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
Expand Down Expand Up @@ -363,20 +396,18 @@ function TCECustomButton.getCaption: string;
result:=inherited caption;
end;

procedure TCECustomButton.DoAutoSize;
var w,h: integer;
procedure TCECustomButton.CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer; WithThemeSpace: Boolean);
begin
w:=0;
h:=0;
canvas.GetTextSize(Caption,w,h);

w:=w+2*canvas.GetTextWidth(' ');
inc(h,4);
PreferredWidth:=0;
PreferredHeight:=0;
canvas.GetTextSize(Caption,PreferredWidth,PreferredHeight);

clientwidth:=2+w+trunc(arctan(roundingx));
clientheight:=2+h+trunc(arctan(roundingy));
PreferredWidth:=4+PreferredWidth+2*canvas.GetTextWidth(' ');
PreferredHeight:=4+PreferredHeight;
end;



procedure TCECustomButton.Paint;
var
w,h: integer;
Expand Down Expand Up @@ -526,8 +557,8 @@ constructor TCECustomButton.Create(AOwner: TComponent);
inherited Create(AOwner);

Alignment:=taCenter;
froundingY:=20;
froundingX:=20;
froundingY:=20; //scaley(20,96);
froundingX:=20; //scalex(20,96);
ControlStyle:=ControlStyle - [csOpaque] + [csParentBackground, csClickEvents];

fButtonColor:=clBtnFace;
Expand All @@ -546,6 +577,8 @@ constructor TCECustomButton.Create(AOwner: TComponent);
fbordersize:=1;

ffocusElipseColor:=clHighlight;
fscaled:=true;

TabStop:=true;
end;

Expand Down
17 changes: 15 additions & 2 deletions Cheat Engine/bin/autorun/dotnetinfo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,9 @@ DotNetValueReaders[ELEMENT_TYPE_PTR]=function(address)
end

local function readDotNetString(address, Field)
if address==0 or address==nil then return nil,'address is invalid' end
if Field==nil then return nil,'Field is nil' end

--assumption: address points to the start of the string object, so not the pointer to the object
if Field.Class.Image.Domain.Control==CONTROL_MONO then
return mono_string_readString(address)
Expand Down Expand Up @@ -1477,7 +1480,12 @@ local function FieldValueUpdaterTimer(frmDotNetInfo, sender)

if Class.Fields[ci].Address and (Class.Fields[ci].Address~=0) and (not (isKeyPressed(VK_CONTROL))) then
if (Class.Fields[ci].VarType==ELEMENT_TYPE_STRING) or (Class.Fields[ci].VarTypeName == "System.String") then
value=readDotNetString(readPointer(Class.Fields[ci].Address), Class.Fields[ci])
local a=readPointer(Class.Fields[ci].Address)
if a then
value=readDotNetString(a, Class.Fields[ci])
else
value='?'
end
else
local reader=DotNetValueReaders[Class.Fields[ci].VarType]
if reader==nil then
Expand Down Expand Up @@ -1508,7 +1516,12 @@ local function FieldValueUpdaterTimer(frmDotNetInfo, sender)
local a=address+Class.Fields[ci].Offset

if (Class.Fields[ci].VarType==ELEMENT_TYPE_STRING) or (Class.Fields[ci].VarTypeName == "System.String") then
value=readDotNetString(readPointer(a), Class.Fields[ci])
local address=readPointer(a)
if address then
value=readDotNetString(address, Class.Fields[ci])
else
value='?'
end
else
local reader=DotNetValueReaders[Class.Fields[ci].VarType]
if reader==nil then
Expand Down
2 changes: 0 additions & 2 deletions Cheat Engine/bin/autorun/monoscript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2143,8 +2143,6 @@ end

function mono_string_readString(stringobject)
if stringobject==nil then
print("mono_string_readString called with nil")
print(debug.traceback())
return nil,'invalid parameter'
end

Expand Down
2 changes: 2 additions & 0 deletions Cheat Engine/bin/celua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ methods

Form Class: (Inheritance: ScrollingWinControl->CustomControl->WinControl->Control->Component->Object)
properties
DesignTimePPI: integer - the PPI/DPI at the time the form was designed
AllowDropFiles: boolean - Allows files to be dragged into the form
ModalResult: integer - The current ModalResult value of the form. Note: When this value gets set the modal form will close
Menu: MainMenu - The main menu of the form
Expand All @@ -1056,6 +1057,7 @@ properties


methods
fixDPI() : Resizes controls and fonts based on the current DPI and the DPI used to create the form. Only use this on forms that are not designed with variable DPI in mind
centerScreen(); : Places the form at the center of the screen
hide() : Hide the form
show() : show the form
Expand Down
10 changes: 5 additions & 5 deletions Cheat Engine/first.pas
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ procedure setDPIAware; //won't work in windows 10 anymore

var
i: integer;
istrainer: boolean;
//istrainer: boolean;
r: TRegistry;
hassetdpiaware: boolean;
initialization
//todo, check registry if not a trainer

istrainer:=false;
//istrainer:=false;
hassetdpiaware:=false;

for i:=1 to Paramcount do
Expand All @@ -71,11 +71,11 @@ initialization
hassetdpiaware:=true;
end;

if pos('.CETRAINER', uppercase(ParamStr(i)))>0 then
istrainer:=true;
//if pos('.CETRAINER', uppercase(ParamStr(i)))>0 then
// istrainer:=true;
end;

if not (istrainer or hassetdpiaware) then
if not hassetdpiaware then
begin
//check the registry
r := TRegistry.Create;
Expand Down
22 changes: 11 additions & 11 deletions Cheat Engine/trainergenerator.lfm
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
object frmTrainerGenerator: TfrmTrainerGenerator
Left = 819
Left = 407
Height = 412
Top = 152
Width = 778
Top = 224
Width = 731
AutoSize = True
Caption = 'Trainer generator'
ClientHeight = 412
ClientWidth = 778
ClientWidth = 731
Constraints.MinHeight = 310
Constraints.MinWidth = 690
OnClose = FormClose
Expand All @@ -23,11 +23,11 @@ object frmTrainerGenerator: TfrmTrainerGenerator
Left = 0
Height = 412
Top = 0
Width = 285
Width = 238
Anchors = [akTop, akLeft, akRight, akBottom]
AutoSize = True
ClientHeight = 412
ClientWidth = 285
ClientWidth = 238
TabOrder = 0
OnResize = Panel2Resize
object lvCheats: TListView
Expand All @@ -37,7 +37,7 @@ object frmTrainerGenerator: TfrmTrainerGenerator
Left = 1
Height = 377
Top = 1
Width = 261
Width = 214
Align = alCustom
Anchors = [akTop, akLeft, akRight, akBottom]
Columns = <
Expand All @@ -60,11 +60,11 @@ object frmTrainerGenerator: TfrmTrainerGenerator
Left = 1
Height = 33
Top = 378
Width = 283
Width = 236
Align = alBottom
AutoSize = True
ClientHeight = 33
ClientWidth = 283
ClientWidth = 236
TabOrder = 1
object btnDelete: TButton
AnchorSideLeft.Control = Panel3
Expand Down Expand Up @@ -103,7 +103,7 @@ object frmTrainerGenerator: TfrmTrainerGenerator
end
end
object Panel4: TPanel
Left = 262
Left = 215
Height = 377
Top = 1
Width = 22
Expand Down Expand Up @@ -177,7 +177,7 @@ object frmTrainerGenerator: TfrmTrainerGenerator
AnchorSideRight.Side = asrBottom
AnchorSideBottom.Control = Owner
AnchorSideBottom.Side = asrBottom
Left = 285
Left = 238
Height = 412
Top = 0
Width = 493
Expand Down
Loading

0 comments on commit 0e30e50

Please sign in to comment.