Message Box for rbSFML

A simple class that let's you show a informative window which can contain debug information or error messages to the user. When shown it will block the rest of application until it is closed which makes it so much easier to use.

Simple Example

begin
  # Do something here which triggers an error
rescue RuntimeError => error
  MessageBox.show( "RuntimeError!", error.to_s )
end

The code

class MessageBox   
  #################################################################################################################
  # Shortcut for creating a message box and showing it.
  #################################################################################################################
  def self.show( aTitle, aMessage, anError = false )
    message = self.new( aTitle, aMessage, anError )
    message.show
  end
 
  #################################################################################################################
  # If the message box is reporting an error and if it should abort when done.
  #################################################################################################################
  attr_accessor :error
 
  #################################################################################################################
  # Get the title string of the window.
  #################################################################################################################
  attr_reader :title
 
  #################################################################################################################
  # Create a new message box.
  #################################################################################################################
  def initialize( aTitle, aMessage, anError = false )
    @window = SFML::RenderWindow.new
    @message = SFML::Text.new( aMessage, SFML::Font.DefaultFont, 15 )
    @message.position = [ 25, 15 ]
    mode = SFML::VideoMode.new( @message.rect.width.to_i + 50, @message.rect.height.to_i + 30 )
    @window.create( mode, aTitle, SFML::Style::Titlebar | SFML::Style::Close )
    @title = aTitle
    @error = anError
  end
 
  #################################################################################################################
  # Set the title string of the window.
  #################################################################################################################
  def title=( aValue )
    @window.title = aValue
    @title = aValue
  end
 
  #################################################################################################################
  # Get the message string.
  #################################################################################################################
  def message
    return @message.string
  end
 
  #################################################################################################################
  # Set the message string.
  #################################################################################################################
  def message=( aValue )
    @message.string = aValue
  end
 
  #################################################################################################################
  # Show the message box. Could also be called the main-loop.
  #################################################################################################################
  def show
    while @window.open?
      while event = @window.get_event
        @window.close if event.type == SFML::Event::Closed || event.type == SFML::Event::KeyPressed
      end
 
      @window.clear
      @window.draw @message
      @window.display
    end
 
    abort if @error == true
  end
end
 
en/sources/messagebox.txt · Last modified: 2011/01/26 10:18 by Groogy
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki