Lib.hx 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package flash;
  23. class Lib {
  24. public static var current : flash.display.MovieClip;
  25. public inline static function getTimer() : Int {
  26. return untyped __global__["flash.utils.getTimer"]();
  27. }
  28. public static function eval( path : String ) : Dynamic {
  29. var p = path.split(".");
  30. var fields = new Array();
  31. var o : Dynamic = null;
  32. while( p.length > 0 ) {
  33. try {
  34. o = untyped __global__["flash.utils.getDefinitionByName"](p.join("."));
  35. } catch( e : Dynamic ) {
  36. fields.unshift(p.pop());
  37. }
  38. if( o != null )
  39. break;
  40. }
  41. for( f in fields ) {
  42. if( o == null ) return null;
  43. o = untyped o[f];
  44. }
  45. return o;
  46. }
  47. public static function getURL( url : flash.net.URLRequest, ?target : String ) {
  48. var f = untyped __global__["flash.net.navigateToURL"];
  49. if( target == null )
  50. f(url);
  51. else
  52. (cast f)(url,target);
  53. }
  54. public static function fscommand( cmd : String, ?param : String ) {
  55. untyped __global__["flash.system.fscommand"](cmd,if( param == null ) "" else param);
  56. }
  57. public static function trace( arg : Dynamic ) {
  58. untyped __global__["trace"](arg);
  59. }
  60. public static function describeType( value : Dynamic ) : flash.xml.XML {
  61. return untyped __global__["flash.utils.describeType"](value);
  62. }
  63. public static function attach( name : String ) : flash.display.MovieClip {
  64. var cl = untyped __as__(__global__["flash.utils.getDefinitionByName"](name),Class);
  65. return untyped __new__(cl);
  66. }
  67. public inline static function as<T>( v : Dynamic, c : Class<T> ) : Null<T> {
  68. return untyped __as__(v,c);
  69. }
  70. public static function redirectTraces() {
  71. if (flash.external.ExternalInterface.available)
  72. haxe.Log.trace = traceToConsole;
  73. }
  74. static function traceToConsole(v : Dynamic, ?inf : haxe.PosInfos ) {
  75. var type = if( inf != null && inf.customParams != null ) inf.customParams[0] else null;
  76. if( type != "warn" && type != "info" && type != "debug" && type != "error" )
  77. type = if( inf == null ) "error" else "log";
  78. var str = if( inf == null ) "" else inf.fileName + ":" + inf.lineNumber + " : ";
  79. try str += Std.string(v) catch( e : Dynamic ) str += "????";
  80. str = str.split("\\").join("\\\\");
  81. flash.external.ExternalInterface.call("console."+type,str);
  82. }
  83. }