DEADSOFTWARE

Net: Add sanity checks. Forward ports only after host is created.
[d2df-sdl.git] / src / flexui / fui_common.pas
1 (* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License ONLY.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit fui_common;
19 interface
22 // ////////////////////////////////////////////////////////////////////////// //
23 type
24 TLaySize = record
25 public
26 w, h: Integer;
28 private
29 function getIdx (idx: Integer): Integer; inline;
30 procedure setIdx (idx, v: Integer); inline;
32 public
33 constructor Create (aw, ah: Integer);
35 function toString (): AnsiString;
37 function equals (constref a: TLaySize): Boolean; inline;
38 public
39 property item[idx: Integer]: Integer read getIdx write setIdx; default;
40 end;
42 TLayPos = record
43 public
44 x, y: Integer;
46 private
47 function getIdx (idx: Integer): Integer; inline;
48 procedure setIdx (idx, v: Integer); inline;
50 public
51 constructor Create (ax, ay: Integer);
53 function toString (): AnsiString;
55 function equals (constref a: TLayPos): Boolean; inline;
57 public
58 property item[idx: Integer]: Integer read getIdx write setIdx; default;
59 end;
61 TLayMargins = record
62 public
63 top, right, bottom, left: Integer;
65 public
66 constructor Create (atop, aright, abottom, aleft: Integer);
68 function toString (): AnsiString;
70 function horiz (): Integer; inline;
71 function vert (): Integer; inline;
72 end;
75 // ////////////////////////////////////////////////////////////////////////// //
76 type
77 TGxRGBA = packed record
78 public
79 r, g, b, a: Byte;
81 public
82 constructor Create (ar, ag, ab: Integer; aa: Integer=255);
84 function asUInt (): LongWord; inline;
85 function isOpaque (): Boolean; inline;
86 function isTransparent (): Boolean; inline;
88 function toString (): AnsiString;
90 // WARNING! This function does blending in RGB space, and RGB space is not linear!
91 // alpha value of `self` doesn't matter
92 // `aa` means: 255 for replace color, 0 for keep `self`
93 function blend (ar, ag, ab, aa: Integer): TGxRGBA; inline;
94 end;
96 TGxRect = packed record
97 public
98 x, y, w, h: Integer;
100 public
101 constructor Create (ax, ay, aw, ah: Integer);
103 function empty (): Boolean; inline; // invalid rects are empty too
104 function valid (): Boolean; inline;
106 function toString (): AnsiString;
108 // modifies this rect, so it won't be bigger than `r`
109 // returns `false` if this rect becomes empty
110 function intersect (constref r: TGxRect): Boolean; inline;
111 end;
113 TGxOfs = packed record
114 public
115 xofs, yofs: Integer;
117 public
118 constructor Create (axofs, ayofs: Integer);
119 end;
122 // ////////////////////////////////////////////////////////////////////////// //
123 // return `false` if destination rect is empty
124 // modifies rect0
125 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean; inline;
126 procedure normRGBA (var r, g, b, a: Integer); inline;
129 implementation
131 uses
132 utils;
134 // ////////////////////////////////////////////////////////////////////////// //
135 constructor TLaySize.Create (aw, ah: Integer); begin w := aw; h := ah; end;
136 function TLaySize.getIdx (idx: Integer): Integer; inline; begin if (idx = 0) then result := w else if (idx = 1) then result := h else result := -1; end;
137 procedure TLaySize.setIdx (idx, v: Integer); inline; begin if (idx = 0) then w := v else if (idx = 1) then h := v; end;
138 function TLaySize.toString (): AnsiString; begin result := formatstrf('[%d,%d]', [w, h]); end;
139 function TLaySize.equals (constref a: TLaySize): Boolean; inline; begin result := (w = a.w) and (h = a.h); end;
141 constructor TLayPos.Create (ax, ay: Integer); begin x := ax; y := ay; end;
142 function TLayPos.getIdx (idx: Integer): Integer; inline; begin if (idx = 0) then result := x else if (idx = 1) then result := y else result := -1; end;
143 procedure TLayPos.setIdx (idx, v: Integer); inline; begin if (idx = 0) then x := v else if (idx = 1) then y := v; end;
144 function TLayPos.toString (): AnsiString; begin result := formatstrf('(%d,%d)', [x, y]); end;
145 function TLayPos.equals (constref a: TLayPos): Boolean; inline; begin result := (x = a.x) and (y = a.y); end;
147 constructor TLayMargins.Create (atop, aright, abottom, aleft: Integer);
148 begin
149 if (atop < 0) then atop := 0;
150 if (aright < 0) then aright := 0;
151 if (abottom < 0) then abottom := 0;
152 if (aleft < 0) then aleft := 0;
153 left := aleft;
154 right := aright;
155 top := atop;
156 bottom := abottom;
157 end;
158 function TLayMargins.toString (): AnsiString; begin result := formatstrf('(%s,%s,%s,%s)', [top, right, bottom, left]); end;
159 function TLayMargins.horiz (): Integer; inline; begin result := left+right; end;
160 function TLayMargins.vert (): Integer; inline; begin result := top+bottom; end;
163 // ////////////////////////////////////////////////////////////////////////// //
164 constructor TGxRGBA.Create (ar, ag, ab: Integer; aa: Integer=255);
165 begin
166 if (ar < 0) then r := 0 else if (ar > 255) then r := 255 else r := Byte(ar);
167 if (ag < 0) then g := 0 else if (ag > 255) then g := 255 else g := Byte(ag);
168 if (ab < 0) then b := 0 else if (ab > 255) then b := 255 else b := Byte(ab);
169 if (aa < 0) then a := 0 else if (aa > 255) then a := 255 else a := Byte(aa);
170 end;
172 function TGxRGBA.asUInt (): LongWord; inline; begin result := LongWord(r) or (LongWord(g) shl 8) or (LongWord(b) shl 16) or (LongWord(a) shl 24); end;
174 function TGxRGBA.isOpaque (): Boolean; inline; begin result := (a = 255); end;
175 function TGxRGBA.isTransparent (): Boolean; inline; begin result := (a = 0); end;
177 function TGxRGBA.blend (ar, ag, ab, aa: Integer): TGxRGBA; inline;
178 var
179 me, it, a_tmp_, dc_tmp_, srb_tmp_, sg_tmp_, drb_tmp_, dg_tmp_, orb_tmp_, og_tmp_: LongWord;
180 begin
181 if (aa <= 0) then begin result := self; exit; end;
182 result := TGxRGBA.Create(ar, ag, ab, aa);
183 if (aa >= 255) then begin result.a := a; exit; end;
184 me := asUInt;
185 it := result.asUInt;
186 a_tmp_ := (256-(255-(it shr 24))) and (-(1-(((255-(it shr 24))+1) shr 8))); // to not loose bits, but 255 should become 0
187 dc_tmp_ := me and $ffffff;
188 srb_tmp_ := (it and $ff00ff);
189 sg_tmp_ := (it and $00ff00);
190 drb_tmp_ := (dc_tmp_ and $ff00ff);
191 dg_tmp_ := (dc_tmp_ and $00ff00);
192 orb_tmp_ := (drb_tmp_+(((srb_tmp_-drb_tmp_)*a_tmp_+$800080) shr 8)) and $ff00ff;
193 og_tmp_ := (dg_tmp_+(((sg_tmp_-dg_tmp_)*a_tmp_+$008000) shr 8)) and $00ff00;
194 me := (orb_tmp_ or og_tmp_); // or $ff000000; /* and $ffffff;*/
195 result.r := Byte(me and $ff);
196 result.g := Byte((me shr 8) and $ff);
197 result.b := Byte((me shr 16) and $ff);
198 result.a := a;
199 end;
201 function TGxRGBA.toString (): AnsiString;
202 begin
203 if (a = 255) then result := formatstrf('rgb(%s,%s,%s)', [r, g, b])
204 else result := formatstrf('rgba(%s,%s,%s,%s)', [r, g, b, a]);
205 end;
208 // ////////////////////////////////////////////////////////////////////////// //
209 constructor TGxRect.Create (ax, ay, aw, ah: Integer); begin x := ax; y := ay; w := aw; h := ah; end;
211 function TGxRect.empty (): Boolean; inline; begin result := (w <= 0) or (h <= 0); end;
212 function TGxRect.valid (): Boolean; inline; begin result := (w < 0) or (h < 0); end;
214 function TGxRect.intersect (constref r: TGxRect): Boolean; inline;
215 begin
216 result := intersectRect(x, y, w, h, r.x, r.y, r.w, r.h);
217 end;
219 function TGxRect.toString (): AnsiString; begin result := formatstrf('(%s,%s;%sx%s)', [x, y, w, h]); end;
222 // ////////////////////////////////////////////////////////////////////////// //
223 constructor TGxOfs.Create (axofs, ayofs: Integer); begin xofs := axofs; yofs := ayofs; end;
226 // ////////////////////////////////////////////////////////////////////////// //
227 //TODO: overflow checks
228 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean; inline;
229 var
230 ex0, ey0: Integer;
231 ex1, ey1: Integer;
232 begin
233 result := false;
234 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit; // at least one rect is null
235 // check for intersection
236 ex0 := x0+w0;
237 ey0 := y0+h0;
238 ex1 := x1+w1;
239 ey1 := y1+h1;
240 if (ex0 <= x1) or (ey0 <= y1) or (ex1 <= x0) or (ey1 <= y0) then exit;
241 if (x0 >= ex1) or (y0 >= ey1) or (x1 >= ex0) or (y1 >= ey0) then exit;
242 // ok, intersects
243 if (x0 < x1) then x0 := x1;
244 if (y0 < y1) then y0 := y1;
245 if (ex0 > ex1) then ex0 := ex1;
246 if (ey0 > ey1) then ey0 := ey1;
247 w0 := ex0-x0;
248 h0 := ey0-y0;
249 result := (w0 > 0) and (h0 > 0);
250 end;
253 procedure normRGBA (var r, g, b, a: Integer); inline;
254 begin
255 if (a < 0) then a := 0 else if (a > 255) then a := 255;
256 if (r < 0) then r := 0 else if (r > 255) then r := 255;
257 if (g < 0) then g := 0 else if (g > 255) then g := 255;
258 if (b < 0) then b := 0 else if (b > 255) then b := 255;
259 end;
262 end.