-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCells.hpp
155 lines (132 loc) · 3.1 KB
/
Cells.hpp
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
//Cells.h
/*Sudoku Cells interface class*/
#pragma once
namespace Sudoku
{
class Cell : public Button
{
private:
// Define editiability (can type number in cell)
bool mEditable;
// Define number currently displayed and the actual solution
char mCharNumber;
char mCharSolution;
//Intiger Solution
int mSolution;
public:
// Constructor
Cell();
// Set and get number
void setNumber(const int number);
char getNumber() const;
// Set solution
void setSolution(const int solution);
void findSolution(SDL_Texture * textureCatch[]);
// Set and get editability (setting editability changes colour mapping for button)
void setEditable(const bool editable);
bool isEditable() const;
// Handle events
void handleKeyboardEvent(const SDL_Event* event, SDL_Texture* textureCache[]);
void handleDisplayedInput(SDL_Texture * textureCatch[], int n);
// Compare number with solution
bool isCorrect() const;
};
};
//#include "SudokuCell.h"
Sudoku::Cell::Cell()
: mEditable(false),
mCharNumber(' '),
mCharSolution(' ')
{
}
void Sudoku::Cell::setNumber(const int number)
{
if (number == 0)
{
mCharNumber = ' ';
}
else
{
mCharNumber = '0' + number;
}
}
char Sudoku::Cell::getNumber() const
{
return mCharNumber;
}
void Sudoku::Cell::setSolution(const int solution)
{
mSolution = solution;
if (solution == 0)
{
mCharSolution = ' ';
}
else
{
mCharSolution = '0' + solution;
}
}
void Sudoku::Cell::findSolution(SDL_Texture * textureCatch[])
{
setTexture(textureCatch[mSolution]);
mCharNumber = mCharSolution;
}
void Sudoku::Cell::setEditable(const bool editable)
{
mEditable = editable;
if (mEditable)
{
mMouseOutColour = { 153, 209, 213, SDL_ALPHA_OPAQUE }; // light Green
mMouseOverMotionColour = { 204, 255, 255, SDL_ALPHA_OPAQUE }; // green blue
mMouseDownColour = { 91, 191, 116, SDL_ALPHA_OPAQUE }; // Green
mMouseUpColour = { 204, 255, 255, SDL_ALPHA_OPAQUE }; // green blue
}else
{
mMouseOutColour = { 192, 192, 192, SDL_ALPHA_OPAQUE }; // Gray
mMouseOverMotionColour = { 192, 192, 192, SDL_ALPHA_OPAQUE }; // Gray
mMouseDownColour = { 192, 192, 192, SDL_ALPHA_OPAQUE }; // Gray
mMouseUpColour = { 192, 192, 192, SDL_ALPHA_OPAQUE }; // Gray
}
}
bool Sudoku::Cell::isEditable() const
{
return mEditable;
}
void Sudoku::Cell::handleKeyboardEvent(const SDL_Event* event, SDL_Texture* textureCache[])
{
// Handle backspace
if (event->key.keysym.sym == SDLK_BACKSPACE && mCharNumber != ' ')
{
// Empty char
mCharNumber = ' ';
// Set empty texture
setTexture(textureCache[0]);
}
// Handle text input
else if (event->type == SDL_TEXTINPUT)
{
// Check if integer > 0
if (atoi(event->text.text))
{
// Replace char
mCharNumber = *(event->text.text);
// Set character based on number
setTexture(textureCache[atoi(event->text.text)]);
}
}
}
void Sudoku::Cell::handleDisplayedInput(SDL_Texture * textureCatch[], int n)
{
if(n == 0)
{
mCharNumber = ' ';
setTexture(textureCatch[0]);
}
else
mCharNumber = '0' + n;
setTexture(textureCatch[n]);
}
bool Sudoku::Cell::isCorrect() const
{
return mCharNumber == mCharSolution;
}