1. -- 
  2. -- Copyright (c) 2012 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. with Entities;                          use Entities; 
  10.  
  11. limited with Game_Views; 
  12.  
  13. private with Tiles.Libraries; 
  14.  
  15. package Widgets.Sprites is 
  16.  
  17.     -- A Sprite is the visible representation of an Entity. It is a partially 
  18.     -- transparent widget, drawn as a tile from a tile library, that is added to 
  19.     -- a Scene container widget. 
  20.     type Sprite is new Widget with private; 
  21.     type A_Sprite is access all Sprite'Class; 
  22.  
  23.     -- Creates a new Sprite. 'eid' is the id of the entity it represents. Class 
  24.     -- is the class of entity it represents. 'x', 'y' are the location of the 
  25.     -- center of the widget in pixels. 'physical' indicates if the entity obeys 
  26.     -- the laws of physics. 'libName' is the name of the tile library to be used 
  27.     -- for the sprite's images. 'frame' is the id of the current frame in the 
  28.     -- tile library to display. 
  29.     function Create_Sprite( view     : not null access Game_Views.Game_View'Class; 
  30.                             eid      : Entity_Id; 
  31.                             class    : String; 
  32.                             x, y     : Float; 
  33.                             physical : Boolean; 
  34.                             libName  : String; 
  35.                             frame    : Natural ) return A_Sprite; 
  36.     pragma Precondition( class'Length > 0 ); 
  37.     pragma Precondition( libName'Length > 0 ); 
  38.     pragma Postcondition( Create_Sprite'Result /= null ); 
  39.  
  40.     -- Centers the sprite at 'x', 'y' in pixels. The sprite will update its 
  41.     -- known location of its entity. 
  42.     procedure Center_At( this : not null access Sprite'Class; x, y : Float ); 
  43.  
  44.     -- Returns the sprite's entity's class. 
  45.     function Get_Class( this : not null access Sprite'Class ) return String; 
  46.     pragma Postcondition( Get_Class'Result'Length > 0 ); 
  47.  
  48.     -- Returns the Entity_Id of the Sprite's entity. 
  49.     function Get_Entity_Id( this : not null access Sprite'Class ) return Entity_Id; 
  50.  
  51.     -- Returns the x location of the Sprite's entity; located at the center of 
  52.     -- the widget. 
  53.     function Get_X( this : not null access Sprite'Class ) return Float; 
  54.  
  55.     -- Returns the y location of the Sprite's entity; located at the center of 
  56.     -- the widget. 
  57.     function Get_Y( this : not null access Sprite'Class ) return Float; 
  58.  
  59.     -- Sets the tile id of the Sprite's current frame. 
  60.     procedure Set_Frame( this : not null access Sprite'Class; frame : Natural ); 
  61.  
  62.     function Is_Updatable( this : not null access Sprite'Class ) return Boolean; 
  63.  
  64.     -- Resizes the Sprite to 'width', 'height' while keeping the widget centered 
  65.     -- at its entity's location. If 'width' or 'height' is 0, the value will not 
  66.     -- be changed. (ex: width => 10, height => 0 will change only the width.) 
  67.     procedure Resize( this   : not null access Sprite'Class; 
  68.                       width, 
  69.                       height : Natural ); 
  70.  
  71.     -- Notifies the sprite of its selected state. This doesn't actually 
  72.     -- select/unselect the widget; that can only be done by its Window. 
  73.     procedure Set_Selected( this : access Sprite; selected : Boolean ); 
  74.  
  75. private 
  76.  
  77.     use Tiles.Libraries; 
  78.  
  79.     type Sprite is new Widget with 
  80.         record 
  81.             eid         : Entity_Id := INVALID_ID; 
  82.             class       : Unbounded_String; 
  83.             x, y        : Float := 0.0; 
  84.             physical    : Boolean := False; 
  85.             lib         : A_Tile_Library := null; 
  86.             frame       : Natural := 0; 
  87.             offsetX, 
  88.             offsetY     : Integer := 0;       -- the center's offset from x, y 
  89.             updatable   : Integer := 0;       -- accepts updates if = 0 
  90.             interactive : Boolean := False; 
  91.             selected    : Boolean := False; 
  92.         end record; 
  93.  
  94.     procedure Construct( this     : access Sprite; 
  95.                          view     : not null access Game_Views.Game_View'Class; 
  96.                          eid      : Entity_Id; 
  97.                          class    : String; 
  98.                          x, y     : Float; 
  99.                          physical : Boolean; 
  100.                          libName  : String; 
  101.                          frame    : Natural ); 
  102.     pragma Precondition( class'Length > 0 ); 
  103.     pragma Precondition( libName'Length > 0 ); 
  104.  
  105.     procedure Delete( this : in out Sprite ); 
  106.  
  107.     procedure Draw_Content( this : access Sprite ); 
  108.  
  109.     function Get_Min_Height( this : access Sprite ) return Natural; 
  110.  
  111.     function Get_Min_Width( this : access Sprite ) return Natural; 
  112.  
  113. end Widgets.Sprites;