Skip to content

Instantly share code, notes, and snippets.

@b005t3r
Created August 18, 2016 16:08
Show Gist options
  • Save b005t3r/4a5e179975e77ec941bb588ef9a70626 to your computer and use it in GitHub Desktop.
Save b005t3r/4a5e179975e77ec941bb588ef9a70626 to your computer and use it in GitHub Desktop.
/**
* User: booster
* Date: 18/08/16
* Time: 9:25
*/
package com.bigdaddyscreations.display.components {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filesystem.File;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.html.HTMLLoader;
import flash.media.StageWebView;
import flash.net.URLRequest;
public class NativeHTMLViewer extends Sprite {
SYSTEM::desktop
private var _htmlLoader:HTMLLoader;
SYSTEM::mobile
private var _webView:StageWebView;
private var _path:String;
public function NativeHTMLViewer() {
SYSTEM::desktop {
//_htmlLoader = HTMLLoader.createRootWindow();
_htmlLoader = new HTMLLoader();
_htmlLoader.paintsDefaultBackground = false;
addEventListener(Event.ADDED_TO_STAGE, function(event:Event):void {
stage.addChild(_htmlLoader);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, true);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseDrag, true);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp, true);
});
addEventListener(Event.REMOVED_FROM_STAGE, function(event:Event):void {
stage.removeChild(_htmlLoader);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, true);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseDrag, true);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp, true);
});
var position:Point = new Point(-1, -1);
function onMouseDown(event:MouseEvent):void {
var rect:Rectangle = _htmlLoader.getBounds(stage);
if(! rect.contains(event.localX, event.localY))
return;
position.x = event.localX;
position.y = event.localY;
}
function onMouseDrag(event:MouseEvent):void {
if(position.x < 0 || position.y < 0)
return;
//var offsetX:Number = position.x - event.localX;
var offsetY:Number = position.y - event.localY;
_htmlLoader.scrollV += offsetY;
position.x = event.localX;
position.y = event.localY;
trace("offsetY:", offsetY, "_htmlLoader.scrollV:", _htmlLoader.scrollV);
}
function onMouseUp(event:MouseEvent):void {
position.setTo(-1, -1);
}
}
SYSTEM::mobile {
_webView = new StageWebView();
addEventListener(Event.ADDED_TO_STAGE, function(event:Event):void {
_webView.stage = this.stage;
});
addEventListener(Event.REMOVED_FROM_STAGE, function(event:Event):void {
_webView.stage = null;
});
}
}
public function setViewport(x:Number, y:Number, w:Number, h:Number):void {
SYSTEM::desktop {
_htmlLoader.x = x;
_htmlLoader.y = y;
_htmlLoader.width = w;
_htmlLoader.height = h;
}
SYSTEM::mobile {
_webView.viewPort = new Rectangle(x, y, w, h);
_webView.stage = stage;
if(_path != null)
loadLocal(_path);
}
}
public function loadLocal(path:String):void {
_path = path;
var manualFile:File, fileURI:String;
if(SYSTEM::android) {
manualFile = File.applicationStorageDirectory.resolvePath(_path);
fileURI = "file://" + manualFile.nativePath;
//fileURI = "https://www.google.com";
}
else {
manualFile = File.applicationDirectory.resolvePath(_path);
fileURI = "file://" + manualFile.nativePath;
}
trace("manualFile:", manualFile);
trace("fileURI:", fileURI);
SYSTEM::desktop {
_htmlLoader.load(new URLRequest(fileURI));
}
SYSTEM::mobile {
_webView.loadURL(fileURI);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment