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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "agi/agi.h"
#include "agi/graphics.h"
#include "agi/keyboard.h"
#include "common/func.h"
namespace Agi {
#define kModePre 0
#define kModeNum 1
#define kModeAbc 2
static byte s_asciiToNumTable[256];
void setAsciiToNumTableBatch(const char *chars, byte value) {
while (*chars) {
s_asciiToNumTable[tolower(*chars)] = value;
s_asciiToNumTable[toupper(*chars)] = value;
chars++;
}
}
void initAsciiToNumTable() {
memset(s_asciiToNumTable, 0, sizeof(s_asciiToNumTable));
setAsciiToNumTableBatch("1'-.&", 1);
setAsciiToNumTableBatch("2abc", 2);
setAsciiToNumTableBatch("3def", 3);
setAsciiToNumTableBatch("4ghi", 4);
setAsciiToNumTableBatch("5jkl", 5);
setAsciiToNumTableBatch("6mno", 6);
setAsciiToNumTableBatch("7pqrs", 7);
setAsciiToNumTableBatch("8tuv", 8);
setAsciiToNumTableBatch("9wxyz", 9);
}
class SearchTree {
public:
//byte val;
//SearchTree *parent; // TODO: Could be used to speed up re-searches
SearchTree *children[10];
Common::StringList words;
SearchTree() {
memset(children, 0, sizeof(children));
}
SearchTree *getChild(byte val) {
assert(val < 10);
if (children[val] == 0) {
children[val] = new SearchTree();
}
return children[val];
}
SearchTree *findChildWithWords() {
if (!words.empty())
return this;
SearchTree *child = 0;
for (int i = 0; i < 10 && !child; ++i) {
if (children[i])
child = children[i]->findChildWithWords();
}
return child;
}
};
void AgiEngine::insertSearchNode(const char *word) {
// Insert the word into the tree
SearchTree *tree = _searchTreeRoot;
assert(tree);
for (int i = 0; word[i] != 0; ++i) {
byte key = s_asciiToNumTable[(int)word[i]];
if (key == 0)
return; // abort!
tree = tree->getChild(key);
}
// TODO: Sort words, remove duplicates... ?
tree->words.push_back(word);
}
bool AgiEngine::predictiveDialog(void) {
int key = 0, active = -1, lastactive = 0;
bool rc = false;
uint8 x;
int y;
int bx[17], by[17];
String prefix;
char temp[MAXWORDLEN + 1], repeatcount[MAXWORDLEN];
AgiBlock tmpwindow;
bool navigationwithkeys = false;
bool processkey;
_predictiveDialogRunning = true;
_system->setFeatureState(OSystem::kFeatureDisableKeyFiltering, true);
// FIXME: Move this to a more appropriate place.
initAsciiToNumTable();
const char *buttonStr[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
const char *buttons[] = {
"(1)'-.&", "(2)abc", "(3)def",
"(4)ghi", "(5)jkl", "(6)mno",
"(7)pqrs", "(8)tuv", "(9)wxyz",
"(#)next", "add",
"<",
"Cancel", "OK",
"Pre", "(0) ", NULL
};
const int colors[] = {
15, 0, 15, 0, 15, 0,
15, 0, 15, 0, 15, 0,
15, 0, 15, 0, 15, 0,
15, 12, 15, 12,
15, 0,
15, 0, 15, 0,
14, 0, 15, 0, 0, 0
};
const char *modes[] = { "(*)Pre", "(*)123", "(*)Abc" };
if (!_searchTreeRoot) {
loadDict();
if (!_searchTreeRoot)
return false;
}
memset(repeatcount, 0, MAXWORDLEN);
// show the predictive dialog.
// if another window is already in display, save its state into tmpwindow
tmpwindow.active = false;
if (_game.window.active)
memcpy(&tmpwindow, &(_game.window), sizeof(AgiBlock));
drawWindow(50, 40, 269, 159);
_gfx->drawRectangle(62, 54, 249, 66, MSG_BOX_TEXT);
_gfx->flushBlock(62, 54, 249, 66);
bx[15] = 73; // Zero/space
by[15] = 120;
bx[9] = 110; // next
by[9] = 120;
bx[10] = 172; // add
by[10] = 120;
bx[14] = 200; // Mode
by[14] = 120;
bx[11] = 252; // Backspace
by[11] = 57;
bx[12] = 180; // Cancel
by[12] = 140;
bx[13] = 240; // OK
by[13] = 140;
x = 73;
y = 75;
for (int i = 0; i < 9; i++) {
bx[i] = x;
by[i] = y;
x += 60;
if (i % 3 == 2) {
y += 15;
x = 73;
}
}
/* clear key queue */
while (_gfx->keypress()) {
_gfx->getKey();
}
prefix.clear();
_currentCode.clear();
_currentWord.clear();
_wordNumber = 0;
_activeTreeNode = 0;
int mode = kModePre;
bool needRefresh = true;
for (;;) {
if (needRefresh) {
for (int i = 0; buttons[i]; i++) {
int color1 = colors[i * 2];
int color2 = colors[i * 2 + 1];
if (i == 9 && !((mode != kModeAbc && _activeTreeNode && _activeTreeNode->words.size() > 1) ||
(mode == kModeAbc && _currentWord.size() && _currentWord.lastChar() != ' '))) { // Next
color2 = 7;
}
bool _addIsActive = false; // FIXME
if (i == 10 && !_addIsActive) { // Add
color2 = 7;
}
if (i == 14) {
_gfx->drawButton(bx[i], by[i], modes[mode], i == active, 0, color1, color2);
} else {
_gfx->drawButton(bx[i], by[i], buttons[i], i == active, 0, color1, color2);
}
}
temp[MAXWORDLEN] = 0;
strncpy(temp, prefix.c_str(), MAXWORDLEN);
strncat(temp, _currentWord.c_str(), MAXWORDLEN);
for (int i = prefix.size() + _currentCode.size(); i < MAXWORDLEN; i++)
temp[i] = ' ';
printText(temp, 0, 8, 7, MAXWORDLEN, 15, 0);
_gfx->flushBlock(62, 54, 249, 66);
if (active >= 0 && !navigationwithkeys) {
// provide visual feedback only when not navigating with the arrows
// so that the user can see the active button.
active = -1;
needRefresh = true;
} else
needRefresh = false;
_gfx->doUpdate();
}
_gfx->pollTimer(); /* msdos driver -> does nothing */
key = doPollKeyboard();
processkey = false;
switch (key) {
case KEY_ENTER:
if (navigationwithkeys) {
// when the user has utilized arrow key navigation,
// interpret enter as 'click' on the active button
active = lastactive;
} else {
// else it is a shortcut for 'Ok'
active = 13;
}
processkey = true;
break;
case KEY_ESCAPE:
rc = false;
goto getout;
case BUTTON_LEFT:
navigationwithkeys = false;
for (int i = 0; buttons[i]; i++) {
if (_gfx->testButton(bx[i], by[i], buttons[i])) {
active = i;
processkey = true;
break;
}
}
break;
case KEY_BACKSPACE:
active = 11;
processkey = true;
break;
case '#':
active = 9;
processkey = true;
break;
case '*':
active = 14;
processkey = true;
break;
case 0x09: /* Tab */
navigationwithkeys = true;
debugC(3, kDebugLevelText, "Focus change");
lastactive = active = lastactive + 1;
active %= ARRAYSIZE(buttons) - 1;
needRefresh = true;
break;
case KEY_LEFT:
navigationwithkeys = true;
if (lastactive == 0 || lastactive == 3 || lastactive == 6)
active = lastactive + 2;
else if (lastactive == 9)
active = 15;
else if (lastactive == 11)
active = 11;
else if (lastactive == 12)
active = 13;
else if (lastactive == 14)
active = 10;
else
active = lastactive - 1;
lastactive = active;
needRefresh = true;
break;
case KEY_RIGHT:
navigationwithkeys = true;
if (lastactive == 2 || lastactive == 5 || lastactive == 8)
active = lastactive - 2;
else if (lastactive == 10)
active = 14;
else if (lastactive == 11)
active = 11;
else if (lastactive == 13)
active = 12;
else if (lastactive == 15)
active = 9;
else
active = lastactive + 1;
lastactive = active;
needRefresh = true;
break;
case KEY_UP:
navigationwithkeys = true;
if (lastactive <= 2)
active = 11;
else if (lastactive == 9 || lastactive == 10)
active = lastactive - 2;
else if (lastactive == 11)
active = 13;
else if (lastactive == 14)
active = 8;
else if (lastactive == 15)
active = 6;
else
active = lastactive - 3;
lastactive = active;
needRefresh = true;
break;
case KEY_DOWN:
navigationwithkeys = true;
if (lastactive == 6)
active = 15;
else if (lastactive == 7 || lastactive == 8)
active = lastactive + 2;
else if (lastactive == 11)
active = 0;
else if (lastactive == 12 || lastactive == 13)
active = 11;
else if (lastactive == 14 || lastactive == 15)
active = lastactive - 2;
else
active = lastactive + 3;
lastactive = active;
needRefresh = true;
break;
default:
// handle numeric buttons
if (key >= '1' && key <= '9') {
active = key - '1';
processkey = true;
} else if (key == '0') {
active = 15;
processkey = true;
}
break;
}
if (processkey) {
if (active >= 0) {
needRefresh = true;
lastactive = active;
if (active == 15 && mode != kModeNum) { // Space
// bring MRU word at the top of the list when changing words
if (mode == kModePre && _activeTreeNode && _activeTreeNode->words.size() > 1 && _wordNumber != 0) {
String tmpstr = _activeTreeNode->words.remove_at(_wordNumber);
_activeTreeNode->words.insert_at(0, tmpstr);
}
strncpy(temp, _currentWord.c_str(), _currentCode.size());
temp[_currentCode.size()] = 0;
prefix += temp;
prefix += " ";
_currentCode.clear();
_currentWord.clear();
memset(repeatcount, 0, MAXWORDLEN);
break;
} if (active < 9 || active == 11 || active == 15) { // number or backspace
if (active == 11) { // backspace
if (_currentCode.size()) {
repeatcount[_currentCode.size() - 1] = 0;
_currentCode.deleteLastChar();
} else {
if (prefix.size())
prefix.deleteLastChar();
}
} else if (prefix.size() + _currentCode.size() < MAXWORDLEN - 1) { // don't overflow the dialog line
if (active == 15) { // zero
_currentCode += buttonStr[9];
} else {
_currentCode += buttonStr[active];
}
}
switch (mode) {
case kModeNum:
_currentWord = _currentCode;
break;
case kModePre:
if (!matchWord() && _currentCode.size()) {
_currentCode.deleteLastChar();
matchWord();
}
break;
case kModeAbc:
for (x = 0; x < _currentCode.size(); x++)
if (_currentCode[x] >= '1')
temp[x] = buttons[_currentCode[x] - '1'][3 + repeatcount[x]];
temp[_currentCode.size()] = 0;
_currentWord = temp;
}
} else if (active == 9) { // next
if (mode != kModeAbc) {
int totalWordsNumber = _activeTreeNode ? _activeTreeNode->words.size() : 0;
if (totalWordsNumber > 0) {
_wordNumber = (_wordNumber + 1) % totalWordsNumber;
_currentWord = String(_activeTreeNode->words[_wordNumber].c_str(), _currentCode.size());
}
} else {
x = _currentCode.size();
if (x) {
if (_currentCode.lastChar() == '1' || _currentCode.lastChar() == '7' || _currentCode.lastChar() == '9')
repeatcount[x - 1] = (repeatcount[x - 1] + 1) % 4;
else
repeatcount[x - 1] = (repeatcount[x - 1] + 1) % 3;
if (_currentCode.lastChar() >= '1')
_currentWord[x - 1] = buttons[_currentCode[x - 1] - '1'][3 + repeatcount[x - 1]];
}
}
} else if (active == 10) { // add
debug(0, "add");
} else if (active == 13) { // Ok
// bring MRU word at the top of the list when ok'ed out of the dialog
if (mode == kModePre && _activeTreeNode && _activeTreeNode->words.size() > 1 && _wordNumber != 0) {
String tmpstr = _activeTreeNode->words.remove_at(_wordNumber);
_activeTreeNode->words.insert_at(0, tmpstr);
}
rc = true;
goto press;
} else if (active == 14) { // Mode
mode++;
if (mode > kModeAbc)
mode = kModePre;
// truncate current input at mode change
strncpy(temp, _currentWord.c_str(), _currentCode.size());
temp[_currentCode.size()] = 0;
prefix += temp;
_currentCode.clear();
_currentWord.clear();
memset(repeatcount, 0, MAXWORDLEN);
} else {
goto press;
}
}
}
}
press:
strncpy(_predictiveResult, prefix.c_str(), 40);
strncat(_predictiveResult, _currentWord.c_str(), 40);
_predictiveResult[prefix.size() + _currentCode.size() + 1] = 0;
getout:
// if another window was shown, bring it up again
if (!tmpwindow.active)
closeWindow();
else {
_gfx->restoreBlock(_game.window.x1, _game.window.y1,
_game.window.x2, _game.window.y2, _game.window.buffer);
free(_game.window.buffer);
memcpy(&(_game.window), &tmpwindow, sizeof(AgiBlock));
_gfx->doUpdate();
}
_system->setFeatureState(OSystem::kFeatureDisableKeyFiltering, false);
_predictiveDialogRunning = false;
return rc;
}
#define MAXLINELEN 80
void AgiEngine::loadDict(void) {
Common::File in;
char buf[MAXLINELEN];
int words = 0, lines = 0;
if (!in.open("pred.txt"))
return;
_searchTreeRoot = new SearchTree();
words = 0;
while (!in.eos() && in.readLine(buf, MAXLINELEN)) {
// Skip leading & trailing whitespaces
char *word = Common::trim(buf);
// Skip empty lines
if (*word == 0)
continue;
lines++;
// The lines are of the form: "1234 word word"
// I.e. first comes a T9 number, then a space separated list of
// words with that T9 code. We simply ignore the T9 code, and then
// insert the words in order of occurance.
char *tok = strtok(word, " \t");
if (tok) {
while ((tok = strtok(NULL, " ")) != NULL) {
insertSearchNode(tok);
words++;
}
}
}
debug(0, "Loaded %d lines with %d words", lines, words);
}
bool AgiEngine::matchWord(void) {
if (_currentCode.empty()) {
return false;
}
// Lookup word in the search tree
SearchTree *tree = _searchTreeRoot;
assert(tree);
for (uint i = 0; i < _currentCode.size(); ++i) {
int key = _currentCode[i] - '0';
if (key < 1 || key > 9) {
tree = 0;
break; // Invalid key/code value, abort!
}
tree = tree->children[key];
if (!tree)
break; // No matching entry in the search tree, abort!
}
if (tree)
tree = tree->findChildWithWords();
_wordNumber = 0;
_activeTreeNode = tree;
_currentWord.clear();
if (tree) {
_currentWord = String(_activeTreeNode->words[_wordNumber].c_str(), _currentCode.size());
}
return tree != 0;
}
} // End of namespace Agi
|