Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
adding mercury lib
Browse files Browse the repository at this point in the history
  • Loading branch information
nexussays committed May 18, 2018
1 parent 4af789d commit a9b043b
Show file tree
Hide file tree
Showing 14 changed files with 1,884 additions and 42 deletions.
45 changes: 45 additions & 0 deletions src/nexus/ClassFactory.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright M. Griffie <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package nexus
{

/**
* A factory used to create instances of an object given a Class and an optional
* Object to assign public properties/variables on the instantiated instance.
*/
public class ClassFactory implements IFactory
{
private var m_class : Class;
private var m_properties : Object;

public function ClassFactory(source:Class, properties:Object = null)
{
m_class = source;
m_properties = properties;
}

public function get type():Class { return m_class; }

public function get properties():Object { return m_properties; }
public function set properties(value:Object):void
{
m_properties = value;
}

public function create():*
{
var instance:* = new m_class();
if(m_properties != null)
{
for(var property:String in m_properties)
{
instance[property] = m_properties[property];
}
}
return instance;
}
}
}
88 changes: 88 additions & 0 deletions src/nexus/math/MathHelper.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright M. Griffie <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package nexus.math
{

/**
* A collection of math utility methods.
*/
public class MathHelper
{
public static function clamp(value : Number, min : Number, max : Number) : Number
{
value = (value > max) ? max : value;
value = (value < min) ? min : value;
return value;
}

public static function sign(value : Number) : int
{
if (value == 0)
{
return 0;
}
return value > 0 ? 1 : -1;
}

public static function distance(value1 : Number, value2 : Number) : Number
{
return Math.abs(value1 - value2);
}

public static function toDegrees(radians : Number) : Number
{
return (radians * 57.29578);
}

public static function toRadians(degrees : Number) : Number
{
return (degrees * 0.01745329);
}

public static function wrapAngleRadians(angle : Number) : Number
{
while(angle <= -3.141593)
{
angle += 6.283185;
}
while(angle > 3.141593)
{
angle -= 6.283185;
}
return angle;
}

public static function wrapAngleDegrees(angle : Number) : Number
{
while(angle <= -180)
{
angle += 360;
}
while(angle > 180)
{
angle -= 360;
}
return angle;
}

/**
* Compares the sign value of source to compareTo and returns source or -source as appropriate
* @param source The value to check the sign of and return
* @param compareTo The value to compare against
*/
static public function matchSign(source:Number, compareTo:Number):Number
{
if(compareTo < 0)
{
return source <= 0 ? source : -source;
}
else
{
return source >= 0 ? source : -source
}
}
}
}
84 changes: 42 additions & 42 deletions src/nexus/net/HttpRequest.as
Original file line number Diff line number Diff line change
Expand Up @@ -19,104 +19,104 @@ public class HttpRequest
//--------------------------------------
// CLASS CONSTANTS
//--------------------------------------

//--------------------------------------
// INSTANCE VARIABLES
//--------------------------------------

/**
* The underlying URLStream for this request
* @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLStream.html
*/
private var m_stream:URLStream;
private var m_url:URLRequest;

//
// transient data per send
//

private var m_response : HttpResponse;
private var m_onCompleteCallback:Function;

//--------------------------------------
// CONSTRUCTOR
//--------------------------------------

public function HttpRequest()
{
m_url = new URLRequest();
m_url.method = URLRequestMethod.GET;

m_stream = new URLStream();
}

//--------------------------------------
// GETTER/SETTERS
//--------------------------------------

public function get url():String { return m_url.url; }
public function set url(value:String):void
{
m_url.url = value;
}

public function get method():String { return m_url.method; }
public function set method(value:String):void
{
m_url.method = value;
}

public function get body():Object { return m_url.data; }
public function set body(value:Object):void
{
m_url.data = value;
}

public function get headers():Array { return m_url.requestHeaders; }

//--------------------------------------
// PUBLIC INSTANCE METHODS
//--------------------------------------

public function send(callback:Function/*HttpRequest*/):void
{
if(m_stream.connected)
{
throw new IllegalOperationError("Cannot send. HttpRequest already in progress.");
}

m_onCompleteCallback = callback;
m_response = new HttpResponse();
//TODO: Provide some mechanism to get progress of the response; then set the response body to the stream while it loads
//m_response.setBody(m_stream);

//
// Add event listeners on each send(), so we can fully tear down when complete and avoid possible memory leaks
//

//Dispatched when a load operation starts.
m_stream.addEventListener(Event.OPEN, stream_open);

//Dispatched when data has loaded successfully.
m_stream.addEventListener(Event.COMPLETE, stream_complete);

//Dispatched when data is received as the download operation progresses.
m_stream.addEventListener(ProgressEvent.PROGRESS, stream_progress);

//Dispatched if a call to URLStream.load() attempts to access data over HTTP, and Flash Player
//or Adobe AIR is able to detect and return the status code for the request.
m_stream.addEventListener(HTTPStatusEvent.HTTP_STATUS, stream_status);

//Dispatched if a call to the URLStream.load() method attempts to access data over HTTP and Adobe AIR
//is able to detect and return the status code for the request.
m_stream.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, stream_responseStatus);

//Dispatched when an input/output error occurs that causes a load operation to fail.
m_stream.addEventListener(IOErrorEvent.IO_ERROR, stream_error);

//Dispatched if a call to URLStream.load() attempts to load data from a server outside the security sandbox.
m_stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, stream_error);

try
{
m_stream.load(m_url);
Expand Down Expand Up @@ -145,15 +145,15 @@ public class HttpRequest
complete(argumentError.getStackTrace() || argumentError.toString());
}
}

public function cancel():void
{
if(m_stream.connected)
{
m_stream.close();
}
}

public function setBasicAuthentication(user:String, password:String):void
{
/*
Expand All @@ -167,16 +167,16 @@ public class HttpRequest
m_authHeader.value = "Basic " + Base64.encode(auth, false);
//*/
}

//--------------------------------------
// PRIVATE & PROTECTED INSTANCE METHODS
//--------------------------------------

private function stream_open(e:Event):void
{
trace(e);
}

private function stream_status(e:HTTPStatusEvent):void
{
//only set if a valid status is returned & one is not already set (stream_responseStatus supercedes this method)
Expand All @@ -185,36 +185,36 @@ public class HttpRequest
m_response.setStatus(e.status);
}
}

private function stream_responseStatus(e:HTTPStatusEvent):void
{
m_response.setStatus(e.status);
m_response.setUrl(e.responseURL);
m_response.addHeaders(e.responseHeaders);
}

private function stream_progress(e:ProgressEvent):void
{
m_response.setBytesLoaded(e.bytesLoaded);
m_response.setBytesTotal(e.bytesTotal);
}

private function stream_complete(e:Event):void
{
complete(m_stream);
}

private function stream_error(e:ErrorEvent):void
{
complete(e.text);
}

private function complete(responseBody:Object):void
{
//
// set response body
//

//set response body to a new ByteArray to break the reference to the stream
var bytes : ByteArray = new ByteArray();
if(responseBody == m_stream || responseBody is IDataInput)
Expand All @@ -229,34 +229,34 @@ public class HttpRequest
{
throw new ArgumentError("Invalid response body type");
}

//TODO: set loaded & total here? What about the case of errors. I think the idea is to make sure any visuals or logic
//that are waiting for bytesLoaded to equal bytesTotal to complete properly, but what about the error case? Don't we
//want to know that we're complete but the bytes don't match?
m_response.setBytesLoaded(bytes.length);
m_response.setBytesTotal(bytes.length);

m_response.setBody(bytes);

//
// clean up stream
//

m_stream.removeEventListener(Event.OPEN, stream_open);
m_stream.removeEventListener(Event.COMPLETE, stream_complete);
m_stream.removeEventListener(ProgressEvent.PROGRESS, stream_progress);
m_stream.removeEventListener(HTTPStatusEvent.HTTP_STATUS, stream_status);
m_stream.removeEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, stream_responseStatus);
m_stream.removeEventListener(IOErrorEvent.IO_ERROR, stream_error);
m_stream.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, stream_error);

//close stream after listeners have been removed so none are triggered
this.cancel();

//
// invoke callback
//

m_onCompleteCallback(m_response);
m_onCompleteCallback = null;
m_response = null;
Expand Down
Loading

0 comments on commit a9b043b

Please sign in to comment.