1 /*
2 	SPDX-FileCopyrightText: 2009-2021 Graeme Gott <graeme@gottcode.org>
3 
4 	SPDX-License-Identifier: GPL-3.0-or-later
5 */
6 
7 #ifndef TANGLET_WINDOW_H
8 #define TANGLET_WINDOW_H
9 
10 class Board;
11 
12 #include <QHash>
13 #include <QMainWindow>
14 class QLabel;
15 class QStackedWidget;
16 
17 /**
18  * @brief The Window class controls creation of the game board and user interface.
19  */
20 class Window : public QMainWindow
21 {
22 	Q_OBJECT
23 
24 	class State;
25 	friend class State;
26 	class NewGameState;
27 	class OpenGameState;
28 	class OptimizingState;
29 	class PlayState;
30 	class AutoPauseState;
31 	class PauseState;
32 	class FinishState;
33 
34 public:
35 	/**
36 	 * Constructs a main window instance.
37 	 * @param file a game file to load instead of the previous game
38 	 */
39 	explicit Window(const QString& file = QString());
40 
41 	/**
42 	 * Override eventFilter to detect click on pause or new game screens.
43 	 * @param watched object to check
44 	 * @param event details of current event
45 	 * @return @c true if event was handled
46 	 */
47 	bool eventFilter(QObject* watched, QEvent* event) override;
48 
49 protected:
50 	/**
51 	 * Override closeEvent to save window size and position.
52 	 * @param event details of the close event
53 	 */
54 	void closeEvent(QCloseEvent* event) override;
55 
56 	/**
57 	 * Override dragEnterEvent to handle player dragging game file onto window.
58 	 * @param event details of drag event
59 	 */
60 	void dragEnterEvent(QDragEnterEvent* event) override;
61 
62 	/**
63 	 * Override dropEvent to handle player dropping game file or data onto window. This will start
64 	 * a new game based on the data received.
65 	 * @param event details of drop event
66 	 */
67 	void dropEvent(QDropEvent* event) override;
68 
69 	/**
70 	 * Override event to handle pausing when game window loses focus.
71 	 * @param event details of current event
72 	 * @return @c true if event was handled
73 	 */
74 	bool event(QEvent* event) override;
75 
76 private slots:
77 	/**
78 	 * Shows a dialog with version information and copyright notices.
79 	 */
80 	void about();
81 
82 	/**
83 	 * Starts a new game with the current settings, using the defaults if none have been chosen yet.
84 	 */
85 	void newRoll();
86 
87 	/**
88 	 * Starts a new game after prompting the player to choose game settings.
89 	 */
90 	void newGame();
91 
92 	/**
93 	 * Opens a new game.
94 	 */
95 	void chooseGame();
96 
97 	/**
98 	 * Prompts player where to save the start details for current game.
99 	 */
100 	void shareGame();
101 
102 	/**
103 	 * Ends current game and adds to high scores.
104 	 * @return @c true when game is ended, @c false otherwise
105 	 */
106 	bool endGame();
107 
108 	/**
109 	 * Automatically pause when window is obscured by menu.
110 	 */
111 	void autoPause();
112 
113 	/**
114 	 * Ends automatic pause and resumes game.
115 	 */
116 	void autoResume();
117 
118 	/**
119 	 * Pauses or resumes the game.
120 	 * @param pause pauses the game if @c true
121 	 */
122 	void setPaused(bool paused);
123 
124 	/**
125 	 * Shows a small dialog with the current game settings.
126 	 */
127 	void showDetails();
128 
129 	/**
130 	 * Show the high scores.
131 	 */
132 	void showScores();
133 
134 	/**
135 	 * Shows a dialog that allows the player to choose the board language, dice, and word list.
136 	 */
137 	void showLanguage();
138 
139 	/**
140 	 * Shows a dialog that allows the player to choose the interface language.
141 	 */
142 	void showLocale();
143 
144 	/**
145 	 * Shows a dialog describing the gameplay controls.
146 	 */
147 	void showControls();
148 
149 	/**
150 	 * Inform player that the word list is being optimized.
151 	 */
152 	void optimizingStarted();
153 
154 	/**
155 	 * Resume state before informing the player.
156 	 */
157 	void optimizingFinished();
158 
159 	/**
160 	 * Shows game board.
161 	 */
162 	void gameStarted();
163 
164 	/**
165 	 * Handles game ending and adds to high score board.
166 	 * @param score how many points the player earned
167 	 * @param max_score the maximum score available on the played board
168 	 */
169 	void gameFinished(int score, int max_score);
170 
171 private:
172 	/**
173 	 * Actually starts a new game.
174 	 * @param filename location of the game data
175 	 */
176 	void startGame(const QString& filename = QString());
177 
178 	/**
179 	 * Tracks if a menu is shown to set the game in the autopause state.
180 	 * @param menu which menu to track
181 	 */
182 	void monitorVisibility(QMenu* menu);
183 
184 private:
185 	Board* m_board; /**< the play area */
186 	QStackedWidget* m_contents; /**< the central widget of the window, shows messages or play area */
187 	QAction* m_details_action; /**< show the details of the current game */
188 	QAction* m_pause_action; /**< controls if the game is paused */
189 	QLabel* m_pause_screen; /**< message screen to show the game is paused */
190 
191 	State* m_state; /**< current state of the window */
192 	State* m_previous_state; /**< previous state of the window */
193 	QHash<QString, State*> m_states; /**< available states of the window */
194 };
195 
196 #endif // TANGLET_WINDOW_H
197