File: elements.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (485 lines) | stat: -rw-r--r-- 17,963 bytes parent folder | download | duplicates (2)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef MTROPOLIS_ELEMENTS_H
#define MTROPOLIS_ELEMENTS_H

#include "graphics/fontman.h"
#include "graphics/palette.h"

#include "mtropolis/data.h"
#include "mtropolis/runtime.h"
#include "mtropolis/render.h"
#include "mtropolis/coroutine_protos.h"

namespace Video {

class VideoDecoder;

} // End of namespace Video

namespace MTropolis {

class AudioPlayer;
class CachedAudio;
class CachedImage;
class CachedMToon;
struct AudioMetadata;
struct ElementLoaderContext;
struct MToonMetadata;

enum MediaState {
	kMediaStatePlaying,
	kMediaStatePlayingLastFrame,
	kMediaStateStopped,
	kMediaStatePaused,
};

class GraphicElement : public VisualElement {
public:
	GraphicElement();
	~GraphicElement();

	bool load(ElementLoaderContext &context, const Data::GraphicElement &data);

	bool readAttribute(MiniscriptThread *thread, DynamicValue &result, const Common::String &attrib) override;
	MiniscriptInstructionOutcome writeRefAttribute(MiniscriptThread *thread, DynamicValueWriteProxy &result, const Common::String &attrib) override;

	void render(Window *window) override;

	Common::SharedPtr<Structural> shallowClone() const override;
	void visitInternalReferences(IStructuralReferenceVisitor *visitor) override;

#ifdef MTROPOLIS_DEBUG_ENABLE
	const char *debugGetTypeName() const override { return "Graphic Element"; }
	SupportStatus debugGetSupportStatus() const override { return kSupportStatusDone; }
#endif

private:
	GraphicElement(const GraphicElement &other);

	bool _cacheBitmap;

	Common::SharedPtr<Graphics::ManagedSurface> _mask;
};

class MovieResizeFilter {
public:
	virtual ~MovieResizeFilter();

	virtual Common::SharedPtr<Graphics::ManagedSurface> scaleFrame(const Graphics::Surface &surface, uint32 timestamp) const = 0;
};

class MovieElement : public VisualElement, public ISegmentUnloadSignalReceiver, public IPlayMediaSignalReceiver {
public:
	MovieElement();
	~MovieElement();

	bool load(ElementLoaderContext &context, const Data::MovieElement &data);

	bool readAttribute(MiniscriptThread *thread, DynamicValue &result, const Common::String &attrib) override;
	MiniscriptInstructionOutcome writeRefAttribute(MiniscriptThread *thread, DynamicValueWriteProxy &result, const Common::String &attrib) override;

	VThreadState asyncConsumeCommand(Runtime *runtime, const Common::SharedPtr<MessageProperties> &msg) override;

	void activate() override;
	void deactivate() override;

	bool canAutoPlay() const override;
	void queueAutoPlayEvents(Runtime *runtime, bool isAutoPlaying) override;

	void render(Window *window) override;
	void playMedia(Runtime *runtime, Project *project) override;
	void tryAutoSetName(Runtime *runtime, Project *project) override;

	void setResizeFilter(const Common::SharedPtr<MovieResizeFilter> &filter);

	Common::SharedPtr<Structural> shallowClone() const override;
	void visitInternalReferences(IStructuralReferenceVisitor *visitor) override;

#ifdef MTROPOLIS_DEBUG_ENABLE
	const char *debugGetTypeName() const override { return "Movie Element"; }
	SupportStatus debugGetSupportStatus() const override { return kSupportStatusDone; }

	void debugSkipMovies() override;
#endif

protected:
	void onPauseStateChanged() override;
	void onSegmentUnloaded(int segmentIndex) override;

	struct MovieElementConsumeCommandCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_3(MovieElement *, self, Runtime *, runtime, Common::SharedPtr<MessageProperties>, msg);
	};

private:
	IntRange computeRealRange() const;

	void stopSubtitles();

	void initFallbackPalette();

	MiniscriptInstructionOutcome scriptSetRange(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetRangeStart(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetRangeEnd(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptRangeWriteRefAttribute(MiniscriptThread *thread, DynamicValueWriteProxy &result, const Common::String &attrib);
	MiniscriptInstructionOutcome scriptSetVolume(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetTimestamp(MiniscriptThread *thread, const DynamicValue &value);

	MiniscriptInstructionOutcome scriptSetRangeTyped(MiniscriptThread *thread, const IntRange &range);

	struct StartPlayingCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_2(MovieElement *, self, Runtime *, runtime);
	};

	struct SeekToTimeCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_3(MovieElement *, self, Runtime *, runtime, uint32, timestamp);
	};

	bool _cacheBitmap;
	bool _alternate;
	bool _playEveryFrame;
	bool _reversed;
	//bool _haveFiredAtLastCel;
	//bool _haveFiredAtFirstCel;
	bool _shouldPlayIfNotPaused;
	bool _needsReset;	// If true, then the video position was reset by a seek or stop and decoding must be restarted even if the target state is the same as the play state.
	MediaState _currentPlayState;
	uint32 _assetID;

	Common::SharedPtr<Video::VideoDecoder> _videoDecoder;
	uint32 _maxTimestamp;
	uint32 _timeScale;
	uint32 _currentTimestamp;
	int32 _volume;
	IntRange _playRange;

	const Graphics::Surface *_displayFrame;
	Common::SharedPtr<Graphics::ManagedSurface> _scaledFrame;
	Common::SharedPtr<MovieResizeFilter> _resizeFilter;

	Common::SharedPtr<SegmentUnloadSignaller> _unloadSignaller;
	Common::SharedPtr<PlayMediaSignaller> _playMediaSignaller;

	Common::SharedPtr<SubtitlePlayer> _subtitles;

	Common::Array<int> _damagedFrames;

	Common::ScopedPtr<Graphics::Palette> _fallbackPalette;
};

class ImageElement : public VisualElement {
public:
	ImageElement();
	~ImageElement();

	bool load(ElementLoaderContext &context, const Data::ImageElement &data);

	bool readAttribute(MiniscriptThread *thread, DynamicValue &result, const Common::String &attrib) override;
	MiniscriptInstructionOutcome writeRefAttribute(MiniscriptThread *thread, DynamicValueWriteProxy &writeProxy, const Common::String &attrib) override;

	void activate() override;
	void deactivate() override;

	void tryAutoSetName(Runtime *runtime, Project *project) override;

	void render(Window *window) override;

	Common::SharedPtr<Structural> shallowClone() const override;
	void visitInternalReferences(IStructuralReferenceVisitor *visitor) override;

#ifdef MTROPOLIS_DEBUG_ENABLE
	const char *debugGetTypeName() const override { return "Image Element"; }
	SupportStatus debugGetSupportStatus() const override { return kSupportStatusDone; }
	void debugInspect(IDebugInspectionReport *report) const override;
#endif

private:
	MiniscriptInstructionOutcome scriptSetFlushPriority(MiniscriptThread *thread, const DynamicValue &value);

	bool _cacheBitmap;
	uint32 _assetID;

	Common::SharedPtr<CachedImage> _cachedImage;

	Common::String _text;	// ...???
};

class MToonElement : public VisualElement, public IPlayMediaSignalReceiver {
public:
	MToonElement();
	~MToonElement();

	bool load(ElementLoaderContext &context, const Data::MToonElement &data);

	bool readAttribute(MiniscriptThread *thread, DynamicValue &result, const Common::String &attrib) override;
	MiniscriptInstructionOutcome writeRefAttribute(MiniscriptThread *thread, DynamicValueWriteProxy &result, const Common::String &attrib) override;

	VThreadState asyncConsumeCommand(Runtime *runtime, const Common::SharedPtr<MessageProperties> &msg) override;

	void activate() override;
	void deactivate() override;

	void tryAutoSetName(Runtime *runtime, Project *project) override;

	bool canAutoPlay() const override;

	void render(Window *window) override;

	bool isMouseCollisionAtPoint(int32 relativeX, int32 relativeY) const override;

	Common::Rect getRelativeCollisionRect() const override;

	Common::SharedPtr<Structural> shallowClone() const override;
	void visitInternalReferences(IStructuralReferenceVisitor *visitor) override;

#ifdef MTROPOLIS_DEBUG_ENABLE
	const char *debugGetTypeName() const override { return "mToon Element"; }
	SupportStatus debugGetSupportStatus() const override { return kSupportStatusDone; }
	void debugInspect(IDebugInspectionReport *report) const override;
#endif

private:
	MToonElement(const MToonElement &other);

	struct MToonConsumeCommandCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_3(MToonElement *, self, Runtime *, runtime, Common::SharedPtr<MessageProperties>, msg);
	};

	struct StartPlayingCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_2(MToonElement *, self, Runtime *, runtime);
	};

	struct StopPlayingCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_2(MToonElement *, self, Runtime *, runtime);
	};

	struct ChangeFrameCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_3(MToonElement *, self, Runtime *, runtime, uint32, frame);
	};

	void playMedia(Runtime *runtime, Project *project) override;
	MiniscriptInstructionOutcome scriptSetRate(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetCel(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetRange(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetRangeStart(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetRangeEnd(MiniscriptThread *thread, const DynamicValue &value);

	MiniscriptInstructionOutcome scriptRangeWriteRefAttribute(MiniscriptThread *thread, DynamicValueWriteProxy &result, const Common::String &attrib);
	MiniscriptInstructionOutcome scriptSetRangeTyped(MiniscriptThread *thread, const IntRange &value);
	MiniscriptInstructionOutcome scriptSetRangeTyped(MiniscriptThread *thread, const Common::Point &value);
	MiniscriptInstructionOutcome scriptSetRangeTyped(MiniscriptThread *thread, const Label &value);

	void onPauseStateChanged() override;

	bool _cacheBitmap;

	// If set, then carry over residual frame time and display at the desired rate.  If not set, reset residual each frame for smoother animation.
	bool _maintainRate;

	uint32 _assetID;
	int32 _rateTimes100000;
	int32 _flushPriority;
	uint32 _celStartTimeMSec;
	bool _isPlaying;	// Is actually rolling media, this is only set by playMedia because it needs to start after scene transition

	// Stop state works independently of pause/hidden even though it has similar effect:
	// If an mToon is stopped, then it it is always hidden and will not play until a Play
	// command is received.
	//
	// Also, MTI depends on not firing Stopped commands if the mToon is already stopped,
	// otherwise the cannon scene in the Hispaniola will get stuck in an infinite loop due
	// to Stopped
	bool _isStopped;

	Common::SharedPtr<Graphics::ManagedSurface> _renderSurface;
	uint32 _renderedFrame;

	Common::SharedPtr<MToonMetadata> _metadata;
	Common::SharedPtr<CachedMToon> _cachedMToon;
	Common::SharedPtr<PlayMediaSignaller> _playMediaSignaller;

	// NOTE: To produce proper behavior, these are not sanitized until playMedia.  render must tolerate invalid values without changing them.
	IntRange _playRange;
	int32 _cel;

	bool _hasIssuedRenderWarning;
};

class TextLabelElement : public VisualElement {
public:
	TextLabelElement();
	~TextLabelElement();

	bool isTextLabel() const override;

	bool load(ElementLoaderContext &context, const Data::TextLabelElement &data);

	bool readAttribute(MiniscriptThread *thread, DynamicValue &result, const Common::String &attrib) override;
	bool readAttributeIndexed(MiniscriptThread *thread, DynamicValue &result, const Common::String &attrib, const DynamicValue &index) override;
	MiniscriptInstructionOutcome writeRefAttribute(MiniscriptThread *thread, DynamicValueWriteProxy &writeProxy, const Common::String &attrib) override;
	MiniscriptInstructionOutcome writeRefAttributeIndexed(MiniscriptThread *thread, DynamicValueWriteProxy &writeProxy, const Common::String &attrib, const DynamicValue &index) override;

	void activate() override;
	void deactivate() override;

	void render(Window *window) override;

	void setTextStyle(uint16 macFontID, const Common::String &fontFamilyName, uint size, TextAlignment alignment, const TextStyleFlags &styleFlags);

	Graphics::FontManager::FontUsage getDefaultUsageForMacFont(uint16 macFontID, uint size);
	Graphics::FontManager::FontUsage getDefaultUsageForNamedFont(const Common::String &fontFamilyName, uint size);

	Common::SharedPtr<Structural> shallowClone() const override;
	void visitInternalReferences(IStructuralReferenceVisitor *visitor) override;

#ifdef MTROPOLIS_DEBUG_ENABLE
	const char *debugGetTypeName() const override { return "Text Label Element"; }
	SupportStatus debugGetSupportStatus() const override { return kSupportStatusPartial; }
#endif

private:
	TextLabelElement(const TextLabelElement &other);

	struct TextLabelLineWriteInterface {
		static MiniscriptInstructionOutcome write(MiniscriptThread *thread, const DynamicValue &dest, void *objectRef, uintptr ptrOrOffset);
		static MiniscriptInstructionOutcome refAttrib(MiniscriptThread *thread, DynamicValueWriteProxy &proxy, void *objectRef, uintptr ptrOrOffset, const Common::String &attrib);
		static MiniscriptInstructionOutcome refAttribIndexed(MiniscriptThread *thread, DynamicValueWriteProxy &proxy, void *objectRef, uintptr ptrOrOffset, const Common::String &attrib, const DynamicValue &index);
	};

	MiniscriptInstructionOutcome scriptSetText(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetLine(MiniscriptThread *thread, size_t lineIndex, const DynamicValue &value);

	bool findLineRange(size_t lineNum, uint32 &outStartPos, uint32 &outEndPos) const;
	size_t countLines() const;

	bool _cacheBitmap;
	bool _needsRender;

	bool _isBitmap;
	uint32 _assetID;

	Common::String _text;
	uint16 _macFontID;
	Common::String _fontFamilyName;
	uint _size;
	TextAlignment _alignment;
	TextStyleFlags _styleFlags;

	Common::Array<MacFormattingSpan> _macFormattingSpans;

	// NOTE: This may be a surface loaded from data, so it must not be altered.
	// If you need to render again, recreate the surface.  If you want to change
	// this behavior, please add a flag indicating that it is from the asset.
	Common::SharedPtr<Graphics::ManagedSurface> _renderedText;
};

class SoundElement : public NonVisualElement, public IPlayMediaSignalReceiver {
public:
	SoundElement();
	~SoundElement();

	bool load(ElementLoaderContext &context, const Data::SoundElement &data);

	bool readAttribute(MiniscriptThread *thread, DynamicValue &result, const Common::String &attrib) override;
	MiniscriptInstructionOutcome writeRefAttribute(MiniscriptThread *thread, DynamicValueWriteProxy &writeProxy, const Common::String &attrib) override;

	VThreadState asyncConsumeCommand(Runtime *runtime, const Common::SharedPtr<MessageProperties> &msg) override;

	void activate() override;
	void deactivate() override;

	bool canAutoPlay() const override;

	void playMedia(Runtime *runtime, Project *project) override;
	void tryAutoSetName(Runtime *runtime, Project *project) override;

	bool resolveMediaMarkerLabel(const Label &label, int32 &outResolution) const override;

	Common::SharedPtr<Structural> shallowClone() const override;
	void visitInternalReferences(IStructuralReferenceVisitor *visitor) override;

#ifdef MTROPOLIS_DEBUG_ENABLE
	const char *debugGetTypeName() const override { return "Sound Element"; }
	SupportStatus debugGetSupportStatus() const override { return kSupportStatusDone; }
	void debugInspect(IDebugInspectionReport *report) const override;
#endif

private:
	SoundElement(const SoundElement &other);

	void initSubtitles();
	void stopPlayer();

	MiniscriptInstructionOutcome scriptSetLoop(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetVolume(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetBalance(MiniscriptThread *thread, const DynamicValue &value);
	MiniscriptInstructionOutcome scriptSetAsset(MiniscriptThread *thread, const DynamicValue &value);

	struct SoundElementConsumeCommandCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_3(SoundElement *, self, Runtime *, runtime, Common::SharedPtr<MessageProperties>, msg);
	};

	struct StartPlayingCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_2(SoundElement *, self, Runtime *, runtime);
	};

	struct StopPlayingCoroutine {
		CORO_DEFINE_RETURN_TYPE(void);
		CORO_DEFINE_PARAMS_2(SoundElement *, self, Runtime *, runtime);
	};

	void setLoop(bool loop);
	void setVolume(uint16 volume);
	void setBalance(int16 balance);

	uint16 _leftVolume;
	uint16 _rightVolume;
	int16 _balance;
	uint32 _assetID;

	Common::SharedPtr<CachedAudio> _cachedAudio;
	Common::SharedPtr<AudioMetadata> _metadata;
	Common::SharedPtr<AudioPlayer> _player;
	uint64 _startTime;
	uint64 _finishTime;
	uint64 _startTimestamp;	// Time in the sound corresponding to the start time
	uint64 _cueCheckTime;
	bool _shouldPlayIfNotPaused;
	bool _needsReset;

	Common::SharedPtr<PlayMediaSignaller> _playMediaSignaller;

	Common::SharedPtr<SubtitlePlayer> _subtitlePlayer;
};

} // End of namespace MTropolis

#endif