require 'gosu'

class TextField < Gosu::TextInput
  # Some constants that define our appearance.
  INACTIVE_COLOR  = 0xbbff1005
  ACTIVE_COLOR    = 0xbbff1005
  SELECTION_COLOR = 0xbbff1005
  CARET_COLOR     = 0xffffffff
  PADDING = 5

  attr_reader :x, :y

  def initialize(window, font, x, y)
    # TextInput's constructor doesn't expect any arguments.
    super()

    @window, @font, @x, @y = window, font, 385, 400

    # Start with a self-explanatory text in each field.
    self.text = "Unknown User"
  
  end

  # Example filter method. You can truncate the text to employ a length limit (watch out
  # with Ruby 1.8 and UTF-8!), limit the text to certain characters etc.
  def filter text
    text.upcase
  end

  def draw
    # Depending on whether this is the currently selected input or not, change the
    # background's color.
    if @window.text_input == self then
      background_color = ACTIVE_COLOR
    else
      background_color = INACTIVE_COLOR
    end
    #@window.draw_quad(x - PADDING,         y - PADDING,          background_color,
                    #  x + width + PADDING, y - PADDING,          background_color,
                    #  x - PADDING,         y + height + PADDING, background_color,
                    #  x + width + PADDING, y + height + PADDING, background_color, 0)

    # 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])

    # Draw the selection background, if any; if not, sel_x and pos_x will be
    # the same value, making this quad empty.
   # @window.draw_quad(sel_x, y,          SELECTION_COLOR,
                    #  pos_x, y,          SELECTION_COLOR,
                   #   sel_x, y + height, SELECTION_COLOR,
                    #  pos_x, y + height, SELECTION_COLOR, 0)

    # Draw the caret; again, only if this is the currently selected field.
    if @window.text_input == self then
      @window.draw_line(pos_x, y,          CARET_COLOR,
                        pos_x, y + height, CARET_COLOR, 0)
    end

    # Finally, draw the text itself!
    @font.draw("Enter Name: ",x-110,y, 1, factor_x = 1, factor_y = 1, color = 0xffff2222, mode = :default)
    @font.draw(self.text,x,y, 1, factor_x = 1, factor_y = 1, color = 0xffff2222, mode = :default)
  end

  # This text field grows with the text that's being entered.
  # (Usually one would use clip_to and scroll around on the text field.)
  def width
    @font.text_width(self.text)
  end

  def height
    @font.height
  end

end