zori.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # encoding: UTF-8
  2. # Zori Eruta Edition:
  3. #
  4. # Zori is a GUI library for mruby and the Eruta engine.
  5. # beoran@gmail.com, 2009, 2010
  6. #
  7. # Zori (japanese footwear) follows the Shoes philosophy in that it sees
  8. # width as largely static, and height as dynamic and scrollable because
  9. # of the mouse wheel and pageup/pagedown keys.
  10. #
  11. # Zori is oriented towards relatively simple UIs.
  12. # Therefore, there is only one top-level widget, the Root widget.
  13. # This widget, in turn contains several Pages, of which only
  14. # one can be active at the same time.
  15. # The default page is used for the in game HUD, NPC or PC dialog and
  16. # status display. Other pages are used for the various menus.
  17. # Every page can contain can contain any amount of child widgets.
  18. # Widgets in turn may or may not contain sub-widgets.
  19. #
  20. # The box model used in Zori is as follows:
  21. # ......................................
  22. # .margin .
  23. # . +-------------border-----------+ .
  24. # . | padding | .
  25. # . | .......................... | .
  26. # . | . . | .
  27. # . | . contents . | .
  28. # . | . . | .
  29. # . | .......................... | .
  30. # . | padding | .
  31. # . +------------------------------+ .
  32. # .margin .
  33. # ......................................
  34. #
  35. # The padding determines the minimal distance between the border or the
  36. # parent widget and it's contents and/or child widgets.
  37. #
  38. # The border's thickness is only relevant for visual effects. It does not change
  39. # the layout. The border is effectively "inside" the padding of the widget.
  40. #
  41. # The margin of a widget determines how closely that widget may be packed
  42. # to it's sibling widgets.
  43. #
  44. # The work in Zori is divided between Root, Page, and Widget. The Root class,
  45. # of which the root widget is the only instance, handles everything that
  46. # depends on and/or may influence several widgets at once, such as event
  47. # dispatching but also setting the focus, determining which widget is
  48. # being hovered, or dragged, etc. The latter functions
  49. # change the state of several widgets, so they are handled on the level of
  50. # the Hanao class.
  51. # The Widget module and the widget classes that include it the individual
  52. # state and actions of the various widgets individually.
  53. #
  54. # Widget state:
  55. # A widget can be considered a state machine with the following states:
  56. # * hidden : Invisible (implies inactive)
  57. # * inactive: Will ignore input
  58. # * active : Ready to receive input
  59. # * marked : Currently "hovered" by keyboard cursor. Only one widget can be
  60. # marked.
  61. # * selected: Previously "pressed" on and still relevantly "selected".
  62. # * hovered: Currently "hovered" by mouse cursor. Only one widget can be hovered
  63. # at most.
  64. # * focused : This widget and it's children receive keyjoy input excusively
  65. # some simpler widgets such as buttons cannot be focused since they
  66. # perform their action immediately when "clicked". Only
  67. # one widget can be focused a the same time.
  68. # * triggered:Widgets that can't be focused become triggered in stead while
  69. # they perform their action.
  70. # * dragged : The widget is being dragged.
  71. #
  72. # The all states and mutually exclusive except for the "marked" and "hovered"
  73. # state which are different since one widget may be e.g. focused and marked
  74. # and hovered at the same time. Yet different is the selected flag,
  75. # which keeps track of previously marked items such as the marked parent menu,
  76. # or for exammple, when swapping two items in an item list, etc.
  77. # Therefore marked and hovered are separate flag from the rest of the states.
  78. #
  79. # Allowed transitions:
  80. # * hide : Set to hidden state.
  81. # * show : Set to active state from hidden state.
  82. # * disable : Set to inactive state from active state or higher.
  83. # * enable : Set to active state from inactive state.
  84. # * mark : Set to marked state.
  85. # * focus : Set to focused state.
  86. # * trigger : Set to triggered state.
  87. #
  88. # The state is orthogonal with the value of the widget. A pushbutton or
  89. # checkbox will have a boolean value, a slider a numerical one,
  90. # and a text boxt a string value.
  91. #
  92. module Zori
  93. DEBUG = true
  94. # We define structs for stuff that's too simple to need classes yet.
  95. # Structure for mouse button info
  96. MouseButtonInfo = Struct.new(:button, :pressed, :released)
  97. # Structure for Click Info
  98. ClickInfo = Struct.new(:button, :widget, :when)
  99. # Structure for Drag Info
  100. DragInfo = Struct.new(:button, :widget, :when)
  101. # Class that models the state of a single key
  102. KeyInfo = Struct.new(:pressed, :sym, :mod, :text, :repeated)
  103. # script 'zori/rect.rb'
  104. # script 'zori/handler.rb'
  105. # script 'zori/element.rb'
  106. script 'zori/style.rb'
  107. script 'zori/state.rb'
  108. script 'zori/capability.rb'
  109. script 'zori/graphic.rb'
  110. script 'zori/widget.rb'
  111. script 'zori/draggable.rb'
  112. script 'zori/button.rb'
  113. script 'zori/longtext.rb'
  114. script 'zori/menu.rb'
  115. script 'zori/page.rb'
  116. script 'zori/root.rb'
  117. # script 'zori/mouse.rb'
  118. # script 'zori/joystick.rb'
  119. # script 'zori/keyboard.rb'
  120. # script 'zori/control.rb'
  121. # script 'zori/hanao.rb'
  122. # script 'zori/sizemixin.rb'
  123. # script 'zori/layoutmixin.rb'
  124. # script 'zori/flowlayout.rb'
  125. # script 'zori/stacklayout.rb'
  126. # script 'zori/fixedlayout.rb'
  127. # script 'zori/widget.rb'
  128. #
  129. # script 'zori/button.rb'
  130. # script 'zori/checkbox.rb'
  131. # script 'zori/flow.rb'
  132. # script 'zori/frame.rb'
  133. # script 'zori/input.rb'
  134. # script 'zori/label.rb'
  135. # script 'zori/mainwidget.rb'
  136. # script 'zori/menu.rb'
  137. # script 'zori/menu.rb'
  138. # script 'zori/menuitem.rb'
  139. # script 'zori/progress.rb'
  140. # script 'zori/radiobutton.rb'
  141. # script 'zori/ring.rb'
  142. #
  143. # script 'zori/slider.rb'
  144. # script 'zori/stack.rb'
  145. #
  146. # script 'zori/style.rb'
  147. # script 'zori/menu.rb'
  148. # script 'zori/text.rb'
  149. # script 'zori/shortcut.rb'
  150. # # script 'zori/mapeditor.rb'
  151. # script 'zori/mouse.rb'
  152. # script 'zori/keyboard.rb'
  153. # script 'zori/joystick.rb'
  154. #
  155. # script 'zori/dialog.rb'
  156. # script 'zori/console.rb'
  157. # Non-mouse cursor graph
  158. def self.cursor_graph
  159. self.root.cursor_graph
  160. end
  161. # Non-mouse cursor image
  162. def self.cursor_image
  163. self.root.cursor_image
  164. end
  165. # Mouse cursor graph
  166. def self.mouse_graph
  167. self.root.mouse_graph
  168. end
  169. # Mouse cursor image
  170. def self.mouse_image
  171. self.root.mouse_image
  172. end
  173. # Default font for the UI.
  174. def self.font
  175. self.root.font
  176. end
  177. # Closes the UI subsystem.
  178. def self.close
  179. @root = nil
  180. end
  181. # Initializes the UI subsystem
  182. def self.open()
  183. @root = Zori::Root.new()
  184. return @root
  185. end
  186. # Returns the root, that is the active top-level controller of the UI
  187. def self.root
  188. unless @root
  189. self.open
  190. end
  191. return @root
  192. end
  193. # Makes a new UI page
  194. def self.make_page(name)
  195. page = Zori::Page.new(:name => name)
  196. yield page if block_given?
  197. @root.register(page)
  198. return page
  199. end
  200. # Activate a given UI page.
  201. def self.go(name)
  202. self.root.go(name)
  203. end
  204. # Looks up a registered page by page name
  205. def self.[](page_name)
  206. return self.root[page_name]
  207. end
  208. # Send an event to the UI subsystem. This may NOT be named on_poll
  209. # or we get infinite recursion due to inheritance from Object.
  210. def self.on_event(*args)
  211. return self.root.on_event(*args)
  212. end
  213. end