Created
January 7, 2017 19:56
-
-
Save aib/ba6898a8ff0bdea419c0e141ad150b62 to your computer and use it in GitHub Desktop.
My old ActionScript Utility file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A static class with misc. utilities | |
* | |
* aib, 20071120 | |
*/ | |
package | |
{ | |
import flash.display.DisplayObject; | |
import flash.display.Graphics; | |
import flash.geom.Rectangle; | |
import flash.filters.BlurFilter; | |
public class aibUtils | |
{ | |
//Because I'm tired of having to create a new one each time | |
public static var dummyFilter:BlurFilter = new BlurFilter(0, 0, 0); | |
//Escape HTML entities | |
public static function EscapeHTML(msg:String):String | |
{ | |
msg = msg.split("&").join("&"); | |
msg = msg.split("<").join("<"); | |
msg = msg.split(">").join(">"); | |
msg = msg.split("\'").join("'"); | |
msg = msg.split("\"").join("""); | |
return msg; | |
} | |
//Unescape HTML entities | |
public static function UnescapeHTML(msg:String):String | |
{ | |
msg = msg.split(""").join("\""); | |
msg = msg.split("'").join("\'"); | |
msg = msg.split(">").join(">"); | |
msg = msg.split("<").join("<"); | |
msg = msg.split("&").join("&"); | |
return msg; | |
} | |
//Converts seconds to [HH:]MM:SS format. | |
public static function TimeFormat(t:uint, nDigits:uint=2):String | |
{ | |
var hours:uint, minutes:uint, seconds:uint; | |
var sTime:String = ""; | |
seconds = t % 60; | |
t /= 60; | |
minutes = t % 60; | |
t /= 60; | |
if (nDigits >= 3) { | |
hours = t; | |
sTime = ((hours >= 10) ? "" : "0") + hours + ":"; | |
} | |
sTime += ((minutes >= 10) ? "" : "0") + minutes + ":"; | |
sTime += ((seconds >= 10) ? "" : "0") + seconds; | |
return sTime; | |
} | |
//Similar to print_r in PHP | |
public static function PrintArray(arr:Array, prefix:String=""):void | |
{ | |
for(var key:String in arr) { | |
if (arr[key] == null) { | |
trace(prefix + key + " -> null"); | |
} else if (arr[key] is Array) { | |
trace(prefix + key + " -> Array("); | |
PrintArray(arr[key], prefix+" "); | |
trace(prefix + ")"); | |
} else { | |
trace(prefix + key + " -> " + arr[key].toString()); | |
} | |
} | |
} | |
public static function ObjectToString(obj:Object, prefix:String=""):String | |
{ | |
if (obj == null) return "null"; | |
var s:String = ""; | |
for(var key:String in obj) { | |
if (obj[key] == null) { | |
s += (prefix + key + " -> null\n"); | |
} | |
else if (obj[key] is Array) { | |
s += (prefix + key + " -> Array(\n"); | |
s += ObjectToString(obj[key], prefix+" ") + "\n"; | |
s += (prefix + ")\n"); | |
} | |
else if (obj[key] is Number) { | |
s += (prefix + key + " -> " + obj[key].toString() + "\n"); | |
} | |
else if (obj[key] is XML) { | |
s += (prefix + key + " -> XML(" + obj[key].toString() + ")\n"); | |
} | |
else if (obj[key] is Object) { | |
s += (prefix + key + " -> Object(\n"); | |
s += ObjectToString(obj[key], prefix+" ") + "\n"; | |
s += (prefix + ")\n"); | |
} | |
//should never reach here, but what the hell... | |
else { | |
s += (prefix + key + " -> ??? (" + obj[key].toString() + ")\n"); | |
} | |
} | |
return s; | |
} | |
//Returns an integer between rMin and rMax (inclusive) | |
public static function RandInt(rMin:int, rMax:int):int | |
{ | |
return rMin + int(Math.floor((Math.random() * (rMax - rMin + 1)))); | |
} | |
//Shuffles a numerically indexed (0~N) array, given arrSize=N+1 | |
public static function ShuffleArray(arr:Array, arrSize:uint):void | |
{ | |
var temp:Object; | |
for (var shuf:uint=arrSize-1; shuf>0; shuf--) { | |
var n:uint = RandInt(0, shuf); | |
temp = arr[shuf]; | |
arr[shuf] = arr[n]; | |
arr[n] = temp; | |
} | |
} | |
//Draws a rectangle outline inside a given Graphics object | |
public static function DrawRectBounds(g:Graphics, r:Rectangle):void | |
{ | |
g.moveTo(r.x, r.y ); | |
g.lineTo(r.x + r.width, r.y ); | |
g.lineTo(r.x + r.width, r.y + r.height); | |
g.lineTo(r.x, r.y + r.height); | |
g.lineTo(r.x, r.y ); | |
} | |
//Draws a full rectangle inside a given Graphics object | |
public static function DrawRect(g:Graphics, r:Rectangle, clr:uint, calpha:Number=1.0):void | |
{ | |
g.beginFill(clr, calpha); | |
g.drawRect(r.x, r.y, r.width, r.height); | |
g.endFill(); | |
} | |
//Initializes a given DisplayObject using the parameters of a given Rectangle | |
public static function SetupDORect(d:DisplayObject, r:Rectangle):void | |
{ | |
d.x = r.x; | |
d.y = r.y; | |
d.width = r.width; | |
d.height = r.height; | |
} | |
//Initializes a given DisplayObject given x, y, width and height parameters | |
public static function SetupDOxywh(d:DisplayObject, x:int, y:int, w:int, h:int):void | |
{ | |
d.x = x; | |
d.y = y; | |
d.width = w; | |
d.height = h; | |
} | |
//Basic rectangle bounds checking | |
public static function PointOverxywh(px:int, py:int, x:int, y:int, w:int, h:int):Boolean | |
{ | |
if ((px >= x) && (px < (x+w)) && (py >= y) && (py < (y+h))) | |
return true; | |
else | |
return false; | |
} | |
//Check if point is over Rectangle | |
public static function PointOverRect(px:int, py:int, r:Rectangle):Boolean | |
{ | |
return PointOverxywh(px, py, r.x, r.y, r.width, r.height); | |
} | |
//Check if point is over DisplayObject. Traverse up the display object tree if requested | |
public static function PointOverDO(px:int, py:int, d:DisplayObject, traverse:Boolean=false):Boolean | |
{ | |
if (traverse) { | |
var p:DisplayObject = d; | |
var dox:int = 0; | |
var doy:int = 0; | |
while (p) { | |
dox += p.x; | |
doy += p.y; | |
p = p.parent; | |
} | |
return PointOverxywh(px, py, dox, doy, d.width, d.height); | |
} else { | |
return PointOverxywh(px, py, d.x, d.y, d.width, d.height); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment