Advertisement
teleias

uQuery

Apr 6th, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.57 KB | None | 0 0
  1. #pragma strict
  2.  
  3. import System.Text.RegularExpressions;
  4. import System.Collections.Generic;
  5. import System.Reflection;
  6.  
  7. public class uQuery extends Array {
  8.    
  9.     public var context : UnityEngine.Component;
  10.     public var rquickExpr : String = "^(?:#([\\w-]+)|(\\w+)|.([\\w-]+))$";
  11.     private var renderer : Component = null;
  12.    
  13.     public function uQuery(selector : System.Object) {
  14.         this(selector, null);
  15.     }
  16.    
  17.     // As originally init()
  18.     public function uQuery(selector : System.Object, context : System.Object) {
  19.         // Init array itself
  20.         //super();
  21.         if ( !selector ) {
  22.             return;
  23.         }
  24.    
  25.         // Handle jQuery(MonoBehaviour)
  26.         if( selector.GetType().IsSubclassOf(Component) ) {
  27.             this[0] = this.context = selector as UnityEngine.Component;
  28.             return;
  29.         }
  30.         // Handle jQuery(GameObject)
  31.         if( selector.GetType().IsSubclassOf(GameObject) ) {
  32.             this[0] = this.context = (selector as GameObject).transform;
  33.             return;
  34.         }
  35.    
  36.         // check different contexts
  37.         if(context != null) {
  38.    
  39.             // Context == jQuery
  40.             if( context.GetType() == this.GetType() ) {
  41.                 this.context = (selector as uQuery).context;
  42.    
  43.             // Context == MonoBehaviour
  44.             } else if( context.GetType().IsSubclassOf(Component) ) {
  45.                 this.context = context as Component;
  46.    
  47.             // Context == GameObject
  48.             } else if( context.GetType().IsSubclassOf(Component) ) {
  49.                 this.context = (context as GameObject).transform;
  50.             }
  51.         }
  52.    
  53.         // Handle HTML strings
  54.         if ( selector.GetType() == System.String ) {
  55.             // If we had id-kind-of-fast selector, we could do "^(#([\\w-]*)$)" match here.
  56.             // But since we don't we just skip and find whatever we want
  57.    
  58.             // todo: handle if we have context
  59.             //if(context) super(context);
  60.    
  61.             this.Find(selector.ToString());
  62.         }
  63.     }
  64.    
  65.    
  66.     public function Find(selector : String) {
  67.    
  68.         var match : System.Text.RegularExpressions.Match;
  69.         match = Regex.Match(selector, rquickExpr);
  70.    
  71.         var i : int = 0;
  72.         var a : Array = new Array();
  73.         if(match.Success) {
  74.    
  75.             // #ID
  76.             if(match.Groups[1].Value) {
  77.                 var path : String = this.GetAbsolutePathFrom(context);
  78.                 var foundGO : GameObject = GameObject.Find(path + match.Groups[1].Value);
  79.                 if(foundGO != null) a.Push(foundGO.transform);
  80.    
  81.             // TAG
  82.             } else if(match.Groups[2].Value) {
  83.                 var gameObjectsWithTag : GameObject[] = GameObject.FindGameObjectsWithTag(match.Groups[2].Value);
  84.                 if(context != null) {
  85.                     for(var children : Transform in context.GetComponentsInChildren.<Transform>()) {
  86.                         var children : GameObject = children.gameObject;
  87.                         for(var gameObjectWithTag : GameObject in gameObjectsWithTag) {
  88.                             if(match == children) a.Push(match);
  89.                         }
  90.                     }
  91.                 } else {
  92.                     for(; i<gameObjectsWithTag.length; ) {
  93.                         a.Push(gameObjectsWithTag[i++].transform);
  94.                     }
  95.                 }
  96.    
  97.             // .CLASS
  98.             } else if(match.Groups[3].Value) {
  99.                 var type : System.Type = System.Type.GetType(match.Groups[3].Value);
  100.                
  101.                 if(type == null) {
  102.                     var assembly : System.Reflection.Assembly = System.Reflection.Assembly.Load("UnityEngine");
  103.                     type = assembly.GetType("UnityEngine." + match.Groups[3].Value);
  104.                 }
  105.                
  106.                 if(type != null) {
  107.                     if(context != null) a = context.GetComponentsInChildren(type);
  108.                     else a = UnityEngine.Object.FindObjectsOfType(type);
  109.                 }
  110.    
  111.             }
  112.    
  113.         }
  114.    
  115.         for(i = 0; i < a.length; ) {
  116.             this[i] = a[i++];
  117.         }
  118.    
  119.     }
  120.    
  121.     // Helper for getting unity object's paths
  122.     public function GetAbsolutePathFrom(target : Component) : String {
  123.         if(target == null) return "";
  124.    
  125.         var ret : String = "";
  126.         var curr : Transform = target.transform;
  127.         while(curr.parent != null) {
  128.             ret += curr.name + "/";
  129.         }
  130.         ret = "/" + ret;
  131.    
  132.         return ret;
  133.     }
  134.    
  135.     public function each( /*obj : System.Object,*/ callback : Function ) {
  136.         var isObj : boolean = !this.GetType().IsSubclassOf(Array);
  137.         var i : int = 0;
  138.    
  139.         if( isObj ) {
  140.             // Confusing, in which case we would have an object?
  141.    
  142.         } else {
  143.             for( ; i < this.length; ) {
  144.                 //(this[i++] as System.Object).call(callback);
  145.                 callback.Call([this[i], this[i++] as Component]);
  146.             }
  147.         }
  148.         return this;
  149.     }
  150.    
  151.     public function isVisble() : boolean {
  152.        
  153.         var ret : boolean = false;
  154.        
  155.         if(context.GetComponent.<Renderer>()) {
  156.             ret = context.GetComponent.<Renderer>().enabled;
  157.         }
  158.         if(context.GetComponent.<GUITexture>()) {
  159.             ret = ret || context.GetComponent.<GUITexture>().enabled;
  160.         }
  161.        
  162.         return ret;
  163.     }
  164.    
  165.     public function children() : uQuery {
  166.         return uQuery(".Transform", context);
  167.     }
  168.    
  169.     public static function ajax(url : String) : uQueryXHR {
  170.         return uQuery.ajax(url, null);
  171.     }
  172.    
  173.     public static function ajax(settings : Boo.Lang.Hash) : uQueryXHR {
  174.         return uQuery.ajax(settings["url"] as String, settings);
  175.     }
  176.    
  177.     public static function ajax(url : String, settings : Boo.Lang.Hash) : uQueryXHR {
  178.         Debug.Log(
  179.             "Type: "+settings['type']+ " \n"+
  180.             "URL: "+settings['url'] + " \n" +
  181.             "ContentType: "+settings['contentType'] + " \n" +
  182.             "DataType: "+settings['dataType'] + " \n" +
  183.             "Data: "+settings['data'] + " \n" +
  184.             "Username: "+settings['username'] + " \n" +
  185.             "Password: "+settings['password'] + " \n"
  186.            
  187.             );
  188.         var xhr : uQueryXHR = new uQueryXHR();
  189.         var form : WWWForm = new WWWForm();
  190.         xhr.headers = form.headers;
  191.         xhr.form = form;
  192.        
  193.         xhr.url = url;
  194.        
  195.         if ( settings['beforeSend'] != null ) {
  196.             xhr.beforeSend = settings['beforeSend'] as Function;
  197.         }
  198.        
  199.         if( settings['cache'] == false ) {
  200.             xhr.cache = false;
  201.         }
  202.        
  203.         if ( settings['complete'] != null ) {
  204.             xhr.complete = settings['complete'] as Function;
  205.         }
  206.        
  207.         if ( settings['contentType'] != null ) {
  208.             xhr.headers['Content-Type'] = settings['contentType'];
  209.         }
  210.        
  211.         if ( settings['data'] != null ) {
  212.             if ( settings['data'].GetType() == Boo.Lang.Hash ) {
  213.                 xhr.data = settings['data'] as Boo.Lang.Hash;
  214.             }
  215.         }
  216.        
  217.         if ( settings['error'] != null ) {
  218.             xhr.error = settings['error'] as Function;
  219.         }
  220.        
  221.         if ( settings['headers'] != null ) {
  222.             for( var entry : System.Collections.DictionaryEntry in settings['headers'] as System.Collections.DictionaryEntry[] ) {
  223.                 xhr.headers.Add(entry.Key.ToString(), entry.Value.ToString());
  224.             }
  225.         }
  226.        
  227.         if ( settings['success'] != null ) {
  228.             xhr.success = settings['success'] as function(String, uQueryXHR);
  229.         }
  230.        
  231.         if ( settings['type'] != null ) {
  232.             xhr.type = settings['type'].ToString();
  233.         }
  234.        
  235.         if ( settings['username'] != null && settings['password'] != null ) {
  236.             xhr.headers.Add("Authorization", "Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(settings['username'] + ":" + settings['password'] )));
  237.         }
  238.        
  239.         xhr.run();
  240.         return xhr;
  241.        
  242.     }
  243.    
  244.     /* GET */
  245.    
  246.     // Todo: Find a way to use uQuery.get (on lowercase).
  247.     public static function Get(url : String) {
  248.         Get(url, {});
  249.     }
  250.    
  251.     public static function Get(url : String, data : Boo.Lang.Hash) {
  252.         Get(url, data, null);
  253.     }
  254.    
  255.     public static function Get(url : String, success : function(String, uQueryXHR)) {
  256.         Get(url, null, success);
  257.     }
  258.    
  259.     public static function Get(url : String, data : Boo.Lang.Hash, success : function(String, uQueryXHR)) {
  260.         Get(url, data, success, null);
  261.     }
  262.    
  263.     public static function Get(url : String, data : Boo.Lang.Hash, success : function(String, uQueryXHR), dataType : String) {
  264.         uQuery.ajax({
  265.             "url": url,
  266.             "data": data,
  267.             "success": success,
  268.             "dataType": dataType
  269.         });
  270.     }
  271.    
  272.    
  273.     /* POST */
  274.    
  275.     public static function post(url : String) {
  276.         post(url, {});
  277.     }
  278.    
  279.     public static function post(url : String, data : Boo.Lang.Hash) {
  280.         post(url, data, null);
  281.     }
  282.    
  283.     public static function post(url : String, data : Boo.Lang.Hash, success : function(String, uQueryXHR)) {
  284.         post(url, data, success, null);
  285.     }
  286.    
  287.     public static function post(url : String, data : Boo.Lang.Hash, success : function(String, uQueryXHR), dataType : String) {
  288.         uQuery.ajax({
  289.             "type": "POST",
  290.             "url": url,
  291.             "data": data,
  292.             "success": success,
  293.             "dataType": dataType,
  294.             "username": 'admin',
  295.             "password": 'admin',
  296.             "contentType": "application/json; charset=utf-8"
  297.         });
  298.     }
  299.    
  300.    
  301.     /**
  302.      * We'll use Unity's own animations.
  303.      *
  304.      * @NOTE:
  305.      * Propably will be changed to something like
  306.      * http://prime31.github.com/GoKit/
  307.      * because animations don't allow us to
  308.      * fade from current state but only from
  309.      * fixed states.
  310.      * Another approach would be to dynamically
  311.      * generate animations just before they are
  312.      * fired.
  313.      */
  314.    
  315.     public function animate( attr : String, prop : String, from : float, to: float, speed : float , callback : Function ) {
  316.         return this.each(function(_, ctx : Component) {
  317.        
  318.             var a : AnimationClip = new AnimationClip();
  319.             var affects : UnityEngine.Object[];
  320.             switch(attr.ToLower()) {
  321.                 case "color":
  322.                     a.SetCurve("", GUITexture, "m_Color." + prop, new AnimationCurve.Linear(0,from,speed,to));
  323.                     a.SetCurve("", Material, "_Color." + prop, new AnimationCurve.Linear(0,from,speed,to));
  324.                     affects = [ctx.GetComponent.<GUITexture>() as UnityEngine.Object, ctx.GetComponent.<Renderer>() as UnityEngine.Object];
  325.                     break;
  326.                 case "scale":
  327.                     affects = [];
  328.                     //a.SetCurve("", GUITexture, "m_Color." + prop, new AnimationCurve.Linear(0,from,speed,to));
  329.                     a.SetCurve("", Transform, "localScale." + prop, new AnimationCurve.Linear(0,from,speed,to));
  330.             }
  331.             var tmpName : String = "anim" + Time.realtimeSinceStartup;
  332.            
  333.             var cb : AnimationEvent = new AnimationEvent();
  334.             cb.messageOptions = SendMessageOptions.DontRequireReceiver;
  335.             cb.functionName = "AnimationEnded";
  336.             cb.time = speed;
  337.             cb.stringParameter = tmpName;
  338.             a.AddEvent(cb);
  339.            
  340.             var start : AnimationEvent = new AnimationEvent();
  341.             start.functionName = "AnimationStarted";
  342.             start.time = 0;
  343.             a.AddEvent(start);
  344.        
  345.             var anim : Animation = ctx.GetComponent.<Animation>();
  346.             if(anim == null) anim = ctx.gameObject.AddComponent.<Animation>();
  347.             anim.AddClip(a, tmpName);
  348.             anim.PlayQueued(tmpName);
  349.             anim.Play();
  350.            
  351.             var cbHolder : AnimationCallback = ctx.gameObject.AddComponent.<AnimationCallback>();
  352.             cbHolder.caller = tmpName;
  353.             cbHolder.holdCallerAlive = this;
  354.             cbHolder.callback = callback;
  355.             cbHolder.affects = affects;
  356.             cbHolder.setAnimationValues = function() {
  357.                 switch(attr.ToLower()) {
  358.                     case "scale":
  359.                         for(var fill : String in ["x", "y", "z"]) {
  360.                             if(fill == prop) return;
  361.                             var val : float = parseFloat(uQuery(ctx.transform).attr(fill, "localScale").ToString());
  362.                             anim.GetClip(tmpName).SetCurve("", Transform, "localScale." + fill, new AnimationCurve.Linear(0,val,speed,val));
  363.                             //Debug.Log(fill + " = " + );
  364.                         }
  365.                         break;
  366.                 }
  367.                
  368.             };
  369.         });
  370.     }
  371.    
  372.     // Show and hide functions
  373.     public function hide() {
  374.         return this.each(function(_, ctx : Component) {
  375.             if(ctx == null) return;
  376.            
  377.             uQuery(ctx).children().each(function(_, ctx) {
  378.                 if(ctx != null) uQuery(ctx).toggleVisible(false);
  379.             });
  380.            
  381.         });
  382.     }
  383.    
  384.     public function show() {
  385.         return this.each(function(_, ctx : Component) {
  386.             if(ctx == null) return;
  387.            
  388.             uQuery(ctx).children().each(function(_, ctx) {
  389.                 if(ctx != null) uQuery(ctx).toggleVisible(true);
  390.             });
  391.         });
  392.     }
  393.    
  394.     public function toggleVisible(visibility : boolean) {
  395.        
  396.         if(context.GetComponent.<Renderer>()) {
  397.             context.GetComponent.<Renderer>().enabled = visibility;
  398.         }
  399.         if(context.GetComponent.<GUITexture>()) {
  400.             context.GetComponent.<GUITexture>().enabled = visibility;
  401.         }
  402.        
  403.     }
  404.    
  405.     public function toggleVisible() {
  406.         this.toggleVisible(!this.isVisble());
  407.     }
  408.    
  409.     public function fadeIn() {
  410.         return this.animate( "color", "a", 0.0, 1.0, 3.0, function(_) { this.show(); });
  411.     }
  412.    
  413.     public function fadeOut() {
  414.         return this.animate( "color", "a", 1.0, 0.0, 3.0, function(_) { this.hide(); });
  415.     }
  416.    
  417.     public function slideOut() {
  418.         return this.animate( "scale", "y", 1.0, 0.0, 3.0, function(_) { this.show(); });
  419.     }
  420.    
  421.     public function slideIn() {
  422.         return this.animate( "scale", "y", 0.0, 1.0, 3.0, function(_) { this.show(); });
  423.     }
  424.    
  425.     // Basic bahviour of callbacks
  426.     //
  427.     // @TODO: Pass data with callback
  428.     //
  429.     public function bind(property : String, callback : Function) {
  430.         return this.on(property, callback);
  431.     }
  432.    
  433.     public function on(property : String, callback : Function) {
  434.         return this.each(function(_, ctx : Component) {
  435.             var event : uQueryEvent = ctx.gameObject.GetComponent.<uQueryEvent>();
  436.             if(event == null) event = ctx.gameObject.AddComponent.<uQueryEvent>();
  437.             event.add(property, callback);
  438.         });
  439.     }
  440.    
  441.     public function trigger(property : String) {
  442.         return this.each(function(_, ctx : Component) {
  443.             var event : uQueryEvent = ctx.gameObject.GetComponent.<uQueryEvent>();
  444.             if(event == null) return;
  445.             event.trigger(property);
  446.         });
  447.     }
  448.    
  449.     // Shorthands for callbacks
  450.     //
  451.     public function click(callback : Function) {
  452.         return this.bind("click", callback);
  453.     }
  454.    
  455.     public function mousedown(callback : Function) {
  456.         return this.bind("mousedown", callback);
  457.     }
  458.    
  459.     public function mouseup(callback : Function) {
  460.         return this.bind("mouseup", callback);
  461.     }
  462.    
  463.     public function mouseenter(callback : Function) {
  464.         return this.bind("mouseenter", callback);
  465.     }
  466.    
  467.     public function mouseleave(callback : Function) {
  468.         return this.bind("mouseleave", callback);
  469.     }
  470.    
  471.     public function mouseout(callback : Function) {
  472.         return this.bind("mouseleave", callback);
  473.     }
  474.    
  475.     public function mouseover(callback : Function) {
  476.         return this.bind("mouseover", callback);
  477.     }
  478.    
  479.     public function attr(property : String, value : Object) {
  480.         var e : PropertyInfo = context.GetType().GetProperty(property);
  481.         if(e != null) {
  482.             e.SetValue(context, value, null);
  483.         }
  484.     }
  485.    
  486.     public function attr(property : String) {
  487.         var e : PropertyInfo = context.GetType().GetProperty(property);
  488.         if(e != null) {
  489.             return e.GetValue(context, null);
  490.         }
  491.         return null;
  492.     }
  493.    
  494.     public function attr(property : String, holder : String) {
  495.         var e1 : PropertyInfo = context.GetType().GetProperty(holder);
  496.         if(e1 != null) {
  497.             var holderObj : Object = e1.GetValue(context, null);
  498.             var e2 : FieldInfo = holderObj.GetType().GetField(property);
  499.             if(e2 != null) {
  500.                 return e2.GetValue(holderObj);
  501.             }
  502.         }
  503.         return null;
  504.     }
  505.    
  506.     public function addClass(className : String) {
  507.         return this.each(function(_, ctx : Component) {
  508.             if(ctx == null) return;
  509.             //UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(ctx.gameObject, "", className);
  510.             //var c : Component = ctx.gameObject.GetComponent(className);
  511.         });
  512.     }
  513.    
  514.     public function removeClass(className : String) {
  515.         return this.each(function(_, ctx : Component) {
  516.             if(ctx == null) return;
  517.             UnityEngine.Object.Destroy(ctx.gameObject.GetComponent(className));
  518.         });
  519.     }
  520.    
  521.     public function toggleClass(className : String) {
  522.         return this.each(function(_, ctx : Component) {
  523.             if(ctx == null) return;
  524.             //var c : Component = ctx.gameObject.GetComponent(className);
  525.             //if(c == null) UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(c);
  526.             //if(c == null) UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(ctx.gameObject, "assets/plugins/uQuery.js(523,39)", (className));
  527.             //else UnityEngine.Object.Destroy(c);
  528.         });
  529.     }
  530.    
  531.     public function hasClass(className : String) : boolean {
  532.         var ctx : Component = context;
  533.         if(ctx == null && this.length > 0) {
  534.             ctx = this[0] as Component;
  535.         }
  536.         if(ctx == null) return false;
  537.         return ctx.gameObject.GetComponent(className) != null;
  538.     }
  539.  
  540. }
  541.  
  542. public class AnimationCallback extends MonoBehaviour {
  543.    
  544.     public var setAnimationValues : Function;
  545.     public var callback : Function;
  546.     public var caller : Object;
  547.     public var holdCallerAlive : uQuery;
  548.     public var affects : UnityEngine.Object[];
  549.    
  550.     public function Awake() {
  551.         this.hideFlags = HideFlags.HideAndDontSave;
  552.     }
  553.    
  554.     public function AnimationStarted() : void {
  555.         for(var a : UnityEngine.Object in affects) {
  556.             if(a != null) {
  557.                 uQuery(a).attr("enabled", true);
  558.             }
  559.         }
  560.         if(setAnimationValues != null) setAnimationValues();
  561.     }
  562.    
  563.     public function AnimationEnded(called : String) : void {
  564.         if(called != caller) return;
  565.         if(callback != null) callback(holdCallerAlive);
  566.         GetComponent.<Animation>().RemoveClip(called);
  567.         if(GetComponent.<Animation>().GetClipCount() == 0) Destroy(GetComponent.<Animation>());
  568.         Destroy(this);
  569.     }
  570. }
  571.  
  572. public class uQueryEvent extends MonoBehaviour {
  573.    
  574.     private var handlers : Dictionary.<String, Array> = new Dictionary.<String, Array>();
  575.    
  576.     public function Awake() {
  577.         this.hideFlags = HideFlags.HideAndDontSave;
  578.     }
  579.    
  580.     public function add(types : String, handler : Function) {
  581.         for(var type : String in types.Split(" "[0])) {
  582.             var currHandler : Array;
  583.             try {
  584.                 currHandler = handlers[type];
  585.             } catch(e) {
  586.                 handlers[type] = currHandler = new Array();
  587.                 currHandler.Push(handler);
  588.             }
  589.         }
  590.     }
  591.    
  592.     public function remove(types : String, handler : Function) {
  593.         for(var type : String in types.Split(" "[0])) {
  594.             var currHandler : Array = handlers[type];
  595.             if(currHandler == null) continue;
  596.            
  597.             // if no handler provided, clear all
  598.             if(handler != null) {
  599.                 handlers['type'] = new Array();
  600.             } else {
  601.                 for( var i : int = 0; i < currHandler.length; i++ ) {
  602.                     if(currHandler[i] != null && currHandler[i] == handler) currHandler.Splice(i, 1);
  603.                 }
  604.             }
  605.         }      
  606.     }
  607.    
  608.     public function trigger(eventType : String) {
  609.         var currHandlers : Array;
  610.         try {
  611.             currHandlers = handlers[eventType];
  612.         } catch(e) {
  613.             return;
  614.         }
  615.        
  616.         for(var f : Function in currHandlers.ToBuiltin(Function) as Function[]) {
  617.             if(f != null) f(this);
  618.         }
  619.     }
  620.    
  621.     public function Update() {
  622.         trigger("update");
  623.     }
  624.    
  625.     public function LateUpdate() {
  626.         trigger("lateupdate");
  627.     }
  628.    
  629.     public function FixedUpdate() {
  630.         trigger("fixedupdate");
  631.     }
  632.    
  633.     public function OnMouseEnter() {
  634.         trigger("mouseenter");
  635.     }
  636.    
  637.     public function OnMouseOver() {
  638.         trigger("mouseover");
  639.     }
  640.    
  641.     public function OnMouseExit() {
  642.         trigger("mouseleave");
  643.     }
  644.    
  645.     public function OnMouseDown() {
  646.         trigger("mousedown");
  647.     }
  648.    
  649.     public function OnMouseUp() {
  650.         trigger("mouseup");
  651.     }
  652.    
  653.     public function OnMouseUpAsButton() {
  654.         trigger("click");
  655.     }
  656.    
  657.     // TODO: Collision triggers
  658.     // TODO: keyboard click triggers
  659. }
  660.  
  661. public class uQueryXHR extends System.Object {
  662.    
  663.     // TODO: Cleanup other callbacks than success function
  664.    
  665.     public var success : function(String, uQueryXHR);
  666.     public var complete : Function;
  667.     public var error : Function;
  668.     public var beforeSend : Function;
  669.     public var form : WWWForm;
  670.     public var cache : boolean = true;
  671.     public var data : Boo.Lang.Hash;
  672.     public var headers : System.Collections.Generic.Dictionary.<String,String>;
  673.     public var type : String = "GET";
  674.     public var www : WWW;
  675.     public var url : String;
  676.     public var _always : Function;
  677.     public var _then : Function;
  678.    
  679.     public function done(callback : function(String, uQueryXHR)) {
  680.         this.success = callback;
  681.         return this;
  682.     }
  683.     public function fail(callback : Function) {
  684.         this.error = callback;
  685.         return this;
  686.     }
  687.     public function always(callback : Function) {
  688.         this._always = callback;
  689.         return this;
  690.     }
  691.     public function then(callback : Function) {
  692.         this._then = callback;
  693.         return this;
  694.     }
  695.     public function run() {
  696.         // Inject
  697.         (MonoBehaviour.FindObjectOfType(MonoBehaviour) as MonoBehaviour).StartCoroutine(_run());
  698.     }
  699.    
  700.     private function _run() {
  701.         if (this.type == "GET") {
  702.             if (!url.Contains("?")) {
  703.                 this.url += "?";
  704.             }
  705.            
  706.             for( var entry : System.Collections.DictionaryEntry in this.data ) {
  707.                 this.url += "&" + entry.Key.ToString() + "=" + entry.Value.ToString();
  708.             }
  709.            
  710.             if (beforeSend != null) beforeSend(this);
  711.             this.www = new WWW(url);
  712.         } else if ( this.type == "POST" ) {
  713.            
  714.             for( var entry : System.Collections.DictionaryEntry in this.data ) {
  715.                 this.form.AddField(entry.Key.ToString(), entry.Value.ToString());
  716.             }
  717.            
  718.             if (beforeSend != null) beforeSend(this);
  719.             this.www = new WWW(this.url, this.form.data, this.headers);
  720.         }
  721.        
  722.         yield www;
  723.        
  724.         if(!String.IsNullOrEmpty(this.www.error)) {
  725.             if (this.error != null) this.error(this);
  726.         } else {
  727.             if (this.success != null) this.success(this.www.text, this);
  728.             if (this.complete != null) this.complete(this);
  729.         }
  730.        
  731.        
  732.         if (this._then != null) this._then(this);
  733.         if (this._always != null) this._always(this);
  734.        
  735.     }
  736. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement