mirror of
https://github.com/4ian/GDevelop.git
synced 2025-10-15 10:19:04 +00:00
131 lines
3.8 KiB
TypeScript
131 lines
3.8 KiB
TypeScript
namespace gdjs {
|
|
import PIXI = GlobalPIXIModule.PIXI;
|
|
|
|
/**
|
|
* The renderer for a gdjs.RuntimeScene using Pixi.js.
|
|
*/
|
|
export class RuntimeScenePixiRenderer
|
|
implements gdjs.RuntimeInstanceContainerPixiRenderer {
|
|
_pixiRenderer: PIXI.Renderer | null;
|
|
_runtimeScene: gdjs.RuntimeScene;
|
|
_pixiContainer: PIXI.Container;
|
|
_profilerText: PIXI.Text | null = null;
|
|
_showCursorAtNextRender: boolean = false;
|
|
|
|
constructor(
|
|
runtimeScene: gdjs.RuntimeScene,
|
|
runtimeGameRenderer: gdjs.RuntimeGamePixiRenderer | null
|
|
) {
|
|
this._pixiRenderer = runtimeGameRenderer
|
|
? runtimeGameRenderer.getPIXIRenderer()
|
|
: null;
|
|
this._runtimeScene = runtimeScene;
|
|
this._pixiContainer = new PIXI.Container();
|
|
|
|
// Contains the layers of the scene (and, optionally, debug PIXI objects).
|
|
this._pixiContainer.sortableChildren = true;
|
|
}
|
|
|
|
onGameResolutionResized() {
|
|
if (!this._pixiRenderer) {
|
|
return;
|
|
}
|
|
const runtimeGame = this._runtimeScene.getGame();
|
|
this._pixiContainer.scale.x =
|
|
this._pixiRenderer.width / runtimeGame.getGameResolutionWidth();
|
|
this._pixiContainer.scale.y =
|
|
this._pixiRenderer.height / runtimeGame.getGameResolutionHeight();
|
|
}
|
|
|
|
// Nothing to do.
|
|
onSceneUnloaded() {}
|
|
|
|
render() {
|
|
if (!this._pixiRenderer) {
|
|
return;
|
|
}
|
|
|
|
// this._renderProfileText(); //Uncomment to display profiling times
|
|
|
|
// render the PIXI container of the scene
|
|
this._pixiRenderer.backgroundColor = this._runtimeScene.getBackgroundColor();
|
|
this._pixiRenderer.render(this._pixiContainer);
|
|
|
|
// synchronize showing the cursor with rendering (useful to reduce
|
|
// blinking while switching from in-game cursor)
|
|
if (this._showCursorAtNextRender) {
|
|
this._pixiRenderer.view.style.cursor = '';
|
|
this._showCursorAtNextRender = false;
|
|
}
|
|
}
|
|
|
|
_renderProfileText() {
|
|
const profiler = this._runtimeScene.getProfiler();
|
|
if (!profiler) {
|
|
return;
|
|
}
|
|
if (!this._profilerText) {
|
|
this._profilerText = new PIXI.Text(' ', {
|
|
align: 'left',
|
|
stroke: '#FFF',
|
|
strokeThickness: 1,
|
|
});
|
|
|
|
// Add on top of all layers:
|
|
this._pixiContainer.addChild(this._profilerText);
|
|
}
|
|
const average = profiler.getFramesAverageMeasures();
|
|
const outputs = [];
|
|
gdjs.Profiler.getProfilerSectionTexts('All', average, outputs);
|
|
this._profilerText.text = outputs.join('\n');
|
|
}
|
|
|
|
hideCursor(): void {
|
|
this._showCursorAtNextRender = false;
|
|
if (!this._pixiRenderer) {
|
|
return;
|
|
}
|
|
this._pixiRenderer.view.style.cursor = 'none';
|
|
}
|
|
|
|
showCursor(): void {
|
|
this._showCursorAtNextRender = true;
|
|
}
|
|
|
|
getPIXIContainer() {
|
|
return this._pixiContainer;
|
|
}
|
|
|
|
getRendererObject() {
|
|
return this._pixiContainer;
|
|
}
|
|
|
|
getPIXIRenderer() {
|
|
return this._pixiRenderer;
|
|
}
|
|
|
|
setLayerIndex(layer: gdjs.Layer, index: float): void {
|
|
const layerPixiRenderer: gdjs.LayerPixiRenderer = layer.getRenderer();
|
|
let layerPixiObject:
|
|
| PIXI.Container
|
|
| PIXI.Sprite
|
|
| null = layerPixiRenderer.getRendererObject();
|
|
if (layer.isLightingLayer()) {
|
|
layerPixiObject = layerPixiRenderer.getLightingSprite();
|
|
}
|
|
if (!layerPixiObject) {
|
|
return;
|
|
}
|
|
if (this._pixiContainer.children.indexOf(layerPixiObject) === index) {
|
|
return;
|
|
}
|
|
this._pixiContainer.removeChild(layerPixiObject);
|
|
this._pixiContainer.addChildAt(layerPixiObject, index);
|
|
}
|
|
}
|
|
|
|
// Register the class to let the engine use it.
|
|
export type RuntimeSceneRenderer = gdjs.RuntimeScenePixiRenderer;
|
|
export const RuntimeSceneRenderer = gdjs.RuntimeScenePixiRenderer;
|
|
}
|