lcvectorialfillcontrol.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. unit LCVectorialFillControl;
  3. {$mode objfpc}{$H+}
  4. interface
  5. uses
  6. Types, Classes, SysUtils, Controls, LCVectorialFillInterface,
  7. LCVectorialFill, BGRABitmap, BGRABitmapTypes, BGRAGradientScanner,
  8. LCVectorOriginal;
  9. type
  10. TLCFillTarget = LCVectorialFillInterface.TLCFillTarget;
  11. const
  12. ftPen = LCVectorialFillInterface.ftPen;
  13. ftBack = LCVectorialFillInterface.ftBack;
  14. ftOutline = LCVectorialFillInterface.ftOutline;
  15. type
  16. { TLCVectorialFillControl }
  17. TLCVectorialFillControl = class(TWinControl)
  18. private
  19. function GetAllowedFillTypes: TVectorialFillTypes;
  20. function GetAverageColor: TBGRAPixel;
  21. function GetCanAdjustToShape: boolean;
  22. function GetCanEditGradTexPoints: boolean;
  23. function GetEditingGradTexPoints: boolean;
  24. function GetFillType: TVectorialFillType;
  25. function GetGradEndColor: TBGRAPixel;
  26. function GetGradInterp: TBGRAColorInterpolation;
  27. function GetGradRepetition: TBGRAGradientRepetition;
  28. function GetGradStartColor: TBGRAPixel;
  29. function GetGradType: TGradientType;
  30. function GetIsTarget: boolean;
  31. function GetPreferredSizeAsSize: TSize;
  32. function GetSolidColor: TBGRAPixel;
  33. function GetTexOpacity: byte;
  34. function GetTexRepetition: TTextureRepetition;
  35. function GetTexture: TBGRABitmap;
  36. function GetToolIconSize: integer;
  37. function GetVerticalPadding: integer;
  38. procedure InterfaceMouseDown(Sender: TObject; Button: TMouseButton;
  39. Shift: TShiftState; X, Y: Integer);
  40. procedure InterfaceMouseEnter(Sender: TObject);
  41. procedure InterfaceMouseLeave(Sender: TObject);
  42. procedure InterfaceMouseMove(Sender: TObject; Shift: TShiftState; X,
  43. Y: Integer);
  44. procedure InterfaceMouseUp(Sender: TObject; Button: TMouseButton;
  45. Shift: TShiftState; X, Y: Integer);
  46. procedure SetAllowedFillTypes(AValue: TVectorialFillTypes);
  47. procedure SetCanAdjustToShape(AValue: boolean);
  48. procedure SetCanEditGradTexPoints(AValue: boolean);
  49. procedure SetEditingGradTexPoints(AValue: boolean);
  50. procedure SetFillType(AValue: TVectorialFillType);
  51. procedure SetGradEndColor(AValue: TBGRAPixel);
  52. procedure SetGradientType(AValue: TGradientType);
  53. procedure SetGradInterpolation(AValue: TBGRAColorInterpolation);
  54. procedure SetGradRepetition(AValue: TBGRAGradientRepetition);
  55. procedure SetGradStartColor(AValue: TBGRAPixel);
  56. procedure SetIsTarget(AValue: boolean);
  57. procedure SetOnChooseColor(AValue: TChooseColorEvent);
  58. procedure SetOnTextureClick(AValue: TNotifyEvent);
  59. procedure SetSolidColor(AValue: TBGRAPixel);
  60. procedure SetTexture(AValue: TBGRABitmap);
  61. procedure SetTextureOpacity(AValue: byte);
  62. procedure SetTextureRepetition(AValue: TTextureRepetition);
  63. procedure SetToolIconSize(AValue: integer);
  64. procedure SetVerticalPadding(AValue: integer);
  65. protected
  66. FInterface: TVectorialFillInterface;
  67. FOnAdjustToShape: TNotifyEvent;
  68. FOnEditGradTexPoints: TNotifyEvent;
  69. FOnChooseColor: TChooseColorEvent;
  70. FOnFillChange: TNotifyEvent;
  71. FOnOpacityChange: TNotifyEvent;
  72. FOnFillTypeChange: TNotifyEvent;
  73. FOnTextureClick: TNotifyEvent;
  74. FOnTextureChange: TNotifyEvent;
  75. procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
  76. {%H-}WithThemeSpace: Boolean); override;
  77. procedure DoOnAdjustToShape(Sender: TObject);
  78. procedure DoOnEditGradTexPoints(Sender: TObject);
  79. procedure DoOnFillChange(Sender: TObject);
  80. procedure DoOnFillTypeChange(Sender: TObject);
  81. procedure DoOnOpacityChange(Sender: TObject);
  82. procedure DoOnTextureClick(Sender: TObject);
  83. procedure DoOnTextureChange(Sender: TObject);
  84. procedure DoOnResize; override;
  85. procedure DoOnChooseColor({%H-}ASender: TObject; AButton: TMouseButton;
  86. AColorIndex: integer; var AColorValue: TBGRAPixel; out AHandled: boolean);
  87. public
  88. constructor Create(TheOwner: TComponent); override;
  89. destructor Destroy; override;
  90. procedure AssignFill(AFill: TVectorialFill);
  91. function CreateShapeFill(AShape: TVectorShape): TVectorialFill;
  92. procedure UpdateShapeFill(AShape: TVectorShape; ATarget: TLCFillTarget);
  93. procedure UpdateFillExceptGeometry(ATargetFill: TVectorialFill);
  94. property FillType: TVectorialFillType read GetFillType write SetFillType;
  95. property IsTarget: boolean read GetIsTarget write SetIsTarget;
  96. property AverageColor: TBGRAPixel read GetAverageColor;
  97. property SolidColor: TBGRAPixel read GetSolidColor write SetSolidColor;
  98. property GradientType: TGradientType read GetGradType write SetGradientType;
  99. property GradStartColor: TBGRAPixel read GetGradStartColor write SetGradStartColor;
  100. property GradEndColor: TBGRAPixel read GetGradEndColor write SetGradEndColor;
  101. property GradRepetition: TBGRAGradientRepetition read GetGradRepetition write SetGradRepetition;
  102. property GradInterpolation: TBGRAColorInterpolation read GetGradInterp write SetGradInterpolation;
  103. property Texture: TBGRABitmap read GetTexture write SetTexture;
  104. property TextureRepetition: TTextureRepetition read GetTexRepetition write SetTextureRepetition;
  105. property TextureOpacity: byte read GetTexOpacity write SetTextureOpacity;
  106. property CanAdjustToShape: boolean read GetCanAdjustToShape write SetCanAdjustToShape;
  107. property PreferredSize: TSize read GetPreferredSizeAsSize;
  108. published
  109. property AutoSize;
  110. property Align;
  111. property Enabled;
  112. property Visible;
  113. property ToolIconSize: integer read GetToolIconSize write SetToolIconSize;
  114. property VerticalPadding: integer read GetVerticalPadding write SetVerticalPadding;
  115. property AllowedFillTypes: TVectorialFillTypes read GetAllowedFillTypes write SetAllowedFillTypes;
  116. property CanEditGradTexPoints: boolean read GetCanEditGradTexPoints write SetCanEditGradTexPoints;
  117. property EditingGradTexPoints: boolean read GetEditingGradTexPoints write SetEditingGradTexPoints;
  118. property OnChooseColor: TChooseColorEvent read FOnChooseColor write SetOnChooseColor;
  119. property OnFillChange: TNotifyEvent read FOnFillChange write FOnFillChange;
  120. property OnOpacityChange: TNotifyEvent read FOnOpacityChange write FOnOpacityChange;
  121. property OnTextureChange: TNotifyEvent read FOnTextureChange write FOnTextureChange;
  122. property OnAdjustToShape: TNotifyEvent read FOnAdjustToShape write FOnAdjustToShape;
  123. property OnEditGradTexPoints: TNotifyEvent read FOnEditGradTexPoints write FOnEditGradTexPoints;
  124. property OnFillTypeChange: TNotifyEvent read FOnFillTypeChange write FOnFillTypeChange;
  125. property OnTextureClick: TNotifyEvent read FOnTextureClick write SetOnTextureClick;
  126. property OnMouseDown;
  127. property OnMouseMove;
  128. property OnMouseUp;
  129. property OnMouseEnter;
  130. property OnMouseLeave;
  131. end;
  132. procedure Register;
  133. implementation
  134. procedure Register;
  135. begin
  136. RegisterComponents('Lazpaint Controls', [TLCVectorialFillControl]);
  137. end;
  138. { TLCVectorialFillControl }
  139. procedure TLCVectorialFillControl.DoOnChooseColor(ASender: TObject; AButton: TMouseButton;
  140. AColorIndex: integer; var AColorValue: TBGRAPixel; out AHandled: boolean);
  141. begin
  142. If Assigned(FOnChooseColor) then
  143. FOnChooseColor(self, AButton, AColorIndex, AColorValue, AHandled)
  144. else
  145. AHandled := false;
  146. end;
  147. procedure TLCVectorialFillControl.DoOnTextureClick(Sender: TObject);
  148. begin
  149. if Assigned(FOnTextureClick) then FOnTextureClick(self);
  150. end;
  151. function TLCVectorialFillControl.GetAllowedFillTypes: TVectorialFillTypes;
  152. begin
  153. result := FInterface.AllowedFillTypes;
  154. end;
  155. function TLCVectorialFillControl.GetAverageColor: TBGRAPixel;
  156. begin
  157. result := FInterface.AverageColor;
  158. end;
  159. function TLCVectorialFillControl.GetCanAdjustToShape: boolean;
  160. begin
  161. result := FInterface.CanAdjustToShape;
  162. end;
  163. function TLCVectorialFillControl.GetCanEditGradTexPoints: boolean;
  164. begin
  165. result := FInterface.CanEditGradTexPoints;
  166. end;
  167. function TLCVectorialFillControl.GetEditingGradTexPoints: boolean;
  168. begin
  169. result := FInterface.EditingGradTexPoints;
  170. end;
  171. function TLCVectorialFillControl.GetFillType: TVectorialFillType;
  172. begin
  173. result := FInterface.FillType;
  174. end;
  175. function TLCVectorialFillControl.GetGradEndColor: TBGRAPixel;
  176. begin
  177. result := FInterface.GradEndColor;
  178. end;
  179. function TLCVectorialFillControl.GetGradInterp: TBGRAColorInterpolation;
  180. begin
  181. result := FInterface.GradInterpolation;
  182. end;
  183. function TLCVectorialFillControl.GetGradRepetition: TBGRAGradientRepetition;
  184. begin
  185. result := FInterface.GradRepetition;
  186. end;
  187. function TLCVectorialFillControl.GetGradStartColor: TBGRAPixel;
  188. begin
  189. result := FInterface.GradStartColor;
  190. end;
  191. function TLCVectorialFillControl.GetGradType: TGradientType;
  192. begin
  193. result := FInterface.GradientType;
  194. end;
  195. function TLCVectorialFillControl.GetIsTarget: boolean;
  196. begin
  197. result := FInterface.IsTarget;
  198. end;
  199. function TLCVectorialFillControl.GetPreferredSizeAsSize: TSize;
  200. begin
  201. result.cx:= Width;
  202. result.cy:= Height;
  203. GetPreferredSize(result.cx, result.cy);
  204. end;
  205. function TLCVectorialFillControl.GetSolidColor: TBGRAPixel;
  206. begin
  207. result := FInterface.SolidColor;
  208. end;
  209. function TLCVectorialFillControl.GetTexOpacity: byte;
  210. begin
  211. result := FInterface.TextureOpacity;
  212. end;
  213. function TLCVectorialFillControl.GetTexRepetition: TTextureRepetition;
  214. begin
  215. result := FInterface.TextureRepetition;
  216. end;
  217. function TLCVectorialFillControl.GetTexture: TBGRABitmap;
  218. begin
  219. result := FInterface.Texture;
  220. end;
  221. function TLCVectorialFillControl.GetToolIconSize: integer;
  222. begin
  223. result := FInterface.ImageListSize.cy;
  224. end;
  225. function TLCVectorialFillControl.GetVerticalPadding: integer;
  226. begin
  227. result := FInterface.VerticalPadding;
  228. end;
  229. procedure TLCVectorialFillControl.InterfaceMouseDown(Sender: TObject;
  230. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  231. begin
  232. if Assigned(OnMouseDown) then OnMouseDown(self, Button, Shift, X,Y);
  233. end;
  234. procedure TLCVectorialFillControl.InterfaceMouseEnter(Sender: TObject);
  235. begin
  236. if Assigned(OnMouseEnter) then OnMouseEnter(self);
  237. end;
  238. procedure TLCVectorialFillControl.InterfaceMouseLeave(Sender: TObject);
  239. begin
  240. if Assigned(OnMouseLeave) then OnMouseLeave(self);
  241. end;
  242. procedure TLCVectorialFillControl.InterfaceMouseMove(Sender: TObject;
  243. Shift: TShiftState; X, Y: Integer);
  244. begin
  245. if Assigned(OnMouseMove) then OnMouseMove(self, Shift, X,Y);
  246. end;
  247. procedure TLCVectorialFillControl.InterfaceMouseUp(Sender: TObject;
  248. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  249. begin
  250. if Assigned(OnMouseUp) then OnMouseUp(self, Button, Shift, X,Y);
  251. end;
  252. procedure TLCVectorialFillControl.SetAllowedFillTypes(
  253. AValue: TVectorialFillTypes);
  254. begin
  255. FInterface.AllowedFillTypes:= AValue;
  256. end;
  257. procedure TLCVectorialFillControl.SetCanAdjustToShape(AValue: boolean);
  258. begin
  259. FInterface.CanAdjustToShape := AValue;
  260. end;
  261. procedure TLCVectorialFillControl.SetCanEditGradTexPoints(AValue: boolean);
  262. begin
  263. FInterface.CanEditGradTexPoints:= AValue;
  264. end;
  265. procedure TLCVectorialFillControl.SetEditingGradTexPoints(AValue: boolean);
  266. begin
  267. FInterface.EditingGradTexPoints := AValue;
  268. end;
  269. procedure TLCVectorialFillControl.SetFillType(AValue: TVectorialFillType);
  270. begin
  271. FInterface.FillType := AValue;
  272. end;
  273. procedure TLCVectorialFillControl.SetGradEndColor(AValue: TBGRAPixel);
  274. begin
  275. FInterface.GradEndColor := AValue;
  276. end;
  277. procedure TLCVectorialFillControl.SetGradientType(AValue: TGradientType);
  278. begin
  279. FInterface.GradientType := AValue;
  280. end;
  281. procedure TLCVectorialFillControl.SetGradInterpolation(
  282. AValue: TBGRAColorInterpolation);
  283. begin
  284. FInterface.GradInterpolation := AValue;
  285. end;
  286. procedure TLCVectorialFillControl.SetGradRepetition(
  287. AValue: TBGRAGradientRepetition);
  288. begin
  289. FInterface.GradRepetition := AValue;
  290. end;
  291. procedure TLCVectorialFillControl.SetGradStartColor(AValue: TBGRAPixel);
  292. begin
  293. FInterface.GradStartColor := AValue;
  294. end;
  295. procedure TLCVectorialFillControl.SetIsTarget(AValue: boolean);
  296. begin
  297. FInterface.IsTarget := AValue;
  298. end;
  299. procedure TLCVectorialFillControl.SetOnChooseColor(AValue: TChooseColorEvent);
  300. begin
  301. if FOnChooseColor=AValue then Exit;
  302. FOnChooseColor:=AValue;
  303. end;
  304. procedure TLCVectorialFillControl.SetOnTextureClick(AValue: TNotifyEvent);
  305. begin
  306. if FOnTextureClick=AValue then Exit;
  307. FOnTextureClick:=AValue;
  308. if Assigned(FOnTextureClick) then
  309. FInterface.OnTextureClick:= @DoOnTextureClick
  310. else
  311. FInterface.OnTextureClick:= nil;
  312. end;
  313. procedure TLCVectorialFillControl.SetSolidColor(AValue: TBGRAPixel);
  314. begin
  315. FInterface.SolidColor := AValue;
  316. end;
  317. procedure TLCVectorialFillControl.SetTexture(AValue: TBGRABitmap);
  318. begin
  319. FInterface.Texture := AValue;
  320. end;
  321. procedure TLCVectorialFillControl.SetTextureOpacity(AValue: byte);
  322. begin
  323. FInterface.TextureOpacity := AValue;
  324. end;
  325. procedure TLCVectorialFillControl.SetTextureRepetition(
  326. AValue: TTextureRepetition);
  327. begin
  328. FInterface.TextureRepetition := AValue;
  329. end;
  330. procedure TLCVectorialFillControl.SetToolIconSize(AValue: integer);
  331. begin
  332. FInterface.ImageListSize := Size(AValue,AValue);
  333. end;
  334. procedure TLCVectorialFillControl.SetVerticalPadding(AValue: integer);
  335. begin
  336. FInterface.VerticalPadding:= AValue;
  337. end;
  338. procedure TLCVectorialFillControl.CalculatePreferredSize(var PreferredWidth,
  339. PreferredHeight: integer; WithThemeSpace: Boolean);
  340. begin
  341. with FInterface.PreferredSize do
  342. begin
  343. PreferredWidth := cx;
  344. PreferredHeight := cy;
  345. end;
  346. end;
  347. procedure TLCVectorialFillControl.DoOnAdjustToShape(Sender: TObject);
  348. begin
  349. if Assigned(FOnAdjustToShape) then FOnAdjustToShape(self);
  350. end;
  351. procedure TLCVectorialFillControl.DoOnEditGradTexPoints(Sender: TObject);
  352. begin
  353. if Assigned(FOnEditGradTexPoints) then FOnEditGradTexPoints(self);
  354. end;
  355. procedure TLCVectorialFillControl.DoOnFillChange(Sender: TObject);
  356. begin
  357. if Assigned(FOnFillChange) then FOnFillChange(self);
  358. end;
  359. procedure TLCVectorialFillControl.DoOnFillTypeChange(Sender: TObject);
  360. begin
  361. InvalidatePreferredSize;
  362. AdjustSize;
  363. if Assigned(FOnFillTypeChange) then FOnFillTypeChange(self);
  364. end;
  365. procedure TLCVectorialFillControl.DoOnOpacityChange(Sender: TObject);
  366. begin
  367. if Assigned(FOnOpacityChange) then FOnOpacityChange(self);
  368. end;
  369. procedure TLCVectorialFillControl.DoOnTextureChange(Sender: TObject);
  370. begin
  371. if Assigned(FOnTextureChange) then FOnTextureChange(self);
  372. end;
  373. procedure TLCVectorialFillControl.DoOnResize;
  374. begin
  375. inherited DoOnResize;
  376. FInterface.LoadImageList;
  377. FInterface.ContainerSizeChanged;
  378. end;
  379. constructor TLCVectorialFillControl.Create(TheOwner: TComponent);
  380. begin
  381. inherited Create(TheOwner);
  382. FInterface := TVectorialFillInterface.Create(nil, 16,16);
  383. FInterface.OnChooseColor:=@DoOnChooseColor;
  384. FInterface.OnFillChange:=@DoOnFillChange;
  385. FInterface.OnTextureChange:=@DoOnTextureChange;
  386. FInterface.OnAdjustToShape:=@DoOnAdjustToShape;
  387. FInterface.OnEditGradTexPoints:=@DoOnEditGradTexPoints;
  388. FInterface.OnFillTypeChange:=@DoOnFillTypeChange;
  389. FInterface.OnOpacityChange:=@DoOnOpacityChange;
  390. FInterface.OnMouseMove:=@InterfaceMouseMove;
  391. FInterface.OnMouseDown:=@InterfaceMouseDown;
  392. FInterface.OnMouseUp:=@InterfaceMouseUp;
  393. FInterface.OnMouseEnter:=@InterfaceMouseEnter;
  394. FInterface.OnMouseLeave:=@InterfaceMouseLeave;
  395. FInterface.Container := self;
  396. end;
  397. destructor TLCVectorialFillControl.Destroy;
  398. begin
  399. FreeAndNil(FInterface);
  400. inherited Destroy;
  401. end;
  402. procedure TLCVectorialFillControl.AssignFill(AFill: TVectorialFill);
  403. begin
  404. FInterface.AssignFill(AFill);
  405. end;
  406. function TLCVectorialFillControl.CreateShapeFill(AShape: TVectorShape): TVectorialFill;
  407. begin
  408. result := FInterface.CreateShapeFill(AShape);
  409. end;
  410. procedure TLCVectorialFillControl.UpdateShapeFill(AShape: TVectorShape;
  411. ATarget: TLCFillTarget);
  412. begin
  413. FInterface.UpdateShapeFill(AShape, ATarget);
  414. end;
  415. procedure TLCVectorialFillControl.UpdateFillExceptGeometry(ATargetFill: TVectorialFill);
  416. begin
  417. FInterface.UpdateFillExceptGeometry(ATargetFill);
  418. end;
  419. end.