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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/* 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$
*
* CursorPositionProcess()
* TagProcess()
* PointProcess()
*/
#include "tinsel/actors.h"
#include "tinsel/background.h"
#include "tinsel/cursor.h"
#include "tinsel/dw.h"
#include "tinsel/events.h"
#include "tinsel/font.h"
#include "tinsel/graphics.h"
#include "tinsel/multiobj.h"
#include "tinsel/object.h"
#include "tinsel/pcode.h"
#include "tinsel/polygons.h"
#include "tinsel/rince.h"
#include "tinsel/sched.h"
#include "tinsel/strres.h"
#include "tinsel/text.h"
namespace Tinsel {
//----------------- EXTERNAL GLOBAL DATA --------------------
#ifdef DEBUG
//extern int Overrun; // The overrun counter, in DOS_DW.C
extern int newestString; // The overrun counter, in STRRES.C
#endif
//----------------- EXTERNAL FUNCTIONS ---------------------
// in BG.C
extern int BackgroundWidth(void);
extern int BackgroundHeight(void);
//----------------- LOCAL DEFINES --------------------
#define LPOSX 295 // X-co-ord of lead actor's position display
#define CPOSX 24 // X-co-ord of cursor's position display
#define OPOSX SCRN_CENTRE_X // X-co-ord of overrun counter's display
#define SPOSX SCRN_CENTRE_X // X-co-ord of string numbner's display
#define POSY 0 // Y-co-ord of these position displays
enum HotSpotTag {
NO_HOTSPOT_TAG,
POLY_HOTSPOT_TAG,
ACTOR_HOTSPOT_TAG
};
//----------------- LOCAL GLOBAL DATA --------------------
static bool DispPath = false;
static bool bShowString = false;
static int TaggedActor = 0;
static HPOLYGON hTaggedPolygon = NOPOLY;
static enum { TAGS_OFF, TAGS_ON } TagsActive = TAGS_ON;
#ifdef DEBUG
/**
* Displays the cursor and lead actor's co-ordinates and the overrun
* counter. Also which path polygon the cursor is in, if required.
*
* This process is only started up if a Glitter showpos() call is made.
* Obviously, this is for testing purposes only...
*/
void CursorPositionProcess(CORO_PARAM, const void *) {
// COROUTINE
CORO_BEGIN_CONTEXT;
int prevsX, prevsY; // Last screen top left
int prevcX, prevcY; // Last displayed cursor position
int prevlX, prevlY; // Last displayed lead actor position
// int prevOver; // Last displayed overrun
int prevString; // Last displayed string number
OBJECT *cpText; // cursor position text object pointer
OBJECT *cpathText; // cursor path text object pointer
OBJECT *rpText; // text object pointer
// OBJECT *opText; // text object pointer
OBJECT *spText; // string number text object pointer
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
_ctx->prevsX = -1;
_ctx->prevsY = -1;
_ctx->prevcX = -1;
_ctx->prevcY = -1;
_ctx->prevlX = -1;
_ctx->prevlY = -1;
// _ctx->prevOver = -1;
_ctx->prevString = -1;
_ctx->cpText = NULL;
_ctx->cpathText = NULL;
_ctx->rpText = NULL;
// _ctx->opText = NULL;
_ctx->spText = NULL;
int aniX, aniY; // cursor/lead actor position
int Loffset, Toffset; // Screen top left
char PositionString[64]; // sprintf() things into here
PMACTOR pActor; // Lead actor
while (1) {
PlayfieldGetPos(FIELD_WORLD, &Loffset, &Toffset);
/*-----------------------------------*\
| Cursor's position and path display. |
\*-----------------------------------*/
GetCursorXY(&aniX, &aniY, false);
// Change in cursor position?
if (aniX != _ctx->prevcX || aniY != _ctx->prevcY ||
Loffset != _ctx->prevsX || Toffset != _ctx->prevsY) {
// kill current text objects
if (_ctx->cpText) {
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), _ctx->cpText);
}
if (_ctx->cpathText) {
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), _ctx->cpathText);
_ctx->cpathText = NULL;
}
// New text objects
sprintf(PositionString, "%d %d", aniX + Loffset, aniY + Toffset);
_ctx->cpText = ObjectTextOut(GetPlayfieldList(FIELD_STATUS), PositionString,
0, CPOSX, POSY, hTagFontHandle(), TXT_CENTRE);
if (DispPath) {
HPOLYGON hp = InPolygon(aniX + Loffset, aniY + Toffset, PATH);
if (hp == NOPOLY)
sprintf(PositionString, "No path");
else
sprintf(PositionString, "%d,%d %d,%d %d,%d %d,%d",
PolyCornerX(hp, 0), PolyCornerY(hp, 0),
PolyCornerX(hp, 1), PolyCornerY(hp, 1),
PolyCornerX(hp, 2), PolyCornerY(hp, 2),
PolyCornerX(hp, 3), PolyCornerY(hp, 3));
_ctx->cpathText = ObjectTextOut(GetPlayfieldList(FIELD_STATUS), PositionString,
0, 4, POSY+ 10, hTagFontHandle(), 0);
}
// update previous position
_ctx->prevcX = aniX;
_ctx->prevcY = aniY;
}
#if 0
/*------------------------*\
| Overrun counter display. |
\*------------------------*/
if (Overrun != _ctx->prevOver) {
// kill current text objects
if (_ctx->opText) {
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), _ctx->opText);
}
sprintf(PositionString, "%d", Overrun);
_ctx->opText = ObjectTextOut(GetPlayfieldList(FIELD_STATUS), PositionString,
0, OPOSX, POSY, hTagFontHandle(), TXT_CENTRE);
// update previous value
_ctx->prevOver = Overrun;
}
#endif
/*----------------------*\
| Lead actor's position. |
\*----------------------*/
pActor = GetMover(LEAD_ACTOR);
if (pActor && pActor->MActorState == NORM_MACTOR) {
// get lead's animation position
GetActorPos(LEAD_ACTOR, &aniX, &aniY);
// Change in position?
if (aniX != _ctx->prevlX || aniY != _ctx->prevlY ||
Loffset != _ctx->prevsX || Toffset != _ctx->prevsY) {
// Kill current text objects
if (_ctx->rpText) {
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), _ctx->rpText);
}
// create new text object list
sprintf(PositionString, "%d %d", aniX, aniY);
_ctx->rpText = ObjectTextOut(GetPlayfieldList(FIELD_STATUS), PositionString,
0, LPOSX, POSY, hTagFontHandle(), TXT_CENTRE);
// update previous position
_ctx->prevlX = aniX;
_ctx->prevlY = aniY;
}
}
/*-------------*\
| String number |
\*-------------*/
if (bShowString && newestString != _ctx->prevString) {
// kill current text objects
if (_ctx->spText) {
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), _ctx->spText);
}
sprintf(PositionString, "String: %d", newestString);
_ctx->spText = ObjectTextOut(GetPlayfieldList(FIELD_STATUS), PositionString,
0, SPOSX, POSY+10, hTalkFontHandle(), TXT_CENTRE);
// update previous value
_ctx->prevString = newestString;
}
// update previous playfield position
_ctx->prevsX = Loffset;
_ctx->prevsY = Toffset;
CORO_SLEEP(1); // allow re-scheduling
}
CORO_END_CODE;
}
#endif
/**
* Tag process keeps us updated as to which tagged actor is currently tagged
* (if one is). Tag process asks us for this information, as does User_Event().
*/
static void SaveTaggedActor(int ano) {
TaggedActor = ano;
}
/**
* Tag process keeps us updated as to which tagged actor is currently tagged
* (if one is). Tag process asks us for this information, as does User_Event().
*/
int GetTaggedActor(void) {
return TaggedActor;
}
/**
* Tag process keeps us updated as to which polygon is currently tagged
* (if one is). Tag process asks us for this information, as does User_Event().
*/
static void SaveTaggedPoly(HPOLYGON hp) {
hTaggedPolygon = hp;
}
HPOLYGON GetTaggedPoly(void) {
return hTaggedPolygon;
}
/**
* Given cursor position and an actor number, ascertains whether the
* cursor is within the actor's tag area.
* Returns TRUE for a positive result, FALSE for negative.
* If TRUE, the mid-top co-ordinates of the actor's tag area are also
* returned.
*/
static bool InHotSpot(int ano, int aniX, int aniY, int *pxtext, int *pytext) {
int Top, Bot; // Top and bottom limits of active area
int left, right; // left and right of active area
int qrt = 0; // 1/4 of height (sometimes 1/2)
// First check if within x-range
if (aniX > (left = GetActorLeft(ano)) && aniX < (right = GetActorRight(ano))) {
Top = GetActorTop(ano);
Bot = GetActorBottom(ano);
// y-range varies according to tag-type
switch (TagType(ano)) {
case TAG_DEF:
// Next to bottom 1/4 of the actor's area
qrt = (Bot - Top) >> 1; // Half actor's height
Top += qrt; // Top = mid-height
qrt = qrt >> 1; // Quarter height
Bot -= qrt; // Bot = 1/4 way up
break;
case TAG_Q1TO3:
// Top 3/4 of the actor's area
qrt = (Bot - Top) >> 2; // 1/4 actor's height
Bot -= qrt; // Bot = 1/4 way up
break;
case TAG_Q1TO4:
// All the actor's area
break;
default:
error("illegal tag area type");
}
// Now check if within y-range
if (aniY >= Top && aniY <= Bot) {
if (TagType(ano) == TAG_Q1TO3)
*pytext = Top + qrt;
else
*pytext = Top;
*pxtext = (left + right) / 2;
return true;
}
}
return false;
}
/**
* See if the cursor is over a tagged actor's hot-spot. If so, display
* the tag or, if tag already displayed, maintain the tag's position on
* the screen.
*/
static bool ActorTag(int curX, int curY, HotSpotTag *pTag, OBJECT **ppText) {
static int Loffset = 0, Toffset = 0; // Values when tag was displayed
int nLoff, nToff; // new values, to keep tag in place
int ano;
int xtext, ytext;
bool newActor;
// For each actor with a tag....
FirstTaggedActor();
while ((ano = NextTaggedActor()) != 0) {
if (InHotSpot(ano, curX, curY, &xtext, &ytext)) {
// Put up or maintain actor tag
if (*pTag != ACTOR_HOTSPOT_TAG)
newActor = true;
else if (ano != GetTaggedActor())
newActor = true; // Different actor
else
newActor = false; // Same actor
if (newActor) {
// Display actor's tag
if (*ppText)
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), *ppText);
*pTag = ACTOR_HOTSPOT_TAG;
SaveTaggedActor(ano); // This actor tagged
SaveTaggedPoly(NOPOLY); // No tagged polygon
PlayfieldGetPos(FIELD_WORLD, &Loffset, &Toffset);
LoadStringRes(GetActorTag(ano), tBufferAddr(), TBUFSZ);
*ppText = ObjectTextOut(GetPlayfieldList(FIELD_STATUS), tBufferAddr(),
0, xtext - Loffset, ytext - Toffset, hTagFontHandle(), TXT_CENTRE);
assert(*ppText); // Actor tag string produced NULL text
MultiSetZPosition(*ppText, Z_TAG_TEXT);
} else {
// Maintain actor tag's position
PlayfieldGetPos(FIELD_WORLD, &nLoff, &nToff);
if (nLoff != Loffset || nToff != Toffset) {
MultiMoveRelXY(*ppText, Loffset - nLoff, Toffset - nToff);
Loffset = nLoff;
Toffset = nToff;
}
}
return true;
}
}
// No tagged actor
if (*pTag == ACTOR_HOTSPOT_TAG) {
*pTag = NO_HOTSPOT_TAG;
SaveTaggedActor(0);
}
return false;
}
/**
* Perhaps some comment in due course.
*
* Under control of PointProcess(), when the cursor is over a TAG or
* EXIT polygon, its pointState flag is set to POINTING. If its Glitter
* code contains a printtag() call, its tagState flag gets set to TAG_ON.
*/
static bool PolyTag(HotSpotTag *pTag, OBJECT **ppText) {
static int Loffset = 0, Toffset = 0; // Values when tag was displayed
int nLoff, nToff; // new values, to keep tag in place
HPOLYGON hp;
bool newPoly;
int shift;
int tagx, tagy; // Tag display co-ordinates
SCNHANDLE hTagtext; // Tag text
// For each polgon with a tag....
for (int i = 0; i < MAX_POLY; i++) {
hp = GetPolyHandle(i);
// Added code for un-tagged tags
if (hp != NOPOLY && PolyPointState(hp) == POINTING && PolyTagState(hp) != TAG_ON) {
// This poly is entitled to be tagged
if (hp != GetTaggedPoly()) {
if (*ppText) {
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), *ppText);
*ppText = NULL;
}
*pTag = POLY_HOTSPOT_TAG;
SaveTaggedActor(0); // No tagged actor
SaveTaggedPoly(hp); // This polygon tagged
}
return true;
} else if (hp != NOPOLY && PolyTagState(hp) == TAG_ON) {
// Put up or maintain polygon tag
if (*pTag != POLY_HOTSPOT_TAG)
newPoly = true; // A new polygon (no current)
else if (hp != GetTaggedPoly())
newPoly = true; // Different polygon
else
newPoly = false; // Same polygon
if (newPoly) {
if (*ppText)
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), *ppText);
*pTag = POLY_HOTSPOT_TAG;
SaveTaggedActor(0); // No tagged actor
SaveTaggedPoly(hp); // This polygon tagged
PlayfieldGetPos(FIELD_WORLD, &Loffset, &Toffset);
getPolyTagInfo(hp, &hTagtext, &tagx, &tagy);
int strLen;
if (PolyTagHandle(hp) != 0)
strLen = LoadStringRes(PolyTagHandle(hp), tBufferAddr(), TBUFSZ);
else
strLen = LoadStringRes(hTagtext, tBufferAddr(), TBUFSZ);
if (strLen == 0)
// No valid string returned, so leave ppText as NULL
ppText = NULL;
else {
// Handle displaying the tag text on-screen
*ppText = ObjectTextOut(GetPlayfieldList(FIELD_STATUS), tBufferAddr(),
0, tagx - Loffset, tagy - Toffset,
hTagFontHandle(), TXT_CENTRE);
assert(*ppText); // Polygon tag string produced NULL text
MultiSetZPosition(*ppText, Z_TAG_TEXT);
/*
* New feature: Don't go off the side of the background
*/
shift = MultiRightmost(*ppText) + Loffset + 2;
if (shift >= BackgroundWidth()) // Not off right
MultiMoveRelXY(*ppText, BackgroundWidth() - shift, 0);
shift = MultiLeftmost(*ppText) + Loffset - 1;
if (shift <= 0) // Not off left
MultiMoveRelXY(*ppText, -shift, 0);
shift = MultiLowest(*ppText) + Toffset;
if (shift > BackgroundHeight()) // Not off bottom
MultiMoveRelXY(*ppText, 0, BackgroundHeight() - shift);
}
} else {
PlayfieldGetPos(FIELD_WORLD, &nLoff, &nToff);
if (nLoff != Loffset || nToff != Toffset) {
MultiMoveRelXY(*ppText, Loffset - nLoff, Toffset - nToff);
Loffset = nLoff;
Toffset = nToff;
}
}
return true;
}
}
// No tagged polygon
if (*pTag == POLY_HOTSPOT_TAG) {
*pTag = NO_HOTSPOT_TAG;
SaveTaggedPoly(NOPOLY);
}
return false;
}
/**
* Handle display of tagged actor and polygon tags.
* Tagged actor's get priority over polygons.
*/
void TagProcess(CORO_PARAM, const void *) {
// COROUTINE
CORO_BEGIN_CONTEXT;
OBJECT *pText; // text object pointer
HotSpotTag Tag;
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
_ctx->pText = NULL;
_ctx->Tag = NO_HOTSPOT_TAG;
SaveTaggedActor(0); // No tagged actor yet
SaveTaggedPoly(NOPOLY); // No tagged polygon yet
while (1) {
if (TagsActive == TAGS_ON) {
int curX, curY; // cursor position
while (!GetCursorXYNoWait(&curX, &curY, true))
CORO_SLEEP(1);
if (!ActorTag(curX, curY, &_ctx->Tag, &_ctx->pText)
&& !PolyTag(&_ctx->Tag, &_ctx->pText)) {
// Nothing tagged. Remove tag, if there is one
if (_ctx->pText) {
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), _ctx->pText);
_ctx->pText = NULL;
}
}
} else {
SaveTaggedActor(0);
SaveTaggedPoly(NOPOLY);
// Remove tag, if there is one
if (_ctx->pText) {
// kill current text objects
MultiDeleteObject(GetPlayfieldList(FIELD_STATUS), _ctx->pText);
_ctx->pText = NULL;
_ctx->Tag = NO_HOTSPOT_TAG;
}
}
CORO_SLEEP(1); // allow re-scheduling
}
CORO_END_CODE;
}
/**
* Called from PointProcess() as appropriate.
*/
static void enteringpoly(HPOLYGON hp) {
SetPolyPointState(hp, POINTING);
RunPolyTinselCode(hp, POINTED, BE_NONE, false);
}
/**
* Called from PointProcess() as appropriate.
*/
static void leavingpoly(HPOLYGON hp) {
SetPolyPointState(hp, NOT_POINTING);
if (PolyTagState(hp) == TAG_ON) {
// Delete this tag entry
SetPolyTagState(hp, TAG_OFF);
}
}
/**
* For TAG and EXIT polygons, monitor cursor entering and leaving.
* Maintain the polygons' pointState and tagState flags accordingly.
* Also run the polygon's Glitter code when the cursor enters.
*/
void PointProcess(CORO_PARAM, const void *) {
// COROUTINE
CORO_BEGIN_CONTEXT;
CORO_END_CONTEXT(_ctx);
CORO_BEGIN_CODE(_ctx);
while (1) {
int aniX, aniY; // cursor/tagged actor position
while (!GetCursorXYNoWait(&aniX, &aniY, true))
CORO_SLEEP(1);
/*----------------------------------*\
| For polygons of type TAG and EXIT. |
\*----------------------------------*/
for (int i = 0; i < MAX_POLY; i++) {
HPOLYGON hp = GetPolyHandle(i);
if (hp != NOPOLY && (PolyType(hp) == TAG || PolyType(hp) == EXIT)) {
if (PolyPointState(hp) == NOT_POINTING) {
if (IsInPolygon(aniX, aniY, hp)) {
enteringpoly(hp);
}
} else if (PolyPointState(hp) == POINTING) {
if (!IsInPolygon(aniX, aniY, hp)) {
leavingpoly(hp);
}
}
}
}
// allow re-scheduling
CORO_SLEEP(1);
}
CORO_END_CODE;
}
void DisableTags(void) {
TagsActive = TAGS_OFF;
}
void EnableTags(void) {
TagsActive = TAGS_ON;
}
bool DisableTagsIfEnabled(void) {
if (TagsActive == TAGS_OFF)
return false;
else {
TagsActive = TAGS_OFF;
return true;
}
}
/**
* For testing purposes only.
* Causes CursorPositionProcess() to display, or not, the path that the
* cursor is in.
*/
void TogglePathDisplay(void) {
DispPath ^= 1; // Toggle path display (XOR with true)
}
void setshowstring(void) {
bShowString = true;
}
} // end of namespace Tinsel
|