| 1 |
{ |
| 2 |
Copyright 2023 Freedomax. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
unit CastleInternalRegionDialog; |
| 16 |
|
| 17 |
{$I castleconf.inc} |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses |
| 22 |
Generics.Collections, Contnrs, |
| 23 |
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, |
| 24 |
ButtonPanel, StdCtrls, ExtCtrls, ComCtrls, CastleVectors, CastleControl, |
| 25 |
CastleGLImages, |
| 26 |
castletransform, CastleKeysMouse, |
| 27 |
castlecontrols, CastleRectangles, CastleLCLUtils, CastleGLUtils, CastleColors; |
| 28 |
|
| 29 |
type |
| 30 |
TDesignMode = (dmRegion, dmBorder); |
| 31 |
|
| 32 |
TRegionDesignDialog = class(TForm) |
| 33 |
ButtonPanel1: TButtonPanel; |
| 34 |
CastleControl1: TCastleControl; |
| 35 |
ColorDialog1: TColorDialog; |
| 36 |
StatusBar1: TStatusBar; |
| 37 |
|
| 38 |
procedure CastleControl1Motion(Sender: TObject; const Event: TInputMotion); |
| 39 |
procedure CastleControl1Press(Sender: TObject; const Event: TInputPressRelease); |
| 40 |
procedure CastleControl1Release(Sender: TObject; |
| 41 |
const Event: TInputPressRelease); |
| 42 |
procedure CastleControl1Render(Sender: TObject); |
| 43 |
procedure FormCreate(Sender: TObject); |
| 44 |
procedure HelpButtonClick(Sender: TObject); |
| 45 |
strict private |
| 46 |
FBorder: TBorder; |
| 47 |
type |
| 48 |
TArrayScreenPoints = array[0..1] of TVector2; |
| 49 |
TArrayImagePoints = array[0..1] of TVector2Integer; |
| 50 |
|
| 51 |
TDirectionEnable = (EnableX, EnableY); |
| 52 |
TDirectionEnables = set of TDirectionEnable; |
| 53 |
|
| 54 |
TMovingRec = record |
| 55 |
Moving: boolean; |
| 56 |
StartTranslation: TVector2; |
| 57 |
StartMousePoint: TVector2; |
| 58 |
end; |
| 59 |
|
| 60 |
TControlPointRec = record |
| 61 |
Adjusting: boolean; |
| 62 |
Index: integer; |
| 63 |
DirectionEnables: TDirectionEnables; |
| 64 |
Points: TArrayImagePoints; |
| 65 |
function AdditionalPoints(const byX: boolean = True): TArrayImagePoints; |
| 66 |
end; |
| 67 |
|
| 68 |
const |
| 69 |
CircleRads = 8; |
| 70 |
var |
| 71 |
FImage: TDrawableImage; |
| 72 |
FRegion, FSourceRegion: TFloatRectangle; |
| 73 |
FScale: single; |
| 74 |
FTranslation: TVector2; |
| 75 |
FDesignMode: TDesignMode; |
| 76 |
FMovingImageRec: TMovingRec; |
| 77 |
FControlPointRec: TControlPointRec; |
| 78 |
procedure InitControlPoints; |
| 79 |
function ScreenRegionRectangle: TFloatRectangle; |
| 80 |
function ScreenImageFullRectangle: TFloatRectangle; |
| 81 |
function ImageRegionRectangle: TRectangle; |
| 82 |
|
| 83 |
function ScreenRegionPoints: TArrayScreenPoints; |
| 84 |
function ScreenAdditionalPoints: TArrayScreenPoints; |
| 85 |
|
| 86 |
procedure SetImage(const AImage: TDrawableImage); |
| 87 |
function ImageWidth: Cardinal; |
| 88 |
function ImageHeight: Cardinal; |
| 89 |
|
| 90 |
{ Restrict the region within the image rectangle. } |
| 91 |
procedure FixControlPoints; |
| 92 |
|
| 93 |
procedure UpdateCursorPosInfo(const MousePoint: TVector2); |
| 94 |
procedure UpdateCursorShape(const MousePoint: TVector2); |
| 95 |
function HitTest(const AControlPoint: TVector2Integer; |
| 96 |
const AMousePoint: TVector2): TDirectionEnables; |
| 97 |
function GetDirectionEnables(const MousePoint: TVector2): TDirectionEnables; |
| 98 |
|
| 99 |
procedure Changed; |
| 100 |
protected |
| 101 |
function ScreenToImage(APoint: TVector2; bRound: boolean = True): TVector2Integer; |
| 102 |
function ImageToScreen(const APoint: TVector2Integer): TVector2; overload; |
| 103 |
function ImageToScreen(const APoints: TArrayImagePoints): TArrayScreenPoints; |
| 104 |
overload; |
| 105 |
|
| 106 |
function RegionFromBorder(ABorder: TBorder): TFloatRectangle; |
| 107 |
|
| 108 |
property Image: TDrawableImage read FImage write SetImage; |
| 109 |
public |
| 110 |
constructor Create(AOwner: TComponent); override; |
| 111 |
destructor Destroy; override; |
| 112 |
procedure ApplyChange; |
| 113 |
procedure ApplyToBorder(ABorder: TBorder); |
| 114 |
|
| 115 |
procedure Load(AImage: TDrawableImage; |
| 116 |
const ARegion: TFloatRectangle); overload; |
| 117 |
procedure Load(AImage: TDrawableImage; |
| 118 |
const ASourceRegion: TFloatRectangle; ABorder: TBorder); overload; |
| 119 |
|
| 120 |
property Region: TFloatRectangle read FRegion; |
| 121 |
property Border: TBorder read FBorder; |
| 122 |
property DesignMode: TDesignMode read FDesignMode; |
| 123 |
end; |
| 124 |
|
| 125 |
|
| 126 |
implementation |
| 127 |
|
| 128 |
uses Math, CastleRenderOptions, CastleUtils; |
| 129 |
|
| 130 |
{$R *.lfm} |
| 131 |
|
| 132 |
{ TRegionDesignDialog.TControlPointRec } |
| 133 |
|
| 134 |
function TRegionDesignDialog.TControlPointRec.AdditionalPoints( |
| 135 |
const byX: boolean = True): TArrayImagePoints; |
| 136 |
begin |
| 137 |
if byX then |
| 138 |
begin |
| 139 |
Result[0] := Vector2Integer(Points[0].X, Points[1].Y); |
| 140 |
Result[1] := Vector2Integer(Points[1].X, Points[0].Y); |
| 141 |
end |
| 142 |
else |
| 143 |
begin |
| 144 |
Result[0] := Vector2Integer(Points[1].X, Points[0].Y); |
| 145 |
Result[1] := Vector2Integer(Points[0].X, Points[1].Y); |
| 146 |
end; |
| 147 |
end; |
| 148 |
|
| 149 |
{ TRegionDesignDialog } |
| 150 |
|
| 151 |
constructor TRegionDesignDialog.Create(AOwner: TComponent); |
| 152 |
begin |
| 153 |
inherited; |
| 154 |
|
| 155 |
FScale := 1; |
| 156 |
FSourceRegion := TFloatRectangle.Empty; |
| 157 |
FRegion := TFloatRectangle.Empty; |
| 158 |
FImage := TDrawableImage.Create(nil, False, False); |
| 159 |
|
| 160 |
end; |
| 161 |
|
| 162 |
destructor TRegionDesignDialog.Destroy; |
| 163 |
begin |
| 164 |
FreeAndNil(FImage); |
| 165 |
|
| 166 |
inherited; |
| 167 |
end; |
| 168 |
|
| 169 |
procedure TRegionDesignDialog.ApplyChange; |
| 170 |
var |
| 171 |
rc: TRectangle; |
| 172 |
begin |
| 173 |
rc := ImageRegionRectangle; |
| 174 |
FRegion := FloatRectangle(rc.Left, rc.Bottom, rc.Width, rc.Height); |
| 175 |
end; |
| 176 |
|
| 177 |
procedure TRegionDesignDialog.Load(AImage: TDrawableImage; |
| 178 |
const ARegion: TFloatRectangle); |
| 179 |
begin |
| 180 |
Image := AImage; |
| 181 |
|
| 182 |
FSourceRegion := TFloatRectangle.Empty; |
| 183 |
FRegion := ARegion; |
| 184 |
FDesignMode := dmRegion; |
| 185 |
|
| 186 |
InitControlPoints; |
| 187 |
end; |
| 188 |
|
| 189 |
procedure TRegionDesignDialog.Load(AImage: TDrawableImage; |
| 190 |
const ASourceRegion: TFloatRectangle; ABorder: TBorder); |
| 191 |
begin |
| 192 |
Image := AImage; |
| 193 |
|
| 194 |
FBorder := ABorder; |
| 195 |
FSourceRegion := ASourceRegion; |
| 196 |
FRegion := RegionFromBorder(FBorder); |
| 197 |
FDesignMode := dmBorder; |
| 198 |
|
| 199 |
InitControlPoints; |
| 200 |
end; |
| 201 |
|
| 202 |
procedure TRegionDesignDialog.ApplyToBorder(ABorder: TBorder); |
| 203 |
begin |
| 204 |
if (FRegion.Left = FRegion.Bottom) and (FRegion.Left = ImageWidth - FRegion.Right) and |
| 205 |
(FRegion.Left = ImageHeight - FRegion.Top) then |
| 206 |
begin |
| 207 |
ABorder.AllSides := FRegion.Left; |
| 208 |
ABorder.Left := 0; |
| 209 |
ABorder.Bottom := 0; |
| 210 |
ABorder.Right := 0; |
| 211 |
ABorder.Top := 0; |
| 212 |
end |
| 213 |
else |
| 214 |
begin |
| 215 |
ABorder.AllSides := 0; |
| 216 |
ABorder.Left := FRegion.Left; |
| 217 |
ABorder.Bottom := FRegion.Bottom; |
| 218 |
ABorder.Right := ImageWidth - FRegion.Right; |
| 219 |
ABorder.Top := ImageHeight - FRegion.Top; |
| 220 |
end; |
| 221 |
end; |
| 222 |
|
| 223 |
function TRegionDesignDialog.RegionFromBorder(ABorder: TBorder): TFloatRectangle; |
| 224 |
begin |
| 225 |
Result := FloatRectangle(FBorder.TotalLeft, FBorder.TotalBottom, |
| 226 |
ImageWidth - FBorder.TotalWidth, ImageHeight - FBorder.TotalHeight); |
| 227 |
end; |
| 228 |
|
| 229 |
function TRegionDesignDialog.ScreenRegionRectangle: TFloatRectangle; |
| 230 |
var |
| 231 |
Points: TArrayScreenPoints; |
| 232 |
vLeft, vBottom, vWidth, vHeight: single; |
| 233 |
begin |
| 234 |
Points := ScreenRegionPoints; |
| 235 |
|
| 236 |
vLeft := Min(Points[0].X, Points[1].X); |
| 237 |
vBottom := Min(Points[0].Y, Points[1].Y); |
| 238 |
vWidth := ABS(Points[0].X - Points[1].X); |
| 239 |
vHeight := ABS(Points[0].Y - Points[1].Y); |
| 240 |
|
| 241 |
Result := FloatRectangle(vLeft, vBottom, vWidth, vHeight); |
| 242 |
end; |
| 243 |
|
| 244 |
function TRegionDesignDialog.ScreenImageFullRectangle: TFloatRectangle; |
| 245 |
var |
| 246 |
Points: TArrayScreenPoints; |
| 247 |
{ FPC3.2.2 not support "Array Of Const" => "const array" } |
| 248 |
imgPoints: TArrayImagePoints; |
| 249 |
begin |
| 250 |
imgPoints[0] := Vector2Integer(0, 0); |
| 251 |
imgPoints[1] := Vector2Integer(ImageWidth, ImageHeight); |
| 252 |
Points := ImageToScreen(imgPoints); |
| 253 |
|
| 254 |
Result.LeftBottom := Points[0]; |
| 255 |
Result.Width := Points[1].X - Points[0].X; |
| 256 |
Result.Height := Points[1].Y - Points[0].Y; |
| 257 |
end; |
| 258 |
|
| 259 |
function TRegionDesignDialog.ImageRegionRectangle: TRectangle; |
| 260 |
begin |
| 261 |
Result.Left := Min(FControlPointRec.Points[0].X, FControlPointRec.Points[1].X); |
| 262 |
Result.Bottom := Min(FControlPointRec.Points[0].Y, FControlPointRec.Points[1].Y); |
| 263 |
Result.Width := ABS(FControlPointRec.Points[0].X - FControlPointRec.Points[1].X); |
| 264 |
Result.Height := ABS(FControlPointRec.Points[0].Y - FControlPointRec.Points[1].Y); |
| 265 |
end; |
| 266 |
|
| 267 |
procedure TRegionDesignDialog.CastleControl1Render(Sender: TObject); |
| 268 |
|
| 269 |
function ScreenRect: TFloatRectangle; |
| 270 |
begin |
| 271 |
Result := FloatRectangle(0, 0, ImageWidth * FScale, ImageHeight * |
| 272 |
FScale).Translate(FTranslation); |
| 273 |
end; |
| 274 |
|
| 275 |
function ImageRect: TFloatRectangle; |
| 276 |
begin |
| 277 |
if FSourceRegion.IsEmpty then |
| 278 |
Result := FloatRectangle(0, 0, ImageWidth, ImageHeight) |
| 279 |
else |
| 280 |
Result := FloatRectangle(Floor(FSourceRegion.Left), |
| 281 |
Floor(FSourceRegion.Bottom), ImageWidth, ImageHeight); |
| 282 |
end; |
| 283 |
|
| 284 |
procedure RenderImage; |
| 285 |
begin |
| 286 |
FImage.Draw(ScreenRect, ImageRect); |
| 287 |
end; |
| 288 |
|
| 289 |
procedure RenderLine(const Points: TArrayImagePoints; |
| 290 |
const LineColor: TCastleColor; const LineWidth: single); |
| 291 |
begin |
| 292 |
DrawPrimitive2D(pmLines, |
| 293 |
ImageToScreen(Points), |
| 294 |
LineColor, bsSrcAlpha, bdOneMinusSrcAlpha, False, LineWidth); |
| 295 |
end; |
| 296 |
|
| 297 |
procedure RenderControlPoints; |
| 298 |
var |
| 299 |
vRect: TFloatRectangle; |
| 300 |
vImageRegionRect: TRectangle; |
| 301 |
Points: TArrayImagePoints; |
| 302 |
const |
| 303 |
LineWidth: single = 2; |
| 304 |
LineColor: TCastleColor = (X: 1; Y: 1; Z: 1; W: 0.8); |
| 305 |
FillColor: TCastleColor = (X: 1; Y: 1; Z: 1; W: 0.382); |
| 306 |
CircleColor: TCastleColor = (X: 0.86; Y: 0.21; Z: 0.14; W: 1); |
| 307 |
|
| 308 |
procedure RenderPoints(const Points: TArrayScreenPoints); |
| 309 |
var |
| 310 |
Point: TVector2; |
| 311 |
Rads: single; |
| 312 |
const |
| 313 |
RenderCircleRads = 5; |
| 314 |
begin |
| 315 |
Rads := Min(RenderCircleRads, CircleRads); |
| 316 |
for Point in Points do |
| 317 |
begin |
| 318 |
DrawCircle(Point, Rads, Rads, CircleColor); |
| 319 |
DrawCircleOutline(Point, Rads, Rads, LineColor, |
| 320 |
LineWidth); |
| 321 |
end; |
| 322 |
end; |
| 323 |
|
| 324 |
begin |
| 325 |
vRect := ScreenRegionRectangle; |
| 326 |
|
| 327 |
case FDesignMode of |
| 328 |
dmRegion: |
| 329 |
begin |
| 330 |
DrawRectangle(vRect,FillColor); |
| 331 |
DrawRectangleOutline(vRect, LineColor, LineWidth); |
| 332 |
RenderPoints(ScreenRegionPoints); |
| 333 |
RenderPoints(ScreenAdditionalPoints); |
| 334 |
end; |
| 335 |
dmBorder: |
| 336 |
begin |
| 337 |
vImageRegionRect := ImageRegionRectangle; |
| 338 |
|
| 339 |
Points[0] := Vector2Integer(vImageRegionRect.Left, 0); |
| 340 |
Points[1] := Vector2Integer(vImageRegionRect.Left, ImageHeight); |
| 341 |
RenderLine(Points, LineColor, LineWidth); |
| 342 |
|
| 343 |
Points[0] := Vector2Integer(vImageRegionRect.Right, 0); |
| 344 |
Points[1] := Vector2Integer(vImageRegionRect.Right, ImageHeight); |
| 345 |
RenderLine(Points, LineColor, LineWidth); |
| 346 |
|
| 347 |
Points[0] := Vector2Integer(0, vImageRegionRect.Bottom); |
| 348 |
Points[1] := Vector2Integer(ImageWidth, vImageRegionRect.Bottom); |
| 349 |
RenderLine(Points, LineColor, LineWidth); |
| 350 |
|
| 351 |
Points[0] := Vector2Integer(0, vImageRegionRect.Top); |
| 352 |
Points[1] := Vector2Integer(ImageWidth, vImageRegionRect.Top); |
| 353 |
RenderLine(Points, LineColor, LineWidth); |
| 354 |
|
| 355 |
RenderPoints(ScreenRegionPoints); |
| 356 |
RenderPoints(ScreenAdditionalPoints); |
| 357 |
end; |
| 358 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 359 |
else raise EInternalError.Create('FDesignMode?'); |
| 360 |
{$endif} |
| 361 |
end; |
| 362 |
end; |
| 363 |
|
| 364 |
begin |
| 365 |
RenderImage; |
| 366 |
RenderControlPoints; |
| 367 |
end; |
| 368 |
|
| 369 |
procedure TRegionDesignDialog.FormCreate(Sender: TObject); |
| 370 |
begin |
| 371 |
CastleControl1.Container.BackgroundColor := Gray; |
| 372 |
end; |
| 373 |
|
| 374 |
procedure TRegionDesignDialog.HelpButtonClick(Sender: TObject); |
| 375 |
begin |
| 376 |
if ColorDialog1.Execute then |
| 377 |
CastleControl1.Container.BackgroundColor := |
| 378 |
Vector4(ColorToVector3(ColorDialog1.Color), 1); |
| 379 |
end; |
| 380 |
|
| 381 |
function TRegionDesignDialog.HitTest(const AControlPoint: TVector2Integer; |
| 382 |
const AMousePoint: TVector2): TDirectionEnables; |
| 383 |
var |
| 384 |
pt: TVector2; |
| 385 |
rc: TFloatRectangle; |
| 386 |
begin |
| 387 |
Result := []; |
| 388 |
|
| 389 |
case FDesignMode of |
| 390 |
dmRegion: |
| 391 |
begin |
| 392 |
rc := ScreenRegionRectangle; |
| 393 |
rc := rc.Grow(CircleRads); |
| 394 |
end; |
| 395 |
dmBorder: |
| 396 |
begin |
| 397 |
rc := ScreenImageFullRectangle; |
| 398 |
rc := rc.Grow(CircleRads); |
| 399 |
end; |
| 400 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 401 |
else raise EInternalError.Create('FDesignMode?'); |
| 402 |
{$endif} |
| 403 |
end; |
| 404 |
|
| 405 |
if not rc.Contains(AMousePoint) then Exit; |
| 406 |
|
| 407 |
pt := ImageToScreen(AControlPoint); |
| 408 |
|
| 409 |
if Abs(pt.X - AMousePoint.X) < CircleRads then Include(Result, EnableX); |
| 410 |
if Abs(pt.Y - AMousePoint.Y) < CircleRads then Include(Result, EnableY); |
| 411 |
end; |
| 412 |
|
| 413 |
function TRegionDesignDialog.GetDirectionEnables( |
| 414 |
const MousePoint: TVector2): TDirectionEnables; |
| 415 |
var |
| 416 |
Point: TVector2Integer; |
| 417 |
begin |
| 418 |
Result := []; |
| 419 |
for Point in FControlPointRec.Points do |
| 420 |
Result := Result + HitTest(Point, MousePoint); |
| 421 |
end; |
| 422 |
|
| 423 |
procedure TRegionDesignDialog.CastleControl1Press(Sender: TObject; |
| 424 |
const Event: TInputPressRelease); |
| 425 |
|
| 426 |
procedure GrowScale(const beIncrease: boolean; const ScreenPoint: TVector2); |
| 427 |
const |
| 428 |
PX = 0.7; |
| 429 |
var |
| 430 |
ImagePoint: TVector2Integer; |
| 431 |
begin |
| 432 |
{ Limit the scaling point to be inside the image to prevent the coordinates from losing control. } |
| 433 |
|
| 434 |
ImagePoint := ScreenToImage(ScreenPoint); |
| 435 |
ImagePoint.X := Clamped(ImagePoint.X, 0, ImageWidth); |
| 436 |
ImagePoint.Y := Clamped(ImagePoint.Y, 0, ImageHeight); |
| 437 |
|
| 438 |
if beIncrease then |
| 439 |
begin |
| 440 |
{ When FScale is less than 6, it increases linearly, otherwise it increases exponentially. } |
| 441 |
if FScale < 1 then |
| 442 |
FScale := FScale + 0.1 |
| 443 |
else if FScale < 2 then |
| 444 |
FScale := FScale + 0.2 |
| 445 |
else if FScale < 6 then |
| 446 |
FScale := FScale + 0.4 |
| 447 |
else |
| 448 |
FScale := FScale / PX; |
| 449 |
|
| 450 |
FScale := Min(FScale, 10000); |
| 451 |
end |
| 452 |
else |
| 453 |
begin |
| 454 |
{ |
| 455 |
Same as above. |
| 456 |
Between 0.5 and 6 : linearly, |
| 457 |
otherwise : exponentially. |
| 458 |
} |
| 459 |
if FScale > 6 then FScale := FScale * PX |
| 460 |
else if FScale > 2 then |
| 461 |
FScale := FScale - 0.4 |
| 462 |
else if FScale > 1 then |
| 463 |
FScale := FScale - 0.2 |
| 464 |
else |
| 465 |
if FScale > 0.5 then |
| 466 |
FScale := FScale - 0.1 |
| 467 |
else |
| 468 |
FScale := FScale * PX; |
| 469 |
|
| 470 |
FScale := Max(FScale, 0.0001); |
| 471 |
end; |
| 472 |
|
| 473 |
{ Zoom in on the pixel at the mouse location. } |
| 474 |
FTranslation := FTranslation + ScreenPoint - ImageToScreen(ImagePoint); |
| 475 |
|
| 476 |
end; |
| 477 |
|
| 478 |
var |
| 479 |
RegionPts: TArrayImagePoints; |
| 480 |
i, vIndex: integer; |
| 481 |
AllDirectionEnables, DirectionEnables: TDirectionEnables; |
| 482 |
begin |
| 483 |
{ LeftMouseButton Pressed. } |
| 484 |
|
| 485 |
if Event.IsMouseButton(TCastleMouseButton.buttonLeft) and |
| 486 |
(not FControlPointRec.Adjusting) then |
| 487 |
begin |
| 488 |
AllDirectionEnables := []; |
| 489 |
vIndex := FControlPointRec.Index; |
| 490 |
RegionPts := FControlPointRec.Points; |
| 491 |
|
| 492 |
for i := Low(RegionPts) to High(RegionPts) do |
| 493 |
begin |
| 494 |
DirectionEnables := HitTest(RegionPts[i], Event.Position); |
| 495 |
AllDirectionEnables := AllDirectionEnables + DirectionEnables; |
| 496 |
|
| 497 |
if DirectionEnables = [EnableX, EnableY] then |
| 498 |
begin |
| 499 |
vIndex := i; |
| 500 |
Break; |
| 501 |
end |
| 502 |
else |
| 503 |
begin |
| 504 |
if AllDirectionEnables = [EnableX, EnableY] then |
| 505 |
begin |
| 506 |
{ Exchange control points.The original control points become additional control points. |
| 507 |
We only ever need two anchors. |
| 508 |
Obviously here i must equal 1. Using 0 and 1 to represent two original control points, |
| 509 |
if DirectionEnables = [EnableX], then point 1 has the same X value as the currently selected point. |
| 510 |
Then calling 'AdditionalPoints' with True will ensure that the current point still has an index of 1, |
| 511 |
which is the correct order. On the other hand, |
| 512 |
if DirectionEnables = [EnableY], pass False to get the correct order. } |
| 513 |
FControlPointRec.Points := |
| 514 |
FControlPointRec.AdditionalPoints(DirectionEnables = [EnableX]); |
| 515 |
vIndex := i; |
| 516 |
Break; |
| 517 |
end |
| 518 |
else if DirectionEnables <> [] then |
| 519 |
vIndex := i; |
| 520 |
end; |
| 521 |
end; |
| 522 |
|
| 523 |
if AllDirectionEnables <> [] then |
| 524 |
begin |
| 525 |
FControlPointRec.Index := vIndex; |
| 526 |
FControlPointRec.DirectionEnables := AllDirectionEnables; |
| 527 |
end |
| 528 |
else |
| 529 |
begin |
| 530 |
{ new region } |
| 531 |
FControlPointRec.Points[0] := ScreenToImage(Event.Position); |
| 532 |
FControlPointRec.Points[1] := FControlPointRec.Points[0]; |
| 533 |
FControlPointRec.Index := 1; |
| 534 |
{ Newly created regions are always free to move. } |
| 535 |
FControlPointRec.DirectionEnables := [EnableX, EnableY]; |
| 536 |
end; |
| 537 |
|
| 538 |
FControlPointRec.Adjusting := True; |
| 539 |
Changed; |
| 540 |
end; |
| 541 |
|
| 542 |
{ RightMouseButton pressed. } |
| 543 |
|
| 544 |
if Event.IsMouseButton(TCastleMouseButton.buttonRight) and |
| 545 |
(not FMovingImageRec.Moving) then |
| 546 |
begin |
| 547 |
FMovingImageRec.StartMousePoint := Event.Position; |
| 548 |
FMovingImageRec.StartTranslation := FTranslation; |
| 549 |
FMovingImageRec.Moving := True; |
| 550 |
end; |
| 551 |
|
| 552 |
{ Mouse wheel. } |
| 553 |
|
| 554 |
if (Event.EventType = TInputPressReleaseType.itMouseWheel) then |
| 555 |
begin |
| 556 |
GrowScale(Event.MouseWheelScroll > 0, Event.Position); |
| 557 |
UpdateCursorPosInfo(Event.Position); |
| 558 |
end; |
| 559 |
|
| 560 |
UpdateCursorShape(Event.Position); |
| 561 |
end; |
| 562 |
|
| 563 |
procedure TRegionDesignDialog.UpdateCursorPosInfo(const MousePoint: TVector2); |
| 564 |
var |
| 565 |
Point: TVector2Integer; |
| 566 |
begin |
| 567 |
Point := ScreenToImage(MousePoint, False); |
| 568 |
StatusBar1.Panels.Items[4].Text := Format('CursorPos: %d , %d', [Point.X, Point.Y]); |
| 569 |
end; |
| 570 |
|
| 571 |
procedure TRegionDesignDialog.UpdateCursorShape(const MousePoint: TVector2); |
| 572 |
var |
| 573 |
DirectionEnables: TDirectionEnables; |
| 574 |
begin |
| 575 |
if FControlPointRec.Adjusting then DirectionEnables := |
| 576 |
FControlPointRec.DirectionEnables |
| 577 |
else |
| 578 |
DirectionEnables := GetDirectionEnables(MousePoint); |
| 579 |
|
| 580 |
if DirectionEnables = [EnableX, EnableY] then |
| 581 |
CastleControl1.Cursor := crHandPoint |
| 582 |
else |
| 583 |
if DirectionEnables = [EnableX] then CastleControl1.Cursor := crSizeWE |
| 584 |
else |
| 585 |
if DirectionEnables = [EnableY] then CastleControl1.Cursor := crSizeNS |
| 586 |
else |
| 587 |
CastleControl1.Cursor := crDefault; |
| 588 |
end; |
| 589 |
|
| 590 |
procedure TRegionDesignDialog.CastleControl1Motion(Sender: TObject; |
| 591 |
const Event: TInputMotion); |
| 592 |
var |
| 593 |
Point: TVector2Integer; |
| 594 |
begin |
| 595 |
if FControlPointRec.Adjusting and not (FControlPointRec.DirectionEnables = []) then |
| 596 |
begin |
| 597 |
Point := ScreenToImage(Event.Position); |
| 598 |
|
| 599 |
if (EnableX in FControlPointRec.DirectionEnables) then |
| 600 |
FControlPointRec.Points[FControlPointRec.Index].X := Point.X; |
| 601 |
if (EnableY in FControlPointRec.DirectionEnables) then |
| 602 |
FControlPointRec.Points[FControlPointRec.Index].Y := Point.Y; |
| 603 |
|
| 604 |
Changed; |
| 605 |
end; |
| 606 |
|
| 607 |
if FMovingImageRec.Moving then |
| 608 |
begin |
| 609 |
FTranslation := Event.Position - FMovingImageRec.StartMousePoint + |
| 610 |
FMovingImageRec.StartTranslation; |
| 611 |
end; |
| 612 |
|
| 613 |
UpdateCursorShape(Event.Position); |
| 614 |
UpdateCursorPosInfo(Event.Position); |
| 615 |
end; |
| 616 |
|
| 617 |
procedure TRegionDesignDialog.FixControlPoints; |
| 618 |
var |
| 619 |
i: integer; |
| 620 |
begin |
| 621 |
for i := Low(FControlPointRec.Points) to High(FControlPointRec.Points) do |
| 622 |
begin |
| 623 |
FControlPointRec.Points[i].X := Clamped(FControlPointRec.Points[i].X, 0, ImageWidth); |
| 624 |
FControlPointRec.Points[i].Y := |
| 625 |
Clamped(FControlPointRec.Points[i].Y, 0, ImageHeight); |
| 626 |
end; |
| 627 |
end; |
| 628 |
|
| 629 |
procedure TRegionDesignDialog.CastleControl1Release(Sender: TObject; |
| 630 |
const Event: TInputPressRelease); |
| 631 |
begin |
| 632 |
if Event.IsMouseButton(TCastleMouseButton.buttonRight) and |
| 633 |
FMovingImageRec.Moving then |
| 634 |
FMovingImageRec.Moving := False; |
| 635 |
|
| 636 |
if Event.IsMouseButton(TCastleMouseButton.buttonLeft) and |
| 637 |
FControlPointRec.Adjusting then |
| 638 |
begin |
| 639 |
FControlPointRec.Adjusting := False; |
| 640 |
FixControlPoints; |
| 641 |
UpdateCursorShape(Event.Position); |
| 642 |
Changed; |
| 643 |
end; |
| 644 |
end; |
| 645 |
|
| 646 |
procedure TRegionDesignDialog.InitControlPoints; |
| 647 |
|
| 648 |
function CenterPoint: TVector2Integer; |
| 649 |
begin |
| 650 |
Result := FControlPointRec.Points[0] + FControlPointRec.Points[1]; |
| 651 |
Result.X := Result.X div 2; |
| 652 |
Result.Y := Result.Y div 2; |
| 653 |
end; |
| 654 |
|
| 655 |
function ContainerCenterPoint: TVector2Integer; |
| 656 |
begin |
| 657 |
Result := CastleControl1.Rect.Center; |
| 658 |
Result.X := Round(Result.X * CastleControl1.Container.Dpi / 96); |
| 659 |
Result.Y := Round(Result.Y * CastleControl1.Container.Dpi / 96); |
| 660 |
end; |
| 661 |
|
| 662 |
begin |
| 663 |
{ "empty region" means using the whole image. } |
| 664 |
if FRegion.IsEmpty then |
| 665 |
begin |
| 666 |
FControlPointRec.Points[0] := |
| 667 |
TVector2Integer.Zero; |
| 668 |
FControlPointRec.Points[1] := |
| 669 |
Vector2Integer(ImageWidth, ImageHeight); |
| 670 |
end else |
| 671 |
begin |
| 672 |
FControlPointRec.Points[0] := |
| 673 |
Vector2Integer(Floor(FRegion.Left), Floor(FRegion.Bottom)); |
| 674 |
FControlPointRec.Points[1] := |
| 675 |
Vector2Integer(Floor(FRegion.Right), Floor(FRegion.Top)); |
| 676 |
end; |
| 677 |
|
| 678 |
FixControlPoints; |
| 679 |
|
| 680 |
{ Center view then region. } |
| 681 |
FTranslation := ImageToScreen(ContainerCenterPoint) - |
| 682 |
ImageToScreen(CenterPoint); |
| 683 |
|
| 684 |
Changed; |
| 685 |
end; |
| 686 |
|
| 687 |
function TRegionDesignDialog.ScreenRegionPoints: TArrayScreenPoints; |
| 688 |
begin |
| 689 |
Result := ImageToScreen(FControlPointRec.Points); |
| 690 |
end; |
| 691 |
|
| 692 |
function TRegionDesignDialog.ScreenAdditionalPoints: TArrayScreenPoints; |
| 693 |
begin |
| 694 |
Result := ImageToScreen(FControlPointRec.AdditionalPoints); |
| 695 |
end; |
| 696 |
|
| 697 |
procedure TRegionDesignDialog.SetImage(const AImage: TDrawableImage); |
| 698 |
begin |
| 699 |
if not Assigned(AImage) then Exit; |
| 700 |
if not Assigned(AImage.Image) then Exit; |
| 701 |
|
| 702 |
FImage.Load(AImage.Image); |
| 703 |
end; |
| 704 |
|
| 705 |
function TRegionDesignDialog.ImageWidth: Cardinal; |
| 706 |
begin |
| 707 |
if FSourceRegion.IsEmpty then |
| 708 |
Result := FImage.Width |
| 709 |
else |
| 710 |
Result := Floor(FSourceRegion.Width); |
| 711 |
end; |
| 712 |
|
| 713 |
function TRegionDesignDialog.ImageHeight: Cardinal; |
| 714 |
begin |
| 715 |
if FSourceRegion.IsEmpty then |
| 716 |
Result := FImage.Height |
| 717 |
else |
| 718 |
Result := Floor(FSourceRegion.Height); |
| 719 |
end; |
| 720 |
|
| 721 |
procedure TRegionDesignDialog.Changed; |
| 722 |
begin |
| 723 |
StatusBar1.Panels.Items[0].Text := |
| 724 |
Format('Left: %d', [Min(FControlPointRec.Points[0].X, |
| 725 |
FControlPointRec.Points[1].X)]); |
| 726 |
StatusBar1.Panels.Items[1].Text := |
| 727 |
Format('Bottom: %d', [Min(FControlPointRec.Points[0].Y, |
| 728 |
FControlPointRec.Points[1].Y)]); |
| 729 |
|
| 730 |
if FDesignMode = dmRegion then |
| 731 |
begin |
| 732 |
StatusBar1.Panels.Items[2].Text := |
| 733 |
Format('Width: %d', [Abs(FControlPointRec.Points[0].X - |
| 734 |
FControlPointRec.Points[1].X)]); |
| 735 |
StatusBar1.Panels.Items[3].Text := |
| 736 |
Format('Height: %d', [Abs(FControlPointRec.Points[0].Y - |
| 737 |
FControlPointRec.Points[1].Y)]); |
| 738 |
end |
| 739 |
else |
| 740 |
begin |
| 741 |
StatusBar1.Panels.Items[2].Text := |
| 742 |
Format('Right: %d', [ImageWidth - Max(FControlPointRec.Points[0].X, |
| 743 |
FControlPointRec.Points[1].X)]); |
| 744 |
StatusBar1.Panels.Items[3].Text := |
| 745 |
Format('Top: %d', [ImageHeight - Max(FControlPointRec.Points[0].Y, |
| 746 |
FControlPointRec.Points[1].Y)]); |
| 747 |
end; |
| 748 |
end; |
| 749 |
|
| 750 |
function TRegionDesignDialog.ScreenToImage(APoint: TVector2; |
| 751 |
bRound: boolean = True): TVector2Integer; |
| 752 |
begin |
| 753 |
APoint := (APoint - FTranslation) / FScale; |
| 754 |
|
| 755 |
if bRound then |
| 756 |
Result := Vector2Integer(Round(APoint.X), Round(APoint.Y)) |
| 757 |
else |
| 758 |
Result := Vector2Integer(Floor(APoint.X), Floor(APoint.Y)); |
| 759 |
end; |
| 760 |
|
| 761 |
function TRegionDesignDialog.ImageToScreen(const APoint: TVector2Integer): TVector2; |
| 762 |
var |
| 763 |
pt: TVector2; |
| 764 |
begin |
| 765 |
pt.X := APoint.X; |
| 766 |
pt.Y := APoint.Y; |
| 767 |
|
| 768 |
Result := pt * FScale + FTranslation; |
| 769 |
end; |
| 770 |
|
| 771 |
function TRegionDesignDialog.ImageToScreen( |
| 772 |
const APoints: TArrayImagePoints): TArrayScreenPoints; |
| 773 |
var |
| 774 |
i: integer; |
| 775 |
begin |
| 776 |
for i := Low(APoints) to High(APoints) do |
| 777 |
Result[i] := ImageToScreen(APoints[i]); |
| 778 |
end; |
| 779 |
|
| 780 |
end. |