Advertisement
Guest User

Virtual joystick with QML

a guest
Oct 15th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     import QtQuick 2.0
  2.      
  3.     Item {
  4.         signal pressed(real x, real y)
  5.         signal moved(real x, real y)
  6.         signal released
  7.         id: joystick
  8.         width: 160
  9.         height: 160
  10.         onPressed: console.log("Pressed:", x, y)
  11.         onMoved: console.log("Moved:", x, y)
  12.         onReleased: console.log("Released")
  13.         Image {
  14.             id: backgroundImage
  15.             anchors.fill: parent
  16.             source: "background.png"
  17.         }
  18.         Item {
  19.             id: finger
  20.             width: 26
  21.             height: 26
  22.             x: 80-13
  23.             y: 80-13
  24.             Image {
  25.                 id: fingerImage
  26.                 anchors.fill: parent
  27.                 source: "finger.png"
  28.             }
  29.         }
  30.         MouseArea {
  31.             id: joystickArea
  32.             anchors.fill: parent
  33.             property int signX: 1
  34.             property int signY: 1
  35.             PropertyAnimation {
  36.                 id: returnToOrigin
  37.                 target: finger
  38.                 properties: "x,y"
  39.                 to: 80-13
  40.                 duration: 100
  41.             }
  42.             onPressed: joystick.pressed(joystickArea.mouseX, joystickArea.mouseY)
  43.             onPositionChanged: {
  44.                 if (Math.pow(joystickArea.mouseX-80, 2) + Math.pow(joystickArea.mouseY-80, 2) < Math.pow(80-13, 2)) {
  45.                     finger.x = joystickArea.mouseX-13
  46.                     finger.y = joystickArea.mouseY-13
  47.                     80 - joystickArea.mouseX > 0 ? signX = 1 : signX = -1
  48.                     80 - joystickArea.mouseY > 0 ? signY = 1 : signY = -1
  49.                     joystick.moved(-(80+13*signX-joystickArea.mouseX)/80, (80+13*signY-joystickArea.mouseY)/80)
  50.                 } else {
  51.                     // else what?
  52.                 }
  53.             }
  54.             onReleased: {
  55.                 returnToOrigin.start()
  56.                 joystick.released()
  57.             }
  58.         }
  59.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement