class TextField

Constants

ACTIVE_COLOR
CARET_COLOR
FONT
INACTIVE_COLOR
LENGTH_LIMIT
PADDING
SELECTION_COLOR
WIDTH

Attributes

x[R]
y[R]

Public Class Methods

new(window, x, y) click to toggle source
Calls superclass method
# File lib/client/text.rb, line 17
def initialize(window, x, y)
  # It's important to call the inherited constructor.
  super()
  
  @window, @x, @y = window, x, y
  
  # Start with a self-explanatory text in each field.
  self.text = "Click to edit"
end

Public Instance Methods

draw(z) click to toggle source
# File lib/client/text.rb, line 34
def draw(z)
  # Change the background colour if this is the currently selected text field.
  if @window.text_input == self
    color = ACTIVE_COLOR
  else
    color = INACTIVE_COLOR
  end
  # ChillerDragon's epic shadow to at least have edited the stolen sample a lil bit
  Gosu.draw_rect (x - PADDING) + 5, (y - PADDING) + 5, WIDTH + 2 * PADDING, height + 2 * PADDING, INACTIVE_COLOR, z
  Gosu.draw_rect x - PADDING, y - PADDING, WIDTH + 2 * PADDING, height + 2 * PADDING, color, z
  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) click to toggle source

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.

# File lib/client/text.rb, line 29
def filter new_text
  allowed_length = [LENGTH_LIMIT - text.length, 0].max
  new_text[0, allowed_length]
end
height() click to toggle source
# File lib/client/text.rb, line 64
def height
  FONT.height
end
move_caret_to_mouse() click to toggle source

Tries to move the caret to the position specifies by mouse_x

# File lib/client/text.rb, line 75
def move_caret_to_mouse
  # Test character by character
  1.upto(self.text.length) do |i|
    if @window.mouse_x < x + FONT.text_width(text[0...i])
      self.caret_pos = self.selection_start = i - 1;
      return
    end
  end
  # Default case: user must have clicked the right edge
  self.caret_pos = self.selection_start = self.text.length
end
under_mouse?() click to toggle source

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

# File lib/client/text.rb, line 69
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