Ejemplo n.º 1
0
        /// <summary>
        /// Walks a generated AST (Abstract Syntax Tree) and creates the elements of this UIScreen.
        /// </summary>
        /// <param name="State">A ParserState instance.</param>
        /// <param name="node">The root node of the AST.</param>
        private void WalkTree(UIParser.ParserState State, AstNode node, ref ParseResult Result)
        {
            NodeType NType = (NodeType)Enum.Parse(typeof(NodeType), node.ToString(), true);

            switch (NType)
            {
            case NodeType.DefineImage:     //Defines an image and loads a texture for it.
                DefineImageNode ImgNode = (DefineImageNode)UINode.GetNode(node);
                UIImage         Img     = new UIImage(ImgNode, m_Screen);
                Result.Elements.Add(ImgNode.Name, Img);
                break;

            case NodeType.DefineString:     //Defines a string with a name.
                DefineStringNode StrNode = (DefineStringNode)UINode.GetNode(node);
                Result.Strings.Add(StrNode.Name, StringManager.StrTable(State.CurrentStringTable)[StrNode.StrIndex]);
                break;

            case NodeType.AddButton:     //Defines a button.
                AddButtonNode ButtonNode = (AddButtonNode)UINode.GetNode(node);
                UIButton      Btn        = new UIButton(ButtonNode, Result, m_Screen);
                Result.Elements.Add(ButtonNode.Name, Btn);

                break;

            case NodeType.AddText:
                AddTextNode TextNode = (AddTextNode)UINode.GetNode(node);
                UILabel     Lbl      = new UILabel(TextNode, Result, m_Screen);
                Result.Elements.Add(TextNode.Name, Lbl);
                break;

            case NodeType.AddTextEdit:
                AddTextEditNode TextEditNode = (AddTextEditNode)UINode.GetNode(node);
                UITextEdit2     Txt          = new UITextEdit2(TextEditNode, State, m_Screen);
                Result.Elements.Add(TextEditNode.Name, Txt);
                break;

            case NodeType.AddSlider:
                AddSliderNode SliderNode = (AddSliderNode)UINode.GetNode(node);
                UISlider      Slider     = new UISlider(SliderNode, State, m_Screen);
                Result.Elements.Add(SliderNode.Name, Slider);
                break;

            case NodeType.SetSharedProperties:     //Assigns a bunch of shared properties to declarations following the statement.
                State.InSharedPropertiesGroup = true;
                SetSharedPropsNode SharedPropsNode = (SetSharedPropsNode)UINode.GetNode(node);

                if (SharedPropsNode.StringTable != null)
                {
                    State.CurrentStringTable = (int)SharedPropsNode.StringTable;
                }

                if (SharedPropsNode.ControlPosition != null)
                {
                    State.Position[0] = SharedPropsNode.ControlPosition.Numbers[0];
                    State.Position[1] = SharedPropsNode.ControlPosition.Numbers[1];
                    break;
                }

                if (SharedPropsNode.Color != null)
                {
                    State.Color   = new Color();
                    State.Color.R = (byte)SharedPropsNode.Color.Numbers[0];
                    State.Color.G = (byte)SharedPropsNode.Color.Numbers[1];
                    State.Color.B = (byte)SharedPropsNode.Color.Numbers[2];
                }

                if (SharedPropsNode.TextColor != null)
                {
                    State.TextColor   = new Color();
                    State.TextColor.R = (byte)SharedPropsNode.TextColor.Numbers[0];
                    State.TextColor.G = (byte)SharedPropsNode.TextColor.Numbers[1];
                    State.TextColor.B = (byte)SharedPropsNode.TextColor.Numbers[2];
                }

                if (SharedPropsNode.TextColorSelected != null)
                {
                    State.TextColorSelected   = new Color();
                    State.TextColorSelected.R = (byte)SharedPropsNode.TextColorSelected.Numbers[0];
                    State.TextColorSelected.G = (byte)SharedPropsNode.TextColorSelected.Numbers[1];
                    State.TextColorSelected.B = (byte)SharedPropsNode.TextColorSelected.Numbers[2];
                }

                if (SharedPropsNode.TextColorHighlighted != null)
                {
                    State.TextColorHighlighted   = new Color();
                    State.TextColorHighlighted.R = (byte)SharedPropsNode.TextColorHighlighted.Numbers[0];
                    State.TextColorHighlighted.G = (byte)SharedPropsNode.TextColorHighlighted.Numbers[1];
                    State.TextColorHighlighted.B = (byte)SharedPropsNode.TextColorHighlighted.Numbers[2];
                }

                if (SharedPropsNode.TextColorDisabled != null)
                {
                    State.TextColorDisabled   = new Color();
                    State.TextColorDisabled.R = (byte)SharedPropsNode.TextColorDisabled.Numbers[0];
                    State.TextColorDisabled.G = (byte)SharedPropsNode.TextColorDisabled.Numbers[1];
                    State.TextColorDisabled.B = (byte)SharedPropsNode.TextColorDisabled.Numbers[2];
                }

                if (SharedPropsNode.BackColor != null)
                {
                    State.BackColor   = new Color();
                    State.BackColor.R = (byte)SharedPropsNode.BackColor.Numbers[0];
                    State.BackColor.G = (byte)SharedPropsNode.BackColor.Numbers[1];
                    State.BackColor.B = (byte)SharedPropsNode.BackColor.Numbers[2];
                }

                if (SharedPropsNode.CursorColor != null)
                {
                    State.CursorColor   = new Color();
                    State.CursorColor.R = (byte)SharedPropsNode.CursorColor.Numbers[0];
                    State.CursorColor.G = (byte)SharedPropsNode.CursorColor.Numbers[1];
                    State.CursorColor.B = (byte)SharedPropsNode.CursorColor.Numbers[2];
                }

                if (SharedPropsNode.TextButton)
                {
                    State.TextButton = true;
                }

                if (SharedPropsNode.Opaque != null)
                {
                    State.IsOpaque = (SharedPropsNode.Opaque == 1) ? true : false;
                }

                if (SharedPropsNode.Transparent != null)
                {
                    State.IsTransparent = (SharedPropsNode.Transparent == 1) ? true : false;
                }

                if (SharedPropsNode.Alignment != null)
                {
                    State.Alignment = (int)SharedPropsNode.Alignment;
                }

                if (SharedPropsNode.Image != "")
                {
                    State.Image = SharedPropsNode.Image;
                }

                if (SharedPropsNode.Tooltip != "")
                {
                    State.Tooltip = SharedPropsNode.Tooltip;
                }

                if (SharedPropsNode.Text != "")
                {
                    State.Caption = SharedPropsNode.Text;
                }

                if (SharedPropsNode.Size != null)
                {
                    State.Size = new Vector2(SharedPropsNode.Size.Numbers[0], SharedPropsNode.Size.Numbers[1]);
                }

                if (SharedPropsNode.Orientation != null)
                {
                    State.Orientation = (int)SharedPropsNode.Orientation;
                }

                if (SharedPropsNode.Font != null)
                {
                    State.Font = (int)SharedPropsNode.Font;
                }

                if (SharedPropsNode.Opaque != null)
                {
                    State.Opaque = (int)SharedPropsNode.Opaque;
                }

                break;

            case NodeType.SetControlProperties:     //Sets a bunch of properties to a specified control.
                SetControlPropsNode ControlPropsNode = (SetControlPropsNode)UINode.GetNode(node);

                UIControl Ctrl = new UIControl(ControlPropsNode, m_Screen, State);
                Result.Controls.Add(ControlPropsNode.Control, Ctrl);

                if (State.InSharedPropertiesGroup)
                {
                    UIElement Test = new UIElement(m_Screen, null);
                    //Script implicitly created an object... :\
                    if (!Result.Elements.TryGetValue(ControlPropsNode.Control, out Test))
                    {
                        Result.Elements.Add(ControlPropsNode.Control, new UIElement(m_Screen, null));

                        if (Ctrl.Image != null)
                        {
                            Result.Elements[ControlPropsNode.Control].Image = new UIImage(Ctrl.Image);
                        }

                        Result.Elements[ControlPropsNode.Control].Position = Ctrl.Position;
                    }
                }

                break;

            case NodeType.End:
                State.InSharedPropertiesGroup = false;
                State.Image      = "";    //Reset
                State.TextButton = false; //Reset
                State.Color      = new Color();
                State.Caption    = "";
                State.Size       = new Vector2(0, 0);
                State.Alignment  = 0;
                State.Font       = 0;
                //TODO: Reset more?
                break;
            }

            foreach (AstNode child in node.ChildNodes)
            {
                WalkTree(State, child, ref Result);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructs a new UITextEdit control from a parsed UIScript.
        /// </summary>
        /// <param name="Node">The AddTextEditNode that defines this UITextEdit control.</param>
        /// <param name="State">The ParserState returned when parsing the UIScript.</param>
        /// <param name="Screen">A UIScreen instance.</param>
        public UITextEdit2(AddTextEditNode Node, ParserState State, UIScreen Screen) :
            base(Screen)
        {
            Name            = Node.Name;
            m_ID            = Node.ID;
            m_KeyboardInput = true; //UITextEdit needs to receive input from keyboard!

            if (!State.InSharedPropertiesGroup)
            {
                if (Node.TextEditPosition.Numbers.Count > 0)
                {
                    Position       = new Vector2(Node.TextEditPosition.Numbers[0], Node.TextEditPosition.Numbers[1]) + Screen.Position;
                    m_TextPosition = Position;
                }

                if (State.InSharedPropertiesGroup)
                {
                    m_Size = State.Size;
                }
                else
                {
                    m_Size   = new Vector2();
                    m_Size.X = Node.Size.Numbers[0];
                    m_Size.Y = Node.Size.Numbers[1];
                }

                if (Node.Tooltip != "")
                {
                    Tooltip = m_Screen.GetString(Node.Tooltip);
                }

                Transparent = (Node.Transparent == 1) ? true : false;

                if (Node.Lines != null)
                {
                    m_NumLines = (int)Node.Lines;
                }

                if (Node.Capacity != null)
                {
                    m_MaxChars = (int)Node.Capacity;
                }

                if (Node.Alignment != null)
                {
                    m_Alignment = (TextEditAlignment)Node.Alignment;
                }

                if (Node.FlashOnEmpty != null)
                {
                    m_FlashOnEmpty = (Node.FlashOnEmpty == 1) ? true : false;
                }

                if (Node.FrameOnFocus != null)
                {
                    m_FrameOnFocus = (Node.FrameOnFocus == 1) ? true : false;
                }

                if (State.InSharedPropertiesGroup)
                {
                    TextColor = State.Color;
                }
                else
                {
                    TextColor = new Color(Node.Color.Numbers[0], Node.Color.Numbers[1], Node.Color.Numbers[2]);
                }

                if (Node.BackColor != null)
                {
                    m_BackColor = new Color(Node.Color.Numbers[0], Node.Color.Numbers[1], Node.Color.Numbers[2]);
                }
                else
                {
                    m_BackColor = new Color(57, 81, 110, 255);
                }

                if (Node.Mode != null)
                {
                    m_Mode = (Node.Mode == "kReadOnly") ? TextEditMode.ReadOnly : TextEditMode.Insert;
                }

                if (Node.ScrollbarImage != string.Empty)
                {
                    m_ScrollbarImage = m_Screen.GetImage(Node.ScrollbarImage).Image.Texture;
                }

                if (Node.ScrollbarGutter != null)
                {
                    m_ScrollbarWidth = (int)Node.ScrollbarGutter;
                }

                if (Node.ScrollbarType != null)
                {
                    m_ScrollbarType = (ScrollbarType)Node.ScrollbarType;
                }

                if (Node.ResizeForExactLineHeight != null)
                {
                    m_ResizeForExactLineHeight = (Node.ResizeForExactLineHeight == 1) ? true : false;
                }

                if (Node.EnableIME != null)
                {
                    m_EnableInputModeEditing = (Node.EnableIME == 1) ? true : false;
                }

                if (Node.CursorColor != null)
                {
                    m_CursorColor = new Color(Node.CursorColor.Numbers[0], Node.CursorColor.Numbers[1], Node.CursorColor.Numbers[2]);
                }

                if (Node.FrameColor != null)
                {
                    m_FrameColor = new Color(Node.FrameColor.Numbers[0], Node.FrameColor.Numbers[1], Node.FrameColor.Numbers[2]);
                }
            }
            else
            {
                if (State.BackColor != null)
                {
                    m_BackColor = State.BackColor;
                }
                if (State.Color != null)
                {
                    TextColor = State.Color;
                }
                if (State.CursorColor != null)
                {
                    m_CursorColor = State.CursorColor;
                }
                if (State.Position != null)
                {
                    Position       = new Vector2(State.Position[0], State.Position[1]) + Screen.Position;
                    m_TextPosition = Position;
                }
                if (State.Tooltip != "")
                {
                    Tooltip = State.Tooltip;
                }
            }

            int Font = 0;

            if (Node.Font != 0)
            {
                Font = Node.Font;
            }
            else
            {
                Font = State.Font;
            }

            switch (Font)
            {
            case 9:
                m_Font = Screen.Font9px;
                break;

            case 10:
                m_Font = Screen.Font10px;
                break;

            case 12:
                m_Font = Screen.Font12px;
                break;

            case 14:
                m_Font = Screen.Font14px;
                break;

            case 16:
                m_Font = Screen.Font16px;
                break;
            }

            m_Renderer = new TextRenderer((m_NumLines > 1) ? true : false,
                                          Position, Size, m_ScrollFactor, Lineheight, m_Font, TextColor, m_NumLines);

            m_Cursor.Position                = Position;
            m_CursorVisibilityTimer          = new Timer(100);
            m_CursorVisibilityTimer.Enabled  = true;
            m_CursorVisibilityTimer.Elapsed += M_CursorVisibilityTimer_Elapsed;
            m_CursorVisibilityTimer.Start();

            m_Screen.Manager.OnTextInput += Manager_OnTextInput;
        }