-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.h
executable file
·333 lines (262 loc) · 6.41 KB
/
UI.h
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* \file UI.h
* A Documented file.
*/
#ifndef UI_H
#define UI_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include "Network.h"
#include "NetHandler.h"
#ifdef __i386
#include <termios.h>
#include <pthread.h>
#endif
#ifdef __arm__
#include <sys/mutex.h>
#include <sys/thread.h>
#include <sys/timer.h>
#endif
#include "Player.h"
#include "Common.h"
#define ITOA_OFFSET 48
#define GHOST_SHIP 9
#define TIME_OUT 0
#define LOOP_WAIT 60
#define CP_SIZE 20
/**
* An enum.
* ASCII codes of used keys.
*/
enum buttons
{
ARROW_FIRST = 27, /**< arrow keys send three values: ARROW_FIRST, ARROW_SECOND and ARROW_DIR*/
ARROW_SECOND = 91,
ARROW_UP = 65, //the same as uppercase A
ARROW_DOWN = 66,
ARROW_LEFT = 68,
ARROW_RIGHT = 67,
ENTER_BUTTON = 10,
UNDO_BUTTON = 8, //backspace
STATS_BUTTON = 9, //tab
EXIT_BUTTON = 113, //q
YES_BUTTON = 'k', //TODO: find the real numbers
NO_BUTTON = 'e',
CHAT_BUTTON = '§'
};
/**
* Declarations of different views.
*/
enum views
{
AWAY = 0,
HOME = 1,
MENU = 2,
STATS = 3,
OPPONENT_MENU = 4,
CHAT = 5,
QUERY = 6
//more needed? SETTINGS, MANUAL , MENU_QUERY?
};
/**
* Main menu members.
*/
enum menuItems
{
MENU_SINGLE = 0,
MENU_MULTI = 1,
MENU_SETTINGS = 2,
MENU_MANUAL = 3,
#ifdef __arm__
MENU_QUIT = 3
#else
MENU_QUIT = 4
#endif
};
/**
* UI struct.
* Contains the data that UI needs to communicate with the user.
*/
typedef struct
{
Player * player; /**< local player*/
Player * enemy; /**< AI player or the enemy-over-net*/
Grids cursor; /**< cursor location*/
uint8_t cursorTable; /**< the table the cursor is on (home / away)*/
uint8_t menuCursor; /**< menu cursor location*/
char * message; /**< message to the user */
uint8_t visibleTable; /**< the table that is currently shown, @see enum views*/
//command queue
Command * frontCommand; /**< the next command to be served (front member of the command queue)*/
Command * backCommand; /**< back member of the command queue */
uint8_t CQsize; /**< number of commands in the command queue */
Command * commandPool; /**< a pool of empty commands */
uint8_t CPindex; /**< commandPool index */
//input flags:
bool terminalInput; /**< flag: do we listen terminal input? */
bool IOboardInput; /**< flag: do we listen IO-board input? */
bool speechInput; /**< flag: do we listen voice commands? */
NH * nh; /**< pointer to the nethandler */
MUTEX * CQmutex; /**< command queue mutex */
} UI;
//---- constructor & destructor ----//
/**
* Initializes the user interface.
* @return a new UI
*/
UI * UI_construct(void);
/**
* Deallocates allocated resources.
* @param ui the ui to be destructed
*/
void UI_closeInput(UI * ui);
//---- user input querys ----
/**
* Enables the user to select a square by moving the cursor.
* @param ui an UI struct
* @return true if enter was pressed, false if undo
*/
bool UI_selectUsingCursor(UI * ui);
/**
* Enables the user to select a direction where the next ship will point at.
* @param ui an UI struct
* @param shipLength the length of the ship
* @param [out] direction the direction selected by the user
* @return true if the selected direction is valid
*/
bool UI_readDirection(UI * ui, int shipLength, int * direction);
/**
* Pops a member from the command queue. If queue is empty, waits until a command arrives,
* or the the maxWait time.
* @param ui an UI struct
* @param maxWait timeout time (in milliseconds)
* @return The command as unsigned char, or TIME_OUT
*/
short UI_getInput(UI * ui, int maxWait);
void UI_CQpush(UI * ui, uint8_t command);
short UI_CQpop(UI * ui);
/**
* Checks the newly popped command, and serves it if it is a common command.
* @param ui an UI struct
* @param command as uint8_t
* @return true if the command was a common command
* @note common commands are "go to menu", "switch to home/away view", "show stats", "type message", ...
*/
bool UI_commonAction(UI * ui, uint8_t command);
//--- drawing ----
/**
* Draws a ghost ship to the board.
* @param shipLength the length of the ship
* @param ui an UI struct
* @param dir the orientation of the ship
* @return true if the ship fits into the given location
*/
bool UI_drawGhost(int shipLength, UI * ui, int dir);
/**
* Erases a ghost ship.
* @param ui an UI struct
* @param dir the orientation of the ship
*/
void UI_eraseGhost(UI * ui, int dir);
/**
* Main menu look & feel.
*/
void UI_menuSelect(UI * ui);
/**
* Settings menu.
* @param ui an UI struct
*/
void UI_settings(UI * ui);
/**
* Manual page.
* @param ui an UI struct
*/
void UI_manual(UI * ui);
/**
* Opponent menu look & feel
* @param ui an UI struct
* @param opponentList an array of ip addresses
* @param numOpponents number of opponents available
* @param cursor pointer to the cursor of the opponentList
*/
uint8_t UI_opponentSelect(UI * ui, uint32_t* opponentList, int numOpponents, int * cursor);
/**
* Chat look & feel
* @param ui an UI struct
*/
void UI_chatView(UI * ui);
//---- TUI methods----
/**
* Clear screen.
*/
void TUI_clear(void);
/**
* Update screen.
* @param ui an UI struct.
*/
void TUI_printArea(UI * ui);
/**
* Print the main menu.
* @param ui the user interface struct
*/
void TUI_printMenu(UI * ui);
/**
* Print the opponent menu
* @param ui an UI struct
* @param opponentList an array of ip addresses in char * format
* @param numOpponents number of opponents available
* @param cursor
*/
void TUI_printOpponentMenu(UI* ui, uint32_t* opponentList, int numOpponents, int cursor);
/**
* Print the chat screen
* @param ui the user interface struct
* @param message user written message
*/
void TUI_printChat(UI * ui);
/**
* Show stats during the game.
* @param ui the user interface struct
*/
void TUI_showStats(UI * ui);
/**
* Get char.
* @return the key that was pressed
*/
int TUI_getch(void);
/**
* Event loop (terminal /UART).
* @param ui an UI struct.
*/
#ifdef __i386
void *TUI_listener(void * ui);
#endif
#ifdef __arm__
THREAD(TUI_listener, arg);
#endif
/**
* Shows the user a message
* @param ui an UI struct.
* @param message the message test
*/
void TUI_showMessage(UI * ui, const char * message);
/**
* Ask the user a yes/no question
* @param ui an UI struct
* @param message
*/
bool TUI_booleanRequest(UI * ui, const char * message);
/**
* Shot animation
* @param ui an UI struct.
*/
void TUI_shotAnimation(UI* ui);
/**
* Get enter.
* @param ui an UI struct.
*/
void TUI_getEnter(UI * ui);
#endif