Class: Fidgit::GuiState

Inherits:
Chingu::GameState
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/fidgit/states/gui_state.rb

Direct Known Subclasses

DialogState

Constant Summary collapse

PIXEL_IMAGE =

A 1x1 white pixel used for drawing.

'pixel.png'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGuiState

Returns a new instance of GuiState.



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
87
88
89
90
91
# File 'lib/fidgit/states/gui_state.rb', line 51

def initialize
  # The container is where the user puts their content.
  @container = MainPacker.new
  @menu = nil
  @last_cursor_pos = [-1, -1]
  @mouse_over = nil

  unless defined? @@draw_pixel
    media_dir = File.expand_path(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: ->{ redirect_mouse_button(:left) },
    holding_left_mouse_button: ->{ redirect_holding_mouse_button(:left) },
    released_left_mouse_button: ->{ redirect_released_mouse_button(:left) },
    
    middle_mouse_button: ->{ redirect_mouse_button(:middle) },
    holding_middle_mouse_button: ->{ redirect_holding_mouse_button(:middle) },
    released_middle_mouse_button: ->{ redirect_released_mouse_button(:middle) },
  
    right_mouse_button: ->{ redirect_mouse_button(:right) },
    holding_right_mouse_button: ->{ redirect_holding_mouse_button(:right) },
    released_right_mouse_button: ->{ redirect_released_mouse_button(:right) },
  
    mouse_wheel_up: :redirect_mouse_wheel_up,
    mouse_wheel_down: :redirect_mouse_wheel_down,

    x: -> { if @focus and (holding_any?(:left_control, :right_control)) then @focus.cut end },
    c: -> { if @focus and (holding_any?(:left_control, :right_control)) then @focus.copy end },
    v: -> { if @focus and (holding_any?(:left_control, :right_control)) then @focus.paste end }
  )
end

Instance Attribute Details

#containerPacker (readonly)

The Container that contains all the elements for this GuiState.

Returns:



15
16
17
# File 'lib/fidgit/states/gui_state.rb', line 15

def container
  @container
end

#focusElement

The element with focus.

Returns:



19
20
21
# File 'lib/fidgit/states/gui_state.rb', line 19

def focus
  @focus
end

Class Method Details

.clearObject

Clear the data which is specific to the current $window.



97
98
99
100
# File 'lib/fidgit/states/gui_state.rb', line 97

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



49
# File 'lib/fidgit/states/gui_state.rb', line 49

def clear(*args, &block); @container.clear(*args, &block); end

#cursorCursor

The Cursor.

Returns:



23
# File 'lib/fidgit/states/gui_state.rb', line 23

def cursor; @@cursor; end

#distance(x1, y1, x2, y2) ⇒ Object



239
240
241
# File 'lib/fidgit/states/gui_state.rb', line 239

def distance(x1, y1, x2, y2)
  Gosu.distance(x1, y1, x2, y2)
end

#drawObject



155
156
157
158
159
160
161
162
# File 'lib/fidgit/states/gui_state.rb', line 155

def draw
  @container.draw
  @menu.draw if @menu
  @tool_tip.draw if @tool_tip
  cursor.draw

  super
end

#draw_frame(x, y, width, height, thickness, z, color, mode = :default) ⇒ Object

Draw an unfilled rectangle.



230
231
232
233
234
235
236
237
# File 'lib/fidgit/states/gui_state.rb', line 230

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.



223
224
225
226
227
# File 'lib/fidgit/states/gui_state.rb', line 223

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)



38
39
40
# File 'lib/fidgit/states/gui_state.rb', line 38

def file_dialog(type, options = {}, &block)
  FileDialog.new(type, options, &block)
end

#finalizeObject



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fidgit/states/gui_state.rb', line 179

def finalize
  unset_mouse_over

  if @focus
    @focus.publish :blur
    @focus = nil
  end

  @tool_tip = nil

  nil
end

#flushObject

Flush all pending drawing to the screen.



218
219
220
# File 'lib/fidgit/states/gui_state.rb', line 218

def flush
  $window.flush
end

#hideObject



248
249
250
251
# File 'lib/fidgit/states/gui_state.rb', line 248

def hide
  $window.game_state_manager.pop if $window.game_state_manager.current == self
  nil
end

#hide_menuObject

Hides the currently shown menu, if any.

Returns:

  • nil



211
212
213
214
215
# File 'lib/fidgit/states/gui_state.rb', line 211

def hide_menu
  @menu = nil

  nil
end

Options Hash (options):

  • :x (Float) — default: cursor x, if in a GuiState
  • :y (Float) — default: cursor y, if in a GuiState
  • :show (Boolean) — default: true

    Whether to show immediately (show later with #show).



43
# File 'lib/fidgit/states/gui_state.rb', line 43

def menu(options = {}, &block); MenuPane.new(options, &block); end

#message(text, options = {}) {|result| ... } ⇒ Object

Parameters:

  • message (String)

Options Hash (options):

  • :type (Symbol) — default: :ok

    One from :ok, :ok_cancel, :yes_no, :yes_no_cancel, :quit_cancel or :quit_save_cancel

  • :ok_text (String) — default: "OK"
  • :yes_text (String) — default: "Yes"
  • :no_text (String) — default: "No"
  • :cancel_text (String) — default: "Cancel"
  • :save_text (String) — default: "Save"
  • :quit_text (String) — default: "Quit"
  • :show (Boolean) — default: true

    Whether to show the message immediately (otherwise need to use #show later).

Yields:

  • when the dialog is closed.

Yield Parameters:

  • result (Symbol)

    :ok, :yes, :no, :quit, :save or :cancel, depending on the button pressed.



46
# File 'lib/fidgit/states/gui_state.rb', line 46

def message(text, options = {}, &block); MessageDialog.new(text, options, &block); end

#setupObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/fidgit/states/gui_state.rb', line 164

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.
  @drag_button = nil
  @dragging_element = nil
  @focus = nil
  @mouse_moved_at = Gosu::milliseconds

  nil
end

#showObject



243
244
245
246
# File 'lib/fidgit/states/gui_state.rb', line 243

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.

Parameters:

Returns:

  • nil



202
203
204
205
206
207
# File 'lib/fidgit/states/gui_state.rb', line 202

def show_menu(menu)
  hide_menu if @menu
  @menu = menu

  nil
end

#t(*args) ⇒ Object

Internationalisation helper.



94
# File 'lib/fidgit/states/gui_state.rb', line 94

def t(*args); I18n.t(*args); end

#tool_tip_delayObject

Delay, in ms, before a tool-tip will appear.



32
33
34
# File 'lib/fidgit/states/gui_state.rb', line 32

def tool_tip_delay
  500 # TODO: configure this.
end

#unset_mouse_overObject

Called by active elements when they are disabled.



193
194
195
196
# File 'lib/fidgit/states/gui_state.rb', line 193

def unset_mouse_over
  @mouse_over.publish :leave if @mouse_over
  @mouse_over = nil
end

#updateObject



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
141
142
143
144
145
# File 'lib/fidgit/states/gui_state.rb', line 102

def update
  cursor.update
  @tool_tip.update if @tool_tip
  @menu.update if @menu
  @container.update

  # Check menu first, then other elements.
  new_mouse_over = @menu.hit_element(cursor.x, cursor.y) if @menu
  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 @menu)
    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

#write_treeObject



147
148
149
150
151
152
153
# File 'lib/fidgit/states/gui_state.rb', line 147

def write_tree
  puts "=== #{self.class} ==="
  indent = "  "
  @container.write_tree(indent)
  @menu.write_tree(indent) if @menu
  @tool_tip.write_tree(indent) if @tool_tip
end