| 1 |
{%MainUnit castlepropedits.pas} |
| 2 |
{ |
| 3 |
Copyright 2023 Freedomax. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Region editor. } |
| 18 |
|
| 19 |
{ TCastleRegionEditor ------------------------------------------------- } |
| 20 |
|
| 21 |
type |
| 22 |
{ Edit either TFloatRectanglePersistent or TBorder (when used for 9-slices). } |
| 23 |
TCastleRegionEditor = class(TSubPropertiesEditor) |
| 24 |
public |
| 25 |
function GetName: shortstring; override; |
| 26 |
function GetValue: String; override; |
| 27 |
function GetAttributes: TPropertyAttributes; override; |
| 28 |
procedure Edit; override; |
| 29 |
end; |
| 30 |
|
| 31 |
function TCastleRegionEditor.GetName: shortstring; |
| 32 |
begin |
| 33 |
Result := inherited GetName; |
| 34 |
Result := SuffixRemove('persistent', Result, True); |
| 35 |
end; |
| 36 |
|
| 37 |
function TCastleRegionEditor.GetValue: String; |
| 38 |
var |
| 39 |
VClassName: String; |
| 40 |
VName: String; |
| 41 |
begin |
| 42 |
VName := GetName; |
| 43 |
if VName = 'Region' then |
| 44 |
VClassName := 'TFloatRectangle' |
| 45 |
else |
| 46 |
VClassName := GetPropInfo^.PropType^.Name; |
| 47 |
Result := '(' + SuffixRemove('persistent', VClassName, True) + ')'; |
| 48 |
if VName = 'ProtectedSides' then |
| 49 |
Result := Result + ' (9-slice scaling)'; |
| 50 |
end; |
| 51 |
|
| 52 |
function TCastleRegionEditor.GetAttributes: TPropertyAttributes; |
| 53 |
begin |
| 54 |
Result := (inherited GetAttributes) + [paDialog]; |
| 55 |
end; |
| 56 |
|
| 57 |
procedure TCastleRegionEditor.Edit; |
| 58 |
var |
| 59 |
D: TRegionDesignDialog; |
| 60 |
Comp: TPersistent; |
| 61 |
ImagePersistent: TCastleImagePersistent; |
| 62 |
VClassName: String; |
| 63 |
begin |
| 64 |
ImagePersistent := nil; |
| 65 |
|
| 66 |
Comp := GetComponent(0); |
| 67 |
if (Comp is TCastleImagePersistent) then |
| 68 |
ImagePersistent := Comp as TCastleImagePersistent |
| 69 |
else |
| 70 |
if (Comp is TCastleImageControl) then |
| 71 |
ImagePersistent := (Comp as TCastleImageControl).Content; |
| 72 |
|
| 73 |
if not Assigned(ImagePersistent) then |
| 74 |
begin |
| 75 |
ShowMessage('ImagePersistent does not exist.'); |
| 76 |
Exit; |
| 77 |
end; |
| 78 |
if (not Assigned(ImagePersistent.DrawableImage)) or |
| 79 |
(ImagePersistent.DrawableImage.Rect.IsEmpty) then |
| 80 |
begin |
| 81 |
ShowMessage('Please load the image first.'); |
| 82 |
Exit; |
| 83 |
end; |
| 84 |
|
| 85 |
VClassName := GetPropInfo^.PropType^.Name; |
| 86 |
|
| 87 |
D := TRegionDesignDialog.Create(Application); |
| 88 |
try |
| 89 |
if VClassName = TFloatRectanglePersistent.ClassName then |
| 90 |
begin |
| 91 |
D.Caption := 'Edit Region'; |
| 92 |
D.Load(ImagePersistent.DrawableImage, ImagePersistent.Region); |
| 93 |
end |
| 94 |
else |
| 95 |
begin |
| 96 |
D.Caption := 'Edit Border'; |
| 97 |
D.Load(ImagePersistent.DrawableImage, ImagePersistent.Region, |
| 98 |
ImagePersistent.ProtectedSides); |
| 99 |
end; |
| 100 |
|
| 101 |
if D.ShowModal = mrOk then |
| 102 |
begin |
| 103 |
D.ApplyChange; |
| 104 |
|
| 105 |
case D.DesignMode of |
| 106 |
dmRegion: |
| 107 |
ImagePersistent.Region := D.Region; |
| 108 |
dmBorder: |
| 109 |
D.ApplyToBorder(ImagePersistent.ProtectedSides); |
| 110 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 111 |
else raise EInternalError.Create('D.DesignMode?'); |
| 112 |
{$endif} |
| 113 |
end; |
| 114 |
|
| 115 |
Modified(''); |
| 116 |
end; |
| 117 |
|
| 118 |
finally |
| 119 |
FreeAndNil(D); |
| 120 |
end; |
| 121 |
end; |