draggable.rb 923 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. module Zori
  2. # A widget may be draggable
  3. module Draggable
  4. def on_mouse_axes(t, x, y, z, w, dx, dy, dz, dw)
  5. super rescue nil
  6. # super if defined? super doesn't seem to work yet or maybe I should
  7. # update my ruby
  8. if drag?
  9. move_to(x - @drag_dx, y - @drag_dy)
  10. end
  11. return false # don't consume the event
  12. end
  13. def on_mouse_button_down(t, x, y, z, w, b)
  14. super rescue nil
  15. return false unless self.inside?(x, y)
  16. unless @action
  17. self.drag
  18. @drag_dx = x - self.x
  19. @drag_dy = y - self.y
  20. puts "Click! #{drag?}"
  21. return true
  22. end
  23. return false
  24. end
  25. def on_mouse_button_up(t, x, y, z, w, b)
  26. super rescue nil
  27. self.drop if self.drag?
  28. puts "Release! #{drag?}"
  29. return false unless self.inside?(x, y)
  30. end
  31. def can_drag?
  32. return true
  33. end
  34. end
  35. end