aboutsummaryrefslogtreecommitdiff
path: root/engines/mohawk/livingbooks.h
blob: 4649b9f5c9aae20846fc7c484521dfad79e21986 (plain)
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* 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.
 *
 */

#ifndef MOHAWK_LIVINGBOOKS_H
#define MOHAWK_LIVINGBOOKS_H

#include "mohawk/mohawk.h"
#include "mohawk/console.h"
#include "mohawk/livingbooks_graphics.h"
#include "mohawk/sound.h"
#include "mohawk/video.h"

#include "common/ini-file.h"
#include "common/rect.h"
#include "common/queue.h"
#include "common/random.h"

#include "livingbooks_code.h"

namespace Common {
	class SeekableSubReadStreamEndian;
	class MemoryReadStreamEndian;
}

namespace Mohawk {

#define LBKEY_MOD_CTRL 1
#define LBKEY_MOD_ALT 2
#define LBKEY_MOD_SHIFT 4

struct LBKey {
	byte code;
	byte modifiers;
	byte char_;
	byte repeats;
};

enum NodeState {
	kLBNodeDone = 0,
	kLBNodeRunning = 1,
	kLBNodeWaiting = 2
};

enum LBMode {
	kLBIntroMode = 1,
	kLBControlMode = 2,
	kLBCreditsMode = 3,
	kLBPreviewMode = 4,
	kLBReadMode = 5,
	kLBPlayMode = 6
};

enum {
	kLBPhaseInit = 0x0,
	kLBPhaseIntro = 0x1,
	kLBPhaseMain = 0x2,
	kLBPhaseNone = 0x7fff,
	kLBPhaseLoad = 0xfffe,
	kLBPhaseCreate = 0xffff
};

// automatic modes used in _timingMode
enum {
	kLBAutoNone = 0,
	kLBAutoIntro = 1,
	kLBAutoUserIdle = 2,
	kLBAutoMain = 3,
	kLBAutoSync = 4,
	kLBAutoInit = 5,
	kLBAutoCreate = 6,
	kLBAutoLoad = 7
};

// control modes used in _controlMode
enum {
	kLBControlNone = 0,
	kLBControlHideMouse = 1,
	kLBControlPauseItems = 2
};

enum {
	kLBStaticTextItem = 0x1,
	kLBPictureItem = 0x2,
	kLBEditTextItem = 0x14,
	kLBLiveTextItem = 0x15,
	kLBAnimationItem = 0x40,
	kLBSoundItem = 0x41,
	kLBGroupItem = 0x42,
	kLBMovieItem = 0x43,
	kLBPaletteAItem = 0x44, // unused?
	kLBPaletteItem = 0x45,
	kLBProxyItem = 0x46,
	kLBMiniGameItem = 666, // EVIL!!!!
	kLBXDataFileItem = 0x3e9,
	kLBDiscDectectorItem = 0xfa1
};

enum {
	// no 0x1?
	kLBAnimOpNotify = 0x2,
	kLBAnimOpSetTempo = 0x3,
	// no 0x4?
	kLBAnimOpMoveTo = 0x5,
	kLBAnimOpWait = 0x6,
	kLBAnimOpSetCel = 0x7,
	kLBAnimOpSleepUntil = 0x8,
	kLBAnimOpDrawMode = 0x9,
	kLBAnimOpPlaySound = 0xa,
	kLBAnimOpWaitForSound = 0xb,
	kLBAnimOpReleaseSound = 0xc,
	kLBAnimOpResetSound = 0xd,
	kLBAnimOpSetTempoDiv = 0xe,
	kLBAnimOpDelay = 0xf
};

enum {
	kLBEventPhaseInit = 0,
	kLBEventPhaseIntro = 1,
	kLBEventMouseDown = 2,
	kLBEventStarted = 3,
	kLBEventDone = 4,
	kLBEventMouseUp = 5,
	kLBEventPhaseMain = 6,
	kLBEventNotified = 7,
	kLBEventDragStart = 8,
	kLBEventDragMove = 9,
	kLBEventDragEnd = 0xa,
	kLBEventRolloverBegin = 0xb,
	kLBEventRolloverMove = 0xc,
	kLBEventRolloverEnd = 0xd,
	kLBEventMouseUpIn = 0xe,
	kLBEventMouseUpOut = 0xf,
	kLBEventMouseTrackIn = 0x10,
	kLBEventMouseTrackMove = 0x11,
	kLBEventMouseTrackMoveIn = 0x12,
	kLBEventMouseTrackMoveOut = 0x13,
	kLBEventMouseTrackOut = 0x14,
	kLBEventFocusBegin = 0x15,
	kLBEventFocusEnd = 0x16,
	kLBEventInit = 0x17,
	kLBEventLoad = 0x1a,
	kLBEventListLoad = 0x1b,
	kLBEventPhaseCreate = 0xff
};

enum {
	kLBGroupData = 0x64,
	kLBLiveTextData = 0x65,
	kLBMsgListScript = 0x66,
	kLBNotifyScript = 0x67,
	kLBSetPlayInfo = 0x68,
	kLBSetRandomLoc = 0x69,  // unused?
	kLBSetDrag = 0x6a,       // unused?
	kLBSetDrawMode = 0x6b,
	kLBSetFont = 0x6c,       // unused?
	kLBSetOneShot = 0x6d,    // unused?
	kLBSetPlayPhase = 0x6e,
	// from here, 2.x+
	kLBSetKeyNotify = 0x6f,
	kLBCommand = 0x70,
	kLBPaletteAData = 0x71,  // unused?
	kLBPaletteXData = 0x72,
	kLBDisable = 0x73,       // unused?
	kLBEnable = 0x74,        // unused?
	kLBSetNotVisible = 0x75,
	kLBSetVisible = 0x76,    // unused?
	kLBGlobalDisable = 0x77,
	kLBGlobalEnable = 0x78,  // unused?
	kLBGlobalSetNotVisible = 0x79,
	kLBGlobalSetVisible = 0x7a, // unused?
	kLBSetAmbient = 0x7b,
	kLBSetDragParams = 0x7c,
	kLBSetKeyEvent = 0x7d,
	kLBSetRolloverData = 0x7e,
	kLBSetParent = 0x7f,
	kLBSetHitTest = 0x80,
	// from here, rugrats
	kLBUnknown194 = 0x194
};

enum {
	kLBOpNone = 0x0,
	kLBOpXShow = 0x1,
	kLBOpTogglePlay = 0x2,
	kLBOpSetNotVisible = 0x3,
	kLBOpSetVisible = 0x4,
	kLBOpDestroy = 0x5,
	kLBOpRewind = 0x6,
	kLBOpStop = 0x7,
	kLBOpDisable = 0x8,
	kLBOpEnable = 0x9,
	// (no 0xa)
	kLBOpGlobalSetNotVisible = 0xb,
	kLBOpGlobalSetVisible = 0xc,
	kLBOpGlobalDisable = 0xd,
	kLBOpGlobalEnable = 0xe,
	kLBOpSeekToEnd = 0xf,
	// (no 0x10)
	kLBOpMute = 0x11,
	kLBOpUnmute = 0x12,
	kLBOpLoad = 0x13,
	kLBOpPreload = 0x14,
	kLBOpUnload = 0x15,
	kLBOpSeekToNext = 0x16,
	kLBOpSeekToPrev = 0x17,
	kLBOpDragBegin = 0x18,
	kLBOpDragEnd = 0x19,
	kLBOpScriptDisable = 0x1a,
	kLBOpScriptEnable = 0x1b,
	kLBOpUnknown1C = 0x1c,
	kLBOpSendExpression = 0x1d,
	kLBOpJumpUnlessExpression = 0xfffb,
	kLBOpBreakExpression = 0xfffc,
	kLBOpJumpToExpression = 0xfffd,
	kLBOpRunSubentries = 0xfffe,
	kLBOpRunData = 0xffff
};

enum {
	kLBNotifyGUIAction = 1,
	kLBNotifyGoToControls = 2,
	kLBNotifyChangePage = 3,
	kLBNotifyGotoQuit = 4,
	kLBNotifyIntroDone = 5,
	kLBNotifyChangeMode = 6,
	kLBNotifyCursorChange = 7,
	kLBNotifyPrintPage = 0xc,
	kLBNotifyQuit = 0xd
};

enum {
	kTargetTypeExpression = 0x3f3f,
	kTargetTypeCode = 0xfffe,
	kTargetTypeName = 0xffff
};

class MohawkEngine_LivingBooks;
class LBPage;
class LBGraphics;
class LBAnimation;

struct LBScriptEntry {
	LBScriptEntry();
	~LBScriptEntry();

	uint16 state;

	uint16 type;
	uint16 event;
	uint16 opcode;
	uint16 param;

	uint16 argc;
	uint16 *argvParam;
	uint16 *argvTarget;

	uint16 targetingType;
	Common::Array<Common::String> targets;

	// kLBNotifyChangeMode
	uint16 newUnknown;
	uint16 newMode;
	uint16 newPage;
	uint16 newSubpage;
	Common::String newCursor;

	// kLBEventNotified
	uint16 matchFrom;
	uint16 matchNotify;

	// kLBOpSendExpression
	uint32 offset;
	// kLBOpJumpUnlessExpression
	uint16 target;

	uint16 dataType;
	uint16 dataLen;
	byte *data;

	Common::Array<Common::String> conditions;
	Common::Array<LBScriptEntry *> subentries;
};

struct LBAnimScriptEntry {
	byte opcode;
	byte size;
	byte *data;
};

class LBAnimationNode {
public:
	LBAnimationNode(MohawkEngine_LivingBooks *vm, LBAnimation *parent, uint16 scriptResourceId);
	~LBAnimationNode();

	void draw(const Common::Rect &_bounds);
	void reset();
	NodeState update(bool seeking = false);
	bool transparentAt(int x, int y);

protected:
	MohawkEngine_LivingBooks *_vm;
	LBAnimation *_parent;

	void loadScript(uint16 resourceId);
	uint _currentEntry;
	Common::Array<LBAnimScriptEntry> _scriptEntries;

	uint _currentCel;
	int16 _xPos, _yPos;
	uint32 _delay;
};

class LBAnimationItem;

class LBAnimation {
public:
	LBAnimation(MohawkEngine_LivingBooks *vm, LBAnimationItem *parent, uint16 resourceId);
	~LBAnimation();

	void draw();
	bool update();

	void start();
	void seek(uint16 pos);
	void seekToTime(uint32 time);
	void stop();

	void playSound(uint16 resourceId);
	bool soundPlaying(uint16 resourceId, const Common::String &cue);

	bool transparentAt(int x, int y);

	void setTempo(uint16 tempo);

	uint getNumResources() { return _shapeResources.size(); }
	uint16 getResource(uint num) { return _shapeResources[num]; }
	Common::Point getOffset(uint num) { return _shapeOffsets[num]; }

	uint32 getCurrentFrame() { return _currentFrame; }

	uint16 getParentId();

protected:
	MohawkEngine_LivingBooks *_vm;
	LBAnimationItem *_parent;

	Common::Rect _bounds, _clip;
	Common::Array<LBAnimationNode *> _nodes;

	uint16 _tempo;

	uint16 _currentSound;
	CueList _cueList;

	uint32 _lastTime, _currentFrame;
	bool _running;

	void loadShape(uint16 resourceId);
	Common::Array<uint16> _shapeResources;
	Common::Array<Common::Point> _shapeOffsets;
};

class LBItem {
	friend class LBCode;

public:
	LBItem(MohawkEngine_LivingBooks *vm, LBPage *page, Common::Rect rect);
	virtual ~LBItem();

	void readFrom(Common::SeekableSubReadStreamEndian *stream);
	void readData(uint16 type, uint16 size, byte *data);
	virtual void readData(uint16 type, uint16 size, Common::MemoryReadStreamEndian *stream);

	virtual void destroySelf(); // 0x2
	virtual void setEnabled(bool enabled); // 0x3
	virtual void setGlobalEnabled(bool enabled);
	virtual bool contains(Common::Point point); // 0x7
	virtual void update(); // 0x8
	virtual void draw() { } // 0x9
	virtual void handleKeyChar(Common::Point pos) { } // 0xA
	virtual void handleMouseDown(Common::Point pos); // 0xB
	virtual void handleMouseMove(Common::Point pos); // 0xC
	virtual void handleMouseUp(Common::Point pos); // 0xD
	virtual bool togglePlaying(bool playing, bool restart = false); // 0xF
	virtual void done(bool onlyNotify); // 0x10
	virtual void init(); // 0x11
	virtual void seek(uint16 pos) { } // 0x13
	virtual void seekToTime(uint32 time) { }
	virtual void setFocused(bool focused) { } // 0x14
	virtual void setVisible(bool visible); // 0x17
	virtual void setGlobalVisible(bool enabled);
	virtual void startPhase(uint phase); // 0x18
	virtual void stop(); // 0x19
	virtual void notify(uint16 data, uint16 from); // 0x1A
	virtual void load();
	virtual void unload();
	virtual void moveBy(const Common::Point &pos);
	virtual void moveTo(const Common::Point &pos);

	LBItem *clone(uint16 newId, const Common::String &newName);

	uint16 getId() { return _itemId; }
	const Common::String &getName() { return _desc; }
	const Common::Rect &getRect() { return _rect; }
	uint16 getSoundPriority() { return _soundMode; }
	bool isLoaded() { return _loaded; }
	bool isAmbient() { return _isAmbient; }

	Common::List<LBItem *>::iterator _iterator;

	// TODO: make private
	Common::HashMap<Common::String, LBValue, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> _variables;

protected:
	MohawkEngine_LivingBooks *_vm;
	LBPage *_page;

	void setNextTime(uint16 min, uint16 max);
	void setNextTime(uint16 min, uint16 max, uint32 start);

	Common::Rect _rect;
	Common::String _desc;
	uint16 _resourceId;
	uint16 _itemId;

	bool _loaded, _visible, _globalVisible, _playing, _enabled, _globalEnabled;

	uint32 _nextTime, _startTime;
	uint16 _loops;

	uint16 _phase, _timingMode, _delayMin, _delayMax;
	uint16 _loopMode, _periodMin, _periodMax;
	uint16 _controlMode, _soundMode;
	Common::Point _relocPoint;

	bool _isAmbient;
	bool _doHitTest;

	virtual LBItem *createClone();

	Common::Array<LBScriptEntry *> _scriptEntries;
	void runScript(uint event, uint16 data = 0, uint16 from = 0);
	int runScriptEntry(LBScriptEntry *entry);

	void runCommand(const Common::String &command);
	bool checkCondition(const Common::String &condition);

	LBScriptEntry *parseScriptEntry(uint16 type, uint16 &size, Common::MemoryReadStreamEndian *stream, bool isSubentry = false);
};

class LBSoundItem : public LBItem {
public:
	LBSoundItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);
	~LBSoundItem();

	void update();
	bool togglePlaying(bool playing, bool restart);
	void stop();

protected:
	LBItem *createClone();

	bool _running;
};

struct GroupEntry {
	uint entryId;
	uint entryType;
};

class LBGroupItem : public LBItem {
public:
	LBGroupItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);

	void readData(uint16 type, uint16 size, Common::MemoryReadStreamEndian *stream);

	void destroySelf();
	void setEnabled(bool enabled);
	void setGlobalEnabled(bool enabled);
	bool contains(Common::Point point);
	bool togglePlaying(bool playing, bool restart);
	// 0x12
	void seek(uint16 pos);
	void setVisible(bool visible);
	void setGlobalVisible(bool visible);
	void startPhase(uint phase);
	void stop();
	void load();
	void unload();
	void moveBy(const Common::Point &pos);
	void moveTo(const Common::Point &pos);

protected:
	LBItem *createClone();

	bool _starting;

	Common::Array<GroupEntry> _groupEntries;
};

class LBPaletteItem : public LBItem {
public:
	LBPaletteItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);
	~LBPaletteItem();

	void readData(uint16 type, uint16 size, Common::MemoryReadStreamEndian *stream);

	bool togglePlaying(bool playing, bool restart);
	void update();

protected:
	LBItem *createClone();

	uint16 _fadeInPeriod, _fadeInStep, _drawStart, _drawCount;
	uint32 _fadeInStart, _fadeInCurrent;
	byte *_palette;
};

struct LiveTextWord {
	Common::Rect bounds;
	uint16 soundId;

	uint16 itemType;
	uint16 itemId;
};

struct LiveTextPhrase {
	uint16 wordStart, wordCount;
	uint16 highlightStart, highlightEnd;
	uint16 startId, endId;
};

class LBLiveTextItem : public LBItem {
public:
	LBLiveTextItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);

	void readData(uint16 type, uint16 size, Common::MemoryReadStreamEndian *stream);

	bool contains(Common::Point point);
	void update();
	void draw();
	void handleMouseDown(Common::Point pos);
	bool togglePlaying(bool playing, bool restart);
	void stop();
	void notify(uint16 data, uint16 from);

protected:
	LBItem *createClone();

	void paletteUpdate(uint16 word, bool on);
	void drawWord(uint word, uint yPos);

	uint16 _currentPhrase, _currentWord;

	byte _backgroundColor[4];
	byte _foregroundColor[4];
	byte _highlightColor[4];
	uint16 _paletteIndex;

	Common::Array<LiveTextWord> _words;
	Common::Array<LiveTextPhrase> _phrases;
};

class LBPictureItem : public LBItem {
public:
	LBPictureItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);

	void readData(uint16 type, uint16 size, Common::MemoryReadStreamEndian *stream);

	bool contains(Common::Point point);
	void draw();
	void init();

protected:
	LBItem *createClone();
};

class LBAnimationItem : public LBItem {
public:
	LBAnimationItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);
	~LBAnimationItem();

	void setEnabled(bool enabled);
	bool contains(Common::Point point);
	void update();
	void draw();
	bool togglePlaying(bool playing, bool restart);
	void done(bool onlyNotify);
	void init();
	void seek(uint16 pos);
	void seekToTime(uint32 time);
	void startPhase(uint phase);
	void stop();

protected:
	LBItem *createClone();

	LBAnimation *_anim;
	bool _running;
};

class LBMovieItem : public LBItem {
public:
	LBMovieItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);
	~LBMovieItem();

	void update();
	bool togglePlaying(bool playing, bool restart);

protected:
	LBItem *createClone();
};

class LBMiniGameItem : public LBItem {
public:
	LBMiniGameItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);
	~LBMiniGameItem();

	bool togglePlaying(bool playing, bool restart);

protected:
	LBItem *createClone();
};

class LBProxyItem : public LBItem {
public:
	LBProxyItem(MohawkEngine_LivingBooks *_vm, LBPage *page, Common::Rect rect);
	~LBProxyItem();

	void load();
	void unload();

protected:
	LBItem *createClone();

	class LBPage *_page;
};

struct NotifyEvent {
	NotifyEvent(uint t, uint p) : type(t), param(p), newUnknown(0), newMode(0), newPage(0), newSubpage(0) { }
	uint type;
	uint param;

	// kLBNotifyChangeMode
	uint16 newUnknown;
	uint16 newMode;
	uint16 newPage;
	uint16 newSubpage;
	Common::String newCursor;
};

enum DelayedEventType {
	kLBDelayedEventDestroy = 0,
	kLBDelayedEventSetNotVisible = 1,
	kLBDelayedEventDone = 2
};

struct DelayedEvent {
	DelayedEvent(LBItem *i, DelayedEventType t) : item(i), type(t) { }
	LBItem *item;
	DelayedEventType type;
};

class LBPage {
public:
	LBPage(MohawkEngine_LivingBooks *vm);
	~LBPage();

	void open(Archive *mhk, uint16 baseId);
	uint16 getResourceVersion();

	void addClonedItem(LBItem *item);
	void itemDestroyed(LBItem *item);

	LBCode *_code;

protected:
	MohawkEngine_LivingBooks *_vm;

	Archive *_mhk;
	Common::Array<LBItem *> _items;

	uint16 _baseId;
	bool _cascade;

	void loadBITL(uint16 resourceId);
};

class MohawkEngine_LivingBooks : public MohawkEngine {
protected:
	Common::Error run() override;

public:
	MohawkEngine_LivingBooks(OSystem *syst, const MohawkGameDescription *gamedesc);
	virtual ~MohawkEngine_LivingBooks();

	Common::RandomSource *_rnd;

	VideoManager *_video;
	Sound *_sound;
	LBGraphics *_gfx;
	bool _needsRedraw, _needsUpdate;

	void addNotifyEvent(NotifyEvent event);

	Common::SeekableSubReadStreamEndian *wrapStreamEndian(uint32 tag, uint16 id);
	Common::String readString(Common::ReadStream *stream);
	Common::Rect readRect(Common::ReadStreamEndian *stream);
	GUI::Debugger *getDebugger() override { return _console; }

	void addArchive(Archive *archive);
	void removeArchive(Archive *archive);
	void addItem(LBItem *item);
	void removeItems(const Common::Array<LBItem *> &items);

	LBItem *getItemById(uint16 id);
	LBItem *getItemByName(Common::String name);

	void setFocus(LBItem *focus);
	void setEnableForAll(bool enable, LBItem *except = 0);
	void notifyAll(uint16 data, uint16 from);
	void queueDelayedEvent(DelayedEvent event);

	bool playSound(LBItem *source, uint16 resourceId);
	void lockSound(LBItem *owner, bool lock);

	bool isBigEndian() const { return getGameType() != GType_LIVINGBOOKSV1 || getPlatform() == Common::kPlatformMacintosh; }
	bool isPreMohawk() const;

	LBMode getCurMode() { return _curMode; }

	bool tryLoadPageStart(LBMode mode, uint page);
	bool loadPage(LBMode mode, uint page, uint subpage);
	void prevPage();
	void nextPage();

	// TODO: make private
	Common::HashMap<Common::String, LBValue, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> _variables;

	// helper functions, also used by LBProxyItem
	Common::String getFileNameFromConfig(const Common::String &section, const Common::String &key, Common::String &leftover);
	Archive *createArchive() const;

private:
	LivingBooksConsole *_console;
	Common::INIFile _bookInfoFile;

	Common::String getBookInfoFileName() const;
	void loadBookInfo(const Common::String &filename);

	Common::String stringForMode(LBMode mode);

	bool _readOnly, _introDone;
	LBMode _curMode;
	uint16 _curPage, _curSubPage;
	uint16 _phase;
	LBPage *_page;
	Common::Array<LBItem *> _items;
	Common::List<LBItem *> _orderedItems;
	Common::Queue<DelayedEvent> _eventQueue;
	LBItem *_focus;
	void destroyPage();
	void updatePage();

	uint16 _lastSoundOwner, _lastSoundId;
	uint16 _lastSoundPriority;
	uint16 _soundLockOwner;
	uint16 _maxSoundPriority;

	void loadSHP(uint16 resourceId);

	bool tryDefaultPage();

	void handleUIMenuClick(uint controlId);
	void handleUIPoetryMenuClick(uint controlId);
	void handleUIQuitClick(uint controlId);
	void handleUIOptionsClick(uint controlId);

	Common::Queue<NotifyEvent> _notifyEvents;
	void handleNotify(NotifyEvent &event);

	uint16 _screenWidth;
	uint16 _screenHeight;
	uint16 _numLanguages;
	uint16 _numPages;
	Common::String _title;
	Common::String _copyright;
	bool _poetryMode;

	uint16 _curLanguage;
	uint16 _curSelectedPage;
	bool _alreadyShowedIntro;

	// String Manipulation Functions
	Common::String removeQuotesFromString(const Common::String &string, Common::String &leftover);
	Common::String convertMacFileName(const Common::String &string);
	Common::String convertWinFileName(const Common::String &string);

	// Configuration File Functions
	Common::String getStringFromConfig(const Common::String &section, const Common::String &key);
	Common::String getStringFromConfig(const Common::String &section, const Common::String &key, Common::String &leftover);
	int getIntFromConfig(const Common::String &section, const Common::String &key);

	void pauseEngineIntern(bool) override;
};

} // End of namespace Mohawk

#endif