DEADSOFTWARE

FlexUI: completely reworked graphics layer -- it is using drawing contexts now
[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, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *)
17 {$INCLUDE ../shared/a_modes.inc}
18 unit fui_common;
20 interface
23 // ////////////////////////////////////////////////////////////////////////// //
24 type
25 TLaySize = record
26 public
27 w, h: Integer;
29 private
30 function getIdx (idx: Integer): Integer; inline;
31 procedure setIdx (idx, v: Integer); inline;
33 public
34 constructor Create (aw, ah: Integer);
36 function toString (): AnsiString;
38 function equals (constref a: TLaySize): Boolean; inline;
39 public
40 property item[idx: Integer]: Integer read getIdx write setIdx; default;
41 end;
43 TLayPos = record
44 public
45 x, y: Integer;
47 private
48 function getIdx (idx: Integer): Integer; inline;
49 procedure setIdx (idx, v: Integer); inline;
51 public
52 constructor Create (ax, ay: Integer);
54 function toString (): AnsiString;
56 function equals (constref a: TLayPos): Boolean; inline;
58 public
59 property item[idx: Integer]: Integer read getIdx write setIdx; default;
60 end;
62 TLayMargins = record
63 public
64 top, right, bottom, left: Integer;
66 public
67 constructor Create (atop, aright, abottom, aleft: Integer);
69 function toString (): AnsiString;
71 function horiz (): Integer; inline;
72 function vert (): Integer; inline;
73 end;
76 // ////////////////////////////////////////////////////////////////////////// //
77 type
78 TGxRGBA = packed record
79 public
80 r, g, b, a: Byte;
82 public
83 constructor Create (ar, ag, ab: Integer; aa: Integer=255);
85 function asUInt (): LongWord; inline;
86 function isOpaque (): Boolean; inline;
87 function isTransparent (): Boolean; inline;
89 // WARNING! This function does blending in RGB space, and RGB space is not linear!
90 // alpha value of `self` doesn't matter
91 // `aa` means: 255 for replace color, 0 for keep `self`
92 function blend (ar, ag, ab, aa: Integer): TGxRGBA; inline;
93 end;
95 TGxRect = packed record
96 public
97 x, y, w, h: Integer;
99 public
100 constructor Create (ax, ay, aw, ah: Integer);
102 function empty (): Boolean; inline; // invalid rects are empty too
103 function valid (): Boolean; inline;
105 // modifies this rect, so it won't be bigger than `r`
106 // returns `false` if this rect becomes empty
107 function intersect (constref r: TGxRect): Boolean; inline;
108 end;
110 TGxOfs = packed record
111 public
112 xofs, yofs: Integer;
114 public
115 constructor Create (axofs, ayofs: Integer);
116 end;
119 // ////////////////////////////////////////////////////////////////////////// //
120 // return `false` if destination rect is empty
121 // modifies rect0
122 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean; inline;
123 procedure normRGBA (var r, g, b, a: Integer); inline;
126 implementation
128 uses
129 utils;
131 // ////////////////////////////////////////////////////////////////////////// //
132 constructor TLaySize.Create (aw, ah: Integer); begin w := aw; h := ah; end;
133 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;
134 procedure TLaySize.setIdx (idx, v: Integer); inline; begin if (idx = 0) then w := v else if (idx = 1) then h := v; end;
135 function TLaySize.toString (): AnsiString; begin result := formatstrf('[%d,%d]', [w, h]); end;
136 function TLaySize.equals (constref a: TLaySize): Boolean; inline; begin result := (w = a.w) and (h = a.h); end;
138 constructor TLayPos.Create (ax, ay: Integer); begin x := ax; y := ay; end;
139 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;
140 procedure TLayPos.setIdx (idx, v: Integer); inline; begin if (idx = 0) then x := v else if (idx = 1) then y := v; end;
141 function TLayPos.toString (): AnsiString; begin result := formatstrf('(%d,%d)', [x, y]); end;
142 function TLayPos.equals (constref a: TLayPos): Boolean; inline; begin result := (x = a.x) and (y = a.y); end;
144 constructor TLayMargins.Create (atop, aright, abottom, aleft: Integer);
145 begin
146 if (atop < 0) then atop := 0;
147 if (aright < 0) then aright := 0;
148 if (abottom < 0) then abottom := 0;
149 if (aleft < 0) then aleft := 0;
150 left := aleft;
151 right := aright;
152 top := atop;
153 bottom := abottom;
154 end;
155 function TLayMargins.toString (): AnsiString; begin result := formatstrf('(%s,%s,%s,%s)', [top, right, bottom, left]); end;
156 function TLayMargins.horiz (): Integer; inline; begin result := left+right; end;
157 function TLayMargins.vert (): Integer; inline; begin result := top+bottom; end;
160 // ////////////////////////////////////////////////////////////////////////// //
161 constructor TGxRGBA.Create (ar, ag, ab: Integer; aa: Integer=255);
162 begin
163 if (ar < 0) then r := 0 else if (ar > 255) then r := 255 else r := Byte(ar);
164 if (ag < 0) then g := 0 else if (ag > 255) then g := 255 else g := Byte(ag);
165 if (ab < 0) then b := 0 else if (ab > 255) then b := 255 else b := Byte(ab);
166 if (aa < 0) then a := 0 else if (aa > 255) then a := 255 else a := Byte(aa);
167 end;
169 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;
171 function TGxRGBA.isOpaque (): Boolean; inline; begin result := (a = 255); end;
172 function TGxRGBA.isTransparent (): Boolean; inline; begin result := (a = 0); end;
174 function TGxRGBA.blend (ar, ag, ab, aa: Integer): TGxRGBA; inline;
175 var
176 me, it, a_tmp_, dc_tmp_, srb_tmp_, sg_tmp_, drb_tmp_, dg_tmp_, orb_tmp_, og_tmp_: LongWord;
177 begin
178 if (aa <= 0) then begin result := self; exit; end;
179 result := TGxRGBA.Create(ar, ag, ab, aa);
180 if (aa >= 255) then begin result.a := a; exit; end;
181 me := asUInt;
182 it := result.asUInt;
183 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
184 dc_tmp_ := me and $ffffff;
185 srb_tmp_ := (it and $ff00ff);
186 sg_tmp_ := (it and $00ff00);
187 drb_tmp_ := (dc_tmp_ and $ff00ff);
188 dg_tmp_ := (dc_tmp_ and $00ff00);
189 orb_tmp_ := (drb_tmp_+(((srb_tmp_-drb_tmp_)*a_tmp_+$800080) shr 8)) and $ff00ff;
190 og_tmp_ := (dg_tmp_+(((sg_tmp_-dg_tmp_)*a_tmp_+$008000) shr 8)) and $00ff00;
191 me := (orb_tmp_ or og_tmp_); // or $ff000000; /* and $ffffff;*/
192 result.r := Byte(me and $ff);
193 result.g := Byte((me shr 8) and $ff);
194 result.b := Byte((me shr 16) and $ff);
195 result.a := a;
196 end;
199 // ////////////////////////////////////////////////////////////////////////// //
200 constructor TGxRect.Create (ax, ay, aw, ah: Integer); begin x := ax; y := ay; w := aw; h := ah; end;
202 function TGxRect.empty (): Boolean; inline; begin result := (w <= 0) or (h <= 0); end;
203 function TGxRect.valid (): Boolean; inline; begin result := (w < 0) or (h < 0); end;
205 function TGxRect.intersect (constref r: TGxRect): Boolean; inline;
206 begin
207 result := intersectRect(x, y, w, h, r.x, r.y, r.w, r.h);
208 end;
211 // ////////////////////////////////////////////////////////////////////////// //
212 constructor TGxOfs.Create (axofs, ayofs: Integer); begin xofs := axofs; yofs := ayofs; end;
215 // ////////////////////////////////////////////////////////////////////////// //
216 //TODO: overflow checks
217 function intersectRect (var x0, y0, w0, h0: Integer; const x1, y1, w1, h1: Integer): Boolean; inline;
218 var
219 ex0, ey0: Integer;
220 ex1, ey1: Integer;
221 begin
222 result := false;
223 if (w0 < 1) or (h0 < 1) or (w1 < 1) or (h1 < 1) then exit; // at least one rect is null
224 // check for intersection
225 ex0 := x0+w0;
226 ey0 := y0+h0;
227 ex1 := x1+w1;
228 ey1 := y1+h1;
229 if (ex0 <= x1) or (ey0 <= y1) or (ex1 <= x0) or (ey1 <= y0) then exit;
230 if (x0 >= ex1) or (y0 >= ey1) or (x1 >= ex0) or (y1 >= ey0) then exit;
231 // ok, intersects
232 if (x0 < x1) then x0 := x1;
233 if (y0 < y1) then y0 := y1;
234 if (ex0 > ex1) then ex0 := ex1;
235 if (ey0 > ey1) then ey0 := ey1;
236 w0 := ex0-x0;
237 h0 := ey0-y0;
238 result := (w0 > 0) and (h0 > 0);
239 end;
242 procedure normRGBA (var r, g, b, a: Integer); inline;
243 begin
244 if (a < 0) then a := 0 else if (a > 255) then a := 255;
245 if (r < 0) then r := 0 else if (r > 255) then r := 255;
246 if (g < 0) then g := 0 else if (g > 255) then g := 255;
247 if (b < 0) then b := 0 else if (b > 255) then b := 255;
248 end;
251 end.