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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
/* 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 "common/util.h"
#include "common/stack.h"
#include "graphics/primitives.h"
#include "sci/sci.h"
#include "sci/engine/state.h"
#include "sci/gfx/operations.h"
#include "sci/gfx/gfx_state_internal.h"
#include "sci/gui/gui_helpers.h"
#include "sci/gui/gui_gfx.h"
#include "sci/gui/gui_cursor.h"
#include "sci/gui/gui_font.h"
#include "sci/gui/gui_text.h"
#include "sci/gui/gui_screen.h"
#include "sci/gui/gui_menu.h"
namespace Sci {
SciGuiMenu::SciGuiMenu(SegManager *segMan, SciGuiGfx *gfx, SciGuiText *text, SciGuiScreen *screen, SciGuiCursor *cursor)
: _segMan(segMan), _gfx(gfx), _text(text), _screen(screen), _cursor(cursor) {
_listCount = 0;
// We actually set active item in here and remember last selection of the user
// sierra sci always defaulted to first item every time menu was called via ESC, we dont follow that logic
_curMenuId = 1;
_curItemId = 1;
_menuSaveHandle = NULL_REG;
_oldPort = NULL;
}
SciGuiMenu::~SciGuiMenu() {
}
void SciGuiMenu::init(GfxState *gfxstate) {
_gfxstate = gfxstate;
}
void SciGuiMenu::add(Common::String title, Common::String content, reg_t contentVmPtr) {
GuiMenuEntry *menuEntry;
uint16 itemCount = 0;
GuiMenuItemEntry *itemEntry;
int contentSize = content.size();
int separatorCount;
int curPos, beginPos, endPos, tempPos;
int tagPos, rightAlignedPos, functionPos, altPos, controlPos;
// Sierra SCI starts with id 1, so we do so as well
_listCount++;
menuEntry = new GuiMenuEntry(_listCount);
menuEntry->text = title;
_list.push_back(menuEntry);
curPos = 0;
do {
itemCount++;
itemEntry = new GuiMenuItemEntry(_listCount, itemCount);
beginPos = curPos;
// Now go through the content till we find end-marker and collect data about it
// ':' is an end-marker for each item
tagPos = 0; rightAlignedPos = 0;
controlPos = 0; altPos = 0; functionPos = 0;
while ((curPos < contentSize) && (content[curPos] != ':')) {
switch (content[curPos]) {
case '=': // Set tag
if (tagPos)
error("multiple tag markers within one menu-item");
tagPos = curPos;
break;
case '`': // Right-aligned
if (rightAlignedPos)
error("multiple right-aligned markers within one menu-item");
rightAlignedPos = curPos;
break;
case '^': // Ctrl-prefix
if (controlPos)
error("multiple control markers within one menu-item");
controlPos = curPos;
break;
case '@': // Alt-prefix
if (altPos)
error("multiple alt markers within one menu-item");
altPos = curPos;
break;
case '#': // Function-prefix
if (functionPos)
error("multiple function markers within one menu-item");
functionPos = curPos;
break;
}
curPos++;
}
endPos = curPos;
// Control/Alt/Function key mapping...
if (controlPos) {
content.setChar(SCI_MENU_REPLACE_ONCONTROL, controlPos);
itemEntry->keyModifier = SCI_EVM_CTRL;
tempPos = controlPos + 1;
if (tempPos >= contentSize)
error("control marker at end of item");
itemEntry->keyPress = tolower(content[tempPos]);
content.setChar(toupper(content[tempPos]), tempPos);
}
if (altPos) {
content.setChar(SCI_MENU_REPLACE_ONALT, altPos);
itemEntry->keyModifier = SCI_EVM_ALT;
tempPos = altPos + 1;
if (tempPos >= contentSize)
error("alt marker at end of item");
itemEntry->keyPress = tolower(content[tempPos]);
content.setChar(toupper(content[tempPos]), tempPos);
}
if (functionPos) {
content.setChar(SCI_MENU_REPLACE_ONFUNCTION, functionPos);
tempPos = functionPos + 1;
if (tempPos >= contentSize)
error("function marker at end of item");
itemEntry->keyPress = content[tempPos];
switch (content[functionPos + 1]) {
case '1': itemEntry->keyPress = SCI_K_F1; break;
case '2': itemEntry->keyPress = SCI_K_F2; break;
case '3': itemEntry->keyPress = SCI_K_F3; break;
case '4': itemEntry->keyPress = SCI_K_F4; break;
case '5': itemEntry->keyPress = SCI_K_F5; break;
case '6': itemEntry->keyPress = SCI_K_F6; break;
case '7': itemEntry->keyPress = SCI_K_F7; break;
case '8': itemEntry->keyPress = SCI_K_F8; break;
case '9': itemEntry->keyPress = SCI_K_F9; break;
case '0': itemEntry->keyPress = SCI_K_F10; break;
default:
error("illegal function key specified");
}
}
// Now get all strings
tempPos = endPos;
if (rightAlignedPos) {
tempPos = rightAlignedPos;
} else if (tagPos) {
tempPos = tagPos;
}
curPos = beginPos;
separatorCount = 0;
while (curPos < tempPos) {
switch (content[curPos]) {
case '!':
case '-':
case ' ':
separatorCount++;
}
curPos++;
}
if (separatorCount == tempPos - beginPos) {
itemEntry->separatorLine = true;
} else {
itemEntry->text = Common::String(content.c_str() + beginPos, tempPos - beginPos);
}
itemEntry->textVmPtr = contentVmPtr;
itemEntry->textVmPtr.offset += beginPos;
if (rightAlignedPos) {
rightAlignedPos++;
tempPos = endPos;
if (tagPos)
tempPos = tagPos;
itemEntry->textRightAligned = Common::String(content.c_str() + rightAlignedPos, tempPos - rightAlignedPos);
}
if (tagPos) {
tempPos = functionPos + 1;
if (tempPos >= contentSize)
error("tag marker at end of item");
itemEntry->tag = atoi(content.c_str() + tempPos);
}
curPos = endPos + 1;
_itemList.push_back(itemEntry);
} while (curPos < contentSize);
}
GuiMenuItemEntry *SciGuiMenu::findItem(uint16 menuId, uint16 itemId) {
GuiMenuItemList::iterator listIterator;
GuiMenuItemList::iterator listEnd = _itemList.end();
GuiMenuItemEntry *listEntry;
listIterator = _itemList.begin();
while (listIterator != listEnd) {
listEntry = *listIterator;
if ((listEntry->menuId == menuId) && (listEntry->id == itemId))
return listEntry;
listIterator++;
}
return NULL;
}
void SciGuiMenu::setAttribute(uint16 menuId, uint16 itemId, uint16 attributeId, reg_t value) {
GuiMenuItemEntry *itemEntry = findItem(menuId, itemId);
if (!itemEntry)
error("Tried to setAttribute() on non-existant menu-item %d:%d", menuId, itemId);
switch (attributeId) {
case SCI_MENU_ATTRIBUTE_ENABLED:
itemEntry->enabled = value.isNull() ? false : true;
break;
case SCI_MENU_ATTRIBUTE_SAID:
itemEntry->said = _segMan->getString(value);
itemEntry->saidVmPtr = value;
break;
case SCI_MENU_ATTRIBUTE_TEXT:
itemEntry->text = _segMan->getString(value);
itemEntry->textVmPtr = value;
// We assume here that no script ever creates a separatorLine dynamically
break;
case SCI_MENU_ATTRIBUTE_KEYPRESS:
itemEntry->keyPress = tolower(value.offset);
itemEntry->keyModifier = 0;
// TODO: Find out how modifier is handled
printf("setAttr keypress %X %X\n", value.segment, value.offset);
break;
case SCI_MENU_ATTRIBUTE_TAG:
itemEntry->tag = value.offset;
break;
default:
error("setAttribute() called with unsupported attributeId %X", attributeId);
}
}
reg_t SciGuiMenu::getAttribute(uint16 menuId, uint16 itemId, uint16 attributeId) {
GuiMenuItemEntry *itemEntry = findItem(menuId, itemId);
if (!itemEntry)
error("Tried to getAttribute() on non-existant menu-item %d:%d", menuId, itemId);
switch (attributeId) {
case SCI_MENU_ATTRIBUTE_ENABLED:
if (itemEntry->enabled)
return make_reg(0, 1);
break;
case SCI_MENU_ATTRIBUTE_SAID:
return itemEntry->saidVmPtr;
case SCI_MENU_ATTRIBUTE_TEXT:
return itemEntry->textVmPtr;
case SCI_MENU_ATTRIBUTE_KEYPRESS:
// TODO: Find out how modifier is handled
return make_reg(0, itemEntry->keyPress);
case SCI_MENU_ATTRIBUTE_TAG:
return make_reg(0, itemEntry->tag);
default:
error("getAttribute() called with unsupported attributeId %X", attributeId);
}
return NULL_REG;
}
void SciGuiMenu::drawBar() {
GuiMenuEntry *listEntry;
GuiMenuList::iterator listIterator;
GuiMenuList::iterator listEnd = _list.end();
// Hardcoded black on white
_gfx->FillRect(_gfx->_menuBarRect, 1, _screen->_colorWhite);
_gfx->PenColor(0);
_gfx->MoveTo(8, 1);
listIterator = _list.begin();
while (listIterator != listEnd) {
listEntry = *listIterator;
_text->Draw_String(listEntry->text.c_str());
listIterator++;
}
}
// This helper calculates all text widths for all menus/items
void SciGuiMenu::calculateTextWidth() {
GuiMenuList::iterator menuIterator;
GuiMenuList::iterator menuEnd = _list.end();
GuiMenuEntry *menuEntry;
GuiMenuItemList::iterator itemIterator;
GuiMenuItemList::iterator itemEnd = _itemList.end();
GuiMenuItemEntry *itemEntry;
int16 dummyHeight;
menuIterator = _list.begin();
while (menuIterator != menuEnd) {
menuEntry = *menuIterator;
_text->StringWidth(menuEntry->text.c_str(), 0, menuEntry->textWidth, dummyHeight);
menuIterator++;
}
itemIterator = _itemList.begin();
while (itemIterator != itemEnd) {
itemEntry = *itemIterator;
_text->StringWidth(itemEntry->text.c_str(), 0, itemEntry->textWidth, dummyHeight);
_text->StringWidth(itemEntry->textRightAligned.c_str(), 0, itemEntry->textRightAlignedWidth, dummyHeight);
itemIterator++;
}
}
reg_t SciGuiMenu::select(reg_t eventObject) {
int16 eventType = GET_SEL32V(_segMan, eventObject, type);
int16 keyPress, keyModifier;
Common::Point mousePosition;
GuiMenuItemList::iterator itemIterator = _itemList.begin();
GuiMenuItemList::iterator itemEnd = _itemList.end();
GuiMenuItemEntry *itemEntry = NULL;
bool forceClaimed = false;
switch (eventType) {
case SCI_EVT_KEYBOARD:
keyPress = GET_SEL32V(_segMan, eventObject, message);
keyModifier = GET_SEL32V(_segMan, eventObject, modifiers);
switch (keyPress) {
case 0:
break;
case SCI_K_ESC:
itemEntry = interactiveWithKeyboard();
forceClaimed = true;
break;
default:
while (itemIterator != itemEnd) {
itemEntry = *itemIterator;
if ((itemEntry->keyPress == keyPress) && (itemEntry->keyModifier == keyModifier))
break;
itemIterator++;
}
if (itemIterator == itemEnd)
itemEntry = NULL;
}
break;
case SCI_EVT_SAID:
while (itemIterator != itemEnd) {
itemEntry = *itemIterator;
// TODO: said comparsion
// said(_s, itemEntry->said.c_str(), 0) != SAID_NO_MATCH
// Where is this used anyway, so we can test it out?
itemIterator++;
}
if (itemIterator == itemEnd)
itemEntry = NULL;
break;
case SCI_EVT_MOUSE_PRESS:
mousePosition = _cursor->getPosition();
if (mousePosition.y < 10) {
itemEntry = interactiveWithMouse();
forceClaimed = true;
}
break;
}
if (!_menuSaveHandle.isNull()) {
_gfx->BitsRestore(_menuSaveHandle);
_gfx->BitsShow(_menuRect);
}
if (!_barSaveHandle.isNull()) {
_gfx->BitsRestore(_barSaveHandle);
_gfx->BitsShow(_gfx->_menuBarRect);
}
if (_oldPort)
_gfx->SetPort(_oldPort);
if ((itemEntry) || (forceClaimed))
PUT_SEL32(_segMan, eventObject, claimed, make_reg(0, 1));
if (itemEntry)
return make_reg(0, (itemEntry->menuId << 8) | (itemEntry->id));
return NULL_REG;
}
GuiMenuItemEntry *SciGuiMenu::interactiveGetItem(uint16 menuId, uint16 itemId, bool menuChanged) {
GuiMenuItemList::iterator itemIterator = _itemList.begin();
GuiMenuItemList::iterator itemEnd = _itemList.end();
GuiMenuItemEntry *itemEntry;
GuiMenuItemEntry *firstItemEntry = NULL;
GuiMenuItemEntry *lastItemEntry = NULL;
// Fixup menuId if needed
if (menuId > _listCount)
menuId = 1;
if (menuId == 0)
menuId = _listCount;
while (itemIterator != itemEnd) {
itemEntry = *itemIterator;
if (itemEntry->menuId == menuId) {
if (itemEntry->id == itemId)
return itemEntry;
if (!firstItemEntry)
firstItemEntry = itemEntry;
if ((!lastItemEntry) || (itemEntry->id > lastItemEntry->id))
lastItemEntry = itemEntry;
}
itemIterator++;
}
if ((itemId == 0) || (menuChanged))
return lastItemEntry;
return firstItemEntry;
}
void SciGuiMenu::drawMenu(uint16 oldMenuId, uint16 newMenuId) {
GuiMenuEntry *listEntry;
GuiMenuList::iterator listIterator;
GuiMenuList::iterator listEnd = _list.end();
GuiMenuItemEntry *listItemEntry;
GuiMenuItemList::iterator listItemIterator;
GuiMenuItemList::iterator listItemEnd = _itemList.end();
Common::Rect menuTextRect;
uint16 listNr = 0;
int16 maxTextWidth = 0, maxTextRightAlignedWidth = 0;
int16 topPos;
Common::Point pixelPos;
// Remove menu, if one is displayed
if (!_menuSaveHandle.isNull()) {
_gfx->BitsRestore(_menuSaveHandle);
_gfx->BitsShow(_menuRect);
}
// First calculate rect of menu and also invert old and new menu text
_menuRect.top = _gfx->_menuBarRect.bottom;
menuTextRect.top = _gfx->_menuBarRect.top;
menuTextRect.bottom = _gfx->_menuBarRect.bottom;
menuTextRect.left = menuTextRect.right = 7;
listIterator = _list.begin();
while (listIterator != listEnd) {
listEntry = *listIterator;
listNr++;
menuTextRect.left = menuTextRect.right;
menuTextRect.right += listEntry->textWidth;
if (listNr == newMenuId)
_menuRect.left = menuTextRect.left;
if ((listNr == newMenuId) || (listNr == oldMenuId)) {
menuTextRect.translate(1, 0);
_gfx->InvertRect(menuTextRect);
menuTextRect.translate(-1, 0);
}
listIterator++;
}
if (oldMenuId != 0)
_gfx->BitsShow(_gfx->_menuBarRect);
_menuRect.bottom = _menuRect.top + 2;
listItemIterator = _itemList.begin();
while (listItemIterator != listItemEnd) {
listItemEntry = *listItemIterator;
if (listItemEntry->menuId == newMenuId) {
_menuRect.bottom += _gfx->_curPort->fontHeight;
maxTextWidth = MAX<int16>(maxTextWidth, listItemEntry->textWidth);
maxTextRightAlignedWidth = MAX<int16>(maxTextRightAlignedWidth, listItemEntry->textRightAlignedWidth);
}
listItemIterator++;
}
_menuRect.right = _menuRect.left + 16 + 4 + 2;
_menuRect.right += maxTextWidth + maxTextRightAlignedWidth;
// Save background
_menuSaveHandle = _gfx->BitsSave(_menuRect, SCI_SCREEN_MASK_VISUAL);
// Do the drawing
_gfx->FillRect(_menuRect, SCI_SCREEN_MASK_VISUAL, 0);
_menuRect.left++; _menuRect.right--; _menuRect.bottom--;
_gfx->FillRect(_menuRect, SCI_SCREEN_MASK_VISUAL, _screen->_colorWhite);
_menuRect.left += 8;
topPos = _menuRect.top + 1;
listItemIterator = _itemList.begin();
while (listItemIterator != listItemEnd) {
listItemEntry = *listItemIterator;
if (listItemEntry->menuId == newMenuId) {
if (!listItemEntry->separatorLine) {
_gfx->MoveTo(_menuRect.left, topPos);
_text->Draw_String(listItemEntry->text.c_str());
_gfx->MoveTo(_menuRect.right - listItemEntry->textRightAlignedWidth - 5, topPos);
_text->Draw_String(listItemEntry->textRightAligned.c_str());
} else {
// We dont 100% follow sierra here, we draw the line from left to right. Looks better
pixelPos.y = topPos + (_gfx->_curPort->fontHeight >> 1) - 1;
pixelPos.x = _menuRect.left - 7;
while (pixelPos.x < (_menuRect.right - 1)) {
_screen->putPixel(pixelPos.x, pixelPos.y, SCI_SCREEN_MASK_VISUAL, 0, 0, 0);
pixelPos.x += 2;
}
}
topPos += _gfx->_curPort->fontHeight;
}
listItemIterator++;
}
_menuRect.left -= 8;
_menuRect.left--; _menuRect.right++; _menuRect.bottom++;
_gfx->BitsShow(_menuRect);
}
void SciGuiMenu::invertMenuSelection(uint16 itemId) {
Common::Rect itemRect = _menuRect;
itemRect.top += (itemId - 1) * _gfx->_curPort->fontHeight;
itemRect.bottom = itemRect.top + _gfx->_curPort->fontHeight + 1;
itemRect.left++; itemRect.right--;
_gfx->InvertRect(itemRect);
_gfx->BitsShow(itemRect);
}
GuiMenuItemEntry *SciGuiMenu::interactiveWithKeyboard() {
sci_event_t curEvent;
uint16 newMenuId = _curMenuId;
uint16 newItemId = _curItemId;
GuiMenuItemEntry *curItemEntry = findItem(_curMenuId, _curItemId);
GuiMenuItemEntry *newItemEntry = curItemEntry;
calculateTextWidth();
_oldPort = _gfx->SetPort(_gfx->_menuPort);
_barSaveHandle = _gfx->BitsSave(_gfx->_menuBarRect, SCI_SCREEN_MASK_VISUAL);
_gfx->PenColor(0);
_gfx->BackColor(_screen->_colorWhite);
drawBar();
drawMenu(0, curItemEntry->menuId);
invertMenuSelection(curItemEntry->id);
_gfx->BitsShow(_gfx->_menuBarRect);
_gfx->BitsShow(_menuRect);
while (true) {
curEvent = gfxop_get_event(_gfxstate, SCI_EVT_ANY);
switch (curEvent.type) {
case SCI_EVT_KEYBOARD:
do {
switch (curEvent.data) {
case SCI_K_ESC:
_curMenuId = curItemEntry->menuId; _curItemId = curItemEntry->id;
return NULL;
case SCI_K_ENTER:
_curMenuId = curItemEntry->menuId; _curItemId = curItemEntry->id;
return curItemEntry;
case SCI_K_LEFT:
newMenuId--;
break;
case SCI_K_RIGHT:
newMenuId++;
break;
case SCI_K_UP:
newItemId--;
break;
case SCI_K_DOWN:
newItemId++;
break;
}
if ((newMenuId != curItemEntry->menuId) || (newItemId != curItemEntry->id)) {
// Selection changed, fix up new selection if required
newItemEntry = interactiveGetItem(newMenuId, newItemId, newMenuId != curItemEntry->menuId);
newMenuId = newItemEntry->menuId; newItemId = newItemEntry->id;
}
} while (newItemEntry->separatorLine);
if ((newMenuId != curItemEntry->menuId) || (newItemId != curItemEntry->id)) {
// paint old and new
if (newMenuId != curItemEntry->menuId) {
// Menu changed, remove cur menu and paint new menu
drawMenu(curItemEntry->menuId, newMenuId);
} else {
invertMenuSelection(curItemEntry->id);
}
invertMenuSelection(newItemId);
curItemEntry = newItemEntry;
}
break;
case SCI_EVT_NONE:
gfxop_sleep(_gfxstate, 2500 / 1000);
break;
}
}
}
GuiMenuItemEntry *SciGuiMenu::interactiveWithMouse() {
calculateTextWidth();
return NULL;
}
} // End of namespace Sci
|