button.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Button widget.
  2. module Zori
  3. class Button
  4. include Widget
  5. BUTTON_BACKGROUND = [0x33, 0x33, 0xff, 0xaa]
  6. HOVER_BACKGROUND = [0x55, 0x55, 0xff, 0xaa]
  7. def initialize(params={}, &block)
  8. super(params, &block)
  9. heading = params[:heading]
  10. @heading = heading
  11. @bg = graph_box(@x, @y, @w, @h)
  12. @bg.border_thickness = 0
  13. @bg.border_color = [255, 255, 255, 128]
  14. @bg.background_color = BUTTON_BACKGROUND
  15. @tg = graph_text(@x, @y, @w, @h, heading)
  16. @tg.text_flags = Eruta::ALIGN_CENTER
  17. @tg.font = Eruta::Zori.font.id
  18. @tg.background_color = [0,0,0]
  19. @tg.color = [255,255, 64]
  20. @tg.margin = 1
  21. self.unhover
  22. end
  23. def on_mouse_in(x, y, from)
  24. super(x, y)
  25. @pushed = false # need to check for mouse pressed or not
  26. end
  27. def on_mouse_out(x, y, to)
  28. super(x, y)
  29. @pushed = false
  30. end
  31. def marked_style
  32. @bg.border_thickness = 1
  33. @bg.background_color = HOVER_BACKGROUND
  34. end
  35. def unmarked_style
  36. @bg.border_thickness = 0
  37. @bg.background_color = BUTTON_BACKGROUND
  38. end
  39. def hover
  40. marked_style
  41. super
  42. end
  43. def unhover
  44. unless mark?
  45. unmarked_style
  46. end
  47. super
  48. end
  49. def mark
  50. marked_style
  51. super
  52. end
  53. def unmark
  54. unless hover?
  55. unmarked_style
  56. end
  57. super
  58. end
  59. def on_mouse_axes(t, x, y, z, w, dx, dy, dz, dw)
  60. # Check for hovering.
  61. if self.inside?(x, y)
  62. hover
  63. else
  64. unhover
  65. end
  66. return false # don't consume the event
  67. end
  68. def trigger
  69. if @components.first.is_a? Zori::Menu
  70. menu = @components.first
  71. menu.show
  72. menu.mark_recall
  73. self.select
  74. return true
  75. end
  76. super
  77. return true
  78. end
  79. def on_mouse_button_down(t, x, y, z, w, b)
  80. return false unless self.inside?(x, y)
  81. self.trigger
  82. end
  83. def can_drag?
  84. return false
  85. end
  86. end
  87. end