Class: GamesAndRpgParadise::GUI::TextField

Inherits:
Gosu::TextInput
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb

Constant Summary collapse

FONT =
#

FONT

#
Gosu::Font.new(30)
WIDTH =
120
LENGTH_LIMIT =

Limit the input here.

1
PADDING =
12
DEFAULT_TEXT =
#

DEFAULT_TEXT

#
''
INACTIVE_COLOUR =
#

INACTIVE_COLOUR

#
0xcc_666666
ACTIVE_COLOUR =
0xcc_ff6666
SELECTION_COLOR =
0xcc_0000ff
CARET_COLOR =
0xff_ffffff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, x = 120, y = 120) ⇒ TextField

#

initialize

#


42
43
44
45
46
47
48
49
50
51
# File 'lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb', line 42

def initialize(
    window,
    x = 120,
    y = 120
  )
  super()
  @window, @x, @y = window, x, y
  # Start with a self-explanatory text in each field.
  self.text = DEFAULT_TEXT
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



36
37
38
# File 'lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb', line 36

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



37
38
39
# File 'lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb', line 37

def y
  @y
end

Instance Method Details

#draw(z) ⇒ Object

#

draw

#


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb', line 70

def draw(z)
  # Change the background colour if this is the currently selected text field.
  if @window.text_input == self
    color = ACTIVE_COLOUR
  else
    color = INACTIVE_COLOUR
  end
  Gosu.draw_rect(
    x - PADDING, y - PADDING, WIDTH + 2 * PADDING, height + 2 * PADDING, color, z
  )

  # ========================================================================= #
  # Calculate the position of the caret and the selection start.
  # ========================================================================= #
  pos_x = x + FONT.text_width(self.text[0...self.caret_pos])
  sel_x = x + FONT.text_width(self.text[0...self.selection_start])
  sel_w = pos_x - sel_x

  # Draw the selection background, if any. (If not, sel_x and pos_x will be
  # the same value, making this a no-op call.)
  Gosu.draw_rect sel_x, y, sel_w, height, SELECTION_COLOR, z

  # Draw the caret if this is the currently selected field.
  if @window.text_input == self
    Gosu.draw_line pos_x, y, CARET_COLOR, pos_x, y + height, CARET_COLOR, z
  end
  # Finally, draw the text itself!
  FONT.draw_text(self.text, x, y, z)
end

#filter(new_text) ⇒ Object

#

filter

In this example, we use the filter method to prevent the user from entering a text that exceeds the length limit. However, you can also use this to blacklist certain characters, etc.

#


60
61
62
63
64
65
# File 'lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb', line 60

def filter(new_text)
  new_text = new_text.dup if new_text.frozen?
  new_text.gsub!(/\d+/,'') # Remove numbers.
  allowed_length = [LENGTH_LIMIT - text.length, 0].max
  new_text[0, allowed_length]
end

#heightObject

height



100
101
102
# File 'lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb', line 100

def height
  FONT.height
end

#move_caret_to_mouseObject

move_caret_to_mouse

Tries to move the caret to the position specifies by mouse_x



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb', line 115

def move_caret_to_mouse
  # Test character by character
  1.upto(self.text.length) { |i|
    if @window.mouse_x < x + FONT.text_width(text[0...i])
      self.caret_pos = self.selection_start = i - 1;
      return
    end
  }
  # Default case: user must have clicked the right edge
  self.caret_pos = self.selection_start = self.text.length
end

#under_mouse?Boolean

under_mouse?

Hit-test for selecting a text field with the mouse.

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/games_and_rpg_paradise/gui/gosu/hangman/text_field.rb', line 107

def under_mouse?
  @window.mouse_x > x - PADDING and @window.mouse_x < x + WIDTH + PADDING and
    @window.mouse_y > y - PADDING and @window.mouse_y < y + height + PADDING
end