| 1 |
{%MainUnit castlecontrols.pas} |
| 2 |
{ |
| 3 |
Copyright 2010-2024 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 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
{ Possible shape type, for @link(TCastleShape.ShapeType). } |
| 20 |
TShapeType = ( |
| 21 |
stRectangle, |
| 22 |
stCircle, |
| 23 |
stTriangleUp, |
| 24 |
stTriangleDown, |
| 25 |
stTriangleRight, |
| 26 |
stTriangleLeft |
| 27 |
); |
| 28 |
|
| 29 |
{ Draw a simple shape (rectangle, circle, triangle) with given color |
| 30 |
and optional outline. } |
| 31 |
TCastleShape = class(TCastleUserInterface) |
| 32 |
strict private |
| 33 |
FFilled, FOutline, FOutlineThick: boolean; |
| 34 |
FColor, FOutlineColor: TCastleColor; |
| 35 |
FOutlineWidth: Single; |
| 36 |
FShapeType: TShapeType; |
| 37 |
procedure SetShapeType(const Value: TShapeType); |
| 38 |
procedure SetFilled(const Value: boolean); |
| 39 |
procedure SetColor(const Value: TCastleColor); |
| 40 |
procedure SetOutline(const Value: boolean); |
| 41 |
procedure SetOutlineColor(const Value: TCastleColor); |
| 42 |
procedure SetOutlineWidth(const Value: Single); |
| 43 |
procedure SetOutlineThick(const Value: boolean); |
| 44 |
public |
| 45 |
constructor Create(AOwner: TComponent); override; |
| 46 |
destructor Destroy; override; |
| 47 |
procedure Render; override; |
| 48 |
function PropertySections(const PropertyName: String): TPropertySections; override; |
| 49 |
|
| 50 |
{ The fill color, used if @link(Filled). By default, opaque white. } |
| 51 |
property Color: TCastleColor read FColor write SetColor; |
| 52 |
|
| 53 |
{ The outline color, used if @link(Outline). By default, opaque black. } |
| 54 |
property OutlineColor: TCastleColor read FOutlineColor write SetOutlineColor; |
| 55 |
published |
| 56 |
{ The shape (rectangle, circle, triangle) to be drawn. } |
| 57 |
property ShapeType: TShapeType read FShapeType write SetShapeType default stRectangle; |
| 58 |
|
| 59 |
{ Determines the drawing method of the outline, used if @link(Outline). |
| 60 |
|
| 61 |
@definitionList( |
| 62 |
@itemLabel(@false (default)) |
| 63 |
@item(Draw the outline using lines, and apply OutlineWidth by changing |
| 64 |
line width. |
| 65 |
|
| 66 |
Disadvantage: |
| 67 |
@bold(outline widths thicker than 1 pixel are not guaranteed |
| 68 |
to be supported. In particular they will almost never work on mobile |
| 69 |
(OpenGLES).) |
| 70 |
|
| 71 |
Consider using other methods if you need to draw a thick shape outline |
| 72 |
in a reliable manner. |
| 73 |
For example, set @link(OutlineThick) to @true. |
| 74 |
Or draw the shape as an image with a frame, |
| 75 |
using @link(TCastleImageControl) and @link(TCastleImageControl.ProtectedSides).) |
| 76 |
|
| 77 |
@itemLabel(@true) |
| 78 |
@item(Draw the outline by first drawing a larger shape with OutlineColor |
| 79 |
underneath the smaller shape with Color. |
| 80 |
|
| 81 |
Disadvantages: |
| 82 |
@unorderedList( |
| 83 |
@item(Cannot work sensibly if @link(Filled) is @false, |
| 84 |
so it's disabled then. When @link(Filled) is @false, |
| 85 |
it's like OutlineThick was always also @false.) |
| 86 |
@item(The alpha blending may not be exactly what you want, |
| 87 |
since the pixels inside are overdrawn with both OutlineColor |
| 88 |
and then with Color.) |
| 89 |
@item(May look a little worse in case of small OutlineWidth |
| 90 |
and non-rectangular shapes.) |
| 91 |
) |
| 92 |
|
| 93 |
Advantage: thick OutlineWidth works reliably.) |
| 94 |
) |
| 95 |
} |
| 96 |
property OutlineThick: boolean read FOutlineThick write SetOutlineThick default false; |
| 97 |
|
| 98 |
{ The outline width, used if @link(Outline). |
| 99 |
It is affected by UI scaling. |
| 100 |
|
| 101 |
If OutlineThick is @false, then |
| 102 |
@bold(outline widths thicker than 1 pixel are not guaranteed |
| 103 |
to be supported. In particular they will almost never work on mobile (OpenGLES).) |
| 104 |
Change OutlineThick to @true to have reliable thick outlines. } |
| 105 |
property OutlineWidth: Single read FOutlineWidth write SetOutlineWidth |
| 106 |
{$ifdef FPC}default 1.0{$endif}; |
| 107 |
|
| 108 |
{ Display outline of the shape using @link(OutlineColor). } |
| 109 |
property Outline: boolean read FOutline write SetOutline default false; |
| 110 |
|
| 111 |
{ Display inside of the shape using @link(Color). } |
| 112 |
property Filled: boolean read FFilled write SetFilled default true; |
| 113 |
|
| 114 |
{$define read_interface_class} |
| 115 |
{$I auto_generated_persistent_vectors/tcastleshape_persistent_vectors.inc} |
| 116 |
{$undef read_interface_class} |
| 117 |
end; |
| 118 |
|
| 119 |
{$endif read_interface} |
| 120 |
|
| 121 |
{$ifdef read_implementation} |
| 122 |
|
| 123 |
{ TCastleShape --------------------------------------------------------------- } |
| 124 |
|
| 125 |
constructor TCastleShape.Create(AOwner: TComponent); |
| 126 |
begin |
| 127 |
inherited; |
| 128 |
FShapeType := stRectangle; |
| 129 |
FFilled := true; |
| 130 |
FColor := White; |
| 131 |
FOutline := false; |
| 132 |
FOutlineWidth := 1.0; |
| 133 |
FOutlineColor := Black; |
| 134 |
|
| 135 |
{$define read_implementation_constructor} |
| 136 |
{$I auto_generated_persistent_vectors/tcastleshape_persistent_vectors.inc} |
| 137 |
{$undef read_implementation_constructor} |
| 138 |
end; |
| 139 |
|
| 140 |
destructor TCastleShape.Destroy; |
| 141 |
begin |
| 142 |
{$define read_implementation_destructor} |
| 143 |
{$I auto_generated_persistent_vectors/tcastleshape_persistent_vectors.inc} |
| 144 |
{$undef read_implementation_destructor} |
| 145 |
inherited; |
| 146 |
end; |
| 147 |
|
| 148 |
procedure TCastleShape.SetShapeType(const Value: TShapeType); |
| 149 |
begin |
| 150 |
if FShapeType <> Value then |
| 151 |
begin |
| 152 |
FShapeType := Value; |
| 153 |
VisibleChange([chRender]); |
| 154 |
end; |
| 155 |
end; |
| 156 |
|
| 157 |
procedure TCastleShape.SetFilled(const Value: boolean); |
| 158 |
begin |
| 159 |
if FFilled <> Value then |
| 160 |
begin |
| 161 |
FFilled := Value; |
| 162 |
VisibleChange([chRender]); |
| 163 |
end; |
| 164 |
end; |
| 165 |
|
| 166 |
procedure TCastleShape.SetColor(const Value: TCastleColor); |
| 167 |
begin |
| 168 |
if not TCastleColor.PerfectlyEquals(FColor, Value) then |
| 169 |
begin |
| 170 |
FColor := Value; |
| 171 |
VisibleChange([chRender]); |
| 172 |
end; |
| 173 |
end; |
| 174 |
|
| 175 |
procedure TCastleShape.SetOutline(const Value: boolean); |
| 176 |
begin |
| 177 |
if FOutline <> Value then |
| 178 |
begin |
| 179 |
FOutline := Value; |
| 180 |
VisibleChange([chRender]); |
| 181 |
end; |
| 182 |
end; |
| 183 |
|
| 184 |
procedure TCastleShape.SetOutlineColor(const Value: TCastleColor); |
| 185 |
begin |
| 186 |
if not TCastleColor.PerfectlyEquals(FOutlineColor, Value) then |
| 187 |
begin |
| 188 |
FOutlineColor := Value; |
| 189 |
VisibleChange([chRender]); |
| 190 |
end; |
| 191 |
end; |
| 192 |
|
| 193 |
procedure TCastleShape.SetOutlineWidth(const Value: Single); |
| 194 |
begin |
| 195 |
if FOutlineWidth <> Value then |
| 196 |
begin |
| 197 |
FOutlineWidth := Value; |
| 198 |
VisibleChange([chRender]); |
| 199 |
end; |
| 200 |
end; |
| 201 |
|
| 202 |
procedure TCastleShape.SetOutlineThick(const Value: boolean); |
| 203 |
begin |
| 204 |
if FOutlineThick <> Value then |
| 205 |
begin |
| 206 |
FOutlineThick := Value; |
| 207 |
VisibleChange([chRender]); |
| 208 |
end; |
| 209 |
end; |
| 210 |
|
| 211 |
type |
| 212 |
TShapeTypeTriangle = stTriangleUp..stTriangleLeft; |
| 213 |
|
| 214 |
{ Draw triangle type indicated by current ShapeType. |
| 215 |
Use only when ShapeType is stTriangleXxx. |
| 216 |
|
| 217 |
@param R Rectangle to draw the triangle inside. |
| 218 |
@param Color Color of the triangle. |
| 219 |
@param Filled Whether we should fill the triangle, otherwise we draw only lines. |
| 220 |
@param LineWidth Line width used when Filled = @false. |
| 221 |
|
| 222 |
Note: This is internal, and deliberately not a method of TCastleShape, |
| 223 |
to not use some TCastleShape property by accident. } |
| 224 |
procedure DrawTriangleCore(const ShapeType: TShapeTypeTriangle; |
| 225 |
const R: TFloatRectangle; const Color: TCastleColor; const Filled: Boolean; |
| 226 |
const LineWidth: Single = 1.0); |
| 227 |
var |
| 228 |
Mode: TPrimitiveMode; |
| 229 |
Points: array [0..2] of TVector2; |
| 230 |
begin |
| 231 |
if Filled then |
| 232 |
Mode := pmTriangles |
| 233 |
else |
| 234 |
Mode := pmLineLoop; |
| 235 |
|
| 236 |
case ShapeType of |
| 237 |
stTriangleUp: |
| 238 |
begin |
| 239 |
Points[0] := Vector2(R.Left, R.Bottom); |
| 240 |
Points[1] := Vector2(R.Right, R.Bottom); |
| 241 |
Points[2] := Vector2((R.Left + R.Right) / 2, R.Top); |
| 242 |
end; |
| 243 |
stTriangleDown: |
| 244 |
begin |
| 245 |
Points[0] := Vector2(R.Right, R.Top); |
| 246 |
Points[1] := Vector2(R.Left, R.Top); |
| 247 |
Points[2] := Vector2((R.Left + R.Right) / 2, R.Bottom); |
| 248 |
end; |
| 249 |
stTriangleRight: |
| 250 |
begin |
| 251 |
Points[0] := Vector2(R.Left, R.Bottom); |
| 252 |
Points[1] := Vector2(R.Right, (R.Top + R.Bottom) / 2); |
| 253 |
Points[2] := Vector2(R.Left, R.Top); |
| 254 |
end; |
| 255 |
stTriangleLeft: |
| 256 |
begin |
| 257 |
Points[0] := Vector2(R.Right, R.Top); |
| 258 |
Points[1] := Vector2(R.Left, (R.Top + R.Bottom) / 2); |
| 259 |
Points[2] := Vector2(R.Right, R.Bottom); |
| 260 |
end; |
| 261 |
else raise EInternalError.Create('DrawTriangleCore only useful for triangle shapes'); |
| 262 |
end; |
| 263 |
|
| 264 |
DrawPrimitive2D(Mode, Points, Color, |
| 265 |
{ We specify BlendingSourceFactor, BlendingDestinationFactor, ForceBlending |
| 266 |
equal to default, only to be able to provide explicit LineWidth. } |
| 267 |
bsSrcAlpha, bdOneMinusSrcAlpha, false, LineWidth); |
| 268 |
end; |
| 269 |
|
| 270 |
procedure TCastleShape.Render; |
| 271 |
|
| 272 |
function RectGrowForTriangleOutline(const RR: TFloatRectangle; |
| 273 |
const OutlineOut: Single): TFloatRectangle; |
| 274 |
var |
| 275 |
ExtraGrowTip: Single; |
| 276 |
begin |
| 277 |
Result := RR.Grow(OutlineOut); |
| 278 |
{ Expand RR more in the direction of the triangle, makes the outline look |
| 279 |
even in typical cases. |
| 280 |
TODO: Use trigonometry to calculate this more precisely. } |
| 281 |
ExtraGrowTip := 2 * OutlineOut; |
| 282 |
case ShapeType of |
| 283 |
stTriangleUp: Result := Result.GrowTop(ExtraGrowTip); |
| 284 |
stTriangleDown: Result := Result.GrowBottom(ExtraGrowTip); |
| 285 |
stTriangleRight: Result := Result.GrowRight(ExtraGrowTip); |
| 286 |
stTriangleLeft: Result := Result.GrowLeft(ExtraGrowTip); |
| 287 |
else raise EInternalError.Create('Only triangle ShapeType should be here'); |
| 288 |
end; |
| 289 |
end; |
| 290 |
|
| 291 |
var |
| 292 |
RR: TFloatRectangle; |
| 293 |
OutlineWidthScaled, OutlineIn, OutlineOut: Single; |
| 294 |
begin |
| 295 |
inherited; |
| 296 |
RR := RenderRect; |
| 297 |
OutlineWidthScaled := UIScale * OutlineWidth; |
| 298 |
|
| 299 |
if Filled and Outline and OutlineThick then |
| 300 |
begin |
| 301 |
{ special case when we use OutlineThick drawing mode } |
| 302 |
|
| 303 |
OutlineIn := -OutlineWidthScaled / 2; |
| 304 |
OutlineOut := OutlineWidthScaled / 2; |
| 305 |
|
| 306 |
case ShapeType of |
| 307 |
stRectangle: |
| 308 |
begin |
| 309 |
DrawRectangle(RR.Grow(OutlineOut), OutlineColor); |
| 310 |
DrawRectangle(RR.Grow(OutlineIn ), Color); |
| 311 |
end; |
| 312 |
stCircle: |
| 313 |
begin |
| 314 |
DrawCircle(RR.Center, RR.Width / 2 + OutlineOut, RR.Height / 2 + OutlineOut, OutlineColor); |
| 315 |
DrawCircle(RR.Center, RR.Width / 2 + OutlineIn , RR.Height / 2 + OutlineIn , Color); |
| 316 |
end; |
| 317 |
stTriangleUp..stTriangleLeft: |
| 318 |
begin |
| 319 |
DrawTriangleCore(ShapeType, RectGrowForTriangleOutline(RR, OutlineOut), OutlineColor, true); |
| 320 |
DrawTriangleCore(ShapeType, RR.Grow(OutlineIn), Color, true); |
| 321 |
end; |
| 322 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 323 |
else raise EInternalError.Create('TCastleShape.Render: ShapeType not implemented (Outline thick)'); |
| 324 |
{$endif} |
| 325 |
end; |
| 326 |
end else |
| 327 |
begin |
| 328 |
if Filled then |
| 329 |
case ShapeType of |
| 330 |
stRectangle: |
| 331 |
DrawRectangle(RR, Color); |
| 332 |
stCircle: |
| 333 |
DrawCircle(RR.Center, RR.Width / 2, RR.Height / 2, Color); |
| 334 |
stTriangleUp..stTriangleLeft: |
| 335 |
DrawTriangleCore(ShapeType, RR, Color, true); |
| 336 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 337 |
else raise EInternalError.Create('TCastleShape.Render: ShapeType not implemented'); |
| 338 |
{$endif} |
| 339 |
end; |
| 340 |
|
| 341 |
if Outline then |
| 342 |
case ShapeType of |
| 343 |
stRectangle: |
| 344 |
DrawRectangleOutline(RR, OutlineColor, OutlineWidthScaled); |
| 345 |
stCircle: |
| 346 |
DrawCircleOutline(RR.Center, RR.Width / 2, RR.Height / 2, OutlineColor, OutlineWidthScaled); |
| 347 |
stTriangleUp..stTriangleLeft: |
| 348 |
DrawTriangleCore(ShapeType, RR, OutlineColor, false, OutlineWidthScaled); |
| 349 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 350 |
else raise EInternalError.Create('TCastleShape.Render: ShapeType not implemented (Outline, not thick)'); |
| 351 |
{$endif} |
| 352 |
end; |
| 353 |
end; |
| 354 |
end; |
| 355 |
|
| 356 |
function TCastleShape.PropertySections( |
| 357 |
const PropertyName: String): TPropertySections; |
| 358 |
begin |
| 359 |
if ArrayContainsString(PropertyName, [ |
| 360 |
'ShapeType', |
| 361 |
'ColorPersistent', 'OutlineColorPersistent', |
| 362 |
'Filled', 'Outline', 'OutlineWidth', 'OutlineThick' |
| 363 |
]) then |
| 364 |
Result := [psBasic] |
| 365 |
else |
| 366 |
Result := inherited PropertySections(PropertyName); |
| 367 |
end; |
| 368 |
|
| 369 |
{$define read_implementation_methods} |
| 370 |
{$I auto_generated_persistent_vectors/tcastleshape_persistent_vectors.inc} |
| 371 |
{$undef read_implementation_methods} |
| 372 |
|
| 373 |
{$endif read_implementation} |