2
0

FmGuiSkinEditor.pas 26 KB

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