Exemplo n.º 1
0
 private static void GetOrCreateRectTransformList(ref Dictionary <string, RectTransform> rectTransforms)
 {
     if (rectTransforms == null)
     {
         rectTransforms = new Dictionary <string, RectTransform>();
         foreach (var item in SceneInfoGrabber <RectTransform> .GetUIComponentsOnScene())
         {
             rectTransforms[item.name] = item;
         }
     }
 }
Exemplo n.º 2
0
#pragma warning restore 0649
        void Awake()
        {
            Instance = this;

            //get my components
            audioSource = GetComponent <AudioSource>();

            //clear static lists
            StaticViews.Clear();
            BackStack.Clear();
            FullScreenFadeStack.Clear();

            //init other objects
            dragger       = new Dragger();
            soundsManager = new SoundsManager(BackgroundSoundPrefab, FadeVolume, VolumeChangeSpeed);

            //find all views
            var views = SceneInfoGrabber <BaseView> .GetUIComponentsOnScene(scene : gameObject.scene);

            //grab components for views
            foreach (var view in views)
            {
                view.GrabComponents();
                StaticViews[view.GetType().Name] = view;
            }

            //grab views for views
            foreach (var view in views)
            {
                view.GrabViews(StaticViews);
            }

            //init views
            foreach (var view in views)
            {
                view.Init();
            }

            //subscribe views
            foreach (var view in views)
            {
                view.AutoSubscribe();
            }

            //show views
            foreach (var view in views.Where(v => v.ShowAtStart))
            {
                Show(view, null, noAnimation: true);
            }

            //init gestures
            InitGestures();
        }
Exemplo n.º 3
0
        private void SynchronizeGraphAndScene()
        {
            var infos = new Dictionary <string, ViewInfo <BaseView> > ();

            foreach (var info in SceneInfoGrabber <BaseView> .GrabInfos(true))
            {
                infos[info.Name] = info;
            }

            Dictionary <string, RectTransform> rectTransforms = null;

            //is nodes w/o GameObject?
            foreach (var node in target.nodes.OfType <ViewNode>())
            {
                if (!infos.ContainsKey(node.name))
                {
                    //find object with the name
                    GetOrCreateRectTransformList(ref rectTransforms);

                    if (rectTransforms.TryGetValue(node.name, out var rt))
                    {
                        //found => add component
                        node.rt = rt;
                        var type = GetTypeByName(node.name, node.rt.gameObject.scene);
                        if (type != null)
                        {
                            rt.gameObject.AddComponent(type);
                        }
                        else
                        {
                            ScriptBuilder.CreateScript(node);
                        }
                    }
                    else
                    {
                        //node w/o GameObject
                        node.rt = null;
                    }
                }
                else
                {
                    //found GameObject
                    node.rt = infos[node.name].Main.RectTransform;
                }
            }
        }
Exemplo n.º 4
0
 public static void TranslateScene()
 {
     foreach (var text in SceneInfoGrabber <Text> .GetUIComponentsOnScene())
     {
         text.text = text.text.Translate();
     }
     foreach (var text in SceneInfoGrabber <TMPro.TextMeshProUGUI> .GetUIComponentsOnScene())
     {
         text.text = text.text.Translate();
     }
     foreach (var dd in SceneInfoGrabber <Dropdown> .GetUIComponentsOnScene())
     {
         foreach (var opt in dd.options)
         {
             opt.text = opt.text.Translate();
         }
     }
 }
Exemplo n.º 5
0
        static IEnumerable <string> GrabAllTextsOnScene()
        {
            foreach (var text in SceneInfoGrabber <Text> .GetUIComponentsOnScene())
            {
                yield return(text.text);
            }
            foreach (var text in SceneInfoGrabber <TMPro.TextMeshProUGUI> .GetUIComponentsOnScene())
            {
                yield return(text.text);
            }

            foreach (var dd in SceneInfoGrabber <Dropdown> .GetUIComponentsOnScene())
            {
                foreach (var opt in dd.options)
                {
                    yield return(opt.text);
                }
            }
        }
Exemplo n.º 6
0
        private void CreateNode(GameObject go)
        {
            var pos = NodeEditorWindow.current.WindowToGridPosition(Event.current.mousePosition);

            var rt = go.GetComponent <RectTransform>();

            if (!rt)
            {
                return;
            }

            //check name
            if (target.nodes.OfType <ViewNode>().Any(n => n.name == rt.name))
            {
                EditorUtility.DisplayDialog("Bad name", $"Node with name '{rt.name}' already exists!", "OK");
                return;
            }

            //check name
            if (!Regex.IsMatch(rt.name, "^[a-zA-Z0-9_]+$") ||
                target.nodes.OfType <ViewNode>().Any(n => n.name == rt.name))
            {
                EditorUtility.DisplayDialog("Bad name", $"Name '{rt.name}' is not allowed!", "OK");
                return;
            }

            //
            var wellKnown = SceneInfoGrabber <BaseView> .GetWellKnownComponent(rt);

            if (wellKnown is BaseView v)
            {
                var node = CreateNode(typeof(ViewNode), pos) as ViewNode;
                node.Build(v);
                //contains new items?
                if (node.ViewInfo.Members.Values.Any(m => m.Component != null || m.Binded != null))
                {
                    ScriptBuilder.CreateScript(node);
                }

                return;
            }

            if (wellKnown is RectTransform || wellKnown is Image)
            {
                //check name
                var type = GetTypeByName(wellKnown.name, go.scene);

                if (type != null && !typeof(BaseView).IsAssignableFrom(type))
                {
                    //the type already exists
                    EditorUtility.DisplayDialog("Bad name", $"Name '{wellKnown.name}' is not allowed!", "OK");
                    return;
                }
                //
                var node = CreateNode(typeof(ViewNode), pos) as ViewNode;
                node.Build(wellKnown.transform as RectTransform);
                BuildScriptForNewNode(node, go);
                return;
            }

            EditorUtility.DisplayDialog("You can not drag this", "You can not drag here Button, Text, InpputField etc.\r\nYou can drag here only RectTransform or View.", "OK");
        }
Exemplo n.º 7
0
        public void GrabInfoAboutView(RectTransform rt)
        {
            if (rt == null)
            {
                return;
            }

            ViewInfo = new ViewInfo();
            ViewInfo.Members.Clear();

            void GrabMethods(Type type, bool checkAttr)
            {
                var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

                foreach (var mi in methods)
                {
                    if (mi.Name.StartsWith("get_") ||
                        mi.Name.StartsWith("<"))
                    {
                        continue;//property getter
                    }
                    var attr = mi.GetCustomAttribute <VisibleInGraphAttribute>();
                    if (attr == null && checkAttr)
                    {
                        continue;
                    }
                    if (attr != null && !attr.Visible)
                    {
                        continue;
                    }

                    var parameters = mi.GetParameters();
                    var count      = parameters.Count(p => !p.HasDefaultValue);
                    if (count != 0)
                    {
                        if (attr == null || !attr.Visible)
                        {
                            continue;
                        }
                    }

                    ViewInfo.Members[mi.Name] = new ViewInfoItem {
                        Name = mi.Name, MethodInfo = mi
                    };
                }
            }

            void GrabEvents(Type type, bool checkAttr)
            {
                var events = type.GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

                foreach (var ei in events)
                {
                    var attr = ei.GetCustomAttribute <VisibleInGraphAttribute>();
                    if (attr == null && checkAttr)
                    {
                        continue;
                    }
                    if (attr != null && !attr.Visible)
                    {
                        continue;
                    }

                    ViewInfo.Members[ei.Name] = new ViewInfoItem {
                        Name = ei.Name, EventInfo = ei
                    };
                }
            }

            //grab components from scene
            foreach (var info in SceneInfoGrabber <BaseView> .GrabInfo(rt, true))
            {
                ViewInfo.Members[info.Key] = new ViewInfoItem {
                    Name = info.Key, Component = info.Value
                }
            }
            ;

            //grab exists fileds
            Type viewType = null;
            var  view     = rt.GetComponent <BaseView>();

            if (view != null)
            {
                viewType = view.GetType();
            }
            else
            {
                viewType = UIGraphEditor.GetTypeByName(rt.name, rt.gameObject.scene);
            }

            if (viewType != null)
            {
                var fields = viewType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                foreach (var fi in fields)
                {
                    if (SceneInfoGrabber <BaseView> .WellKnownTypes.Contains(fi.FieldType) || typeof(BaseView).IsAssignableFrom(fi.FieldType))
                    {
                        var isAutogenerated = fi.GetCustomAttribute <AutoGeneratedAttribute>();
                        if (isAutogenerated != null)
                        {
                            //skip isAutogenerated fields => they will recretaed
                            continue;
                        }

                        ViewInfo.Members[fi.Name] = new ViewInfoItem {
                            Name = fi.Name, FieldInfo = fi
                        };
                    }
                }

                //grab argless public methods
                GrabMethods(viewType, false);

                //grab public events
                GrabEvents(viewType, false);
            }

            //grab argless public methods of base class
            GrabMethods(typeof(BaseView), true);

            //add linked Views
            foreach (var port in Outputs)
            {
                foreach (var linked in port.GetConnections())
                {
                    if (linked.node is ViewNode otherNode)
                    {
                        if (!ViewInfo.Members.ContainsKey(otherNode.name))
                        {
                            ViewInfo.Members[otherNode.name] = new ViewInfoItem()
                            {
                                Binded = otherNode.name, Name = otherNode.name, IsAutogenerated = true, IsAutogeneratedReallyExists = true
                            }
                        }
                        ;
                    }
                }
            }

            //remove Ports w/o members
            foreach (var port in Ports.ToArray())
            {
                if (port.ValueType == typeof(ActionInputPort) || port.ValueType == typeof(ActionOutputPort) || port.ValueType == typeof(EventPort))
                {
                    if (!ViewInfo.Members.ContainsKey(port.fieldName))
                    {
                        //remove port
                        RemoveDynamicPort(port);
                    }
                }
            }
        }
    }