types.pas 4.7 KB

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