Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # ** Button - This class is used to create buttons.
- #------------------------------------------------------------------------------
- # Author Trebor777
- # Version 2.0
- #------------------------------------------------------------------------------
- # * Refatorado e Modificado por Paulo Soreto
- #==============================================================================
- class Button < Widget
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize(win, x, y, text, &block)
- super(win, x, y)
- @btnRect = nil
- @text = text
- @block = block
- @image = nil
- @back = RPG::Cache.picture('btn')
- @select = RPG::Cache.picture('btn_hover')
- @off = false
- @sstate = false
- setup_rect
- refresh
- end
- #--------------------------------------------------------------------------
- # Get button rect
- #--------------------------------------------------------------------------
- def setup_rect
- temp = Bitmap.new(1, 1)
- temp.font.size = 16
- temp.font.color = Color.new(255, 255, 255)
- tx = temp.text_size(@text)
- s.bitmap = Bitmap.new(tx.width + 20, 20)
- s.bitmap.font.size = 14
- s.bitmap.font.color = Color.new(255, 255,255)
- end
- #--------------------------------------------------------------------------
- # Draw the button text, and create the mask.
- #--------------------------------------------------------------------------
- def refresh
- s.bitmap.clear
- @sstate ? @image = @select : @image =@back
- s.bitmap.blt(0, 0, @image, Rect.new(0, 0,5, 20))
- s.bitmap.stretch_blt(Rect.new(5, 0, s.bitmap.width - 5, 20), @image, Rect.new(5,0, 5, 20))
- s.bitmap.blt(s.bitmap.width - 5, 0,@image, Rect.new(10, 0, 5, 20))
- draw_text
- end
- def draw_text
- s.bitmap.font.color = Color.new(0, 0, 0)
- s.bitmap.draw_text(1, 1, s.bitmap.width,20, @text, 1)
- s.bitmap.font.color = Color.new(255, 255,255)
- s.bitmap.draw_text(0, 0, s.bitmap.width,20, @text, 1)
- end
- #--------------------------------------------------------------------------
- # Dispose the mask, and itself
- #--------------------------------------------------------------------------
- def hide; s.visible = false; end
- def show; s.visible = true; end
- #--------------------------------------------------------------------------
- # Update opacity on click
- #--------------------------------------------------------------------------
- def update
- if @off && !Input.pressed?(Input::Mouse_Left)
- @off = false
- s.opacity = 255
- end
- super
- end
- #--------------------------------------------------------------------------
- # Change the button opacity when clicked
- #--------------------------------------------------------------------------
- def clicked
- s.opacity = 160
- @block.call unless @block.nil?
- @off = true
- end
- #--------------------------------------------------------------------------
- # Visible
- #--------------------------------------------------------------------------
- def visible=(v)
- s.visible = v if s != nil
- end
- def text=(v)
- @text = v
- refresh
- end
- def opacity=(v)
- s.opacity = v
- refresh
- end
- def select=(v)
- @sstate = v
- refresh
- end
- def select; @sstate; end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement