types.pas 5.0 KB

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