FGuiSkinEditor.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit FGuiSkinEditor;
  5. (* Editor for Gui skin. *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. Winapi.Windows,
  10. Winapi.Messages,
  11. System.SysUtils,
  12. System.Classes,
  13. VCL.Graphics,
  14. VCL.Controls,
  15. VCL.Forms,
  16. VCL.Dialogs,
  17. VCL.StdCtrls,
  18. VCL.ComCtrls,
  19. VCL.ExtCtrls,
  20. VCL.Menus,
  21. GLTexture,
  22. GLScene,
  23. GLObjects,
  24. GLWindows,
  25. GLHUDObjects,
  26. GLSceneViewer,
  27. GLGui,
  28. GLGraphics,
  29. GLS.Utils,
  30. GLCrossPlatform,
  31. GLCoordinates,
  32. GLBaseClasses,
  33. GLMaterial;
  34. type
  35. TGLSkinEditorForm = class(TForm)
  36. GLScene1: TGLScene;
  37. GLCamera1: TGLCamera;
  38. GLPanel1: TGLPanel;
  39. HUDSprite1: TGLHUDSprite;
  40. GLMemoryViewer1: TGLMemoryViewer;
  41. GLLightSource1: TGLLightSource;
  42. StatusBar: TStatusBar;
  43. panBottom: TPanel;
  44. panZoomImage: TPanel;
  45. imgFull: TImage;
  46. sbarHorizontal: TScrollBar;
  47. sbarVertical: TScrollBar;
  48. Button5: TButton;
  49. Button6: TButton;
  50. panImageProperties: TPanel;
  51. Label5: TLabel;
  52. Label6: TLabel;
  53. Panel2: TPanel;
  54. imgPreview: TImage;
  55. Panel3: TPanel;
  56. Label2: TLabel;
  57. Label1: TLabel;
  58. Button3: TButton;
  59. Button4: TButton;
  60. CheckBox1: TCheckBox;
  61. WidthEdit: TEdit;
  62. HeightEdit: TEdit;
  63. panElements: TPanel;
  64. Bevel2: TBevel;
  65. Bevel1: TBevel;
  66. Label3: TLabel;
  67. Label4: TLabel;
  68. Label11: TLabel;
  69. Label12: TLabel;
  70. Label13: TLabel;
  71. Label9: TLabel;
  72. Label10: TLabel;
  73. Label14: TLabel;
  74. lbElements: TListBox;
  75. btnAdd: TButton;
  76. btnDelete: TButton;
  77. ComboBox1: TComboBox;
  78. LeftEdit: TEdit;
  79. TopEdit: TEdit;
  80. RightEdit: TEdit;
  81. BottomEdit: TEdit;
  82. ScaleXEdit: TEdit;
  83. ScaleYEdit: TEdit;
  84. popElements: TPopupMenu;
  85. mnuTopLeft: TMenuItem;
  86. mnuTop: TMenuItem;
  87. mnuTopRight: TMenuItem;
  88. mnuLeft: TMenuItem;
  89. mnuCenter: TMenuItem;
  90. mnuRight: TMenuItem;
  91. mnuBottomLeft: TMenuItem;
  92. mnuBottom: TMenuItem;
  93. mnuBottomRight: TMenuItem;
  94. N1: TMenuItem;
  95. mnuAddAll: TMenuItem;
  96. N2: TMenuItem;
  97. mnuAllTop: TMenuItem;
  98. mnuAllMiddle: TMenuItem;
  99. mnuAllBottom: TMenuItem;
  100. procedure FormCreate(Sender: TObject);
  101. procedure FormDestroy(Sender: TObject);
  102. procedure Button3Click(Sender: TObject);
  103. procedure Button4Click(Sender: TObject);
  104. procedure ScrollBarScroll(Sender: TObject; ScrollCode: TScrollCode;
  105. var ScrollPos: Integer);
  106. procedure ScrollbarChange(Sender: TObject);
  107. procedure WidthEditChange(Sender: TObject);
  108. procedure HeightEditChange(Sender: TObject);
  109. procedure btnAddClick(Sender: TObject);
  110. procedure lbElementsClick(Sender: TObject);
  111. procedure ComboBox1Change(Sender: TObject);
  112. procedure btnDeleteClick(Sender: TObject);
  113. procedure imgFullMouseDown(Sender: TObject; Button: TMouseButton;
  114. Shift: TShiftState; X, Y: Integer);
  115. procedure imgFullMouseUp(Sender: TObject; Button: TMouseButton;
  116. Shift: TShiftState; X, Y: Integer);
  117. procedure imgFullMouseMove(Sender: TObject; Shift: TShiftState; X,
  118. Y: Integer);
  119. procedure lbElementsKeyDown(Sender: TObject; var Key: Word;
  120. Shift: TShiftState);
  121. procedure CheckBox1Click(Sender: TObject);
  122. procedure ScaleXEditChange(Sender: TObject);
  123. procedure ScaleYEditChange(Sender: TObject);
  124. procedure LeftEditChange(Sender: TObject);
  125. procedure TopEditChange(Sender: TObject);
  126. procedure RightEditChange(Sender: TObject);
  127. procedure BottomEditChange(Sender: TObject);
  128. procedure EditKeyPress(Sender: TObject; var Key: Char);
  129. procedure FormResize(Sender: TObject);
  130. procedure MenuItemClick(Sender: TObject);
  131. procedure mnuAddAllClick(Sender: TObject);
  132. procedure mnuAllTopClick(Sender: TObject);
  133. procedure mnuAllMiddleClick(Sender: TObject);
  134. procedure mnuAllBottomClick(Sender: TObject);
  135. procedure imgPreviewMouseDown(Sender: TObject; Button: TMouseButton;
  136. Shift: TShiftState; X, Y: Integer);
  137. procedure imgPreviewMouseMove(Sender: TObject; Shift: TShiftState; X,
  138. Y: Integer);
  139. private
  140. FOriginalWndProc: TWndMethod;
  141. FFocusRect: TRect;
  142. VisibleRect: TRect;
  143. PreviewMousePoint: TPoint;
  144. PreviewWidth,
  145. PreviewHeight: Integer;
  146. FullMousePoint: TPoint;
  147. MouseDown: Boolean;
  148. procedure ImageWndProc(var Message: TMessage);
  149. procedure DrawImageFocusRect(ARect: TRect);
  150. procedure AlignZoomPanel;
  151. procedure UpdateRegionEdits;
  152. procedure SetEditState(Parent: TControl; Enabled: Boolean);
  153. procedure AddElement(Index: Integer);
  154. procedure DrawCrossair(Point: TPoint);
  155. public
  156. TheGuiComponent: TGLGuiElementList;
  157. SelectedElement: TGLGUIElement;
  158. Tex: TGLTexture;
  159. Zoom: Single;
  160. Width: Integer;
  161. Height: Integer;
  162. function Edit(GuiComponent: TGLGuiElementList): Boolean;
  163. procedure Render;
  164. procedure SetMax(Scrollbar: TScrollbar; Val: Integer);
  165. end;
  166. var
  167. GLSkinEditorForm: TGLSkinEditorForm;
  168. function GUIComponentDialog(GuiComponent: TGLGuiElementList): Boolean;
  169. //--------------------------------------------------------------------
  170. implementation
  171. //--------------------------------------------------------------------
  172. {$R *.dfm}
  173. function GUIComponentDialog(GuiComponent: TGLGuiElementList): Boolean;
  174. var
  175. Editor: TGLSkinEditorForm;
  176. begin
  177. Editor := TGLSkinEditorForm.Create(nil);
  178. Result := Editor.Edit(GuiComponent);
  179. Editor.Free;
  180. end;
  181. procedure TGLSkinEditorForm.FormCreate(Sender: TObject);
  182. begin
  183. //override original WndProc to capture image mouse leave message
  184. FOriginalWndProc := imgFull.WindowProc;
  185. imgFull.WindowProc := ImageWndProc;
  186. Tex := TGLTexture.Create(Self);
  187. Tex.SetImageClassName('TGLPersistentImage');
  188. GLPanel1.RedrawAtOnce := True;
  189. StatusBar.Panels[0].Text := 'X : 0';
  190. StatusBar.Panels[1].Text := 'Y : 0';
  191. AlignZoomPanel;
  192. UpdateRegionEdits;
  193. DoubleBuffered := True;
  194. FullMousePoint := Point(-1, -1);
  195. //this Delphi bug shows all panels transparent
  196. //the code below is to avoid this bug in XP
  197. panElements.ParentBackground := False;
  198. panElements.ParentBackground := True;
  199. panElements.ParentBackground := False;
  200. panImageProperties.ParentBackground := False;
  201. panImageProperties.ParentBackground := True;
  202. panImageProperties.ParentBackground := False;
  203. panBottom.ParentBackground := False;
  204. panBottom.ParentBackground := True;
  205. panBottom.ParentBackground := False;
  206. panZoomImage.ParentBackground := False;
  207. panZoomImage.ParentBackground := True;
  208. panZoomImage.ParentBackground := False;
  209. end;
  210. procedure TGLSkinEditorForm.FormDestroy(Sender: TObject);
  211. begin
  212. Tex.Free;
  213. end;
  214. function TGLSkinEditorForm.Edit(GuiComponent: TGLGuiElementList): Boolean;
  215. var
  216. Mat: TGLMaterial;
  217. GuiLayout: TGLGuiLayout;
  218. XC: Integer;
  219. begin
  220. TheGuiComponent := GuiComponent;
  221. GuiLayout := (GuiComponent.GetOwner as TGLGuiComponent).Owner.GetOwner as
  222. TGLGuiLayout;
  223. Mat := GuiLayout.Material;
  224. GLPanel1.Visible := True;
  225. GLPanel1.GuiLayout := GuiLayout;
  226. GLPanel1.GuiLayoutName := (GuiComponent.GetOwner as TGLGuiComponent).Name;
  227. Zoom := 1.0;
  228. if (Assigned(mat.MaterialLibrary)
  229. and (mat.MaterialLibrary is TGLMaterialLibrary)
  230. and (Mat.LibMaterialName <> '')) then
  231. begin
  232. mat :=
  233. TGLMaterialLibrary(mat.MaterialLibrary).Materials.GetLibMaterialByName(Mat.LibMaterialName).Material;
  234. end;
  235. Width := Mat.Texture.Image.Width;
  236. Height := Mat.Texture.Image.Height;
  237. WidthEdit.Text := IntToStr(Mat.Texture.Image.Width);
  238. HeightEdit.Text := IntToStr(Mat.Texture.Image.Height);
  239. GLPanel1.GuiLayout.Material.Assign(Mat);
  240. Tex.Assign(mat.Texture);
  241. imgPreview.Picture.Bitmap.Canvas.StretchDraw(imgPreview.ClientRect, (Tex.Image
  242. as TGLPersistentImage).Picture.Graphic);
  243. PreviewWidth := (Tex.Image as TGLPersistentImage).Picture.Width;
  244. Previewheight := (Tex.Image as TGLPersistentImage).Picture.Height;
  245. lbElements.Clear;
  246. for XC := 0 to TheGuiComponent.Count - 1 do
  247. begin
  248. lbElements.Items.Add(TheGuiComponent.Items[XC].Name);
  249. end;
  250. if TheGuiComponent.Count > 0 then
  251. begin
  252. SelectedElement := TheGuiComponent.Items[0];
  253. lbElements.ItemIndex := 0;
  254. end
  255. else
  256. SelectedElement := nil;
  257. Render;
  258. Result := ShowModal = mrOk;
  259. end;
  260. procedure TGLSkinEditorForm.Button3Click(Sender: TObject);
  261. begin
  262. Zoom := Zoom + 0.5;
  263. Label2.Caption := FormatFloat('####0.0', Zoom);
  264. {$IFDEF MSWINDOWS}
  265. sbarVertical.PageSize := Round(256 / Zoom);
  266. sbarHorizontal.PageSize := Round(256 / Zoom);
  267. {$ENDIF}
  268. Render;
  269. end;
  270. procedure TGLSkinEditorForm.Button4Click(Sender: TObject);
  271. begin
  272. if Abs(Zoom - 0.5) > 0.001 then
  273. Zoom := Zoom - 0.5;
  274. Label2.Caption := FormatFloat('####0.0', Zoom);
  275. // panel3.Invalidate;
  276. {$IFDEF MSWINDOWS}
  277. sbarVertical.PageSize := Round(256 / Zoom);
  278. sbarHorizontal.PageSize := Round(256 / Zoom);
  279. {$ENDIF}
  280. Render;
  281. end;
  282. procedure TGLSkinEditorForm.Render;
  283. var
  284. BitMap: TBitmap;
  285. Image: TGLBitmap32;
  286. begin
  287. if CheckBox1.Checked then
  288. begin
  289. GLPanel1.Width := Width;
  290. GLPanel1.Height := Height;
  291. GLPanel1.Left := 1 - sbarHorizontal.position;
  292. GLPanel1.Top := 1 - sbarVertical.position;
  293. GLMemoryViewer1.Render;
  294. Image := GLMemoryViewer1.Buffer.CreateSnapShot;
  295. ;
  296. Bitmap := Image.Create32BitsBitmap;
  297. try
  298. imgFull.Canvas.Brush.Color := clBlack;
  299. imgFull.Canvas.FillRect(imgFull.Canvas.ClipRect);
  300. imgFull.Canvas.StretchDraw(Rect(0, 0, Round(((Tex.Image as
  301. TGLPersistentImage).Width) * Zoom), Round(((Tex.Image as
  302. TGLPersistentImage).Height) * Zoom)), Bitmap); {}
  303. finally
  304. Bitmap.Free;
  305. Image.Free;
  306. end;
  307. // imgFull.Canvas.StretchDraw(Rect(Round((1-sbarHorizontal.position)*Zoom),Round((1-sbarVertical.position)*Zoom),Round((1-sbarHorizontal.position+(Tex.Image as TGLPersistentImage).Width)*Zoom),Round((1-sbarVertical.position+(Tex.Image as TGLPersistentImage).Height)*Zoom)),Bitmap);{}
  308. end
  309. else
  310. begin
  311. imgFull.Canvas.Brush.Color := clBlack;
  312. imgFull.Canvas.FillRect(imgFull.Canvas.ClipRect);
  313. imgFull.Canvas.StretchDraw(
  314. Rect(
  315. Round((1 - sbarHorizontal.position) * Zoom),
  316. Round((1 - sbarVertical.position) * Zoom),
  317. Round((1 - sbarHorizontal.position + (Tex.Image as
  318. TGLPersistentImage).Width) * Zoom),
  319. Round((1 - sbarVertical.position + (Tex.Image as
  320. TGLPersistentImage).Height) * Zoom)
  321. ),
  322. (Tex.Image as TGLPersistentImage).Picture.Graphic
  323. );
  324. if Assigned(SelectedElement) then
  325. begin
  326. imgFull.Canvas.Brush.Style := bsClear;
  327. imgFull.Canvas.Pen.Color := clWhite;
  328. imgFull.Canvas.Pen.Style := psSolid;
  329. imgFull.Canvas.Pen.Mode := pmXor;
  330. imgFull.Canvas.Rectangle(
  331. Rect(Round((1 - sbarHorizontal.position + SelectedElement.TopLeft.X) *
  332. Zoom),
  333. Round((1 - sbarVertical.position + SelectedElement.TopLeft.Y) * Zoom),
  334. Round((1 - sbarHorizontal.position + SelectedElement.BottomRight.X) *
  335. Zoom),
  336. Round((1 - sbarVertical.position + SelectedElement.BottomRight.Y) * Zoom)
  337. )
  338. );
  339. end;
  340. //rectangle the part that is visible in the preview
  341. imgPreview.Canvas.DrawFocusRect(VisibleRect);
  342. if (PreviewWidth = 0) or (PreviewHeight = 0) then
  343. begin
  344. PreviewWidth := 2;
  345. PreviewHeight := 2;
  346. end;
  347. if Zoom = 0 then
  348. Zoom := 0.5;
  349. // {$R-}
  350. VisibleRect := Rect(
  351. Round(sbarHorizontal.Position / PreviewWidth * imgPreview.Width),
  352. Round(sbarVertical.Position / PreviewHeight * imgPreview.Height),
  353. Round((sbarHorizontal.Position + (imgFull.Width - 1) / Zoom) / PreviewWidth
  354. * imgPreview.Width),
  355. Round((sbarVertical.Position + (imgFull.Height - 1) / Zoom) / PreviewHeight
  356. * imgPreview.Height)
  357. );
  358. imgPreview.Canvas.DrawFocusRect(VisibleRect);
  359. end;
  360. DrawCrossair(FullMousePoint);
  361. end;
  362. procedure TGLSkinEditorForm.SetMax(Scrollbar: TScrollbar; Val: Integer);
  363. begin
  364. {$IFDEF MSWINDOWS}
  365. if Scrollbar.Position + Scrollbar.PageSize >= val then
  366. begin
  367. Scrollbar.Position := val - Scrollbar.PageSize + 1;
  368. end;
  369. {$ENDIF}
  370. Scrollbar.Max := val;
  371. end;
  372. procedure TGLSkinEditorForm.ScrollBarScroll(Sender: TObject;
  373. ScrollCode: TScrollCode; var ScrollPos: Integer);
  374. begin
  375. {$IFDEF MSWINDOWS}
  376. if ScrollPos + (Sender as TScrollBar).PageSize > (Sender as TScrollBar).Max
  377. then
  378. ScrollPos := (Sender as TScrollBar).Max - (Sender as TScrollBar).PageSize +
  379. 1;
  380. {$ENDIF}
  381. end;
  382. procedure TGLSkinEditorForm.ScrollbarChange(Sender: TObject);
  383. begin
  384. Render;
  385. end;
  386. procedure TGLSkinEditorForm.WidthEditChange(Sender: TObject);
  387. var
  388. Val: Integer;
  389. begin
  390. val := StrToIntDef(WidthEdit.Text, 0);
  391. if Val > 0 then
  392. begin
  393. Width := Val;
  394. GLPanel1.Width := Val;
  395. GLPanel1.ReBuildGui := True;
  396. GLPanel1.GUIRedraw := True;
  397. if Val > 256 then
  398. begin
  399. SetMax(sbarHorizontal, Val);
  400. end
  401. else
  402. begin
  403. SetMax(sbarHorizontal, 256);
  404. end;
  405. Render;
  406. end;
  407. end;
  408. procedure TGLSkinEditorForm.HeightEditChange(Sender: TObject);
  409. var
  410. Val: Integer;
  411. begin
  412. val := StrToIntDef(HeightEdit.Text, 0);
  413. if Val > 0 then
  414. begin
  415. Height := Val;
  416. GLPanel1.Height := Val;
  417. GLPanel1.ReBuildGui := True;
  418. GLPanel1.GUIRedraw := True;
  419. if Val > 256 then
  420. begin
  421. SetMax(sbarVertical, Val);
  422. end
  423. else
  424. begin
  425. SetMax(sbarVertical, 256);
  426. end;
  427. Render;
  428. end;
  429. end;
  430. procedure TGLSkinEditorForm.btnAddClick(Sender: TObject);
  431. var
  432. PopupPoint: TPoint;
  433. begin
  434. PopupPoint := btnAdd.Parent.ClientToScreen(Point(btnAdd.Left, btnAdd.Top +
  435. btnAdd.Height));
  436. popElements.Popup(PopupPoint.X, PopupPoint.Y);
  437. end;
  438. procedure TGLSkinEditorForm.lbElementsClick(Sender: TObject);
  439. begin
  440. UpdateRegionEdits;
  441. end;
  442. procedure TGLSkinEditorForm.ComboBox1Change(Sender: TObject);
  443. var
  444. S: string;
  445. Count: Integer;
  446. begin
  447. if Assigned(SelectedElement) then
  448. begin
  449. if SelectedElement.Align = TGUIAlignments(ComboBox1.ItemIndex) then
  450. Exit;
  451. SelectedElement.Align := TGUIAlignments(ComboBox1.ItemIndex);
  452. GLPanel1.ReBuildGui := True;
  453. GLPanel1.GUIRedraw := True;
  454. Count := 1;
  455. repeat
  456. S := ComboBox1.Items[ComboBox1.ItemIndex] + IntToStr(Count);
  457. inc(Count);
  458. until lbElements.Items.IndexOf(S) = -1;
  459. lbElements.Items[lbElements.Itemindex] := S;
  460. Render;
  461. end;
  462. end;
  463. procedure TGLSkinEditorForm.btnDeleteClick(Sender: TObject);
  464. var
  465. Index: Integer;
  466. begin
  467. if (lbElements.ItemIndex >= 0) and (lbElements.ItemIndex <
  468. lbElements.Items.Count) then
  469. begin
  470. Index := lbElements.ItemIndex;
  471. TheGuiComponent.Delete(Index);
  472. lbElements.Items.Delete(Index);
  473. if Index > lbElements.Items.Count - 1 then
  474. while (Index > -1) and (Index > lbElements.Items.Count - 1) do
  475. Dec(Index);
  476. if Index > -1 then
  477. lbElements.ItemIndex := Index;
  478. Render;
  479. end;
  480. UpdateRegionEdits;
  481. end;
  482. procedure TGLSkinEditorForm.imgFullMouseDown(Sender: TObject;
  483. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  484. begin
  485. if SelectedElement = nil then
  486. exit;
  487. if Button = TMouseButton(mbLeft) then
  488. begin
  489. MouseDown := True;
  490. if not CheckBox1.Checked then
  491. if Assigned(SelectedElement) then
  492. begin
  493. SelectedElement.TopLeft.X := (sbarHorizontal.Position - 1) + Int(x /
  494. Zoom);
  495. SelectedElement.TopLeft.Y := (sbarVertical.Position - 1) + Int(y /
  496. Zoom);
  497. end;
  498. end;
  499. FFocusRect.TopLeft := Point(Round(Round(X / Zoom - 0.5) * Zoom), Round(Round(Y
  500. / Zoom - 0.5) * Zoom));
  501. FFocusRect.BottomRight := FFocusRect.TopLeft;
  502. DrawImageFocusRect(FFocusRect);
  503. end;
  504. procedure TGLSkinEditorForm.imgFullMouseUp(Sender: TObject;
  505. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  506. begin
  507. if SelectedElement = nil then
  508. exit;
  509. if Button = TMouseButton(mbLeft) then
  510. begin
  511. MouseDown := False;
  512. if not CheckBox1.Checked then
  513. if Assigned(SelectedElement) then
  514. begin
  515. SelectedElement.BottomRight.X := (sbarHorizontal.Position) + Int(x /
  516. Zoom);
  517. SelectedElement.BottomRight.Y := (sbarVertical.Position) + Int(y /
  518. Zoom);
  519. Render;
  520. end;
  521. end;
  522. StatusBar.Panels[2].Text := 'dx : ';
  523. StatusBar.Panels[3].Text := 'dy : ';
  524. DrawImageFocusRect(FFocusRect);
  525. UpdateRegionEdits;
  526. end;
  527. procedure TGLSkinEditorForm.imgFullMouseMove(Sender: TObject;
  528. Shift: TShiftState; X, Y: Integer);
  529. begin
  530. DrawCrossair(FullMousePoint);
  531. FullMousePoint := Point(X, Y);
  532. DrawCrossair(FullMousePoint);
  533. if ssLeft in Shift then
  534. begin
  535. if SelectedElement <> nil then
  536. begin
  537. DrawImageFocusRect(FFocusRect);
  538. FFocusRect.BottomRight := Point(Round(Round(X / Zoom + 0.5) * Zoom),
  539. Round(Round(Y / Zoom + 0.5) * Zoom));
  540. DrawImageFocusRect(FFocusRect);
  541. StatusBar.Panels[0].Text := 'X : ' + FormatFloat('###0',
  542. Round(sbarHorizontal.Position + (x - 1) / Zoom));
  543. StatusBar.Panels[1].Text := 'Y : ' + FormatFloat('###0',
  544. Round(sbarVertical.Position + (y - 1) / Zoom));
  545. StatusBar.Panels[2].Text := 'dx : ' + FormatFloat('###0',
  546. Round(sbarHorizontal.Position + (x - 1) / Zoom -
  547. SelectedElement.TopLeft.X));
  548. StatusBar.Panels[3].Text := 'dy : ' + FormatFloat('###0',
  549. Round(sbarVertical.Position + (y - 1) / Zoom -
  550. SelectedElement.TopLeft.Y));
  551. end;
  552. end
  553. else
  554. begin
  555. StatusBar.Panels[0].Text := 'X : ' + FormatFloat('###0',
  556. Trunc(sbarHorizontal.Position + (x - 1) / Zoom));
  557. StatusBar.Panels[1].Text := 'Y : ' + FormatFloat('###0',
  558. Trunc(sbarVertical.Position + (y - 1) / Zoom));
  559. end;
  560. end;
  561. procedure TGLSkinEditorForm.lbElementsKeyDown(Sender: TObject; var Key: Word;
  562. Shift: TShiftState);
  563. begin
  564. if key = VK_LEFT then
  565. begin
  566. if lbElements.ItemIndex > 0 then
  567. lbElements.ItemIndex := lbElements.ItemIndex - 1;
  568. key := VK_CANCEL;
  569. end;
  570. if key = VK_RIGHT then
  571. begin
  572. if lbElements.ItemIndex + 1 < lbElements.Items.Count then
  573. lbElements.ItemIndex := lbElements.ItemIndex + 1;
  574. key := VK_CANCEL;
  575. end;
  576. end;
  577. procedure TGLSkinEditorForm.CheckBox1Click(Sender: TObject);
  578. begin
  579. GLPanel1.ReBuildGui := True;
  580. GLPanel1.GUIRedraw := True;
  581. Render;
  582. end;
  583. procedure TGLSkinEditorForm.ScaleXEditChange(Sender: TObject);
  584. var
  585. res: Single;
  586. begin
  587. if Assigned(SelectedElement) then
  588. begin
  589. res := GLS.Utils.StrToFloatDef(ScaleXEdit.Text, 0);
  590. if res > 0 then
  591. begin
  592. SelectedElement.Scale.X := Res;
  593. GLPanel1.ReBuildGui := True;
  594. GLPanel1.GUIRedraw := True;
  595. Render;
  596. end;
  597. end;
  598. end;
  599. procedure TGLSkinEditorForm.ScaleYEditChange(Sender: TObject);
  600. var
  601. res: Single;
  602. begin
  603. if Assigned(SelectedElement) then
  604. begin
  605. res := GLS.Utils.StrToFloatDef(ScaleYEdit.Text, 0);
  606. if res > 0 then
  607. begin
  608. SelectedElement.Scale.Y := Res;
  609. GLPanel1.ReBuildGui := True;
  610. GLPanel1.GUIRedraw := True;
  611. Render;
  612. end;
  613. end;
  614. end;
  615. procedure TGLSkinEditorForm.LeftEditChange(Sender: TObject);
  616. var
  617. res: Single;
  618. begin
  619. if Assigned(SelectedElement) then
  620. begin
  621. GLPanel1.BlockRender;
  622. try
  623. res := GLS.Utils.StrToFloatDef(LeftEdit.Text, -1);
  624. if res >= 0 then
  625. begin
  626. SelectedElement.TopLeft.X := Res;
  627. end;
  628. finally
  629. GLPanel1.UnBlockRender;
  630. end;
  631. GLPanel1.ReBuildGui := True;
  632. GLPanel1.GUIRedraw := True;
  633. Render;
  634. end;
  635. end;
  636. procedure TGLSkinEditorForm.TopEditChange(Sender: TObject);
  637. var
  638. res: Single;
  639. begin
  640. if Assigned(SelectedElement) then
  641. begin
  642. GLPanel1.BlockRender;
  643. try
  644. res := GLS.Utils.StrToFloatDef(TopEdit.Text, -1);
  645. if res >= 0 then
  646. begin
  647. SelectedElement.TopLeft.Y := Res;
  648. end;
  649. finally
  650. GLPanel1.UnBlockRender;
  651. end;
  652. GLPanel1.ReBuildGui := True;
  653. GLPanel1.GUIRedraw := True;
  654. Render;
  655. end;
  656. end;
  657. procedure TGLSkinEditorForm.RightEditChange(Sender: TObject);
  658. var
  659. res: Single;
  660. begin
  661. if Assigned(SelectedElement) then
  662. begin
  663. GLPanel1.BlockRender;
  664. try
  665. res := GLS.Utils.StrToFloatDef(RightEdit.Text, -1);
  666. if res >= 0 then
  667. begin
  668. SelectedElement.BottomRight.X := Res;
  669. end;
  670. finally
  671. GLPanel1.UnBlockRender;
  672. end;
  673. GLPanel1.ReBuildGui := True;
  674. GLPanel1.GUIRedraw := True;
  675. Render;
  676. end;
  677. end;
  678. procedure TGLSkinEditorForm.BottomEditChange(Sender: TObject);
  679. var
  680. res: Single;
  681. begin
  682. if Assigned(SelectedElement) then
  683. begin
  684. GLPanel1.BlockRender;
  685. try
  686. res := GLS.Utils.StrToFloatDef(BottomEdit.Text, -1);
  687. if res >= 0 then
  688. begin
  689. SelectedElement.BottomRight.Y := Res;
  690. end;
  691. finally
  692. GLPanel1.UnBlockRender;
  693. end;
  694. GLPanel1.ReBuildGui := True;
  695. GLPanel1.GUIRedraw := True;
  696. Render;
  697. end;
  698. end;
  699. procedure TGLSkinEditorForm.DrawImageFocusRect(ARect: TRect);
  700. begin
  701. imgFull.Canvas.Brush.Style := bsClear;
  702. imgFull.Canvas.Pen.Color := clRed;
  703. imgFull.Canvas.Pen.Style := psSolid;
  704. imgFull.Canvas.Pen.Mode := pmXor;
  705. imgFull.Canvas.Rectangle(ARect);
  706. end;
  707. procedure TGLSkinEditorForm.EditKeyPress(Sender: TObject; var Key: Char);
  708. begin
  709. if (Key > #32) and not CharInSet(Key, ['0'..'9',
  710. FormatSettings.DecimalSeparator]) then
  711. Key := #0;
  712. end;
  713. procedure TGLSkinEditorForm.AlignZoomPanel;
  714. begin
  715. imgFull.Left := 0;
  716. imgFull.Top := 0;
  717. imgFull.Width := panZoomImage.Width - sbarVertical.Width - 1;
  718. imgFull.Height := panZoomImage.Height - sbarHorizontal.Height - 1;
  719. imgFull.Picture.Bitmap.Width := imgFull.Width;
  720. imgFull.Picture.Bitmap.Height := imgFull.Height;
  721. sbarVertical.Left := imgFull.Width;
  722. sbarVertical.Top := 0;
  723. sbarVertical.Height := imgFull.Height;
  724. sbarHorizontal.Left := 0;
  725. sbarHorizontal.Top := imgFull.Height;
  726. sbarHorizontal.Width := imgFull.Width;
  727. Render;
  728. end;
  729. procedure TGLSkinEditorForm.UpdateRegionEdits;
  730. begin
  731. if (lbElements.ItemIndex >= 0) and (lbElements.ItemIndex <
  732. lbElements.Items.Count) then
  733. begin
  734. SelectedElement := TheGuiComponent.Items[lbElements.ItemIndex];
  735. ComboBox1.ItemIndex := Integer(SelectedElement.Align);
  736. ScaleXEdit.Text := FloatToStr(SelectedElement.Scale.X);
  737. ScaleYEdit.Text := FloatToStr(SelectedElement.Scale.Y);
  738. LeftEdit.Text := FloatToStr(SelectedElement.TopLeft.X);
  739. TopEdit.Text := FloatToStr(SelectedElement.TopLeft.Y);
  740. RightEdit.Text := FloatToStr(SelectedElement.BottomRight.X);
  741. BottomEdit.Text := FloatToStr(SelectedElement.BottomRight.Y);
  742. SetEditState(Self, True);
  743. Render;
  744. end
  745. else
  746. begin
  747. SelectedElement := nil;
  748. SetEditState(Self, False);
  749. end;
  750. end;
  751. procedure TGLSkinEditorForm.SetEditState(Parent: TControl; Enabled: Boolean);
  752. var
  753. i: Integer;
  754. begin
  755. for i := 0 to Parent.ComponentCount - 1 do
  756. begin
  757. if Parent.Components[i].Tag <> 1 then
  758. continue;
  759. if Parent.Components[i] is TLabel then
  760. begin
  761. (Parent.Components[i] as TLabel).Enabled := Enabled;
  762. end
  763. else if Parent.Components[i] is TEdit then
  764. begin
  765. (Parent.Components[i] as TEdit).Enabled := Enabled;
  766. if Enabled then
  767. begin
  768. (Parent.Components[i] as TEdit).Color := clWindow;
  769. end
  770. else
  771. begin
  772. (Parent.Components[i] as TEdit).Text := '';
  773. (Parent.Components[i] as TEdit).Color := clBtnFace;
  774. end;
  775. end
  776. else if Parent.Components[i] is TComboBox then
  777. begin
  778. (Parent.Components[i] as TComboBox).Enabled := Enabled;
  779. if Enabled then
  780. begin
  781. (Parent.Components[i] as TComboBox).Color := clWindow;
  782. end
  783. else
  784. begin
  785. (Parent.Components[i] as TComboBox).ItemIndex := -1;
  786. (Parent.Components[i] as TComboBox).Color := clBtnFace;
  787. end;
  788. end
  789. else if Parent.Components[i] is TButton then
  790. begin
  791. (Parent.Components[i] as TButton).Enabled := Enabled;
  792. end;
  793. end;
  794. end;
  795. procedure TGLSkinEditorForm.FormResize(Sender: TObject);
  796. begin
  797. AlignZoomPanel;
  798. end;
  799. procedure TGLSkinEditorForm.MenuItemClick(Sender: TObject);
  800. begin
  801. AddElement((Sender as TMenuItem).Tag);
  802. end;
  803. procedure TGLSkinEditorForm.mnuAddAllClick(Sender: TObject);
  804. var
  805. i: Integer;
  806. begin
  807. for i := 0 to 8 do
  808. begin
  809. AddElement(i);
  810. end;
  811. end;
  812. procedure TGLSkinEditorForm.AddElement(Index: Integer);
  813. var
  814. S: string;
  815. Count: Integer;
  816. NewElement: TGLGuiElement;
  817. begin
  818. Count := 1;
  819. repeat
  820. S := ComboBox1.Items[Index] + IntToStr(Count);
  821. inc(Count);
  822. until lbElements.Items.IndexOf(S) = -1;
  823. NewElement := TheGuiComponent.Add as TGLGuiElement;
  824. NewElement.Name := S;
  825. NewElement.Align := TGUIAlignments(Index);
  826. NewElement.BottomRight.SetPoint2D(0, 0);
  827. NewElement.TopLeft.SetPoint2D(0, 0);
  828. lbElements.ItemIndex := lbElements.Items.Add(S);
  829. UpdateRegionEdits;
  830. end;
  831. procedure TGLSkinEditorForm.mnuAllTopClick(Sender: TObject);
  832. var
  833. i: Integer;
  834. begin
  835. for i := 0 to 2 do
  836. begin
  837. AddElement(i);
  838. end;
  839. end;
  840. procedure TGLSkinEditorForm.mnuAllMiddleClick(Sender: TObject);
  841. var
  842. i: Integer;
  843. begin
  844. for i := 3 to 5 do
  845. begin
  846. AddElement(i);
  847. end;
  848. end;
  849. procedure TGLSkinEditorForm.mnuAllBottomClick(Sender: TObject);
  850. var
  851. i: Integer;
  852. begin
  853. for i := 6 to 8 do
  854. begin
  855. AddElement(i);
  856. end;
  857. end;
  858. procedure TGLSkinEditorForm.imgPreviewMouseDown(Sender: TObject;
  859. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  860. begin
  861. PreviewMousePoint := Point(X, Y);
  862. end;
  863. procedure TGLSkinEditorForm.imgPreviewMouseMove(Sender: TObject;
  864. Shift: TShiftState; X, Y: Integer);
  865. begin
  866. if PtInRect(VisibleRect, Point(X, Y)) then
  867. begin
  868. imgPreview.Cursor := crHandPoint;
  869. end
  870. else
  871. begin
  872. imgPreview.Cursor := crDefault;
  873. end;
  874. if ssLeft in Shift then
  875. begin
  876. if imgPreview.Cursor <> crDefault then
  877. begin
  878. sbarVertical.Position := sbarVertical.Position + Round((Y -
  879. PreviewMousePoint.Y) * PreviewWidth / imgPreview.Width);
  880. sbarHorizontal.Position := sbarHorizontal.Position + Round((X -
  881. PreviewMousePoint.X) * PreviewHeight / imgPreview.Height);
  882. end;
  883. PreviewMousePoint := Point(X, Y);
  884. VisibleRect := Rect(
  885. Round(sbarHorizontal.Position / PreviewWidth * imgPreview.Width),
  886. Round(sbarVertical.Position / PreviewHeight * imgPreview.Height),
  887. Round((sbarHorizontal.Position + (imgFull.Width - 1) / Zoom) / PreviewWidth
  888. * imgPreview.Width),
  889. Round((sbarVertical.Position + (imgFull.Height - 1) / Zoom) / PreviewHeight
  890. * imgPreview.Height)
  891. );
  892. end;
  893. end;
  894. procedure TGLSkinEditorForm.DrawCrossair(Point: TPoint);
  895. begin
  896. if Point.X < 0 then
  897. Exit;
  898. imgFull.Canvas.Pen.Color := clWhite;
  899. imgFull.Canvas.Pen.Style := psDot;
  900. imgFull.Canvas.Pen.Mode := pmXor;
  901. imgFull.Canvas.MoveTo(Point.X, 0);
  902. imgFull.Canvas.LineTo(Point.X, imgFull.Height);
  903. imgFull.Canvas.MoveTo(0, Point.Y);
  904. imgFull.Canvas.LineTo(imgFull.Width, Point.Y);
  905. end;
  906. procedure TGLSkinEditorForm.ImageWndProc(var Message: TMessage);
  907. begin
  908. if (Message.Msg = CM_MOUSELEAVE) then
  909. begin
  910. DrawCrossair(FullMousePoint);
  911. FullMousePoint := Point(-1, -1);
  912. end;
  913. FOriginalWndProc(Message);
  914. end;
  915. end.