|
@@ -1,16 +1,45 @@
|
|
|
package h2d;
|
|
|
import hxd.Math;
|
|
|
|
|
|
+/**
|
|
|
+ h2d.Scene is the root class for a 2D scene. All root objects are added to it before being drawn on screen.
|
|
|
+**/
|
|
|
class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.InteractiveScene {
|
|
|
|
|
|
+ /**
|
|
|
+ The current width (in pixels) of the scene. Can change if the screen gets resized.
|
|
|
+ **/
|
|
|
public var width(default,null) : Int;
|
|
|
+
|
|
|
+ /**
|
|
|
+ The current height (in pixels) of the scene. Can change if the screen gets resized.
|
|
|
+ **/
|
|
|
public var height(default, null) : Int;
|
|
|
|
|
|
+ /**
|
|
|
+ The current mouse X coordinates (in pixel) relative to the scene.
|
|
|
+ **/
|
|
|
public var mouseX(get, null) : Float;
|
|
|
+
|
|
|
+ /**
|
|
|
+ The current mouse Y coordinates (in pixel) relative to the scene.
|
|
|
+ **/
|
|
|
public var mouseY(get, null) : Float;
|
|
|
|
|
|
+ /**
|
|
|
+ The zoom factor of the scene, allows to set a fixed x2, x4 etc. zoom for pixel art
|
|
|
+ When setting a zoom > 0, the scene resize will be automaticaly managed.
|
|
|
+ **/
|
|
|
public var zoom(get, set) : Int;
|
|
|
+
|
|
|
+ /**
|
|
|
+ Set the default value for `h2d.Drawable.smooth` (default: false)
|
|
|
+ **/
|
|
|
public var defaultSmooth(get, set) : Bool;
|
|
|
+
|
|
|
+ /**
|
|
|
+ The scene current renderer. Can be customized.
|
|
|
+ **/
|
|
|
public var renderer(get, set) : RenderContext;
|
|
|
|
|
|
var fixedSize : Bool;
|
|
@@ -21,6 +50,9 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
@:allow(h2d.Interactive)
|
|
|
var events : hxd.SceneEvents;
|
|
|
|
|
|
+ /**
|
|
|
+ Create a new scene. A default 2D scene is already available in `hxd.App.s2d`
|
|
|
+ **/
|
|
|
public function new() {
|
|
|
super(null);
|
|
|
var e = h3d.Engine.getCurrent();
|
|
@@ -36,6 +68,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
inline function get_defaultSmooth() return ctx.defaultSmooth;
|
|
|
inline function set_defaultSmooth(v) return ctx.defaultSmooth = v;
|
|
|
|
|
|
+ @:dox(hide) @:noCompletion
|
|
|
public function setEvents(events : hxd.SceneEvents) {
|
|
|
this.events = events;
|
|
|
}
|
|
@@ -60,6 +93,9 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
function get_renderer() return ctx;
|
|
|
function set_renderer(v) { ctx = v; return v; }
|
|
|
|
|
|
+ /**
|
|
|
+ Set the fixed size for the scene, will prevent automatic scene resizing when screen size changes.
|
|
|
+ **/
|
|
|
public function setFixedSize( w : Int, h : Int ) {
|
|
|
width = w;
|
|
|
height = h;
|
|
@@ -67,6 +103,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
posChanged = true;
|
|
|
}
|
|
|
|
|
|
+ @:dox(hide) @:noCompletion
|
|
|
public function checkResize() {
|
|
|
if( fixedSize ) return;
|
|
|
var engine = h3d.Engine.getCurrent();
|
|
@@ -93,6 +130,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
return screenYToLocal(stage.mouseY);
|
|
|
}
|
|
|
|
|
|
+ @:dox(hide) @:noCompletion
|
|
|
public function dispatchListeners( event : hxd.Event ) {
|
|
|
screenToLocal(event);
|
|
|
for( l in eventListeners ) {
|
|
@@ -101,6 +139,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @:dox(hide) @:noCompletion
|
|
|
public function isInteractiveVisible( i : hxd.SceneEvents.Interactive ) : Bool {
|
|
|
var s : Object = cast i;
|
|
|
while( s != null ) {
|
|
@@ -110,6 +149,9 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Return the topmost visible Interactive at the specific coordinates
|
|
|
+ **/
|
|
|
public function getInteractive( x : Float, y : Float ) : Interactive {
|
|
|
var rx = x * matA + y * matB + absX;
|
|
|
var ry = x * matC + y * matD + absY;
|
|
@@ -166,6 +208,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
e.relY = ry;
|
|
|
}
|
|
|
|
|
|
+ @:dox(hide) @:noCompletion
|
|
|
public function dispatchEvent( event : hxd.Event, to : hxd.SceneEvents.Interactive ) {
|
|
|
var i : Interactive = cast to;
|
|
|
screenToLocal(event);
|
|
@@ -192,6 +235,7 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
i.handleEvent(event);
|
|
|
}
|
|
|
|
|
|
+ @:dox(hide) @:noCompletion
|
|
|
public function handleEvent( event : hxd.Event, last : hxd.SceneEvents.Interactive ) : hxd.SceneEvents.Interactive {
|
|
|
screenToLocal(event);
|
|
|
var rx = event.relX;
|
|
@@ -253,10 +297,16 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Add an event listener that will capture all events not caught by an h2d.Interactive
|
|
|
+ **/
|
|
|
public function addEventListener( f : hxd.Event -> Void ) {
|
|
|
eventListeners.push(f);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Remove a previously added event listener, return false it was not part of our event listeners.
|
|
|
+ **/
|
|
|
public function removeEventListener( f : hxd.Event -> Void ) {
|
|
|
for( e in eventListeners )
|
|
|
if( Reflect.compareMethods(e, f) ) {
|
|
@@ -266,17 +316,28 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- public function startDrag( f : hxd.Event -> Void, ?onCancel : Void -> Void, ?refEvent : hxd.Event ) {
|
|
|
+ /**
|
|
|
+ Start a drag and drop operation, sending all events to `onEvent` instead of the scene until `stopDrag()` is called.
|
|
|
+ @param onCancel If defined, will be called when stopDrag is called
|
|
|
+ @param refEvent For touch events, only capture events that matches the reference event touchId
|
|
|
+ **/
|
|
|
+ public function startDrag( onEvent : hxd.Event -> Void, ?onCancel : Void -> Void, ?refEvent : hxd.Event ) {
|
|
|
events.startDrag(function(e) {
|
|
|
screenToLocal(e);
|
|
|
- f(e);
|
|
|
+ onEvent(e);
|
|
|
},onCancel, refEvent);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Stop the current drag and drop operation
|
|
|
+ **/
|
|
|
public function stopDrag() {
|
|
|
events.stopDrag();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Get the currently focused Interactive
|
|
|
+ **/
|
|
|
public function getFocus() : Interactive {
|
|
|
if( events == null )
|
|
|
return null;
|
|
@@ -348,12 +409,19 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
@:privateAccess events.onRemove(i);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Dispose the scene and all its children, freeing used GPU memory
|
|
|
+ **/
|
|
|
public function dispose() {
|
|
|
if( allocated )
|
|
|
onRemove();
|
|
|
ctx.dispose();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Before render() or sync() are called, allow to set how much time has elapsed (in seconds) since the last frame in order to update scene animations.
|
|
|
+ This is managed automatically by hxd.App
|
|
|
+ **/
|
|
|
public function setElapsedTime( v : Float ) {
|
|
|
ctx.elapsedTime = v;
|
|
|
}
|
|
@@ -374,6 +442,9 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
ctx.engine.frameCount--;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Synchronize the scene without rendering, updating all objects and animations by the given amount of time, in seconds.
|
|
|
+ **/
|
|
|
public function syncOnly( et : Float ) {
|
|
|
var engine = h3d.Engine.getCurrent();
|
|
|
setElapsedTime(et);
|
|
@@ -384,6 +455,9 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
sync(ctx);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Render the scene on screen. Internal usage only.
|
|
|
+ **/
|
|
|
public function render( engine : h3d.Engine ) {
|
|
|
ctx.engine = engine;
|
|
|
ctx.frame++;
|
|
@@ -407,6 +481,9 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
|
|
|
super.sync(ctx);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ Capture the scene into a texture and render the resulting Bitmap
|
|
|
+ **/
|
|
|
public function captureBitmap( ?target : Tile ) {
|
|
|
var engine = h3d.Engine.getCurrent();
|
|
|
if( target == null ) {
|