drag.gui_script 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. function init(self)
  2. msg.post(".", "acquire_input_focus")
  3. -- list of boxes to drag
  4. self.boxes = {
  5. gui.get_node("box1"),
  6. gui.get_node("box2"),
  7. gui.get_node("box3"),
  8. }
  9. -- variable where the currently dragged box is stored
  10. self.dragged_box = nil
  11. end
  12. function on_input(self, action_id, action)
  13. if action_id == hash("touch") then
  14. -- update the position of the currently dragged box
  15. if self.dragged_box then
  16. local mouse_position = vmath.vector3(action.x, action.y, 0)
  17. gui.set_position(self.dragged_box, mouse_position)
  18. end
  19. -- check if the mouse button was pressed
  20. if action.pressed then
  21. -- iterate the list of boxes and check if the mouse was
  22. -- clicked on a box
  23. for i=1,#self.boxes do
  24. local box = self.boxes[i]
  25. -- this will return true if the x and y is within the
  26. -- bounds of the box
  27. if gui.pick_node(box, action.x, action.y) then
  28. -- keep track of the box as being dragged
  29. self.dragged_box = box
  30. break
  31. end
  32. end
  33. -- check if the mouse button was released
  34. -- clear the variable which keeps track of which box is dragged
  35. elseif action.released then
  36. self.dragged_box = nil
  37. end
  38. end
  39. end