| 1 |
{%MainUnit castleeditorpropedits.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2023 Michalis Kamburelis. |
| 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 |
{ Color property editors and TCastlePropertyEditorHook |
| 18 |
(for now used only by color property editors). } |
| 19 |
|
| 20 |
{ TCastleColorPropertyEditor ------------------------------------------------- } |
| 21 |
|
| 22 |
{$ifdef read_interface} |
| 23 |
type |
| 24 |
TCastlePropertyEditorHook = class(TPropertyEditorHook) |
| 25 |
public |
| 26 |
{ Used by color picker to specify the position to show the window. } |
| 27 |
InspectorAreaOnScreen: TRect; |
| 28 |
end; |
| 29 |
|
| 30 |
{ Abstract class for grouping all color property editors } |
| 31 |
TCastleAbstractColorPropertyEditor = class(TSubPropertiesEditor) |
| 32 |
end; |
| 33 |
|
| 34 |
TCastleColorPropertyEditor = class(TCastleAbstractColorPropertyEditor) |
| 35 |
strict private |
| 36 |
procedure SetAllValues(const NewValue: TCastleColor); |
| 37 |
public |
| 38 |
function GetAttributes: TPropertyAttributes; override; |
| 39 |
procedure Edit; override; |
| 40 |
function GetName: shortstring; override; |
| 41 |
function GetValue: String; override; |
| 42 |
procedure SetValue(const NewValue: String); override; |
| 43 |
function GetDefaultValue: String; override; |
| 44 |
function ValueIsStreamed: boolean; override; |
| 45 |
function AllEqual: Boolean; override; |
| 46 |
end; |
| 47 |
{$endif read_interface} |
| 48 |
|
| 49 |
{$ifdef read_implementation} |
| 50 |
|
| 51 |
function TCastleColorPropertyEditor.GetAttributes: TPropertyAttributes; |
| 52 |
begin |
| 53 |
Result := inherited GetAttributes + [paDialog, paRevertable] |
| 54 |
// our SetValue allows to set color from string |
| 55 |
- [paReadOnly]; |
| 56 |
end; |
| 57 |
|
| 58 |
procedure TCastleColorPropertyEditor.Edit; |
| 59 |
var |
| 60 |
ColorPersistent: TCastleColorPersistent; |
| 61 |
Color: TCastleColor; |
| 62 |
ColorPicker: TCastleColorPickerForm; |
| 63 |
InspectorArea: TRect; |
| 64 |
CastlePropertyHook: TCastlePropertyEditorHook; |
| 65 |
begin |
| 66 |
ColorPicker := TCastleColorPickerForm.Create(nil); |
| 67 |
try |
| 68 |
ColorPersistent := (GetObjectValue as TCastleColorPersistent); |
| 69 |
Color := ColorPersistent.Value; |
| 70 |
ColorPicker.Init(Self, Color); |
| 71 |
|
| 72 |
if (PropertyHook <> nil) and (PropertyHook is TCastlePropertyEditorHook) then |
| 73 |
begin |
| 74 |
CastlePropertyHook := PropertyHook as TCastlePropertyEditorHook; |
| 75 |
ColorPicker.Position := poDesigned; |
| 76 |
InspectorArea := CastlePropertyHook.InspectorAreaOnScreen; |
| 77 |
ColorPicker.Left := InspectorArea.Left - |
| 78 |
(ColorPicker.Width - InspectorArea.Width); |
| 79 |
if InspectorArea.Height > ColorPicker.Height then |
| 80 |
ColorPicker.Top := InspectorArea.Top + |
| 81 |
Round((InspectorArea.Height - ColorPicker.Height) / 2) |
| 82 |
else |
| 83 |
ColorPicker.Top := InspectorArea.Top; |
| 84 |
end else |
| 85 |
ColorPicker.Position := poOwnerFormCenter; |
| 86 |
|
| 87 |
ColorPicker.ShowModal; |
| 88 |
SetAllValues(ColorPicker.CurrentCastleColor); |
| 89 |
finally |
| 90 |
FreeAndNil(ColorPicker); |
| 91 |
end; |
| 92 |
end; |
| 93 |
|
| 94 |
function TCastleColorPropertyEditor.GetName: shortstring; |
| 95 |
begin |
| 96 |
Result := inherited GetName; |
| 97 |
Result := SuffixRemove('persistent', Result, true); |
| 98 |
end; |
| 99 |
|
| 100 |
function TCastleColorPropertyEditor.GetValue: String; |
| 101 |
var |
| 102 |
ColorPersistent: TCastleColorPersistent; |
| 103 |
begin |
| 104 |
ColorPersistent := (GetObjectValue as TCastleColorPersistent); |
| 105 |
Result := ColorToHex(ColorPersistent.Value); |
| 106 |
end; |
| 107 |
|
| 108 |
procedure TCastleColorPropertyEditor.SetAllValues(const NewValue: TCastleColor); |
| 109 |
var |
| 110 |
I: Integer; |
| 111 |
ColorPersistent: TCastleColorPersistent; |
| 112 |
begin |
| 113 |
for I := 0 to PropCount - 1 do |
| 114 |
begin |
| 115 |
ColorPersistent := GetObjectValueAt(I) as TCastleColorPersistent; |
| 116 |
ColorPersistent.Value := NewValue; |
| 117 |
end; |
| 118 |
Modified; |
| 119 |
end; |
| 120 |
|
| 121 |
procedure TCastleColorPropertyEditor.SetValue(const NewValue: String); |
| 122 |
begin |
| 123 |
SetAllValues(HexToColor(NewValue)); |
| 124 |
end; |
| 125 |
|
| 126 |
function TCastleColorPropertyEditor.GetDefaultValue: String; |
| 127 |
var |
| 128 |
ColorPersistent: TCastleColorPersistent; |
| 129 |
begin |
| 130 |
ColorPersistent := (GetObjectValue as TCastleColorPersistent); |
| 131 |
Result := ColorPersistent.InternalDefaultValue.ToString; |
| 132 |
end; |
| 133 |
|
| 134 |
function TCastleColorPropertyEditor.ValueIsStreamed: boolean; |
| 135 |
var |
| 136 |
ColorPersistent: TCastleColorPersistent; |
| 137 |
begin |
| 138 |
ColorPersistent := (GetObjectValue as TCastleColorPersistent); |
| 139 |
Result := ColorPersistent.ValueIsStreamed; |
| 140 |
end; |
| 141 |
|
| 142 |
function TCastleColorPropertyEditor.AllEqual: Boolean; |
| 143 |
var |
| 144 |
I: Integer; |
| 145 |
Value1, Value2: TCastleColor; |
| 146 |
begin |
| 147 |
if PropCount > 1 then |
| 148 |
begin |
| 149 |
Value1 := (GetObjectValueAt(0) as TCastleColorPersistent).Value; |
| 150 |
for I := 1 to PropCount - 1 do |
| 151 |
begin |
| 152 |
Value2 := (GetObjectValueAt(I) as TCastleColorPersistent).Value; |
| 153 |
if not TCastleColor.PerfectlyEquals(Value1, Value2) then |
| 154 |
Exit(false); |
| 155 |
end; |
| 156 |
end; |
| 157 |
|
| 158 |
Result := true; |
| 159 |
end; |
| 160 |
|
| 161 |
{ TCastleColorRGBPropertyEditor ------------------------------------------------- } |
| 162 |
|
| 163 |
type |
| 164 |
TCastleColorRGBPropertyEditor = class(TCastleAbstractColorPropertyEditor) |
| 165 |
strict private |
| 166 |
procedure SetAllValues(const NewValue: TCastleColorRGB); |
| 167 |
public |
| 168 |
function GetAttributes: TPropertyAttributes; override; |
| 169 |
procedure Edit; override; |
| 170 |
function GetName: shortstring; override; |
| 171 |
function GetValue: String; override; |
| 172 |
procedure SetValue(const NewValue: String); override; |
| 173 |
function GetDefaultValue: String; override; |
| 174 |
function ValueIsStreamed: boolean; override; |
| 175 |
function AllEqual: Boolean; override; |
| 176 |
end; |
| 177 |
|
| 178 |
function TCastleColorRGBPropertyEditor.GetAttributes: TPropertyAttributes; |
| 179 |
begin |
| 180 |
Result := inherited GetAttributes + [paDialog, paRevertable] |
| 181 |
// our SetValue allows to set color from string |
| 182 |
- [paReadOnly]; |
| 183 |
end; |
| 184 |
|
| 185 |
procedure TCastleColorRGBPropertyEditor.Edit; |
| 186 |
var |
| 187 |
ColorPersistent: TCastleColorRGBPersistent; |
| 188 |
Color: TCastleColorRGB; |
| 189 |
ColorPicker: TCastleColorPickerForm; |
| 190 |
InspectorArea: TRect; |
| 191 |
CastlePropertyHook: TCastlePropertyEditorHook; |
| 192 |
begin |
| 193 |
ColorPicker := TCastleColorPickerForm.Create(nil); |
| 194 |
try |
| 195 |
ColorPersistent := (GetObjectValue as TCastleColorRGBPersistent); |
| 196 |
Color := ColorPersistent.Value; |
| 197 |
ColorPicker.Init(Self, Color); |
| 198 |
|
| 199 |
if (PropertyHook <> nil) and (PropertyHook is TCastlePropertyEditorHook) then |
| 200 |
begin |
| 201 |
CastlePropertyHook := PropertyHook as TCastlePropertyEditorHook; |
| 202 |
ColorPicker.Position := poDefaultPosOnly; |
| 203 |
InspectorArea := CastlePropertyHook.InspectorAreaOnScreen; |
| 204 |
ColorPicker.Left := InspectorArea.Left - |
| 205 |
(ColorPicker.Width - InspectorArea.Width); |
| 206 |
if InspectorArea.Height > ColorPicker.Height then |
| 207 |
ColorPicker.Top := InspectorArea.Top + |
| 208 |
Round((InspectorArea.Height - ColorPicker.Height) / 2) |
| 209 |
else |
| 210 |
ColorPicker.Top := InspectorArea.Top; |
| 211 |
end else |
| 212 |
ColorPicker.Position := poOwnerFormCenter; |
| 213 |
|
| 214 |
ColorPicker.ShowModal; |
| 215 |
SetAllValues(ColorPicker.CurrentCastleColorRGB); |
| 216 |
finally |
| 217 |
FreeAndNil(ColorPicker); |
| 218 |
end; |
| 219 |
end; |
| 220 |
|
| 221 |
|
| 222 |
function TCastleColorRGBPropertyEditor.GetName: shortstring; |
| 223 |
begin |
| 224 |
Result := inherited GetName; |
| 225 |
Result := SuffixRemove('persistent', Result, true); |
| 226 |
end; |
| 227 |
|
| 228 |
function TCastleColorRGBPropertyEditor.GetValue: String; |
| 229 |
var |
| 230 |
ColorPersistent: TCastleColorRGBPersistent; |
| 231 |
begin |
| 232 |
ColorPersistent := (GetObjectValue as TCastleColorRGBPersistent); |
| 233 |
Result := ColorRGBToHex(ColorPersistent.Value); |
| 234 |
end; |
| 235 |
|
| 236 |
procedure TCastleColorRGBPropertyEditor.SetAllValues(const NewValue: TCastleColorRGB); |
| 237 |
var |
| 238 |
I: Integer; |
| 239 |
ColorPersistent: TCastleColorRGBPersistent; |
| 240 |
begin |
| 241 |
for I := 0 to PropCount - 1 do |
| 242 |
begin |
| 243 |
ColorPersistent := GetObjectValueAt(I) as TCastleColorRGBPersistent; |
| 244 |
ColorPersistent.Value := NewValue; |
| 245 |
end; |
| 246 |
Modified; |
| 247 |
end; |
| 248 |
|
| 249 |
procedure TCastleColorRGBPropertyEditor.SetValue(const NewValue: String); |
| 250 |
begin |
| 251 |
SetAllValues(HexToColorRGB(NewValue)); |
| 252 |
end; |
| 253 |
|
| 254 |
function TCastleColorRGBPropertyEditor.GetDefaultValue: String; |
| 255 |
var |
| 256 |
ColorPersistent: TCastleColorRGBPersistent; |
| 257 |
begin |
| 258 |
ColorPersistent := (GetObjectValue as TCastleColorRGBPersistent); |
| 259 |
Result := ColorPersistent.InternalDefaultValue.ToString; |
| 260 |
end; |
| 261 |
|
| 262 |
function TCastleColorRGBPropertyEditor.ValueIsStreamed: boolean; |
| 263 |
var |
| 264 |
ColorPersistent: TCastleColorRGBPersistent; |
| 265 |
begin |
| 266 |
ColorPersistent := (GetObjectValue as TCastleColorRGBPersistent); |
| 267 |
Result := ColorPersistent.ValueIsStreamed; |
| 268 |
end; |
| 269 |
|
| 270 |
function TCastleColorRGBPropertyEditor.AllEqual: Boolean; |
| 271 |
var |
| 272 |
I: Integer; |
| 273 |
Value1, Value2: TCastleColorRGB; |
| 274 |
begin |
| 275 |
if PropCount > 1 then |
| 276 |
begin |
| 277 |
Value1 := (GetObjectValueAt(0) as TCastleColorRGBPersistent).Value; |
| 278 |
for I := 1 to PropCount - 1 do |
| 279 |
begin |
| 280 |
Value2 := (GetObjectValueAt(I) as TCastleColorRGBPersistent).Value; |
| 281 |
if not TCastleColorRGB.PerfectlyEquals(Value1, Value2) then |
| 282 |
Exit(false); |
| 283 |
end; |
| 284 |
end; |
| 285 |
|
| 286 |
Result := true; |
| 287 |
end; |
| 288 |
|
| 289 |
{$endif read_implementation} |