Handle HTML5 window resizing better
This commit is contained in:
@@ -18,6 +18,7 @@ import flash.Lib;
|
|||||||
|
|
||||||
|
|
||||||
@:access(lime.app.Application)
|
@:access(lime.app.Application)
|
||||||
|
@:access(lime.graphics.Renderer)
|
||||||
|
|
||||||
|
|
||||||
class Window {
|
class Window {
|
||||||
@@ -53,6 +54,9 @@ class Window {
|
|||||||
public var handle:Dynamic;
|
public var handle:Dynamic;
|
||||||
#end
|
#end
|
||||||
|
|
||||||
|
private var setHeight:Int;
|
||||||
|
private var setWidth:Int;
|
||||||
|
|
||||||
|
|
||||||
public function new (config:Config) {
|
public function new (config:Config) {
|
||||||
|
|
||||||
@@ -73,6 +77,9 @@ class Window {
|
|||||||
|
|
||||||
public function create (application:Application):Void {
|
public function create (application:Application):Void {
|
||||||
|
|
||||||
|
setWidth = width;
|
||||||
|
setHeight = height;
|
||||||
|
|
||||||
#if js
|
#if js
|
||||||
|
|
||||||
if (Std.is (element, CanvasElement)) {
|
if (Std.is (element, CanvasElement)) {
|
||||||
@@ -111,9 +118,6 @@ class Window {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//__originalWidth = width;
|
|
||||||
//__originalHeight = height;
|
|
||||||
|
|
||||||
if (width == 0 && height == 0) {
|
if (width == 0 && height == 0) {
|
||||||
|
|
||||||
if (element != null) {
|
if (element != null) {
|
||||||
@@ -132,9 +136,6 @@ class Window {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//stageWidth = width;
|
|
||||||
//stageHeight = height;
|
|
||||||
|
|
||||||
if (canvas != null) {
|
if (canvas != null) {
|
||||||
|
|
||||||
canvas.width = width;
|
canvas.width = width;
|
||||||
@@ -147,8 +148,7 @@ class Window {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//__resize ();
|
handleDOMResize ();
|
||||||
//Browser.window.addEventListener ("resize", window_onResize);
|
|
||||||
|
|
||||||
if (element != null) {
|
if (element != null) {
|
||||||
|
|
||||||
@@ -291,10 +291,21 @@ class Window {
|
|||||||
|
|
||||||
case "resize":
|
case "resize":
|
||||||
|
|
||||||
eventInfo.type = WINDOW_RESIZE;
|
var cacheWidth = width;
|
||||||
eventInfo.width = Browser.window.innerWidth;
|
var cacheHeight = height;
|
||||||
eventInfo.height = Browser.window.innerHeight;
|
|
||||||
dispatch ();
|
handleDOMResize ();
|
||||||
|
|
||||||
|
if (width != cacheWidth || height != cacheHeight) {
|
||||||
|
|
||||||
|
eventInfo.type = WINDOW_RESIZE;
|
||||||
|
eventInfo.width = width;
|
||||||
|
eventInfo.height = height;
|
||||||
|
dispatch ();
|
||||||
|
|
||||||
|
Renderer.dispatch ();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
case "beforeunload":
|
case "beforeunload":
|
||||||
|
|
||||||
@@ -304,6 +315,72 @@ class Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function handleDOMResize ():Void {
|
||||||
|
|
||||||
|
var stretch = fullscreen || (setWidth == 0 && setHeight == 0);
|
||||||
|
|
||||||
|
if (element != null && (div == null || (div != null && stretch))) {
|
||||||
|
|
||||||
|
if (stretch) {
|
||||||
|
|
||||||
|
if (width != element.clientWidth || height != element.clientHeight) {
|
||||||
|
|
||||||
|
width = element.clientWidth;
|
||||||
|
height = element.clientHeight;
|
||||||
|
|
||||||
|
if (canvas != null) {
|
||||||
|
|
||||||
|
if (element != cast canvas) {
|
||||||
|
|
||||||
|
canvas.width = element.clientWidth;
|
||||||
|
canvas.height = element.clientHeight;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
div.style.width = element.clientWidth + "px";
|
||||||
|
div.style.height = element.clientHeight + "px";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
var scaleX = element.clientWidth / setWidth;
|
||||||
|
var scaleY = element.clientHeight / setHeight;
|
||||||
|
|
||||||
|
var currentRatio = scaleX / scaleY;
|
||||||
|
var targetRatio = Math.min (scaleX, scaleY);
|
||||||
|
|
||||||
|
if (canvas != null) {
|
||||||
|
|
||||||
|
if (element != cast canvas) {
|
||||||
|
|
||||||
|
canvas.style.width = setWidth * targetRatio + "px";
|
||||||
|
canvas.style.height = setHeight * targetRatio + "px";
|
||||||
|
canvas.style.marginLeft = ((element.clientWidth - (setWidth * targetRatio)) / 2) + "px";
|
||||||
|
canvas.style.marginTop = ((element.clientHeight - (setHeight * targetRatio)) / 2) + "px";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
div.style.width = setWidth * targetRatio + "px";
|
||||||
|
div.style.height = setHeight * targetRatio + "px";
|
||||||
|
div.style.marginLeft = ((element.clientWidth - (setWidth * targetRatio)) / 2) + "px";
|
||||||
|
div.style.marginTop = ((element.clientHeight - (setHeight * targetRatio)) / 2) + "px";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
#end
|
#end
|
||||||
|
|
||||||
|
|
||||||
@@ -344,6 +421,9 @@ class Window {
|
|||||||
|
|
||||||
public function resize (width:Int, height:Int):Void {
|
public function resize (width:Int, height:Int):Void {
|
||||||
|
|
||||||
|
setWidth = width;
|
||||||
|
setHeight = height;
|
||||||
|
|
||||||
#if (cpp || neko)
|
#if (cpp || neko)
|
||||||
lime_window_resize (handle, width, height);
|
lime_window_resize (handle, width, height);
|
||||||
#end
|
#end
|
||||||
|
|||||||
@@ -139,6 +139,8 @@ class Main extends Application {
|
|||||||
|
|
||||||
case OPENGL (gl):
|
case OPENGL (gl):
|
||||||
|
|
||||||
|
gl.viewport (0, 0, window.width, window.height);
|
||||||
|
|
||||||
var r = ((config.background >> 16) & 0xFF) / 0xFF;
|
var r = ((config.background >> 16) & 0xFF) / 0xFF;
|
||||||
var g = ((config.background >> 8) & 0xFF) / 0xFF;
|
var g = ((config.background >> 8) & 0xFF) / 0xFF;
|
||||||
var b = (config.background & 0xFF) / 0xFF;
|
var b = (config.background & 0xFF) / 0xFF;
|
||||||
|
|||||||
Reference in New Issue
Block a user