-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInput.cpp
162 lines (129 loc) · 4.57 KB
/
Input.cpp
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
#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::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
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()
string s;
s = GetString(pO);
// Note: stoi(s) converts string s into its equivalent integer (for example, "55" is converted to 55)
return stoi(s); // this line should be changed with your implementation
}
//======================================================================================//
// 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_ADD_LADDER: return ADD_LADDER;
case ITM_ADD_SNAKE: return ADD_SNAKE;
case ITM_ADD_CARD: return ADD_CARD;
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
case ITM_COPY_CARD: return COPY_CARD;
case ITM_CUT_CARD: return CUT_CARD;
case ITM_PASTE_CARD: return PASTE_CARD;
case ITM_DELETE_GAME_OBJECT: return DELETE_GAME_OBJECT;
case ITM_SAVE_GRID:return SAVE_GRID;
case ITM_OPEN_GRID:return OPEN_GRID;
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 if (UI.InterfaceMode == MODE_PLAY)
{
///TODO:
// perform checks similar to Design mode checks above for the Play Mode
// and return the corresponding ActionType
if (y >= 0 && y < UI.ToolBarHeight) {
int ClickedItemOrder = (x / UI.MenuItemWidth);
switch (ClickedItemOrder) {
case ITM_ROLL_DICE: return ROLL_DICE;
case ITM_SWITCH_TO_DESIGN_MODE: return TO_DESIGN_MODE;
case ITM_INPUT_DICE_VALUE: return INPUT_DICE_VALUE;
case ITM_NEW_GAME: return NEW_GAME;
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////
CellPosition Input::GetCellClicked() const
{
int x, y, x_pos, y_pos;
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))
{
///TODO: SetHCell and SetVCell of the object cellPost appropriately
// using the coordinates x, y and the appropriate variables of the UI_Info Object (UI)
x_pos = x / UI.CellWidth;
y_pos = (y - UI.ToolBarHeight) / UI.CellHeight;
cellPos.SetHCell(x_pos);
cellPos.SetVCell(y_pos);
}
else {
cellPos.SetHCell(-1);
cellPos.SetVCell(-1);
}
}
return cellPos;
}
//////////////////////////////////////////////////////////////////////////////////////////