Move layer with mouse
Posted: Tue Sep 24, 2019 10:51 pm
Hi guys,
I feel quite dump to ask this, but ... What I want to do is to have serveral brush layers, click on a one of the layer and move the layer/brush while I hold down the mouse button. Just like you do in standard graphic programms like Photoshop and the like to place the image somewhere else.
So, this is the code I came up with, but 1) it looks very complicated and 2) it does not work well:
Sorry it is not shorter. I tried to shorten it as good as I could. In fact, I use it in a Hollywood display in RapaGUI.
So, the functional problems are that because of SetLayerAnchor() moves the image position, I have to re-position the image, which does not look nice.
Also, when I move the mouse the image follows quite slowly/laggy which is really bad.
So, I assume that this is a standard task to do (let an object follow the mouse instantly). What is the best way to achieve it/what am I missing?
Thanks a lot!
I feel quite dump to ask this, but ... What I want to do is to have serveral brush layers, click on a one of the layer and move the layer/brush while I hold down the mouse button. Just like you do in standard graphic programms like Photoshop and the like to place the image somewhere else.
So, this is the code I came up with, but 1) it looks very complicated and 2) it does not work well:
Code: Select all
@VERSION 8,0
@BRUSH 1, "Limo1_freigestellt.png" ; this is a 100 x 105 px graphic
EnableLayers()
DisplayBrush(1, #CENTER, #CENTER)
mouseIsPressed = False
currentMousePosition = {}
layerID = 0
Function handleMouseEvents(msg)
Switch msg.Action
Case "OnMouseDown":
; get curent mouse position
currentMousePosition = {
x = MouseX(),
y = MouseY()
}
; set flag, rhat mouse button is pressed
mouseIsPressed = True
; get the layer that is at the current mouse position
layerID = GetLayerAtPos(currentMousePosition.x, currentMousePosition.y)
If (layerID > 0)
; get the layer position on the screen
layerXPos = GetAttribute(#LAYER, layerID, #ATTRXPOS)
layerYPos = GetAttribute(#LAYER, layerID, #ATTRYPOS)
; get layer dimensions
Local layerWidth = GetAttribute(#LAYER, layerID, #ATTRWIDTH)
Local layerHeight = GetAttribute(#LAYER, layerID, #ATTRHEIGHT)
; calculate the relative click position on the layer
relativeLayerXPos = currentMousePosition.x - layerXPos
relativeLayerYPos = currentMousePosition.y - layerYPos
; with the calculated relative position set the new anchor point to where you clicked with the mouse
Local newAnchorX = (relativeLayerXPos) / layerWidth
Local newAnchorY = (relativeLayerYPos) / layerHeight
SetLayerAnchor(layerID, newAnchorX, newAnchorY)
; unfortunately as SetLayerAnchor moves the image, set it back to its orginal position
MoveLayer(layerID, #USELAYERPOSITION, #USELAYERPOSITION, layerXPos + relativeLayerXPos, layerYPos + relativeLayerYPos, {Speed = #FASTSPEED})
EndIf
Case "OnMouseUp":
; reset all values and set anchor back to standard
currentMousePosition = {}
mouseIsPressed = False
SetLayerAnchor(layerID, 0, 0)
; unfortunately move the layer back as SetLayerAnchor changes the position
MoveLayer(layerID, #USELAYERPOSITION, #USELAYERPOSITION, #USELAYERPOSITION - relativeLayerXPos, #USELAYERPOSITION - relativeLayerYPos, {Speed = #FASTSPEED})
layerID = 0
Case "OnMouseMove":
If mouseIsPressed = True
If (layerID > 0)
; the layer should follow the mouse now (but it is very laggy)
MoveLayer(layerID, #USELAYERPOSITION, #USELAYERPOSITION, msg.x, msg.y, {Speed = #FASTSPEED})
EndIf
EndIf
EndSwitch
EndFunction
InstallEventHandler({OnMouseUp = handleMouseEvents, OnMouseDown = handleMouseEvents, OnMouseMove = handleMouseEvents})
Repeat
WaitEvent
Forever
So, the functional problems are that because of SetLayerAnchor() moves the image position, I have to re-position the image, which does not look nice.
Also, when I move the mouse the image follows quite slowly/laggy which is really bad.
So, I assume that this is a standard task to do (let an object follow the mouse instantly). What is the best way to achieve it/what am I missing?
Thanks a lot!