Example #1
0
        void DistributeUnityEvent(JObject ev)
        {
            string eventName = (string)ev["event"];

            //Debug.Log("Bridge: DistributeUnityEvent: eventName: " + eventName + " ev: " + ev);

            JObject data = ev["data"] as JObject;

            switch (eventName)
            {
            case "StartedJS": {
                //Debug.Log("Bridge: DistributeUnityEvent: StartedJS: " + ev);
                startedJS = true;

                break;
            }

            case "Log": {
                string line = (string)data["line"];

                Debug.Log("Bridge: DistributeUnityEvent: Log: line: " + line);

                break;
            }

            case "Create": {
                string  id                 = (string)data["id"];
                string  prefab             = data.GetString("prefab");
                string  component          = data.GetString("component");
                JArray  preEvents          = data.GetArray("preEvents");
                string  parent             = data.GetString("parent");
                bool    worldPositionStays = data.GetBoolean("worldPositionStays", true);
                JObject update             = data.GetObject("update");
                JObject interests          = data.GetObject("interests");
                JArray  postEvents         = data.GetArray("postEvents");

                //Debug.Log("Bridge: DistributeUnityEvent: Create: id: " + id + " prefab: " + prefab + " component: " + component + " preEvents: " + preEvents + " parent: " + parent + " worldPositionStay: " + worldPositionStays + " update: " + update + " interests: " + interests + " postEvents: " + postEvents);

                GameObject instance = null;
                if (string.IsNullOrEmpty(prefab))
                {
                    instance = new GameObject();
                }
                else
                {
                    GameObject prefabObject = Resources.Load <GameObject>(prefab);
                    //Debug.Log("Bridge: DistributeUnityEvent: Create: prefab: " + prefab + " prefabObject: " + prefabObject);
                    if (prefabObject == null)
                    {
                        Debug.LogError("Bridge: DistributeUnityEvent: Create: Can't find prefab: " + prefab);
                        return;
                    }
                    instance = Instantiate(prefabObject);
                    //Debug.Log("Bridge: DistributeUnityEvent: Create: instance: " + instance);
                    if (instance == null)
                    {
                        Debug.LogError("Bridge: DistributeUnityEvent: Create: Can't instantiate prefab: " + prefab + " prefabObject: " + prefabObject);
                        return;
                    }
                }

                BridgeObject bridgeObject;

                if (string.IsNullOrEmpty(component))
                {
                    bridgeObject = instance.GetComponent <BridgeObject>();
                    //Debug.Log("Bridge: DistributeUnityEvent: Create: bridgeObject: " + bridgeObject);

                    if (bridgeObject == null)
                    {
                        bridgeObject = instance.AddComponent <BridgeObject>();
                    }
                }
                else
                {
                    Type componentType = Type.GetType(component);

                    if (componentType == null)
                    {
                        componentType = Type.GetType("UnityJS." + component);
                    }

                    if (componentType == null)
                    {
                        componentType = Type.GetType("UnityEngine." + component);
                    }

                    if (componentType == null)
                    {
                        Debug.LogError("Bridge: DistributeUnityEvent: Create: undefined component class: " + component);
                        return;
                    }

                    if ((componentType != typeof(BridgeObject)) &&
                        (!componentType.IsSubclassOf(typeof(BridgeObject))))
                    {
                        Debug.LogError("Bridge: DistributeUnityEvent: Create: component class is not subclass of BridgeObject: " + component);
                        return;
                    }

                    bridgeObject = (BridgeObject)instance.AddComponent(componentType);
                }

                instance.name       = id;
                bridgeObject.id     = id;
                bridgeObject.bridge = this;
                bridgeObject.AddInterests(interests);
                objectToID[bridgeObject] = id;
                idToObject[id]           = bridgeObject;

                //Debug.Log("Bridge: DistributeUnityEvent: Create: created, position: " + bridgeObject.transform.position.x + " " + bridgeObject.transform.position.y + " " + bridgeObject.transform.position.z + " bridgeObject: " + bridgeObject, bridgeObject);

                if (preEvents != null)
                {
                    bridgeObject.HandleEvents(preEvents);
                }

                if (!String.IsNullOrEmpty(parent))
                {
                    //Debug.Log("BridgeObject: DistributeUnityEvent: Create: parent: bridgeObject: " + bridgeObject + " parent: " + parent);

                    Accessor accessor = null;
                    if (!Accessor.FindAccessor(
                            bridgeObject,
                            parent,
                            ref accessor))
                    {
                        Debug.LogError("Bridge: DistributeUnityEvent: Create: parent: can't find accessor for bridgeObject: " + bridgeObject + " parent: " + parent);
                    }
                    else
                    {
                        object obj = null;
                        if (!accessor.Get(ref obj))
                        {
                            if (!accessor.conditional)
                            {
                                Debug.LogError("Bridge: DistributeUnityEvent: Create: parent: can't get accessor: " + accessor + " bridgeObject: " + bridgeObject + " parent: " + parent);
                            }
                        }
                        else
                        {
                            Component comp = obj as Component;
                            if (comp == null)
                            {
                                if (!accessor.conditional)
                                {
                                    Debug.LogError("Bridge: DistributeUnityEvent: Create: parent: expected Component obj: " + obj + " this: " + this + " parent: " + parent);
                                }
                            }
                            else
                            {
                                GameObject go    = comp.gameObject;
                                Transform  xform = go.transform;
                                //Debug.Log("Bridge: DistributeUnityEvent: Create: parent: xform: " + xform + " parent: " + parent + " worldPositionStays: " + worldPositionStays);

                                bridgeObject.transform.SetParent(xform, worldPositionStays);
                            }
                        }
                    }
                }

                if (update != null)
                {
                    bridgeObject.LoadUpdate(update);
                }

                bridgeObject.SendEventName("Created");

                if (postEvents != null)
                {
                    bridgeObject.HandleEvents(postEvents);
                }

                //Debug.Log("Bridge: DistributeUnityEvent: Create: done, position: " + bridgeObject.transform.position.x + " " + bridgeObject.transform.position.y + " " + bridgeObject.transform.position.z + " bridgeObject: " + bridgeObject);

                break;
            }

            case "Query": {
                JObject dataObject = (JObject)data;
                JObject query      = (JObject)dataObject["query"];
                string  callbackID = (string)dataObject["callbackID"];
                //Debug.Log("Bridge: DistributeUnityEvent: Query: dataObject: " + dataObject + " query: " + query + " callbackID: " + callbackID + " bridge: " + bridge);

                JToken idToken  = ev["id"];
                string idString = idToken.IsString() ? (string)idToken : null;
                JArray idArray  = idToken.IsArray() ? (JArray)idToken : null;
                bool   isSingle = idString != null;
                if ((idString == null) &&
                    (idArray == null))
                {
                    Debug.Log("Bridge: DistributeUnityEvent: Query: bad id: " + idToken + " data: " + data);
                    break;
                }

                JArray queryResults = new JArray();

                for (int i = 0, n = isSingle ? 1 : idArray.ArrayLength(); i < n; i++)
                {
                    string id = isSingle ? idString : (string)idArray[i];
                    if (id == null)
                    {
                        //Debug.Log("Bridge: DistributeUnityEvent: empty id!");
                        continue;
                    }

                    //Debug.Log("Bridge: DistributeUnityEvent: id: " + id + " ev: " + ev);

                    if (string.IsNullOrEmpty(id))
                    {
                        Debug.LogError("Bridge: DistributeUnityEvent: undefined id on eventName: " + eventName + " ev: " + ev);
                        continue;
                    }

                    if (!idToObject.ContainsKey(id))
                    {
                        Debug.LogWarning("Bridge: DistributeUnityEvent: missing id: " + id + " ev: " + ev);
                        continue;
                    }

                    object obj = idToObject[id];
                    //Debug.Log("Bridge: DistributeUnityEvent: obj: " + obj);

                    BridgeObject bridgeObject = obj as BridgeObject;

                    if (bridgeObject == null)
                    {
                        Debug.LogError("Bridge: DistributeUnityEvent: tried to send eventName: " + eventName + " to non-BridgeObject obj: " + obj + " id: " + id + " ev: " + ev);
                        continue;
                    }

                    JObject queryResult = new JObject();
                    AddQueryData(obj, query, queryResult);

                    //Debug.Log("Bridge: QueryData: queryResult: " + queryResult);

                    queryResults.Add(queryResult);
                }

                if (!string.IsNullOrEmpty(callbackID))
                {
                    SendCallbackData(callbackID, isSingle ? queryResults[0] : queryResults);
                }

                break;
            }

            default: {
                string id = (string)ev["id"];
                //Debug.Log("Bridge: DistributeUnityEvent: id: " + id + " ev: " + ev);

                if (string.IsNullOrEmpty(id))
                {
                    Debug.LogError("Bridge: DistributeUnityEvent: undefined id on eventName: " + eventName + " ev: " + ev);
                    return;
                }

                if (!idToObject.ContainsKey(id))
                {
                    Debug.LogWarning("Bridge: DistributeUnityEvent: missing id: " + id + " ev: " + ev);
                    return;
                }

                object obj = idToObject[id];
                //Debug.Log("Bridge: DistributeUnityEvent: obj: " + obj);

                BridgeObject bridgeObject = obj as BridgeObject;

                if (bridgeObject == null)
                {
                    Debug.LogError("Bridge: DistributeUnityEvent: tried to send eventName: " + eventName + " to non-BridgeObject obj: " + obj + " id: " + id + " ev: " + ev);
                    return;
                }

                bridgeObject.HandleEvent(ev);

                break;
            }
            }
        }
Example #2
0
        public void LayoutPie()
        {
            //Debug.Log("LayoutPie: _pie: " + _pie);

            if (_pie == null)
            {
                //Debug.Log("LayoutPie: null _pie");
                slices = 0;
                return;
            }

            JArray pieSlices = _pie.GetArray("slices");

            if (pieSlices == null)
            {
                //Debug.Log("LayoutPie: no slices");
                slices = 0;
                return;
            }

            slices = pieSlices.ArrayLength();
            //Debug.Log("LayoutPie: slices: " + slices);

            initialDirection = _pie.GetFloat("initialDirection", 0.5f * Mathf.PI);
            subtend          = _pie.GetFloat("subtend", 0.0f);
            clockwise        = _pie.GetBoolean("clockwise", true);
            inactiveDistance = _pie.GetFloat("inactiveDistance", 10.0f);
            itemDistance     = _pie.GetFloat("itemDistance", 10.0f);

            float sliceSizeTotal = 0.0f;

            foreach (JObject slice in pieSlices)
            {
                float sliceSize = slice.GetFloat("sliceSize", 1.0f);
                sliceSizeTotal += sliceSize;
            }

            //Debug.Log("slices: " + slices + " sliceSizeTotal: " + sliceSizeTotal);

            float pieSubtend =
                (subtend == 0.0f)
                ? (2.0f * Mathf.PI)
                : subtend;
            float sliceSizeScale =
                (sliceSizeTotal == 0)
                ? 1.0f
                : (pieSubtend / sliceSizeTotal);
            float clockSign      = clockwise ? -1 : 1;
            float sliceDirection = initialDirection;
            bool  firstSlice     = true;

            //Debug.Log("pieSubtend: " + pieSubtend + " sliceSizeScale: " + sliceSizeScale + " clockSign: " + clockSign + " sliceDirection: " + sliceDirection);

            foreach (JObject slice in pieSlices)
            {
                float sliceSize    = slice.GetFloat("sliceSize", 1.0f);
                float sliceSubtend = sliceSize * sliceSizeScale;
                float halfTurn     = 0.5f * clockSign * sliceSubtend;
                //Debug.Log("start sliceDirection: " + sliceDirection + " sliceSize: " + sliceSize + " sliceSubtend: " + sliceSubtend + " halfTurn: " + halfTurn);

                if (firstSlice)
                {
                    firstSlice = false;
                    // If the subtend was zero, use the whole pie, but start the first slice centered no the initial direction.
                    if (subtend == 0.0f)
                    {
                        sliceDirection -= halfTurn;
                        //Debug.Log("firstSlice and zero subtend, turning back halfTurn: " + halfTurn + " to sliceDirection: " + sliceDirection);
                    }
                }

                sliceDirection += halfTurn;
                //Debug.Log("center sliceDirection: " + sliceDirection);

                //Debug.Log("slice: " + slice + " sliceSize: " + sliceSize + " sliceDirection: " + sliceDirection + " sliceSubtend: " + sliceSubtend + " dx: " + dx + " dy: " + dy);

                slice["sliceDirection"] = sliceDirection;
                slice["sliceSubtend"]   = sliceSubtend;

                sliceDirection += halfTurn;
                //Debug.Log("end sliceDirection: " + sliceDirection);
            }
        }