lcvectorialfillcontrol.pas 16 KB

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