root.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. module Zori
  2. # The root widget is the single toplevel widget that holds all
  3. # other sub-widgets.
  4. class Root
  5. include Widget
  6. # There are two cursors, a mouse cursor and a keyboard/joystick one.
  7. # Non-mouse cursor graph
  8. attr_reader :cursor_graph
  9. # Non-mouse cursor image
  10. attr_reader :cursor_image
  11. # Mouse cursor graph
  12. attr_reader :mouse_graph
  13. # Mouse cursor image
  14. attr_reader :mouse_image
  15. # Non-mouse cursor currently "focusing" this widget.
  16. attr_reader :cursor_widget
  17. # Default font for the UI.
  18. attr_reader :font
  19. # Loads the common data for the Root widget.
  20. def load_common_data
  21. @font = Font.load(:font_gui, '/font/unifont.ttf', 16)
  22. @mouse_image = Bitmap.load(:mouse_cursor,
  23. '/image/gin/fountain-pen_32.png')
  24. @mouse_image.average_to_alpha(255,255,255)
  25. @mouse_graph = Graph.make_image(200, 200, @mouse_image.id)
  26. @mouse_graph.z = 9999
  27. @mouse_graph.image_flags = Eruta::FLIP_HORIZONTAL | Eruta::FLIP_VERTICAL
  28. @cursor_image = Bitmap.load(:cursor, '/image/gin/curled-leaf_32.png')
  29. @cursor_image.average_to_alpha(255,255,255)
  30. @cursor_graph = Graph.make_image(100,100, @cursor_image.id)
  31. @cursor_graph.z = 10000
  32. @cursor_graph.image_flags = Eruta::FLIP_HORIZONTAL
  33. # @cursor_graph.speed = [ rand(10) - 5, rand(10) - 5]
  34. Eruta.show_mouse_cursor = false
  35. end
  36. def initialize(params={}, &block)
  37. load_common_data
  38. @pages = {}
  39. @active = nil
  40. @cursor_widget = nil
  41. end
  42. # Creates a select mark graph for any widget
  43. def create_select_mark
  44. select_mark = Graph.make_image(100, 100, @cursor_image.id)
  45. select_mark.z = 9998
  46. select_mark.image_flags = Eruta::FLIP_HORIZONTAL
  47. return select_mark
  48. end
  49. # Unregisters a page
  50. def register(page)
  51. @pages ||= {}
  52. @pages[page.name.to_sym] = page
  53. end
  54. # Registers a page
  55. def unregister(page)
  56. @pages ||= {}
  57. @pages[page.name.to_sym] = nil
  58. end
  59. # Returns the page for the given name.
  60. def for_name(name)
  61. @pages ||= {}
  62. return @pages[name.to_sym]
  63. end
  64. # Looks up a registered page
  65. def [](name)
  66. return for_name(name)
  67. end
  68. # Returns the currently active page or nil for none.
  69. def active
  70. @active
  71. end
  72. # Returns the currently active page id or nil for none.
  73. def active_id
  74. return nil unless @active
  75. return @active.name
  76. end
  77. # Activates the named page, and passes the data if any.
  78. # Returns true on success or false if no such page is registered.
  79. def go(name, data = {})
  80. page = self.for_name(name)
  81. return false unless page
  82. @active.on_leave(name) if @active
  83. @active = page
  84. @active.on_enter(data)
  85. return true
  86. end
  87. # Sends the event to the active page
  88. def on_event(*args)
  89. # move the mouse cursor around in all cases
  90. if args[0] == :mouse_axes
  91. x = args[2]
  92. y = args[3]
  93. @mouse_graph.position = [x, y] if @mouse_graph
  94. end
  95. return nil unless @active
  96. return @active.on_event(*args)
  97. end
  98. # Moves the keyboard cursor to a given position
  99. def move_cursor(x, y)
  100. @cursor_graph.position = x, y
  101. end
  102. # Sets the given widget as the one widget that is marked by the keyboard
  103. # cursor.
  104. def mark_widget(widget)
  105. return false unless widget.mark
  106. @cursor_widget.unmark if @cursor_widget
  107. @cursor_widget = widget
  108. move_cursor(widget.x + widget.w, widget.y)
  109. return true
  110. end
  111. # Sets the given widget as the one that is hovered by the mouse cursor.
  112. def hover_widget(widget)
  113. return false unless widget.hover
  114. @mouse_widget.unmark if @mouse_widget
  115. @mouse_widget = widget
  116. widget.mark
  117. move_cursor(widget.x + widget.w, widget.y)
  118. return true
  119. end
  120. end
  121. end