Posted 16 years ago on 5/29/2008 and updated 12/27/2008
Take Away: Getting started with moving objects with the mouse.
KB101136
Enabling the user to move objects around during View Data mode is very useful. In ObjectPAL, you can enable the user to move an object around to reveal something behind it, or you can enable the user to move an object to a new location. The following example shows you how to move an object around. When you let go of the mouse button, the mouseUp event occurs and the code in it snaps the object back to its original position. You could use this technique for many tasks. For example, you could use this technique in a game to reveal answers or offer clues in an educational game.
Create a form with several text boxes on it. Give them various frames and colors.
Setup form
Add lines 3-5 to the Var window of the form.
1: ;Form :: Var 2: Var 3: x,y,x1,y1,w,h SmallInt 4: ui UIObject 5: sTargetClass String 6: endVar
Add lines 8-10 to the mouseDown event of the form:
1: ;Form :: mouseDown 2: method mouseDown(var eventInfo MouseEvent) 3: if eventInfo.isprefilter() then 4: ;This code executes for each object on the form 5: 6: else 7: ;This code executes only for the form 8: eventinfo.getTarget(ui) 9: ui.getPosition(x1, y1, w, h) 10: sTargetClass = ui.class 11: endIf 12: endMethod
Add lines 3-5 and 11-17 to the mouseMove event of the form:
1: ;Form :: mouseMove 2: method mouseMove(var eventInfo MouseEvent) 3: var 4: liX, liY LongInt 5: endVar 6: if eventInfo.isprefilter() then 7: ;This code executes for each object on the form 8: 9: else 10: ;This code executes only for the form 11: if eventinfo.isLeftDown() and 12: sTargetClass = "Text" then 13: liX = eventinfo.x() 14: liY = eventinfo.y() 15: ui.getPosition(x, y, w, h) 16: ui.setPosition(x + liX - 400, y + liY - 400, w, h) 17: endIf 18:endIf 19:endMethod
Add line 8 to the mouseUp event of the form:
1: ;Form :: mouseUp 2: method mouseUp(var eventInfo MouseEvent) 3: if eventInfo.isprefilter() then 4: ;This code executes for each object on the form 5: 6: else 7: ;This code executes only for the form 8: ui.setPosition(x1,y1,w,h) 9: endIf 10: endMethod
Check the syntax, save the form as MOVER.FSL, and run the form. Click and drag any text box you placed on the form to move it. When you let go (mouseUp), the object snaps back to its original location.