-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
225 lines (164 loc) · 6.17 KB
/
Copy pathInput.cpp
File metadata and controls
225 lines (164 loc) · 6.17 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
#include "Input.h"
#include "Output.h"
//======================================================================================//
// 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::GetSrting(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
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()
// Note: stoi(s) converts string s into its equivalent integer (for example, "55" is converted to 55)
string input = GetSrting(pO);
int integerValue = stoi(input);
return integerValue;
}
//======================================================================================//
// Game Functions //
//======================================================================================//
ActionType Input::GetUserAction() const
{
int x = -1, y = -1;
GetPointClicked(x, y);
// ============ GUI in the Design mode ============
if (UI.InterfaceMode == MODE_DESIGN)
{
// [1] If user clicks on the Toolbar
if (y >= 0 && y < UI.ToolBarHeight)
{
// Check which Menu item was clicked
// ==> This assumes that menu items are lined up horizontally <==
int clickedItemOrder = (x / UI.MenuItemWidth);
// Divide x coord of the point clicked by the menu item width (integer division)
// 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;
case ITM_ADD_BELT: return SET_BELT;
case ITM_ADD_ANTENNA: return SET_ANTENNA;
case ITM_ADD_WATER_PIT: return SET_WATER_PIT;
case ITM_DANGER_ZONE: return SET_DANGER_ZONE;
case ITM_WORKSHOP: return SET_WORKSHOP;
case ITM_ROTATING_CLOCK: return SET_ROTATING_CLOCK;
case ITM_ROTATING_COUNTER_CLOCK: return SET_ROTATING_COUNTER_CLOCK;
case ITM_COPY_GAME_OBJECT: return COPY_OBJ;
case ITM_CUT_GAME_OBJECT: return CUT_OBJ;
case ITM_PASTE_GAME_OBJECT: return PASTE_OBJ;
case ITM_DELETE_GAME_OBJECT: return DELETE_OBJ;
case ITM_SAVE_GRID: return SAVE_GRID;
case ITM_LOAD_GRID: return OPEN_GRID;
///TODO: Add cases for the other items of Design Mode
//DONE
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))
{
return GRID_AREA;
}
// [3] User clicks on the status bar
return STATUS;
}
// ============ GUI in the Play mode ============
else
{
///TODO:
// perform checks similar to Design mode checks above for the Play Mode
// and return the corresponding ActionType
// [1] If user clicks on the Toolbar
if (y >= 0 && y < UI.ToolBarHeight)
{
// Check which Menu item was clicked
// ==> This assumes that menu items are lined up horizontally <==
int clickedItemOrder = (x / UI.MenuItemWidth);
// Divide x coord of the point clicked by the menu item width (integer division)
// If division result is 0 ==> first item is clicked, if 1 ==> 2nd item and so on
switch (clickedItemOrder)
{
case ITM_SWITCH_TO_DESIGN_MODE: return TO_DESIGN_MODE;
case ITM_EXIT_PLAY_MODE: return EXIT;
case ITM_SELECT_COMMAND: return SELECT_COMMAND;
case ITM_EXECUTE_COMMANDS: return EXECUTE_COMMANDS;
case ITM_REPAIR: return REPAIR;
case ITM_USE_CONSUMABLE: return USE_CONSUMABLE;
case ITM_NEW_GAME: return NEW_GAME;
}
///TODO:
//done
// perform checks similar to Design mode checks above for the Play Mode
// and return the corresponding ActionType
return TO_DESIGN_MODE; // just for now ==> This should be updated
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
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))) {
int hCell = x / UI.CellWidth;
int vCell = (y - UI.ToolBarHeight) / UI.CellHeight;
//int division hate3m3l round fa tgeeb cell num
cellPos.SetHCell(hCell);
cellPos.SetVCell(vCell);
return cellPos;
}
//mesh el mafrood || badal &&
// in this case yayeb2a INvalid ya3ni
return CellPosition(-1, -1);
///TODO: SetHCell and SetVCell of the object cellPost appropriately
//DONE
// using the coordinates x, y and the appropriate variables of the UI_Info Object (UI)
}
return cellPos;
}
//////////////////////////////////////////////////////////////////////////////////////////
int Input::GetSelectedCommandIndex() const
{
int x = -1, y = -1;
GetPointClicked(x, y);
if ((y >= UI.height - UI.StatusBarHeight - UI.CommandsBarHeight - UI.AvailableCommandsYOffset) && (y < UI.height - UI.StatusBarHeight))
{
if (x < UI.AvailableCommandsXOffset || x > UI.AvailableCommandsXOffset + (UI.CommandItemWidth / 2) * MaxAvailableCommands)
return -1;
return (x - UI.AvailableCommandsXOffset) / (UI.CommandItemWidth / 2);;
}
return -1;
}
//////////////////////////////////////////////////////////////////////////////////////////