1 ///////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2011 by The Allacrost Project
3 //            Copyright (C) 2012-2016 by Bertram (Valyria Tear)
4 //                         All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See https://www.gnu.org/copyleft/gpl.html for details.
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef __MAP_DIALOGUE_SUPERVISOR_HEADER__
12 #define __MAP_DIALOGUE_SUPERVISOR_HEADER__
13 
14 #include "common/dialogue.h"
15 
16 namespace vt_map
17 {
18 
19 namespace private_map
20 {
21 
22 class SpriteDialogue;
23 class MapDialogueOptions;
24 
25 /** ****************************************************************************
26 *** \brief Manages dialogue execution on maps
27 ***
28 *** The MapMode class creates an instance of this class to handle all dialogue
29 *** processing that occurs on the map. This includes containing the dialogue objects,
30 *** handling user input, processing of dialogue events, and display timing of the
31 *** dialogue.
32 *** ***************************************************************************/
33 class MapDialogueSupervisor
34 {
35 public:
36     MapDialogueSupervisor();
37 
38     ~MapDialogueSupervisor();
39 
40     //! \brief Processes user input and updates the state of the dialogue
41     void Update();
42 
43     //! \brief Draws the dialogue window, text, portraits, and other visuals to the screen
44     void Draw();
45 
46     /** \brief Adds a new dialogue to be managed by the supervisor
47     *** \param dialogue Pointer to a CommonDialogue object that was created with the new operator
48     ***
49     *** The dialogue to add must have a unique dialogue ID that is not already stored by this class
50     *** instance. If a dialogue is found with the same ID, the dialogue will not be added and the
51     *** dialogue object will be deleted from memory. All dialogues that are successfully added will
52     *** be later deleted when this class' destructor is invoked, so make sure you only pass in objects
53     *** that were created with the "new" operator.
54     **/
55     void AddDialogue(SpriteDialogue *dialogue);
56 
57     /** \brief Prepares the dialogue manager to begin processing a new dialogue
58     *** \param dialogue_id The id number of the dialogue to begin
59     **/
60     void StartDialogue(const std::string& dialogue_id);
61 
62     //! \brief Immediately ends any dialogue that is taking place
63     void EndDialogue();
64 
65     /** \brief Returns a pointer to the CommonDialogue with the requested ID value
66     *** \param dialogue_id The identification number of the dialogue to retrieve
67     *** \return A pointer to the dialogue requested, or nullptr if no such dialogue was found
68     **/
69     SpriteDialogue* GetDialogue(const std::string& dialogue_id);
70 
71     //! \name Class member access functions
72     //@{
GetDialogueState()73     vt_common::DIALOGUE_STATE GetDialogueState() const {
74         return _state;
75     }
76 
GetCurrentDialogue()77     SpriteDialogue* GetCurrentDialogue() const {
78         return _current_dialogue;
79     }
80 
GetCurrentOptions()81     MapDialogueOptions* GetCurrentOptions() const {
82         return _current_options;
83     }
84 
GetLineTimer()85     vt_system::SystemTimer &GetLineTimer() {
86         return _line_timer;
87     }
88 
GetLineCounter()89     uint32_t GetLineCounter() const {
90         return _line_counter;
91     }
92 
93     //! \brief Returns a new unique dialogue id string.
GenerateDialogueID()94     std::string GenerateDialogueID() {
95         return vt_utils::NumberToString(_next_dialogue_id++);
96     }
97     //@}
98 
99 private:
100     //! \brief Retains the current state of the dialogue execution
101     vt_common::DIALOGUE_STATE _state;
102 
103     //! \brief A numeric value used to generate unique dialogue ids.
104     uint32_t _next_dialogue_id;
105 
106     //! \brief Contains all dialogues used in the map in a std::map structure. The dialogue IDs serve as the map keys
107     std::map<std::string, SpriteDialogue*> _dialogues;
108 
109     //! \brief A pointer to the current piece of dialogue that is active
110     SpriteDialogue* _current_dialogue;
111 
112     //! \brief A pointer to the current set of options for the active dialogue line
113     MapDialogueOptions* _current_options;
114 
115     //! \brief A timer employed for dialogues which have a display time limit
116     vt_system::SystemTimer _line_timer;
117 
118     //! \brief Keeps track of which line is active for the current dialogue
119     uint32_t _line_counter;
120 
121     //! \brief Holds the text and graphics that should be displayed for the dialogue
122     vt_common::DialogueWindow _dialogue_window;
123 
124     //! \brief Keeps in memory whether the emote event has been triggered.
125     bool _emote_triggered;
126 
127     // ---------- Private methods
128 
129     //! \brief Basically wait for the emote to be finished before starting the line.
130     void _UpdateEmote();
131 
132     //! \brief Updates the dialogue when it is in the line state
133     void _UpdateLine();
134 
135     //! \brief Updates the dialogue when it is in the option state
136     void _UpdateOptions();
137 
138     /** \brief Begins the display of the line indexed by the value of _line_counter
139     ***
140     *** This is called whenever a dialogue begins or is moved to the next line. Its duties include updating the
141     *** dialogue state, dialogue window displays with data from the new line, and setting up the line timer.
142     ***
143     *** \note This method does not check that the _line_counter member refers to a valid line. It is the caller's
144     *** responsibility to ensure that _line_counter is valid prior to calling this method.
145     **/
146     void _BeginLine();
147 
148     /** \brief Finishes the current dialogue line and moves the dialogue forward to the next line
149     ***
150     *** This function determines the next line that the dialogue should proceed to based on the properties of the
151     *** current line. This includes "branching" to the appropriate next line based on the option selected by the player
152     *** when options were enabled on the current line. Should the line counter become invalid or the dialogue is to end
153     *** after the present line, this function will invoke the EndDialogue() method. In addition to proceeding to the next
154     *** line, this method is also responsible for invoking any events that were to occur at the conclusion of the present
155     *** line.
156     **/
157     void _EndLine();
158 
159     //! \brief Restores participating sprites to their state before this dialogue started
160     void _RestoreSprites();
161 };
162 
163 } // namespace private_map
164 
165 } // namespace vt_map
166 
167 #endif // __MAP_DIALOGUE_SUPERVISOR_HEADER__
168