| 1 |
{ |
| 2 |
Copyright 2016-2023 Michalis Kamburelis. |
| 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 |
{ Main view, where most of the application logic takes place. } |
| 16 |
unit GameViewMain; |
| 17 |
|
| 18 |
interface |
| 19 |
|
| 20 |
uses Classes, |
| 21 |
CastleComponentSerialize, CastleUIControls, CastleControls, |
| 22 |
CastleKeysMouse; |
| 23 |
|
| 24 |
type |
| 25 |
{ Main view, where most of the application logic takes place. } |
| 26 |
TViewMain = class(TCastleView) |
| 27 |
published |
| 28 |
{ Components designed using CGE editor. |
| 29 |
These fields will be automatically initialized at Start. } |
| 30 |
LabelMain: TCastleLabel; |
| 31 |
ButtonHtml, ButtonWrap, ButtonAlignLeft, ButtonAlignMiddle, ButtonAlignRight: TCastleButton; |
| 32 |
ScrollView: TCastleScrollView; |
| 33 |
private |
| 34 |
{ The critical thing to make TCastleScrollView work correctly is to set right |
| 35 |
the ScrollView.ScrollArea size. |
| 36 |
ScrollView.ScrollArea should enclose the children vertically, |
| 37 |
but the width of ScrollView.ScrollArea should match the available parent width. |
| 38 |
This is what this routine does. |
| 39 |
|
| 40 |
It is possible it will be possible easier (without even writing code) |
| 41 |
in the future. We have WidthFraction, we have AutoSizeToChildren, |
| 42 |
but we would need AutoSizeToChildrenHeight (that doesn't cause recursive loop |
| 43 |
in case parent<->child each depend on other size). |
| 44 |
|
| 45 |
This routine also updates LabelMain.MaxWidth. } |
| 46 |
procedure UpdateSize; |
| 47 |
|
| 48 |
procedure ClickHtml(Sender: TObject); |
| 49 |
procedure ClickWrap(Sender: TObject); |
| 50 |
procedure ClickAlignLeft(Sender: TObject); |
| 51 |
procedure ClickAlignMiddle(Sender: TObject); |
| 52 |
procedure ClickAlignRight(Sender: TObject); |
| 53 |
public |
| 54 |
constructor Create(AOwner: TComponent); override; |
| 55 |
procedure Start; override; |
| 56 |
procedure Resize; override; |
| 57 |
end; |
| 58 |
|
| 59 |
var |
| 60 |
ViewMain: TViewMain; |
| 61 |
|
| 62 |
implementation |
| 63 |
|
| 64 |
{ TViewMain ----------------------------------------------------------------- } |
| 65 |
|
| 66 |
constructor TViewMain.Create(AOwner: TComponent); |
| 67 |
begin |
| 68 |
inherited; |
| 69 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 70 |
end; |
| 71 |
|
| 72 |
procedure TViewMain.Start; |
| 73 |
begin |
| 74 |
inherited; |
| 75 |
|
| 76 |
{ Assign buttons' OnClick handlers } |
| 77 |
ButtonHtml.OnClick := {$ifdef FPC}@{$endif} ClickHtml; |
| 78 |
ButtonWrap.OnClick := {$ifdef FPC}@{$endif} ClickWrap; |
| 79 |
ButtonAlignRight.OnClick := {$ifdef FPC}@{$endif} ClickAlignRight; |
| 80 |
ButtonAlignMiddle.OnClick := {$ifdef FPC}@{$endif} ClickAlignMiddle; |
| 81 |
ButtonAlignLeft.OnClick := {$ifdef FPC}@{$endif} ClickAlignLeft; |
| 82 |
end; |
| 83 |
|
| 84 |
procedure TViewMain.Resize; |
| 85 |
begin |
| 86 |
inherited; |
| 87 |
UpdateSize; |
| 88 |
end; |
| 89 |
|
| 90 |
procedure TViewMain.UpdateSize; |
| 91 |
var |
| 92 |
WidthInsideScrollArea: Single; |
| 93 |
begin |
| 94 |
WidthInsideScrollArea := ScrollView.EffectiveWidthForChildren |
| 95 |
- ScrollView.EffectiveScrollBarWidth; |
| 96 |
|
| 97 |
// do this first, as it updates LabelMain.EffectiveHeight |
| 98 |
if ButtonWrap.Pressed then |
| 99 |
LabelMain.MaxWidth := WidthInsideScrollArea |
| 100 |
else |
| 101 |
LabelMain.MaxWidth := 0; |
| 102 |
|
| 103 |
ScrollView.ScrollArea.Width := WidthInsideScrollArea; |
| 104 |
ScrollView.ScrollArea.Height := LabelMain.EffectiveHeight; |
| 105 |
end; |
| 106 |
|
| 107 |
procedure TViewMain.ClickHtml(Sender: TObject); |
| 108 |
begin |
| 109 |
ButtonHtml.Pressed := not ButtonHtml.Pressed; |
| 110 |
LabelMain.Html := ButtonHtml.Pressed; |
| 111 |
UpdateSize; |
| 112 |
end; |
| 113 |
|
| 114 |
procedure TViewMain.ClickWrap(Sender: TObject); |
| 115 |
begin |
| 116 |
ButtonWrap.Pressed := not ButtonWrap.Pressed; |
| 117 |
UpdateSize; |
| 118 |
end; |
| 119 |
|
| 120 |
procedure TViewMain.ClickAlignLeft(Sender: TObject); |
| 121 |
begin |
| 122 |
ButtonAlignLeft.Pressed := true; |
| 123 |
ButtonAlignMiddle.Pressed := false; |
| 124 |
ButtonAlignRight.Pressed := false; |
| 125 |
LabelMain.Alignment := hpLeft; |
| 126 |
LabelMain.Anchor(hpLeft); |
| 127 |
end; |
| 128 |
|
| 129 |
procedure TViewMain.ClickAlignMiddle(Sender: TObject); |
| 130 |
begin |
| 131 |
ButtonAlignLeft.Pressed := false; |
| 132 |
ButtonAlignMiddle.Pressed := true; |
| 133 |
ButtonAlignRight.Pressed := false; |
| 134 |
LabelMain.Alignment := hpMiddle; |
| 135 |
LabelMain.Anchor(hpMiddle); |
| 136 |
end; |
| 137 |
|
| 138 |
procedure TViewMain.ClickAlignRight(Sender: TObject); |
| 139 |
begin |
| 140 |
ButtonAlignLeft.Pressed := false; |
| 141 |
ButtonAlignMiddle.Pressed := false; |
| 142 |
ButtonAlignRight.Pressed := true; |
| 143 |
LabelMain.Alignment := hpRight; |
| 144 |
LabelMain.Anchor(hpRight); |
| 145 |
end; |
| 146 |
|
| 147 |
end. |