Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.74 KB | None | 0 0
  1. #==============================================================================
  2.  
  3. # ** Button - This class is used to create buttons.
  4.  
  5. #------------------------------------------------------------------------------
  6.  
  7. # Author    Trebor777
  8.  
  9. # Version   2.0
  10.  
  11. #------------------------------------------------------------------------------
  12.  
  13. # * Refatorado e Modificado por Paulo Soreto
  14.  
  15. #==============================================================================
  16.  
  17. class Button < Widget
  18.  
  19.   #--------------------------------------------------------------------------
  20.  
  21.   # * Object Initialization
  22.  
  23.   #--------------------------------------------------------------------------
  24.  
  25.   def initialize(win, x, y, text, &block)
  26.  
  27.     super(win, x, y)
  28.  
  29.     @btnRect = nil
  30.  
  31.     @text = text
  32.  
  33.     @block = block
  34.  
  35.     @image = nil
  36.  
  37.     @back = RPG::Cache.picture('btn')
  38.  
  39.     @select = RPG::Cache.picture('btn_hover')
  40.  
  41.     @off = false
  42.  
  43.     @sstate = false
  44.  
  45.     setup_rect
  46.  
  47.     refresh
  48.  
  49.   end
  50.  
  51.   #--------------------------------------------------------------------------
  52.  
  53.   # Get button rect
  54.  
  55.   #--------------------------------------------------------------------------
  56.  
  57.   def setup_rect
  58.  
  59.     temp = Bitmap.new(11)
  60.  
  61.     temp.font.size = 16
  62.  
  63.     temp.font.color = Color.new(255255255)
  64.  
  65.     tx = temp.text_size(@text)
  66.  
  67.     s.bitmap = Bitmap.new(tx.width + 2020)
  68.  
  69.     s.bitmap.font.size = 14
  70.  
  71.     s.bitmap.font.color = Color.new(255255,255)
  72.  
  73.   end
  74.  
  75.   #--------------------------------------------------------------------------
  76.  
  77.   # Draw the button text, and create the mask.
  78.  
  79.   #--------------------------------------------------------------------------
  80.  
  81.   def refresh
  82.  
  83.     s.bitmap.clear
  84.  
  85.     @sstate ? @image = @select : @image =@back
  86.  
  87.     s.bitmap.blt(00, @image, Rect.new(00,520))
  88.  
  89.     s.bitmap.stretch_blt(Rect.new(50, s.bitmap.width - 520), @image, Rect.new(5,0520))
  90.  
  91.     s.bitmap.blt(s.bitmap.width - 50,@image, Rect.new(100520))
  92.  
  93.     draw_text
  94.  
  95.   end
  96.  
  97.   def draw_text
  98.  
  99.     s.bitmap.font.color = Color.new(000)
  100.  
  101.     s.bitmap.draw_text(11, s.bitmap.width,20, @text, 1)
  102.  
  103.     s.bitmap.font.color = Color.new(255255,255)
  104.  
  105.     s.bitmap.draw_text(00, s.bitmap.width,20, @text, 1)
  106.  
  107.   end
  108.  
  109.   #--------------------------------------------------------------------------
  110.  
  111.   # Dispose the mask, and itself
  112.  
  113.   #--------------------------------------------------------------------------
  114.  
  115.   def hide; s.visible = falseend
  116.  
  117.   def show; s.visible = trueend
  118.  
  119.   #--------------------------------------------------------------------------
  120.  
  121.   # Update opacity on click
  122.  
  123.   #--------------------------------------------------------------------------
  124.  
  125.   def update
  126.  
  127.     if @off && !Input.pressed?(Input::Mouse_Left)
  128.  
  129.       @off = false
  130.  
  131.       s.opacity = 255
  132.  
  133.     end
  134.  
  135.     super
  136.  
  137.   end
  138.  
  139.   #--------------------------------------------------------------------------
  140.  
  141.   # Change the button opacity when clicked
  142.  
  143.   #--------------------------------------------------------------------------
  144.  
  145.   def clicked
  146.  
  147.     s.opacity = 160
  148.  
  149.     @block.call unless @block.nil?
  150.  
  151.     @off = true
  152.  
  153.   end
  154.  
  155.   #--------------------------------------------------------------------------
  156.  
  157.   # Visible
  158.  
  159.   #--------------------------------------------------------------------------
  160.  
  161.   def visible=(v)
  162.  
  163.     s.visible = v if s != nil
  164.  
  165.   end
  166.  
  167.   def text=(v)
  168.  
  169.     @text = v
  170.  
  171.     refresh
  172.  
  173.   end  
  174.  
  175.   def opacity=(v)
  176.  
  177.     s.opacity = v
  178.  
  179.     refresh
  180.  
  181.   end
  182.  
  183.   def select=(v)
  184.  
  185.     @sstate = v
  186.  
  187.     refresh
  188.  
  189.   end
  190.  
  191.   def select; @sstate; end
  192.  
  193. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement