ScummVM API documentation
dialog.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef TWP_DIALOG_H
23 #define TWP_DIALOG_H
24 
25 #include "common/array.h"
26 #include "common/str.h"
27 #include "twp/yack.h"
28 #include "twp/scenegraph.h"
29 #include "twp/font.h"
30 #include "twp/motor.h"
31 
32 #define MAXDIALOGSLOTS 9
33 #define MAXCHOICES 6
34 #define SLIDINGSPEED 25.f
35 #define SLOTMARGIN 4.f
36 
37 namespace Twp {
38 
39 class Dialog;
40 class DialogSlot : public Node {
41 public:
42  DialogSlot();
43  virtual ~DialogSlot() override {}
44 
45 public:
46  bool _isValid = false;
47  Text _text;
49  Dialog *_dlg = nullptr;
50  float _shakeTime = 0.f;
51  bool _over = false;
53 };
54 
55 struct DialogContext {
56  Common::String actor;
57  Common::String dialogName;
58  bool parrot = true;
59  int limit = MAXCHOICES;
60 };
61 
62 enum DialogState {
63  None,
64  Active,
65  WaitingForChoice
66 };
67 
68 enum DialogConditionMode {
69  Once,
70  ShowOnce,
71  OnceEver,
72  ShowOnceEver,
73  TempOnce
74 };
75 
76 enum DialogSelMode {
77  Choose,
78  Show
79 };
80 
82  DialogConditionMode mode;
83  Common::String actorKey, dialog;
84  int line;
85 
87  DialogConditionState(DialogConditionMode mode, const Common::String &actorKey, const Common::String &dialog, int line);
88 };
89 
90 class DialogTarget {
91 public:
92  virtual ~DialogTarget() {}
93 
94  virtual Color actorColor(const Common::String &actor) = 0;
95  virtual Color actorColorHover(const Common::String &actor) = 0;
96  virtual Common::SharedPtr<Motor> say(const Common::String &actor, const Common::String &text) = 0;
97  virtual Common::SharedPtr<Motor> waitWhile(const Common::String &cond) = 0;
98  virtual void shutup() = 0;
99  virtual Common::SharedPtr<Motor> pause(float time) = 0;
100  virtual bool execCond(const Common::String &cond) = 0;
101 };
102 
104 public:
105  CondStateVisitor(Dialog *dlg, DialogSelMode mode);
106  DialogConditionState createState(int line, DialogConditionMode mode);
107 
108 private:
109  void visit(const YOnce &node) override;
110  void visit(const YShowOnce &node) override;
111  void visit(const YOnceEver &node) override;
112  void visit(const YTempOnce &node) override;
113 
114 private:
115  DialogSelMode _mode;
116  Dialog *_dlg = nullptr;
117 };
118 
119 class IsGoto : public YackVisitor {
120 public:
121  virtual ~IsGoto() override {}
122  void visit(const YGoto &node) override { _isGoto = true; }
123 
124 public:
125  bool _isGoto = false;
126 };
127 
128 class IsChoice : public YackVisitor {
129 public:
130  virtual ~IsChoice() override {}
131  void visit(const YChoice &node) override { _isChoice = true; }
132 
133 public:
134  bool _isChoice = false;
135 };
136 
137 class IsShutup : public YackVisitor {
138 public:
139  virtual ~IsShutup() override {}
140  void visit(const YShutup &node) override { _isShutup = true; }
141 
142 public:
143  bool _isShutup = false;
144 };
145 
146 class ExpVisitor : public YackVisitor {
147 public:
148  explicit ExpVisitor(Dialog *dialog);
149  virtual ~ExpVisitor() override;
150 
151 private:
152  void visit(const YCodeExp &node) override;
153  void visit(const YGoto &node) override;
154  void visit(const YSay &node) override;
155  void visit(const YPause &node) override;
156  void visit(const YParrot &node) override;
157  void visit(const YDialog &node) override;
158  void visit(const YOverride &node) override;
159  void visit(const YAllowObjects &node) override;
160  void visit(const YLimit &node) override;
161  void visit(const YWaitWhile &node) override;
162  void visit(const YWaitFor &node) override;
163  void visit(const YShutup &node) override;
164 
165 private:
166  Dialog *_dialog = nullptr;
167 };
168 
169 class CondVisitor : public YackVisitor {
170 public:
171  CondVisitor(Dialog *dialog);
172  virtual ~CondVisitor() override;
173 
174 private:
175  void visit(const YCodeCond &node) override;
176  void visit(const YOnce &node) override;
177  void visit(const YShowOnce &node) override;
178  void visit(const YOnceEver &node) override;
179  void visit(const YTempOnce &node) override;
180 
181 public:
182  bool _accepted = true;
183 
184 private:
185  Dialog *_dialog = nullptr;
186 };
187 
188 class Dialog : public Node {
189 public:
190  Dialog();
191  virtual ~Dialog() override;
192 
193  void choose(int choice);
194  void update(float dt);
195  DialogState getState() const { return _state; }
196 
197  void setMousePos(const Math::Vector2d &pos) { _mousePos = pos; }
198  Math::Vector2d getNextChoicePos(const Math::Vector2d &pos);
199  Math::Vector2d getPreviousChoicePos(const Math::Vector2d &pos);
200 
201  void start(const Common::String &actor, const Common::String &name, const Common::String &node);
202  void selectLabel(int line, const Common::String &name);
203  bool isOnce(int line) const;
204  bool isShowOnce(int line) const;
205  bool isOnceEver(int line) const;
206  bool isTempOnce(int line) const;
207  bool isCond(const Common::String &cond) const;
208 
209 private:
210  void choose(DialogSlot *slot);
211  Common::SharedPtr<YLabel> label(int line, const Common::String &name) const;
212  void gotoNextLabel();
213  bool choicesReady() const { return numSlots() > 0; }
214  void updateChoiceStates();
215  void run(Common::SharedPtr<YStatement> stmt);
216  bool acceptConditions(Common::SharedPtr<YStatement> stmt);
217  void running(float dt);
218 
219  void addSlot(Common::SharedPtr<YStatement> stmt);
220  int numSlots() const;
221  void clearSlots();
222 
223  Math::Vector2d getChoicePos(int index) const;
224  int getActiveSlot(const Math::Vector2d &pos) const;
225 
226  virtual void drawCore(const Math::Matrix4 &trsf) override final;
227 
228 public:
230  DialogContext _context;
232  Common::SharedPtr<Motor> _action;
233 
234 private:
235  DialogState _state = DialogState::None;
236  size_t _currentStatement = 0;
239  DialogSlot _slots[MAXDIALOGSLOTS];
240  Math::Vector2d _mousePos;
241  float _fadeTime = 0.f;
242 };
243 
244 } // namespace Twp
245 
246 #endif
Definition: dialog.h:119
Definition: yack.h:140
Definition: yack.h:197
Definition: str.h:59
Definition: dialog.h:90
Definition: yack.h:160
Definition: dialog.h:137
Definition: yack.h:84
Definition: dialog.h:188
Definition: array.h:52
Definition: font.h:144
Definition: yack.h:170
Definition: yack.h:282
Definition: dialog.h:103
Definition: yack.h:179
Definition: ptr.h:572
Definition: yack.h:101
Definition: yack.h:221
Definition: gfx.h:35
Definition: dialog.h:81
Definition: yack.h:149
Definition: yack.h:212
Definition: yack.h:206
Definition: dialog.h:169
Definition: dialog.h:55
Definition: dialog.h:40
Definition: dialog.h:146
Definition: yack.h:239
Definition: yack.h:94
Definition: dialog.h:128
Definition: scenegraph.h:41
Definition: yack.h:115
Definition: ptr.h:159
Definition: yack.h:188
Definition: yack.h:230
Definition: yack.h:129
Definition: achievements_tables.h:27
Definition: yack.h:108