Class: Fidgit::GuiState
- Extended by:
- Forwardable
- Defined in:
- lib/fidgit/states/gui_state.rb
Direct Known Subclasses
Constant Summary collapse
- PIXEL_IMAGE =
A 1x1 white pixel used for drawing.
'pixel.png'
Instance Attribute Summary collapse
-
#container ⇒ Packer
readonly
The Container that contains all the elements for this GuiState.
-
#focus ⇒ Element
The element with focus.
Class Method Summary collapse
-
.clear ⇒ Object
Clear the data which is specific to the current $window.
Instance Method Summary collapse
- #clear(*args, &block) ⇒ Object
-
#cursor ⇒ Cursor
The Cursor.
- #distance(x1, y1, x2, y2) ⇒ Object
- #draw ⇒ Object
-
#draw_frame(x, y, width, height, thickness, z, color, mode = :default) ⇒ Object
Draw an unfilled rectangle.
-
#draw_rect(x, y, width, height, z, color, mode = :default) ⇒ Object
Draw a filled rectangle.
-
#file_dialog(type, options = {}, &block) ⇒ Object
Show a file_dialog.
- #finalize ⇒ Object
-
#flush ⇒ Object
Flush all pending drawing to the screen.
- #hide ⇒ Object
-
#hide_menu ⇒ Object
Hides the currently shown menu, if any.
-
#initialize ⇒ GuiState
constructor
A new instance of GuiState.
- #menu(options = {}, &block) ⇒ Object
- #message(text, options = {}) {|result| ... } ⇒ Object
- #setup ⇒ Object
- #show ⇒ Object
-
#show_menu(menu) ⇒ Object
Set the menu pane to be displayed.
-
#t(*args) ⇒ Object
Internationalisation helper.
-
#tool_tip_delay ⇒ Object
Delay, in ms, before a tool-tip will appear.
-
#unset_mouse_over ⇒ Object
Called by active elements when they are disabled.
- #update ⇒ Object
Constructor Details
#initialize ⇒ GuiState
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fidgit/states/gui_state.rb', line 50 def initialize # The container is where the user puts their content. @container = MainPacker.new = nil @last_cursor_pos = [-1, -1] @mouse_over = nil unless defined? @@draw_pixel media_dir = File.(File.join(File.dirname(__FILE__), '..', '..', '..', 'media')) Gosu::Image.autoload_dirs << File.join(media_dir, 'images') Gosu::Sample.autoload_dirs << File.join(media_dir, 'sounds') @@draw_pixel = Gosu::Image.new($window, File.join(media_dir, 'images', PIXEL_IMAGE), true) # Must be tileable or it will blur. @@cursor = Cursor.new end @min_drag_distance = 0 super() add_inputs( left_mouse_button: ->{ (:left) }, holding_left_mouse_button: ->{ (:left) }, released_left_mouse_button: ->{ (:left) }, middle_mouse_button: ->{ (:middle) }, holding_middle_mouse_button: ->{ (:middle) }, released_middle_mouse_button: ->{ (:middle) }, right_mouse_button: ->{ (:right) }, holding_right_mouse_button: ->{ (:right) }, released_right_mouse_button: ->{ (:right) }, mouse_wheel_up: :redirect_mouse_wheel_up, mouse_wheel_down: :redirect_mouse_wheel_down ) end |
Instance Attribute Details
#container ⇒ Packer (readonly)
The Container that contains all the elements for this GuiState.
14 15 16 |
# File 'lib/fidgit/states/gui_state.rb', line 14 def container @container end |
#focus ⇒ Element
The element with focus.
18 19 20 |
# File 'lib/fidgit/states/gui_state.rb', line 18 def focus @focus end |
Class Method Details
.clear ⇒ Object
Clear the data which is specific to the current $window.
92 93 94 95 |
# File 'lib/fidgit/states/gui_state.rb', line 92 def self.clear remove_class_variable '@@cursor' if defined? @@cursor remove_class_variable '@@draw_pixel' if defined? @@draw_pixel end |
Instance Method Details
#clear(*args, &block) ⇒ Object
48 |
# File 'lib/fidgit/states/gui_state.rb', line 48 def clear(*args, &block); @container.clear(*args, &block); end |
#cursor ⇒ Cursor
The Cursor.
22 |
# File 'lib/fidgit/states/gui_state.rb', line 22 def cursor; @@cursor; end |
#distance(x1, y1, x2, y2) ⇒ Object
221 222 223 |
# File 'lib/fidgit/states/gui_state.rb', line 221 def distance(x1, y1, x2, y2) Gosu.distance(x1, y1, x2, y2) end |
#draw ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/fidgit/states/gui_state.rb', line 142 def draw @container.draw .draw if @tool_tip.draw if @tool_tip cursor.draw nil end |
#draw_frame(x, y, width, height, thickness, z, color, mode = :default) ⇒ Object
Draw an unfilled rectangle.
212 213 214 215 216 217 218 219 |
# File 'lib/fidgit/states/gui_state.rb', line 212 def draw_frame(x, y, width, height, thickness, z, color, mode = :default) draw_rect(x - thickness, y, thickness, height, z, color, mode) # left draw_rect(x - thickness, y - thickness, width + thickness * 2, thickness, z, color, mode) # top (full) draw_rect(x + width, y, thickness, height, z, color, mode) # right draw_rect(x - thickness, y + height, width + thickness * 2, thickness, z, color, mode) # bottom (full) nil end |
#draw_rect(x, y, width, height, z, color, mode = :default) ⇒ Object
Draw a filled rectangle.
205 206 207 208 209 |
# File 'lib/fidgit/states/gui_state.rb', line 205 def draw_rect(x, y, width, height, z, color, mode = :default) @@draw_pixel.draw x, y, z, width, height, color, mode nil end |
#file_dialog(type, options = {}, &block) ⇒ Object
Show a file_dialog. (see FileDialog#initialize)
37 38 39 |
# File 'lib/fidgit/states/gui_state.rb', line 37 def file_dialog(type, = {}, &block) FileDialog.new(type, , &block) end |
#finalize ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/fidgit/states/gui_state.rb', line 166 def finalize unset_mouse_over @tool_tip = nil nil end |
#flush ⇒ Object
Flush all pending drawing to the screen.
200 201 202 |
# File 'lib/fidgit/states/gui_state.rb', line 200 def flush $window.flush end |
#hide ⇒ Object
230 231 232 233 |
# File 'lib/fidgit/states/gui_state.rb', line 230 def hide $window.game_state_manager.pop if $window.game_state_manager.current == self nil end |
#hide_menu ⇒ Object
Hides the currently shown menu, if any.
193 194 195 196 197 |
# File 'lib/fidgit/states/gui_state.rb', line 193 def = nil nil end |
#menu(options = {}, &block) ⇒ Object
42 |
# File 'lib/fidgit/states/gui_state.rb', line 42 def ( = {}, &block); MenuPane.new(, &block); end |
#message(text, options = {}) {|result| ... } ⇒ Object
45 |
# File 'lib/fidgit/states/gui_state.rb', line 45 def (text, = {}, &block); MessageDialog.new(text, , &block); end |
#setup ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/fidgit/states/gui_state.rb', line 151 def setup super @tool_tip = nil @mouse_over = nil # Element the mouse is hovering over. @mouse_down_on = Hash.new # Element that each button was pressed over. @mouse_down_pos = Hash.new # Position that each button was pressed down at. = nil @dragging_element = nil @focus = nil @mouse_moved_at = Gosu::milliseconds nil end |
#show ⇒ Object
225 226 227 228 |
# File 'lib/fidgit/states/gui_state.rb', line 225 def show $window.game_state_manager.push self unless $window.game_state_manager.game_states.include? self nil end |
#show_menu(menu) ⇒ Object
Set the menu pane to be displayed.
184 185 186 187 188 189 |
# File 'lib/fidgit/states/gui_state.rb', line 184 def () if = nil end |
#t(*args) ⇒ Object
Internationalisation helper.
89 |
# File 'lib/fidgit/states/gui_state.rb', line 89 def t(*args); I18n.t(*args); end |
#tool_tip_delay ⇒ Object
Delay, in ms, before a tool-tip will appear.
31 32 33 |
# File 'lib/fidgit/states/gui_state.rb', line 31 def tool_tip_delay 500 # TODO: configure this. end |
#unset_mouse_over ⇒ Object
Called by active elements when they are disabled.
175 176 177 178 |
# File 'lib/fidgit/states/gui_state.rb', line 175 def unset_mouse_over @mouse_over.publish :leave if @mouse_over @mouse_over = nil end |
#update ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/fidgit/states/gui_state.rb', line 97 def update cursor.update @tool_tip.update if @tool_tip .update if @container.update # Check menu first, then other elements. new_mouse_over = .hit_element(cursor.x, cursor.y) if new_mouse_over = @container.hit_element(cursor.x, cursor.y) unless new_mouse_over if new_mouse_over new_mouse_over.publish :enter if new_mouse_over != @mouse_over new_mouse_over.publish :hover, cursor.x, cursor.y end @mouse_over.publish :leave if @mouse_over and new_mouse_over != @mouse_over @mouse_over = new_mouse_over # Check if the mouse has moved, and no menu is shown, so we can show a tooltip. if [cursor.x, cursor.y] == @last_cursor_pos and (not ) if @mouse_over and (Gosu::milliseconds - @mouse_moved_at) > tool_tip_delay if text = @mouse_over.tip and not text.empty? @tool_tip ||= ToolTip.new @tool_tip.text = text @tool_tip.x = cursor.x @tool_tip.y = cursor.y + cursor.height # Place the tip beneath the cursor. else @tool_tip = nil @mouse_moved_at = Gosu::milliseconds end end else @tool_tip = nil @mouse_moved_at = Gosu::milliseconds end # The element that grabs input. @active_element = @dragging_element || @focus || @mouse_over @last_cursor_pos = [cursor.x, cursor.y] super end |