Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


move an object inside the Form

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Roman012
!BEWARE! Deletes post on answer
Reputation: 0

Joined: 25 Dec 2022
Posts: 37

PostPosted: Wed Jul 12, 2023 5:46 am    Post subject: move an object inside the Form Reply with quote

is it possible to move an object inside a form?
Like for example
function UDF1_FormMouseDown(sender, button, x, y)
UDF1.dragNow()
end



c..png
 Description:
 Filesize:  5.33 KB
 Viewed:  1811 Time(s)

c..png


Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1531

PostPosted: Wed Jul 12, 2023 1:08 pm    Post subject: Reply with quote

Code sample that @Corroder shared sometime.
You can now apply it to all objects in the Form.

Code:
if fm1 then fm1.Destroy() fm1=nil end

fm1 = createForm()
fm1.Height=400 fm1.Width=450

p1 = createPanel(fm1)
p1.Name="p1"
p1.Height=60 p1.Width=80
p1.Top=50 p1.Left=100

p2 = createPanel(fm1)
p2.Name="p2"
p2.Height=60 p2.Width=80
p2.Top=250 p2.Left=180

local moving = false

function dragObj(obj)
 obj.OnMouseDown=function(sender, button, x, y)
  moving=true
  start={}
  start.x,start.y=sender.clientToScreen(x,y)
  start.relx=x
  start.rely=y

end

obj.OnMouseMove=function(sender, x, y)
  if moving then
    local cx,cy=sender.clientToScreen(x,y)
    local px,py=sender.parent.ScreenToClient(cx,cy)
    local newx,newy
    newx=px-start.relx
    newy=py-start.rely
    print((obj.Name..".Left=%d \n"..obj.Name..".Top=%d\n\n"):format(newx,newy))
    sender.Left=newx
    sender.Top=newy
  end
end

obj.OnMouseUp=function(sender, button, x, y)
  moving=false
end
end

--use
--dragObj(UDF1.CEPanel1)
dragObj(p1)
dragObj(p2)

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Wed Jul 12, 2023 8:46 pm    Post subject: Reply with quote

Another primitives way. Without mouse trail effect.
Code:

---GUI FORM----
if f then f.destroy() end
f = createForm()
f.Caption = 'Whatever...'

lbl = createLabel(f)
lbl.Left = 10
lbl.Caption = 'My Label'

edt = createEdit(f)
edt.Left = 10
edt.Top = 60
edt.Text = 'My Edit Box'

pnl = createPanel(f)
pnl.Left = 100
pnl.Caption = 'My Panel'

-- Control ---
local down = false

function ObjMouseDown(sender, x, y)
 down = true
end

function ObjMouseMove(sender, x, y)
 if down == true then
 local x,y = getMousePos()
 x,y = f.ScreenToClient(x,y) --- change form name here
 sender.Top = y
 sender.Left = x
 end
end

function ObjMouseUp(sender, x, y)
 down = false
end

f.show()

lbl.OnMouseDown = ObjMouseDown
lbl.OnMouseMove = ObjMouseMove
lbl.OnMouseUp = ObjMouseUp

edt.OnMouseDown = ObjMouseDown
edt.OnMouseMove = ObjMouseMove
edt.OnMouseUp = ObjMouseUp

pnl.OnMouseDown = ObjMouseDown
pnl.OnMouseMove = ObjMouseMove
pnl.OnMouseUp = ObjMouseUp


Your home work, try to put the sender (components/objects) as a global variable. So, when call the drag function just need e.q :

Code:
obj.OnMouseDown = ObjMouseDown
obj.OnMouseMove = ObjMouseMove
obj.OnMouseUp = ObjMouseUp


see @Aylin example above

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Roman012
!BEWARE! Deletes post on answer
Reputation: 0

Joined: 25 Dec 2022
Posts: 37

PostPosted: Thu Jul 13, 2023 2:20 am    Post subject: Reply with quote

Many thanks
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1531

PostPosted: Fri Jul 14, 2023 12:51 pm    Post subject: Reply with quote

Just boredom.
Add the code between the marked lines to your existing code.
Just edit the Trainer.Form name. (fm1 = UDF1 etc.)

Code:
--############################################################################--
--############################# Your drag code  ##############################--
local selObj = ""

function dragObj()
  if isKeyPressed(VK_LEFT) then
    selObj.Left = selObj.Left - 1 -- 2 or 10 lines..
    print((selObj.Name..".Left=%d \n"..selObj.Name..".Top=%d\n\n"):format(selObj.Left,selObj.Top))
  end
  if isKeyPressed(VK_RIGHT) then
    selObj.Left = selObj.Left + 1
    print((selObj.Name..".Left=%d \n"..selObj.Name..".Top=%d\n\n"):format(selObj.Left,selObj.Top))
  end
  if isKeyPressed(VK_UP) then
    selObj.Top = selObj.Top - 1
    print((selObj.Name..".Left=%d \n"..selObj.Name..".Top=%d\n\n"):format(selObj.Left,selObj.Top))
  end
  if isKeyPressed(VK_DOWN) then
    selObj.Top = selObj.Top + 1
    print((selObj.Name..".Left=%d \n"..selObj.Name..".Top=%d\n\n"):format(selObj.Left,selObj.Top))
  end
end

if keyTim1 then keyTim1.Destroy() keyTim1=nil end
keyTim1 = createTimer() keyTim1.Interval=100 keyTim1.Enabled=false

-- fm1 = UDF1 --or your Trainer form name ..

fm1.OnClick=function()  -- UDF1.OnClick
  if selObj=="" then
    print("Please click and select object!")
  else
    if keyTim1.Enabled==false then
      keyTim1.Enabled=true
    else
     keyTim1.Enabled=false
    end
  end
end

for i = 0, fm1.ComponentCount - 1 do -- UDF1.ComponentCount
 local obj1 = ""
  print(fm1.Component[i].Name)
  obj1 = fm1.Component[i]
  obj1.OnClick=function() selObj=obj1 print(obj1.Name) end
end

keyTim1.OnTimer=dragObj
--############################################################################--
--############################################################################--


How to use?
1) Click on the object you want to relocate. (If all goes well, it will print the name of the clicked object.)

2) Click on an empty area of the form. (The timer will run to read the gesture keys. Click the form again to stop.)

3) Use the Up, Down, Left and Right keys on the keyboard. The object you select will move according to the given command.

test:

Code:
if fm1 then fm1.Destroy() fm1=nil end

fm1 = createForm()
fm1.Height=400 fm1.Width=450
fm1.PopupMode=0

p1 = createPanel(fm1)
p1.Name="p1"
p1.Height=60 p1.Width=80
p1.Top=50 p1.Left=100

p2 = createPanel(fm1)
p2.Name="p2"
p2.Height=60 p2.Width=80
p2.Top=250 p2.Left=180

--############################################################################--
--############################# Your drag code  ##############################--
local selObj = ""

function dragObj()
  if isKeyPressed(VK_LEFT) then
    selObj.Left = selObj.Left - 1 -- 2 or 10 lines..
    print((selObj.Name..".Left=%d \n"..selObj.Name..".Top=%d\n\n"):format(selObj.Left,selObj.Top))
  end
  if isKeyPressed(VK_RIGHT) then
    selObj.Left = selObj.Left + 1
    print((selObj.Name..".Left=%d \n"..selObj.Name..".Top=%d\n\n"):format(selObj.Left,selObj.Top))
  end
  if isKeyPressed(VK_UP) then
    selObj.Top = selObj.Top - 1
    print((selObj.Name..".Left=%d \n"..selObj.Name..".Top=%d\n\n"):format(selObj.Left,selObj.Top))
  end
  if isKeyPressed(VK_DOWN) then
    selObj.Top = selObj.Top + 1
    print((selObj.Name..".Left=%d \n"..selObj.Name..".Top=%d\n\n"):format(selObj.Left,selObj.Top))
  end
end

if keyTim1 then keyTim1.Destroy() keyTim1=nil end
keyTim1 = createTimer() keyTim1.Interval=100 keyTim1.Enabled=false

-- fm1 = UDF1 --or your Trainer form name ..

fm1.OnClick=function()  -- UDF1.OnClick
  if selObj=="" then
    print("Please click and select object!")
  else
    if keyTim1.Enabled==false then
      keyTim1.Enabled=true
    else
     keyTim1.Enabled=false
    end
  end
end

for i = 0, fm1.ComponentCount - 1 do -- UDF1.ComponentCount
 local obj1 = ""
  print(fm1.Component[i].Name)
  obj1 = fm1.Component[i]
  obj1.OnClick=function() selObj=obj1 print(obj1.Name) end
end

keyTim1.OnTimer=dragObj
--############################################################################--
--############################################################################--



I'm just testing different ideas. Wink

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites