marlosgama

Untitled

Jun 17th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.40 KB | None | 0 0
  1. #==============================================================================
  2. # ** Text_Box
  3. #------------------------------------------------------------------------------
  4. #  Esta classe lida com a caixa de texto.
  5. #------------------------------------------------------------------------------
  6. #  Autor: Valentine
  7. #==============================================================================
  8.  
  9. class Text_Box < Widget
  10.  
  11.   attr_reader   :text
  12.  
  13.   def initialize(win, x, y, width, max, pass = false, secondary_text = '')
  14.     super(win, x, y, width, 20)
  15.     # Tamanho padrão dos caracteres
  16.     @c_size = @back.bitmap.text_size('a').width
  17.     @secondary_text = secondary_text
  18.     @max = max
  19.     @pass = pass
  20.     @text = ''
  21.     @blink_time = 0
  22.     @text_width = 0
  23.     create_blink
  24.     refresh
  25.   end
  26.  
  27.   def create_blink
  28.     @blink = Sprite.new
  29.     @blink.bitmap = Bitmap.new(3, line_height)
  30.     @blink.bitmap.draw_text(@blink.bitmap.rect, '|')
  31.     @blink.z = @back.z
  32.     @blink.visible = false
  33.   end
  34.  
  35.   def dispose
  36.     super
  37.     @blink.bitmap.dispose
  38.     @blink.dispose
  39.   end
  40.  
  41.   def text=(text)
  42.     @text = text
  43.     refresh
  44.   end
  45.  
  46.   def clear
  47.     self.text = ''
  48.   end
  49.  
  50.   def max_chars?
  51.     @text.size >= @max
  52.   end
  53.  
  54.   def visible=(visible)
  55.     super
  56.     @blink.visible = false
  57.     # Limpa o typing
  58.     out_click
  59.   end
  60.  
  61.   def active=(active)
  62.     super
  63.     @blink.visible = false
  64.     return unless $typing == self || active
  65.     $typing = active ? self : nil
  66.   end
  67.  
  68.   def refresh
  69.     @back.bitmap.clear
  70.     text = display_text
  71.     bitmap = Cache.system('TextBox')
  72.     fill_background(bitmap)
  73.     if !@secondary_text.empty? && text.empty?
  74.       @back.bitmap.font.color.set(195, 195, 195)
  75.       @back.bitmap.font.size = Font.default_size - 1
  76.       @back.bitmap.draw_text(4, 0, width, line_height, @secondary_text)
  77.       @text_width = 0
  78.     else
  79.       color = max_chars? ? Color.new(255, 128, 128) : Font.default_color
  80.       @back.bitmap.font.color.set(color)
  81.       @back.bitmap.font.size = Font.default_size
  82.       # Corrige a compressão de texto do RGD
  83.       @back.bitmap.draw_text(4, 0, width + 25, line_height, text)
  84.       @text_width = @back.bitmap.text_size(text).width
  85.     end
  86.   end
  87.  
  88.   def display_text
  89.     text = @pass ? '•' * @text.size : @text
  90.     # O 13 em vez de 8 corrige a compressão de texto do RGD
  91.     wth = width - 13
  92.     index = text.size * @c_size > wth ? text.size - wth / @c_size : 0
  93.     text[index...text.size]
  94.   end
  95.  
  96.   def update
  97.     super
  98.     @blink.x = @back.x + @text_width + 4
  99.     @blink.y = @back.y
  100.     if @active
  101.       update_blink
  102.       update_input
  103.     end
  104.   end
  105.  
  106.   def update_blink
  107.     @blink_time += 1
  108.     if @blink_time == 20
  109.       @blink.visible = !@blink.visible
  110.       @blink_time = 0
  111.     end
  112.   end
  113.  
  114.   def update_input
  115.     c = Input.utf8_string
  116.     if Input.repeat?(:BACKSPACE) && !@text.empty?
  117.       @text.chop!
  118.       refresh
  119.     elsif valid_character?(c) && !max_chars?
  120.       @text << c
  121.       refresh
  122.     end
  123.   end
  124.  
  125.   def valid_character?(c)
  126.     !c.empty?
  127.   end
  128.  
  129. end
  130.  
  131. #==============================================================================
  132. # ** Number_Box
  133. #==============================================================================
  134. class Number_Box < Text_Box
  135.  
  136.   def value
  137.     @text.to_i
  138.   end
  139.  
  140.   def valid_character?(c)
  141.     super && c =~ /[\d-]/
  142.   end
  143.  
  144. end
Advertisement
Add Comment
Please, Sign In to add comment