menu.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. module Zori
  2. # A menu is a control that can contain several rows and/or columns
  3. # of buttons
  4. class Menu
  5. include Widget
  6. # returns the last/currently marked item for the menu.
  7. attr_reader :marked
  8. def initialize(params={}, &block)
  9. @marked = 0
  10. super(params, &block)
  11. @heading = params[:heading]
  12. @bg = graph_box(x, y, w, h)
  13. @bg.border_thickness = 0
  14. @bg.border_color = [255, 255, 255, 128]
  15. @bg.background_color = [0, 0, 255, 128]
  16. if @heading
  17. @tg = graph_text(x, y, w, h, @heading)
  18. @tg.margin = 3
  19. @tg.text_flags = Eruta::ALIGN_CENTER
  20. @tg.font = Eruta::Zori.font.id
  21. @tg.background_color = [0,0,0]
  22. @tg.color = [255,255, 64]
  23. end
  24. @action = block
  25. self.unhover
  26. end
  27. def on_resize
  28. @bg.position= @x, @y
  29. @bg.size = @w, @h
  30. end
  31. def mark_index(index)
  32. return nil if disabled?
  33. size = @components.size
  34. return nil if size < 1
  35. return nil if index < 0
  36. return nil if index >= size
  37. @marked = index
  38. Zori.root.mark_widget(@components[@marked])
  39. @components[@marked].mark
  40. return @marked
  41. end
  42. def mark_previous
  43. return nil if disabled?
  44. size = @components.size
  45. return nil if size < 1
  46. newmark = @marked - 1
  47. if newmark < 0
  48. newmark = size - 1
  49. end
  50. return mark_index(newmark)
  51. end
  52. def mark_next
  53. return nil if disabled?
  54. size = @components.size
  55. return nil if size < 1
  56. newmark = @marked + 1
  57. if newmark >= size
  58. newmark = 0
  59. end
  60. return mark_index(newmark)
  61. end
  62. def mark_recall
  63. return nil if disabled?
  64. newmark = @marked || 0
  65. return mark_index(newmark)
  66. end
  67. def mark_first
  68. return nil if disabled?
  69. return mark_index(0)
  70. return true
  71. end
  72. def activate_marked
  73. return nil if disabled?
  74. size = @components.size
  75. return nil if size < 1
  76. @components[@marked].trigger
  77. return true
  78. end
  79. def unselect_marked
  80. return nil if disabled?
  81. size = @components.size
  82. return nil if size < 1
  83. @components[@marked].unselect
  84. return true
  85. end
  86. def previous_menu
  87. return nil if disabled?
  88. if @parent && @parent.parent && @parent.parent.is_a?(Zori::Menu)
  89. self.hide
  90. @parent.parent.unselect_marked
  91. @parent.parent.mark_recall
  92. end
  93. return true
  94. end
  95. def on_key_down(*args)
  96. time, key = *args
  97. case key
  98. when KEY_UP
  99. return mark_previous
  100. when KEY_DOWN
  101. return mark_next
  102. when KEY_ENTER, KEY_SPACE, KEY_LCTRL, KEY_RCTRL
  103. return activate_marked
  104. when KEY_BACKSPACE, KEY_LSHIFT, KEY_RSHIFT
  105. return previous_menu
  106. else
  107. p "#{self} on_key_down", time, key
  108. end
  109. end
  110. def on_enter
  111. return false if disabled?
  112. mark_recall
  113. return false
  114. end
  115. def can_drag?
  116. return false
  117. end
  118. def fit_children
  119. super
  120. mark_first
  121. end
  122. end
  123. end