types.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (c) 2018 by Mattias Gaertner
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. unit Types;
  11. {$mode objfpc}
  12. interface
  13. type
  14. TDirection = (FromBeginning, FromEnd);
  15. TBooleanDynArray = array of Boolean;
  16. TWordDynArray = array of Word;
  17. TIntegerDynArray = array of Integer;
  18. TNativeIntDynArray = array of NativeInt;
  19. TStringDynArray = array of String;
  20. TDoubleDynArray = array of Double;
  21. TJSValueDynArray = array of JSValue;
  22. TObjectDynArray = array of TObject;
  23. TByteDynArray = array of Byte;
  24. TDuplicates = (dupIgnore, dupAccept, dupError);
  25. TProc = Reference to Procedure;
  26. TProcString = Reference to Procedure(Const aString : String);
  27. TListCallback = procedure(data, arg: JSValue) of object;
  28. TListStaticCallback = procedure(data, arg: JSValue);
  29. TSize = record
  30. cx, cy: integer;
  31. end;
  32. TPoint = record
  33. x, y: integer;
  34. end;
  35. TRect = record
  36. Left, Top, Right, Bottom: Integer;
  37. end;
  38. function EqualRect(const r1,r2 : TRect) : Boolean;
  39. function Rect(Left, Top, Right, Bottom : Integer) : TRect;
  40. function Bounds(ALeft, ATop, AWidth, AHeight : Integer) : TRect;
  41. function Point(x,y : Integer): TPoint; {$IFDEF Has_Inline}inline;{$ENDIF}
  42. function PtInRect(const aRect: TRect; const p: TPoint) : Boolean;
  43. function IntersectRect(out aRect: TRect; const R1,R2: TRect) : Boolean;
  44. function UnionRect(out aRect: TRect; const R1,R2: TRect) : Boolean;
  45. function IsRectEmpty(const aRect: TRect) : Boolean;
  46. function OffsetRect(var aRect: TRect; DX, DY: Integer) : Boolean;
  47. function CenterPoint(const aRect: TRect): TPoint;
  48. function InflateRect(var aRect: TRect; dx, dy: Integer): Boolean;
  49. function Size(AWidth, AHeight: Integer): TSize;
  50. function Size(const aRect: TRect): TSize;
  51. implementation
  52. function EqualRect(const r1, r2: TRect): Boolean;
  53. begin
  54. Result:=(r1.left=r2.left) and (r1.right=r2.right) and (r1.top=r2.top) and (r1.bottom=r2.bottom);
  55. end;
  56. function Rect(Left, Top, Right, Bottom: Integer): TRect;
  57. begin
  58. Result.Left:=Left;
  59. Result.Top:=Top;
  60. Result.Right:=Right;
  61. Result.Bottom:=Bottom;
  62. end;
  63. function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
  64. begin
  65. Result.Left:=ALeft;
  66. Result.Top:=ATop;
  67. Result.Right:=ALeft+AWidth;
  68. Result.Bottom:=ATop+AHeight;
  69. end;
  70. function Point(x, y: Integer): TPoint;
  71. begin
  72. Result.x:=x;
  73. Result.y:=y;
  74. end;
  75. function PtInRect(const aRect: TRect; const p: TPoint): Boolean;
  76. begin
  77. Result:=(p.y>=aRect.Top) and
  78. (p.y<aRect.Bottom) and
  79. (p.x>=aRect.Left) and
  80. (p.x<aRect.Right);
  81. end;
  82. function IntersectRect(out aRect: TRect; const R1, R2: TRect): Boolean;
  83. var
  84. lRect: TRect;
  85. begin
  86. lRect := R1;
  87. if R2.Left > R1.Left then
  88. lRect.Left := R2.Left;
  89. if R2.Top > R1.Top then
  90. lRect.Top := R2.Top;
  91. if R2.Right < R1.Right then
  92. lRect.Right := R2.Right;
  93. if R2.Bottom < R1.Bottom then
  94. lRect.Bottom := R2.Bottom;
  95. // The var parameter is only assigned in the end to avoid problems
  96. // when passing the same rectangle in the var and const parameters.
  97. if IsRectEmpty(lRect) then
  98. begin
  99. aRect:=Rect(0,0,0,0);
  100. Result:=false;
  101. end
  102. else
  103. begin
  104. Result:=true;
  105. aRect := lRect;
  106. end;
  107. end;
  108. function UnionRect(out aRect: TRect; const R1, R2: TRect): Boolean;
  109. var
  110. lRect: TRect;
  111. begin
  112. lRect:=R1;
  113. if R2.Left<R1.Left then
  114. lRect.Left:=R2.Left;
  115. if R2.Top<R1.Top then
  116. lRect.Top:=R2.Top;
  117. if R2.Right>R1.Right then
  118. lRect.Right:=R2.Right;
  119. if R2.Bottom>R1.Bottom then
  120. lRect.Bottom:=R2.Bottom;
  121. if IsRectEmpty(lRect) then
  122. begin
  123. aRect:=Rect(0,0,0,0);
  124. Result:=false;
  125. end
  126. else
  127. begin
  128. aRect:=lRect;
  129. Result:=true;
  130. end;
  131. end;
  132. function IsRectEmpty(const aRect: TRect): Boolean;
  133. begin
  134. Result:=(aRect.Right<=aRect.Left) or (aRect.Bottom<=aRect.Top);
  135. end;
  136. function OffsetRect(var aRect: TRect; DX, DY: Integer): Boolean;
  137. begin
  138. with aRect do
  139. begin
  140. inc(Left,dx);
  141. inc(Top,dy);
  142. inc(Right,dx);
  143. inc(Bottom,dy);
  144. end;
  145. Result:=true;
  146. end;
  147. function CenterPoint(const aRect: TRect): TPoint;
  148. function Avg(a, b: Longint): Longint;
  149. begin
  150. if a < b then
  151. Result := a + ((b - a) shr 1)
  152. else
  153. Result := b + ((a - b) shr 1);
  154. end;
  155. begin
  156. with aRect do
  157. begin
  158. Result.X := Avg(Left, Right);
  159. Result.Y := Avg(Top, Bottom);
  160. end;
  161. end;
  162. function InflateRect(var aRect: TRect; dx, dy: Integer): Boolean;
  163. begin
  164. with aRect do
  165. begin
  166. dec(Left, dx);
  167. dec(Top, dy);
  168. inc(Right, dx);
  169. inc(Bottom, dy);
  170. end;
  171. Result := True;
  172. end;
  173. function Size(AWidth, AHeight: Integer): TSize;
  174. begin
  175. Result.cx := AWidth;
  176. Result.cy := AHeight;
  177. end;
  178. function Size(const aRect: TRect): TSize;
  179. begin
  180. Result.cx := aRect.Right - aRect.Left;
  181. Result.cy := aRect.Bottom - aRect.Top;
  182. end;
  183. end.