-
Notifications
You must be signed in to change notification settings - Fork 0
/
horhints.cpp
206 lines (167 loc) · 4.49 KB
/
horhints.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
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
#include "horhints.h"
#include "main.h"
#include "utils.h"
#include "sound.h"
#define HINTS_COLS 3
#define HINTS_ROWS 8
#define TILE_GAP_X 4
#define TILE_GAP_Y 4
#define TILE_X 348
#define TILE_Y 68
#define TILE_WIDTH 48
#define TILE_HEIGHT 48
HorHints::HorHints(IconSet &is, Rules &r): iconSet(is)
{
reset(r);
}
HorHints::HorHints(IconSet &is, Rules &rl, std::istream &stream): iconSet(is)
{
int qty = readInt(stream);
for (int i = 0; i < qty; i++) {
int no = readInt(stream);
numbersArr.push_back(no);
Rule *r = getRule(rl, no);
int excluded = readInt(stream);
if (excluded) {
excludedRules.push_back(r);
rules.push_back(NULL);
} else {
excludedRules.push_back(NULL);
rules.push_back(r);
}
}
showExcluded = readInt(stream);
int x, y;
SDL_GetMouseState(&x, &y);
highlighted = getRuleNo(x, y);
}
void HorHints::reset(Rules &r)
{
rules.clear();
excludedRules.clear();
numbersArr.clear();
int no = 0;
for (auto rule : r) {
if (rule->getShowOpts() == Rule::SHOW_HORIZ) {
rules.push_back(rule);
excludedRules.push_back(NULL);
numbersArr.push_back(no);
}
no++;
}
showExcluded = false;
int x, y;
SDL_GetMouseState(&x, &y);
highlighted = getRuleNo(x, y);
}
void HorHints::draw()
{
for (int i = 0; i < HINTS_ROWS; i++)
for (int j = 0; j < HINTS_COLS; j++)
drawCell(j, i, true);
}
void HorHints::drawCell(int col, int row, bool addToUpdate)
{
int x = TILE_X + col * (TILE_WIDTH*3 + TILE_GAP_X);
int y = TILE_Y + row * (TILE_HEIGHT + TILE_GAP_Y);
Rule *r = NULL;
int no = row * HINTS_COLS + col;
if (no < (int)rules.size())
if (showExcluded)
r = excludedRules[no];
else
r = rules[no];
if (r)
r->draw(x, y, iconSet, no == highlighted);
else
for (int i = 0; i < 3; i++)
screen.draw(x + TILE_HEIGHT*i, y, iconSet.getEmptyHintIcon());
if (addToUpdate)
screen.addRegionToUpdate(x, y, TILE_WIDTH*3, TILE_HEIGHT);
}
bool HorHints::onMouseButtonDown(int button, int x, int y)
{
if (button != 3)
return false;
int no = getRuleNo(x, y);
if (no < 0) return false;
int row = no / HINTS_COLS;
int col = no - row * HINTS_COLS;
if (showExcluded) {
Rule *r = excludedRules[no];
if (r) {
sound->play(L"whizz.wav");
rules[no] = r;
excludedRules[no] = NULL;
drawCell(col, row);
}
} else {
Rule *r = rules[no];
if (r) {
sound->play(L"whizz.wav");
rules[no] = NULL;
excludedRules[no] = r;
drawCell(col, row);
}
}
return true;
}
void HorHints::toggleExcluded()
{
showExcluded = !showExcluded;
draw();
}
bool HorHints::onMouseMove(int x, int y)
{
int no = getRuleNo(x, y);
if (no != highlighted) {
int old = highlighted;
highlighted = no;
if (isActive(old)) {
int row = old / HINTS_COLS;
int col = old - row * HINTS_COLS;
drawCell(col, row);
}
if (isActive(no)) {
int row = no / HINTS_COLS;
int col = no - row * HINTS_COLS;
drawCell(col, row);
}
}
return false;
}
int HorHints::getRuleNo(int x, int y)
{
if (! isInRect(x, y, TILE_X, TILE_Y, (TILE_WIDTH*3 + TILE_GAP_X) * HINTS_COLS,
(TILE_HEIGHT + TILE_GAP_Y) * HINTS_ROWS))
return -1;
x = x - TILE_X;
y = y - TILE_Y;
int col = x / (TILE_WIDTH*3 + TILE_GAP_X);
if (col * (TILE_WIDTH*3 + TILE_GAP_X) + TILE_WIDTH*3 < x)
return -1;
int row = y / (TILE_HEIGHT + TILE_GAP_Y);
if (row * (TILE_HEIGHT + TILE_GAP_Y) + TILE_HEIGHT < y)
return -1;
int no = row * HINTS_COLS + col;
if (no >= (int)rules.size())
return -1;
return no;
}
bool HorHints::isActive(int ruleNo)
{
if ((ruleNo < 0) || (ruleNo >= (int)rules.size()))
return false;
Rule *r = showExcluded ? excludedRules[ruleNo] : rules[ruleNo];
return r != NULL;
}
void HorHints::save(std::ostream &stream)
{
int cnt = numbersArr.size();
writeInt(stream, cnt);
for (int i = 0; i < cnt; i++) {
writeInt(stream, numbersArr[i]);
writeInt(stream, rules[i] ? 0 : 1);
}
writeInt(stream, showExcluded ? 1 : 0);
}