Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # ** Text_Box
- #------------------------------------------------------------------------------
- # Esta classe lida com a caixa de texto.
- #------------------------------------------------------------------------------
- # Autor: Valentine
- #==============================================================================
- class Text_Box < Widget
- attr_reader :text
- def initialize(win, x, y, width, max, pass = false, secondary_text = '')
- super(win, x, y, width, 20)
- # Tamanho padrão dos caracteres
- @c_size = @back.bitmap.text_size('a').width
- @secondary_text = secondary_text
- @max = max
- @pass = pass
- @text = ''
- @blink_time = 0
- @text_width = 0
- create_blink
- refresh
- end
- def create_blink
- @blink = Sprite.new
- @blink.bitmap = Bitmap.new(3, line_height)
- @blink.bitmap.draw_text(@blink.bitmap.rect, '|')
- @blink.z = @back.z
- @blink.visible = false
- end
- def dispose
- super
- @blink.bitmap.dispose
- @blink.dispose
- end
- def text=(text)
- @text = text
- refresh
- end
- def clear
- self.text = ''
- end
- def max_chars?
- @text.size >= @max
- end
- def visible=(visible)
- super
- @blink.visible = false
- # Limpa o typing
- out_click
- end
- def active=(active)
- super
- @blink.visible = false
- return unless $typing == self || active
- $typing = active ? self : nil
- end
- def refresh
- @back.bitmap.clear
- text = display_text
- bitmap = Cache.system('TextBox')
- fill_background(bitmap)
- if !@secondary_text.empty? && text.empty?
- @back.bitmap.font.color.set(195, 195, 195)
- @back.bitmap.font.size = Font.default_size - 1
- @back.bitmap.draw_text(4, 0, width, line_height, @secondary_text)
- @text_width = 0
- else
- color = max_chars? ? Color.new(255, 128, 128) : Font.default_color
- @back.bitmap.font.color.set(color)
- @back.bitmap.font.size = Font.default_size
- # Corrige a compressão de texto do RGD
- @back.bitmap.draw_text(4, 0, width + 25, line_height, text)
- @text_width = @back.bitmap.text_size(text).width
- end
- end
- def display_text
- text = @pass ? '•' * @text.size : @text
- # O 13 em vez de 8 corrige a compressão de texto do RGD
- wth = width - 13
- index = text.size * @c_size > wth ? text.size - wth / @c_size : 0
- text[index...text.size]
- end
- def update
- super
- @blink.x = @back.x + @text_width + 4
- @blink.y = @back.y
- if @active
- update_blink
- update_input
- end
- end
- def update_blink
- @blink_time += 1
- if @blink_time == 20
- @blink.visible = !@blink.visible
- @blink_time = 0
- end
- end
- def update_input
- c = Input.utf8_string
- if Input.repeat?(:BACKSPACE) && !@text.empty?
- @text.chop!
- refresh
- elsif valid_character?(c) && !max_chars?
- @text << c
- refresh
- end
- end
- def valid_character?(c)
- !c.empty?
- end
- end
- #==============================================================================
- # ** Number_Box
- #==============================================================================
- class Number_Box < Text_Box
- def value
- @text.to_i
- end
- def valid_character?(c)
- super && c =~ /[\d-]/
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment