state.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. module Zori
  2. # State manages the current state of a widget or Hanao.
  3. module State
  4. def initialize(params={}, &block)
  5. super(params, &block)
  6. @state = :active
  7. @mark = false
  8. @hover = false
  9. end
  10. # Hides the widget and all it's children
  11. def hide
  12. @components.each do |comp|
  13. comp.hide_component
  14. end
  15. @state = :hidden
  16. unmark
  17. unhover
  18. hide_graph
  19. end
  20. # Hides a child component. This hides only the graph, and does not
  21. # touch the @hidden field
  22. def hide_component
  23. @components.each do |comp|
  24. comp.hide_component
  25. end
  26. hide_graph
  27. end
  28. # Shows the widget and all it's children
  29. def show
  30. @components.each do |comp|
  31. comp.show_component
  32. end
  33. @state = :active
  34. show_graph
  35. end
  36. # Shows a child component, but only if it isn't hidden itself.
  37. def show_component
  38. if hidden?
  39. return
  40. end
  41. @components.each do |comp|
  42. comp.show_component
  43. end
  44. show_graph
  45. end
  46. # Moves the component and all it's children to (x, y)
  47. def move_to(x, y)
  48. @x = x
  49. @y = y
  50. @components.each do |comp|
  51. comp.move_to(x, y)
  52. end
  53. move_graph(x, y)
  54. end
  55. # Enables the widget, has no effect on the children
  56. def enable
  57. @state = :active
  58. end
  59. # Disables the widget, has no effect on the children.
  60. def disable
  61. @state = :disabled
  62. end
  63. # Marks the widget. Has no effect on the children.
  64. # Does nothing on non-active widgets.
  65. def mark
  66. return false if disabled?
  67. @mark = true
  68. return true
  69. end
  70. # Removes the mark from the widget. Has no effect on the children.
  71. # Does noting on non-marked widgets.
  72. def unmark
  73. return false unless mark?
  74. @mark = false
  75. return true
  76. end
  77. # Hovers the widget. Has no effect on the children.
  78. # Does nothing on non-active widgets.
  79. def hover
  80. return false if disabled?
  81. @hover = true
  82. return true
  83. end
  84. # Removes the hover flag from the widget. Has no effect on the children.
  85. # Does noting on non-marked widgets.
  86. def unhover
  87. return false unless hover?
  88. @hover = false
  89. return true
  90. end
  91. # Selects the widget. Has no effect on the children.
  92. # Does nothing on non-active widgets.
  93. def select
  94. return false if disabled?
  95. @select = true
  96. @select_mark = Zori.root.create_select_mark
  97. @select_mark.position = self.left - 20, self.y
  98. graph_add(@select_mark)
  99. return true
  100. end
  101. # Removes the hover flag from the widget. Has no effect on the children.
  102. # Does noting on non-marked widgets.
  103. def unselect
  104. return false unless selected?
  105. @select = false
  106. graph_delete(@select_mark)
  107. @select_mark.close
  108. @select_mark = nil
  109. return true
  110. end
  111. # Focuses the widget. Has no effect on the children.
  112. # Does nothing on non-active or non-marked widgets.
  113. def focus
  114. return false if disabled?
  115. @state = :focus
  116. return true
  117. end
  118. # Removes the focus from the widget. Has no effect on the children.
  119. # Does noting on non-focused widgets.
  120. def unfocus
  121. return false unless focus?
  122. @state = :active
  123. return true
  124. end
  125. # Sets the dragging state to the widget. Has no effect on the children.
  126. # Does noting on disabled widgets.
  127. def drag
  128. return false if disabled?
  129. @state = :drag
  130. return true
  131. end
  132. # Removes the dragging state from the widget. Has no effect on the children.
  133. # Does noting on non-dragging widgets.
  134. def drop
  135. return false unless drag?
  136. @state = :active
  137. return true
  138. end
  139. # Checks if the widget is in a certain state.
  140. def is?(qstate)
  141. @state == qstate
  142. end
  143. def hidden?
  144. return is?(:hidden)
  145. end
  146. # Is the object disabled or hidden (which implies disabled)
  147. def disabled?
  148. return is?(:disabled) || is?(:hidden)
  149. end
  150. # Is the object being dragged?
  151. def drag?
  152. return is?(:drag)
  153. end
  154. # Does the object have focus?
  155. def focus?
  156. return is?(:focus)
  157. end
  158. # Is the object active?
  159. def active?
  160. return is?(:active)
  161. end
  162. # Is the object being triggered?
  163. def trigger?
  164. return is?(:trigger)
  165. end
  166. # Is the object being marked?
  167. def mark?
  168. return @mark
  169. end
  170. # Is the object being hovered?
  171. def hover?
  172. return @hover
  173. end
  174. # Is the object selected?
  175. def selected?
  176. return @select
  177. end
  178. end
  179. end