示例#1
0
        /// <summary>
        /// Draw a text content
        /// </summary>
        /// <param name="drawList"></param>
        /// <param name="rect">the rect (of the text layouting box) to draw this text content</param>
        /// <param name="text">the text</param>
        /// <param name="style">style of the box model</param>
        /// <param name="state">state of the style</param>
        public static void DrawText(this DrawList drawList, Rect rect, string text, GUIStyle style, GUIState state)
        {
            TextAlignment alignment = (TextAlignment)style.Get <int>(GUIStyleName.TextAlignment, state);

            if (alignment != TextAlignment.Leading)
            {
                var textWidth = style.CalcSize(text, state).Width;
                if (rect.Width > textWidth)
                {
                    if (alignment == TextAlignment.Center)
                    {
                        var offsetX = (rect.Width - textWidth) / 2;
                        rect.X     += offsetX;
                        rect.Width -= offsetX;
                    }
                    else if (alignment == TextAlignment.Trailing)
                    {
                        rect.X     = rect.Right - textWidth;
                        rect.Width = textWidth;
                    }
                }
            }

            drawList.AddText(rect, text, style, state);
        }
示例#2
0
        /// <remarks>
        /// Note: Design of a toggle
        /// |←16→|
        /// |    |---------------+
        /// |    |               |
        /// +----+               |
        /// | √  | label         |
        /// +----+               |
        ///      |               |
        ///      +---------------+
        /// </remarks>
        public static void DrawToggle(Rect rect, string label, bool value, GUIState state)
        {
            GUIContext    g      = Form.current.uiContext;
            WindowManager w      = g.WindowManager;
            Window        window = w.CurrentWindow;
            GUIStyle      style  = GUIStyle.Basic;
            DrawList      d      = window.DrawList;

            var spacing  = GUISkin.Instance.InternalStyle.Get <double>(GUIStyleName._ControlLabelSpacing);
            var boxRect  = new Rect(rect.X, rect.Y + MathEx.ClampTo0(rect.Height - 16) / 2, 16, 16);
            var textRect = new Rect(rect.X + 16 + spacing, rect.Y, MathEx.ClampTo0(rect.Width - 16 - spacing),
                                    rect.Height);

            // box
            var filledBoxColor = Color.Rgb(0, 151, 167);
            var boxBorderColor = Color.White;
            var tickColor      = Color.Rgb(48, 48, 48);

            d.AddRectFilled(boxRect.TopLeft, boxRect.BottomRight, filledBoxColor);    //□
            d.AddRect(boxRect.TopLeft, boxRect.BottomRight, boxBorderColor, 0, 0, 2); //□
            if (value)                                                                //√
            {
                var h = boxRect.Height;
                d.PathMoveTo(new Point(0.125f * h + boxRect.X, 0.50f * h + boxRect.Y));
                d.PathLineTo(new Point(0.333f * h + boxRect.X, 0.75f * h + boxRect.Y));
                d.PathLineTo(new Point(0.875f * h + boxRect.X, 0.25f * h + boxRect.Y));
                d.PathStroke(tickColor, false, 2);
            }
            // label
            d.DrawBoxModel(textRect, label, style, state);
        }
示例#3
0
        /// <summary>
        /// Create an auto-layout button that acts like a toggle.
        /// </summary>
        /// <param name="text">text to display on the button</param>
        /// <param name="selected">whether this selectable is selected</param>
        /// <param name="options">layout options that specify layouting properties. See also <see cref="GUILayout.Width"/>, <see cref="GUILayout.Height"/>, <see cref="GUILayout.ExpandWidth"/>, <see cref="GUILayout.ExpandHeight"/>, <see cref="GUILayout.StretchWidth"/>, <see cref="GUILayout.StretchHeight"/></param>
        /// <returns>new value of the toggle-button</returns>
        public static bool Selectable(string text, bool selected, LayoutOptions?options)
        {
            Window window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return(false);
            }

            int id = window.GetID(text);

            // style
            var style = GUIStyle.Basic;

            style.ApplySkin(GUIControlName.Selectable);
            style.ApplyOption(options);

            // rect
            Size size = style.CalcSize(text, GUIState.Normal);
            Rect rect = window.GetRect(id, size);

            // interact
            selected = GUIBehavior.SelectableBehavior(rect, id, selected, out bool hovered, out bool held);

            // render
            DrawList d     = window.DrawList;
            var      state = (selected || (hovered && held)) ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal;

            d.DrawBoxModel(rect, text, style, state);

            style.Restore();

            return(selected);
        }
示例#4
0
文件: Label.cs 项目: jangocheng/ImGui
        /// <summary>
        /// Create an auto-layout label.
        /// </summary>
        /// <param name="text">text to display</param>
        public static void Label(string text, LayoutOptions?options)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return;
            }

            int id = window.GetID(text);

            // style
            var style = GUIStyle.Basic;

            style.Save();
            style.ApplyOption(options);

            // rect
            Size contentSize = style.CalcSize(text, GUIState.Normal);
            Rect rect        = window.GetRect(id);

            // rendering
            DrawList d = window.DrawList;

            d.DrawBoxModel(rect, text, style);

            style.Restore();
        }
示例#5
0
        private static void DrawColorDragButton(DrawList drawList, Rect rect, int id, char colorChar, double value, GUIState state)
        {
            GUIContext g      = Form.current.uiContext;
            Window     window = g.WindowManager.CurrentWindow;

            var style = GUIStyle.Basic;
            var d     = window.DrawList;

            style.PushBgColor(new Color(0.80f, 0.80f, 0.80f, 0.30f)); //+1
            d.AddRectFilled(rect, style.BackgroundColor);
            style.PopStyle();                                         //-1

            string text;

            text = string.Format("{0}:{1,3}", colorChar, (int)(value * 255));
            var fullTextSize = style.CalcSize(text, state);
            var contentRect  = style.GetContentRect(rect, state);

            if (fullTextSize.Width > contentRect.Width)
            {
                text = ((int)(value * 255)).ToString();
            }

            style.PushTextAlignment(TextAlignment.Center);
            d.DrawBoxModel(rect, text, style, state);
            style.PopStyle();
        }
示例#6
0
        /// <summary>
        /// Create an auto-layout image.
        /// </summary>
        /// <param name="filePath">file path of the image to display. The path should be relative to current dir or absolute.</param>
        public static void Image(string filePath)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return;
            }

            var id = window.GetID(filePath);

            // style
            var style = GUIStyle.Basic;

            style.PushBorder(1.0);

            // rect
            var  texture = TextureUtil.GetTexture(filePath);
            Size size    = style.CalcSize(texture, GUIState.Normal);
            var  rect    = window.GetRect(id);

            // render
            DrawList d = window.DrawList;

            style.PushBorderColor(Color.Black);//+4
            d.DrawBoxModel(rect, texture, style);

            style.PopStyle(4 + 4);
        }
示例#7
0
        /// <summary>
        /// Create an auto-layout image.
        /// </summary>
        /// <param name="rect">position and size</param>
        /// <param name="texture">texture, call<see cref="CreateTexture"/>to load it manually.</param>
        public static void Image(Rect rect, ITexture texture)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return;
            }

            // style
            var style = GUIStyle.Basic;

            style.PushBorder(1.0);

            // rect
            rect = window.GetRect(rect);

            // render
            DrawList d = window.DrawList;

            style.PushBorderColor(Color.Black);//+4
            d.DrawBoxModel(rect, texture, style);

            style.PopStyle(4 + 4);
        }
示例#8
0
        public static double ProgressBar(string str_id, double percent, Size size, string overlayText = null)
        {
            Window window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return(percent);
            }

            GUIContext g  = GetCurrentContext();
            int        id = window.GetID(str_id);

            // style
            var style = GUIStyle.Basic;

            // rect
            var rect = window.GetRect(id, size);

            percent = MathEx.Clamp01(percent);

            // render
            DrawList d = window.DrawList;

            GUIAppearance.DrawProgressBar(rect, percent);
            if (overlayText != null)
            {
                style.PushTextAlignment(TextAlignment.Center);
                d.DrawBoxModel(rect, overlayText, style);
                style.PopStyle();
            }

            return(percent);
        }
示例#9
0
        public static void DoControl(Rect rect, string text)
        {
            Form       form   = Form.current;
            GUIContext g      = form.uiContext;
            Window     window = g.WindowManager.CurrentWindow;
            DrawList   d      = window.DrawList;

            d.DrawBoxModel(rect, text, GUISkin.Instance[GUIControlName.Box]);
        }
示例#10
0
 /// <summary>
 /// Render a rectangle shaped with optional rounding and borders
 /// </summary>
 /// <param name="drawList"></param>
 /// <param name="p_min"></param>
 /// <param name="p_max"></param>
 /// <param name="fill_col"></param>
 /// <param name="border"></param>
 /// <param name="rounding"></param>
 public static void RenderFrame(this DrawList drawList, Point p_min, Point p_max, Color fill_col,
                                bool border = false, float rounding = 15)
 {
     drawList.AddRectFilled(p_min, p_max, fill_col, rounding);
     if (border)
     {
         drawList.AddRect(p_min + new Vector(1, 1), p_max + new Vector(1, 1), Color.Black, rounding);
         drawList.AddRect(p_min, p_max, new Color(0.70f, 0.70f, 0.70f, 0.65f), rounding);
     }
 }
示例#11
0
        public static void DrawVSlider(Rect rect, string label, double value, double minValue, double maxValue, GUIState state,
                                       Rect sliderRect, double labelHeight)
        {
            GUIContext    g      = Form.current.uiContext;
            WindowManager w      = g.WindowManager;
            Window        window = w.CurrentWindow;
            GUIStyle      style  = GUIStyle.Basic;
            DrawList      d      = window.DrawList;

            var colorForLineUsed   = Color.Rgb(0, 151, 167);
            var colorForLineUnused = state == GUIState.Normal ? Color.Rgb(117, 117, 117) : Color.Rgb(255, 128, 171);

            //slider
            var h           = sliderRect.Width;
            var a           = 0.2 * h;
            var b           = 0.3 * h;
            var upPoint     = new Point(sliderRect.X + sliderRect.Width / 2, sliderRect.Y + 10);
            var bottomPoint = new Point(sliderRect.X + sliderRect.Width / 2, sliderRect.Bottom - 10);

            var minY         = upPoint.Y;
            var maxY         = bottomPoint.Y;
            var currentPoint = upPoint + new Vector(0, (value - minValue) / (maxValue - minValue) * (maxY - minY));

            var leftArcCenter   = currentPoint + new Vector(-b, 0);
            var rightArcCenter  = currentPoint + new Vector(b, 0);
            var rightStartPoint = rightArcCenter + new Vector(0, -a);

            d.PathMoveTo(upPoint);
            d.PathLineTo(currentPoint);
            d.PathStroke(colorForLineUsed, false, 2);

            d.PathMoveTo(currentPoint);
            d.PathLineTo(bottomPoint);
            d.PathStroke(colorForLineUnused, false, 2);

            d.PathArcToFast(leftArcCenter, a, 3, 9);
            d.PathLineTo(rightStartPoint);
            d.PathArcToFast(rightArcCenter, a, 9, 12);
            d.PathArcToFast(rightArcCenter, a, 0, 3);
            d.PathClose();

            //label
            var labelRect = new Rect(rect.X, rect.Bottom - labelHeight,
                                     rect.Width, labelHeight);

            d.DrawText(labelRect, label, style, state);

            style.PushBgColor(new Color(0.67f, 0.40f, 0.40f, 0.60f), GUIState.Normal); //+1 TODO It's stupid to sprcifiy style like this. There should be a better way to do this.
            style.PushBgColor(new Color(0.67f, 0.40f, 0.40f, 1.00f), GUIState.Hover);  //+1
            style.PushBgColor(new Color(0.80f, 0.50f, 0.50f, 1.00f), GUIState.Active); //+1
            var fillColor = style.Get <Color>(GUIStyleName.BackgroundColor, state);

            d.PathFill(fillColor);
            style.PopStyle(3);
        }
示例#12
0
        public static bool DoHoverButton(Rect rect, string text)
        {
            Form       form   = Form.current;
            GUIContext g      = form.uiContext;
            Window     window = g.WindowManager.CurrentWindow;
            DrawList   d      = window.DrawList;

            int id       = window.GetID(text);
            var mousePos = Mouse.Instance.Position;
            var result   = false;
            var hovered  = rect.Contains(mousePos);

            //control logic
            var uiState = Form.current.uiContext;

            uiState.KeepAliveID(id);
            if (hovered)
            {
                uiState.SetHoverID(id);
                if (uiState.ActiveId == GUIContext.None)
                {
                    uiState.SetActiveID(id);
                    result = true;
                }
            }
            else
            {
                if (uiState.ActiveId == id)
                {
                    uiState.SetActiveID(GUIContext.None);
                }
                result = false;
            }

            // ui representation
            var state = GUI.Normal;

            if (uiState.ActiveId == id)
            {
                state = GUI.Active;
            }

            // ui painting
            {
                d.DrawBoxModel(rect, text, GUISkin.Instance[GUIControlName.Button], state);
            }

            return(result);
        }
示例#13
0
        public static void DrawProgressBar(Rect rect, double percent, GUIState state = GUIState.Normal)
        {
            GUIContext    g      = Form.current.uiContext;
            WindowManager w      = g.WindowManager;
            Window        window = w.CurrentWindow;
            GUIStyle      style  = GUIStyle.Basic;
            DrawList      d      = window.DrawList;

            style.PushBgColor(new Color(0.80f, 0.80f, 0.80f, 0.30f)); //+1
            d.AddRectFilled(rect, style.BackgroundColor);
            style.PopStyle();                                         //-1
            var fillWidth = rect.Width * percent;
            var fillRect  = new Rect(rect.X, rect.Y, fillWidth, rect.Height);

            style.PushFillColor(new Color(0.90f, 0.70f, 0.00f, 1.00f)); //+1
            d.AddRectFilled(fillRect, style.FillColor);
            style.PopStyle();                                           //-1
        }
示例#14
0
文件: Label.cs 项目: jangocheng/ImGui
        /// <summary>
        /// Create a label.
        /// </summary>
        /// <param name="rect">position and size</param>
        /// <param name="text">text to display</param>
        public static void Label(Rect rect, string text)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return;
            }

            // style apply
            var style = GUIStyle.Basic;

            // rect
            rect = window.GetRect(rect);

            // render
            DrawList d = window.DrawList;

            d.DrawBoxModel(rect, text, style);
        }
示例#15
0
        public static void RenderCollapseTriangle(this DrawList drawList, Point pMin, bool isOpen, double height, Color color, double scale = 1)
        {
            double h      = height;
            double r      = h * 0.40f * scale;
            Point  center = pMin + new Vector(h * 0.50f, h * 0.50f) * scale;

            Point a, b, c;

            if (isOpen)
            {
                center.Y -= r * 0.25f;
                a         = center + new Vector(0, 1) * r;
                b         = center + new Vector(-0.866f, -0.5f) * r;
                c         = center + new Vector(0.866f, -0.5f) * r;
            }
            else
            {
                a = center + new Vector(1, 0) * r;
                b = center + new Vector(-0.500f, 0.866f) * r;
                c = center + new Vector(-0.500f, -0.866f) * r;
            }

            drawList.AddTriangleFilled(a, b, c, color);
        }
示例#16
0
        /// <summary>
        /// Draw a box model
        /// </summary>
        /// <param name="drawList"></param>
        /// <param name="rect">the rect (of the border-box) to draw this box model </param>
        /// <param name="text">text of the box model</param>
        /// <param name="style">style of the box model</param>
        /// <param name="state"></param>
        public static void DrawBoxModel(this DrawList drawList, Rect rect, string text, GUIStyle style, GUIState state = GUIState.Normal)
        {
            if (rect == Layout.StackLayout.DummyRect)
            {
                return;
            }

            //Widths of border
            var bt = style.Get <double>(GUIStyleName.BorderTop, state);
            var br = style.Get <double>(GUIStyleName.BorderRight, state);
            var bb = style.Get <double>(GUIStyleName.BorderBottom, state);
            var bl = style.Get <double>(GUIStyleName.BorderLeft, state);

            //Widths of padding
            var pt = style.Get <double>(GUIStyleName.PaddingTop, state);
            var pr = style.Get <double>(GUIStyleName.PaddingRight, state);
            var pb = style.Get <double>(GUIStyleName.PaddingBottom, state);
            var pl = style.Get <double>(GUIStyleName.PaddingLeft, state);

            //4 corner of the border-box
            var btl           = new Point(rect.Left, rect.Top);
            var btr           = new Point(rect.Right, rect.Top);
            var bbr           = new Point(rect.Right, rect.Bottom);
            var bbl           = new Point(rect.Left, rect.Bottom);
            var borderBoxRect = new Rect(btl, bbr);

            //4 corner of the padding-box
            var ptl = new Point(btl.X + bl, btl.Y + bt);
            var ptr = new Point(btr.X - br, btr.Y + bt);
            var pbr = new Point(bbr.X - br, bbr.Y - bb);
            var pbl = new Point(bbl.X + bl, bbl.Y - bb);
            //if (ptl.X > ptr.X) return;//TODO what if (ptl.X > ptr.X) happens?
            var paddingBoxRect = new Rect(ptl, pbr);

            //4 corner of the content-box
            var ctl            = new Point(ptl.X + pl, ptl.Y + pt);
            var ctr            = new Point(ptr.X - pr, ptr.Y + pr);
            var cbr            = new Point(pbr.X - pr, pbr.Y - pb);
            var cbl            = new Point(pbl.X + pl, pbl.Y - pb);
            var contentBoxRect = new Rect(ctl, cbr);

            // draw background in padding-box
            var gradient = (Gradient)style.Get <int>(GUIStyleName.BackgroundGradient, state);

            if (gradient == Gradient.None)
            {
                var bgColor        = style.Get <Color>(GUIStyleName.BackgroundColor, state);
                var borderRounding = style.BorderRadius.topLeft;                        //FIXME
                drawList.AddRectFilled(paddingBoxRect, bgColor, (float)borderRounding); //TODO drawing method needed: rect with custom rounding at each corner
            }
            else if (gradient == Gradient.TopBottom)
            {
                var topColor    = style.Get <Color>(GUIStyleName.GradientTopColor, state);
                var bottomColor = style.Get <Color>(GUIStyleName.GradientBottomColor, state);
                drawList.AddRectFilledGradient(paddingBoxRect, topColor, bottomColor);
            }
            else
            {
                throw new InvalidOperationException();
            }

            //Content
            //Content-box
            if (text != null && ctl.X < ctr.X)//content should not be visible when ctl.X > ctr.X
            {
                //var textSize = style.CalcSize(text, state);
                /*HACK Don't check text size because the size calculated by Typography is not accurate. */
                /*if (textSize.Height < contentBoxRect.Height && textSize.Width < contentBoxRect.Width)*/
                {
                    drawList.DrawText(contentBoxRect, text, style, state);
                }
            }

            //Border
            //  Top
            if (!MathEx.AmostZero(bt))
            {
                var borderTopColor = style.Get <Color>(GUIStyleName.BorderTopColor, state);
                if (!MathEx.AmostZero(borderTopColor.A))
                {
                    drawList.PathLineTo(ptl);
                    drawList.PathLineTo(btl);
                    drawList.PathLineTo(btr);
                    drawList.PathLineTo(ptr);
                    drawList.PathFill(borderTopColor);
                }
            }
            //  Right
            if (!MathEx.AmostZero(br))
            {
                var borderRightColor = style.Get <Color>(GUIStyleName.BorderRightColor, state);
                if (!MathEx.AmostZero(borderRightColor.A))
                {
                    drawList.PathLineTo(ptr);
                    drawList.PathLineTo(btr);
                    drawList.PathLineTo(bbr);
                    drawList.PathLineTo(pbr);
                    drawList.PathFill(borderRightColor);
                }
            }
            //  Bottom
            if (!MathEx.AmostZero(bb))
            {
                var borderBottomColor = style.Get <Color>(GUIStyleName.BorderBottomColor, state);
                if (!MathEx.AmostZero(borderBottomColor.A))
                {
                    drawList.PathLineTo(pbr);
                    drawList.PathLineTo(bbr);
                    drawList.PathLineTo(bbl);
                    drawList.PathLineTo(pbl);
                    drawList.PathFill(borderBottomColor);
                }
            }
            //  Left
            if (!MathEx.AmostZero(bl))
            {
                var borderLeftColor = style.Get <Color>(GUIStyleName.BorderLeftColor, state);
                if (!MathEx.AmostZero(borderLeftColor.A))
                {
                    drawList.PathLineTo(pbl);
                    drawList.PathLineTo(bbl);
                    drawList.PathLineTo(btl);
                    drawList.PathLineTo(ptl);
                    drawList.PathFill(borderLeftColor);
                }
            }

            //Outline
            var outlineWidth = style.Get <double>(GUIStyleName.OutlineWidth, state);

            if (!MathEx.AmostZero(outlineWidth))
            {
                var outlineColor = style.Get <Color>(GUIStyleName.OutlineColor, state);
                if (!MathEx.AmostZero(outlineColor.A))
                {
                    drawList.PathRect(btl, bbr);
                    drawList.PathStroke(outlineColor, true, outlineWidth);
                }
            }

#if DrawPaddingBox
            drawList.PathRect(ptl, pbr);
            drawList.PathStroke(Color.ColorRgb(0, 100, 100), true, 1);
#endif

#if DrawContentBox
            drawList.PathRect(ctl, cbr);
            drawList.PathStroke(Color.ColorRgb(100, 0, 100), true, 1);
#endif
        }
示例#17
0
        /// <summary>
        /// Create an auto-layout collapsing header.
        /// </summary>
        /// <param name="text">header text</param>
        /// <param name="open">opened</param>
        /// <returns>true when opened</returns>
        /// <remarks> It is horizontally stretched (factor 1).</remarks>
        public static bool CollapsingHeader(string text, ref bool open, float scale = 1)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return(false);
            }

            var id = window.GetID(text);

            // style apply
            var style = GUIStyle.Basic;

            style.PushStretchFactor(false, 1); //+1, always expand width
            style.PushPadding(2);              //4

            // rect
            var  height = style.GetLineHeight();
            Rect rect   = window.GetRect(id, new Size(0, height));

            if (rect == Layout.StackLayout.DummyRect) //TODO how shold dummy rect be correctly handled in every control?
            {
                style.PopStyle();                     //-1
                style.PopStyle(4);                    //-4
                return(false);
            }

            // interact
            bool hovered, held;
            bool pressed = GUIBehavior.ButtonBehavior(rect, id, out hovered, out held, ButtonFlags.PressedOnClick);

            if (pressed)
            {
                open = !open;
            }

            // render
            {
                DrawList d = window.DrawList;
                style.PushBgColor(new Color(0.40f, 0.40f, 0.90f, 0.45f), GUIState.Normal); //+1 TODO It's stupid to sprcifiy style like this. There should be a better way to do this.
                style.PushBgColor(new Color(0.45f, 0.45f, 0.90f, 0.80f), GUIState.Hover);  //+1
                style.PushBgColor(new Color(0.53f, 0.53f, 0.87f, 0.80f), GUIState.Active); //+1
                var   state = (hovered && held) ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal;
                Color col   = style.Get <Color>(GUIStyleName.BackgroundColor, state);
                d.RenderFrame(rect.Min, rect.Max, col, false, 0);
                style.PopStyle(3);
                d.RenderCollapseTriangle(rect.Min, open, rect.Height, Color.Black, scale);
                rect.X += rect.Height;
                var delta = rect.Width - rect.Height;
                if (delta > 0)
                {
                    rect.Width = delta;
                }
                d.DrawText(rect, text, style, state);
            }

            // style restore
            style.PopStyle();  //-1
            style.PopStyle(4); //-4

            return(open);
        }
示例#18
0
文件: Label.cs 项目: jangocheng/ImGui
 public static void RenderBullet(this DrawList drawList, Point pos, double lineHeight, Color color)
 {
     drawList.AddCircleFilled(pos, (float)lineHeight * 0.20f, color, 8);
 }
示例#19
0
        /// <summary>
        /// Draw an image content
        /// </summary>
        /// <param name="drawList"></param>
        /// <param name="rect">the rect to draw this image content</param>
        /// <param name="texture">the texture</param>
        /// <param name="style">style of the image content</param>
        public static void DrawImage(this DrawList drawList, Rect rect, ITexture texture, GUIStyle style, GUIState state)
        {
            var uvMin = new Point(
                style.Get <double>(GUIStyleName.MinTextureCoordinateU, state),
                style.Get <double>(GUIStyleName.MinTextureCoordinateV, state));
            var uvMax = new Point(
                style.Get <double>(GUIStyleName.MaxTextureCoordinateU, state),
                style.Get <double>(GUIStyleName.MaxTextureCoordinateV, state));

            drawList.AddImage(texture, rect.TopLeft, rect.BottomRight, uvMin, uvMax, Color.White);//TODO apply tint color
#if false
            {
                var(top, right, bottom, left) = style.BorderImageSlice;
                Point uv0 = new Point(left / texture.Width, top / texture.Height);
                Point uv1 = new Point(1 - right / texture.Width, 1 - bottom / texture.Height);

                //     | L |   | R |
                // ----a---b---c---+
                //   T | 1 | 2 | 3 |
                // ----d---e---f---g
                //     | 4 | 5 | 6 |
                // ----h---i---j---k
                //   B | 7 | 8 | 9 |
                // ----+---l---m---n

                var a = rect.TopLeft;
                var b = a + new Vector(left, 0);
                var c = rect.TopRight + new Vector(-right, 0);

                var d = a + new Vector(0, top);
                var e = b + new Vector(0, top);
                var f = c + new Vector(0, top);
                var g = f + new Vector(right, 0);

                var h = rect.BottomLeft + new Vector(0, -bottom);
                var i = h + new Vector(left, 0);
                var j = rect.BottomRight + new Vector(-right, -bottom);
                var k = j + new Vector(right, 0);

                var l = i + new Vector(0, bottom);
                var m = rect.BottomRight + new Vector(-right, 0);
                var n = rect.BottomRight;

                var uv_a = new Point(0, 0);
                var uv_b = new Point(uv0.X, 0);
                var uv_c = new Point(uv1.X, 0);

                var uv_d = new Point(0, uv0.Y);
                var uv_e = new Point(uv0.X, uv0.Y);
                var uv_f = new Point(uv1.X, uv0.Y);
                var uv_g = new Point(1, uv0.Y);

                var uv_h = new Point(0, uv1.Y);
                var uv_i = new Point(uv0.X, uv1.Y);
                var uv_j = new Point(uv1.X, uv1.Y);
                var uv_k = new Point(1, uv1.Y);

                var uv_l = new Point(uv0.X, 1);
                var uv_m = new Point(uv1.X, 1);
                var uv_n = new Point(1, 1);

                //TODO merge these draw-calls (each call of AddImage will introduce a draw call)
                drawList.AddImage(texture, a, e, uv_a, uv_e, Color.White); //1
                drawList.AddImage(texture, b, f, uv_b, uv_f, Color.White); //2
                drawList.AddImage(texture, c, g, uv_c, uv_g, Color.White); //3
                drawList.AddImage(texture, d, i, uv_d, uv_i, Color.White); //4
                drawList.AddImage(texture, e, j, uv_e, uv_j, Color.White); //5
                drawList.AddImage(texture, f, k, uv_f, uv_k, Color.White); //6
                drawList.AddImage(texture, h, l, uv_h, uv_l, Color.White); //7
                drawList.AddImage(texture, i, m, uv_i, uv_m, Color.White); //8
                drawList.AddImage(texture, j, n, uv_j, uv_n, Color.White); //9
            }
#endif
        }
示例#20
0
        internal static bool DoPolygonButton(Rect rect, IReadOnlyList <Point> points, Rect textRect, string text)
        {
            Form       form     = Form.current;
            GUIContext g        = form.uiContext;
            Window     window   = g.WindowManager.CurrentWindow;
            DrawList   d        = window.DrawList;
            int        id       = window.GetID(text);
            var        mousePos = Mouse.Instance.Position;

            var clicked = false;
            var hovered = MathEx.IsPointInPolygon(mousePos, points, new Vector(rect.X, rect.Y));

            textRect.Offset(rect.X, rect.Y);

            //control logic
            g.KeepAliveID(id);
            if (hovered)
            {
                g.SetHoverID(id);

                if (Mouse.Instance.LeftButtonPressed)//start track
                {
                    g.SetActiveID(id);
                }

                if (Mouse.Instance.LeftButtonReleased)//end track
                {
                    clicked = true;
                    g.SetActiveID(GUIContext.None);
                }
            }

            // ui representation
            var state = GUI.Normal;

            if (hovered)
            {
                state = GUI.Hover;
                if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down)
                {
                    state = GUI.Active;
                }
            }

            // ui painting
            {
                var style = GUISkin.Instance[GUIControlName.PolygonButton];

                d.PathClear();
                foreach (var point in points)
                {
                    d.PathMoveTo(point + new Vector(rect.X, rect.Y));
                }
                d.PathFill(style.FillColor);

                d.PathClear();
                foreach (var point in points)
                {
                    d.PathMoveTo(point + new Vector(rect.X, rect.Y));
                }
                d.PathStroke(style.LineColor, true, 2);

                d.DrawBoxModel(textRect, text, style, state);
            }

            return(clicked);
        }
示例#21
0
        public Window(string name, Point position, Size size, WindowFlags Flags)
        {
            Form       form = Form.current;
            GUIContext g    = form.uiContext;

            this.ID   = name.GetHashCode();
            this.Name = name;
            this.IDStack.Push(this.ID);
            this.Flags    = Flags;
            this.PosFloat = position;
            this.Position = new Point((int)PosFloat.X, (int)PosFloat.Y);
            this.Size     = this.FullSize = size;
            this.DrawList = new DrawList();
            this.MoveID   = GetID("#MOVE");
            this.Active   = WasActive = false;

            // window styles
            {
                var style = new GUIStyle();
                style.Set(GUIStyleName.BorderTop, 1.0);
                style.Set(GUIStyleName.BorderRight, 1.0);
                style.Set(GUIStyleName.BorderBottom, 1.0);
                style.Set(GUIStyleName.BorderLeft, 1.0);
                style.Set(GUIStyleName.PaddingTop, 5.0);
                style.Set(GUIStyleName.PaddingRight, 10.0);
                style.Set(GUIStyleName.PaddingBottom, 5.0);
                style.Set(GUIStyleName.PaddingLeft, 10.0);
                style.Set(GUIStyleName.WindowBorderColor, Color.Rgb(170, 170, 170), GUIState.Normal);
                style.Set(GUIStyleName.WindowBorderColor, Color.Rgb(24, 131, 215), GUIState.Active);
                style.Set(GUIStyleName.WindowShadowColor, Color.Argb(100, 227, 227, 227));
                style.Set(GUIStyleName.WindowShadowWidth, 15.0);
                style.Set(GUIStyleName.BackgroundColor, Color.White);
                style.Set(GUIStyleName.ResizeGripSize, 20.0);
                style.Set(GUIStyleName.ResizeGripColor, Color.Argb(75, 102, 102, 102));
                style.Set(GUIStyleName.ResizeGripColor, Color.Argb(150, 102, 102, 102), GUIState.Hover);
                style.Set(GUIStyleName.ResizeGripColor, Color.Argb(225, 102, 102, 102), GUIState.Active);
                style.Set(GUIStyleName.WindowRounding, 3.0);
                style.Set(GUIStyleName.ScrollBarWidth, CurrentOS.IsDesktopPlatform ? 10.0 : 20.0);
                style.Set(GUIStyleName.ScrollBarBackgroundColor, Color.Rgb(240));
                style.Set(GUIStyleName.ScrollBarButtonColor, Color.Rgb(205), GUIState.Normal);
                style.Set(GUIStyleName.ScrollBarButtonColor, Color.Rgb(166), GUIState.Hover);
                style.Set(GUIStyleName.ScrollBarButtonColor, Color.Rgb(96), GUIState.Active);
                this.Style = style;
            }

            // window header styles
            {
                var style = new GUIStyle();
                style.Set(GUIStyleName.BackgroundColor, Color.White);
                style.Set(GUIStyleName.BackgroundColor, Color.White, GUIState.Active);
                style.Set(GUIStyleName.BackgroundColor, Color.White, GUIState.Disabled);
                style.Set <double>(GUIStyleName.BorderTopLeftRadius, 3.0);
                style.Set <double>(GUIStyleName.BorderTopRightRadius, 3.0);
                style.Set(GUIStyleName.PaddingTop, 8.0);
                style.Set(GUIStyleName.PaddingRight, 8.0);
                style.Set(GUIStyleName.PaddingBottom, 8.0);
                style.Set(GUIStyleName.PaddingLeft, 8.0);
                style.Set(GUIStyleName.FontColor, Color.Black, GUIState.Normal);
                style.Set(GUIStyleName.FontColor, Color.Rgb(153, 153, 153), GUIState.Active);
                style.FontFamily   = GUIStyle.Default.FontFamily;
                style.FontSize     = 12.0;
                this.TitleBarStyle = style;
            }

            var scrollBarWidth = this.Style.Get <double>(GUIStyleName.ScrollBarWidth);
            var clientSize     = new Size(
                this.Size.Width - scrollBarWidth - this.Style.PaddingHorizontal - this.Style.BorderHorizontal,
                this.Size.Height - this.Style.PaddingVertical - this.Style.BorderVertical - this.TitleBarHeight);

            this.StackLayout = new StackLayout(this.ID, clientSize);
        }
示例#22
0
        public static bool TreeNode(string label, ref bool open)
        {
            GUIContext g      = GetCurrentContext();
            Window     window = GetCurrentWindow();

            if (window.SkipItems)
            {
                return(false);
            }

            BeginVertical(label + "_Tree");

            var id = window.GetID(label);

            // style apply
            var style = GUIStyle.Basic;

            style.PushStretchFactor(false, 1);  //+1, always expand width
            style.PushPadding((1, 1, 1, 5));    //+4

            do
            {
                // rect
                var  lineHeight = style.GetLineHeight();
                Rect rect       = window.GetRect(id, new Size(0, lineHeight));
                if (rect == Layout.StackLayout.DummyRect)    //TODO how shold dummy rect be correctly handled in every control?
                {
                    break;
                }

                // interact
                bool hovered, held;
                bool pressed = GUIBehavior.ButtonBehavior(rect, id, out hovered, out held, ButtonFlags.PressedOnClick);
                if (pressed)
                {
                    open = !open;
                }

                // render
                {
                    DrawList d     = window.DrawList;
                    var      state = (hovered && held) ? GUIState.Active : hovered ? GUIState.Hover : GUIState.Normal;
                    if (hovered || held)
                    {
                        style.PushBgColor(new Color(0.40f, 0.40f, 0.90f, 0.45f), GUIState.Normal);   //+1 TODO It's stupid to sprcifiy style like this. There should be a better way to do this.
                        style.PushBgColor(new Color(0.45f, 0.45f, 0.90f, 0.80f), GUIState.Hover);    //+1
                        style.PushBgColor(new Color(0.53f, 0.53f, 0.87f, 0.80f), GUIState.Active);   //+1
                        var color = style.Get <Color>(GUIStyleName.BackgroundColor, state);
                        d.RenderFrame(rect.Min, rect.Max, color, false, 0);
                        style.PopStyle(3);    //-3
                    }
                    d.RenderCollapseTriangle(rect.Min + new Vector(0 + style.PaddingTop, lineHeight * 0.15f), open, lineHeight, Color.White, 0.7);
                    rect.X += rect.Height;
                    var delta = rect.Width - rect.Height;
                    if (delta > 0)
                    {
                        rect.Width = delta;
                    }
                    d.DrawText(rect, label, style, state);
                }
            }while(false);

            // style restore
            style.PopStyle();    //-1
            style.PopStyle(4);   //-4

            BeginHorizontal("#Content");
            Space("Space", 20);
            BeginVertical("#Items");
            return(open);
        }