예제 #1
0
 public void RegisterOperation(EGUIOperation op, EGUIComponent elem, Rect bounds, params object[] args)
 {
     _OPs.Add(op);
     _OPComponentIDs.Add(CurrentComponentID);
     _OPElementIDs.Add(CurrentElementID);
     _OPComponents.Add(elem);
     _OPBounds.Add(bounds);
     _OPData.Add(args);
     // Console.WriteLine("SGUI-IM: Registered OP " + op + " " + elem + " @ " + bounds + " for E" + CurrentElementID + "C" + CurrentComponentID);
 }
예제 #2
0
        private bool _RecreateOperation(int opID, int handledComponentID = -1)
        {
            if (opID < 0)
            {
                opID = _OPs.Count + opID;
            }
            Event e = Event.current;

            EGUIOperation op      = _OPs[opID];
            int           elemID  = _OPElementIDs[opID];
            int           compID  = _OPComponentIDs[opID];
            EGUIComponent elemGUI = _OPComponents[opID];
            Rect          bounds  = _OPBounds[opID];

            object[] data = _OPData[opID];

            // Console.WriteLine("SGUI-IM: Recreating operation for E" + elemID + "C" + compID + ": " + op + " " + elemGUI);

            SElement elem = _Elements[elemID];

            if (op == EGUIOperation.Draw && handledComponentID != -1)
            {
                if (e.type == EventType.Used)
                {
                    // Console.WriteLine("SGUI-IM: Current event used - recreating operation @ NULLRECT");
                    bounds = NULLRECT;
                }

                if (compID < handledComponentID)
                {
                    // Console.WriteLine("SGUI-IM: Current component before handled component (" + handledComponentID + ") - recreating operation @ NULLRECT");
                    bounds = NULLRECT;
                }

                if (elem == null || !elem.Visible || !elem.Enabled)
                {
                    // Console.WriteLine("SGUI-IM: Current component element not interactable - recreating operation @ NULLRECT");
                    bounds = NULLRECT;
                }
            }

            switch (op)
            {
            case EGUIOperation.Draw:
                switch (elemGUI)
                {
                case EGUIComponent.Label:
                    RegisterNextComponent();
                    GUI.Label(bounds, (string)data[0]);
                    break;

                case EGUIComponent.Button:
                    RegisterNextComponent();
                    GUI.Button(bounds, (string)data[0]);
                    break;

                case EGUIComponent.TextField:
                    RegisterNextComponent();
                    // TextField and Button use mouse input by themselves.
                    if (elemGUI == EGUIComponent.TextField)
                    {
                        if (e.isMouse && bounds.Contains(e.mousePosition))
                        {
                            // ... although the TextField mouse input requires some help with the cursor placement.
                            string    text          = (string)data[0];
                            Vector2   mousePosition = e.mousePosition;
                            EventType type          = e.type;
                            Event.current.Use();
                            GUI.TextField(bounds, text);
                            // Focus(compID); // Actually kills focus.
                            TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
#pragma warning disable CS0618
                            // TextEditor.content is obsolete, yet it must be accessed.
                            // Alternatively access TextEditor.m_Content via reflection.
                            GUIContent content = editor.content;
#pragma warning restore CS0618

                            // editor.style.GetCursorStringIndex(this.position, this.m_Content, cursorPosition + this.scrollOffset);
                            // GetCursorStringIndex seems broken.
                            int     index    = 0;
                            Vector2 position = -editor.scrollOffset;
                            PreparePosition(elem, ref position);
                            Rect boundsScrolled = new Rect(position.x, position.y, bounds.size.x, bounds.size.y);
                            for (; index < text.Length; index++)
                            {
                                if (mousePosition.x < editor.style.GetCursorPixelPosition(boundsScrolled, content, index).x - LineHeight * 0.5f)
                                {
                                    break;
                                }
                            }

                            if (type == EventType.MouseDown)
                            {
                                editor.cursorIndex = index;
                                editor.selectIndex = index;
                            }
                            else if (type == EventType.MouseDrag)
                            {
                                editor.cursorIndex = index;
                            }

                            editor.UpdateScrollOffsetIfNeeded();
                            return(true);
                        }
                        else
                        {
                            GUI.TextField(bounds, (string)data[0]);
                        }
                    }

                    if (e.isMouse && bounds.Contains(e.mousePosition))
                    {
                        // Still need to know whether mouse is in it or not, though
                        return(true);
                    }
                    break;
                }
                break;

            case EGUIOperation.Start:
                RegisterNextComponent();
                switch (elemGUI)
                {
                case EGUIComponent.Clip:
                    GUI.BeginClip(bounds, Vector2.zero, Vector2.zero, true);
                    _ClipScopeTypes.Push(EClipScopeType.Clip);
                    _ClipScopeRects.Push(bounds);
                    break;

                case EGUIComponent.Group:
                    GUI.BeginGroup(bounds);
                    _ClipScopeTypes.Push(EClipScopeType.Group);
                    _ClipScopeRects.Push(bounds);
                    break;

                case EGUIComponent.Scroll:
                    data[0] = GUI.BeginScrollView(bounds, (Vector2)data[0], (Rect)data[1], (bool)data[2], (bool)data[3]);
                    SGroup group = elem as SGroup;
                    if (group != null)
                    {
                        group.ScrollPosition = (Vector2)data[0];
                    }
                    _ClipScopeTypes.Push(EClipScopeType.Scroll);
                    _ClipScopeRects.Push(bounds);
                    break;
                }
                break;

            case EGUIOperation.End:
                switch (elemGUI)
                {
                case EGUIComponent.Clip:
                    GUI.EndClip();
                    break;

                case EGUIComponent.Group:
                    GUI.EndGroup();
                    break;

                case EGUIComponent.Scroll:
                    GUI.EndScrollView();
                    break;
                }
                _ClipScopeTypes.Pop();
                _ClipScopeRects.Pop();
                break;
            }

            return(false);
        }