controller.script 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. local lookup = require "example.easing_functions"
  2. local PREVIOUS = -1; local NEXT = 1
  3. local function change_index_by(index, delta)
  4. index = (index + delta) % #lookup
  5. if index < 1 then
  6. index = #lookup
  7. end
  8. return index
  9. end
  10. local function restart_demo_animations(easing)
  11. msg.post("position_demo", "restart", { easing = easing.value })
  12. msg.post("rotation_demo", "restart", { easing = easing.value })
  13. msg.post("scale_demo", "restart", { easing = easing.value })
  14. end
  15. local function update_gui(self)
  16. msg.post("/hud#gui", "demo_changed", {
  17. index = self.index,
  18. total = #lookup,
  19. easing_name = self.easing.name
  20. })
  21. end
  22. local function change_easing_demo(self, index_change)
  23. self.index = change_index_by(self.index, index_change)
  24. self.easing = lookup.get_by_index(self.index)
  25. restart_demo_animations(self.easing)
  26. update_gui(self)
  27. end
  28. function init(self)
  29. msg.post(".", "acquire_input_focus")
  30. self.index = 1
  31. self.easing = lookup.get_by_index(self.index)
  32. restart_demo_animations(self.easing)
  33. update_gui(self)
  34. end
  35. function final(self)
  36. msg.post(".", "release_input_focus")
  37. end
  38. function on_message(self, message_id, message, sender)
  39. if message_id == hash("prev_easing_demo") then
  40. change_easing_demo(self, PREVIOUS)
  41. elseif message_id == hash("next_easing_demo") then
  42. change_easing_demo(self, NEXT)
  43. end
  44. end
  45. function on_input(self, action_id, action)
  46. if action_id == hash("key_left") and action.pressed then
  47. change_easing_demo(self, PREVIOUS)
  48. elseif action_id == hash("key_right") and action.pressed then
  49. change_easing_demo(self, NEXT)
  50. end
  51. end