-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputZone.cpp
More file actions
204 lines (161 loc) · 4.53 KB
/
InputZone.cpp
File metadata and controls
204 lines (161 loc) · 4.53 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
#include "StdAfx.h"
#include "Globals.h"
#include "InputZone.h"
#include "CharacterRules.h"
InputZone::InputZone(void) : charBox(Globals::defaultImageBox())
{
}
InputZone::~InputZone(void)
{
}
void InputZone::updateCharBox()
{
charBox = Globals::defaultImageBox();
}
void InputZone::draw(HDC hdc, RECT rect) const
{
Box currentBox = charBox;
std::vector<int> indices = displayedIndices(rect);
for(std::vector<int>::iterator it = indices.begin(); it != indices.end(); ++it )
{
int i = *it;
const CompositeImage & image = m_images[i];
drawRect(hdc, realBox(rect, currentBox).shrunk_by(1), is_recognized(i) ? dsInputZoneCell : dsInputZoneInvalidCell);
image.draw(hdc, realBox(rect, currentBox));
currentBox = shiftBox(currentBox, rect);
}
drawRect(hdc, realBox(rect,currentInputBox(rect)).shrunk_by(1), dsInputZoneCurrent);
drawRect(hdc, realBox(rect,nextInputBox(rect)).shrunk_by(1), dsInputZoneNext);
}
std::vector<int> InputZone::displayedIndices(RECT _rect) const
{
Box rect = _rect;
int max_rows = rect.height() / charBox.height();
int max_columns = rect.width() / charBox.width();
int max_cells = max_rows * max_columns;
int first_cell = 0;
while((int)m_images.size() + 1 - first_cell > max_cells)
first_cell += max_columns;
std::vector<int> result;
for (int i = first_cell; i < (int)m_images.size(); ++i)
result.push_back(i);
return result;
}
bool InputZone::is_recognized(size_t index) const
{
return m_unrecognized_images.find(index) == m_unrecognized_images.end();
}
void InputZone::clear()
{
m_images.clear();
m_characters.clear();
m_recognized_characters.clear();
m_unrecognized_images.clear();
}
void InputZone::add(const CompositeImage & image)
{
if (!m_images.empty() && !m_images.back().empty())
{
step();
m_images.pop_back();
}
m_images.push_back(image);
recognize();
}
void InputZone::drag(const std::vector<Point> & points, RECT rect)
{
if (is_in_current_input_area(points, rect))
{
dragInInputZone(points, currentInputBox(rect), rect);
}
else if (is_in_next_area(points, rect))
{
Box inputBox = nextInputBox(rect);
step();
dragInInputZone(points, inputBox, rect);
}
}
void InputZone::dragInInputZone(const std::vector<Point> & points, Box inputBox, RECT rect)
{
m_images.back().add(Image(realBox(rect,inputBox), points));
recognize();
}
void InputZone::step()
{
if(m_recognized_characters.size() == 1)
m_characters.push_back(m_recognized_characters.front());
else
m_unrecognized_images.insert(m_images.size()-1);
m_recognized_characters.clear();
m_images.push_back(CompositeImage());
}
void InputZone::recognize()
{
m_recognized_characters.clear();
CharacterRules::CharacterProbabilities probs = CharacterRules::recognize(m_images.back());
for (CharacterRules::CharacterProbabilities::iterator it = probs.begin(); it != probs.end(); ++it)
{
CharacterRules::CharacterProbability & prob = *it;
Character & ch = prob.first;
m_recognized_characters.push_back(ch);
}
}
bool InputZone::is_in_next_area(const std::vector<Point> & points, RECT rect) const
{
Box nextBox = charBox;
std::vector<int> indices = displayedIndices(rect);
for(std::vector<int>::iterator it = indices.begin(); it != indices.end(); ++it )
{
nextBox = shiftBox(nextBox, rect);
}
//return nextBox.contains(points);
return realBox(rect,nextBox).contains(points);
}
bool InputZone::is_in_current_input_area(const std::vector<Point> & points, RECT rect) const
{
if (m_images.empty())
return false;
//return currentInputBox(rect).contains(points);
return realBox(rect,currentInputBox(rect)).contains(points);
}
Box InputZone::currentInputBox(RECT rect) const
{
Box nextBox = charBox;
Box prevBox;
std::vector<int> indices = displayedIndices(rect);
for(std::vector<int>::iterator it = indices.begin(); it != indices.end(); ++it )
{
prevBox = nextBox;
nextBox = shiftBox(nextBox, rect);
}
return prevBox;
}
Box InputZone::nextInputBox(RECT rect) const
{
Box nextBox = charBox;
Box prevBox;
std::vector<int> indices = displayedIndices(rect);
for(std::vector<int>::iterator it = indices.begin(); it != indices.end(); ++it )
{
prevBox = nextBox;
nextBox = shiftBox(nextBox, rect);
}
return nextBox;
}
std::vector<Character> InputZone::characters() const
{
std::vector<Character> result;
for(std::vector<Character>::const_iterator it = m_characters.begin(); it != m_characters.end(); ++it)
{
const Character & ch = *it;
if (! ch.m_name.empty() )
{
result.push_back(ch);
}
}
if(m_recognized_characters.size() == 1)
{
result.push_back(m_recognized_characters.front());
}
return result;
}