cursor.script 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. -- step animation forward or backwards 'amount' number of frames
  2. local function step(self, amount)
  3. -- frame_count is the number of frames in the current animationm
  4. local frame_count = go.get("#sprite", "frame_count")
  5. -- cursor is the normalized (0.0 to 1.0) animation cursor
  6. local cursor = go.get("#sprite", "cursor")
  7. -- normalized length of a frame in the current animation
  8. local frame_length = 1 / frame_count
  9. -- move the cursor amount number of frames
  10. cursor = cursor + (frame_length * amount)
  11. -- wrap animation if advancing beyond the first or last frame
  12. if cursor < 0 then
  13. cursor = cursor + 1
  14. elseif cursor >= 1 then
  15. cursor = cursor - 1
  16. end
  17. -- set new sprite cursor position
  18. go.set("#sprite", "cursor", cursor)
  19. -- calculate the current animation frame and show on a label
  20. local current_frame = 1 + math.floor(cursor * frame_count)
  21. label.set_text("#frame", ("%d / %d"):format(current_frame, frame_count))
  22. end
  23. -- stop automatic animation playback
  24. local function stop(self)
  25. -- only try to stop if there is an active timer
  26. if self.playback_timer then
  27. -- visually update the start/stop sprite to show the 'start image
  28. sprite.play_flipbook("controls#playstop", "start")
  29. -- cancel the animation timer
  30. timer.cancel(self.playback_timer)
  31. self.playback_timer = nil
  32. end
  33. end
  34. -- start automatic animation playback using a timer to advance the animation
  35. -- one frame at a time
  36. local function start(self)
  37. -- visually update the start/stop sprite to show the 'stop' image
  38. sprite.play_flipbook("controls#playstop", "stop")
  39. -- start a timer to advance the animation roughly every 0.15 seconds
  40. self.playback_timer = timer.delay(0.15, true, function()
  41. step(self, 1)
  42. end)
  43. end
  44. function init(self)
  45. msg.post(".", "acquire_input_focus")
  46. self.playback_timer = nil
  47. end
  48. function on_input(self, action_id, action)
  49. if action.pressed then
  50. -- key left or mouse click on left part of screen
  51. -- step animation one frame backwards
  52. if action_id == hash("key_left")
  53. or action_id == hash("mouse_button_left") and action.x < 240
  54. then
  55. stop(self)
  56. step(self, -1)
  57. return
  58. end
  59. -- key right or mouse click on right part of the screen
  60. -- step animation one frame forward
  61. if action_id == hash("key_right")
  62. or action_id == hash("mouse_button_left") and action.x > 480 then
  63. stop(self)
  64. step(self, 1)
  65. return
  66. end
  67. -- key space or mouse click in central part of the screen
  68. -- start or stop animation playback
  69. if action_id == hash("key_space")
  70. or action_id == hash("mouse_button_left") and action.x > 240 and action.x < 480 then
  71. if self.playback_timer then
  72. stop(self)
  73. else
  74. start(self)
  75. end
  76. end
  77. end
  78. end