2009-01-29 00:18:34 +08:00
module view ;
import dsfml.system.all ;
import dsfml.window.all ;
import dsfml.graphics.all ;
void main ( )
{
2010-01-14 05:49:59 +08:00
RenderWindow window = new RenderWindow ( VideoMode ( 800 , 600 ) , "View sample" ) ;
2010-08-26 17:06:20 +08:00
window . framerateLimit = 100 ;
Input input = window . input ;
2010-01-14 05:49:59 +08:00
Vector2f top ;
2010-08-26 17:06:20 +08:00
FloatRect bound ;
2010-01-14 05:49:59 +08:00
Shape s ;
bool mousePressed ;
Sprite background = new Sprite ( new Image ( "Data/background.jpg" ) ) ;
Font f = new Font ( "Data/cheeseburger.ttf" ) ;
Text str = new Text ( "Create a selection of the background with your mouse.\nPress Enter to zoom to this selection.\nPress Escape to return to the default view."c , f ) ;
while ( window . isOpened ( ) )
{
Event evt ;
while ( window . getEvent ( evt ) )
{
if ( evt . Type = = EventType . MouseButtonPressed & &
evt . MouseButton . Button = = MouseButtons . Left )
{
2010-08-26 17:06:20 +08:00
top = window . convertCoords ( input . mouseX , input . mouseY ) ;
2010-01-14 05:49:59 +08:00
mousePressed = true ;
}
else if ( evt . Type = = EventType . MouseButtonReleased & &
evt . MouseButton . Button = = MouseButtons . Left )
{
2010-08-26 17:06:20 +08:00
mousePressed = false ;
2010-01-14 05:49:59 +08:00
}
else if ( evt . Type = = EventType . MouseMoved & &
mousePressed )
{
2010-08-26 17:06:20 +08:00
Vector2f bottom = window . convertCoords ( input . mouseX , input . mouseY ) ;
bound = FloatRect ( top . x , top . y , bottom . x - top . x , bottom . y - top . y ) ;
s = Shape . rectangle ( bound . left , bound . top , bound . width , bound . height , Color ( 0 , 0 , 0 , 0 ) , 1 , Color . BLACK ) ;
2010-01-14 05:49:59 +08:00
}
else if ( evt . Type = = EventType . KeyPressed & &
evt . Key . Code = = KeyCode . Return )
{
if ( bound ! = FloatRect ( ) )
2010-08-26 17:06:20 +08:00
window . view = new View ( bound ) ;
2010-01-14 05:49:59 +08:00
s = null ;
}
else if ( evt . Type = = EventType . KeyPressed & &
evt . Key . Code = = KeyCode . Escape )
{
2010-08-26 17:06:20 +08:00
window . view = window . defaultView ;
2010-01-14 05:49:59 +08:00
}
else if ( evt . Type = = EventType . Closed )
window . close ( ) ;
}
window . draw ( background ) ;
window . draw ( str ) ;
if ( s ! is null ) window . draw ( s ) ;
window . display ( ) ;
}
2010-08-26 17:06:20 +08:00
}