File: gamestateaskdialog.pas

package info (click to toggle)
castle-game-engine 6.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 194,520 kB
  • sloc: pascal: 364,585; ansic: 8,606; java: 2,851; objc: 2,601; cpp: 1,412; xml: 851; makefile: 725; sh: 563; php: 26
file content (149 lines) | stat: -rw-r--r-- 4,549 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
  Copyright 2016-2017 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Game state that asks a question using a dialog. }
unit GameStateAskDialog;

interface

uses Classes, CastleControls, CastleUIState;

type
  TStateAskDialog = class(TUIState)
  strict private
    type
      TZombieDialog = class(TCastleRectangleControl)
      strict private
        InsideRect: TCastleRectangleControl;
        Image: TCastleImageControl;
        LabelStats: TCastleLabel;
        ButtonRun, ButtonFight: TCastleButton;
        procedure RunClick(Sender: TObject);
        procedure FightClick(Sender: TObject);
      public
        constructor Create(AOwner: TComponent; const Male: boolean); reintroduce;
      end;
    var
    TransparentBackground: TCastleRectangleControl;
    Dialog: TZombieDialog;
  public
    { Whether to show male image. Set before doing @link(Start). }
    Male: boolean;
    procedure Start; override;
  end;

var
  StateAskDialog: TStateAskDialog;

implementation

uses CastleColors, CastleWindow, CastleUIControls, CastleFilesUtils,
  CastleUtils, CastleVectors;

{ TStateAskDialog.TZombieDialog ---------------------------------------------- }

constructor TStateAskDialog.TZombieDialog.Create(AOwner: TComponent; const Male: boolean);
begin
  inherited Create(AOwner);

  Width := 400;
  Height := 500;
  Color := HexToColor('5f3939'); // equivalent: Vector4(95/255, 57/255, 57/255, 1.0);

  InsideRect := TCastleRectangleControl.Create(Self);
  InsideRect.Width := CalculatedWidth - 10;
  InsideRect.Height := CalculatedHeight - 10;
  InsideRect.Color := Silver;
  InsideRect.Anchor(hpMiddle);
  InsideRect.Anchor(vpMiddle);
  InsertFront(InsideRect);

  Image := TCastleImageControl.Create(Self);
  if Male then
    Image.URL := ApplicationData('Male-Zombie-300px.png')
  else
    Image.URL := ApplicationData('Female-Zombie-300px.png');
  Image.Anchor(hpMiddle);
  Image.Anchor(vpTop, -10);
  InsideRect.InsertFront(Image);

  LabelStats := TCastleLabel.Create(Self);
  LabelStats.Color := Black;
  LabelStats.Html := true;
  { anything, just to show off the HTML :) }
  LabelStats.Caption := 'Statistics:' + NL +
    'Life: <font color="#ff0000">12%</font>' + NL +
    'Stamina: <font color="#ffff00">34%</font>' + NL +
    'Mana: <font color="#0000ff">56%</font>';
  LabelStats.Anchor(hpMiddle);
  LabelStats.Anchor(vpBottom, 100);
  InsideRect.InsertFront(LabelStats);

  ButtonRun := TCastleButton.Create(Self);
  ButtonRun.Caption := 'Run';
  ButtonRun.Anchor(hpLeft, 10);
  ButtonRun.Anchor(vpBottom, 10);
  ButtonRun.PaddingHorizontal := 40;
  ButtonRun.OnClick := @RunClick;
  InsideRect.InsertFront(ButtonRun);

  ButtonFight := TCastleButton.Create(Self);
  ButtonFight.Caption := 'Fight';
  ButtonFight.Anchor(hpRight, -10);
  ButtonFight.Anchor(vpBottom, 10);
  ButtonFight.PaddingHorizontal := 40;
  ButtonFight.OnClick := @FightClick;
  InsideRect.InsertFront(ButtonFight);
end;

procedure TStateAskDialog.TZombieDialog.RunClick(Sender: TObject);
begin
  { As this is just a demo, there's no actual "running",
    we just return to StatePlay. }
  TUIState.Pop(StateAskDialog);
end;

procedure TStateAskDialog.TZombieDialog.FightClick(Sender: TObject);
begin
  { As this is just a demo, there's no actual "fighting",
    we just return to StatePlay. }
  TUIState.Pop(StateAskDialog);
end;

{ TStateAskDialog ------------------------------------------------------------ }

procedure TStateAskDialog.Start;
begin
  inherited;

  { Do not allow clicks to pass to StatePlay underneath.
    We are transparent (show the StatePlay underneath),
    but we don't want to allow user to interact with it (e.g. by causing
    another StateAskDialog by clicking, or by pressing on
    StatePlay.ButtonBack). }
  InterceptInput := true;

  TransparentBackground := TCastleRectangleControl.Create(FreeAtStop);
  TransparentBackground.Color := Vector4(0.1, 0.1, 0.1, 0.5);
  TransparentBackground.FullSize := true;
  InsertFront(TransparentBackground);

  Dialog := TZombieDialog.Create(FreeAtStop, Male);
  Dialog.Anchor(hpMiddle);
  Dialog.Anchor(vpMiddle);
  InsertFront(Dialog);
end;

end.