When moving a component via setLocation(), component will not stay in new position

I have a button, with a script that runs on mouseClicked, which calls event.source.setLocation(x, y). The button does move to the specified location, but it quickly returns to its original position (when in designer), or stays in the new position until you click somewhere inside the window (but not on another component) when running the client.

How do you move components via scripting without them moving back to the original location?

What I am trying to work up to is being able to drag components around inside the client with the mouse.

It seems that system.gui.moveComponent() should be used here instead of setLocation(). Components stay in new position now.

I typically use system.gui.transform for this sort of thing.

2 Likes

system.gui.transform works great, thank you for the suggestion as I found that system.gui.moveComponent worked fine as long as you did not maximize the client (changing the scaling), but as soon as the scaling changed, it would not work as intended.

For context, this is what i ended up with while attempting to make a component "draggable" It seems to work great, but I'm all ears on better ways to accomplish this.

mousePressed:

from java.awt import MouseInfo
c = event.source

cLoc = c.getLocation()
c.pressedPosX = cLoc.getX()
c.pressedPosY = cLoc.getY()

mLoc = MouseInfo.getPointerInfo().getLocation()
c.pressedMousePosX = int(mLoc.getX())
c.pressedMousePosY = int(mLoc.getY())

mouseDragged:

from java.awt import MouseInfo

c = event.source

mLoc = MouseInfo.getPointerInfo().getLocation()
mX = int(mLoc.getX())
mY = int(mLoc.getY())

xMovement = mX - c.pressedMousePosX
yMovement = mY - c.pressedMousePosY

newPosX = c.pressedPosX + xMovement
newPosY = c.pressedPosY + yMovement

system.gui.transform(c, newPosX, newPosY)

3 Likes

Hi Jacob,

Thanks for starting this post. I tried your script on a group (polygon and a numeric label). I put the script in the event handler for the group, (I right clicked on the group and selected scripting..) I pasted the above into the MousePressed and MouseDragged events. I got the following error.

Traceback (most recent call last):
File "event:mousePressed", line 5, in
AttributeError: 'com.inductiveautomation.factorypmi.application.com' object has no attribute 'pressedPosX'

I don't really need to click and drag. I just need to position dynamically on load. I like the added functionality of your solution though. Thinking the user can drag the group to set the new level.

I'm building an HMI for a wetwell and want an indicator(a triangle and a numeric label) for the Lead and Lag start level for the pumps (the group will be dynamically placed on load to a scaled height on the wetwell).

image

In Wonderware this was three clicks and expression. Not sure why I don't have exposure to the x,y postion of the group once I add a Numeric label. I can move a group of polygons all day.

Thanks for any help!

Ok. Just figured out that pressedPosX, pressedMousePosx, et.al. are custom properties of the item I want to move. Works like a charm now!

1 Like