1 /*
2  * Copyright (C) 2015-2020, by Laszlo Szeremi under the Boost license.
3  *
4  * Pixel Perfect Engine, graphics.layers.base module
5  */
6 
7 module pixelperfectengine.graphics.layers.interfaces;
8 
9 import pixelperfectengine.graphics.layers.base;
10 
11 /**
12  * Tile interface, defines common functions shared between tile layers.
13  */
14 public interface ITileLayer {
15 	/// Retrieves the mapping from the tile layer.
16 	/// Can be used to retrieve data, e.g. for editors, saving game states
17 	public MappingElement[] getMapping() @nogc @safe pure nothrow;
18 	/** 
19 	 * Reads the mapping element from the given area, while accounting for warp mode.
20 	 * Params:
21 	 *  x: x offset of the tile.
22 	 *  y: y offset of the tile.
23 	 * Returns: The tile at the given point.
24 	 */
25 	public MappingElement readMapping(int x, int y) @nogc @safe pure nothrow const;
26 	/**
27 	 * Writes the given element into the mapping at the given location.
28 	 * Params:
29 	 *  x: x offset of the tile.
30 	 *  y: y offset of the tile.
31 	 */
32 	public void writeMapping(int x, int y, MappingElement w) @nogc @safe pure nothrow;
33 	/** 
34 	 * Loads a mapping into the layer.
35 	 * Params:
36 	 *   x = width of the map.
37 	 *   y = height of the map.
38 	 *   mapping = an array representing the map.
39 	 * Throws: MapFormatException if width and height doesn't represent the map.
40 	 */
41 	public void loadMapping(int x, int y, MappingElement[] mapping) @safe pure;
42 	/// Removes the tile from the display list with the given ID.
43 	public void removeTile(wchar id) pure;
44 	/// .
45 	/** 
46 	 * Reads the mapping element from the given area, while accounting for warp mode.
47 	 * Params:
48 	 *  x: x offset of the tile.
49 	 *  y: y offset of the tile.
50 	 * Returns: The tile at the given point.
51 	 */
52 	public MappingElement tileByPixel(int x, int y) @nogc @safe pure nothrow const;
53 	/// Returns the width of the tiles.
54 	public int getTileWidth() @nogc @safe pure nothrow const;
55 	/// Returns the height of the tiles.
56 	public int getTileHeight() @nogc @safe pure nothrow const;
57 	/// Returns the width of the mapping.
58 	public int getMX() @nogc @safe pure nothrow const;
59 	/// Returns the height of the mapping.
60 	public int getMY() @nogc @safe pure nothrow const;
61 	/// Returns the total width of the tile layer.
62 	public size_t getTX() @nogc @safe pure nothrow const;
63 	/// Returns the total height of the tile layer.
64 	public size_t getTY() @nogc @safe pure nothrow const;
65 	/**
66 	 * Adds a new tile to the layer.
67 	 * Params: 
68 	 *  tile: the bitmap representing the tile. Must be the same size as all the others. Some tilelayers might require
69 	 * an exact format of tiles.
70 	 *  id: the character ID of the tile represented on the map.
71 	 *  paletteSh: palette shift amount, or how many bits are actually used of the bitmap. This enables less than 16 
72 	 * or 256 color chunks on the palette to be selected.
73 	 * Throws: TileFormatException if size or format is wrong.
74 	 */
75 	public void addTile(ABitmap tile, wchar id, ubyte paletteSh = 0) pure;
76 	/// Returns the bitmap associated with the tile ID.
77 	public ABitmap getTile(wchar id) @nogc @safe pure nothrow;
78 	/// Sets the warp mode.
79 	/// Returns the new warp mode that is being used.
80 	public WarpMode setWarpMode(WarpMode mode) @nogc @safe pure nothrow;
81 	/// Returns the currently used warp mode.
82 	public WarpMode getWarpMode() @nogc @safe pure nothrow const;
83 	///Clears the tilemap
84 	public void clearTilemap() @nogc @safe pure nothrow;
85 }
86 /**
87  * Defines functions specific to transformable tile layers.
88  * All transform parameters (A, B, C, D) are 256-based "fractional integers".
89  */
90 public interface ITTL {
91     ///Returns the horizontal scaling amount.
92     ///256 means no scaling, negative values flip everything horizontally.
93 	public @property short A() @nogc @safe nothrow pure const;
94 	///Returns the shearing amount on the X axis.
95 	///256 means one pixel moved downwards for each horizontal scanline.
96 	public @property short B() @nogc @safe nothrow pure const;
97 	///Returns the shearing amount on the Y axis.
98 	///256 means one pixel moved right for each vertical scanline.
99 	public @property short C() @nogc @safe nothrow pure const;
100 	///Returns the vertical scaling amount.
101     ///256 means no scaling, negative values flip everything vertically.
102 	public @property short D() @nogc @safe nothrow pure const;
103 	///Returns the x origin point of the transformation relative to the screen.
104 	public @property short x_0() @nogc @safe nothrow pure const;
105 	///Returns the y origin point of the transformation relative to the screen.
106 	public @property short y_0() @nogc @safe nothrow pure const;
107 	///Sets the horizontal scaling amount.
108     ///256 means no scaling, negative values flip everything horizontally.
109 	public @property short A(short newval) @nogc @safe nothrow pure;
110 	///Sets the shearing amount on the X axis.
111 	///256 means one pixel moved downwards for each horizontal scanline.
112 	public @property short B(short newval) @nogc @safe nothrow pure;
113 	///Sets the shearing amount on the Y axis.
114 	///256 means one pixel moved right for each vertical scanline.
115 	public @property short C(short newval) @nogc @safe nothrow pure;
116 	///Sets the vertical scaling amount.
117     ///256 means no scaling, negative values flip everything vertically.
118 	public @property short D(short newval) @nogc @safe nothrow pure;
119 	///Returns the x origin point of the transformation relative to the screen.
120 	public @property short x_0(short newval) @nogc @safe nothrow pure;
121 	///Returns the y origin point of the transformation relative to the screen.
122 	public @property short y_0(short newval) @nogc @safe nothrow pure;
123 }
124 /**
125  *General SpriteLayer interface.
126  */
127 public interface ISpriteLayer {
128 	///Clears all sprite from the layer.
129 	public void clear() @trusted nothrow;
130 	///Removes the sprite with the given ID.
131 	public void removeSprite(int n) @trusted nothrow;
132 	/** 
133 	 * Moves the sprite to the exact location.
134 	 * Params:
135 	 *   n = The identifier of the sprite.
136 	 *   x = New x position of the sprite.
137 	 *   y = New y position of the sprite.
138 	 */
139 	public void moveSprite(int n, int x, int y) @trusted nothrow;
140 	/** 
141 	 * Relatively moves the sprite by the given values.
142 	 * Params:
143 	 *   n = The identifier of the sprite.
144 	 *   x = New x position of the sprite.
145 	 *   y = New y position of the sprite.
146 	 */
147 	public void relMoveSprite(int n, int x, int y) @trusted nothrow;
148 	///Gets the coordinate of the sprite.
149 	public Box getSpriteCoordinate(int n) @nogc @trusted nothrow;
150 	/** 
151 	 * Creates a sprite from a bitmap with the given data, then places it to the display list. (New architecture)
152 	 * Params:
153 	 *   sprt = The bitmap to be used as the sprite.
154 	 *   n = Priority ID of the sprite. Both identifies the sprite and decides it's display priority. Larger numbers will be drawn first, 
155 	 * and thus will appear behind of smaller numbers, which also include negatives.
156 	 *   x = X position of the sprite (top-left corner).
157 	 *   y = Y position of the sprite (top-left corner).
158 	 *   paletteSel = Selects a given palette.
159 	 *   paletteSh = Determines how many bits are being used, and thus the palette size for selection.
160 	 *   alpha = The transparency of the sprite.
161 	 *   scaleHoriz = Horizontal scaling of the sprite. 1024 is the base value, anything less will stretch, greater will shrink the sprite.
162 	 *   scaleVert = Ditto for vertical.
163 	 *   renderMode = Determines the rendering mode of the sprite. By default, it's determined by the layer itself. Any of the default 
164 	 * other methods can be selected here, or a specially written rendering function can be specified with a different function.
165 	 * Returns: The current area of the sprite.
166 	 */
167 	public Box addSprite(ABitmap sprt, int n, int x, int y, ushort paletteSel = 0, ubyte paletteSh = 0, 
168 			ubyte alpha = ubyte.max, int scaleHoriz = 1024, int scaleVert = 1024, RenderingMode renderMode = RenderingMode.init) 
169 			@trusted nothrow;
170 	/+
171 	/** 
172 	 * Places a new sprite onto the layer with the given parameters.
173 	 * Params:
174 	 *   s = The bitmap to be displayed as the sprite.
175 	 *   n = Priority ID. Lower number (including negatives) get to drawn last, thus appearing on top.
176 	 *   c = The box that sets the position of the sprite.
177 	 *   paletteSel = The ID of the selected palette.
178 	 *   scaleHoriz = Horizontal scaling.
179 	 *   scaleVert = Vertical scaling.
180 	 */
181 	public void addSprite(ABitmap s, int n, Box c, ushort paletteSel = 0, int scaleHoriz = 1024, 
182 			int scaleVert = 1024) @safe nothrow;
183 	///Adds a sprite to the layer.
184 	public void addSprite(ABitmap s, int n, int x, int y, ushort paletteSel = 0, int scaleHoriz = 1024, 
185 			int scaleVert = 1024) @safe nothrow;+/
186 	///Sets the rendering function for the sprite (defaults to the layer's rendering function)
187 	public RenderFunc setSpriteRenderingMode(int n, RenderingMode mode) @nogc @trusted pure nothrow;
188 	///Replaces the sprite. If the new sprite has a different dimension, the old sprite's upper-left corner will be used.
189 	public void replaceSprite(ABitmap s, int n) @trusted nothrow;
190 	///Replaces the sprite and moves to the given position.
191 	public void replaceSprite(ABitmap s, int n, int x, int y) @trusted nothrow;
192 	///Replaces the sprite and moves to the given position.
193 	public void replaceSprite(ABitmap s, int n, Box c) @trusted nothrow;
194 	///Returns the displayed portion of the sprite.
195 	public @nogc Box getSlice(int n) @trusted nothrow;
196 	///Writes the displayed portion of the sprite.
197 	///Returns the new slice, if invalid (greater than the bitmap, etc.) returns the old one.
198 	public Box setSlice(int n, Box slice) @trusted nothrow;
199 	///Returns the selected paletteID of the sprite.
200 	public @nogc ushort getPaletteID(int n) @trusted nothrow;
201 	///Sets the paletteID of the sprite. Returns the new ID, which is truncated to the possible values with a simple binary and operation
202 	///Palette must exist in the parent Raster, otherwise AccessError might happen
203 	public @nogc ushort setPaletteID(int n, ushort paletteID) @trusted nothrow;
204 	///Scales bitmap horizontally
205 	public int scaleSpriteHoriz(int n, int hScl) @trusted nothrow;
206 	///Scales bitmap vertically
207 	public int scaleSpriteVert(int n, int vScl) @trusted nothrow;
208 	///Gets the sprite's current horizontal scale value
209 	public int getScaleSpriteHoriz(int n) @nogc @trusted nothrow;
210 	///Gets the sprite's current vertical scale value
211 	public int getScaleSpriteVert(int n) @nogc @trusted nothrow;
212 }