utool.pas 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. unit UTool;
  3. {$mode objfpc}{$H+}
  4. interface
  5. uses
  6. Classes, Types, SysUtils, Graphics, BGRABitmap, BGRABitmapTypes, uimage, UImageType,
  7. ULayerAction, LCLType, Controls, UBrushType, UConfig, LCVectorPolyShapes,
  8. BGRAGradientScanner, BGRALayerOriginal, LCVectorRectShapes, UScripting,
  9. LCVectorialFill, BGRAGradientOriginal;
  10. const
  11. VK_SNAP = {$IFDEF DARWIN}VK_LWIN{$ELSE}VK_CONTROL{$ENDIF};
  12. VK_SNAP2 = {$IFDEF DARWIN}VK_RWIN{$ELSE}VK_CONTROL{$ENDIF};
  13. ssSnap = {$IFDEF DARWIN}ssMeta{$ELSE}ssCtrl{$ENDIF};
  14. type TPaintToolType = (ptHand,ptHotSpot, ptMoveLayer,ptRotateLayer,ptZoomLayer,
  15. ptPen, ptBrush, ptClone, ptColorPicker, ptEraser,
  16. ptEditShape, ptRect, ptEllipse, ptPolygon, ptSpline, ptPolyline, ptOpenedCurve,
  17. ptFloodFill, ptGradient, ptPhong,
  18. ptSelectPen, ptSelectRect, ptSelectEllipse, ptSelectPoly, ptSelectSpline,
  19. ptMoveSelection, ptRotateSelection, ptMagicWand, ptDeformation, ptTextureMapping, ptLayerMapping,
  20. ptText);
  21. const
  22. PaintToolTypeStr : array[TPaintToolType] of string = ('Hand','HotSpot', 'MoveLayer','RotateLayer','ZoomLayer',
  23. 'Pen', 'Brush', 'Clone', 'ColorPicker', 'Eraser',
  24. 'EditShape', 'Rect', 'Ellipse', 'Polygon', 'Spline', 'Polyline', 'OpenedCurve',
  25. 'FloodFill', 'Gradient', 'Phong',
  26. 'SelectPen', 'SelectRect', 'SelectEllipse', 'SelectPoly', 'SelectSpline',
  27. 'MoveSelection', 'RotateSelection', 'MagicWand', 'Deformation', 'TextureMapping', 'LayerMapping',
  28. 'Text');
  29. function StrToPaintToolType(const s: ansistring): TPaintToolType;
  30. type
  31. TContextualToolbar = (ctPenFill, ctPenWidth, ctPenStyle, ctJoinStyle, ctLineCap,
  32. ctCloseShape, ctSplineStyle, ctShape, ctRatio, ctBackFill,
  33. ctBrush, ctEraserOption, ctAliasing, ctTolerance, ctDeformation, ctPerspective,
  34. ctText, ctOutlineWidth, ctOutlineFill, ctTextShadow, ctPhong, ctAltitude);
  35. TContextualToolbars = set of TContextualToolbar;
  36. type
  37. TToolManager = class;
  38. TBitmapToVirtualScreenFunction = function(PtF: TPointF): TPointF of object;
  39. TEraserMode = (emEraseAlpha, emSharpen, emSoften, emLighten, emDarken);
  40. TToolCommand = (tcCut, tcCopy, tcPaste, tcDelete, tcFinish, tcMoveUp, tcMoveDown, tcMoveToFront, tcMoveToBack,
  41. tcAlignLeft, tcCenterHorizontally, tcAlignRight, tcAlignTop, tcCenterVertically, tcAlignBottom,
  42. tcShapeToSpline, tcForeAdjustToShape, tcBackAdjustToShape, tcOutlineAdjustToShape,
  43. tcForeEditGradTexPoints, tcBackEditGradTexPoints, tcOutlineEditGradTexPoints);
  44. TDeformationGridMode = (gmDeform, gmMovePointWithoutDeformation);
  45. const
  46. MaxPenWidth = 999.9;
  47. MinPenWidth = 1;
  48. MaxArrowSize = 9.9;
  49. MinArrowSize = 1;
  50. MaxBrushSpacing = 99;
  51. MinPhongShapeAltitude = 1;
  52. MaxPhongShapeAltitude = 100;
  53. MinPhongBorderSize = 1;
  54. MaxPhongBorderSize = 100;
  55. MinDeformationGridSize = 3;
  56. function GradientInterpolationToDisplay(AValue: TBGRAColorInterpolation): string;
  57. function DisplayToGradientInterpolation(AValue: string): TBGRAColorInterpolation;
  58. type
  59. TLayerKind = (lkUnknown, lkEmpty, lkBitmap, lkTransformedBitmap, lkGradient, lkVectorial, lkSVG, lkOther);
  60. { TGenericTool }
  61. TGenericTool = class
  62. private
  63. FShiftState: TShiftState;
  64. FAction: TLayerAction;
  65. FForeFill, FBackFill: TVectorialFill;
  66. FBackFillScan, FForeFillScan: TBGRACustomScanner;
  67. function GetUniversalBrush(ASource: TVectorialFill; var ADest: TVectorialFill; var AScan: TBGRACustomScanner): TUniversalBrush;
  68. protected
  69. FManager: TToolManager;
  70. FLastToolDrawingLayer: TBGRABitmap;
  71. FValidating, FCanceling: boolean;
  72. function GetAction: TLayerAction; virtual;
  73. function GetIdleAction: TLayerAction; virtual;
  74. function GetLayerOffset: TPoint; virtual;
  75. function GetIsSelectingTool: boolean; virtual; abstract;
  76. function GetIsEditingText: boolean; virtual;
  77. function FixSelectionTransform: boolean; virtual;
  78. function FixLayerOffset: boolean; virtual;
  79. function DoToolDown(toolDest: TBGRABitmap; pt: TPoint; ptF: TPointF; rightBtn: boolean): TRect; virtual;
  80. function DoToolMove(toolDest: TBGRABitmap; pt: TPoint; ptF: TPointF): TRect; virtual;
  81. function DoToolKeyDown(var key: Word): TRect; virtual;
  82. function DoToolKeyUp(var key: Word): TRect; virtual;
  83. function DoToolUpdate({%H-}toolDest: TBGRABitmap): TRect; virtual;
  84. procedure OnTryStop({%H-}sender: TCustomLayerAction); virtual;
  85. function SelectionMaxPointDistance: single;
  86. function GetStatusText: string; virtual;
  87. function DoGetToolDrawingLayer: TBGRABitmap; virtual;
  88. function GetCurrentLayerKind: TLayerKind;
  89. function GetIsForeEditGradTexPoints: boolean; virtual;
  90. function GetIsBackEditGradTexPoints: boolean; virtual;
  91. function GetIsOutlineEditGradTexPoints: boolean; virtual;
  92. function GetAllowedBackFillTypes: TVectorialFillTypes; virtual;
  93. function GetAllowedForeFillTypes: TVectorialFillTypes; virtual;
  94. function GetAllowedOutlineFillTypes: TVectorialFillTypes; virtual;
  95. property ShiftState: TShiftState read FShiftState;
  96. public
  97. ToolUpdateNeeded: boolean;
  98. Cursor: TCursor;
  99. constructor Create(AManager: TToolManager); virtual;
  100. destructor Destroy; override;
  101. function GetForeUniversalBrush: TUniversalBrush;
  102. function GetBackUniversalBrush: TUniversalBrush;
  103. procedure ReleaseUniversalBrushes; virtual;
  104. procedure ValidateAction;
  105. procedure ValidateActionPartially;
  106. procedure CancelAction;
  107. procedure CancelActionPartially;
  108. function HasPen: boolean; virtual;
  109. function ToolUpdate: TRect;
  110. function ToolDown(X,Y: single; rightBtn: boolean): TRect;
  111. function ToolMove(X,Y: single): TRect;
  112. function ToolKeyDown(var key: Word): TRect;
  113. function ToolKeyUp(var key: Word): TRect;
  114. function ToolKeyPress(var key: TUTF8Char): TRect; virtual;
  115. function ToolUp: TRect; virtual;
  116. function ToolCommand({%H-}ACommand: TToolCommand): boolean; virtual;
  117. function ToolProvideCommand({%H-}ACommand: TToolCommand): boolean; virtual;
  118. function SuggestGradientBox: TAffineBox; virtual;
  119. function GetContextualToolbars: TContextualToolbars; virtual;
  120. function GetToolDrawingLayer: TBGRABitmap;
  121. procedure RestoreBackupDrawingLayer;
  122. function GetBackupLayerIfExists: TBGRABitmap;
  123. function Render(VirtualScreen: TBGRABitmap; VirtualScreenWidth, VirtualScreenHeight: integer; BitmapToVirtualScreen: TBitmapToVirtualScreenFunction): TRect; virtual;
  124. property Manager : TToolManager read FManager;
  125. property IsSelectingTool: boolean read GetIsSelectingTool;
  126. property IsEditingText: boolean read GetIsEditingText;
  127. property Action : TLayerAction read GetAction;
  128. property LayerOffset : TPoint read GetLayerOffset;
  129. property LastToolDrawingLayer: TBGRABitmap read FLastToolDrawingLayer;
  130. property StatusText: string read GetStatusText;
  131. property Validating: boolean read FValidating;
  132. property Canceling: boolean read FCanceling;
  133. property ForeUniversalBrush: TUniversalBrush read GetForeUniversalBrush;
  134. property IsForeEditGradTexPoints: boolean read GetIsForeEditGradTexPoints;
  135. property IsBackEditGradTexPoints: boolean read GetIsBackEditGradTexPoints;
  136. property IsOutlineEditGradTexPoints: boolean read GetIsOutlineEditGradTexPoints;
  137. property AllowedForeFillTypes: TVectorialFillTypes read GetAllowedForeFillTypes;
  138. property AllowedBackFillTypes: TVectorialFillTypes read GetAllowedBackFillTypes;
  139. property AllowedOutlineFillTypes: TVectorialFillTypes read GetAllowedOutlineFillTypes;
  140. end;
  141. { TReadonlyTool }
  142. TReadonlyTool = class(TGenericTool)
  143. protected
  144. function GetAction: TLayerAction; override;
  145. function GetIsSelectingTool: boolean; override;
  146. function DoGetToolDrawingLayer: TBGRABitmap; override;
  147. end;
  148. TToolClass = class of TGenericTool;
  149. TToolPopupMessage= (tpmNone, tpmHoldKeyForSquare, tpmHoldKeySnapToPixel,
  150. tpmReturnValides, tpmBackspaceRemoveLastPoint, tpmRightClickFinishShape, tpmHoldKeyRestrictRotation,
  151. tpmHoldKeysScaleMode, tpmCurveModeHint, tpmBlendOpBackground,
  152. tpmRightClickForSource, tpmNothingToBeDeformed, tpmRightClickForTransformCenter,
  153. tpmLayerEmpty, tpmOpacity0);
  154. TOnToolChangedHandler = procedure(sender: TToolManager; ANewToolType: TPaintToolType) of object;
  155. TOnPopupToolHandler = procedure(sender: TToolManager; APopupMessage: TToolPopupMessage; AKey: Word; AAlways: boolean) of object;
  156. TOnQueryColorTargetHandler = procedure(sender: TToolManager; ATarget: TVectorialFill) of object;
  157. TShapeOption = (toAliasing, toDrawShape, toFillShape, toCloseShape);
  158. TShapeOptions = set of TShapeOption;
  159. TFloodFillOption = (ffProgressive, ffFillAll);
  160. TFloodFillOptions = set of TFloodFillOption;
  161. TPerspectiveOption = (poRepeat, poTwoPlanes);
  162. TPerspectiveOptions = set of TPerspectiveOption;
  163. { TToolManager }
  164. TToolManager = class
  165. private
  166. FConfigProvider: IConfigProvider;
  167. FOnQueryColorTarget: TOnQueryColorTargetHandler;
  168. FShouldExitTool: boolean;
  169. FSwitchAfterExitTool: TPaintToolType;
  170. FImage: TLazPaintImage;
  171. FBlackAndWhite: boolean;
  172. FScriptContext: TScriptContext;
  173. FToolPressure: single;
  174. FInTool, FInToolUpdate, FInSwapFill: boolean;
  175. FCurrentTool : TGenericTool;
  176. FCurrentToolType : TPaintToolType;
  177. FToolCurrentCursorPos: TPointF;
  178. FSleepingTool: TGenericTool;
  179. FSleepingToolType: TPaintToolType;
  180. FOnToolChangedHandler: TOnToolChangedHandler;
  181. FOnToolRenderChanged: TNotifyEvent;
  182. FOnToolbarChanged: TNotifyEvent;
  183. FOnPopupToolHandler: TOnPopupToolHandler;
  184. FForeFill, FBackFill, FOutlineFill: TVectorialFill;
  185. FForeLastGradient, FBackLastGradient, FOutlineLastGradient: TBGRALayerGradientOriginal;
  186. FEraserMode: TEraserMode;
  187. FEraserAlpha: byte;
  188. FBrushInfoList: TList;
  189. FBrushInfoListChanged: boolean;
  190. FBrushIndex: integer;
  191. FBrushSpacing: integer;
  192. FPenStyle: TPenStyle;
  193. FJoinStyle: TPenJoinStyle;
  194. FNormalPenWidth, FEraserWidth: Single;
  195. FShapeOptions: TShapeOptions;
  196. FTextFontName: string;
  197. FTextFontSize: single;
  198. FTextFontStyle: TFontStyles;
  199. FTextAlign: TAlignment;
  200. FTextVerticalAlign: TTextLayout;
  201. FTextBidiMode: TFontBidiMode;
  202. FTextOutline: boolean;
  203. FTextOutlineWidth: single;
  204. FTextPhong: boolean;
  205. FLightPosition: TPointF;
  206. FLightAltitude: integer;
  207. FTextShadow: boolean;
  208. FTextShadowBlurRadius: single;
  209. FTextShadowOffset: TPoint;
  210. FLineCap: TPenEndCap;
  211. FArrowStart,FArrowEnd: TArrowKind;
  212. FArrowSize: TPointF;
  213. FSplineStyle: TSplineStyle;
  214. FPhongShapeAltitude: integer;
  215. FPhongShapeBorderSize: integer;
  216. FPhongShapeKind: TPhongShapeKind;
  217. FDeformationGridNbX,FDeformationGridNbY: integer;
  218. FDeformationGridMode: TDeformationGridMode;
  219. FTolerance: byte;
  220. FFloodFillOptions: TFloodFillOptions;
  221. FPerspectiveOptions: TPerspectiveOptions;
  222. FShapeRatio: Single;
  223. FOnFillChanged: TNotifyEvent;
  224. FOnEraserChanged: TNotifyEvent;
  225. FOnJoinStyleChanged: TNotifyEvent;
  226. FOnLineCapChanged: TNotifyEvent;
  227. FOnPenStyleChanged: TNotifyEvent;
  228. FOnPenWidthChanged: TNotifyEvent;
  229. FOnBrushChanged, FOnBrushListChanged: TNotifyEvent;
  230. FOnPhongShapeChanged: TNotifyEvent;
  231. FOnSplineStyleChanged: TNotifyEvent;
  232. FOnTextFontChanged, FOnTextAlignChanged: TNotifyEvent;
  233. FOnTextOutlineChanged: TNotifyEvent;
  234. FOnTextPhongChanged, FOnLightChanged: TNotifyEvent;
  235. FOnTextShadowChanged: TNotifyEvent;
  236. FOnShapeOptionChanged, FOnShapeRatioChanged: TNotifyEvent;
  237. FOnDeformationGridChanged: TNotifyEvent;
  238. FOnToleranceChanged: TNotifyEvent;
  239. FOnFloodFillOptionChanged: TNotifyEvent;
  240. FOnPerspectiveOptionChanged: TNotifyEvent;
  241. procedure FillChange(ASender: TObject;
  242. var {%H-}ADiff: TCustomVectorialFillDiff);
  243. function GetAllowedBackFillTypes: TVectorialFillTypes;
  244. function GetAllowedForeFillTypes: TVectorialFillTypes;
  245. function GetAllowedOutlineFillTypes: TVectorialFillTypes;
  246. function GetCursor: TCursor;
  247. function GetBackColor: TBGRAPixel;
  248. function GetBrushAt(AIndex: integer): TLazPaintBrush;
  249. function GetBrushCount: integer;
  250. function GetBrushInfo: TLazPaintBrush;
  251. function GetForeColor: TBGRAPixel;
  252. function GetIsEditingText: boolean;
  253. function GetMaxDeformationGridSize: TSize;
  254. function GetOutlineColor: TBGRAPixel;
  255. function GetShapeOptionAliasing: boolean;
  256. function GetPenWidth: single;
  257. function GetToolSleeping: boolean;
  258. function GetTextFontName: string;
  259. function GetTextFontSize: single;
  260. function GetTextFontStyle: TFontStyles;
  261. function ScriptGetAliasing(AVars: TVariableSet): TScriptResult;
  262. function ScriptGetArrowEnd(AVars: TVariableSet): TScriptResult;
  263. function ScriptGetArrowSize(AVars: TVariableSet): TScriptResult;
  264. function ScriptGetArrowStart(AVars: TVariableSet): TScriptResult;
  265. function ScriptGetBackColor(AVars: TVariableSet): TScriptResult;
  266. function ScriptGetOutlineColor(AVars: TVariableSet): TScriptResult;
  267. function ScriptGetBrushCount(AVars: TVariableSet): TScriptResult;
  268. function ScriptGetBrushIndex(AVars: TVariableSet): TScriptResult;
  269. function ScriptGetBrushSpacing(AVars: TVariableSet): TScriptResult;
  270. function ScriptGetDeformationGridMode(AVars: TVariableSet): TScriptResult;
  271. function ScriptGetDeformationGridSize(AVars: TVariableSet): TScriptResult;
  272. function ScriptGetEraserAlpha(AVars: TVariableSet): TScriptResult;
  273. function ScriptGetEraserMode(AVars: TVariableSet): TScriptResult;
  274. function ScriptGetFloodFillOptions(AVars: TVariableSet): TScriptResult;
  275. function ScriptGetFontName(AVars: TVariableSet): TScriptResult;
  276. function ScriptGetFontSize(AVars: TVariableSet): TScriptResult;
  277. function ScriptGetFontStyle(AVars: TVariableSet): TScriptResult;
  278. function ScriptGetGradientInterpolation(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  279. function ScriptGetGradientRepetition(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  280. function ScriptGetGradientType(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  281. function ScriptGetGradientColors(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  282. function ScriptGetBackGradientInterpolation(AVars: TVariableSet): TScriptResult;
  283. function ScriptGetBackGradientRepetition(AVars: TVariableSet): TScriptResult;
  284. function ScriptGetBackGradientType(AVars: TVariableSet): TScriptResult;
  285. function ScriptGetBackGradientColors(AVars: TVariableSet): TScriptResult;
  286. function ScriptGetForeGradientInterpolation(AVars: TVariableSet): TScriptResult;
  287. function ScriptGetForeGradientRepetition(AVars: TVariableSet): TScriptResult;
  288. function ScriptGetForeGradientType(AVars: TVariableSet): TScriptResult;
  289. function ScriptGetForeGradientColors(AVars: TVariableSet): TScriptResult;
  290. function ScriptGetOutlineGradientInterpolation(AVars: TVariableSet): TScriptResult;
  291. function ScriptGetOutlineGradientRepetition(AVars: TVariableSet): TScriptResult;
  292. function ScriptGetOutlineGradientType(AVars: TVariableSet): TScriptResult;
  293. function ScriptGetOutlineGradientColors(AVars: TVariableSet): TScriptResult;
  294. function ScriptGetTextureRepetition(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  295. function ScriptGetTextureOpacity(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  296. function ScriptGetBackTextureRepetition(AVars: TVariableSet): TScriptResult;
  297. function ScriptGetBackTextureOpacity(AVars: TVariableSet): TScriptResult;
  298. function ScriptGetForeTextureRepetition(AVars: TVariableSet): TScriptResult;
  299. function ScriptGetForeTextureOpacity(AVars: TVariableSet): TScriptResult;
  300. function ScriptGetOutlineTextureRepetition(AVars: TVariableSet): TScriptResult;
  301. function ScriptGetOutlineTextureOpacity(AVars: TVariableSet): TScriptResult;
  302. function ScriptGetJoinStyle(AVars: TVariableSet): TScriptResult;
  303. function ScriptGetLightPosition(AVars: TVariableSet): TScriptResult;
  304. function ScriptGetLineCap(AVars: TVariableSet): TScriptResult;
  305. function ScriptGetForeColor(AVars: TVariableSet): TScriptResult;
  306. function ScriptGetPenStyle(AVars: TVariableSet): TScriptResult;
  307. function ScriptGetPenWidth(AVars: TVariableSet): TScriptResult;
  308. function ScriptGetPerspectiveOptions(AVars: TVariableSet): TScriptResult;
  309. function ScriptGetPhongShapeAltitude(AVars: TVariableSet): TScriptResult;
  310. function ScriptGetPhongShapeBorderSize(AVars: TVariableSet): TScriptResult;
  311. function ScriptGetPhongShapeKind(AVars: TVariableSet): TScriptResult;
  312. function ScriptGetShapeOptions(AVars: TVariableSet): TScriptResult;
  313. function ScriptGetShapeRatio(AVars: TVariableSet): TScriptResult;
  314. function ScriptGetSplineStyle(AVars: TVariableSet): TScriptResult;
  315. function ScriptGetTextAlign(AVars: TVariableSet): TScriptResult;
  316. function ScriptGetTextVerticalAlign(AVars: TVariableSet): TScriptResult;
  317. function ScriptGetTextBidiMode(AVars: TVariableSet): TScriptResult;
  318. function ScriptGetTextOutline(AVars: TVariableSet): TScriptResult;
  319. function ScriptGetTextPhong(AVars: TVariableSet): TScriptResult;
  320. function ScriptGetTolerance(AVars: TVariableSet): TScriptResult;
  321. function ScriptSetAliasing(AVars: TVariableSet): TScriptResult;
  322. function ScriptSetArrowEnd(AVars: TVariableSet): TScriptResult;
  323. function ScriptSetArrowSize(AVars: TVariableSet): TScriptResult;
  324. function ScriptSetArrowStart(AVars: TVariableSet): TScriptResult;
  325. function ScriptSetBackColor(AVars: TVariableSet): TScriptResult;
  326. function ScriptSetOutlineColor(AVars: TVariableSet): TScriptResult;
  327. function ScriptSetBrushIndex(AVars: TVariableSet): TScriptResult;
  328. function ScriptSetBrushSpacing(AVars: TVariableSet): TScriptResult;
  329. function ScriptSetDeformationGridMode(AVars: TVariableSet): TScriptResult;
  330. function ScriptSetDeformationGridSize(AVars: TVariableSet): TScriptResult;
  331. function ScriptSetEraserAlpha(AVars: TVariableSet): TScriptResult;
  332. function ScriptSetEraserMode(AVars: TVariableSet): TScriptResult;
  333. function ScriptSetFloodFillOptions(AVars: TVariableSet): TScriptResult;
  334. function ScriptSetFontName(AVars: TVariableSet): TScriptResult;
  335. function ScriptSetFontSize(AVars: TVariableSet): TScriptResult;
  336. function ScriptSetFontStyle(AVars: TVariableSet): TScriptResult;
  337. function ScriptSetGradientInterpolation(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  338. function ScriptSetGradientRepetition(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  339. function ScriptSetGradientType(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  340. function ScriptSetGradientColors(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  341. function ScriptSetBackGradientInterpolation(AVars: TVariableSet): TScriptResult;
  342. function ScriptSetBackGradientRepetition(AVars: TVariableSet): TScriptResult;
  343. function ScriptSetBackGradientType(AVars: TVariableSet): TScriptResult;
  344. function ScriptSetBackGradientColors(AVars: TVariableSet): TScriptResult;
  345. function ScriptSetOutlineGradientInterpolation(AVars: TVariableSet): TScriptResult;
  346. function ScriptSetOutlineGradientRepetition(AVars: TVariableSet): TScriptResult;
  347. function ScriptSetOutlineGradientType(AVars: TVariableSet): TScriptResult;
  348. function ScriptSetOutlineGradientColors(AVars: TVariableSet): TScriptResult;
  349. function ScriptSetForeGradientInterpolation(AVars: TVariableSet): TScriptResult;
  350. function ScriptSetForeGradientRepetition(AVars: TVariableSet): TScriptResult;
  351. function ScriptSetForeGradientType(AVars: TVariableSet): TScriptResult;
  352. function ScriptSetForeGradientColors(AVars: TVariableSet): TScriptResult;
  353. function ScriptSetTexture(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  354. function ScriptSetTextureRepetition(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  355. function ScriptSetTextureOpacity(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  356. function ScriptSetBackTexture(AVars: TVariableSet): TScriptResult;
  357. function ScriptSetBackTextureRepetition(AVars: TVariableSet): TScriptResult;
  358. function ScriptSetBackTextureOpacity(AVars: TVariableSet): TScriptResult;
  359. function ScriptSetForeTexture(AVars: TVariableSet): TScriptResult;
  360. function ScriptSetForeTextureRepetition(AVars: TVariableSet): TScriptResult;
  361. function ScriptSetForeTextureOpacity(AVars: TVariableSet): TScriptResult;
  362. function ScriptSetOutlineTexture(AVars: TVariableSet): TScriptResult;
  363. function ScriptSetOutlineTextureRepetition(AVars: TVariableSet): TScriptResult;
  364. function ScriptSetOutlineTextureOpacity(AVars: TVariableSet): TScriptResult;
  365. function ScriptSetJoinStyle(AVars: TVariableSet): TScriptResult;
  366. function ScriptSetLightPosition(AVars: TVariableSet): TScriptResult;
  367. function ScriptSetLineCap(AVars: TVariableSet): TScriptResult;
  368. function ScriptSetForeColor(AVars: TVariableSet): TScriptResult;
  369. function ScriptSetPenStyle(AVars: TVariableSet): TScriptResult;
  370. function ScriptSetPenWidth(AVars: TVariableSet): TScriptResult;
  371. function ScriptSetPerspectiveOptions(AVars: TVariableSet): TScriptResult;
  372. function ScriptSetPhongShapeAltitude(AVars: TVariableSet): TScriptResult;
  373. function ScriptSetPhongShapeBorderSize(AVars: TVariableSet): TScriptResult;
  374. function ScriptSetPhongShapeKind(AVars: TVariableSet): TScriptResult;
  375. function ScriptSetShapeOptions(AVars: TVariableSet): TScriptResult;
  376. function ScriptSetShapeRatio(AVars: TVariableSet): TScriptResult;
  377. function ScriptSetSplineStyle(AVars: TVariableSet): TScriptResult;
  378. function ScriptSetTextAlign(AVars: TVariableSet): TScriptResult;
  379. function ScriptSetTextVerticalAlign(AVars: TVariableSet): TScriptResult;
  380. function ScriptSetTextBidiMode(AVars: TVariableSet): TScriptResult;
  381. function ScriptSetTextOutline(AVars: TVariableSet): TScriptResult;
  382. function ScriptSetTextPhong(AVars: TVariableSet): TScriptResult;
  383. function ScriptSetTolerance(AVars: TVariableSet): TScriptResult;
  384. procedure SetBrushIndex(AValue: integer);
  385. procedure SetBrushSpacing(AValue: integer);
  386. function SetControlsVisible(AControls: TList; AVisible: Boolean; AName: string = ''): boolean;
  387. procedure SetArrowEnd(AValue: TArrowKind);
  388. procedure SetArrowSize(AValue: TPointF);
  389. procedure SetArrowStart(AValue: TArrowKind);
  390. procedure SetBackColor(AValue: TBGRAPixel);
  391. procedure SetDeformationGridMode(AValue: TDeformationGridMode);
  392. procedure SetEraserAlpha(AValue: byte);
  393. procedure SetEraserMode(AValue: TEraserMode);
  394. procedure SetFloodFillOptions(AValue: TFloodFillOptions);
  395. procedure SetForeColor(AValue: TBGRAPixel);
  396. procedure SetJoinStyle(AValue: TPenJoinStyle);
  397. procedure SetLightAltitude(AValue: integer);
  398. procedure SetLightPosition(AValue: TPointF);
  399. procedure SetLineCap(AValue: TPenEndCap);
  400. procedure SetOnQueryColorTarget(AValue: TOnQueryColorTargetHandler);
  401. procedure SetOutlineColor(AValue: TBGRAPixel);
  402. procedure SetPerspectiveOptions(AValue: TPerspectiveOptions);
  403. procedure SetPhongShapeAltitude(AValue: integer);
  404. procedure SetPhongShapeBorderSize(AValue: integer);
  405. procedure SetPhongShapeKind(AValue: TPhongShapeKind);
  406. procedure SetShapeOptions(AValue: TShapeOptions);
  407. procedure SetPenStyle(AValue: TPenStyle);
  408. procedure SetPenWidth(AValue: single);
  409. procedure SetShapeRatio(AValue: Single);
  410. procedure SetSplineStyle(AValue: TSplineStyle);
  411. procedure SetTextAlign(AValue: TAlignment);
  412. procedure SetTextVerticalAlign(AValue: TTextLayout);
  413. procedure SetTextBidiMode(AValue: TFontBidiMode);
  414. procedure SetTextFontStyle(AValue: TFontStyles);
  415. procedure SetTextPhong(AValue: boolean);
  416. procedure SetTextShadow(AValue: boolean);
  417. procedure SetTextShadowBlurRadius(AValue: single);
  418. procedure SetTextShadowOffset(AValue: TPoint);
  419. procedure SetTolerance(AValue: byte);
  420. procedure ToolCloseAndReopenImmediatly;
  421. protected
  422. function CheckExitTool: boolean;
  423. procedure NotifyImageOrSelectionChanged(ALayer: TBGRABitmap; ARect: TRect);
  424. procedure InternalSetCurrentToolType(tool: TPaintToolType);
  425. function InternalBitmapToVirtualScreen(PtF: TPointF): TPointF;
  426. function AddLayerOffset(ARect: TRect) : TRect;
  427. procedure RegisterScriptFunctions(ARegister: boolean);
  428. public
  429. BitmapToVirtualScreen: TBitmapToVirtualScreenFunction;
  430. PenWidthControls, AliasingControls, EraserControls, ToleranceControls,
  431. ShapeControls, PenStyleControls, JoinStyleControls, SplineStyleControls,
  432. CloseShapeControls, LineCapControls, DeformationControls,
  433. TextControls, TextShadowControls, PhongControls, AltitudeControls,
  434. PerspectiveControls,FillControls,OutlineFillControls,
  435. BrushControls, RatioControls, DonateControls: TList;
  436. CanvasScale: integer;
  437. PenWidthVisible: boolean;
  438. constructor Create(AImage: TLazPaintImage; AConfigProvider: IConfigProvider;
  439. ABitmapToVirtualScreen: TBitmapToVirtualScreenFunction = nil;
  440. ABlackAndWhite : boolean = false;
  441. AScriptContext: TScriptContext = nil);
  442. destructor Destroy; override;
  443. procedure LoadFromConfig;
  444. procedure SaveToConfig;
  445. procedure ReloadBrushes;
  446. procedure SaveBrushes;
  447. function ApplyPressure(AColor: TBGRAPixel): TBGRAPixel;
  448. function ApplyPressure(AOpacity: byte): byte;
  449. procedure SetPressure(APressure: single);
  450. function GetPressureB: Byte;
  451. procedure StepPenSize(ADecrease: boolean);
  452. function GetCurrentToolType: TPaintToolType;
  453. function SetCurrentToolType(tool: TPaintToolType): boolean;
  454. function UpdateContextualToolbars: boolean;
  455. function GetContextualToolbars: TContextualToolbars;
  456. function ToolCanBeUsed: boolean;
  457. function ToolHasLineCap: boolean;
  458. procedure ToolWakeUp;
  459. procedure ToolSleep;
  460. function ToolDown(X,Y: single; ARightBtn: boolean; APressure: single): boolean; overload;
  461. function ToolMove(X,Y: single; APressure: single): boolean; overload;
  462. function ToolDown(ACoord: TPointF; ARightBtn: boolean; APressure: single): boolean; overload;
  463. function ToolMove(ACoord: TPointF; APressure: single): boolean; overload;
  464. function ToolKeyDown(var key: Word): boolean;
  465. function ToolKeyUp(var key: Word): boolean;
  466. function ToolKeyPress(var key: TUTF8Char): boolean;
  467. function ToolCommand(ACommand: TToolCommand): boolean; virtual;
  468. function ToolProvideCommand(ACommand: TToolCommand): boolean; virtual;
  469. function ToolUp: boolean;
  470. procedure ToolCloseDontReopen;
  471. procedure ToolOpen;
  472. function ToolUpdate: boolean;
  473. function ToolUpdateNeeded: boolean;
  474. procedure ToolPopup(AMessage: TToolPopupMessage; AKey: Word = 0; AAlways: boolean = false);
  475. function IsSelectingTool: boolean;
  476. function DisplayFilledSelection: boolean;
  477. function IsForeEditGradTexPoints: boolean;
  478. function IsBackEditGradTexPoints: boolean;
  479. function IsOutlineEditGradTexPoints: boolean;
  480. procedure QueryExitTool;
  481. procedure QueryExitTool(ASwitchTo: TPaintToolType);
  482. procedure QueryColorTarget(ATarget: TVectorialFill);
  483. function RenderTool(formBitmap: TBGRABitmap): TRect;
  484. function GetRenderBounds(VirtualScreenWidth, VirtualScreenHeight: integer): TRect;
  485. function SuggestGradientBox: TAffineBox;
  486. function SwapToolColors: boolean;
  487. procedure NeedBackGradient;
  488. procedure NeedForeGradient;
  489. procedure NeedOutlineGradient;
  490. procedure AddBrush(brush: TLazPaintBrush);
  491. procedure RemoveBrushAt(index: integer);
  492. procedure SetTextFont(AName: string; ASize: single; AStyle: TFontStyles);
  493. procedure SetTextFont(AFont: TFont);
  494. procedure SetTextOutline(AEnabled: boolean; AWidth: single);
  495. function GetDeformationGridSize: TSize;
  496. procedure SetDeformationGridSize(ASize: TSize);
  497. property Image: TLazPaintImage read FImage;
  498. property Scripting: TScriptContext read FScriptContext;
  499. property BlackAndWhite: boolean read FBlackAndWhite write FBlackAndWhite;
  500. property CurrentTool: TGenericTool read FCurrentTool;
  501. property ToolCurrentCursorPos: TPointF read FToolCurrentCursorPos;
  502. property ToolSleeping: boolean read GetToolSleeping;
  503. property Cursor: TCursor read GetCursor;
  504. property ForeFill: TVectorialFill read FForeFill;
  505. property AllowedForeFillTypes: TVectorialFillTypes read GetAllowedForeFillTypes;
  506. property BackFill: TVectorialFill read FBackFill;
  507. property AllowedBackFillTypes: TVectorialFillTypes read GetAllowedBackFillTypes;
  508. property OutlineFill: TVectorialFill read FOutlineFill;
  509. property AllowedOutlineFillTypes: TVectorialFillTypes read GetAllowedOutlineFillTypes;
  510. property ForeColor: TBGRAPixel read GetForeColor write SetForeColor;
  511. property BackColor: TBGRAPixel read GetBackColor write SetBackColor;
  512. property OutlineColor: TBGRAPixel read GetOutlineColor write SetOutlineColor;
  513. property ForeLastGradient: TBGRALayerGradientOriginal read FForeLastGradient;
  514. property BackLastGradient: TBGRALayerGradientOriginal read FBackLastGradient;
  515. property OutlineLastGradient: TBGRALayerGradientOriginal read FOutlineLastGradient;
  516. property EraserMode: TEraserMode read FEraserMode write SetEraserMode;
  517. property EraserAlpha: byte read FEraserAlpha write SetEraserAlpha;
  518. property PenWidth: single read GetPenWidth write SetPenWidth;
  519. property PenStyle: TPenStyle read FPenStyle write SetPenStyle;
  520. property JoinStyle: TPenJoinStyle read FJoinStyle write SetJoinStyle;
  521. property ShapeOptions: TShapeOptions read FShapeOptions write SetShapeOptions;
  522. property ShapeOptionAliasing: boolean read GetShapeOptionAliasing;
  523. property ShapeRatio: Single read FShapeRatio write SetShapeRatio;
  524. property BrushInfo: TLazPaintBrush read GetBrushInfo;
  525. property BrushAt[AIndex: integer]: TLazPaintBrush read GetBrushAt;
  526. property BrushCount: integer read GetBrushCount;
  527. property BrushIndex: integer read FBrushIndex write SetBrushIndex;
  528. property BrushSpacing: integer read FBrushSpacing write SetBrushSpacing;
  529. property TextFontName: string read GetTextFontName;
  530. property TextFontSize: single read GetTextFontSize;
  531. property TextFontStyle: TFontStyles read GetTextFontStyle write SetTextFontStyle;
  532. property TextAlign: TAlignment read FTextAlign write SetTextAlign;
  533. property TextVerticalAlign: TTextLayout read FTextVerticalAlign write SetTextVerticalAlign;
  534. property TextBidiMode: TFontBidiMode read FTextBidiMode write SetTextBidiMode;
  535. property TextOutline: boolean read FTextOutline;
  536. property TextOutlineWidth: single read FTextOutlineWidth;
  537. property TextPhong: boolean read FTextPhong write SetTextPhong;
  538. property IsEditingText: boolean read GetIsEditingText;
  539. property LightPosition: TPointF read FLightPosition write SetLightPosition;
  540. property LightAltitude: integer read FLightAltitude write SetLightAltitude;
  541. property TextShadow: boolean read FTextShadow write SetTextShadow;
  542. property TextShadowBlurRadius: single read FTextShadowBlurRadius write SetTextShadowBlurRadius;
  543. property TextShadowOffset: TPoint read FTextShadowOffset write SetTextShadowOffset;
  544. property LineCap: TPenEndCap read FLineCap write SetLineCap;
  545. property ArrowStart: TArrowKind read FArrowStart write SetArrowStart;
  546. property ArrowEnd: TArrowKind read FArrowEnd write SetArrowEnd;
  547. property ArrowSize: TPointF read FArrowSize write SetArrowSize;
  548. property SplineStyle: TSplineStyle read FSplineStyle write SetSplineStyle;
  549. property PhongShapeAltitude: integer read FPhongShapeAltitude write SetPhongShapeAltitude;
  550. property PhongShapeBorderSize: integer read FPhongShapeBorderSize write SetPhongShapeBorderSize;
  551. property PhongShapeKind: TPhongShapeKind read FPhongShapeKind write SetPhongShapeKind;
  552. property DeformationGridNbX: integer read FDeformationGridNbX;
  553. property DeformationGridNbY: integer read FDeformationGridNbY;
  554. property DeformationGridSize: TSize read GetDeformationGridSize write SetDeformationGridSize;
  555. property MaxDeformationGridSize: TSize read GetMaxDeformationGridSize;
  556. property DeformationGridMode: TDeformationGridMode read FDeformationGridMode write SetDeformationGridMode;
  557. property Tolerance: byte read FTolerance write SetTolerance;
  558. property FloodFillOptions: TFloodFillOptions read FFloodFillOptions write SetFloodFillOptions;
  559. property PerspectiveOptions: TPerspectiveOptions read FPerspectiveOptions write SetPerspectiveOptions;
  560. property OnToolChanged: TOnToolChangedHandler read FOnToolChangedHandler write FOnToolChangedHandler;
  561. property OnToolRenderChanged: TNotifyEvent read FOnToolRenderChanged write FOnToolRenderChanged;
  562. property OnToolbarChanged: TNotifyEvent read FOnToolbarChanged write FOnToolbarChanged;
  563. property OnPopup: TOnPopupToolHandler read FOnPopupToolHandler write FOnPopupToolHandler;
  564. property OnEraserChanged: TNotifyEvent read FOnEraserChanged write FOnEraserChanged;
  565. property OnFillChanged: TNotifyEvent read FOnFillChanged write FOnFillChanged;
  566. property OnQueryColorTarget: TOnQueryColorTargetHandler read FOnQueryColorTarget write SetOnQueryColorTarget;
  567. property OnPenWidthChanged: TNotifyEvent read FOnPenWidthChanged write FOnPenWidthChanged;
  568. property OnBrushChanged: TNotifyEvent read FOnBrushChanged write FOnBrushChanged;
  569. property OnBrushListChanged: TNotifyEvent read FOnBrushListChanged write FOnBrushListChanged;
  570. property OnPenStyleChanged: TNotifyEvent read FOnPenStyleChanged write FOnPenStyleChanged;
  571. property OnJoinStyleChanged: TNotifyEvent read FOnJoinStyleChanged write FOnJoinStyleChanged;
  572. property OnShapeOptionChanged: TNotifyEvent read FOnShapeOptionChanged write FOnShapeOptionChanged;
  573. property OnShapeRatioChanged: TNotifyEvent read FOnShapeRatioChanged write FOnShapeRatioChanged;
  574. property OnTextFontChanged: TNotifyEvent read FOnTextFontChanged write FOnTextFontChanged;
  575. property OnTextAlignChanged: TNotifyEvent read FOnTextAlignChanged write FOnTextAlignChanged;
  576. property OnTextOutlineChanged: TNotifyEvent read FOnTextOutlineChanged write FOnTextOutlineChanged;
  577. property OnTextPhongChanged: TNotifyEvent read FOnTextPhongChanged write FOnTextPhongChanged;
  578. property OnLightChanged: TNotifyEvent read FOnLightChanged write FOnLightChanged;
  579. property OnTextShadowChanged: TNotifyEvent read FOnTextShadowChanged write FOnTextShadowChanged;
  580. property OnLineCapChanged: TNotifyEvent read FOnLineCapChanged write FOnLineCapChanged;
  581. property OnSplineStyleChanged: TNotifyEvent read FOnSplineStyleChanged write FOnSplineStyleChanged;
  582. property OnPhongShapeChanged: TNotifyEvent read FOnPhongShapeChanged write FOnPhongShapeChanged;
  583. property OnDeformationGridChanged: TNotifyEvent read FOnDeformationGridChanged write FOnDeformationGridChanged;
  584. property OnToleranceChanged: TNotifyEvent read FOnToleranceChanged write FOnToleranceChanged;
  585. property OnFloodFillOptionChanged: TNotifyEvent read FOnFloodFillOptionChanged write FOnFloodFillOptionChanged;
  586. property OnPerspectiveOptionChanged: TNotifyEvent read FOnPerspectiveOptionChanged write FOnPerspectiveOptionChanged;
  587. end;
  588. procedure RegisterTool(ATool: TPaintToolType; AClass: TToolClass);
  589. function ToolPopupMessageToStr(AMessage :TToolPopupMessage; AKey: Word = 0): string;
  590. implementation
  591. uses UGraph, LCScaleDPI, LazPaintType, UCursors, BGRATextFX, ULoading, UResourceStrings,
  592. BGRATransform, LCVectorOriginal, BGRASVGOriginal, math, ULoadImage, LCVectorTextShapes,
  593. LResources;
  594. function StrToPaintToolType(const s: ansistring): TPaintToolType;
  595. var pt: TPaintToolType;
  596. ls: ansistring;
  597. begin
  598. result := ptHand;
  599. ls:= LowerCase(s);
  600. for pt := low(TPaintToolType) to high(TPaintToolType) do
  601. if ls = LowerCase(PaintToolTypeStr[pt]) then
  602. begin
  603. result := pt;
  604. break;
  605. end;
  606. end;
  607. function GradientInterpolationToDisplay(AValue: TBGRAColorInterpolation): string;
  608. begin
  609. case AValue of
  610. ciLinearRGB: result := rsLinearRGB;
  611. ciLinearHSLPositive: result := rsHueCW;
  612. ciLinearHSLNegative: result := rsHueCCW;
  613. ciGSBPositive: result := rsCorrectedHueCW;
  614. ciGSBNegative: result := rsCorrectedHueCCW;
  615. else
  616. result := rsRGB;
  617. end;
  618. end;
  619. function DisplayToGradientInterpolation(AValue: string): TBGRAColorInterpolation;
  620. begin
  621. if AValue=rsLinearRGB then result := ciLinearRGB else
  622. if AValue=rsHueCW then result := ciLinearHSLPositive else
  623. if AValue=rsHueCCW then result := ciLinearHSLNegative else
  624. if AValue=rsCorrectedHueCW then result := ciGSBPositive else
  625. if AValue=rsCorrectedHueCCW then result := ciGSBNegative
  626. else
  627. result := ciStdRGB;
  628. end;
  629. function GradientInterpolationToStr(AValue: TBGRAColorInterpolation): string;
  630. begin
  631. case AValue of
  632. ciStdRGB: result := 'StdRGB';
  633. ciLinearHSLPositive: result := 'LinearHSLPositive';
  634. ciLinearHSLNegative: result := 'LinearHSLNegative';
  635. ciGSBPositive: result := 'GSBPositive';
  636. ciGSBNegative: result := 'GSBNegative';
  637. else
  638. result := 'LinearRGB';
  639. end;
  640. end;
  641. function StrToGradientInterpolation(AValue: string): TBGRAColorInterpolation;
  642. begin
  643. if AValue='StdRGB' then result := ciStdRGB else
  644. if AValue='LinearHSLPositive' then result := ciLinearHSLPositive else
  645. if AValue='LinearHSLNegative' then result := ciLinearHSLNegative else
  646. if AValue='GSBPositive' then result := ciGSBPositive else
  647. if AValue='GSBNegative' then result := ciGSBNegative
  648. else
  649. result := ciLinearRGB;
  650. end;
  651. function GradientRepetitionToStr(AValue: TBGRAGradientRepetition): string;
  652. begin
  653. case AValue of
  654. grRepeat: result:= 'Repeat';
  655. grReflect: result:= 'Reflect';
  656. grSine: result:= 'Sine';
  657. else result := 'Pad';
  658. end;
  659. end;
  660. function StrToGradientRepetition(AValue: string): TBGRAGradientRepetition;
  661. begin
  662. case AValue of
  663. 'Repeat': result:= grRepeat;
  664. 'Reflect': result:= grReflect;
  665. 'Sine': result:= grSine;
  666. else result := grPad;
  667. end;
  668. end;
  669. function GradientToConfigStr(AGradient: TBGRALayerGradientOriginal): string;
  670. var
  671. vars: TVariableSet;
  672. begin
  673. vars := TVariableSet.Create('');
  674. vars.Pixels['StartColor'] := AGradient.StartColor;
  675. vars.Pixels['EndColor'] := AGradient.EndColor;
  676. vars.Strings['GradientType'] := GradientTypeStr[AGradient.GradientType];
  677. vars.Strings['ColorInterpolation'] := GradientInterpolationToStr(AGradient.ColorInterpolation);
  678. vars.Strings['Repetition'] := GradientRepetitionToStr(AGradient.Repetition);
  679. result := vars.VariablesAsString;
  680. vars.Free;
  681. end;
  682. procedure AssignGradientFromConfigStr(AGradient: TBGRALayerGradientOriginal; AValue: string);
  683. var
  684. vars: TVariableSet;
  685. begin
  686. vars := TVariableSet.Create('', AValue);
  687. if vars.IsDefined('StartColor') then AGradient.StartColor := vars.Pixels['StartColor'];
  688. if vars.IsDefined('EndColor') then AGradient.EndColor := vars.Pixels['EndColor'];
  689. if vars.IsDefined('GradientType') then AGradient.GradientType := StrToGradientType(vars.Strings['GradientType']);
  690. if vars.IsDefined('ColorInterpolation') then AGradient.ColorInterpolation := StrToGradientInterpolation(vars.Strings['ColorInterpolation']);
  691. if vars.IsDefined('Repetition') then AGradient.Repetition := StrToGradientRepetition(vars.Strings['Repetition']);
  692. vars.Free;
  693. end;
  694. var
  695. PaintTools: array[TPaintToolType] of TToolClass;
  696. procedure RegisterTool(ATool: TPaintToolType; AClass: TToolClass);
  697. begin
  698. PaintTools[ATool] := AClass;
  699. end;
  700. function ReplaceKey(AText: string; AKey: Word; AParam: integer = 1): string;
  701. begin
  702. if AKey = VK_SHIFT then result := StringReplace(AText, '%'+inttostr(AParam), rsShift, []) else
  703. if AKey = VK_CONTROL then result := StringReplace(AText, '%'+inttostr(AParam), {$IFDEF DARWIN}rsCmd{$ELSE}rsCtrl{$ENDIF}, []) else
  704. if AKey = VK_MENU then result := StringReplace(AText, '%'+inttostr(AParam), rsAlt, []) else
  705. result := AText;
  706. end;
  707. function ToolPopupMessageToStr(AMessage: TToolPopupMessage; AKey: Word = 0): string;
  708. begin
  709. case AMessage of
  710. tpmHoldKeyForSquare: result := ReplaceKey(rsHoldKeyForSquare, AKey);
  711. tpmHoldKeySnapToPixel: result := ReplaceKey(rsHoldKeySnapToPixel, AKey);
  712. tpmReturnValides: result := rsReturnValides;
  713. tpmBackspaceRemoveLastPoint: result := rsBackspaceRemoveLastPoint;
  714. tpmRightClickFinishShape: result := rsRightClickFinishShape;
  715. tpmHoldKeyRestrictRotation: result := ReplaceKey(rsHoldKeyRestrictRotation, AKey);
  716. tpmHoldKeysScaleMode: result := ReplaceKey(ReplaceKey(rsHoldKeysScaleMode, AKey, 2), VK_MENU);
  717. tpmCurveModeHint: result := rsCurveModeHint;
  718. tpmBlendOpBackground: result := rsBlendOpNotUsedForBackground;
  719. tpmRightClickForSource: result := rsRightClickForSource;
  720. tpmNothingToBeDeformed: result := rsNothingToBeDeformed;
  721. tpmRightClickForTransformCenter: result := rsRightClickForTransformCenter;
  722. tpmLayerEmpty: result := rsEmptyLayer;
  723. tpmOpacity0: result := rsOpacity + ' = 0';
  724. else
  725. result := '';
  726. end;
  727. end;
  728. { TReadonlyTool }
  729. function TReadonlyTool.GetAction: TLayerAction;
  730. begin
  731. Result:= nil;
  732. end;
  733. function TReadonlyTool.GetIsSelectingTool: boolean;
  734. begin
  735. result := false;
  736. end;
  737. function TReadonlyTool.DoGetToolDrawingLayer: TBGRABitmap;
  738. begin
  739. if Manager.Image.SelectionMaskEmpty or not assigned(Manager.Image.SelectionLayerReadonly) then
  740. Result:= Manager.Image.CurrentLayerReadOnly
  741. else
  742. Result:= Manager.Image.SelectionLayerReadonly;
  743. end;
  744. { TGenericTool }
  745. {$hints off}
  746. function TGenericTool.GetLayerOffset: TPoint;
  747. begin
  748. if IsSelectingTool or not Assigned(Manager.Image) then
  749. result := Point(0,0)
  750. else
  751. if GetToolDrawingLayer = Manager.Image.CurrentLayerReadOnly then
  752. result := Manager.Image.LayerOffset[Manager.Image.CurrentLayerIndex]
  753. else
  754. result := Point(0,0);
  755. end;
  756. function TGenericTool.GetUniversalBrush(ASource: TVectorialFill;
  757. var ADest: TVectorialFill; var AScan: TBGRACustomScanner): TUniversalBrush;
  758. begin
  759. FreeAndNil(AScan);
  760. FreeAndNil(ADest);
  761. case ASource.FillType of
  762. vftNone: TBGRABitmap.IdleBrush(result);
  763. vftSolid: TBGRABitmap.SolidBrush(result, Manager.ApplyPressure(ASource.SolidColor));
  764. else
  765. begin
  766. ADest := ASource.Duplicate;
  767. ADest.FitGeometry(TAffineBox.AffineBox(RectF(0,0,Manager.Image.Width,Manager.Image.Height)));
  768. ADest.ApplyOpacity(Manager.GetPressureB);
  769. AScan := ADest.CreateScanner(AffineMatrixIdentity, false);
  770. TBGRABitmap.ScannerBrush(result, AScan);
  771. end;
  772. end;
  773. end;
  774. function TGenericTool.GetIsEditingText: boolean;
  775. begin
  776. result := false;
  777. end;
  778. function TGenericTool.GetAllowedBackFillTypes: TVectorialFillTypes;
  779. begin
  780. result := [vftSolid,vftGradient,vftTexture];
  781. end;
  782. function TGenericTool.GetAllowedForeFillTypes: TVectorialFillTypes;
  783. begin
  784. result := [vftSolid,vftGradient,vftTexture];
  785. end;
  786. function TGenericTool.GetAllowedOutlineFillTypes: TVectorialFillTypes;
  787. begin
  788. result := [vftSolid,vftGradient,vftTexture];
  789. end;
  790. function TGenericTool.GetIsBackEditGradTexPoints: boolean;
  791. begin
  792. result := false;
  793. end;
  794. function TGenericTool.GetIsOutlineEditGradTexPoints: boolean;
  795. begin
  796. result := false;
  797. end;
  798. function TGenericTool.GetIsForeEditGradTexPoints: boolean;
  799. begin
  800. result := false;
  801. end;
  802. function TGenericTool.GetForeUniversalBrush: TUniversalBrush;
  803. begin
  804. result := GetUniversalBrush(Manager.ForeFill, FForeFill, FForeFillScan);
  805. end;
  806. function TGenericTool.GetBackUniversalBrush: TUniversalBrush;
  807. begin
  808. result := GetUniversalBrush(Manager.BackFill, FBackFill, FBackFillScan);
  809. end;
  810. function TGenericTool.GetStatusText: string;
  811. begin
  812. result := '';
  813. end;
  814. function TGenericTool.DoGetToolDrawingLayer: TBGRABitmap;
  815. begin
  816. if Action = nil then
  817. result := nil
  818. else if IsSelectingTool then
  819. begin
  820. Action.QuerySelection;
  821. result := Action.CurrentSelection;
  822. if result = nil then
  823. raise exception.Create('Selection not created');
  824. end
  825. else
  826. result := Action.DrawingLayer;
  827. end;
  828. function TGenericTool.GetCurrentLayerKind: TLayerKind;
  829. var
  830. c: TBGRALayerOriginalAny;
  831. begin
  832. if not Manager.Image.LayerOriginalDefined[Manager.Image.CurrentLayerIndex] then
  833. begin
  834. if Manager.Image.CurrentLayerEmpty then exit(lkEmpty)
  835. else exit(lkBitmap);
  836. end else
  837. if not Manager.Image.LayerOriginalKnown[Manager.Image.CurrentLayerIndex] then
  838. exit(lkUnknown)
  839. else
  840. begin
  841. c := Manager.Image.LayerOriginalClass[Manager.Image.CurrentLayerIndex];
  842. if c = TVectorOriginal then exit(lkVectorial) else
  843. if c = TBGRALayerImageOriginal then exit(lkTransformedBitmap) else
  844. if c = TBGRALayerGradientOriginal then exit(lkGradient) else
  845. if c = TBGRALayerSVGOriginal then exit(lkSVG) else
  846. exit(lkOther);
  847. end;
  848. end;
  849. function TGenericTool.GetAction: TLayerAction;
  850. begin
  851. if not Assigned(FAction) then
  852. begin
  853. FAction := Manager.Image.CreateAction(not IsSelectingTool And Manager.Image.SelectionMaskEmpty,
  854. IsSelectingTool or not Manager.Image.SelectionMaskEmpty);
  855. FAction.OnTryStop := @OnTryStop;
  856. FAction.ChangeBoundsNotified:= true;
  857. end;
  858. result := FAction;
  859. end;
  860. function TGenericTool.GetIdleAction: TLayerAction;
  861. begin
  862. if not Assigned(FAction) then
  863. begin
  864. FAction := Manager.Image.CreateAction(false);
  865. FAction.OnTryStop := @OnTryStop;
  866. FAction.ChangeBoundsNotified:= true;
  867. end;
  868. result := FAction;
  869. end;
  870. function TGenericTool.FixSelectionTransform: boolean;
  871. begin
  872. result:= true;
  873. end;
  874. function TGenericTool.FixLayerOffset: boolean;
  875. begin
  876. result:= true;
  877. end;
  878. function TGenericTool.DoToolDown(toolDest: TBGRABitmap; pt: TPoint;
  879. ptF: TPointF; rightBtn: boolean): TRect;
  880. begin
  881. result := EmptyRect;
  882. end;
  883. {$hints on}
  884. {$hints off}
  885. function TGenericTool.DoToolMove(toolDest: TBGRABitmap; pt: TPoint; ptF: TPointF): TRect;
  886. begin
  887. result := EmptyRect;
  888. end;
  889. function TGenericTool.DoToolKeyDown(var key: Word): TRect;
  890. begin
  891. result := EmptyRect;
  892. //defined later
  893. end;
  894. function TGenericTool.DoToolKeyUp(var key: Word): TRect;
  895. begin
  896. result := EmptyRect;
  897. //defined later
  898. end;
  899. {$hints on}
  900. constructor TGenericTool.Create(AManager: TToolManager);
  901. begin
  902. inherited Create;
  903. FManager := AManager;
  904. FAction := nil;
  905. FShiftState:= [];
  906. Cursor := crDefault;
  907. end;
  908. destructor TGenericTool.Destroy;
  909. begin
  910. FAction.Free;
  911. inherited Destroy;
  912. end;
  913. procedure TGenericTool.ReleaseUniversalBrushes;
  914. begin
  915. FreeAndNil(FForeFillScan);
  916. FreeAndNil(FForeFill);
  917. FreeAndNil(FBackFillScan);
  918. FreeAndNil(FBackFill);
  919. end;
  920. procedure TGenericTool.ValidateAction;
  921. begin
  922. if Assigned(FAction) then
  923. begin
  924. FValidating := true;
  925. FAction.Validate;
  926. FValidating := false;
  927. FreeAndNil(FAction);
  928. end;
  929. end;
  930. procedure TGenericTool.ValidateActionPartially;
  931. begin
  932. if Assigned(FAction) then
  933. begin
  934. FValidating := true;
  935. FAction.PartialValidate;
  936. FValidating := false;
  937. end;
  938. end;
  939. procedure TGenericTool.CancelAction;
  940. begin
  941. if FAction <> nil then
  942. begin
  943. FCanceling := true;
  944. FreeAndNil(FAction);
  945. FCanceling := false;
  946. end;
  947. end;
  948. procedure TGenericTool.CancelActionPartially;
  949. begin
  950. if Assigned(FAction) then
  951. begin
  952. FCanceling := true;
  953. FAction.PartialCancel;
  954. FCanceling := false;
  955. end;
  956. end;
  957. function TGenericTool.HasPen: boolean;
  958. begin
  959. result := (toDrawShape in Manager.ShapeOptions) or not (ctShape in GetContextualToolbars);
  960. end;
  961. function TGenericTool.DoToolUpdate(toolDest: TBGRABitmap): TRect;
  962. begin
  963. result := EmptyRect;
  964. //nothing
  965. end;
  966. procedure TGenericTool.OnTryStop(sender: TCustomLayerAction);
  967. begin
  968. Manager.ToolCloseAndReopenImmediatly;
  969. end;
  970. function TGenericTool.SelectionMaxPointDistance: single;
  971. begin
  972. result := DoScaleX(10,OriginalDPI);
  973. result /= Manager.Image.ZoomFactor;
  974. end;
  975. function TGenericTool.ToolUpdate: TRect;
  976. var toolDest :TBGRABitmap;
  977. begin
  978. toolDest := GetToolDrawingLayer;
  979. if toolDest = nil then
  980. begin
  981. result := EmptyRect;
  982. exit;
  983. end;
  984. toolDest.JoinStyle := Manager.JoinStyle;
  985. toolDest.LineCap := Manager.LineCap;
  986. toolDest.PenStyle := Manager.PenStyle;
  987. result := DoToolUpdate(toolDest);
  988. end;
  989. function TGenericTool.ToolDown(X, Y: single; rightBtn: boolean): TRect;
  990. var
  991. toolDest: TBGRABitmap;
  992. ptF: TPointF;
  993. begin
  994. result := EmptyRect;
  995. toolDest := GetToolDrawingLayer;
  996. if toolDest = nil then exit;
  997. toolDest.JoinStyle := Manager.JoinStyle;
  998. toolDest.LineCap := Manager.LineCap;
  999. toolDest.PenStyle := Manager.PenStyle;
  1000. ptF := PointF(x,y);
  1001. if toolDest = Manager.Image.CurrentLayerReadOnly then
  1002. begin
  1003. if FixLayerOffset then
  1004. begin
  1005. ptF.x -= LayerOffset.x;
  1006. ptF.y -= LayerOffset.y;
  1007. end;
  1008. end else if FixSelectionTransform and ((toolDest = Manager.Image.SelectionMaskReadonly)
  1009. or (toolDest = Manager.Image.SelectionLayerReadonly)) and
  1010. IsAffineMatrixInversible(Manager.Image.SelectionTransform) then
  1011. ptF := AffineMatrixInverse(Manager.Image.SelectionTransform)*ptF;
  1012. result := DoToolDown(toolDest,ptF.Round,ptF,rightBtn);
  1013. end;
  1014. function TGenericTool.ToolMove(X, Y: single): TRect;
  1015. var
  1016. toolDest: TBGRABitmap;
  1017. ptF: TPointF;
  1018. begin
  1019. ptF := PointF(x,y);
  1020. Manager.FToolCurrentCursorPos := ptF;
  1021. result := EmptyRect;
  1022. toolDest := GetToolDrawingLayer;
  1023. if toolDest = nil then exit;
  1024. toolDest.JoinStyle := Manager.JoinStyle;
  1025. toolDest.LineCap := Manager.LineCap;
  1026. toolDest.PenStyle := Manager.PenStyle;
  1027. if toolDest = Manager.Image.CurrentLayerReadOnly then
  1028. begin
  1029. if FixLayerOffset then
  1030. begin
  1031. ptF.x -= LayerOffset.x;
  1032. ptF.y -= LayerOffset.y;
  1033. end;
  1034. end else if FixSelectionTransform and ((toolDest = Manager.Image.SelectionMaskReadonly)
  1035. or (toolDest = Manager.Image.SelectionLayerReadonly)) and
  1036. IsAffineMatrixInversible(Manager.Image.SelectionTransform) then
  1037. ptF := AffineMatrixInverse(Manager.Image.SelectionTransform)*ptF;
  1038. result := DoToolMove(toolDest,ptF.Round,ptF);
  1039. end;
  1040. {$hints off}
  1041. function TGenericTool.ToolKeyDown(var key: Word): TRect;
  1042. var
  1043. key2: Word;
  1044. begin
  1045. if key = VK_SHIFT then
  1046. begin
  1047. Include(FShiftState, ssShift);
  1048. //do not reset Key to preserve typing ^o or "o
  1049. end else
  1050. if (key = VK_MENU) then
  1051. Include(FShiftState, ssAlt);
  1052. if (Key = VK_SNAP) or (Key = VK_SNAP2) then
  1053. begin
  1054. key2 := VK_CONTROL;
  1055. Include(FShiftState, ssSnap);
  1056. result := DoToolKeyDown(key2);
  1057. if key2 = 0 then key := 0;
  1058. end else
  1059. result := DoToolKeyDown(key);
  1060. end;
  1061. function TGenericTool.ToolKeyUp(var key: Word): TRect;
  1062. var
  1063. key2: word;
  1064. begin
  1065. if (key = VK_SHIFT) and (ssShift in FShiftState) then
  1066. begin
  1067. Exclude(FShiftState, ssShift);
  1068. //do not reset key to preserve typing ^o or "o
  1069. end else
  1070. if (key = VK_MENU) and (ssAlt in FShiftState) then
  1071. Exclude(FShiftState, ssAlt);
  1072. //propagate in all cases to know when keys are released for unicode input
  1073. if (Key = VK_SNAP) or (Key = VK_SNAP2) then
  1074. begin
  1075. key2 := VK_CONTROL;
  1076. Exclude(FShiftState, ssSnap);
  1077. result := DoToolKeyUp(key2);
  1078. if key2 = 0 then key := 0;
  1079. end else
  1080. result := DoToolKeyUp(key);
  1081. end;
  1082. function TGenericTool.ToolKeyPress(var key: TUTF8Char): TRect;
  1083. begin
  1084. result := EmptyRect;
  1085. //defined later
  1086. end;
  1087. {$hints on}
  1088. function TGenericTool.ToolUp: TRect;
  1089. begin
  1090. result := EmptyRect;
  1091. //defined later
  1092. end;
  1093. function TGenericTool.ToolCommand(ACommand: TToolCommand): boolean;
  1094. begin
  1095. result := false;
  1096. end;
  1097. function TGenericTool.ToolProvideCommand(ACommand: TToolCommand): boolean;
  1098. begin
  1099. result := false;
  1100. end;
  1101. function TGenericTool.SuggestGradientBox: TAffineBox;
  1102. var
  1103. m: TAffineMatrix;
  1104. begin
  1105. result := TAffineBox.AffineBox(RectF(PointF(0,0),PointF(Manager.Image.Width,Manager.Image.Height)));
  1106. if not IsSelectingTool and Manager.Image.SelectionMaskEmpty then
  1107. begin
  1108. m := Manager.Image.LayerOriginalMatrix[Manager.Image.CurrentLayerIndex];
  1109. result := AffineMatrixInverse(m)*result;
  1110. end;
  1111. end;
  1112. function TGenericTool.GetContextualToolbars: TContextualToolbars;
  1113. begin
  1114. result := [ctPenFill, ctBackFill];
  1115. end;
  1116. function TGenericTool.GetToolDrawingLayer: TBGRABitmap;
  1117. begin
  1118. result := DoGetToolDrawingLayer;
  1119. FLastToolDrawingLayer := result;
  1120. end;
  1121. procedure TGenericTool.RestoreBackupDrawingLayer;
  1122. begin
  1123. if Assigned(FAction) then
  1124. begin
  1125. if IsSelectingTool then
  1126. Action.RestoreSelectionMask
  1127. else
  1128. Action.RestoreDrawingLayer;
  1129. end;
  1130. end;
  1131. function TGenericTool.GetBackupLayerIfExists: TBGRABitmap;
  1132. begin
  1133. if Action = nil then
  1134. begin
  1135. result := nil;
  1136. exit;
  1137. end;
  1138. if IsSelectingTool then
  1139. result := Action.BackupSelection
  1140. else
  1141. result := Action.BackupDrawingLayer;
  1142. end;
  1143. {$hints off}
  1144. function TGenericTool.Render(VirtualScreen: TBGRABitmap; VirtualScreenWidth, VirtualScreenHeight: integer; BitmapToVirtualScreen: TBitmapToVirtualScreenFunction): TRect;
  1145. begin
  1146. result := EmptyRect;
  1147. end;
  1148. {$hints on}
  1149. { TToolManager }
  1150. function TToolManager.GetCurrentToolType: TPaintToolType;
  1151. begin
  1152. result := FCurrentToolType;
  1153. end;
  1154. function TToolManager.SetCurrentToolType(tool: TPaintToolType): boolean;
  1155. begin
  1156. if not ToolSleeping then
  1157. begin
  1158. InternalSetCurrentToolType(tool);
  1159. result := true;
  1160. end
  1161. else result := false;
  1162. end;
  1163. function TToolManager.SetControlsVisible(AControls: TList; AVisible: Boolean; AName: string): boolean;
  1164. procedure SetVisibility(AControl: TControl; AVisible: boolean);
  1165. begin
  1166. if AControl.Visible <> AVisible then
  1167. begin
  1168. AControl.Visible := AVisible;
  1169. result := true;
  1170. end;
  1171. end;
  1172. var i: integer;
  1173. begin
  1174. result := false;
  1175. if AName <> '' then
  1176. begin
  1177. for i := AControls.Count-1 downto 0 do
  1178. if (TObject(AControls[i]) as TControl).Name <> AName then
  1179. SetVisibility(TObject(AControls[i]) as TControl, False);
  1180. for i := 0 to AControls.Count-1 do
  1181. if (TObject(AControls[i]) as TControl).Name = AName then
  1182. SetVisibility(TObject(AControls[i]) as TControl, True);
  1183. end else
  1184. begin
  1185. if AVisible then
  1186. begin
  1187. for i := 0 to AControls.Count-1 do
  1188. SetVisibility(TObject(AControls[i]) as TControl, True);
  1189. end else
  1190. for i := AControls.Count-1 downto 0 do
  1191. SetVisibility(TObject(AControls[i]) as TControl, False);
  1192. end;
  1193. end;
  1194. procedure TToolManager.SetArrowEnd(AValue: TArrowKind);
  1195. begin
  1196. if FArrowEnd=AValue then Exit;
  1197. FArrowEnd:=AValue;
  1198. ToolUpdate;
  1199. if Assigned(FOnLineCapChanged) then FOnLineCapChanged(self);
  1200. end;
  1201. procedure TToolManager.SetArrowSize(AValue: TPointF);
  1202. begin
  1203. if AValue.x < MinArrowSize then AValue.x := MinArrowSize;
  1204. if AValue.x > MaxArrowSize then AValue.x := MaxArrowSize;
  1205. if AValue.y < MinArrowSize then AValue.y := MinArrowSize;
  1206. if AValue.y > MaxArrowSize then AValue.y := MaxArrowSize;
  1207. if FArrowSize=AValue then Exit;
  1208. FArrowSize:=AValue;
  1209. ToolUpdate;
  1210. if Assigned(FOnLineCapChanged) then FOnLineCapChanged(self);
  1211. end;
  1212. procedure TToolManager.SetArrowStart(AValue: TArrowKind);
  1213. begin
  1214. if FArrowStart=AValue then Exit;
  1215. FArrowStart:=AValue;
  1216. ToolUpdate;
  1217. if Assigned(FOnLineCapChanged) then FOnLineCapChanged(self);
  1218. end;
  1219. procedure TToolManager.SetBackColor(AValue: TBGRAPixel);
  1220. begin
  1221. FBackFill.SolidColor := AValue;
  1222. end;
  1223. procedure TToolManager.SetDeformationGridMode(AValue: TDeformationGridMode);
  1224. begin
  1225. if FDeformationGridMode=AValue then Exit;
  1226. FDeformationGridMode:=AValue;
  1227. ToolUpdate;
  1228. if Assigned(FOnDeformationGridChanged) then FOnDeformationGridChanged(self);
  1229. end;
  1230. procedure TToolManager.SetEraserAlpha(AValue: byte);
  1231. begin
  1232. if FEraserAlpha=AValue then Exit;
  1233. FEraserAlpha:=AValue;
  1234. ToolUpdate;
  1235. if Assigned(FOnEraserChanged) then FOnEraserChanged(self);
  1236. end;
  1237. procedure TToolManager.SetEraserMode(AValue: TEraserMode);
  1238. begin
  1239. if FEraserMode=AValue then Exit;
  1240. FEraserMode:=AValue;
  1241. ToolUpdate;
  1242. if Assigned(FOnEraserChanged) then FOnEraserChanged(self);
  1243. end;
  1244. procedure TToolManager.SetFloodFillOptions(AValue: TFloodFillOptions);
  1245. begin
  1246. if FFloodFillOptions=AValue then Exit;
  1247. FFloodFillOptions:=AValue;
  1248. ToolUpdate;
  1249. if Assigned(FOnFloodFillOptionChanged) then FOnFloodFillOptionChanged(self);
  1250. end;
  1251. procedure TToolManager.SetForeColor(AValue: TBGRAPixel);
  1252. begin
  1253. FForeFill.SolidColor := AValue;
  1254. end;
  1255. procedure TToolManager.SetJoinStyle(AValue: TPenJoinStyle);
  1256. begin
  1257. if FJoinStyle=AValue then Exit;
  1258. FJoinStyle:=AValue;
  1259. ToolUpdate;
  1260. if Assigned(FOnJoinStyleChanged) then FOnJoinStyleChanged(self);
  1261. end;
  1262. procedure TToolManager.SetLightAltitude(AValue: integer);
  1263. begin
  1264. if FLightAltitude=AValue then Exit;
  1265. FLightAltitude:=AValue;
  1266. ToolUpdate;
  1267. if Assigned(FOnLightChanged) then FOnLightChanged(self);
  1268. end;
  1269. procedure TToolManager.SetLightPosition(AValue: TPointF);
  1270. begin
  1271. if FLightPosition=AValue then Exit;
  1272. FLightPosition:=AValue;
  1273. ToolUpdate;
  1274. if Assigned(FOnLightChanged) then FOnLightChanged(self);
  1275. end;
  1276. procedure TToolManager.SetLineCap(AValue: TPenEndCap);
  1277. begin
  1278. if FLineCap=AValue then Exit;
  1279. FLineCap:=AValue;
  1280. ToolUpdate;
  1281. if Assigned(FOnLineCapChanged) then FOnLineCapChanged(self);
  1282. end;
  1283. procedure TToolManager.SetOnQueryColorTarget(AValue: TOnQueryColorTargetHandler);
  1284. begin
  1285. if FOnQueryColorTarget=AValue then Exit;
  1286. FOnQueryColorTarget:=AValue;
  1287. end;
  1288. procedure TToolManager.SetOutlineColor(AValue: TBGRAPixel);
  1289. begin
  1290. FOutlineFill.SolidColor := AValue;
  1291. end;
  1292. procedure TToolManager.SetPerspectiveOptions(AValue: TPerspectiveOptions);
  1293. begin
  1294. if FPerspectiveOptions=AValue then Exit;
  1295. FPerspectiveOptions:=AValue;
  1296. ToolUpdate;
  1297. if Assigned(FOnPerspectiveOptionChanged) then FOnPerspectiveOptionChanged(self);
  1298. end;
  1299. procedure TToolManager.SetPhongShapeAltitude(AValue: integer);
  1300. begin
  1301. if AValue < MinPhongShapeAltitude then AValue := MinPhongShapeAltitude;
  1302. if AValue > MaxPhongShapeAltitude then AValue := MaxPhongShapeAltitude;
  1303. if FPhongShapeAltitude=AValue then Exit;
  1304. FPhongShapeAltitude:=AValue;
  1305. ToolUpdate;
  1306. if Assigned(FOnPhongShapeChanged) then FOnPhongShapeChanged(self);
  1307. end;
  1308. procedure TToolManager.SetPhongShapeBorderSize(AValue: integer);
  1309. begin
  1310. if AValue < MinPhongBorderSize then AValue := MinPhongBorderSize;
  1311. if AValue > MaxPhongBorderSize then AValue := MaxPhongBorderSize;
  1312. if FPhongShapeBorderSize=AValue then Exit;
  1313. FPhongShapeBorderSize:=AValue;
  1314. ToolUpdate;
  1315. if Assigned(FOnPhongShapeChanged) then FOnPhongShapeChanged(self);
  1316. end;
  1317. procedure TToolManager.SetPhongShapeKind(AValue: TPhongShapeKind);
  1318. begin
  1319. if FPhongShapeKind=AValue then Exit;
  1320. FPhongShapeKind:=AValue;
  1321. ToolUpdate;
  1322. if Assigned(FOnPhongShapeChanged) then FOnPhongShapeChanged(self);
  1323. end;
  1324. procedure TToolManager.SetShapeOptions(AValue: TShapeOptions);
  1325. begin
  1326. if FShapeOptions=AValue then Exit;
  1327. FShapeOptions:=AValue;
  1328. ToolUpdate;
  1329. if Assigned(FOnShapeOptionChanged) then FOnShapeOptionChanged(self);
  1330. end;
  1331. procedure TToolManager.SetPenStyle(AValue: TPenStyle);
  1332. begin
  1333. if FPenStyle=AValue then Exit;
  1334. FPenStyle:=AValue;
  1335. ToolUpdate;
  1336. if Assigned(FOnPenStyleChanged) then FOnPenStyleChanged(self);
  1337. end;
  1338. procedure TToolManager.SetPenWidth(AValue: single);
  1339. begin
  1340. if AValue < MinPenWidth then AValue := MinPenWidth;
  1341. if AValue > MaxPenWidth then AValue := MaxPenWidth;
  1342. if GetCurrentToolType = ptEraser then
  1343. begin
  1344. if FEraserWidth <> AValue then
  1345. begin
  1346. FEraserWidth := AValue;
  1347. ToolUpdate;
  1348. if Assigned(FOnPenWidthChanged) then FOnPenWidthChanged(self);
  1349. end;
  1350. end else
  1351. begin
  1352. if FNormalPenWidth <> AValue then
  1353. begin
  1354. FNormalPenWidth := AValue;
  1355. ToolUpdate;
  1356. if Assigned(FOnPenWidthChanged) then FOnPenWidthChanged(self);
  1357. end;
  1358. end;
  1359. end;
  1360. procedure TToolManager.SetShapeRatio(AValue: Single);
  1361. begin
  1362. if FShapeRatio=AValue then Exit;
  1363. FShapeRatio:=AValue;
  1364. ToolUpdate;
  1365. if Assigned(FOnShapeRatioChanged) then FOnShapeRatioChanged(self);
  1366. end;
  1367. procedure TToolManager.SetSplineStyle(AValue: TSplineStyle);
  1368. begin
  1369. if FSplineStyle=AValue then Exit;
  1370. FSplineStyle:=AValue;
  1371. ToolUpdate;
  1372. if Assigned(FOnSplineStyleChanged) then FOnSplineStyleChanged(self);
  1373. end;
  1374. procedure TToolManager.SetTextAlign(AValue: TAlignment);
  1375. begin
  1376. if FTextAlign=AValue then Exit;
  1377. FTextAlign:=AValue;
  1378. ToolUpdate;
  1379. if Assigned(FOnTextAlignChanged) then FOnTextAlignChanged(self);
  1380. end;
  1381. procedure TToolManager.SetTextVerticalAlign(AValue: TTextLayout);
  1382. begin
  1383. if FTextVerticalAlign=AValue then Exit;
  1384. FTextVerticalAlign:=AValue;
  1385. ToolUpdate;
  1386. if Assigned(FOnTextAlignChanged) then FOnTextAlignChanged(self);
  1387. end;
  1388. procedure TToolManager.SetTextBidiMode(AValue: TFontBidiMode);
  1389. begin
  1390. if FTextBidiMode=AValue then Exit;
  1391. FTextBidiMode:=AValue;
  1392. ToolUpdate;
  1393. if Assigned(FOnTextAlignChanged) then FOnTextAlignChanged(self);
  1394. end;
  1395. procedure TToolManager.SetTextFontStyle(AValue: TFontStyles);
  1396. begin
  1397. if FTextFontStyle=AValue then Exit;
  1398. FTextFontStyle:=AValue;
  1399. ToolUpdate;
  1400. if Assigned(FOnTextFontChanged) then FOnTextFontChanged(self);
  1401. end;
  1402. procedure TToolManager.SetTextPhong(AValue: boolean);
  1403. begin
  1404. if FTextPhong=AValue then Exit;
  1405. FTextPhong:=AValue;
  1406. ToolUpdate;
  1407. if Assigned(FOnTextPhongChanged) then FOnTextPhongChanged(self);
  1408. end;
  1409. procedure TToolManager.SetTextShadow(AValue: boolean);
  1410. begin
  1411. if FTextShadow=AValue then Exit;
  1412. FTextShadow:=AValue;
  1413. ToolUpdate;
  1414. if Assigned(FOnTextShadowChanged) then FOnTextShadowChanged(self);
  1415. end;
  1416. procedure TToolManager.SetTextShadowBlurRadius(AValue: single);
  1417. begin
  1418. if FTextShadowBlurRadius=AValue then Exit;
  1419. FTextShadowBlurRadius:=AValue;
  1420. ToolUpdate;
  1421. if Assigned(FOnTextShadowChanged) then FOnTextShadowChanged(self);
  1422. end;
  1423. procedure TToolManager.SetTextShadowOffset(AValue: TPoint);
  1424. begin
  1425. if FTextShadowOffset=AValue then Exit;
  1426. FTextShadowOffset:=AValue;
  1427. ToolUpdate;
  1428. if Assigned(FOnTextShadowChanged) then FOnTextShadowChanged(self);
  1429. end;
  1430. procedure TToolManager.SetTolerance(AValue: byte);
  1431. begin
  1432. if FTolerance=AValue then Exit;
  1433. FTolerance:=AValue;
  1434. ToolUpdate;
  1435. if Assigned(FOnToleranceChanged) then FOnToleranceChanged(self);
  1436. end;
  1437. function TToolManager.CheckExitTool: boolean;
  1438. begin
  1439. if FShouldExitTool and not ToolSleeping then
  1440. begin
  1441. FShouldExitTool:= false;
  1442. SetCurrentToolType(FSwitchAfterExitTool);
  1443. result := true;
  1444. end else
  1445. result := false;
  1446. end;
  1447. procedure TToolManager.NotifyImageOrSelectionChanged(ALayer: TBGRABitmap; ARect: TRect);
  1448. begin
  1449. if (CurrentTool <> nil) and not IsRectEmpty(ARect) then
  1450. begin
  1451. if Assigned(CurrentTool.FAction) then
  1452. if not IsOnlyRenderChange(ARect) then
  1453. CurrentTool.FAction.NotifyChange(ALayer, ARect);
  1454. if Assigned(ALayer) then
  1455. begin
  1456. if ALayer = Image.CurrentLayerReadOnly then
  1457. Image.ImageMayChange(AddLayerOffset(ARect))
  1458. else
  1459. Image.LayerMayChange(ALayer, ARect);
  1460. end
  1461. end;
  1462. end;
  1463. function TToolManager.ToolCanBeUsed: boolean;
  1464. begin
  1465. result := (FCurrentToolType = ptHand) or ((CurrentTool <> nil) and (CurrentTool.IsSelectingTool or Image.CurrentLayerVisible));
  1466. end;
  1467. function TToolManager.ToolHasLineCap: boolean;
  1468. var
  1469. contextualToolbars: TContextualToolbars;
  1470. begin
  1471. contextualToolbars := GetContextualToolbars;
  1472. result := (ctLineCap in contextualToolbars) and CurrentTool.HasPen and
  1473. (not (toCloseShape in ShapeOptions) or not (ctCloseShape in contextualToolbars));
  1474. end;
  1475. function TToolManager.GetBackColor: TBGRAPixel;
  1476. begin
  1477. if BlackAndWhite then
  1478. result := BGRAToGrayscale(FBackFill.AverageColor)
  1479. else
  1480. result := FBackFill.AverageColor;
  1481. end;
  1482. function TToolManager.GetBrushAt(AIndex: integer): TLazPaintBrush;
  1483. begin
  1484. if (FBrushInfoList = nil) or (AIndex < 0) or (AIndex >= FBrushInfoList.Count) then
  1485. result := nil
  1486. else
  1487. result := TObject(FBrushInfoList[AIndex]) as TLazPaintBrush;
  1488. end;
  1489. function TToolManager.GetBrushCount: integer;
  1490. begin
  1491. if Assigned(FBrushInfoList) then
  1492. result := FBrushInfoList.Count
  1493. else
  1494. result := 0;
  1495. end;
  1496. function TToolManager.GetBrushInfo: TLazPaintBrush;
  1497. begin
  1498. if (FBrushIndex < 0) or (FBrushIndex > FBrushInfoList.Count) then
  1499. FBrushIndex := 0;
  1500. if FBrushIndex > FBrushInfoList.Count then
  1501. result := nil
  1502. else
  1503. result := TObject(FBrushInfoList[FBrushIndex]) as TLazPaintBrush;
  1504. end;
  1505. function TToolManager.GetCursor: TCursor;
  1506. var toolCursor: TCursor;
  1507. begin
  1508. case GetCurrentToolType of
  1509. ptHand, ptMoveSelection, ptZoomLayer: result := crSizeAll;
  1510. ptRotateSelection,ptRotateLayer: result := crCustomRotate;
  1511. ptPen,ptBrush,ptClone: result := crCustomCrosshair;
  1512. ptRect,ptEllipse,ptSelectRect,ptSelectEllipse: result := crCustomCrosshair;
  1513. ptColorPicker: result := crCustomColorPicker;
  1514. ptFloodFill: result := crCustomFloodfill;
  1515. ptSelectPen: result := crHandPoint;
  1516. ptEraser: result := crDefault;
  1517. else result := crDefault;
  1518. end;
  1519. if CurrentTool <> nil then
  1520. toolCursor := CurrentTool.Cursor
  1521. else
  1522. toolCursor := crDefault;
  1523. if toolCursor <> crDefault then result := toolCursor;
  1524. end;
  1525. procedure TToolManager.FillChange(ASender: TObject;
  1526. var ADiff: TCustomVectorialFillDiff);
  1527. begin
  1528. if FInToolUpdate or FInSwapFill then exit;
  1529. ToolUpdate;
  1530. if Assigned(FOnFillChanged) then FOnFillChanged(self);
  1531. if (ASender = FBackFill) and (FBackFill.FillType = vftGradient) then
  1532. begin
  1533. FBackLastGradient.Free;
  1534. FBackLastGradient := FBackFill.Gradient.Duplicate as TBGRALayerGradientOriginal;
  1535. end else
  1536. if (ASender = FForeFill) and (FForeFill.FillType = vftGradient) then
  1537. begin
  1538. FForeLastGradient.Free;
  1539. FForeLastGradient := FForeFill.Gradient.Duplicate as TBGRALayerGradientOriginal;
  1540. end else
  1541. if (ASender = FOutlineFill) and (FOutlineFill.FillType = vftGradient) then
  1542. begin
  1543. FOutlineLastGradient.Free;
  1544. FOutlineLastGradient := FOutlineFill.Gradient.Duplicate as TBGRALayerGradientOriginal;
  1545. end;
  1546. end;
  1547. function TToolManager.GetAllowedBackFillTypes: TVectorialFillTypes;
  1548. begin
  1549. if Assigned(CurrentTool) then result := CurrentTool.AllowedBackFillTypes
  1550. else result := [vftSolid,vftGradient,vftTexture];
  1551. end;
  1552. function TToolManager.GetAllowedForeFillTypes: TVectorialFillTypes;
  1553. begin
  1554. if Assigned(CurrentTool) then result := CurrentTool.AllowedForeFillTypes
  1555. else result := [vftSolid,vftGradient,vftTexture];
  1556. end;
  1557. function TToolManager.GetAllowedOutlineFillTypes: TVectorialFillTypes;
  1558. begin
  1559. if Assigned(CurrentTool) then result := CurrentTool.AllowedOutlineFillTypes
  1560. else result := [vftSolid,vftGradient,vftTexture];
  1561. end;
  1562. function TToolManager.GetForeColor: TBGRAPixel;
  1563. begin
  1564. if BlackAndWhite then
  1565. result := BGRAToGrayscale(FForeFill.AverageColor)
  1566. else
  1567. result := FForeFill.AverageColor;
  1568. end;
  1569. function TToolManager.GetIsEditingText: boolean;
  1570. begin
  1571. result := Assigned(CurrentTool) and CurrentTool.IsEditingText;
  1572. end;
  1573. function TToolManager.GetMaxDeformationGridSize: TSize;
  1574. begin
  1575. result.cx := Max(MinDeformationGridSize,Min(image.Width div 2,50)+1);
  1576. result.cy := Max(MinDeformationGridSize,Min(image.Height div 2,50)+1);
  1577. end;
  1578. function TToolManager.GetOutlineColor: TBGRAPixel;
  1579. begin
  1580. if BlackAndWhite then
  1581. result := BGRAToGrayscale(FOutlineFill.AverageColor)
  1582. else
  1583. result := FOutlineFill.AverageColor;
  1584. end;
  1585. function TToolManager.GetShapeOptionAliasing: boolean;
  1586. begin
  1587. result := toAliasing in FShapeOptions;
  1588. end;
  1589. function TToolManager.GetPenWidth: single;
  1590. begin
  1591. if GetCurrentToolType = ptEraser then
  1592. result := FEraserWidth else result := FNormalPenWidth;
  1593. end;
  1594. function TToolManager.GetToolSleeping: boolean;
  1595. begin
  1596. result := FSleepingTool <> nil;
  1597. end;
  1598. function TToolManager.GetTextFontName: string;
  1599. begin
  1600. result := FTextFontName;
  1601. end;
  1602. function TToolManager.GetTextFontSize: single;
  1603. begin
  1604. result := FTextFontSize;
  1605. end;
  1606. function TToolManager.GetTextFontStyle: TFontStyles;
  1607. begin
  1608. result := FTextFontStyle;
  1609. end;
  1610. function TToolManager.ScriptGetAliasing(AVars: TVariableSet): TScriptResult;
  1611. begin
  1612. AVars.Booleans['Result'] := toAliasing in ShapeOptions;
  1613. result := srOk;
  1614. end;
  1615. function TToolManager.ScriptGetArrowEnd(AVars: TVariableSet): TScriptResult;
  1616. begin
  1617. AVars.Strings['Result'] := CSSToPascalCase(ArrowKindToStr[ArrowEnd]);
  1618. result := srOk;
  1619. end;
  1620. function TToolManager.ScriptGetArrowSize(AVars: TVariableSet): TScriptResult;
  1621. begin
  1622. AVars.Points2D['Result'] := ArrowSize;
  1623. result := srOk;
  1624. end;
  1625. function TToolManager.ScriptGetArrowStart(AVars: TVariableSet): TScriptResult;
  1626. begin
  1627. AVars.Strings['Result'] := CSSToPascalCase(ArrowKindToStr[ArrowStart]);
  1628. result := srOk;
  1629. end;
  1630. function TToolManager.ScriptGetBackColor(AVars: TVariableSet): TScriptResult;
  1631. begin
  1632. AVars.Pixels['Result'] := BackColor;
  1633. result := srOk;
  1634. end;
  1635. function TToolManager.ScriptGetOutlineColor(AVars: TVariableSet): TScriptResult;
  1636. begin
  1637. AVars.Pixels['Result'] := OutlineColor;
  1638. result := srOk;
  1639. end;
  1640. function TToolManager.ScriptGetBrushCount(AVars: TVariableSet): TScriptResult;
  1641. begin
  1642. AVars.Integers['Result'] := BrushCount;
  1643. result := srOk;
  1644. end;
  1645. function TToolManager.ScriptGetBrushIndex(AVars: TVariableSet): TScriptResult;
  1646. begin
  1647. AVars.Integers['Result'] := BrushIndex;
  1648. result := srOk;
  1649. end;
  1650. function TToolManager.ScriptGetBrushSpacing(AVars: TVariableSet): TScriptResult;
  1651. begin
  1652. AVars.Integers['Result'] := BrushSpacing;
  1653. result := srOk;
  1654. end;
  1655. function TToolManager.ScriptGetDeformationGridMode(AVars: TVariableSet): TScriptResult;
  1656. begin
  1657. result := srOk;
  1658. case DeformationGridMode of
  1659. gmDeform: AVars.Strings['Result'] := 'Deform';
  1660. gmMovePointWithoutDeformation: AVars.Strings['Result'] := 'MovePointWithoutDeformation';
  1661. else result := srException;
  1662. end;
  1663. end;
  1664. function TToolManager.ScriptGetDeformationGridSize(AVars: TVariableSet): TScriptResult;
  1665. begin
  1666. result := srOk;
  1667. with DeformationGridSize do
  1668. AVars.Points2D['Result'] := PointF(cx,cy);
  1669. end;
  1670. function TToolManager.ScriptGetEraserAlpha(AVars: TVariableSet): TScriptResult;
  1671. begin
  1672. AVars.Integers['Result'] := EraserAlpha;
  1673. result := srOk;
  1674. end;
  1675. function TToolManager.ScriptGetEraserMode(AVars: TVariableSet): TScriptResult;
  1676. begin
  1677. result := srOk;
  1678. case EraserMode of
  1679. emEraseAlpha: AVars.Strings['Result'] := 'EraseAlpha';
  1680. emSharpen: AVars.Strings['Result'] := 'Sharpen';
  1681. emSoften: AVars.Strings['Result'] := 'Soften';
  1682. emLighten: AVars.Strings['Result'] := 'Lighten';
  1683. emDarken: AVars.Strings['Result'] := 'Darken';
  1684. else result := srException;
  1685. end;
  1686. end;
  1687. function TToolManager.ScriptGetFloodFillOptions(AVars: TVariableSet): TScriptResult;
  1688. var
  1689. optionsVar: TScriptVariableReference;
  1690. option: TFloodFillOption;
  1691. begin
  1692. optionsVar := AVars.AddStringList('Result');
  1693. for option := low(TFloodFillOption) to high(TFloodFillOption) do
  1694. if option in FloodFillOptions then
  1695. case option of
  1696. ffProgressive: AVars.AppendString(optionsVar, 'Progressive');
  1697. ffFillAll: Avars.AppendString(optionsVar, 'FillAll');
  1698. end;
  1699. result := srOk;
  1700. end;
  1701. function TToolManager.ScriptGetFontName(AVars: TVariableSet): TScriptResult;
  1702. begin
  1703. AVars.Strings['Name'] := TextFontName;
  1704. result := srOk;
  1705. end;
  1706. function TToolManager.ScriptGetFontSize(AVars: TVariableSet): TScriptResult;
  1707. begin
  1708. AVars.Floats['Result'] := TextFontSize;
  1709. result := srOk;
  1710. end;
  1711. function TToolManager.ScriptGetFontStyle(AVars: TVariableSet): TScriptResult;
  1712. var
  1713. styles: TScriptVariableReference;
  1714. style: TFontStyle;
  1715. begin
  1716. styles := AVars.AddStringList('Result');
  1717. for style := low(TFontStyle) to high(TFontStyle) do
  1718. if style in TextFontStyle then
  1719. case style of
  1720. fsBold: AVars.AppendString(styles, 'Bold');
  1721. fsItalic: Avars.AppendString(styles, 'Italic');
  1722. fsUnderline: Avars.AppendString(styles, 'Underline');
  1723. fsStrikeOut: Avars.AppendString(styles, 'StrikeOut');
  1724. end;
  1725. result := srOk;
  1726. end;
  1727. function TToolManager.ScriptGetGradientInterpolation(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  1728. begin
  1729. result := srOk;
  1730. if AFill.FillType <> vftGradient then result := srException else
  1731. AVars.Strings['Result'] := GradientInterpolationToStr(AFill.Gradient.ColorInterpolation);
  1732. end;
  1733. function TToolManager.ScriptGetGradientRepetition(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  1734. begin
  1735. result := srOk;
  1736. if AFill.FillType <> vftGradient then result := srException else
  1737. AVars.Strings['Result'] := GradientRepetitionToStr(AFill.Gradient.Repetition);
  1738. end;
  1739. function TToolManager.ScriptGetGradientType(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  1740. begin
  1741. result := srOk;
  1742. if AFill.FillType <> vftGradient then result := srException else
  1743. AVars.Strings['Result'] := GradientTypeStr[AFill.Gradient.GradientType];
  1744. end;
  1745. function TToolManager.ScriptGetGradientColors(AVars: TVariableSet;
  1746. AFill: TVectorialFill): TScriptResult;
  1747. var
  1748. colors: TScriptVariableReference;
  1749. begin
  1750. result := srOk;
  1751. if AFill.FillType <> vftGradient then result := srException else
  1752. begin
  1753. colors := AVars.AddPixelList('Result');
  1754. TVariableSet.AppendPixel(colors, AFill.Gradient.StartColor);
  1755. TVariableSet.AppendPixel(colors, AFill.Gradient.EndColor);
  1756. end;
  1757. end;
  1758. function TToolManager.ScriptGetBackGradientInterpolation(AVars: TVariableSet): TScriptResult;
  1759. begin
  1760. result := ScriptGetGradientInterpolation(AVars, FBackFill);
  1761. end;
  1762. function TToolManager.ScriptGetBackGradientRepetition(AVars: TVariableSet): TScriptResult;
  1763. begin
  1764. result := ScriptGetGradientRepetition(AVars, FBackFill);
  1765. end;
  1766. function TToolManager.ScriptGetBackGradientType(AVars: TVariableSet): TScriptResult;
  1767. begin
  1768. result := ScriptGetGradientType(AVars, FBackFill);
  1769. end;
  1770. function TToolManager.ScriptGetBackGradientColors(AVars: TVariableSet): TScriptResult;
  1771. begin
  1772. result := ScriptGetGradientColors(AVars, FBackFill);
  1773. end;
  1774. function TToolManager.ScriptGetForeGradientInterpolation(AVars: TVariableSet): TScriptResult;
  1775. begin
  1776. result := ScriptGetGradientInterpolation(AVars, FForeFill);
  1777. end;
  1778. function TToolManager.ScriptGetForeGradientRepetition(AVars: TVariableSet): TScriptResult;
  1779. begin
  1780. result := ScriptGetGradientRepetition(AVars, FForeFill);
  1781. end;
  1782. function TToolManager.ScriptGetForeGradientType(AVars: TVariableSet): TScriptResult;
  1783. begin
  1784. result := ScriptGetGradientType(AVars, FForeFill);
  1785. end;
  1786. function TToolManager.ScriptGetForeGradientColors(AVars: TVariableSet): TScriptResult;
  1787. begin
  1788. result := ScriptGetGradientColors(AVars, FForeFill);
  1789. end;
  1790. function TToolManager.ScriptGetOutlineGradientInterpolation(AVars: TVariableSet): TScriptResult;
  1791. begin
  1792. result := ScriptGetGradientInterpolation(AVars, FOutlineFill);
  1793. end;
  1794. function TToolManager.ScriptGetOutlineGradientRepetition(AVars: TVariableSet): TScriptResult;
  1795. begin
  1796. result := ScriptGetGradientRepetition(AVars, FOutlineFill);
  1797. end;
  1798. function TToolManager.ScriptGetOutlineGradientType(AVars: TVariableSet): TScriptResult;
  1799. begin
  1800. result := ScriptGetGradientType(AVars, FOutlineFill);
  1801. end;
  1802. function TToolManager.ScriptGetOutlineGradientColors(AVars: TVariableSet): TScriptResult;
  1803. begin
  1804. result := ScriptGetGradientColors(AVars, FOutlineFill);
  1805. end;
  1806. function TToolManager.ScriptGetTextureRepetition(AVars: TVariableSet;
  1807. AFill: TVectorialFill): TScriptResult;
  1808. begin
  1809. if AFill.FillType <> vftTexture then exit(srException);
  1810. result := srOk;
  1811. case AFill.TextureRepetition of
  1812. trNone: AVars.Strings['Result'] := 'None';
  1813. trRepeatX: AVars.Strings['Result'] := 'RepeatX';
  1814. trRepeatY: AVars.Strings['Result'] := 'RepeatY';
  1815. trRepeatBoth: AVars.Strings['Result'] := 'RepeatBoth';
  1816. else
  1817. result := srException;
  1818. end;
  1819. end;
  1820. function TToolManager.ScriptGetTextureOpacity(AVars: TVariableSet;
  1821. AFill: TVectorialFill): TScriptResult;
  1822. begin
  1823. if AFill.FillType <> vftTexture then exit(srException);
  1824. AVars.Integers['Result'] := AFill.TextureOpacity;
  1825. result := srOk;
  1826. end;
  1827. function TToolManager.ScriptGetBackTextureRepetition(AVars: TVariableSet): TScriptResult;
  1828. begin
  1829. result := ScriptGetTextureRepetition(AVars, BackFill);
  1830. end;
  1831. function TToolManager.ScriptGetBackTextureOpacity(AVars: TVariableSet): TScriptResult;
  1832. begin
  1833. result := ScriptGetTextureOpacity(AVars, BackFill);
  1834. end;
  1835. function TToolManager.ScriptGetForeTextureRepetition(AVars: TVariableSet): TScriptResult;
  1836. begin
  1837. result := ScriptGetTextureRepetition(AVars, ForeFill);
  1838. end;
  1839. function TToolManager.ScriptGetForeTextureOpacity(AVars: TVariableSet): TScriptResult;
  1840. begin
  1841. result := ScriptGetTextureOpacity(AVars, ForeFill);
  1842. end;
  1843. function TToolManager.ScriptGetOutlineTextureRepetition(AVars: TVariableSet): TScriptResult;
  1844. begin
  1845. result := ScriptGetTextureRepetition(AVars, OutlineFill);
  1846. end;
  1847. function TToolManager.ScriptGetOutlineTextureOpacity(AVars: TVariableSet): TScriptResult;
  1848. begin
  1849. result := ScriptGetTextureOpacity(AVars, OutlineFill);
  1850. end;
  1851. function TToolManager.ScriptGetJoinStyle(AVars: TVariableSet): TScriptResult;
  1852. begin
  1853. result := srOk;
  1854. case JoinStyle of
  1855. pjsBevel: AVars.Strings['Result'] := 'Bevel';
  1856. pjsRound: AVars.Strings['Result'] := 'Round';
  1857. pjsMiter: AVars.Strings['Result'] := 'Miter';
  1858. else result := srException;
  1859. end;
  1860. end;
  1861. function TToolManager.ScriptGetLightPosition(AVars: TVariableSet): TScriptResult;
  1862. begin
  1863. AVars.Points2D['Result'] := LightPosition;
  1864. result := srOk;
  1865. end;
  1866. function TToolManager.ScriptGetLineCap(AVars: TVariableSet): TScriptResult;
  1867. begin
  1868. case LineCap of
  1869. pecSquare: AVars.Strings['Result'] := 'Square';
  1870. pecRound: AVars.Strings['Result'] := 'Round';
  1871. pecFlat: AVars.Strings['Result'] := 'Flat';
  1872. else exit(srException);
  1873. end;
  1874. result := srOk;
  1875. end;
  1876. function TToolManager.ScriptGetForeColor(AVars: TVariableSet): TScriptResult;
  1877. begin
  1878. AVars.Pixels['Result'] := ForeColor;
  1879. result := srOk;
  1880. end;
  1881. function TToolManager.ScriptGetPenStyle(AVars: TVariableSet): TScriptResult;
  1882. begin
  1883. result := srOk;
  1884. case PenStyle of
  1885. psSolid: AVars.Strings['Result'] := 'Solid';
  1886. psDash: AVars.Strings['Result'] := 'Dash';
  1887. psDot: AVars.Strings['Result'] := 'Dot';
  1888. psDashDot: AVars.Strings['Result'] := 'DashDot';
  1889. psDashDotDot: AVars.Strings['Result'] := 'DashDotDot';
  1890. else result := srException;
  1891. end;
  1892. end;
  1893. function TToolManager.ScriptGetPenWidth(AVars: TVariableSet): TScriptResult;
  1894. begin
  1895. AVars.Floats['Result'] := PenWidth;
  1896. result := srOk;
  1897. end;
  1898. function TToolManager.ScriptGetPerspectiveOptions(AVars: TVariableSet): TScriptResult;
  1899. var
  1900. optionsVar: TScriptVariableReference;
  1901. option: TPerspectiveOption;
  1902. begin
  1903. optionsVar := AVars.AddStringList('Result');
  1904. for option := low(TPerspectiveOption) to high(TPerspectiveOption) do
  1905. if option in PerspectiveOptions then
  1906. case option of
  1907. poRepeat: Avars.AppendString(optionsVar, 'Repeat');
  1908. poTwoPlanes: Avars.AppendString(optionsVar, 'TwoPlanes');
  1909. end;
  1910. result := srOk;
  1911. end;
  1912. function TToolManager.ScriptGetPhongShapeAltitude(AVars: TVariableSet): TScriptResult;
  1913. begin
  1914. result := srOk;
  1915. AVars.Integers['Result'] := PhongShapeAltitude;
  1916. end;
  1917. function TToolManager.ScriptGetPhongShapeBorderSize(AVars: TVariableSet): TScriptResult;
  1918. begin
  1919. result := srOk;
  1920. AVars.Integers['Result'] := PhongShapeBorderSize;
  1921. end;
  1922. function TToolManager.ScriptGetPhongShapeKind(AVars: TVariableSet): TScriptResult;
  1923. begin
  1924. result := srOk;
  1925. case PhongShapeKind of
  1926. pskRectangle: AVars.Strings['Result'] := 'Rectangle';
  1927. pskRoundRectangle: AVars.Strings['Result'] := 'RoundRectangle';
  1928. pskHalfSphere: AVars.Strings['Result'] := 'HalfSphere';
  1929. pskConeTop: AVars.Strings['Result'] := 'ConeTop';
  1930. pskConeSide: AVars.Strings['Result'] := 'ConeSide';
  1931. pskHorizCylinder: AVars.Strings['Result'] := 'HorizCylinder';
  1932. pskVertCylinder: AVars.Strings['Result'] := 'VertCylinder';
  1933. else result := srException;
  1934. end;
  1935. end;
  1936. function TToolManager.ScriptGetShapeOptions(AVars: TVariableSet): TScriptResult;
  1937. var
  1938. options: TScriptVariableReference;
  1939. opt: TShapeOption;
  1940. begin
  1941. options := AVars.AddStringList('Result');
  1942. for opt := low(TShapeOption) to high(TShapeOption) do
  1943. if opt in ShapeOptions then
  1944. case opt of
  1945. toDrawShape: Avars.AppendString(options, 'DrawShape');
  1946. toFillShape: Avars.AppendString(options, 'FillShape');
  1947. toCloseShape: Avars.AppendString(options, 'CloseShape');
  1948. end;
  1949. result := srOk;
  1950. end;
  1951. function TToolManager.ScriptGetShapeRatio(AVars: TVariableSet): TScriptResult;
  1952. begin
  1953. AVars.Floats['Result'] := ShapeRatio;
  1954. result := srOk;
  1955. end;
  1956. function TToolManager.ScriptGetSplineStyle(AVars: TVariableSet): TScriptResult;
  1957. var
  1958. s: String;
  1959. begin
  1960. case SplineStyle of
  1961. ssInside: s := 'Inside';
  1962. ssInsideWithEnds: s := 'InsideWithEnds';
  1963. ssCrossing: s := 'Crossing';
  1964. ssCrossingWithEnds: s := 'CrossingWithEnds';
  1965. ssOutside: s := 'Outside';
  1966. ssRoundOutside: s := 'RoundOutside';
  1967. ssVertexToSide: s := 'VertexToSide';
  1968. ssEasyBezier: s := 'EasyBezier';
  1969. else
  1970. exit(srException);
  1971. end;
  1972. AVars.Strings['Result'] := s;
  1973. result := srOk;
  1974. end;
  1975. function TToolManager.ScriptGetTextAlign(AVars: TVariableSet): TScriptResult;
  1976. begin
  1977. case TextAlign of
  1978. taLeftJustify: AVars.Strings['Result'] := 'Left';
  1979. taCenter: AVars.Strings['Result'] := 'Center';
  1980. taRightJustify: AVars.Strings['Result'] := 'Right';
  1981. else exit(srException);
  1982. end;
  1983. result := srOk;
  1984. end;
  1985. function TToolManager.ScriptGetTextVerticalAlign(AVars: TVariableSet): TScriptResult;
  1986. begin
  1987. case TextVerticalAlign of
  1988. tlTop: AVars.Strings['Result'] := 'Top';
  1989. tlCenter: AVars.Strings['Result'] := 'Middle';
  1990. tlBottom: AVars.Strings['Result'] := 'Bottom';
  1991. else exit(srException);
  1992. end;
  1993. result := srOk;
  1994. end;
  1995. function TToolManager.ScriptGetTextBidiMode(AVars: TVariableSet): TScriptResult;
  1996. begin
  1997. case TextBidiMode of
  1998. fbmAuto: AVars.Strings['Result'] := 'BidiAuto';
  1999. fbmLeftToRight: AVars.Strings['Result'] := 'LeftToRight';
  2000. fbmRightToLeft: AVars.Strings['Result'] := 'RightToLeft';
  2001. else exit(srException);
  2002. end;
  2003. result := srOk;
  2004. end;
  2005. function TToolManager.ScriptGetTextOutline(AVars: TVariableSet): TScriptResult;
  2006. begin
  2007. if TextOutline then
  2008. AVars.Floats['Result'] := TextOutlineWidth
  2009. else
  2010. AVars.Floats['Result'] := 0;
  2011. result := srOk;
  2012. end;
  2013. function TToolManager.ScriptGetTextPhong(AVars: TVariableSet): TScriptResult;
  2014. begin
  2015. AVars.Booleans['Result'] := TextPhong;
  2016. result := srOk;
  2017. end;
  2018. function TToolManager.ScriptGetTolerance(AVars: TVariableSet): TScriptResult;
  2019. begin
  2020. AVars.Integers['Result'] := Tolerance;
  2021. result := srOk;
  2022. end;
  2023. function TToolManager.ScriptSetAliasing(AVars: TVariableSet): TScriptResult;
  2024. begin
  2025. if AVars.Booleans['Enabled'] then
  2026. ShapeOptions:= ShapeOptions + [toAliasing]
  2027. else
  2028. ShapeOptions:= ShapeOptions - [toAliasing];
  2029. result := srOk;
  2030. end;
  2031. function TToolManager.ScriptSetArrowEnd(AVars: TVariableSet): TScriptResult;
  2032. var ak: TArrowKind;
  2033. kindStr: String;
  2034. begin
  2035. kindStr := PascalToCSSCase(AVars.Strings['Kind']);
  2036. ak := StrToArrowKind(kindStr);
  2037. if (ak = akNone) and (kindStr <> ArrowKindToStr[akNone]) then
  2038. exit(srInvalidParameters);
  2039. ArrowEnd := ak;
  2040. result := srOk;
  2041. end;
  2042. function TToolManager.ScriptSetArrowSize(AVars: TVariableSet): TScriptResult;
  2043. var
  2044. s: TPointF;
  2045. begin
  2046. s := AVars.Points2D['Size'];
  2047. if isEmptyPointF(s) then exit(srInvalidParameters);
  2048. ArrowSize := s;
  2049. result := srOk;
  2050. end;
  2051. function TToolManager.ScriptSetArrowStart(AVars: TVariableSet): TScriptResult;
  2052. var ak: TArrowKind;
  2053. kindStr: String;
  2054. begin
  2055. kindStr := PascalToCSSCase(AVars.Strings['Kind']);
  2056. ak := StrToArrowKind(kindStr);
  2057. if (ak = akNone) and (kindStr <> ArrowKindToStr[akNone]) then
  2058. exit(srInvalidParameters);
  2059. ArrowStart := ak;
  2060. result := srOk;
  2061. end;
  2062. function TToolManager.ScriptSetBackColor(AVars: TVariableSet): TScriptResult;
  2063. begin
  2064. BackColor := AVars.Pixels['Color'];
  2065. result := srOk;
  2066. end;
  2067. function TToolManager.ScriptSetOutlineColor(AVars: TVariableSet): TScriptResult;
  2068. begin
  2069. OutlineColor := AVars.Pixels['Color'];
  2070. result := srOk;
  2071. end;
  2072. function TToolManager.ScriptSetBrushIndex(AVars: TVariableSet): TScriptResult;
  2073. var
  2074. index: Int64;
  2075. begin
  2076. index := AVars.Integers['Index'];
  2077. if (index < 0) or (index >= BrushCount) then exit(srException);
  2078. BrushIndex:= index;
  2079. result := srOk;
  2080. end;
  2081. function TToolManager.ScriptSetBrushSpacing(AVars: TVariableSet): TScriptResult;
  2082. begin
  2083. BrushSpacing := AVars.Integers['Spacing'];
  2084. result := srOk;
  2085. end;
  2086. function TToolManager.ScriptSetDeformationGridMode(AVars: TVariableSet): TScriptResult;
  2087. begin
  2088. result := srOk;
  2089. case AVars.Strings['Mode'] of
  2090. 'Deform': DeformationGridMode := gmDeform;
  2091. 'MovePointWithoutDeformation': DeformationGridMode := gmMovePointWithoutDeformation;
  2092. else result := srInvalidParameters;
  2093. end;
  2094. end;
  2095. function TToolManager.ScriptSetDeformationGridSize(AVars: TVariableSet): TScriptResult;
  2096. var
  2097. s: TPointF;
  2098. begin
  2099. s := AVars.Points2D['Size'];
  2100. if s.x < MinDeformationGridSize then exit(srInvalidParameters);
  2101. if s.y < MinDeformationGridSize then exit(srInvalidParameters);
  2102. DeformationGridSize := Size(round(s.x),round(s.y));
  2103. result := srOk;
  2104. end;
  2105. function TToolManager.ScriptSetEraserAlpha(AVars: TVariableSet): TScriptResult;
  2106. var
  2107. alpha: Int64;
  2108. begin
  2109. alpha := AVars.Integers['Alpha'];
  2110. if alpha < 0 then alpha := 0;
  2111. if alpha > 255 then alpha := 255;
  2112. EraserAlpha:= alpha;
  2113. result := srOk;
  2114. end;
  2115. function TToolManager.ScriptSetEraserMode(AVars: TVariableSet): TScriptResult;
  2116. begin
  2117. result := srOk;
  2118. case AVars.Strings['Mode'] of
  2119. 'EraseAlpha': EraserMode:= emEraseAlpha;
  2120. 'Soften': EraserMode := emSoften;
  2121. else result := srInvalidParameters;
  2122. end;
  2123. end;
  2124. function TToolManager.ScriptSetFloodFillOptions(AVars: TVariableSet): TScriptResult;
  2125. var optionsSet: TFloodFillOptions;
  2126. optionsVar: TScriptVariableReference;
  2127. i: Integer;
  2128. optionStr: string;
  2129. begin
  2130. optionsSet := [];
  2131. optionsVar := AVars.GetVariable('Options');
  2132. for i := 0 to AVars.GetListCount(optionsVar)-1 do
  2133. begin
  2134. optionStr := AVars.GetStringAt(optionsVar, i);
  2135. case optionStr of
  2136. 'Progressive': include(optionsSet, ffProgressive);
  2137. 'FillAll': include(optionsSet, ffFillAll);
  2138. else exit(srInvalidParameters);
  2139. end;
  2140. end;
  2141. FloodFillOptions:= optionsSet;
  2142. result := srOk;
  2143. end;
  2144. function TToolManager.ScriptSetFontName(AVars: TVariableSet): TScriptResult;
  2145. begin
  2146. SetTextFont(AVars.Strings['Name'], TextFontSize, TextFontStyle);
  2147. result := srOk;
  2148. end;
  2149. function TToolManager.ScriptSetFontSize(AVars: TVariableSet): TScriptResult;
  2150. begin
  2151. SetTextFont(TextFontName, AVars.Floats['Size'], TextFontStyle);
  2152. result := srOk;
  2153. end;
  2154. function TToolManager.ScriptSetFontStyle(AVars: TVariableSet): TScriptResult;
  2155. var style: TFontStyles;
  2156. styles: TScriptVariableReference;
  2157. i: Integer;
  2158. styleStr: string;
  2159. begin
  2160. style := [];
  2161. styles := AVars.GetVariable('Style');
  2162. for i := 0 to AVars.GetListCount(styles)-1 do
  2163. begin
  2164. styleStr := AVars.GetStringAt(styles, i);
  2165. case styleStr of
  2166. 'Bold': include(style, fsBold);
  2167. 'Italic': include(style, fsItalic);
  2168. 'Underline': include(style, fsUnderline);
  2169. 'StrikeOut': include(style, fsStrikeOut);
  2170. else exit(srInvalidParameters);
  2171. end;
  2172. end;
  2173. SetTextFont(TextFontName, TextFontSize, style);
  2174. result := srOk;
  2175. end;
  2176. function TToolManager.ScriptSetGradientInterpolation(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  2177. var
  2178. ci: TBGRAColorInterpolation;
  2179. begin
  2180. if AFill.FillType <> vftGradient then exit(srException);
  2181. result := srOk;
  2182. ci := StrToGradientInterpolation(AVars.Strings['Interpolation']);
  2183. if GradientInterpolationToStr(ci) <> AVars.Strings['Interpolation'] then
  2184. result := srInvalidParameters
  2185. else AFill.Gradient.ColorInterpolation:= ci;
  2186. end;
  2187. function TToolManager.ScriptSetGradientRepetition(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  2188. var
  2189. gr: TBGRAGradientRepetition;
  2190. begin
  2191. if AFill.FillType <> vftGradient then exit(srException);
  2192. result := srOk;
  2193. gr := StrToGradientRepetition(AVars.Strings['Repetition']);
  2194. if GradientRepetitionToStr(gr) <> AVars.Strings['Repetition'] then
  2195. result := srInvalidParameters
  2196. else AFill.Gradient.Repetition:= gr;
  2197. end;
  2198. function TToolManager.ScriptSetGradientType(AVars: TVariableSet; AFill: TVectorialFill): TScriptResult;
  2199. var
  2200. gt: TGradientType;
  2201. b: TAffineBox;
  2202. lastGrad: TBGRALayerGradientOriginal;
  2203. begin
  2204. result := srOk;
  2205. gt := StrToGradientType(AVars.Strings['GradientType']);
  2206. if GradientTypeStr[gt] <> AVars.Strings['GradientType'] then
  2207. exit(srInvalidParameters);
  2208. if AFill.FillType = vftGradient then
  2209. AFill.Gradient.GradientType:= gt
  2210. else
  2211. begin
  2212. if AFill = BackFill then lastGrad := FBackLastGradient
  2213. else if AFill = OutlineFill then lastGrad := FOutlineLastGradient
  2214. else lastGrad := FForeLastGradient;
  2215. lastGrad.GradientType:= gt;
  2216. b := SuggestGradientBox;
  2217. if gt = gtLinear then lastGrad.Origin := b.TopLeft else
  2218. lastGrad.Origin := (b.TopLeft+b.BottomRight)*0.5;
  2219. lastGrad.XAxis := b.BottomRight;
  2220. lastGrad.YAxis := EmptyPointF;
  2221. lastGrad.FocalPoint := EmptyPointF;
  2222. lastGrad.Radius := 1;
  2223. lastGrad.FocalRadius := 0;
  2224. AFill.SetGradient(lastGrad, False);
  2225. end;
  2226. end;
  2227. function TToolManager.ScriptSetGradientColors(AVars: TVariableSet;
  2228. AFill: TVectorialFill): TScriptResult;
  2229. var
  2230. colors: TScriptVariableReference;
  2231. lastGrad: TBGRALayerGradientOriginal;
  2232. b: TAffineBox;
  2233. begin
  2234. result := srOk;
  2235. colors := AVars.GetVariable('Colors');
  2236. if TVariableSet.GetListCount(colors) <> 2 then
  2237. exit(srInvalidParameters);
  2238. if AFill.FillType = vftGradient then
  2239. AFill.Gradient.SetColors(TVariableSet.GetPixelAt(colors, 0), TVariableSet.GetPixelAt(colors, 1))
  2240. else
  2241. begin
  2242. if AFill = BackFill then lastGrad := FBackLastGradient
  2243. else if AFill = OutlineFill then lastGrad := FOutlineLastGradient
  2244. else lastGrad := FForeLastGradient;
  2245. b := SuggestGradientBox;
  2246. if lastGrad.GradientType = gtLinear then lastGrad.Origin := b.TopLeft else
  2247. lastGrad.Origin := (b.TopLeft+b.BottomRight)*0.5;
  2248. lastGrad.XAxis := b.BottomRight;
  2249. lastGrad.YAxis := EmptyPointF;
  2250. lastGrad.FocalPoint := EmptyPointF;
  2251. lastGrad.Radius := 1;
  2252. lastGrad.FocalRadius := 0;
  2253. lastGrad.SetColors(TVariableSet.GetPixelAt(colors, 0), TVariableSet.GetPixelAt(colors, 1));
  2254. AFill.SetGradient(lastGrad, False);
  2255. end;
  2256. end;
  2257. function TToolManager.ScriptSetBackGradientInterpolation(AVars: TVariableSet): TScriptResult;
  2258. begin
  2259. result := ScriptSetGradientInterpolation(AVars, FBackFill);
  2260. end;
  2261. function TToolManager.ScriptSetBackGradientRepetition(AVars: TVariableSet): TScriptResult;
  2262. begin
  2263. result := ScriptSetGradientRepetition(AVars, FBackFill);
  2264. end;
  2265. function TToolManager.ScriptSetBackGradientType(AVars: TVariableSet): TScriptResult;
  2266. begin
  2267. result := ScriptSetGradientType(AVars, FBackFill);
  2268. end;
  2269. function TToolManager.ScriptSetBackGradientColors(AVars: TVariableSet): TScriptResult;
  2270. begin
  2271. result := ScriptSetGradientColors(AVars, FBackFill);
  2272. end;
  2273. function TToolManager.ScriptSetOutlineGradientInterpolation(AVars: TVariableSet): TScriptResult;
  2274. begin
  2275. result := ScriptSetGradientInterpolation(AVars, FOutlineFill);
  2276. end;
  2277. function TToolManager.ScriptSetOutlineGradientRepetition(AVars: TVariableSet): TScriptResult;
  2278. begin
  2279. result := ScriptSetGradientRepetition(AVars, FOutlineFill);
  2280. end;
  2281. function TToolManager.ScriptSetOutlineGradientType(AVars: TVariableSet): TScriptResult;
  2282. begin
  2283. result := ScriptSetGradientType(AVars, FOutlineFill);
  2284. end;
  2285. function TToolManager.ScriptSetOutlineGradientColors(AVars: TVariableSet): TScriptResult;
  2286. begin
  2287. result := ScriptSetGradientColors(AVars, FOutlineFill);
  2288. end;
  2289. function TToolManager.ScriptSetForeGradientInterpolation(AVars: TVariableSet): TScriptResult;
  2290. begin
  2291. result := ScriptSetGradientInterpolation(AVars, FForeFill);
  2292. end;
  2293. function TToolManager.ScriptSetForeGradientRepetition(AVars: TVariableSet): TScriptResult;
  2294. begin
  2295. result := ScriptSetGradientRepetition(AVars, FForeFill);
  2296. end;
  2297. function TToolManager.ScriptSetForeGradientType(AVars: TVariableSet): TScriptResult;
  2298. begin
  2299. result := ScriptSetGradientType(AVars, FForeFill);
  2300. end;
  2301. function TToolManager.ScriptSetForeGradientColors(AVars: TVariableSet): TScriptResult;
  2302. begin
  2303. result := ScriptSetGradientColors(AVars, FForeFill);
  2304. end;
  2305. function TToolManager.ScriptSetTexture(AVars: TVariableSet;
  2306. AFill: TVectorialFill): TScriptResult;
  2307. var
  2308. fileName: String;
  2309. flatImg: TBGRABitmap;
  2310. begin
  2311. fileName := trim(AVars.Strings['FileName']);
  2312. if fileName='' then exit(srInvalidParameters);
  2313. flatImg := LoadFlatImageUTF8(fileName).bmp;
  2314. if flatImg = nil then exit(srException);
  2315. try
  2316. if AFill.FillType <> vftTexture then
  2317. AFill.SetTexture(flatImg, AffineMatrixIdentity)
  2318. else
  2319. AFill.SetTexture(flatImg, AffineMatrixIdentity, AFill.TextureOpacity, AFill.TextureRepetition);
  2320. result := srOk;
  2321. finally
  2322. flatImg.FreeReference;
  2323. end;
  2324. end;
  2325. function TToolManager.ScriptSetTextureRepetition(AVars: TVariableSet;
  2326. AFill: TVectorialFill): TScriptResult;
  2327. begin
  2328. if AFill.FillType <> vftTexture then exit(srException);
  2329. case AVars.Strings['Repetition'] of
  2330. 'None': AFill.TextureRepetition:= trNone;
  2331. 'RepeatX': AFill.TextureRepetition:= trRepeatX;
  2332. 'RepeatY': AFill.TextureRepetition:= trRepeatY;
  2333. 'RepeatBoth': AFill.TextureRepetition:= trRepeatBoth;
  2334. else exit(srInvalidParameters);
  2335. end;
  2336. result := srOk;
  2337. end;
  2338. function TToolManager.ScriptSetTextureOpacity(AVars: TVariableSet;
  2339. AFill: TVectorialFill): TScriptResult;
  2340. begin
  2341. if AFill.FillType <> vftTexture then exit(srException);
  2342. AFill.TextureOpacity := min(255, max(0, AVars.Integers['Opacity']));
  2343. result := srOk;
  2344. end;
  2345. function TToolManager.ScriptSetBackTexture(AVars: TVariableSet): TScriptResult;
  2346. begin
  2347. result := ScriptSetTexture(AVars, BackFill);
  2348. end;
  2349. function TToolManager.ScriptSetBackTextureRepetition(AVars: TVariableSet): TScriptResult;
  2350. begin
  2351. result := ScriptSetTextureRepetition(AVars, BackFill);
  2352. end;
  2353. function TToolManager.ScriptSetBackTextureOpacity(AVars: TVariableSet): TScriptResult;
  2354. begin
  2355. result := ScriptSetTextureOpacity(AVars, BackFill);
  2356. end;
  2357. function TToolManager.ScriptSetForeTexture(AVars: TVariableSet): TScriptResult;
  2358. begin
  2359. result := ScriptSetTexture(AVars, ForeFill);
  2360. end;
  2361. function TToolManager.ScriptSetForeTextureRepetition(AVars: TVariableSet): TScriptResult;
  2362. begin
  2363. result := ScriptSetTextureRepetition(AVars, ForeFill);
  2364. end;
  2365. function TToolManager.ScriptSetForeTextureOpacity(AVars: TVariableSet): TScriptResult;
  2366. begin
  2367. result := ScriptSetTextureOpacity(AVars, ForeFill);
  2368. end;
  2369. function TToolManager.ScriptSetOutlineTexture(AVars: TVariableSet): TScriptResult;
  2370. begin
  2371. result := ScriptSetTexture(AVars, OutlineFill);
  2372. end;
  2373. function TToolManager.ScriptSetOutlineTextureRepetition(AVars: TVariableSet): TScriptResult;
  2374. begin
  2375. result := ScriptSetTextureRepetition(AVars, OutlineFill);
  2376. end;
  2377. function TToolManager.ScriptSetOutlineTextureOpacity(AVars: TVariableSet): TScriptResult;
  2378. begin
  2379. result := ScriptSetTextureOpacity(AVars, OutlineFill);
  2380. end;
  2381. function TToolManager.ScriptSetJoinStyle(AVars: TVariableSet): TScriptResult;
  2382. begin
  2383. result := srOk;
  2384. case AVars.Strings['Style'] of
  2385. 'Bevel': JoinStyle := pjsBevel;
  2386. 'Miter': JoinStyle := pjsMiter;
  2387. 'Round': JoinStyle := pjsRound;
  2388. else result := srInvalidParameters;
  2389. end;
  2390. end;
  2391. function TToolManager.ScriptSetLightPosition(AVars: TVariableSet): TScriptResult;
  2392. var
  2393. ptF: TPointF;
  2394. begin
  2395. ptF := AVars.Points2D['Position'];
  2396. if IsEmptyPointF(ptF) then exit(srInvalidParameters);
  2397. LightPosition := ptF;
  2398. result := srOk;
  2399. end;
  2400. function TToolManager.ScriptSetLineCap(AVars: TVariableSet): TScriptResult;
  2401. var
  2402. capStr: String;
  2403. begin
  2404. capStr := AVars.Strings['Cap'];
  2405. case capStr of
  2406. 'Round': LineCap := pecRound;
  2407. 'Square': LineCap := pecSquare;
  2408. 'Flat': LineCap := pecFlat;
  2409. else exit(srInvalidParameters);
  2410. end;
  2411. result := srOk;
  2412. end;
  2413. function TToolManager.ScriptSetForeColor(AVars: TVariableSet): TScriptResult;
  2414. begin
  2415. ForeColor := AVars.Pixels['Color'];
  2416. ToolUpdate;
  2417. result := srOk;
  2418. end;
  2419. function TToolManager.ScriptSetPenStyle(AVars: TVariableSet): TScriptResult;
  2420. begin
  2421. result := srOk;
  2422. case AVars.Strings['Style'] of
  2423. 'Solid': PenStyle := psSolid;
  2424. 'Dash': PenStyle := psDash;
  2425. 'Dot': PenStyle := psDot;
  2426. 'DashDot': PenStyle := psDashDot;
  2427. 'DashDotDot': PenStyle := psDashDotDot;
  2428. else result := srInvalidParameters;
  2429. end;
  2430. end;
  2431. function TToolManager.ScriptSetPenWidth(AVars: TVariableSet): TScriptResult;
  2432. begin
  2433. PenWidth:= AVars.Floats['Width'];
  2434. result := srOk;
  2435. end;
  2436. function TToolManager.ScriptSetPerspectiveOptions(AVars: TVariableSet): TScriptResult;
  2437. var optionsSet: TPerspectiveOptions;
  2438. optionsVar: TScriptVariableReference;
  2439. i: Integer;
  2440. optionStr: string;
  2441. begin
  2442. optionsSet := [];
  2443. optionsVar := AVars.GetVariable('Options');
  2444. for i := 0 to AVars.GetListCount(optionsVar)-1 do
  2445. begin
  2446. optionStr := AVars.GetStringAt(optionsVar, i);
  2447. case optionStr of
  2448. 'Repeat': include(optionsSet, poRepeat);
  2449. 'TwoPlanes': include(optionsSet, poTwoPlanes);
  2450. else exit(srInvalidParameters);
  2451. end;
  2452. end;
  2453. PerspectiveOptions := optionsSet;
  2454. result := srOk;
  2455. end;
  2456. function TToolManager.ScriptSetPhongShapeAltitude(AVars: TVariableSet): TScriptResult;
  2457. begin
  2458. if (AVars.Floats['Size'] < MinPhongShapeAltitude) or
  2459. (AVars.Floats['Size'] > MaxPhongShapeAltitude) then exit(srInvalidParameters);
  2460. result := srOk;
  2461. PhongShapeAltitude := AVars.Integers['Altitude'];
  2462. end;
  2463. function TToolManager.ScriptSetPhongShapeBorderSize(AVars: TVariableSet): TScriptResult;
  2464. begin
  2465. if (AVars.Floats['Size'] < MinPhongBorderSize) or
  2466. (AVars.Floats['Size'] > MaxPhongBorderSize) then exit(srInvalidParameters);
  2467. result := srOk;
  2468. PhongShapeBorderSize := AVars.Integers['Size'];
  2469. end;
  2470. function TToolManager.ScriptSetPhongShapeKind(AVars: TVariableSet): TScriptResult;
  2471. begin
  2472. result := srOk;
  2473. case AVars.Strings['Kind'] of
  2474. 'Rectangle': PhongShapeKind := pskRectangle;
  2475. 'RoundRectangle': PhongShapeKind := pskRoundRectangle;
  2476. 'HalfSphere': PhongShapeKind := pskHalfSphere;
  2477. 'ConeTop': PhongShapeKind := pskConeTop;
  2478. 'ConeSide': PhongShapeKind := pskConeSide;
  2479. 'HorizCylinder': PhongShapeKind := pskHorizCylinder;
  2480. 'VertCylinder': PhongShapeKind := pskVertCylinder;
  2481. else result := srInvalidParameters;
  2482. end;
  2483. end;
  2484. function TToolManager.ScriptSetShapeOptions(AVars: TVariableSet): TScriptResult;
  2485. var so: TShapeOptions;
  2486. options: TScriptVariableReference;
  2487. i: Integer;
  2488. opt: String;
  2489. begin
  2490. so := [];
  2491. if toAliasing in ShapeOptions then include(so, toAliasing);
  2492. options := AVars.GetVariable('Options');
  2493. for i := 0 to AVars.GetListCount(options)-1 do
  2494. begin
  2495. opt := AVars.GetStringAt(options, i);
  2496. case opt of
  2497. 'DrawShape': include(so, toDrawShape);
  2498. 'FillShape': include(so, toFillShape);
  2499. 'CloseShape': include(so, toCloseShape);
  2500. else exit(srInvalidParameters);
  2501. end;
  2502. end;
  2503. if [toDrawShape,toFillShape]*so = [] then
  2504. so := so + [toDrawShape,toFillShape]*ShapeOptions;
  2505. ShapeOptions := so;
  2506. result := srOk;
  2507. end;
  2508. function TToolManager.ScriptSetShapeRatio(AVars: TVariableSet): TScriptResult;
  2509. var
  2510. ratio: single;
  2511. begin
  2512. ratio := AVars.Floats['Ratio'];
  2513. if ratio <= 0 then result := srException else
  2514. begin
  2515. ShapeRatio := ratio;
  2516. result := srOk;
  2517. end;
  2518. end;
  2519. function TToolManager.ScriptSetSplineStyle(AVars: TVariableSet): TScriptResult;
  2520. var
  2521. s: TSplineStyle;
  2522. begin
  2523. case AVars.Strings['Style'] of
  2524. 'Inside': s := ssInside;
  2525. 'InsideWithEnds': s := ssInsideWithEnds;
  2526. 'Crossing': s := ssCrossing;
  2527. 'CrossingWithEnds': s := ssCrossingWithEnds;
  2528. 'Outside': s := ssOutside;
  2529. 'RoundOutside': s := ssRoundOutside;
  2530. 'VertexToSide': s := ssVertexToSide;
  2531. 'EasyBezier': s := ssEasyBezier;
  2532. else
  2533. exit(srInvalidParameters);
  2534. end;
  2535. SplineStyle := s;
  2536. result := srOk;
  2537. end;
  2538. function TToolManager.ScriptSetTextAlign(AVars: TVariableSet): TScriptResult;
  2539. begin
  2540. case AVars.Strings['Align'] of
  2541. 'Left': TextAlign:= taLeftJustify;
  2542. 'Center': TextAlign:= taCenter;
  2543. 'Right': TextAlign:= taRightJustify;
  2544. else exit(srInvalidParameters);
  2545. end;
  2546. result := srOk;
  2547. end;
  2548. function TToolManager.ScriptSetTextVerticalAlign(AVars: TVariableSet): TScriptResult;
  2549. begin
  2550. case AVars.Strings['Align'] of
  2551. 'Top': TextVerticalAlign:= tlTop;
  2552. 'Middle': TextVerticalAlign:= tlCenter;
  2553. 'Bottom': TextVerticalAlign:= tlBottom;
  2554. else exit(srInvalidParameters);
  2555. end;
  2556. result := srOk;
  2557. end;
  2558. function TToolManager.ScriptSetTextBidiMode(AVars: TVariableSet): TScriptResult;
  2559. begin
  2560. case AVars.Strings['BidiMode'] of
  2561. 'BidiAuto': TextBidiMode:= fbmAuto;
  2562. 'LeftToRight': TextBidiMode:= fbmLeftToRight;
  2563. 'RightToLeft': TextBidiMode:= fbmRightToLeft;
  2564. else exit(srInvalidParameters);
  2565. end;
  2566. result := srOk;
  2567. end;
  2568. function TToolManager.ScriptSetTextOutline(AVars: TVariableSet): TScriptResult;
  2569. begin
  2570. if AVars.IsDefined('Width') and (AVars.Floats['Width'] > 0) then
  2571. SetTextOutline(true, AVars.Floats['Width'])
  2572. else
  2573. SetTextOutline(false, TextOutlineWidth);
  2574. result := srOk;
  2575. end;
  2576. function TToolManager.ScriptSetTextPhong(AVars: TVariableSet): TScriptResult;
  2577. begin
  2578. TextPhong:= AVars.Booleans['Enabled'];
  2579. result := srOk;
  2580. end;
  2581. function TToolManager.ScriptSetTolerance(AVars: TVariableSet): TScriptResult;
  2582. var
  2583. alpha: Int64;
  2584. begin
  2585. alpha := AVars.Integers['Tolerance'];
  2586. if alpha < 0 then alpha := 0;
  2587. if alpha > 255 then alpha := 255;
  2588. Tolerance:= alpha;
  2589. result := srOk;
  2590. end;
  2591. procedure TToolManager.SetBrushIndex(AValue: integer);
  2592. begin
  2593. if FBrushIndex=AValue then Exit;
  2594. FBrushIndex:=AValue;
  2595. ToolUpdate;
  2596. if Assigned(FOnBrushChanged) then FOnBrushChanged(self);
  2597. end;
  2598. procedure TToolManager.SetBrushSpacing(AValue: integer);
  2599. begin
  2600. if AValue < 0 then AValue := 0;
  2601. if AValue > MaxBrushSpacing then AValue := MaxBrushSpacing;
  2602. if FBrushSpacing=AValue then Exit;
  2603. FBrushSpacing:=AValue;
  2604. ToolUpdate;
  2605. if Assigned(FOnBrushChanged) then FOnBrushChanged(self);
  2606. end;
  2607. constructor TToolManager.Create(AImage: TLazPaintImage; AConfigProvider: IConfigProvider;
  2608. ABitmapToVirtualScreen: TBitmapToVirtualScreenFunction;
  2609. ABlackAndWhite : boolean; AScriptContext: TScriptContext);
  2610. begin
  2611. FImage:= AImage;
  2612. CanvasScale := 1;
  2613. PenWidthVisible := false;
  2614. BitmapToVirtualScreen := ABitmapToVirtualScreen;
  2615. FShouldExitTool:= false;
  2616. FConfigProvider := AConfigProvider;
  2617. FBlackAndWhite := ABlackAndWhite;
  2618. FScriptContext := AScriptContext;
  2619. RegisterScriptFunctions(True);
  2620. FForeFill := TVectorialFill.Create;
  2621. FForeFill.TransparentMode := tmAlphaZeroOnly;
  2622. FForeFill.SolidColor := BGRABlack;
  2623. FForeFill.OnChange:= @FillChange;
  2624. FForeLastGradient:= TBGRALayerGradientOriginal.Create;
  2625. FForeLastGradient.ColorInterpolation:= ciLinearRGB;
  2626. FBackFill := TVectorialFill.Create;
  2627. FBackFill.TransparentMode := tmAlphaZeroOnly;
  2628. FBackFill.SolidColor := CSSSkyBlue;
  2629. FBackFill.OnChange:= @FillChange;
  2630. FBackLastGradient:= TBGRALayerGradientOriginal.Create;
  2631. FBackLastGradient.ColorInterpolation:= ciLinearRGB;
  2632. FOutlineFill := TVectorialFill.Create;
  2633. FOutlineFill.TransparentMode := tmAlphaZeroOnly;
  2634. FOutlineFill.SolidColor := CSSRed;
  2635. FOutlineFill.OnChange:= @FillChange;
  2636. FOutlineLastGradient:= TBGRALayerGradientOriginal.Create;
  2637. FOutlineLastGradient.ColorInterpolation:= ciLinearRGB;
  2638. FNormalPenWidth := 5;
  2639. FEraserWidth := 10;
  2640. FEraserAlpha := 255;
  2641. FEraserMode := emEraseAlpha;
  2642. ReloadBrushes;
  2643. FBrushSpacing := 1;
  2644. FShapeOptions := [toDrawShape, toFillShape, toCloseShape];
  2645. FPenStyle := psSolid;
  2646. FLineCap := pecRound;
  2647. FJoinStyle := pjsRound;
  2648. FArrowStart := akNone;
  2649. FArrowEnd := akNone;
  2650. FArrowSize := PointF(2,2);
  2651. FSplineStyle := ssEasyBezier;
  2652. FFloodFillOptions := [ffProgressive];
  2653. FTolerance := 64;
  2654. FTextOutline := False;
  2655. FTextOutlineWidth := 2;
  2656. FTextShadow := false;
  2657. FTextFontSize := 10;
  2658. FTextFontName := TTextShape.DefaultFontName;
  2659. FTextFontStyle:= [];
  2660. FTextAlign := taLeftJustify;
  2661. FTextVerticalAlign := tlTop;
  2662. FTextBidiMode := fbmAuto;
  2663. FTextPhong := False;
  2664. FTextShadowBlurRadius := 4;
  2665. FTextShadowOffset := Point(5,5);
  2666. FLightPosition := PointF(0,0);
  2667. FLightAltitude := 100;
  2668. FPhongShapeKind := pskRectangle;
  2669. FPhongShapeAltitude := 50;
  2670. FPhongShapeBorderSize := 20;
  2671. FPerspectiveOptions:= [];
  2672. FDeformationGridNbX := 5;
  2673. FDeformationGridNbY := 5;
  2674. FDeformationGridMode := gmDeform;
  2675. PenWidthControls := TList.Create;
  2676. AliasingControls := TList.Create;
  2677. ShapeControls := TList.Create;
  2678. PenStyleControls := TList.Create;
  2679. CloseShapeControls := TList.Create;
  2680. LineCapControls := TList.Create;
  2681. JoinStyleControls := TList.Create;
  2682. SplineStyleControls := TList.Create;
  2683. EraserControls := TList.Create;
  2684. ToleranceControls := TList.Create;
  2685. DeformationControls := TList.Create;
  2686. TextControls := TList.Create;
  2687. TextShadowControls := TList.Create;
  2688. PhongControls := TList.Create;
  2689. AltitudeControls := TList.Create;
  2690. PerspectiveControls := TList.Create;
  2691. FillControls := TList.Create;
  2692. OutlineFillControls := TList.Create;
  2693. BrushControls := TList.Create;
  2694. RatioControls := TList.Create;
  2695. DonateControls := TList.Create;
  2696. FCurrentToolType := ptHand;
  2697. FCurrentTool := PaintTools[ptHand].Create(Self);
  2698. end;
  2699. destructor TToolManager.Destroy;
  2700. var
  2701. i: Integer;
  2702. begin
  2703. SaveBrushes;
  2704. CurrentTool.Free;
  2705. PenWidthControls.Free;
  2706. AliasingControls.Free;
  2707. ShapeControls.Free;
  2708. PenStyleControls.Free;
  2709. CloseShapeControls.Free;
  2710. LineCapControls.Free;
  2711. JoinStyleControls.Free;
  2712. SplineStyleControls.Free;
  2713. EraserControls.Free;
  2714. ToleranceControls.Free;
  2715. DeformationControls.Free;
  2716. TextControls.Free;
  2717. TextShadowControls.Free;
  2718. PhongControls.Free;
  2719. AltitudeControls.Free;
  2720. PerspectiveControls.Free;
  2721. FillControls.Free;
  2722. OutlineFillControls.Free;
  2723. BrushControls.Free;
  2724. RatioControls.Free;
  2725. DonateControls.Free;
  2726. for i := 0 to BrushCount do
  2727. BrushAt[i].Free;
  2728. FBrushInfoList.Free;
  2729. FForeFill.Free;
  2730. FBackFill.Free;
  2731. FOutlineFill.Free;
  2732. FForeLastGradient.Free;
  2733. FBackLastGradient.Free;
  2734. FOutlineLastGradient.Free;
  2735. RegisterScriptFunctions(False);
  2736. inherited Destroy;
  2737. end;
  2738. procedure TToolManager.LoadFromConfig;
  2739. var
  2740. Config: TLazPaintConfig;
  2741. opt: TShapeOptions;
  2742. begin
  2743. if Assigned(FConfigProvider) then
  2744. Config := FConfigProvider.GetConfig
  2745. else
  2746. exit;
  2747. ForeColor := Config.DefaultToolForeColor;
  2748. BackColor := Config.DefaultToolBackColor;
  2749. OutlineColor := Config.DefaultToolOutlineColor;
  2750. AssignGradientFromConfigStr(FForeLastGradient, Config.DefaultToolForeGradient);
  2751. AssignGradientFromConfigStr(FBackLastGradient, Config.DefaultToolBackGradient);
  2752. AssignGradientFromConfigStr(FOutlineLastGradient, Config.DefaultToolOutlineGradient);
  2753. FNormalPenWidth := Config.DefaultToolPenWidth;
  2754. FEraserWidth := Config.DefaultToolEraserWidth;
  2755. if Assigned(FOnPenWidthChanged) then FOnPenWidthChanged(self);
  2756. ReloadBrushes;
  2757. opt := [];
  2758. if Config.DefaultToolOptionDrawShape then include(opt, toDrawShape);
  2759. if Config.DefaultToolOptionFillShape then include(opt, toFillShape);
  2760. if Config.DefaultToolOptionCloseShape then include(opt, toCloseShape);
  2761. ShapeOptions:= opt;
  2762. Tolerance := Config.DefaultToolTolerance;
  2763. //TextShadow := Config.DefaultToolTextShadow;
  2764. SetTextOutline(Config.DefaultToolTextOutline, Config.DefaultToolTextOutlineWidth);
  2765. TextPhong := Config.DefaultToolTextPhong;
  2766. with Config.DefaultToolTextFont do
  2767. SetTextFont(Name, Size, Style);
  2768. TextShadowBlurRadius := Config.DefaultToolTextBlur;
  2769. TextShadowOffset := Config.DefaultToolTextShadowOffset;
  2770. LightPosition := Config.DefaultToolLightPosition;
  2771. LightAltitude := Config.DefaultToolLightAltitude;
  2772. PhongShapeAltitude := Config.DefaultToolShapeAltitude;
  2773. PhongShapeBorderSize := Config.DefaultToolShapeBorderSize;
  2774. PhongShapeKind := Config.DefaultToolShapeType;
  2775. end;
  2776. procedure TToolManager.SaveToConfig;
  2777. var
  2778. Config: TLazPaintConfig;
  2779. begin
  2780. if Assigned(FConfigProvider) then
  2781. Config := FConfigProvider.GetConfig
  2782. else
  2783. exit;
  2784. if ForeFill.FillType = vftSolid then Config.SetDefaultToolForeColor(ForeColor);
  2785. if BackFill.FillType = vftSolid then Config.SetDefaultToolBackColor(BackColor);
  2786. if OutlineFill.FillType = vftSolid then Config.SetDefaultToolOutlineColor(OutlineColor);
  2787. Config.SetDefaultToolForeGradient(GradientToConfigStr(FForeLastGradient));
  2788. Config.SetDefaultToolBackGradient(GradientToConfigStr(FBackLastGradient));
  2789. Config.SetDefaultToolOutlineGradient(GradientToConfigStr(FOutlineLastGradient));
  2790. Config.SetDefaultToolPenWidth(FNormalPenWidth);
  2791. Config.SetDefaultToolEraserWidth(FEraserWidth);
  2792. Config.SetDefaultToolOptionDrawShape(toDrawShape in ShapeOptions);
  2793. Config.SetDefaultToolOptionFillShape(toFillShape in ShapeOptions);
  2794. Config.SetDefaultToolOptionCloseShape(toCloseShape in ShapeOptions);
  2795. Config.SetDefaultToolTolerance(Tolerance);
  2796. Config.SetDefaultToolTextFont(FTextFontName, FTextFontSize, FTextFontStyle);
  2797. Config.SetDefaultToolTextShadow(TextShadow);
  2798. Config.SetDefaultToolTextOutline(TextOutline);
  2799. Config.SetDefaultToolTextOutlineWidth(TextOutlineWidth);
  2800. Config.SetDefaultToolTextBlur(TextShadowBlurRadius);
  2801. Config.SetDefaultToolTextShadowOffset(TextShadowOffset);
  2802. Config.SetDefaultToolTextPhong(TextPhong);
  2803. Config.SetDefaultToolLightPosition(LightPosition);
  2804. Config.SetDefaultToolLightAltitude(LightAltitude);
  2805. Config.SetDefaultToolShapeBorderSize(PhongShapeBorderSize);
  2806. Config.SetDefaultToolShapeAltitude(PhongShapeAltitude);
  2807. Config.SetDefaultToolShapeType(PhongShapeKind);
  2808. end;
  2809. procedure TToolManager.ReloadBrushes;
  2810. var
  2811. i: Integer;
  2812. bi: TLazPaintBrush;
  2813. s: TStream;
  2814. begin
  2815. If Assigned(FBrushInfoList) then
  2816. begin
  2817. for i := 0 to FBrushInfoList.Count-1 do
  2818. TObject(FBrushInfoList[i]).Free;
  2819. FBrushInfoList.Clear;
  2820. end else
  2821. FBrushInfoList := TList.Create;
  2822. if Assigned(FConfigProvider) and (FConfigProvider.GetConfig <> nil) then
  2823. begin
  2824. for i := 0 to FConfigProvider.GetConfig.BrushCount-1 do
  2825. begin
  2826. bi := TLazPaintBrush.Create;
  2827. try
  2828. bi.AsString := FConfigProvider.GetConfig.BrushInfo[i];
  2829. except
  2830. continue;
  2831. end;
  2832. FBrushInfoList.Add(bi);
  2833. end;
  2834. end;
  2835. if FBrushInfoList.Count = 0 then
  2836. FBrushInfoList.Add(TLazPaintBrush.Create(0,True));
  2837. if (FBrushInfoList.Count = 1) and (BrushAt[0].Stream64 = '') then
  2838. begin
  2839. s := BGRAResource.GetResourceStream('defaultbrush.lzp');
  2840. FBrushInfoList.Add(TLazPaintBrush.CreateFromStream(s));
  2841. s.Free;
  2842. end;
  2843. FBrushInfoListChanged := false;
  2844. end;
  2845. procedure TToolManager.SaveBrushes;
  2846. var
  2847. i: Integer;
  2848. infos: TStringList;
  2849. begin
  2850. if Assigned(FConfigProvider) and FBrushInfoListChanged then
  2851. begin
  2852. infos := TStringList.Create;
  2853. try
  2854. for i := 0 to BrushCount-1 do
  2855. infos.Add(BrushAt[i].AsString);
  2856. FConfigProvider.GetConfig.SetBrushes(infos);
  2857. except
  2858. end;
  2859. infos.Free;
  2860. end;
  2861. FBrushInfoListChanged := false;
  2862. end;
  2863. function TToolManager.ApplyPressure(AColor: TBGRAPixel): TBGRAPixel;
  2864. var alpha: integer;
  2865. begin
  2866. alpha := round(AColor.alpha*FToolPressure);
  2867. if alpha <= 0 then
  2868. result := BGRAPixelTransparent
  2869. else if alpha >= 255 then
  2870. result := AColor
  2871. else
  2872. begin
  2873. result := AColor;
  2874. result.alpha := alpha;
  2875. end;
  2876. end;
  2877. function TToolManager.ApplyPressure(AOpacity: byte): byte;
  2878. begin
  2879. result := round(AOpacity*FToolPressure);
  2880. end;
  2881. procedure TToolManager.SetPressure(APressure: single);
  2882. begin
  2883. if APressure <= 0 then
  2884. FToolPressure := 0
  2885. else if APressure >= 1 then
  2886. FToolPressure := 1
  2887. else
  2888. FToolPressure:= APressure;
  2889. end;
  2890. function TToolManager.GetPressureB: Byte;
  2891. begin
  2892. result := round(FToolPressure*255);
  2893. end;
  2894. procedure TToolManager.StepPenSize(ADecrease: boolean);
  2895. function SizeDelta: single;
  2896. var v: single;
  2897. begin
  2898. v := PenWidth;
  2899. if ADecrease then v := v - 0.1;
  2900. if v < 10 then result := 1 else
  2901. if v < 20 then result := 2 else
  2902. if v < 50 then result := 5 else
  2903. if v < 100 then result := 10 else
  2904. if v < 200 then result := 20 else
  2905. if v < 500 then result := 50 else
  2906. result := 100;
  2907. end;
  2908. begin
  2909. if ADecrease then
  2910. PenWidth := PenWidth - SizeDelta
  2911. else PenWidth := PenWidth + SizeDelta;
  2912. end;
  2913. procedure TToolManager.InternalSetCurrentToolType(tool: TPaintToolType);
  2914. begin
  2915. if (tool <> FCurrentToolType) or (FCurrentTool=nil) then
  2916. begin
  2917. FreeAndNil(FCurrentTool);
  2918. if PaintTools[tool] <> nil then
  2919. FCurrentTool := PaintTools[tool].Create(self)
  2920. else
  2921. FCurrentTool := nil;
  2922. FCurrentToolType:= tool;
  2923. if not IsSelectingTool then
  2924. Image.ReleaseEmptySelection;
  2925. UpdateContextualToolbars;
  2926. If Assigned(FOnToolChangedHandler) then
  2927. FOnToolChangedHandler(self, FCurrentToolType);
  2928. If Assigned(FOnToolRenderChanged) then
  2929. FOnToolRenderChanged(self);
  2930. end;
  2931. FShouldExitTool:= false;
  2932. end;
  2933. function TToolManager.UpdateContextualToolbars: boolean;
  2934. var
  2935. contextualToolbars: TContextualToolbars;
  2936. hasPen: Boolean;
  2937. procedure OrResult(AValue: boolean);
  2938. begin
  2939. if AValue then result := true;
  2940. end;
  2941. begin
  2942. result := false;
  2943. contextualToolbars := GetContextualToolbars;
  2944. if Assigned(FCurrentTool) then
  2945. hasPen := FCurrentTool.HasPen
  2946. else hasPen := false;
  2947. if (ctBackFill in contextualToolbars) and not (ctPenFill in contextualToolbars) then
  2948. OrResult(SetControlsVisible(FillControls, True, 'Panel_BackFill')) else
  2949. if (ctPenFill in contextualToolbars) and not (ctBackFill in contextualToolbars) then
  2950. OrResult(SetControlsVisible(FillControls, True, 'Panel_PenFill'))
  2951. else
  2952. OrResult(SetControlsVisible(FillControls, (ctPenFill in contextualToolbars) and (ctBackFill in contextualToolbars)));
  2953. OrResult(SetControlsVisible(BrushControls, ctBrush in contextualToolbars));
  2954. OrResult(SetControlsVisible(ShapeControls, ctShape in contextualToolbars));
  2955. PenWidthVisible := (ctPenWidth in contextualToolbars) and hasPen;
  2956. OrResult(SetControlsVisible(PenWidthControls, PenWidthVisible));
  2957. OrResult(SetControlsVisible(JoinStyleControls, (ctJoinStyle in contextualToolbars) and hasPen));
  2958. OrResult(SetControlsVisible(PenStyleControls, (ctPenStyle in contextualToolbars) and hasPen));
  2959. OrResult(SetControlsVisible(CloseShapeControls, ctCloseShape in contextualToolbars));
  2960. OrResult(SetControlsVisible(LineCapControls, ToolHasLineCap));
  2961. OrResult(SetControlsVisible(AliasingControls, ctAliasing in contextualToolbars));
  2962. OrResult(SetControlsVisible(SplineStyleControls, ctSplineStyle in contextualToolbars));
  2963. OrResult(SetControlsVisible(EraserControls, ctEraserOption in contextualToolbars));
  2964. OrResult(SetControlsVisible(ToleranceControls, ctTolerance in contextualToolbars));
  2965. OrResult(SetControlsVisible(DeformationControls, ctDeformation in contextualToolbars));
  2966. if (ctText in contextualToolbars) and not (ctOutlineWidth in contextualToolbars) then
  2967. OrResult(SetControlsVisible(TextControls, True, 'Panel_Text')) else
  2968. if (ctOutlineWidth in contextualToolbars) and not (ctText in contextualToolbars) then
  2969. OrResult(SetControlsVisible(TextControls, True, 'Panel_TextOutline'))
  2970. else
  2971. OrResult(SetControlsVisible(TextControls, (ctText in contextualToolbars) and (ctOutlineWidth in contextualToolbars)));
  2972. OrResult(SetControlsVisible(OutlineFillControls, ctOutlineFill in contextualToolbars));
  2973. OrResult(SetControlsVisible(TextShadowControls, ctTextShadow in contextualToolbars));
  2974. OrResult(SetControlsVisible(PhongControls, ctPhong in contextualToolbars));
  2975. OrResult(SetControlsVisible(AltitudeControls, ctAltitude in contextualToolbars));
  2976. OrResult(SetControlsVisible(PerspectiveControls, ctPerspective in contextualToolbars));
  2977. OrResult(SetControlsVisible(RatioControls, ctRatio in contextualToolbars));
  2978. OrResult(SetControlsVisible(DonateControls, FCurrentToolType = ptHand));
  2979. if result and Assigned(FOnToolbarChanged) then FOnToolbarChanged(self);
  2980. end;
  2981. function TToolManager.GetContextualToolbars: TContextualToolbars;
  2982. begin
  2983. if Assigned(FCurrentTool) then
  2984. result := FCurrentTool.GetContextualToolbars
  2985. else
  2986. result := [ctPenFill, ctBackFill];
  2987. end;
  2988. function TToolManager.InternalBitmapToVirtualScreen(PtF: TPointF): TPointF;
  2989. begin
  2990. if Assigned(FCurrentTool) then
  2991. begin
  2992. ptF.x += FCurrentTool.LayerOffset.X;
  2993. ptF.y += FCurrentTool.LayerOffset.Y;
  2994. end;
  2995. result := BitmapToVirtualScreen(ptF);
  2996. end;
  2997. function TToolManager.AddLayerOffset(ARect: TRect): TRect;
  2998. begin
  2999. result := ARect;
  3000. if (result.Left = OnlyRenderChange.Left) and
  3001. (result.Top = OnlyRenderChange.Top) and
  3002. (result.Right = OnlyRenderChange.Right) and
  3003. (result.Bottom = OnlyRenderChange.Bottom) then exit;
  3004. if Assigned(FCurrentTool) then
  3005. OffsetRect(result, FCurrentTool.LayerOffset.X,FCurrentTool.LayerOffset.Y);
  3006. end;
  3007. procedure TToolManager.RegisterScriptFunctions(ARegister: boolean);
  3008. begin
  3009. if not Assigned(FScriptContext) then exit;
  3010. FScriptContext.RegisterScriptFunction('ToolSetForeColor', @ScriptSetForeColor, ARegister);
  3011. FScriptContext.RegisterScriptFunction('ToolGetForeColor', @ScriptGetForeColor, ARegister);
  3012. FScriptContext.RegisterScriptFunction('ToolSetBackColor', @ScriptSetBackColor, ARegister);
  3013. FScriptContext.RegisterScriptFunction('ToolGetBackColor', @ScriptGetBackColor, ARegister);
  3014. FScriptContext.RegisterScriptFunction('ToolSetOutlineColor', @ScriptSetOutlineColor, ARegister);
  3015. FScriptContext.RegisterScriptFunction('ToolGetOutlineColor', @ScriptGetOutlineColor, ARegister);
  3016. FScriptContext.RegisterScriptFunction('ToolSetEraserMode', @ScriptSetEraserMode, ARegister);
  3017. FScriptContext.RegisterScriptFunction('ToolGetEraserMode', @ScriptGetEraserMode, ARegister);
  3018. FScriptContext.RegisterScriptFunction('ToolSetEraserAlpha', @ScriptSetEraserAlpha, ARegister);
  3019. FScriptContext.RegisterScriptFunction('ToolGetEraserAlpha', @ScriptGetEraserAlpha, ARegister);
  3020. FScriptContext.RegisterScriptFunction('ToolSetPenWidth', @ScriptSetPenWidth, ARegister);
  3021. FScriptContext.RegisterScriptFunction('ToolGetPenWidth', @ScriptGetPenWidth, ARegister);
  3022. FScriptContext.RegisterScriptFunction('ToolSetPenStyle', @ScriptSetPenStyle, ARegister);
  3023. FScriptContext.RegisterScriptFunction('ToolGetPenStyle', @ScriptGetPenStyle, ARegister);
  3024. FScriptContext.RegisterScriptFunction('ToolSetJoinStyle', @ScriptSetJoinStyle, ARegister);
  3025. FScriptContext.RegisterScriptFunction('ToolGetJoinStyle', @ScriptGetJoinStyle, ARegister);
  3026. FScriptContext.RegisterScriptFunction('ToolSetShapeOptions', @ScriptSetShapeOptions, ARegister);
  3027. FScriptContext.RegisterScriptFunction('ToolGetShapeOptions', @ScriptGetShapeOptions, ARegister);
  3028. FScriptContext.RegisterScriptFunction('ToolSetAliasing', @ScriptSetAliasing, ARegister);
  3029. FScriptContext.RegisterScriptFunction('ToolGetAliasing', @ScriptGetAliasing, ARegister);
  3030. FScriptContext.RegisterScriptFunction('ToolSetShapeRatio', @ScriptSetShapeRatio, ARegister);
  3031. FScriptContext.RegisterScriptFunction('ToolGetShapeRatio', @ScriptGetShapeRatio, ARegister);
  3032. FScriptContext.RegisterScriptFunction('ToolSetBrushIndex', @ScriptSetBrushIndex, ARegister);
  3033. FScriptContext.RegisterScriptFunction('ToolGetBrushIndex', @ScriptGetBrushIndex, ARegister);
  3034. FScriptContext.RegisterScriptFunction('ToolGetBrushCount', @ScriptGetBrushCount, ARegister);
  3035. FScriptContext.RegisterScriptFunction('ToolSetBrushSpacing', @ScriptSetBrushSpacing, ARegister);
  3036. FScriptContext.RegisterScriptFunction('ToolGetBrushSpacing', @ScriptGetBrushSpacing, ARegister);
  3037. FScriptContext.RegisterScriptFunction('ToolSetFontName', @ScriptSetFontName, ARegister);
  3038. FScriptContext.RegisterScriptFunction('ToolGetFontName', @ScriptGetFontName, ARegister);
  3039. FScriptContext.RegisterScriptFunction('ToolSetFontSize', @ScriptSetFontSize, ARegister);
  3040. FScriptContext.RegisterScriptFunction('ToolGetFontSize', @ScriptGetFontSize, ARegister);
  3041. FScriptContext.RegisterScriptFunction('ToolSetFontStyle', @ScriptSetFontStyle, ARegister);
  3042. FScriptContext.RegisterScriptFunction('ToolGetFontStyle', @ScriptGetFontStyle, ARegister);
  3043. FScriptContext.RegisterScriptFunction('ToolSetTextAlign', @ScriptSetTextAlign, ARegister);
  3044. FScriptContext.RegisterScriptFunction('ToolGetTextAlign', @ScriptGetTextAlign, ARegister);
  3045. FScriptContext.RegisterScriptFunction('ToolSetTextVerticalAlign', @ScriptSetTextVerticalAlign, ARegister);
  3046. FScriptContext.RegisterScriptFunction('ToolGetTextVerticalAlign', @ScriptGetTextVerticalAlign, ARegister);
  3047. FScriptContext.RegisterScriptFunction('ToolSetTextBidiMode', @ScriptSetTextBidiMode, ARegister);
  3048. FScriptContext.RegisterScriptFunction('ToolGetTextBidiMode', @ScriptGetTextBidiMode, ARegister);
  3049. FScriptContext.RegisterScriptFunction('ToolSetTextOutline', @ScriptSetTextOutline, ARegister);
  3050. FScriptContext.RegisterScriptFunction('ToolGetTextOutline', @ScriptGetTextOutline, ARegister);
  3051. FScriptContext.RegisterScriptFunction('ToolSetTextPhong', @ScriptSetTextPhong, ARegister);
  3052. FScriptContext.RegisterScriptFunction('ToolGetTextPhong', @ScriptGetTextPhong, ARegister);
  3053. FScriptContext.RegisterScriptFunction('ToolSetLightPosition', @ScriptSetLightPosition, ARegister);
  3054. FScriptContext.RegisterScriptFunction('ToolGetLightPosition', @ScriptGetLightPosition, ARegister);
  3055. { FScriptContext.RegisterScriptFunction('ToolSetLightAltitude', @ScriptSetLightAltitude, ARegister);
  3056. FScriptContext.RegisterScriptFunction('ToolGetLightAltitude', @ScriptGetLightAltitude, ARegister);}
  3057. FScriptContext.RegisterScriptFunction('ToolSetLineCap', @ScriptSetLineCap, ARegister);
  3058. FScriptContext.RegisterScriptFunction('ToolGetLineCap', @ScriptGetLineCap, ARegister);
  3059. FScriptContext.RegisterScriptFunction('ToolSetArrowStart', @ScriptSetArrowStart, ARegister);
  3060. FScriptContext.RegisterScriptFunction('ToolGetArrowStart', @ScriptGetArrowStart, ARegister);
  3061. FScriptContext.RegisterScriptFunction('ToolSetArrowEnd', @ScriptSetArrowEnd, ARegister);
  3062. FScriptContext.RegisterScriptFunction('ToolGetArrowEnd', @ScriptGetArrowEnd, ARegister);
  3063. FScriptContext.RegisterScriptFunction('ToolSetArrowSize', @ScriptSetArrowSize, ARegister);
  3064. FScriptContext.RegisterScriptFunction('ToolGetArrowSize', @ScriptGetArrowSize, ARegister);
  3065. FScriptContext.RegisterScriptFunction('ToolSetSplineStyle', @ScriptSetSplineStyle, ARegister);
  3066. FScriptContext.RegisterScriptFunction('ToolGetSplineStyle', @ScriptGetSplineStyle, ARegister);
  3067. FScriptContext.RegisterScriptFunction('ToolSetForeGradientType', @ScriptSetForeGradientType, ARegister);
  3068. FScriptContext.RegisterScriptFunction('ToolGetForeGradientType', @ScriptGetForeGradientType, ARegister);
  3069. FScriptContext.RegisterScriptFunction('ToolSetForeGradientRepetition', @ScriptSetForeGradientRepetition, ARegister);
  3070. FScriptContext.RegisterScriptFunction('ToolGetForeGradientRepetition', @ScriptGetForeGradientRepetition, ARegister);
  3071. FScriptContext.RegisterScriptFunction('ToolSetForeGradientInterpolation', @ScriptSetForeGradientInterpolation, ARegister);
  3072. FScriptContext.RegisterScriptFunction('ToolGetForeGradientInterpolation', @ScriptGetForeGradientInterpolation, ARegister);
  3073. FScriptContext.RegisterScriptFunction('ToolSetForeGradientColors', @ScriptSetForeGradientColors, ARegister);
  3074. FScriptContext.RegisterScriptFunction('ToolGetForeGradientColors', @ScriptGetForeGradientColors, ARegister);
  3075. FScriptContext.RegisterScriptFunction('ToolSetBackGradientType', @ScriptSetBackGradientType, ARegister);
  3076. FScriptContext.RegisterScriptFunction('ToolGetBackGradientType', @ScriptGetBackGradientType, ARegister);
  3077. FScriptContext.RegisterScriptFunction('ToolSetBackGradientRepetition', @ScriptSetBackGradientRepetition, ARegister);
  3078. FScriptContext.RegisterScriptFunction('ToolGetBackGradientRepetition', @ScriptGetBackGradientRepetition, ARegister);
  3079. FScriptContext.RegisterScriptFunction('ToolSetBackGradientInterpolation', @ScriptSetBackGradientInterpolation, ARegister);
  3080. FScriptContext.RegisterScriptFunction('ToolGetBackGradientInterpolation', @ScriptGetBackGradientInterpolation, ARegister);
  3081. FScriptContext.RegisterScriptFunction('ToolSetBackGradientColors', @ScriptSetBackGradientColors, ARegister);
  3082. FScriptContext.RegisterScriptFunction('ToolGetBackGradientColors', @ScriptGetBackGradientColors, ARegister);
  3083. FScriptContext.RegisterScriptFunction('ToolSetOutlineGradientType', @ScriptSetOutlineGradientType, ARegister);
  3084. FScriptContext.RegisterScriptFunction('ToolGetOutlineGradientType', @ScriptGetOutlineGradientType, ARegister);
  3085. FScriptContext.RegisterScriptFunction('ToolSetOutlineGradientRepetition', @ScriptSetOutlineGradientRepetition, ARegister);
  3086. FScriptContext.RegisterScriptFunction('ToolGetOutlineGradientRepetition', @ScriptGetOutlineGradientRepetition, ARegister);
  3087. FScriptContext.RegisterScriptFunction('ToolSetOutlineGradientInterpolation', @ScriptSetOutlineGradientInterpolation, ARegister);
  3088. FScriptContext.RegisterScriptFunction('ToolGetOutlineGradientInterpolation', @ScriptGetOutlineGradientInterpolation, ARegister);
  3089. FScriptContext.RegisterScriptFunction('ToolSetOutlineGradientColors', @ScriptSetOutlineGradientColors, ARegister);
  3090. FScriptContext.RegisterScriptFunction('ToolGetOutlineGradientColors', @ScriptGetOutlineGradientColors, ARegister);
  3091. FScriptContext.RegisterScriptFunction('ToolSetForeTexture', @ScriptSetForeTexture, ARegister);
  3092. FScriptContext.RegisterScriptFunction('ToolSetForeTextureRepetition', @ScriptSetForeTextureRepetition, ARegister);
  3093. FScriptContext.RegisterScriptFunction('ToolGetForeTextureRepetition', @ScriptGetForeTextureRepetition, ARegister);
  3094. FScriptContext.RegisterScriptFunction('ToolSetForeTextureOpacity', @ScriptSetForeTextureOpacity, ARegister);
  3095. FScriptContext.RegisterScriptFunction('ToolGetForeTextureOpacity', @ScriptGetForeTextureOpacity, ARegister);
  3096. FScriptContext.RegisterScriptFunction('ToolSetBackTexture', @ScriptSetBackTexture, ARegister);
  3097. FScriptContext.RegisterScriptFunction('ToolSetBackTextureRepetition', @ScriptSetBackTextureRepetition, ARegister);
  3098. FScriptContext.RegisterScriptFunction('ToolGetBackTextureRepetition', @ScriptGetBackTextureRepetition, ARegister);
  3099. FScriptContext.RegisterScriptFunction('ToolSetBackTextureOpacity', @ScriptSetBackTextureOpacity, ARegister);
  3100. FScriptContext.RegisterScriptFunction('ToolGetBackTextureOpacity', @ScriptGetBackTextureOpacity, ARegister);
  3101. FScriptContext.RegisterScriptFunction('ToolSetOutlineTexture', @ScriptSetOutlineTexture, ARegister);
  3102. FScriptContext.RegisterScriptFunction('ToolSetOutlineTextureRepetition', @ScriptSetOutlineTextureRepetition, ARegister);
  3103. FScriptContext.RegisterScriptFunction('ToolGetOutlineTextureRepetition', @ScriptGetOutlineTextureRepetition, ARegister);
  3104. FScriptContext.RegisterScriptFunction('ToolSetOutlineTextureOpacity', @ScriptSetOutlineTextureOpacity, ARegister);
  3105. FScriptContext.RegisterScriptFunction('ToolGetOutlineTextureOpacity', @ScriptGetOutlineTextureOpacity, ARegister);
  3106. FScriptContext.RegisterScriptFunction('ToolSetPhongShapeAltitude', @ScriptSetPhongShapeAltitude, ARegister);
  3107. FScriptContext.RegisterScriptFunction('ToolGetPhongShapeAltitude', @ScriptGetPhongShapeAltitude, ARegister);
  3108. FScriptContext.RegisterScriptFunction('ToolSetPhongShapeBorderSize', @ScriptSetPhongShapeBorderSize, ARegister);
  3109. FScriptContext.RegisterScriptFunction('ToolGetPhongShapeBorderSize', @ScriptGetPhongShapeBorderSize, ARegister);
  3110. FScriptContext.RegisterScriptFunction('ToolSetPhongShapeKind', @ScriptSetPhongShapeKind, ARegister);
  3111. FScriptContext.RegisterScriptFunction('ToolGetPhongShapeKind', @ScriptGetPhongShapeKind, ARegister);
  3112. FScriptContext.RegisterScriptFunction('ToolSetDeformationGridSize', @ScriptSetDeformationGridSize, ARegister);
  3113. FScriptContext.RegisterScriptFunction('ToolGetDeformationGridSize', @ScriptGetDeformationGridSize, ARegister);
  3114. FScriptContext.RegisterScriptFunction('ToolSetDeformationGridMode', @ScriptSetDeformationGridMode, ARegister);
  3115. FScriptContext.RegisterScriptFunction('ToolGetDeformationGridMode', @ScriptGetDeformationGridMode, ARegister);
  3116. FScriptContext.RegisterScriptFunction('ToolSetTolerance', @ScriptSetTolerance, ARegister);
  3117. FScriptContext.RegisterScriptFunction('ToolGetTolerance', @ScriptGetTolerance, ARegister);
  3118. FScriptContext.RegisterScriptFunction('ToolSetFloodFillOptions', @ScriptSetFloodFillOptions, ARegister);
  3119. FScriptContext.RegisterScriptFunction('ToolGetFloodFillOptions', @ScriptGetFloodFillOptions, ARegister);
  3120. FScriptContext.RegisterScriptFunction('ToolSetPerspectiveOptions', @ScriptSetPerspectiveOptions, ARegister);
  3121. FScriptContext.RegisterScriptFunction('ToolGetPerspectiveOptions', @ScriptGetPerspectiveOptions, ARegister);
  3122. end;
  3123. procedure TToolManager.ToolWakeUp;
  3124. begin
  3125. if FSleepingTool <> nil then
  3126. begin
  3127. FreeAndNil(FCurrentTool);
  3128. FCurrentTool := FSleepingTool;
  3129. FSleepingTool := nil;
  3130. FCurrentToolType := FSleepingToolType;
  3131. UpdateContextualToolbars;
  3132. If Assigned(FOnToolChangedHandler) then
  3133. FOnToolChangedHandler(self, FCurrentToolType);
  3134. If Assigned(FOnToolRenderChanged) then
  3135. FOnToolRenderChanged(self);
  3136. end;
  3137. end;
  3138. procedure TToolManager.ToolSleep;
  3139. begin
  3140. if (FSleepingTool = nil) and (FCurrentToolType <> ptHand) then
  3141. begin
  3142. FSleepingTool := FCurrentTool;
  3143. FSleepingToolType := FCurrentToolType;
  3144. FCurrentTool := nil;
  3145. InternalSetCurrentToolType(ptHand);
  3146. end;
  3147. end;
  3148. { tool implementation }
  3149. procedure TToolManager.SetDeformationGridSize(ASize: TSize);
  3150. begin
  3151. if ASize.cx < 3 then ASize.cx := 3;
  3152. if ASize.cy < 3 then ASize.cy := 3;
  3153. if (ASize.cx <> DeformationGridNbX) or (ASize.cy <> DeformationGridNbY) then
  3154. begin
  3155. FDeformationGridNbX := ASize.cx;
  3156. FDeformationGridNbY := ASize.cy;
  3157. if ToolUpdate then
  3158. Image.OnImageChanged.NotifyObservers;
  3159. if Assigned(FOnDeformationGridChanged) then FOnDeformationGridChanged(self);
  3160. end;
  3161. end;
  3162. function TToolManager.SwapToolColors: boolean;
  3163. var
  3164. tmpFill: TVectorialFill;
  3165. begin
  3166. result := false;
  3167. if FInSwapFill then exit;
  3168. if FForeFill.Equals(FBackFill) then exit;
  3169. FInSwapFill:= true;
  3170. tmpFill := FForeFill.Duplicate;
  3171. FForeFill.Assign(FBackFill);
  3172. FBackFill.Assign(tmpFill);
  3173. tmpFill.Free;
  3174. if FForeFill.FillType = vftGradient then
  3175. begin
  3176. FForeLastGradient.Free;
  3177. FForeLastGradient := FForeFill.Gradient.Duplicate as TBGRALayerGradientOriginal;
  3178. end;
  3179. if FBackFill.FillType = vftGradient then
  3180. begin
  3181. FBackLastGradient.Free;
  3182. FBackLastGradient := FBackFill.Gradient.Duplicate as TBGRALayerGradientOriginal;
  3183. end;
  3184. if Assigned(FOnFillChanged) then FOnFillChanged(self);
  3185. FInSwapFill:= false;
  3186. result := true;
  3187. end;
  3188. procedure TToolManager.NeedBackGradient;
  3189. var
  3190. tempFill: TVectorialFill;
  3191. begin
  3192. if BackFill.FillType <> vftGradient then
  3193. begin
  3194. tempFill := TVectorialFill.Create;
  3195. tempFill.SetGradient(FBackLastGradient, False);
  3196. tempFill.FitGeometry(SuggestGradientBox);
  3197. BackFill.Assign(tempFill);
  3198. tempFill.Free;
  3199. end;
  3200. end;
  3201. procedure TToolManager.NeedForeGradient;
  3202. var
  3203. tempFill: TVectorialFill;
  3204. begin
  3205. if ForeFill.FillType <> vftGradient then
  3206. begin
  3207. tempFill := TVectorialFill.Create;
  3208. tempFill.SetGradient(FForeLastGradient, False);
  3209. tempFill.FitGeometry(SuggestGradientBox);
  3210. ForeFill.Assign(tempFill);
  3211. tempFill.Free;
  3212. end;
  3213. end;
  3214. procedure TToolManager.NeedOutlineGradient;
  3215. var
  3216. tempFill: TVectorialFill;
  3217. begin
  3218. if OutlineFill.FillType <> vftGradient then
  3219. begin
  3220. tempFill := TVectorialFill.Create;
  3221. tempFill.SetGradient(FOutlineLastGradient, False);
  3222. tempFill.FitGeometry(SuggestGradientBox);
  3223. OutlineFill.Assign(tempFill);
  3224. tempFill.Free;
  3225. end;
  3226. end;
  3227. procedure TToolManager.AddBrush(brush: TLazPaintBrush);
  3228. begin
  3229. FBrushIndex := FBrushInfoList.Add(brush);
  3230. FBrushInfoListChanged := true;
  3231. if Assigned(FOnBrushListChanged) then FOnBrushListChanged(self);
  3232. end;
  3233. procedure TToolManager.RemoveBrushAt(index: integer);
  3234. begin
  3235. if Assigned(FBrushInfoList) then
  3236. begin
  3237. if (index >= 1) and (index < BrushCount) then
  3238. begin
  3239. BrushAt[index].Free;
  3240. FBrushInfoList.Delete(index);
  3241. if index < FBrushIndex then dec(FBrushIndex)
  3242. else if index = FBrushIndex then
  3243. begin
  3244. if FBrushIndex >= BrushCount then
  3245. dec(FBrushIndex);
  3246. end;
  3247. FBrushInfoListChanged := true;
  3248. if Assigned(FOnBrushListChanged) then FOnBrushListChanged(self);
  3249. end;
  3250. end;
  3251. end;
  3252. procedure TToolManager.SetTextFont(AName: string; ASize: single;
  3253. AStyle: TFontStyles);
  3254. begin
  3255. if AName = '' then AName := FTextFontName;
  3256. if (FTextFontName <> AName) or
  3257. (FTextFontSize <> ASize) or
  3258. (FTextFontStyle <> AStyle) then
  3259. begin
  3260. FTextFontName := AName;
  3261. if ASize >= 0 then FTextFontSize := ASize;
  3262. FTextFontStyle := AStyle;
  3263. ToolUpdate;
  3264. if Assigned(FOnTextFontChanged) then FOnTextFontChanged(self);
  3265. end;
  3266. end;
  3267. procedure TToolManager.SetTextFont(AFont: TFont);
  3268. begin
  3269. SetTextFont(AFont.Name, AFont.Size, AFont.Style);
  3270. end;
  3271. procedure TToolManager.SetTextOutline(AEnabled: boolean; AWidth: single);
  3272. begin
  3273. if (FTextOutline <> AEnabled) or
  3274. (FTextOutlineWidth <> AWidth) then
  3275. begin
  3276. FTextOutlineWidth := AWidth;
  3277. FTextOutline := AEnabled;
  3278. ToolUpdate;
  3279. if Assigned(FOnTextOutlineChanged) then FOnTextOutlineChanged(self);
  3280. end;
  3281. end;
  3282. function TToolManager.ToolDown(X, Y: single; ARightBtn: boolean;
  3283. APressure: single): boolean;
  3284. var changed: TRect;
  3285. begin
  3286. if FInTool then exit(false);
  3287. FInTool := true;
  3288. try
  3289. SetPressure(APressure);
  3290. if ToolCanBeUsed and Assigned(CurrentTool) then
  3291. changed := CurrentTool.ToolDown(X,Y,ARightBtn)
  3292. else
  3293. changed := EmptyRect;
  3294. result := not IsRectEmpty(changed);
  3295. if IsOnlyRenderChange(changed) then changed := EmptyRect;
  3296. if CheckExitTool then result := true;
  3297. if result then NotifyImageOrSelectionChanged(CurrentTool.LastToolDrawingLayer, changed);
  3298. finally
  3299. FInTool := false;
  3300. end;
  3301. end;
  3302. function TToolManager.ToolMove(X, Y: single; APressure: single): boolean;
  3303. var changed: TRect;
  3304. begin
  3305. if FInTool then exit(false);
  3306. FInTool := true;
  3307. try
  3308. SetPressure(APressure);
  3309. if ToolCanBeUsed and Assigned(CurrentTool) then
  3310. changed := CurrentTool.ToolMove(X,Y)
  3311. else
  3312. changed := EmptyRect;
  3313. result := not IsRectEmpty(changed);
  3314. if IsOnlyRenderChange(changed) then changed := EmptyRect;
  3315. if CheckExitTool then result := true;
  3316. if result then NotifyImageOrSelectionChanged(CurrentTool.LastToolDrawingLayer, changed);
  3317. finally
  3318. FInTool := false;
  3319. end;
  3320. end;
  3321. function TToolManager.ToolKeyDown(var key: Word): boolean;
  3322. var changed: TRect;
  3323. begin
  3324. if FInTool then exit(false);
  3325. FInTool := true;
  3326. try
  3327. if ToolCanBeUsed and Assigned(CurrentTool) then
  3328. changed := CurrentTool.ToolKeyDown(key)
  3329. else
  3330. changed := EmptyRect;
  3331. result := not IsRectEmpty(changed);
  3332. if IsOnlyRenderChange(changed) then changed := EmptyRect;
  3333. if CheckExitTool then result := true;
  3334. if result then NotifyImageOrSelectionChanged(CurrentTool.LastToolDrawingLayer, changed);
  3335. finally
  3336. FInTool := false;
  3337. end;
  3338. end;
  3339. function TToolManager.ToolKeyUp(var key: Word): boolean;
  3340. var changed: TRect;
  3341. begin
  3342. if FInTool then exit(false);
  3343. FInTool := true;
  3344. try
  3345. if ToolCanBeUsed and Assigned(CurrentTool) then
  3346. changed := CurrentTool.ToolKeyUp(key)
  3347. else
  3348. changed := EmptyRect;
  3349. result := not IsRectEmpty(changed);
  3350. if IsOnlyRenderChange(changed) then changed := EmptyRect;
  3351. if CheckExitTool then result := true;
  3352. if result then NotifyImageOrSelectionChanged(CurrentTool.LastToolDrawingLayer, changed);
  3353. finally
  3354. FInTool := false;
  3355. end;
  3356. end;
  3357. function TToolManager.ToolKeyPress(var key: TUTF8Char): boolean;
  3358. var changed: TRect;
  3359. begin
  3360. if FInTool then exit(false);
  3361. FInTool := true;
  3362. try
  3363. if ToolCanBeUsed and Assigned(CurrentTool) then
  3364. changed := CurrentTool.ToolKeyPress(key)
  3365. else
  3366. changed := EmptyRect;
  3367. result := not IsRectEmpty(changed);
  3368. if IsOnlyRenderChange(changed) then changed := EmptyRect;
  3369. if CheckExitTool then result := true;
  3370. if result then NotifyImageOrSelectionChanged(CurrentTool.LastToolDrawingLayer, changed);
  3371. finally
  3372. FInTool := false;
  3373. end;
  3374. end;
  3375. function TToolManager.ToolCommand(ACommand: TToolCommand): boolean;
  3376. begin
  3377. if FInTool then exit(false);
  3378. FInTool := true;
  3379. try
  3380. if Assigned(FCurrentTool) then
  3381. begin
  3382. result := FCurrentTool.ToolCommand(ACommand);
  3383. CheckExitTool;
  3384. end
  3385. else
  3386. result := false;
  3387. finally
  3388. FInTool := false;
  3389. end;
  3390. end;
  3391. function TToolManager.ToolProvideCommand(ACommand: TToolCommand): boolean;
  3392. begin
  3393. if Assigned(FCurrentTool) then
  3394. result := FCurrentTool.ToolProvideCommand(ACommand)
  3395. else
  3396. result := false;
  3397. end;
  3398. function TToolManager.ToolUp: boolean;
  3399. var changed: TRect;
  3400. begin
  3401. if FInTool then exit(false);
  3402. FInTool := true;
  3403. try
  3404. if ToolCanBeUsed and Assigned(CurrentTool) then
  3405. changed := CurrentTool.ToolUp
  3406. else
  3407. changed := EmptyRect;
  3408. result := not IsRectEmpty(changed);
  3409. if IsOnlyRenderChange(changed) then changed := EmptyRect;
  3410. if CheckExitTool then result := true;
  3411. if result then NotifyImageOrSelectionChanged(CurrentTool.LastToolDrawingLayer, changed);
  3412. finally
  3413. FInTool := false;
  3414. end;
  3415. end;
  3416. procedure TToolManager.ToolCloseDontReopen;
  3417. begin
  3418. if CurrentTool <> nil then
  3419. begin
  3420. if FInTool then raise exception.Create('Cannot close active tool');
  3421. FreeAndNil(FCurrentTool);
  3422. end;
  3423. end;
  3424. procedure TToolManager.ToolCloseAndReopenImmediatly;
  3425. begin
  3426. if CurrentTool <> nil then
  3427. begin
  3428. if FInTool then raise exception.Create('Cannot close active tool');
  3429. FInTool := true;
  3430. try
  3431. FreeAndNil(FCurrentTool);
  3432. finally
  3433. FInTool := false;
  3434. end;
  3435. ToolOpen;
  3436. end;
  3437. end;
  3438. procedure TToolManager.ToolOpen;
  3439. begin
  3440. if (FCurrentTool = nil) and (PaintTools[FCurrentToolType] <> nil) then
  3441. begin
  3442. if FInTool then raise exception.Create('Internal error');
  3443. FInTool := true;
  3444. try
  3445. FCurrentTool := PaintTools[FCurrentToolType].Create(self);
  3446. UpdateContextualToolbars;
  3447. If Assigned(FOnToolRenderChanged) then
  3448. FOnToolRenderChanged(self);
  3449. finally
  3450. FInTool := false;
  3451. end;
  3452. end;
  3453. end;
  3454. function TToolManager.ToolUpdate: boolean;
  3455. var changed: TRect;
  3456. begin
  3457. if FInTool then exit(false);
  3458. FInTool := true;
  3459. FInToolUpdate := true;
  3460. try
  3461. if ToolCanBeUsed and Assigned(CurrentTool) then
  3462. changed := CurrentTool.ToolUpdate
  3463. else
  3464. changed := EmptyRect;
  3465. result := not IsRectEmpty(changed);
  3466. if IsOnlyRenderChange(changed) then changed := EmptyRect;
  3467. if CheckExitTool then result := true;
  3468. if result then NotifyImageOrSelectionChanged(CurrentTool.LastToolDrawingLayer, changed);
  3469. finally
  3470. FInTool := false;
  3471. FInToolUpdate := false;
  3472. end;
  3473. end;
  3474. function TToolManager.ToolUpdateNeeded: boolean;
  3475. begin
  3476. if ToolCanBeUsed and Assigned(CurrentTool) then
  3477. result := CurrentTool.ToolUpdateNeeded
  3478. else
  3479. result := false;
  3480. if CheckExitTool then
  3481. result := true;
  3482. end;
  3483. procedure TToolManager.ToolPopup(AMessage: TToolPopupMessage; AKey: Word = 0; AAlways: boolean = false);
  3484. begin
  3485. if Assigned(FOnPopupToolHandler) then
  3486. FOnPopupToolHandler(self, AMessage, AKey, AAlways);
  3487. end;
  3488. function TToolManager.IsSelectingTool: boolean;
  3489. begin
  3490. if CurrentTool <> nil then
  3491. result := CurrentTool.IsSelectingTool
  3492. else
  3493. result := false;
  3494. end;
  3495. function TToolManager.DisplayFilledSelection: boolean;
  3496. begin
  3497. result := IsSelectingTool or (FCurrentToolType = ptEditShape);
  3498. end;
  3499. function TToolManager.IsForeEditGradTexPoints: boolean;
  3500. begin
  3501. if Assigned(CurrentTool) then result := CurrentTool.IsForeEditGradTexPoints
  3502. else result := false;
  3503. end;
  3504. function TToolManager.IsBackEditGradTexPoints: boolean;
  3505. begin
  3506. if Assigned(CurrentTool) then result := CurrentTool.IsBackEditGradTexPoints
  3507. else result := false;
  3508. end;
  3509. function TToolManager.IsOutlineEditGradTexPoints: boolean;
  3510. begin
  3511. if Assigned(CurrentTool) then result := CurrentTool.IsOutlineEditGradTexPoints
  3512. else result := false;
  3513. end;
  3514. procedure TToolManager.QueryExitTool;
  3515. begin
  3516. if FCurrentToolType in[ptRect,ptEllipse,ptPolygon,ptSpline,ptText,ptPhong,ptGradient] then
  3517. QueryExitTool(ptEditShape)
  3518. else
  3519. QueryExitTool(ptHand);
  3520. end;
  3521. procedure TToolManager.QueryExitTool(ASwitchTo: TPaintToolType);
  3522. begin
  3523. FShouldExitTool:= true;
  3524. FSwitchAfterExitTool:= ASwitchTo;
  3525. end;
  3526. procedure TToolManager.QueryColorTarget(ATarget: TVectorialFill);
  3527. begin
  3528. if Assigned(OnQueryColorTarget) then
  3529. OnQueryColorTarget(self, ATarget);
  3530. end;
  3531. function TToolManager.RenderTool(formBitmap: TBGRABitmap): TRect;
  3532. begin
  3533. if ToolCanBeUsed and Assigned(CurrentTool) and not FInTool then
  3534. begin
  3535. FInTool := true;
  3536. try
  3537. result := CurrentTool.Render(formBitmap,formBitmap.Width,formBitmap.Height, @InternalBitmapToVirtualScreen);
  3538. finally
  3539. FInTool := false;
  3540. end;
  3541. end else
  3542. result := EmptyRect;
  3543. end;
  3544. function TToolManager.GetRenderBounds(VirtualScreenWidth, VirtualScreenHeight: integer): TRect;
  3545. begin
  3546. if ToolCanBeUsed and Assigned(CurrentTool) and not CurrentTool.Validating and not CurrentTool.Canceling then
  3547. result := CurrentTool.Render(nil,VirtualScreenWidth,VirtualScreenHeight, @InternalBitmapToVirtualScreen)
  3548. else
  3549. result := EmptyRect;
  3550. end;
  3551. function TToolManager.SuggestGradientBox: TAffineBox;
  3552. begin
  3553. if Assigned(CurrentTool) then
  3554. result := CurrentTool.SuggestGradientBox
  3555. else
  3556. result := TAffineBox.AffineBox(RectF(PointF(0,0),PointF(Image.Width,Image.Height)));
  3557. end;
  3558. function TToolManager.GetDeformationGridSize: TSize;
  3559. begin
  3560. result := Size(DeformationGridNbX, DeformationGridNbY);
  3561. end;
  3562. function TToolManager.ToolDown(ACoord: TPointF; ARightBtn: boolean;
  3563. APressure: single): boolean;
  3564. begin
  3565. result := ToolDown(ACoord.x,ACoord.y,ARightBtn,APressure)
  3566. end;
  3567. function TToolManager.ToolMove(ACoord: TPointF; APressure: single): boolean;
  3568. begin
  3569. result := ToolMove(ACoord.x,ACoord.y,APressure)
  3570. end;
  3571. initialization
  3572. {$i ../resources/brushes.lrs}
  3573. fillchar({%H-}PaintTools,sizeof(PaintTools),0);
  3574. end.