lcvectorialfillinterface.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. unit LCVectorialFillInterface;
  3. {$mode objfpc}{$H+}
  4. interface
  5. uses
  6. Classes, SysUtils, Types,
  7. Controls, ComCtrls, Menus, Dialogs, ExtDlgs, ExtCtrls,
  8. BGRAImageList, BCTrackbarUpdown,
  9. BGRABitmap, BGRABitmapTypes, LCVectorialFill, LCVectorOriginal,
  10. BGRAGradientScanner, Graphics, BGRAGraphics;
  11. function GradRepetitionToStr(AValue: TBGRAGradientRepetition): string;
  12. function ColorInterpToStr(AValue: TBGRAColorInterpolation): string;
  13. function TextureRepetitionToStr(AValue: TTextureRepetition): string;
  14. type
  15. TLCFillTarget = (ftPen, ftBack, ftOutline);
  16. TChooseColorEvent = procedure(ASender: TObject; AButton: TMouseButton; AColorIndex: integer;
  17. var AColorValue: TBGRAPixel; out AHandled: boolean) of object;
  18. { TVectorialFillInterface }
  19. TVectorialFillInterface = class(TComponent)
  20. private
  21. FAllowKeyInput: boolean;
  22. FCanEditGradTexPoints: boolean;
  23. FIsTarget: boolean;
  24. FOnMouseDown: TMouseEvent;
  25. FOnMouseEnter: TNotifyEvent;
  26. FOnMouseLeave: TNotifyEvent;
  27. FOnMouseMove: TMouseMoveEvent;
  28. FOnMouseUp: TMouseEvent;
  29. procedure EditGradTextPointsClick(Sender: TObject);
  30. function GetEditingGradTexPoints: boolean;
  31. procedure Preview_MouseUp(Sender: TObject; Button: TMouseButton;
  32. {%H-}Shift: TShiftState; X, {%H-}Y: Integer);
  33. procedure SetAllowKeyInput(AValue: boolean);
  34. procedure SetCanEditGradTexPoints(AValue: boolean);
  35. procedure SetEditingGradTexPoints(AValue: boolean);
  36. procedure SetIsTarget(AValue: boolean);
  37. procedure SetVerticalPadding(AValue: integer);
  38. procedure ToolbarMouseDown(Sender: TObject; Button: TMouseButton;
  39. Shift: TShiftState; X, Y: Integer);
  40. procedure ToolbarMouseEnter(Sender: TObject);
  41. procedure ToolbarMouseLeave(Sender: TObject);
  42. procedure ToolbarMouseMove(Sender: TObject; Shift: TShiftState; X,
  43. Y: Integer);
  44. procedure ToolbarMouseUp(Sender: TObject; Button: TMouseButton;
  45. Shift: TShiftState; X, Y: Integer);
  46. procedure AnyButtonMouseDown(Sender: TObject; Button: TMouseButton;
  47. Shift: TShiftState; X, Y: Integer);
  48. procedure AnyButtonMouseEnter(Sender: TObject);
  49. procedure AnyButtonMouseLeave(Sender: TObject);
  50. procedure AnyButtonMouseMove(Sender: TObject; Shift: TShiftState; X,
  51. Y: Integer);
  52. procedure AnyButtonMouseUp(Sender: TObject; Button: TMouseButton;
  53. Shift: TShiftState; X, Y: Integer);
  54. protected
  55. FFillType: TVectorialFillType;
  56. FAllowedFillTypes: TVectorialFillTypes;
  57. FSolidColor: TBGRAPixel;
  58. FOnChooseColor: TChooseColorEvent;
  59. FGradStartColor, FGradEndColor: TBGRAPixel;
  60. FGradType: TGradientType;
  61. FGradRepetition: TBGRAGradientRepetition;
  62. FGradInterp: TBGRAColorInterpolation;
  63. FTexRepetition: TTextureRepetition;
  64. FTexture: TBGRABitmap;
  65. FTexOpacity: byte;
  66. FTextureAverageColor: TBGRAPixel;
  67. FTextureAverageColorComputed: boolean;
  68. //interface
  69. FContainer: TWinControl;
  70. FVerticalPadding: integer;
  71. FPreview: TImage;
  72. FButtonFillNone, FButtonFillSolid,
  73. FButtonFillGradient, FButtonFillTexture: TToolButton;
  74. FOnFillChange, FOnFillTypeChange, FOnOpacityChange: TNotifyEvent;
  75. FButtonEditGradTexPoints, FButtonAdjustToShape: TToolButton;
  76. FOnEditGradTexPoints, FOnAdjustToShape: TNotifyEvent;
  77. FSolidColorInterfaceCreated: boolean;
  78. FShapeSolidColor: TShape;
  79. FUpDownSolidAlpha: TBCTrackbarUpdown;
  80. FSolidColorChange: TNotifyEvent;
  81. FTextureInterfaceCreated: boolean;
  82. FCanAdjustToShape: boolean;
  83. FButtonTexRepeat, FButtonLoadTexture: TToolButton;
  84. FUpDownTexAlpha: TBCTrackbarUpdown;
  85. FOnTextureClick: TNotifyEvent;
  86. FOnTextureChange: TNotifyEvent;
  87. FGradientInterfaceCreated: boolean;
  88. //FShapeStartColor, FShapeEndColor: TShape;
  89. FUpDownStartAlpha, FUpDownEndAlpha: TBCTrackbarUpdown;
  90. FButtonSwapColor, FButtonGradRepetition, FButtonGradInterp: TToolButton;
  91. FGradTypeMenu, FGradRepetitionMenu, FGradInterpMenu: TPopupMenu;
  92. FColorDlg: TColorDialog;
  93. FOpenPictureDlg: TOpenPictureDialog;
  94. FTexRepetitionMenu: TPopupMenu;
  95. FToolbar: TToolBar;
  96. FImageList: TBGRAImageList;
  97. FImageListLoaded: boolean;
  98. FImageListSize: TSize;
  99. procedure AdjustToShapeClick(Sender: TObject);
  100. procedure ButtonFillChange(Sender: TObject);
  101. procedure ButtonFillGradClick(Sender: TObject);
  102. procedure ButtonFillTexClick(Sender: TObject);
  103. procedure ButtonGradInterpClick(Sender: TObject);
  104. procedure ButtonGradRepetitionClick(Sender: TObject);
  105. procedure ButtonLoadTextureClick(Sender: TObject);
  106. procedure ButtonSwapColorClick(Sender: TObject);
  107. procedure ButtonTexRepeatClick(Sender: TObject);
  108. procedure Changed(AUpdatePreview: boolean = True);
  109. procedure OnClickBackGradType(ASender: TObject);
  110. procedure OnClickBackTexRepeat(ASender: TObject);
  111. procedure OnClickGradInterp(ASender: TObject);
  112. procedure OnClickGradRepeat(ASender: TObject);
  113. function GetPreferredSize: TSize;
  114. function GetAverageColor: TBGRAPixel;
  115. procedure SetCanAdjustToShape(AValue: boolean);
  116. procedure SetContainer(AValue: TWinControl);
  117. procedure SetFillType(AValue: TVectorialFillType);
  118. procedure SetAllowedFillTypes(AValue: TVectorialFillTypes);
  119. procedure SetSolidColor(AValue: TBGRAPixel);
  120. procedure SetGradientType(AValue: TGradientType);
  121. procedure SetGradEndColor(AValue: TBGRAPixel);
  122. procedure SetGradStartColor(AValue: TBGRAPixel);
  123. procedure SetGradRepetition(AValue: TBGRAGradientRepetition);
  124. procedure SetGradInterpolation(AValue: TBGRAColorInterpolation);
  125. procedure SetImageListSize(AValue: TSize);
  126. procedure SetTexture(AValue: TBGRABitmap);
  127. procedure SetTextureRepetition(AValue: TTextureRepetition);
  128. procedure SetTextureOpacity(AValue: byte);
  129. procedure SetOnTextureClick(AValue: TNotifyEvent);
  130. // procedure ShapeEndColorMouseUp({%H-}Sender: TObject; {%H-}Button: TMouseButton;
  131. // {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
  132. procedure ShapeSolidColorMouseUp({%H-}Sender: TObject; {%H-}Button: TMouseButton;
  133. {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
  134. // procedure ShapeStartColorMouseUp({%H-}Sender: TObject; {%H-}Button: TMouseButton;
  135. // {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
  136. procedure UpdateAccordingToFillType;
  137. procedure UpdateTopToolbar;
  138. procedure UpdatePreview;
  139. procedure UpdateShapeSolidColor;
  140. procedure UpdateTextureParams;
  141. procedure UpdateGradientParams;
  142. procedure UpdateButtonAdjustToShape;
  143. procedure UpDownEndAlphaChange(Sender: TObject; AByUser: boolean);
  144. procedure UpDownSolidAlphaChange(Sender: TObject; AByUser: boolean);
  145. procedure UpDownStartAlphaChange(Sender: TObject; AByUser: boolean);
  146. procedure UpDownTexAlphaChange(Sender: TObject; AByUser: boolean);
  147. procedure ChooseColor(AColorIndex: integer; AButton: TMouseButton);
  148. procedure CreateSolidColorInterface;
  149. procedure CreateGradientInterface;
  150. procedure CreateTextureInterface;
  151. procedure HideSolidColorInterface;
  152. procedure HideGradientInterface;
  153. procedure HideTextureInterface;
  154. procedure Init(AImageListWidth,AImageListHeight: Integer);
  155. procedure AttachMouseEvent(AControl: TToolBar); overload;
  156. procedure AttachMouseEvent(AControl: TToolButton); overload;
  157. procedure AttachMouseEvent(AControl: TBCTrackbarUpdown); overload;
  158. procedure AttachMouseEvent(AControl: TImage); overload;
  159. public
  160. constructor Create(AOwner: TComponent); override;
  161. constructor Create(AOwner: TComponent; AImageListWidth,AImageListHeight: Integer);
  162. destructor Destroy; override;
  163. procedure LoadTexture;
  164. procedure LoadImageList;
  165. procedure ContainerSizeChanged;
  166. function GetTextureThumbnail(AWidth, AHeight: integer; ABackColor: TColor): TBitmap;
  167. procedure AssignFill(AFill: TVectorialFill);
  168. procedure UpdateFillExceptGeometry(ATargetFill: TVectorialFill);
  169. function CreateShapeFill(AShape: TVectorShape): TVectorialFill;
  170. procedure UpdateShapeFill(AShape: TVectorShape; ATarget: TLCFillTarget);
  171. property FillType: TVectorialFillType read FFillType write SetFillType;
  172. property IsTarget: boolean read FIsTarget write SetIsTarget;
  173. property SolidColor: TBGRAPixel read FSolidColor write SetSolidColor;
  174. property AverageColor: TBGRAPixel read GetAverageColor;
  175. property GradientType: TGradientType read FGradType write SetGradientType;
  176. property GradStartColor: TBGRAPixel read FGradStartColor write SetGradStartColor;
  177. property GradEndColor: TBGRAPixel read FGradEndColor write SetGradEndColor;
  178. property GradRepetition: TBGRAGradientRepetition read FGradRepetition write SetGradRepetition;
  179. property GradInterpolation: TBGRAColorInterpolation read FGradInterp write SetGradInterpolation;
  180. property Texture: TBGRABitmap read FTexture write SetTexture;
  181. property TextureRepetition: TTextureRepetition read FTexRepetition write SetTextureRepetition;
  182. property TextureOpacity: byte read FTexOpacity write SetTextureOpacity;
  183. property CanAdjustToShape: boolean read FCanAdjustToShape write SetCanAdjustToShape;
  184. property CanEditGradTexPoints: boolean read FCanEditGradTexPoints write SetCanEditGradTexPoints;
  185. property EditingGradTexPoints: boolean read GetEditingGradTexPoints write SetEditingGradTexPoints;
  186. property OnFillChange: TNotifyEvent read FOnFillChange write FOnFillChange;
  187. property OnTextureChange: TNotifyEvent read FOnTextureChange write FOnTextureChange;
  188. property OnTextureClick: TNotifyEvent read FOnTextureClick write SetOnTextureClick;
  189. property OnAdjustToShape: TNotifyEvent read FOnAdjustToShape write FOnAdjustToShape;
  190. property OnEditGradTexPoints: TNotifyEvent read FOnEditGradTexPoints write FOnEditGradTexPoints;
  191. property OnFillTypeChange: TNotifyEvent read FOnFillTypeChange write FOnFillTypeChange;
  192. property OnOpacityChange: TNotifyEvent read FOnOpacityChange write FOnOpacityChange;
  193. property OnChooseColor: TChooseColorEvent read FOnChooseColor write FOnChooseColor;
  194. property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;
  195. property OnMouseMove: TMouseMoveEvent read FOnMouseMove write FOnMouseMove;
  196. property OnMouseUp: TMouseEvent read FOnMouseUp write FOnMouseUp;
  197. property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
  198. property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  199. property Container: TWinControl read FContainer write SetContainer;
  200. property ImageListSize: TSize read FImageListSize write SetImageListSize;
  201. property VerticalPadding: integer read FVerticalPadding write SetVerticalPadding;
  202. property PreferredSize: TSize read GetPreferredSize;
  203. property AllowedFillTypes: TVectorialFillTypes read FAllowedFillTypes write SetAllowedFillTypes;
  204. property AllowKeyInput: boolean read FAllowKeyInput write SetAllowKeyInput;
  205. end;
  206. implementation
  207. uses LCToolbars, BGRAThumbnail, LResources,
  208. LCVectorShapes, BGRAGradientOriginal, BGRATransform, math,
  209. LCResourceString;
  210. function GradRepetitionToStr(AValue: TBGRAGradientRepetition): string;
  211. begin
  212. case AValue of
  213. grPad: result := rsGrPad;
  214. grRepeat: result := rsGrRepeat;
  215. grReflect: result := rsGrReflect;
  216. grSine: result := rsGrSine;
  217. else result := '';
  218. end;
  219. end;
  220. function ColorInterpToStr(AValue: TBGRAColorInterpolation): string;
  221. begin
  222. case AValue of
  223. ciStdRGB: result := rsCiStdRGB;
  224. ciLinearRGB: result := rsCiLinearRGB;
  225. ciLinearHSLPositive: result := rsCiLinearHSLPositive;
  226. ciLinearHSLNegative: result := rsCiLinearHSLNegative;
  227. ciGSBPositive: result := rsCiGSBPositive;
  228. ciGSBNegative: result := rsCiGSBNegative;
  229. else result := '';
  230. end;
  231. end;
  232. function TextureRepetitionToStr(AValue: TTextureRepetition): string;
  233. begin
  234. case AValue of
  235. trNone: result := rsTrNone;
  236. trRepeatX: result := rsTrRepeatX;
  237. trRepeatY: result := rsTrRepeatY;
  238. trRepeatBoth: result := rsTrRepeatBoth;
  239. else result := '';
  240. end;
  241. end;
  242. { TVectorialFillInterface }
  243. procedure TVectorialFillInterface.LoadImageList;
  244. var
  245. i: Integer;
  246. lst: TStringList;
  247. begin
  248. if FImageList = nil then FImageList := TBGRAImageList.Create(self);
  249. if FImageListLoaded and (FImageList.Width=FImageListSize.cx) and (FImageList.Height=FImageListSize.cy) then exit;
  250. FImageList.Clear;
  251. FImageList.Width := FImageListSize.cx;
  252. FImageList.Height := FImageListSize.cy;
  253. {$IFDEF DARWIN}
  254. FImageList.Scaled := true;
  255. FImageList.RegisterResolutions([FImageListSize.cx, FImageListSize.cx*2]);
  256. {$ENDIF}
  257. lst := TStringList.Create;
  258. lst.CommaText := GetResourceString('fillimages.lst');
  259. for i := 0 to lst.Count-1 do
  260. LoadToolbarImage(FImageList, i, lst[i]);
  261. lst.Free;
  262. FImageListLoaded := true;
  263. if Assigned(FToolbar) then
  264. begin
  265. SetToolbarImages(FToolbar, FImageList, 5, VerticalPadding);
  266. for i := 0 to FToolbar.ControlCount-1 do
  267. if FToolbar.Controls[i] is TBCTrackbarUpdown then
  268. FToolbar.Controls[i].Width := FToolbar.ButtonWidth*2
  269. else if FToolbar.Controls[i] is TShape then
  270. FToolbar.Controls[i].Width := FToolbar.ButtonWidth;
  271. end;
  272. UpdatePreview;
  273. end;
  274. procedure TVectorialFillInterface.Changed(AUpdatePreview: boolean);
  275. begin
  276. if AUpdatePreview then UpdatePreview;
  277. if Assigned(FOnFillChange) then
  278. FOnFillChange(self);
  279. end;
  280. procedure TVectorialFillInterface.OnClickBackGradType(ASender: TObject);
  281. begin
  282. GradientType:= TGradientType((ASender as TMenuItem).Tag);
  283. FillType := vftGradient;
  284. end;
  285. procedure TVectorialFillInterface.OnClickBackTexRepeat(ASender: TObject);
  286. begin
  287. TextureRepetition := TTextureRepetition((ASender as TMenuItem).Tag);
  288. end;
  289. procedure TVectorialFillInterface.OnClickGradInterp(ASender: TObject);
  290. begin
  291. GradInterpolation:= TBGRAColorInterpolation((ASender as TMenuItem).Tag);
  292. end;
  293. procedure TVectorialFillInterface.OnClickGradRepeat(ASender: TObject);
  294. begin
  295. GradRepetition:= TBGRAGradientRepetition((ASender as TMenuItem).Tag);
  296. end;
  297. procedure TVectorialFillInterface.SetTexture(AValue: TBGRABitmap);
  298. begin
  299. if FTexture=AValue then Exit;
  300. if Assigned(FTexture) then
  301. begin
  302. FTexture.FreeReference;
  303. FTexture := nil;
  304. end;
  305. if Assigned(AValue) then
  306. FTexture := AValue.NewReference as TBGRABitmap;
  307. FTextureAverageColorComputed := false;
  308. if Assigned(FOnTextureChange) then FOnTextureChange(self);
  309. if FFillType = vftTexture then Changed;
  310. end;
  311. procedure TVectorialFillInterface.LoadTexture;
  312. var
  313. newTex: TBGRABitmap;
  314. begin
  315. if FOpenPictureDlg.Execute then
  316. begin
  317. try
  318. newTex := TBGRABitmap.Create(FOpenPictureDlg.FileName, true);
  319. Texture := newTex;
  320. newTex.FreeReference;
  321. FillType:= vftTexture;
  322. except
  323. on ex: exception do
  324. ShowMessage(ex.Message);
  325. end;
  326. end;
  327. end;
  328. procedure TVectorialFillInterface.ContainerSizeChanged;
  329. begin
  330. FToolbar.Align:= alTop;
  331. FToolbar.Height := FContainer.Height;
  332. end;
  333. procedure TVectorialFillInterface.SetFillType(AValue: TVectorialFillType);
  334. begin
  335. if FFillType=AValue then Exit;
  336. FFillType:=AValue;
  337. UpdateAccordingToFillType;
  338. UpdatePreview;
  339. if Assigned(FOnFillTypeChange) then FOnFillTypeChange(self);
  340. Changed(False);
  341. end;
  342. procedure TVectorialFillInterface.ShapeSolidColorMouseUp(Sender: TObject;
  343. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  344. begin
  345. ChooseColor(-1, Button);
  346. end;
  347. {procedure TVectorialFillInterface.ShapeStartColorMouseUp(Sender: TObject;
  348. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  349. begin
  350. ChooseColor(0, Button);
  351. end;}
  352. procedure TVectorialFillInterface.UpdateAccordingToFillType;
  353. begin
  354. FButtonFillNone.Down := FillType = vftNone;
  355. FButtonFillSolid.Down := FillType = vftSolid;
  356. FButtonFillGradient.Down := FillType = vftGradient;
  357. FButtonFillTexture.Down := FillType = vftTexture;
  358. UpdateButtonAdjustToShape;
  359. if FillType <> vftSolid then HideSolidColorInterface;
  360. if FillType <> vftGradient then HideGradientInterface;
  361. if FillType <> vftTexture then HideTextureInterface;
  362. case FillType of
  363. vftSolid: begin
  364. CreateSolidColorInterface;
  365. UpdateShapeSolidColor;
  366. ShowAppendToolButtons([FShapeSolidColor,FUpDownSolidAlpha]);
  367. end;
  368. vftGradient: begin
  369. CreateGradientInterface;
  370. UpdateGradientParams;
  371. ShowAppendToolButtons([FButtonGradRepetition,FButtonGradInterp,
  372. {FShapeStartColor,}FUpDownStartAlpha,FButtonSwapColor,
  373. {FShapeEndColor,}FUpDownEndAlpha]);
  374. end;
  375. vftTexture: begin
  376. CreateTextureInterface;
  377. UpdateTextureParams;
  378. ShowAppendToolButtons([FButtonTexRepeat,FUpDownTexAlpha,FButtonLoadTexture]);
  379. end;
  380. end;
  381. end;
  382. procedure TVectorialFillInterface.UpdateTopToolbar;
  383. var
  384. x: Integer;
  385. begin
  386. FToolbar.BeginUpdate;
  387. x := FToolbar.Indent;
  388. FButtonFillNone.Left := x;
  389. //FButtonFillNone.Wrap := [vftSolid,vftGradient,vftTexture]*FAllowedFillTypes = [];
  390. FButtonFillNone.Visible:= vftNone in FAllowedFillTypes;
  391. if vftNone in FAllowedFillTypes then inc(x, FButtonFillNone.Width);
  392. FButtonFillSolid.Left := x;
  393. //FButtonFillSolid.Wrap := [vftGradient,vftTexture]*FAllowedFillTypes = [];
  394. FButtonFillSolid.Visible:= vftSolid in FAllowedFillTypes;
  395. if vftSolid in FAllowedFillTypes then inc(x, FButtonFillSolid.Width);
  396. FButtonFillGradient.Left := x;
  397. //FButtonFillGradient.Wrap := [vftTexture]*FAllowedFillTypes = [];
  398. FButtonFillGradient.Visible:= vftGradient in FAllowedFillTypes;
  399. if vftGradient in FAllowedFillTypes then inc(x, FButtonFillGradient.Width);
  400. FButtonFillTexture.Left := x;
  401. FButtonFillTexture.Visible:= vftTexture in FAllowedFillTypes;
  402. if vftTexture in FAllowedFillTypes then inc(x, FButtonFillTexture.Width);
  403. FPreview.Left := x;
  404. inc(x, FPreview.Width);
  405. FButtonEditGradTexPoints.Left := x;
  406. inc(x, FButtonEditGradTexPoints.Width);
  407. FButtonAdjustToShape.Left := x;
  408. FToolbar.EndUpdate;
  409. end;
  410. procedure TVectorialFillInterface.UpdatePreview;
  411. var
  412. bmp, thumb: TBGRABitmap;
  413. grad: TBGRALayerGradientOriginal;
  414. bmpCopy: TBitmap;
  415. ratio: single;
  416. previewWidth: Integer;
  417. begin
  418. if FillType = vftGradient then
  419. previewWidth := round(FToolbar.ButtonWidth*1.5)
  420. else previewWidth := FToolbar.ButtonWidth;
  421. FPreview.Width:= previewWidth + round(FToolbar.ButtonWidth*0.2);
  422. FPreview.Height:= FToolbar.ButtonHeight;
  423. if not FImageListLoaded then exit;
  424. bmp := TBGRABitmap.Create(previewWidth, FPreview.Height - VerticalPadding);
  425. bmp.DrawCheckers(bmp.ClipRect, CSSWhite, CSSSilver);
  426. case FillType of
  427. vftSolid: bmp.Fill(SolidColor, dmDrawWithTransparency);
  428. vftTexture:
  429. if Assigned(FTexture) and (FTexture.Width > 0) and (FTexture.Height > 0) then
  430. begin
  431. ratio := min(bmp.Width/FTexture.Width, bmp.Height/FTexture.Height);
  432. if ratio > 1 then ratio := 1;
  433. thumb := TBGRABitmap.Create(max(round(FTexture.Width*ratio),1),
  434. max(round(FTexture.Height*ratio),1));
  435. thumb.StretchPutImage(thumb.ClipRect, FTexture, dmSet);
  436. bmp.Fill(thumb, dmDrawWithTransparency, TextureOpacity*$0101);
  437. thumb.Free;
  438. end;
  439. vftGradient:
  440. begin
  441. grad := TBGRALayerGradientOriginal.Create;
  442. grad.StartColor := GradStartColor;
  443. grad.EndColor := GradEndColor;
  444. grad.Origin := PointF(0,0);
  445. grad.XAxis := PointF(bmp.Width, 0);
  446. grad.ColorInterpolation:= GradInterpolation;
  447. grad.Render(bmp, AffineMatrixIdentity, false, dmDrawWithTransparency);
  448. grad.Free;
  449. end;
  450. end;
  451. if IsTarget then
  452. begin
  453. if bmp.GetPixel(bmp.Width/2,bmp.Height/2).Lightness > 20000 then
  454. bmp.Rectangle(bmp.ClipRect, BGRABlack, dmDrawWithTransparency)
  455. else bmp.Rectangle(bmp.ClipRect, CSSSilver, dmDrawWithTransparency);
  456. end
  457. else bmp.Rectangle(bmp.ClipRect, BGRA(0,0,0,128), dmDrawWithTransparency);
  458. bmpCopy := bmp.MakeBitmapCopy(clBtnFace);
  459. bmp.Free;
  460. FPreview.Picture.Assign(bmpCopy);
  461. bmpCopy.Free;
  462. if (FillType = vftTexture) and Assigned(Texture) and Assigned(FOnTextureClick) then
  463. FPreview.Cursor := crHandPoint
  464. else
  465. FPreview.Cursor := crDefault;
  466. end;
  467. procedure TVectorialFillInterface.UpdateShapeSolidColor;
  468. var
  469. c: TBGRAPixel;
  470. begin
  471. c := SolidColor;
  472. c.alpha := 255;
  473. if Assigned(FShapeSolidColor) then FShapeSolidColor.Brush.Color := c;
  474. if Assigned(FUpDownSolidAlpha) then FUpDownSolidAlpha.Value := SolidColor.alpha;
  475. end;
  476. procedure TVectorialFillInterface.UpdateTextureParams;
  477. begin
  478. if Assigned(FButtonTexRepeat) then FButtonTexRepeat.ImageIndex := 17 + ord(TextureRepetition);
  479. if Assigned(FUpDownTexAlpha) then FUpDownTexAlpha.Value := TextureOpacity;
  480. end;
  481. procedure TVectorialFillInterface.UpdateGradientParams;
  482. {var
  483. c: TBGRAPixel;}
  484. begin
  485. { c := GradStartColor;
  486. c.alpha := 255;
  487. if Assigned(FShapeStartColor) then FShapeStartColor.Brush.Color := c;}
  488. if Assigned(FUpDownStartAlpha) then FUpDownStartAlpha.Value := GradStartColor.alpha;
  489. { c := GradEndColor;
  490. c.alpha := 255;
  491. if Assigned(FShapeEndColor) then FShapeEndColor.Brush.Color := c;}
  492. if Assigned(FUpDownEndAlpha) then FUpDownEndAlpha.Value := GradEndColor.alpha;
  493. if Assigned(FButtonGradRepetition) then FButtonGradRepetition.ImageIndex := 7+ord(FGradRepetition);
  494. if Assigned(FButtonGradInterp) then FButtonGradInterp.ImageIndex := 11+ord(FGradInterp);
  495. end;
  496. procedure TVectorialFillInterface.UpdateButtonAdjustToShape;
  497. begin
  498. if Assigned(FButtonAdjustToShape) then
  499. begin
  500. FButtonAdjustToShape.Enabled := FCanAdjustToShape and (FillType in[vftGradient,vftTexture]);
  501. if FillType in[vftGradient,vftTexture] then
  502. FButtonAdjustToShape.Style := tbsButton
  503. else
  504. FButtonAdjustToShape.Style := tbsDivider;
  505. end;
  506. if Assigned(FButtonEditGradTexPoints) then
  507. begin
  508. FButtonEditGradTexPoints.Enabled := FCanEditGradTexPoints and (FillType in [vftGradient,vftTexture]);
  509. if FillType in [vftGradient,vftTexture] then
  510. FButtonEditGradTexPoints.Style := tbsCheck
  511. else
  512. FButtonEditGradTexPoints.Style := tbsDivider;
  513. end;
  514. end;
  515. procedure TVectorialFillInterface.UpDownEndAlphaChange(Sender: TObject;
  516. AByUser: boolean);
  517. var
  518. c: TBGRAPixel;
  519. begin
  520. if AByUser then
  521. begin
  522. c := GradEndColor;
  523. c.alpha := FUpDownEndAlpha.Value;
  524. GradEndColor:= c;
  525. if assigned(FOnOpacityChange) then FOnOpacityChange(self);
  526. end;
  527. end;
  528. procedure TVectorialFillInterface.UpDownSolidAlphaChange(Sender: TObject;
  529. AByUser: boolean);
  530. begin
  531. if AByUser then
  532. begin
  533. SolidColor:= ColorToBGRA(FShapeSolidColor.Brush.Color, FUpDownSolidAlpha.Value);
  534. if assigned(FOnOpacityChange) then FOnOpacityChange(self);
  535. end;
  536. end;
  537. procedure TVectorialFillInterface.UpDownStartAlphaChange(Sender: TObject;
  538. AByUser: boolean);
  539. var
  540. c: TBGRAPixel;
  541. begin
  542. if AByUser then
  543. begin
  544. c := GradStartColor;
  545. c.alpha := FUpDownStartAlpha.Value;
  546. GradStartColor:= c;
  547. if assigned(FOnOpacityChange) then FOnOpacityChange(self);
  548. end;
  549. end;
  550. procedure TVectorialFillInterface.UpDownTexAlphaChange(Sender: TObject;
  551. AByUser: boolean);
  552. begin
  553. if AByUser then
  554. begin
  555. FTexOpacity:= FUpDownTexAlpha.Value;
  556. if FillType = vftTexture then Changed;
  557. end;
  558. end;
  559. procedure TVectorialFillInterface.ChooseColor(AColorIndex: integer; AButton: TMouseButton);
  560. procedure AssignNewColor(AColor: TBGRAPixel);
  561. begin
  562. case AColorIndex of
  563. -1: SolidColor := AColor;
  564. 0: GradStartColor := AColor;
  565. 1: GradEndColor := AColor;
  566. end;
  567. end;
  568. var
  569. curColorBGRA: TBGRAPixel;
  570. curColor: TColor;
  571. handled: boolean;
  572. begin
  573. case AColorIndex of
  574. -1: curColorBGRA := SolidColor;
  575. 0: curColorBGRA := GradStartColor;
  576. 1: curColorBGRA := GradEndColor;
  577. else exit;
  578. end;
  579. if Assigned(FOnChooseColor) then
  580. begin
  581. FOnChooseColor(self, AButton, AColorIndex, curColorBGRA, handled);
  582. if handled then
  583. begin
  584. AssignNewColor( curColorBGRA );
  585. exit;
  586. end;
  587. end;
  588. curColor := RGBToColor(curColorBGRA.red, curColorBGRA.green, curColorBGRA.blue);
  589. FColorDlg.Color := curColor;
  590. if FColorDlg.Execute then
  591. begin
  592. if curColorBGRA.alpha = 0 then
  593. AssignNewColor( ColorToBGRA(FColorDlg.Color) )
  594. else
  595. AssignNewColor( ColorToBGRA(FColorDlg.Color, curColorBGRA.alpha) );
  596. end;
  597. end;
  598. procedure TVectorialFillInterface.CreateSolidColorInterface;
  599. begin
  600. if FSolidColorInterfaceCreated then exit;
  601. FSolidColorInterfaceCreated := true;
  602. //solid color interface
  603. FShapeSolidColor := TShape.Create(FToolbar);
  604. FShapeSolidColor.Width := FToolbar.ButtonWidth;
  605. FShapeSolidColor.Height := FToolbar.ButtonHeight;
  606. FShapeSolidColor.OnMouseUp:= @ShapeSolidColorMouseUp;
  607. FShapeSolidColor.Hint := rsColor;
  608. AddToolbarControl(FToolbar, FShapeSolidColor);
  609. FUpDownSolidAlpha := TBCTrackbarUpdown.Create(FToolbar);
  610. FUpDownSolidAlpha.Width := FToolbar.ButtonWidth*2;
  611. FUpDownSolidAlpha.Height := FToolbar.ButtonHeight;
  612. FUpDownSolidAlpha.MinValue := 0;
  613. FUpDownSolidAlpha.MaxValue := 255;
  614. FUpDownSolidAlpha.Increment:= 15;
  615. FUpDownSolidAlpha.OnChange:=@UpDownSolidAlphaChange;
  616. FUpDownSolidAlpha.Hint := rsOpacity;
  617. FUpDownSolidAlpha.Enabled:= FAllowKeyInput;
  618. AddToolbarControl(FToolbar, FUpDownSolidAlpha);
  619. AttachMouseEvent(FUpDownSolidAlpha);
  620. end;
  621. procedure TVectorialFillInterface.CreateGradientInterface;
  622. var
  623. gr: TBGRAGradientRepetition;
  624. ci: TBGRAColorInterpolation;
  625. item: TMenuItem;
  626. begin
  627. if FGradientInterfaceCreated then exit;
  628. FGradientInterfaceCreated := true;
  629. FButtonGradRepetition := AddToolbarButton(FToolbar, rsGradientRepetition+'...', 7+ord(FGradRepetition), @ButtonGradRepetitionClick);
  630. AttachMouseEvent(FButtonGradRepetition);
  631. FButtonGradInterp := AddToolbarButton(FToolbar, rsColorInterpolation+'...', 11+ord(FGradInterp), @ButtonGradInterpClick);
  632. AttachMouseEvent(FButtonGradInterp);
  633. { FShapeStartColor := TShape.Create(FToolbar);
  634. FShapeStartColor.Width := FToolbar.ButtonWidth*3 div 4;
  635. FShapeStartColor.Height := FToolbar.ButtonHeight;
  636. FShapeStartColor.OnMouseUp:=@ShapeStartColorMouseUp;
  637. FShapeStartColor.Hint := 'Start color';
  638. AddToolbarControl(FToolbar, FShapeStartColor);}
  639. FUpDownStartAlpha := TBCTrackbarUpdown.Create(FToolbar);
  640. FUpDownStartAlpha.Width := FToolbar.ButtonWidth*2;
  641. FUpDownStartAlpha.Height := FToolbar.ButtonHeight;
  642. FUpDownStartAlpha.MinValue := 0;
  643. FUpDownStartAlpha.MaxValue := 255;
  644. FUpDownStartAlpha.Increment:= 15;
  645. FUpDownStartAlpha.OnChange:=@UpDownStartAlphaChange;
  646. FUpDownStartAlpha.Hint := rsStartOpacity;
  647. FUpDownStartAlpha.Enabled:= FAllowKeyInput;
  648. AddToolbarControl(FToolbar, FUpDownStartAlpha);
  649. AttachMouseEvent(FUpDownStartAlpha);
  650. FButtonSwapColor := AddToolbarButton(FToolbar, rsSwapColors, 23, @ButtonSwapColorClick);
  651. AttachMouseEvent(FButtonSwapColor);
  652. { FShapeEndColor := TShape.Create(FToolbar);
  653. FShapeEndColor.Width := FToolbar.ButtonWidth*3 div 4;
  654. FShapeEndColor.Height := FToolbar.ButtonHeight;
  655. FShapeEndColor.OnMouseUp:=@ShapeEndColorMouseUp;
  656. FShapeEndColor.Hint := 'End color';
  657. AddToolbarControl(FToolbar, FShapeEndColor);}
  658. FUpDownEndAlpha := TBCTrackbarUpdown.Create(FToolbar);
  659. FUpDownEndAlpha.Width := FToolbar.ButtonWidth*2;
  660. FUpDownEndAlpha.Height := FToolbar.ButtonHeight;
  661. FUpDownEndAlpha.MinValue := 0;
  662. FUpDownEndAlpha.MaxValue := 255;
  663. FUpDownEndAlpha.Increment:= 15;
  664. FUpDownEndAlpha.OnChange:=@UpDownEndAlphaChange;
  665. FUpDownEndAlpha.Hint := rsEndOpacity;
  666. FUpDownEndAlpha.Enabled:= FAllowKeyInput;
  667. AddToolbarControl(FToolbar, FUpDownEndAlpha);
  668. AttachMouseEvent(FUpDownEndAlpha);
  669. FGradRepetitionMenu := TPopupMenu.Create(self);
  670. FGradRepetitionMenu.Images := FImageList;
  671. for gr := low(TBGRAGradientRepetition) to high(TBGRAGradientRepetition) do
  672. begin
  673. item := TMenuItem.Create(FGradRepetitionMenu); item.Caption := GradRepetitionToStr(gr);
  674. item.OnClick:=@OnClickGradRepeat; item.Tag := ord(gr);
  675. item.ImageIndex:= 7+ord(gr);
  676. FGradRepetitionMenu.Items.Add(item);
  677. end;
  678. FGradInterpMenu := TPopupMenu.Create(self);
  679. FGradInterpMenu.Images := FImageList;
  680. for ci := low(TBGRAColorInterpolation) to high(TBGRAColorInterpolation) do
  681. begin
  682. item := TMenuItem.Create(FGradInterpMenu); item.Caption := ColorInterpToStr(ci);
  683. item.OnClick:=@OnClickGradInterp; item.Tag := ord(ci);
  684. item.ImageIndex:= 11+ord(ci);
  685. FGradInterpMenu.Items.Add(item);
  686. end;
  687. end;
  688. procedure TVectorialFillInterface.CreateTextureInterface;
  689. var
  690. tr: TTextureRepetition;
  691. item: TMenuItem;
  692. begin
  693. if FTextureInterfaceCreated then exit;
  694. FTextureInterfaceCreated := true;
  695. FButtonTexRepeat := AddToolbarButton(FToolbar, rsTextureRepetition+'...', -1, @ButtonTexRepeatClick);
  696. AttachMouseEvent(FButtonTexRepeat);
  697. FUpDownTexAlpha := TBCTrackbarUpdown.Create(FToolbar);
  698. FUpDownTexAlpha.Width := FToolbar.ButtonWidth*2;
  699. FUpDownTexAlpha.Height := FToolbar.ButtonHeight;
  700. FUpDownTexAlpha.MinValue := 0;
  701. FUpDownTexAlpha.MaxValue := 255;
  702. FUpDownTexAlpha.Increment:= 15;
  703. FUpDownTexAlpha.OnChange:=@UpDownTexAlphaChange;
  704. FUpDownTexAlpha.Hint := rsOpacity;
  705. FUpDownTexAlpha.Enabled:= FAllowKeyInput;
  706. AddToolbarControl(FToolbar, FUpDownTexAlpha);
  707. AttachMouseEvent(FUpDownTexAlpha);
  708. FButtonLoadTexture := AddToolbarButton(FToolbar, rsLoadTexture+'...', 22, @ButtonLoadTextureClick);
  709. AttachMouseEvent(FButtonLoadTexture);
  710. FTextureAverageColorComputed := false;
  711. FTexRepetitionMenu := TPopupMenu.Create(self);
  712. FTexRepetitionMenu.Images := FImageList;
  713. for tr := low(TTextureRepetition) to high(TTextureRepetition) do
  714. begin
  715. item := TMenuItem.Create(FTexRepetitionMenu); item.Caption := TextureRepetitionToStr(tr);
  716. item.OnClick:=@OnClickBackTexRepeat; item.Tag := ord(tr);
  717. item.ImageIndex:= 17+ord(tr);
  718. FTexRepetitionMenu.Items.Add(item);
  719. end;
  720. end;
  721. procedure TVectorialFillInterface.HideSolidColorInterface;
  722. begin
  723. if not FSolidColorInterfaceCreated then exit;
  724. FShapeSolidColor.Visible := false;
  725. FUpDownSolidAlpha.Visible := false;
  726. end;
  727. procedure TVectorialFillInterface.HideGradientInterface;
  728. begin
  729. if not FGradientInterfaceCreated then exit;
  730. FButtonGradRepetition.Visible := false;
  731. FButtonGradInterp.Visible := false;
  732. //FShapeStartColor.Visible := false;
  733. FUpDownStartAlpha.Visible := false;
  734. FButtonSwapColor.Visible := false;
  735. //FShapeEndColor.Visible := false;
  736. FUpDownEndAlpha.Visible := false;
  737. end;
  738. procedure TVectorialFillInterface.HideTextureInterface;
  739. begin
  740. if not FTextureInterfaceCreated then exit;
  741. FButtonTexRepeat.Visible := false;
  742. FUpDownTexAlpha.Visible := false;
  743. FButtonLoadTexture.Visible := false;
  744. end;
  745. procedure TVectorialFillInterface.Init(AImageListWidth,
  746. AImageListHeight: Integer);
  747. var
  748. gt: TGradientType;
  749. item: TMenuItem;
  750. begin
  751. FContainer := nil;
  752. FAllowKeyInput:= true;
  753. FAllowedFillTypes := [vftNone, vftSolid, vftGradient, vftTexture];
  754. FFillType:= vftSolid;
  755. FSolidColor:= BGRAWhite;
  756. FGradStartColor:= CSSRed;
  757. FGradEndColor:= CSSYellow;
  758. FGradType:= gtLinear;
  759. FGradRepetition:= grPad;
  760. FGradInterp:= ciLinearRGB;
  761. FTexture:= nil;
  762. FTexRepetition:= trRepeatBoth;
  763. FTexOpacity:= 255;
  764. FCanAdjustToShape:= true;
  765. FVerticalPadding:= 4;
  766. FImageList := TBGRAImageList.Create(self);
  767. FImageListLoaded:= false;
  768. FImageListSize := Size(AImageListWidth,AImageListHeight);
  769. FOpenPictureDlg := TOpenPictureDialog.Create(self);
  770. FColorDlg:= TColorDialog.Create(self);
  771. FOnFillChange:= nil;
  772. FOnTextureChange:= nil;
  773. FToolbar := CreateToolBar(FImageList);
  774. FToolbar.Wrapable := false;
  775. AttachMouseEvent(FToolbar);
  776. FButtonFillNone := AddToolbarCheckButton(FToolbar, rsNoFill, 0, @ButtonFillChange, False, False);
  777. AttachMouseEvent(FButtonFillNone);
  778. FButtonFillSolid := AddToolbarCheckButton(FToolbar, rsSolidColor, 1, @ButtonFillChange, False, False);
  779. AttachMouseEvent(FButtonFillSolid);
  780. FButtonFillGradient := AddToolbarButton(FToolbar, rsGradientFill, 2+ord(FGradType), @ButtonFillGradClick);
  781. AttachMouseEvent(FButtonFillGradient);
  782. FButtonFillTexture := AddToolbarButton(FToolbar, rsTextureFill, 24, @ButtonFillTexClick);
  783. AttachMouseEvent(FButtonFillTexture);
  784. FPreview := TImage.Create(FToolbar);
  785. FPreview.Center:= true;
  786. FPreview.OnMouseUp:=@Preview_MouseUp;
  787. FPreview.Hint := rsPreview;
  788. UpdatePreview;
  789. AddToolbarControl(FToolbar, FPreview);
  790. AttachMouseEvent(FPreview);
  791. FButtonEditGradTexPoints := AddToolbarCheckButton(FToolbar, rsEditGradTexPoints, 25, @EditGradTextPointsClick, false, false);
  792. AttachMouseEvent(FButtonEditGradTexPoints);
  793. FButtonAdjustToShape := AddToolbarButton(FToolbar, rsAdjustToShape, 21, @AdjustToShapeClick);
  794. AttachMouseEvent(FButtonAdjustToShape);
  795. FButtonAdjustToShape.Wrap := true;
  796. UpdateButtonAdjustToShape;
  797. //menu to access gradient interface
  798. FGradTypeMenu := TPopupMenu.Create(self);
  799. FGradTypeMenu.Images := FImageList;
  800. for gt := low(TGradientType) to high(TGradientType) do
  801. begin
  802. item := TMenuItem.Create(FGradTypeMenu); item.Caption := GradientTypeToTranslatedStr(gt);
  803. item.OnClick:=@OnClickBackGradType; item.Tag := ord(gt);
  804. item.ImageIndex:= 2+ord(gt);
  805. FGradTypeMenu.Items.Add(item);
  806. end;
  807. FSolidColorInterfaceCreated := false;
  808. FGradientInterfaceCreated:= false;
  809. FTextureInterfaceCreated:= false;
  810. UpdateAccordingToFillType;
  811. end;
  812. procedure TVectorialFillInterface.AttachMouseEvent(AControl: TToolBar);
  813. begin
  814. AControl.OnMouseMove:=@ToolbarMouseMove;
  815. AControl.OnMouseDown:=@ToolbarMouseDown;
  816. AControl.OnMouseUp:=@ToolbarMouseUp;
  817. AControl.OnMouseEnter:=@ToolbarMouseEnter;
  818. AControl.OnMouseLeave:=@ToolbarMouseLeave;
  819. end;
  820. procedure TVectorialFillInterface.AttachMouseEvent(AControl: TToolButton);
  821. begin
  822. AControl.OnMouseMove:=@AnyButtonMouseMove;
  823. AControl.OnMouseDown:=@AnyButtonMouseDown;
  824. AControl.OnMouseUp:=@AnyButtonMouseUp;
  825. AControl.OnMouseEnter:=@AnyButtonMouseEnter;
  826. AControl.OnMouseLeave:=@AnyButtonMouseLeave;
  827. end;
  828. procedure TVectorialFillInterface.AttachMouseEvent(AControl: TBCTrackbarUpdown);
  829. begin
  830. AControl.OnMouseMove:=@AnyButtonMouseMove;
  831. AControl.OnMouseDown:=@AnyButtonMouseDown;
  832. AControl.OnMouseUp:=@AnyButtonMouseUp;
  833. AControl.OnMouseEnter:=@AnyButtonMouseEnter;
  834. AControl.OnMouseLeave:=@AnyButtonMouseLeave;
  835. end;
  836. procedure TVectorialFillInterface.AttachMouseEvent(AControl: TImage);
  837. begin
  838. AControl.OnMouseMove:=@AnyButtonMouseMove;
  839. AControl.OnMouseEnter:=@AnyButtonMouseEnter;
  840. AControl.OnMouseLeave:=@AnyButtonMouseLeave;
  841. end;
  842. procedure TVectorialFillInterface.SetSolidColor(AValue: TBGRAPixel);
  843. begin
  844. if FSolidColor.EqualsExactly(AValue) then Exit;
  845. FSolidColor:=AValue;
  846. UpdateShapeSolidColor;
  847. If FillType = vftSolid then Changed;
  848. end;
  849. procedure TVectorialFillInterface.ButtonFillChange(Sender: TObject);
  850. begin
  851. if Sender = FButtonFillNone then
  852. begin
  853. FillType:= vftNone;
  854. FButtonFillNone.Down := true;
  855. end
  856. else if Sender = FButtonFillSolid then
  857. begin
  858. FillType:= vftSolid;
  859. FButtonFillSolid.Down := true;
  860. end;
  861. end;
  862. procedure TVectorialFillInterface.SetTextureRepetition(
  863. AValue: TTextureRepetition);
  864. begin
  865. if FTexRepetition=AValue then Exit;
  866. FTexRepetition:=AValue;
  867. UpdateTextureParams;
  868. If FillType = vftTexture then Changed;
  869. end;
  870. procedure TVectorialFillInterface.SetTextureOpacity(AValue: byte);
  871. begin
  872. if FTexOpacity=AValue then Exit;
  873. FTexOpacity:=AValue;
  874. FUpDownTexAlpha.Value := AValue;
  875. If FillType = vftTexture then Changed;
  876. end;
  877. {procedure TVectorialFillInterface.ShapeEndColorMouseUp(Sender: TObject;
  878. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  879. begin
  880. ChooseColor(1, Button);
  881. end;}
  882. procedure TVectorialFillInterface.SetGradientType(AValue: TGradientType);
  883. begin
  884. if FGradType=AValue then Exit;
  885. FGradType:=AValue;
  886. FButtonFillGradient.ImageIndex := 2+ord(GradientType);
  887. if FillType = vftGradient then Changed;
  888. end;
  889. procedure TVectorialFillInterface.SetGradEndColor(AValue: TBGRAPixel);
  890. begin
  891. if FGradEndColor.EqualsExactly(AValue) then Exit;
  892. FGradEndColor:=AValue;
  893. UpdateGradientParams;
  894. if FillType = vftGradient then Changed;
  895. end;
  896. procedure TVectorialFillInterface.SetGradStartColor(AValue: TBGRAPixel);
  897. begin
  898. if FGradStartColor.EqualsExactly(AValue) then Exit;
  899. FGradStartColor:=AValue;
  900. UpdateGradientParams;
  901. if FillType = vftGradient then Changed;
  902. end;
  903. procedure TVectorialFillInterface.SetGradRepetition(AValue: TBGRAGradientRepetition);
  904. begin
  905. if FGradRepetition=AValue then Exit;
  906. FGradRepetition:=AValue;
  907. UpdateGradientParams;
  908. if FillType = vftGradient then Changed;
  909. end;
  910. procedure TVectorialFillInterface.SetGradInterpolation(
  911. AValue: TBGRAColorInterpolation);
  912. begin
  913. if FGradInterp=AValue then Exit;
  914. FGradInterp:=AValue;
  915. UpdateGradientParams;
  916. if FillType = vftGradient then Changed;
  917. end;
  918. procedure TVectorialFillInterface.SetContainer(AValue: TWinControl);
  919. begin
  920. if FContainer=AValue then Exit;
  921. if Assigned(FContainer) then FContainer.RemoveControl(FToolbar);
  922. FContainer:=AValue;
  923. if Assigned(FContainer) then
  924. begin
  925. FContainer.InsertControl(FToolBar);
  926. ContainerSizeChanged;
  927. end;
  928. end;
  929. function TVectorialFillInterface.GetPreferredSize: TSize;
  930. begin
  931. result := GetToolbarSize(FToolbar,0);
  932. end;
  933. procedure TVectorialFillInterface.SetCanAdjustToShape(AValue: boolean);
  934. begin
  935. if FCanAdjustToShape=AValue then Exit;
  936. FCanAdjustToShape:=AValue;
  937. UpdateButtonAdjustToShape;
  938. end;
  939. procedure TVectorialFillInterface.SetImageListSize(AValue: TSize);
  940. begin
  941. if (FImageListSize.cx=AValue.cx) and (FImageListSize.cy=AValue.cy) then Exit;
  942. FImageListSize:=AValue;
  943. if FImageListLoaded then LoadImageList;
  944. end;
  945. procedure TVectorialFillInterface.SetAllowedFillTypes(
  946. AValue: TVectorialFillTypes);
  947. begin
  948. Include(AValue, FFillType); //cannot exclude current type
  949. if FAllowedFillTypes=AValue then Exit;
  950. FAllowedFillTypes:=AValue;
  951. UpdateTopToolbar;
  952. end;
  953. procedure TVectorialFillInterface.SetOnTextureClick(AValue: TNotifyEvent);
  954. begin
  955. if FOnTextureClick=AValue then Exit;
  956. FOnTextureClick:=AValue;
  957. UpdatePreview;
  958. end;
  959. function TVectorialFillInterface.GetAverageColor: TBGRAPixel;
  960. begin
  961. case FillType of
  962. vftNone: result := BGRAPixelTransparent;
  963. vftGradient: result := MergeBGRAWithGammaCorrection(GradStartColor, 1, GradEndColor, 1);
  964. vftTexture: begin
  965. if not FTextureAverageColorComputed then
  966. begin
  967. if Assigned(FTexture) then
  968. FTextureAverageColor := FTexture.AverageColor
  969. else
  970. FTextureAverageColor := BGRAPixelTransparent;
  971. FTextureAverageColorComputed := true;
  972. end;
  973. result := FTextureAverageColor;
  974. end
  975. else {vftSolid} result := SolidColor;
  976. end;
  977. end;
  978. procedure TVectorialFillInterface.ToolbarMouseMove(Sender: TObject;
  979. Shift: TShiftState; X, Y: Integer);
  980. begin
  981. if Assigned(FOnMouseMove) then FOnMouseMove(self, Shift, X+FToolbar.Left,Y+FToolbar.Top);
  982. end;
  983. procedure TVectorialFillInterface.ToolbarMouseUp(Sender: TObject;
  984. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  985. begin
  986. if Assigned(FOnMouseUp) then FOnMouseUp(self, Button, Shift, X+FToolbar.Left,Y+FToolbar.Top);
  987. end;
  988. procedure TVectorialFillInterface.AnyButtonMouseDown(Sender: TObject;
  989. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  990. begin
  991. if Assigned(FOnMouseDown) then FOnMouseDown(self, Button, Shift,
  992. X+FToolbar.Left+TControl(Sender).Left,Y+FToolbar.Top+TControl(Sender).Top);
  993. end;
  994. procedure TVectorialFillInterface.AnyButtonMouseEnter(Sender: TObject);
  995. begin
  996. If Assigned(FOnMouseEnter) then FOnMouseEnter(self);
  997. end;
  998. procedure TVectorialFillInterface.AnyButtonMouseLeave(Sender: TObject);
  999. begin
  1000. If Assigned(FOnMouseLeave) then FOnMouseLeave(self);
  1001. end;
  1002. procedure TVectorialFillInterface.AnyButtonMouseMove(Sender: TObject;
  1003. Shift: TShiftState; X, Y: Integer);
  1004. begin
  1005. if Assigned(FOnMouseMove) then FOnMouseMove(self, Shift,
  1006. X+FToolbar.Left+TControl(Sender).Left,Y+FToolbar.Top+TControl(Sender).Top);
  1007. end;
  1008. procedure TVectorialFillInterface.AnyButtonMouseUp(Sender: TObject;
  1009. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  1010. begin
  1011. if Assigned(FOnMouseUp) then FOnMouseUp(self, Button, Shift,
  1012. X+FToolbar.Left+TControl(Sender).Left,Y+FToolbar.Top+TControl(Sender).Top);
  1013. end;
  1014. procedure TVectorialFillInterface.ToolbarMouseDown(Sender: TObject;
  1015. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  1016. begin
  1017. if Assigned(FOnMouseDown) then FOnMouseDown(self, Button, Shift, X+FToolbar.Left,Y+FToolbar.Top);
  1018. end;
  1019. procedure TVectorialFillInterface.SetVerticalPadding(AValue: integer);
  1020. begin
  1021. if FVerticalPadding=AValue then Exit;
  1022. FVerticalPadding:=AValue;
  1023. if Assigned(FToolbar) and Assigned(FImageList) then
  1024. begin
  1025. FToolbar.ButtonHeight:= FImageList.Height+AValue;
  1026. UpdatePreview;
  1027. end;
  1028. end;
  1029. procedure TVectorialFillInterface.Preview_MouseUp(Sender: TObject;
  1030. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  1031. begin
  1032. case FillType of
  1033. vftSolid: ChooseColor(-1, Button);
  1034. vftGradient: if X < FPreview.Width div 2 then ChooseColor(0, Button) else ChooseColor(1, Button);
  1035. vftTexture: if Assigned(Texture) and Assigned(FOnTextureClick) then
  1036. FOnTextureClick(self);
  1037. end;
  1038. end;
  1039. procedure TVectorialFillInterface.SetAllowKeyInput(AValue: boolean);
  1040. begin
  1041. if FAllowKeyInput=AValue then Exit;
  1042. FAllowKeyInput:=AValue;
  1043. if Assigned(FUpDownStartAlpha) then
  1044. FUpDownStartAlpha.Enabled:= AValue;
  1045. if Assigned(FUpDownEndAlpha) then
  1046. FUpDownEndAlpha.Enabled:= AValue;
  1047. if Assigned(FUpDownSolidAlpha) then
  1048. FUpDownSolidAlpha.Enabled:= AValue;
  1049. if Assigned(FUpDownTexAlpha) then
  1050. FUpDownTexAlpha.Enabled:= AValue;
  1051. end;
  1052. procedure TVectorialFillInterface.EditGradTextPointsClick(Sender: TObject);
  1053. begin
  1054. if Assigned(FOnEditGradTexPoints) then FOnEditGradTexPoints(self);
  1055. end;
  1056. function TVectorialFillInterface.GetEditingGradTexPoints: boolean;
  1057. begin
  1058. if Assigned(FButtonEditGradTexPoints) then
  1059. result := FButtonEditGradTexPoints.Down
  1060. else result := false;
  1061. end;
  1062. procedure TVectorialFillInterface.SetCanEditGradTexPoints(AValue: boolean);
  1063. begin
  1064. if FCanEditGradTexPoints=AValue then Exit;
  1065. FCanEditGradTexPoints:=AValue;
  1066. UpdateButtonAdjustToShape;
  1067. end;
  1068. procedure TVectorialFillInterface.SetEditingGradTexPoints(AValue: boolean);
  1069. begin
  1070. if Assigned(FButtonEditGradTexPoints) then
  1071. FButtonEditGradTexPoints.Down := AValue;
  1072. end;
  1073. procedure TVectorialFillInterface.SetIsTarget(AValue: boolean);
  1074. begin
  1075. if FIsTarget=AValue then Exit;
  1076. FIsTarget:=AValue;
  1077. UpdatePreview;
  1078. end;
  1079. procedure TVectorialFillInterface.ToolbarMouseEnter(Sender: TObject);
  1080. begin
  1081. If Assigned(FOnMouseEnter) then FOnMouseEnter(self);
  1082. end;
  1083. procedure TVectorialFillInterface.ToolbarMouseLeave(Sender: TObject);
  1084. begin
  1085. If Assigned(FOnMouseLeave) then FOnMouseLeave(self);
  1086. end;
  1087. procedure TVectorialFillInterface.AdjustToShapeClick(Sender: TObject);
  1088. begin
  1089. if Assigned(FOnAdjustToShape) then FOnAdjustToShape(self);
  1090. end;
  1091. procedure TVectorialFillInterface.ButtonFillGradClick(Sender: TObject);
  1092. begin
  1093. if Assigned(FGradTypeMenu) then
  1094. with FButtonFillGradient.ClientToScreen(Point(0,FButtonFillGradient.Height)) do
  1095. FGradTypeMenu.PopUp(X,Y);
  1096. FButtonFillGradient.Down := (FillType = vftGradient);
  1097. end;
  1098. procedure TVectorialFillInterface.ButtonFillTexClick(Sender: TObject);
  1099. begin
  1100. if FFillType = vftTexture then
  1101. begin
  1102. FButtonFillTexture.Down := true;
  1103. exit;
  1104. end;
  1105. if Assigned(FTexture) then FillType := vftTexture
  1106. else LoadTexture;
  1107. end;
  1108. procedure TVectorialFillInterface.ButtonGradInterpClick(Sender: TObject);
  1109. begin
  1110. if Assigned(FGradInterpMenu) then
  1111. with FButtonGradInterp.ClientToScreen(Point(0,FButtonGradInterp.Height)) do
  1112. FGradInterpMenu.PopUp(X,Y);
  1113. end;
  1114. procedure TVectorialFillInterface.ButtonGradRepetitionClick(Sender: TObject);
  1115. begin
  1116. if Assigned(FGradRepetitionMenu) then
  1117. with FButtonGradRepetition.ClientToScreen(Point(0,FButtonGradRepetition.Height)) do
  1118. FGradRepetitionMenu.PopUp(X,Y);
  1119. end;
  1120. procedure TVectorialFillInterface.ButtonLoadTextureClick(Sender: TObject);
  1121. begin
  1122. LoadTexture;
  1123. end;
  1124. procedure TVectorialFillInterface.ButtonSwapColorClick(Sender: TObject);
  1125. var
  1126. temp: TBGRAPixel;
  1127. begin
  1128. temp := GradStartColor;
  1129. FGradStartColor := GradEndColor;
  1130. FGradEndColor := temp;
  1131. UpdateGradientParams;
  1132. if FillType = vftGradient then Changed;
  1133. end;
  1134. procedure TVectorialFillInterface.ButtonTexRepeatClick(Sender: TObject);
  1135. begin
  1136. if Assigned(FTexRepetitionMenu) then
  1137. with FButtonTexRepeat.ClientToScreen(Point(0,FButtonTexRepeat.Height)) do
  1138. FTexRepetitionMenu.PopUp(X,Y);
  1139. end;
  1140. constructor TVectorialFillInterface.Create(AOwner: TComponent);
  1141. begin
  1142. inherited Create(AOwner);
  1143. Init(16,16);
  1144. end;
  1145. constructor TVectorialFillInterface.Create(AOwner: TComponent; AImageListWidth,
  1146. AImageListHeight: Integer);
  1147. begin
  1148. inherited Create(AOwner);
  1149. Init(AImageListWidth,AImageListHeight);
  1150. end;
  1151. destructor TVectorialFillInterface.Destroy;
  1152. begin
  1153. FTexture.FreeReference;
  1154. if Assigned(FContainer) then
  1155. begin
  1156. FContainer.RemoveControl(FToolbar);
  1157. FContainer := nil;
  1158. end;
  1159. FToolbar.Free;
  1160. inherited Destroy;
  1161. end;
  1162. function TVectorialFillInterface.GetTextureThumbnail(AWidth, AHeight: integer; ABackColor: TColor): TBitmap;
  1163. var
  1164. thumb: TBGRABitmap;
  1165. begin
  1166. if FTexture = nil then exit(nil);
  1167. thumb := GetBitmapThumbnail(FTexture, AWidth,AHeight,BGRAPixelTransparent,true);
  1168. try
  1169. result := thumb.MakeBitmapCopy(ABackColor);
  1170. finally
  1171. thumb.Free;
  1172. end;
  1173. end;
  1174. procedure TVectorialFillInterface.AssignFill(AFill: TVectorialFill);
  1175. begin
  1176. FillType := AFill.FillType;
  1177. case FillType of
  1178. vftTexture:
  1179. begin
  1180. Texture := AFill.Texture;
  1181. TextureOpacity:= AFill.TextureOpacity;
  1182. TextureRepetition:= AFill.TextureRepetition;
  1183. end;
  1184. vftSolid: SolidColor := AFill.SolidColor;
  1185. vftGradient:
  1186. begin
  1187. GradStartColor := AFill.Gradient.StartColor;
  1188. GradEndColor := AFill.Gradient.EndColor;
  1189. GradientType:= AFill.Gradient.GradientType;
  1190. GradRepetition:= AFill.Gradient.Repetition;
  1191. GradInterpolation := AFill.Gradient.ColorInterpolation;
  1192. end;
  1193. end;
  1194. end;
  1195. procedure TVectorialFillInterface.UpdateFillExceptGeometry(ATargetFill: TVectorialFill);
  1196. var
  1197. f: TVectorialFill;
  1198. begin
  1199. f := CreateShapeFill(nil);
  1200. if Assigned(ATargetFill) then
  1201. ATargetFill.AssignExceptGeometry(f);
  1202. f.Free;
  1203. end;
  1204. function TVectorialFillInterface.CreateShapeFill(AShape: TVectorShape): TVectorialFill;
  1205. var
  1206. grad: TBGRALayerGradientOriginal;
  1207. begin
  1208. if FillType = vftSolid then
  1209. exit(TVectorialFill.CreateAsSolid(SolidColor))
  1210. else if (FillType = vftTexture) and Assigned(Texture) then
  1211. result := TVectorialFill.CreateAsTexture(Texture, AffineMatrixIdentity,
  1212. TextureOpacity, TextureRepetition)
  1213. else if FillType = vftGradient then
  1214. begin
  1215. grad := TBGRALayerGradientOriginal.Create;
  1216. grad.StartColor := GradStartColor;
  1217. grad.EndColor := GradEndColor;
  1218. grad.GradientType:= GradientType;
  1219. grad.Repetition := GradRepetition;
  1220. grad.ColorInterpolation:= GradInterpolation;
  1221. result := TVectorialFill.CreateAsGradient(grad, true);
  1222. end
  1223. else exit(nil); //none
  1224. if Assigned(AShape) then
  1225. result.FitGeometry(AShape.SuggestGradientBox(AffineMatrixIdentity));
  1226. end;
  1227. procedure TVectorialFillInterface.UpdateShapeFill(AShape: TVectorShape;
  1228. ATarget: TLCFillTarget);
  1229. var
  1230. vectorFill: TVectorialFill;
  1231. curFill: TVectorialFill;
  1232. begin
  1233. case ATarget of
  1234. ftPen: curFill:= AShape.PenFill;
  1235. ftBack: curFill := AShape.BackFill;
  1236. ftOutline: curFill := AShape.OutlineFill;
  1237. else exit;
  1238. end;
  1239. if (FillType = vftTexture) and (TextureOpacity = 0) then
  1240. vectorFill := nil else
  1241. if (FillType = vftTexture) and (curFill.FillType = vftTexture) then
  1242. begin
  1243. vectorFill := TVectorialFill.CreateAsTexture(Texture, curFill.TextureMatrix,
  1244. TextureOpacity, TextureRepetition);
  1245. end
  1246. else if (FillType = vftGradient) and (curFill.FillType = vftGradient) then
  1247. begin
  1248. vectorFill := curFill.Duplicate;
  1249. vectorFill.Gradient.StartColor := GradStartColor;
  1250. vectorFill.Gradient.EndColor := GradEndColor;
  1251. vectorFill.Gradient.GradientType := GradientType;
  1252. vectorFill.Gradient.Repetition := GradRepetition;
  1253. vectorFill.Gradient.ColorInterpolation:= GradInterpolation;
  1254. end else
  1255. vectorFill := CreateShapeFill(AShape);
  1256. case ATarget of
  1257. ftPen: AShape.PenFill:= vectorFill;
  1258. ftBack: AShape.BackFill:= vectorFill;
  1259. ftOutline: AShape.OutlineFill:= vectorFill;
  1260. end;
  1261. vectorFill.Free;
  1262. end;
  1263. begin
  1264. {$i ../resources/fillimages.lrs}
  1265. end.