# To change this template, choose Tools | Templates
# and open the template in the editor.

class EnhancedTextInput < Gosu::TextInput
  attr_accessor :x, :y, :color, :font

  #color constants - variable names need to be capital for constants
  INACTIVE_COLOR  = 0xcc666666
  ACTIVE_COLOR    = 0xcccd2c2c
  SELECTION_COLOR = 0xcc0000ff
  CARET_COLOR     = 0xffffffff
  PADDING = 5


  def initialize(window,x,y,color,font,defaultText,maxlength)
    super()
    @window = window
    @x = x
    @y = y
    @color = color
    @font = font

    self.text = defaultText
    @maxlength = maxlength
  end

  def width
    #width in pixels of the box    
    @font.text_width(self.text)
  end

  def height
    @font.height
  end

  def draw
 # Depending on whether this is the currently selected input or not, change the
    # background's color.
    @counter = width

      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 + @maxlength + PADDING, y - PADDING,          background_color,
                        x - PADDING,         y + height + PADDING, background_color,
                        x + @maxlength + 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


    if @maxlength  > @counter then
      # Finally, draw the text itself!
      @font.draw(self.text, x, y, 0)
    else
      @font.draw('')
    end

    return self.text
  end

  # Hit-test for selecting a text field with the mouse.  Returns true if I clicked on text.
  def under_point?(mouse_x, mouse_y)
    mouse_x > x - PADDING and mouse_x < x + width + PADDING and
      mouse_y > y - PADDING and mouse_y < y + height + PADDING
  end

  # Tries to move the caret to the position specifies by mouse_x
  def move_caret(mouse_x)
    # Test character by character
    1.upto(self.text.length) do |i|
      if mouse_x < x + @font.text_width(text[0...i]) then
        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


end
