コード例 #1
0
ファイル: Demo.cs プロジェクト: rds1983/NvgSharp
        public static void drawButton(NvgContext vg, string preicon, string text, float x, float y, float w, float h, Color col)
        {
            Paint bg;
            float cornerRadius = 4.0f;
            float tw = 0, iw = 0;

            bg = vg.LinearGradient(x, y, x, y + h, new Color(255, 255, 255, isBlack(col) ? 16 : 32), new Color(0, 0, 0, isBlack(col) ? 16 : 32));
            vg.BeginPath();
            vg.RoundedRect(x + 1, y + 1, w - 2, h - 2, cornerRadius - 1);
            if (!isBlack(col))
            {
                vg.FillColor(col);
                vg.Fill();
            }
            vg.FillPaint(bg);
            vg.Fill();

            vg.BeginPath();
            vg.RoundedRect(x + 0.5f, y + 0.5f, w - 1, h - 1, cornerRadius - 0.5f);
            vg.StrokeColor(new Color(0, 0, 0, 48));
            vg.Stroke();

            vg.FontSize(20.0f);
            vg.FontFace("sans-bold");
            Bounds bounds = new Bounds();

            tw = vg.TextBounds(0, 0, text, ref bounds);
            if (!string.IsNullOrEmpty(preicon))
            {
                vg.FontSize(h * 1.3f);
                vg.FontFace("icons");
                iw  = vg.TextBounds(0, 0, preicon, ref bounds);
                iw += h * 0.15f;
            }

            if (!string.IsNullOrEmpty(preicon))
            {
                vg.FontSize(h * 1.3f);
                vg.FontFace("icons");
                vg.FillColor(new Color(255, 255, 255, 96));
                vg.TextAlign(Alignment.Left | Alignment.Middle);
                vg.Text(x + w * 0.5f - tw * 0.5f - iw * 0.75f, y + h * 0.5f, preicon);
            }

            vg.FontSize(20.0f);
            vg.FontFace("sans-bold");
            vg.TextAlign(Alignment.Left | Alignment.Middle);
            vg.FillColor(new Color(0, 0, 0, 160));
            vg.Text(x + w * 0.5f - tw * 0.5f + iw * 0.25f, y + h * 0.5f - 1, text);
            vg.FillColor(new Color(255, 255, 255, 160));
            vg.Text(x + w * 0.5f - tw * 0.5f + iw * 0.25f, y + h * 0.5f, text);
        }
コード例 #2
0
ファイル: Demo.cs プロジェクト: rds1983/NvgSharp
        public static void drawEditBoxNum(NvgContext vg,
                                          string text, string units, float x, float y, float w, float h)
        {
            float uw;

            drawEditBoxBase(vg, x, y, w, h);

            Bounds bounds = new Bounds();

            uw = vg.TextBounds(0, 0, units, ref bounds);

            vg.FontSize(18.0f);
            vg.FontFace("sans");
            vg.FillColor(new Color(255, 255, 255, 64));
            vg.TextAlign(Alignment.Right | Alignment.Middle);
            vg.Text(x + w - h * 0.3f, y + h * 0.5f, units);

            vg.FontSize(20.0f);
            vg.FontFace("sans");
            vg.FillColor(new Color(255, 255, 255, 128));
            vg.TextAlign(Alignment.Right | Alignment.Middle);
            vg.Text(x + w - uw - h * 0.5f, y + h * 0.5f, text);
        }
コード例 #3
0
ファイル: Demo.cs プロジェクト: rds1983/NvgSharp
        public static void drawParagraph(NvgContext vg, float x, float y, float width, float height, float mx, float my)
        {
            TextRow[]       rows   = new TextRow[3];
            GlyphPosition[] glyphs = new GlyphPosition[100];
            string          text   = "This is longer chunk of text.\n  \n  Would have used lorem ipsum but she    was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party.";
            StringSegment   start;
            int             nrows, i, nglyphs, j, lnum = 0;
            float           lineh;
            float           caretx, px;
            Bounds          bounds = new Bounds();
            float           a;
            float           gx = 0, gy = 0;
            int             gutter = 0;

            for (i = 0; i < rows.Length; ++i)
            {
                rows[i] = new TextRow();
            }

            vg.Save();

            vg.FontSize(18.0f);
            vg.FontFace("sans");
            vg.TextAlign(Alignment.Left | Alignment.Top);

            float ascender, descender;

            vg.TextMetrics(out ascender, out descender, out lineh);

            // The text break API can be used to fill a large buffer of rows,
            // or to iterate over the text just few lines (or just one) at a time.
            // The "next" variable of the last returned item tells where to continue.
            start = text;
            while (true)
            {
                nrows = vg.TextBreakLines(start, width, rows, out start);

                if (nrows <= 0)
                {
                    break;
                }
                for (i = 0; i < nrows; i++)
                {
                    TextRow row = rows[i];
                    var     hit = mx > x && mx < (x + width) && my >= y && my < (y + lineh);

                    vg.BeginPath();
                    vg.FillColor(new Color(255, 255, 255, hit ? 64 : 16));
                    vg.Rect(x, y, row.Width, lineh);
                    vg.Fill();

                    vg.FillColor(new Color(255, 255, 255, 255));
                    vg.Text(x, y, row.Str);

                    if (hit)
                    {
                        caretx  = (mx < x + row.Width / 2) ? x : x + row.Width;
                        px      = x;
                        nglyphs = vg.TextGlyphPositions(x, y, row.Str, glyphs);
                        for (j = 0; j < nglyphs; j++)
                        {
                            float x0  = glyphs[j].X;
                            float x1  = (j + 1 < nglyphs) ? glyphs[j + 1].X : x + row.Width;
                            float gx2 = x0 * 0.3f + x1 * 0.7f;
                            if (mx >= px && mx < gx2)
                            {
                                caretx = glyphs[j].X;
                            }
                            px = gx2;
                        }
                        vg.BeginPath();
                        vg.FillColor(new Color(255, 192, 0, 255));
                        vg.Rect(caretx, y, 1, lineh);
                        vg.Fill();

                        gutter = lnum + 1;
                        gx     = x - 10;
                        gy     = y + lineh / 2;
                    }
                    lnum++;
                    y += lineh;
                }
            }

            if (gutter > 0)
            {
                string txt = gutter.ToString();
                vg.FontSize(13.0f);
                vg.TextAlign(Alignment.Right | Alignment.Middle);

                vg.TextBounds(gx, gy, txt, ref bounds);

                vg.BeginPath();
                vg.FillColor(new Color(255, 192, 0, 255));
                vg.RoundedRect((int)bounds.b1 - 4, (int)bounds.b2 - 2,
                               (int)(bounds.b3 - bounds.b1) + 8,
                               (int)(bounds.b4 - bounds.b2) + 4,
                               ((int)(bounds.b4 - bounds.b2) + 4) / 2 - 1);
                vg.Fill();

                vg.FillColor(new Color(32, 32, 32, 255));
                vg.Text(gx, gy, txt);
            }

            y += 20.0f;

            vg.FontSize(13.0f);
            vg.TextAlign(Alignment.Left | Alignment.Top);
            vg.TextLineHeight(1.2f);

            vg.TextBoxBounds(x, y, 150, "Hover your mouse over the text to see calculated caret position.", ref bounds);

            // Fade the tooltip out when close to it.
            gx = (float)Math.Abs((mx - (bounds.b1 + bounds.b3) * 0.5f) / (bounds.b1 - bounds.b3));
            gy = (float)Math.Abs((my - (bounds.b2 + bounds.b4) * 0.5f) / (bounds.b2 - bounds.b4));
            a  = maxf(gx, gy) - 0.5f;
            a  = clampf(a, 0, 1);
            vg.GlobalAlpha(a);

            vg.BeginPath();
            vg.FillColor(new Color(220, 220, 220, 255));
            vg.RoundedRect(bounds.b1 - 2, bounds.b2 - 2, (int)(bounds.b3 - bounds.b1) + 4, (int)(bounds.b4 - bounds.b2) + 4, 3);
            px = (int)((bounds.b3 + bounds.b1) / 2);
            vg.MoveTo(px, bounds.b2 - 10);
            vg.LineTo(px + 7, bounds.b2 + 1);
            vg.LineTo(px - 7, bounds.b2 + 1);
            vg.Fill();

            vg.FillColor(new Color(0, 0, 0, 220));
            vg.TextBox(x, y, 150, "Hover your mouse over the text to see calculated caret position.");

            vg.Restore();
        }