-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
242 lines (196 loc) · 7.2 KB
/
Copy pathInput.cpp
File metadata and controls
242 lines (196 loc) · 7.2 KB
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
#include "Input.h"
#include "Output.h"
#include "DEFS.h"
#include <iostream>
//======================================================================================//
// General Functions //
//======================================================================================//
Input::Input(window* pW)
{
pWind = pW; // point to the passed window
}
//////////////////////////////////////////////////////////////////////////////////////////
void Input::GetPointClicked(int &x, int &y) const
{
pWind->WaitMouseClick(x, y); // Note: x and y of WaitMouseClick are sent by reference
}
//////////////////////////////////////////////////////////////////////////////////////////
string Input::GetString(Output *pO) const
{
string Label;
char Key;
while(1)
{
pWind->WaitKeyPress(Key);
if(Key == 27 ) // ESCAPE key is pressed
return ""; // returns nothing as user has cancelled label
if (Key == 13) { // ENTER key is pressed
if (Label == "") {
return "0";
}
return Label;
}
if((Key == 8) && (Label.size() >= 1)) // BackSpace is pressed
Label.resize(Label.size() -1 );
else
Label += Key;
if (pO)
pO->PrintMessage(Label);
}
}
//////////////////////////////////////////////////////////////////////////////////////////
int Input::GetInteger(Output *pO) const
{
///TODO: implement the GetInteger function as described in Input.h file
// using function GetString() defined above and function stoi()
//start of additions//
//Clearing the status bar and preparing for input
pO->ClearStatusBar();
//Call GetString() to handle the actual keyboard typing and backspacing which is defined above
string Label = GetString(pO);
//Handling cases where the user enters nothing or cancels (Escape)
if (Label == "")
{
return 0;
}
// Note: stoi(s) converts string s into its equivalent integer (for example, "55" is converted to 55)
try {
return stoi(Label); //convert string -> into integer
}
catch (...) {
pO->PrintMessage("Invalid input! Please enter an integer: ");
return GetInteger(pO); // ba call GetInteger repetitevly le7ad a valid integer is entered
}
}
//======================================================================================//
// Game Functions //
//======================================================================================//
ActionType Input::GetUserAction() const
{
int x = -1, y = -1;
GetPointClicked(x, y);
// ============ GUI in the Design mode ============
if ( UI.InterfaceMode == MODE_DESIGN )
{
//if the user clicked on the Toolbar
if ( y >= 0 && y < UI.ToolBarHeight)
{
// Check which Menu item was clicked
// ==> This assumes that menu items are lined up horizontally <==
// Divide x coord of the point clicked by the menu item width (integer division)
int clickedItemOrder = (x / UI.MenuItemWidth);
// If division result is 0 ==> first item is clicked, if 1 ==> 2nd item and so on
switch (clickedItemOrder)
{
case ITM_SET_FLAG_CELL: return SET_FLAG_CELL;
case ITM_EXIT: return EXIT;
case ITM_SWITCH_TO_PLAY_MODE: return TO_PLAY_MODE;
///TODO: Add cases for the other items of Design Mode
//start of adjustments
case ITM_ADD_ANTENNA: return ADD_ANTENNA;
case ITM_ADD_BELT: return ADD_BELT;
case ITM_ADD_WATER_PIT: return ADD_WATER_PIT;
case ITM_ADD_DANGER_ZONE: return ADD_DANGER_ZONE;
case ITM_ADD_WORKSHOP: return ADD_WORKSHOP;
case ITM_ADD_ROTATING_GEAR: return ADD_ROTATING_GEAR;
case ITM_COPY: return COPY_OBJECT;
case ITM_CUT: return CUT_OBJECT;
case ITM_PASTE: return PASTE_OBJECT;
case ITM_DELETE: return DELETE_OBJECT;
case ITM_SAVE: return SAVE_GRID;
case ITM_LOAD: return LOAD_GRID;
//end of adjustments & all of the above was adding Items needed for design mode
default: return EMPTY; // A click on empty place in toolbar
}
}
// [2] User clicks on the grid area
if ( (y >= UI.ToolBarHeight) && (y < UI.height - UI.StatusBarHeight - UI.CommandsBarHeight))
{
return GRID_AREA; //click in middle of screen
}
if ((y >= UI.height - UI.StatusBarHeight - UI.CommandsBarHeight) && (y < UI.height - UI.StatusBarHeight))
{
return COMMAND; //click was in command bar
}
// [3] User clicks on the status bar
return STATUS; // clicked in status line at bottom
}
// ============ GUI in the Play mode ============
else
{
///TODO:
// perform checks similar to Design mode checks above for the Play Mode
// and return the corresponding ActionType
//start of adjustments(Abdallah)
if (y >= 0 && y <= UI.ToolBarHeight) {
int clickedItemOrder = (x / UI.MenuItemWidth);
//all of the cases beneth represent gameplay actions
switch (clickedItemOrder)
{
case ITM_EXECUTE_COMMANDS: return EXECUTE_COMMANDS;
case ITM_SELECT_COMMAND: return SELECT_COMMAND;
case ITM_SWITCH_TO_DESIGN_MODE: return TO_DESIGN_MODE;
case ITM_REBOOT_REPAIR: return REBOOT_REPAIR;
case ITM_NEW_GAME: return NEW_GAME;
case ITM_EXIT_PLAY: return EXIT;
default: return EMPTY; // A click on empty place in toolbar
}
}
if ((y >= UI.ToolBarHeight) && (y < UI.height - UI.StatusBarHeight - UI.CommandsBarHeight))
{
return GRID_AREA;
}
if ((y >= UI.height - UI.StatusBarHeight - UI.CommandsBarHeight) && (y < UI.height - UI.StatusBarHeight))
{
return COMMAND;
}
// [3] User clicks on the status bar
return STATUS;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
CellPosition Input::GetCellClicked() const
{
int x,y;
pWind->WaitMouseClick(x, y); // Get the coordinates of the user click
CellPosition cellPos;
if ( UI.InterfaceMode == MODE_DESIGN )
{
if ( y >= UI.ToolBarHeight && y <= (UI.height - UI.StatusBarHeight - UI.CommandsBarHeight))
{
///TODO: SetHCell and SetVCell of the object cellPost appropriately
// using the coordinates x, y and the appropriate variables of the UI_Info Object (UI)
//start of adjustments
//
//subtracting the ToolBarHeight to get the position wrt the grid start aka origin
int vCell = (y - UI.ToolBarHeight) / UI.CellHeight;
//calculating the Horizantal cell
int hCell = x / UI.CellWidth;
//setting the cell pos calculated above using the setter functions
cellPos.SetVCell(vCell);
cellPos.SetHCell(hCell);
}
else
{
//Click outside of the grid results into an error
cellPos.SetVCell(-1);
cellPos.SetHCell(-1);
}
//end of adjusments
}
return cellPos;
}
//////////////////////////////////////////////////////////////////////////////////////////
int Input::GetSelectedCommandIndex() const
{
int x = -1, y = -1;
GetPointClicked(x, y);
if ((y >= UI.height - UI.StatusBarHeight - (UI.CommandsBarHeight / 2) - UI.AvailableCommandsYOffset) && (y < UI.height - UI.StatusBarHeight - (UI.CommandsBarHeight / 2) + UI.AvailableCommandsYOffset))
{
if (x < (UI.width/2) + UI.AvailableCommandsTextWidth || x > (UI.width / 2) + UI.AvailableCommandsTextWidth + (UI.CommandItemWidth / 2) * (MaxAvailableCommands + 1))
return -1;
return (x - ((UI.width / 2) + UI.AvailableCommandsTextWidth + (2*UI.AvailableCommandsXOffset))) / (UI.CommandItemWidth / 2);
}
return -1;
}
//////////////////////////////////////////////////////////////////////////////////////////