コード例 #1
0
ファイル: NanoVG.cs プロジェクト: WildGenie/Libraria
        public static float[] TextBounds(float X, float Y, string Txt, out float W, out float H)
        {
            float[] Bounds = new float[4];
            NVG.TextBounds(Engine.NVGCtx, X, Y, Txt, IntPtr.Zero, Bounds);

            W = Bounds[2] - Bounds[0];
            H = Bounds[3] - Bounds[1];
            return(Bounds);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: masums/NetCoreNanoVG
        static unsafe void DrawButton(NanoVGContext vg, int preicon, string text, float x, float y, float w, float h, NVGColor col)
        {
            NVGPaint bg;
            int      icon = 0xE740;
            float    cornerRadius = 4.0f;
            float    tw = 0, iw = 0;

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

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

            NVG.FontSize(vg, 20.0f);
            NVG.FontFace(vg, "sans-bold");
            tw = NVG.TextBounds(vg, 0, 0, Encoding.UTF8.GetBytes(text), null, null);
            if (preicon != 0)
            {
                NVG.FontSize(vg, h * 1.3f);
                NVG.FontFace(vg, "icons");
                iw  = NVG.TextBounds(vg, 0, 0, cpToUTF8(icon), null, null);
                iw += h * 0.15f;
            }

            if (preicon != 0)
            {
                NVG.FontSize(vg, h * 1.3f);
                NVG.FontFace(vg, "icons");
                NVG.FillColor(vg, NVG.RGBA(255, 255, 255, 96));
                NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_LEFT | NVGAlign.NVG_ALIGN_MIDDLE);
                NVG.Text(vg, x + w * 0.5f - tw * 0.5f - iw * 0.75f, y + h * 0.5f, cpToUTF8(icon), null);
            }

            NVG.FontSize(vg, 20.0f);
            NVG.FontFace(vg, "sans-bold");
            NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_LEFT | NVGAlign.NVG_ALIGN_MIDDLE);
            NVG.FillColor(vg, NVG.RGBA(0, 0, 0, 160));
            NVG.Text(vg, x + w * 0.5f - tw * 0.5f + iw * 0.25f, y + h * 0.5f - 1, text, null);
            NVG.FillColor(vg, NVG.RGBA(255, 255, 255, 160));
            NVG.Text(vg, x + w * 0.5f - tw * 0.5f + iw * 0.25f, y + h * 0.5f, text, null);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: masums/NetCoreNanoVG
        static unsafe void DrawEditBoxNum(NanoVGContext vg, string text, byte[] units, float x, float y, float w, float h)
        {
            float uw;

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

            uw = NVG.TextBounds(vg, 0, 0, units, null, null);

            NVG.FontSize(vg, 18.0f);
            NVG.FontFace(vg, "sans");
            NVG.FillColor(vg, NVG.RGBA(255, 255, 255, 64));
            NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_RIGHT | NVGAlign.NVG_ALIGN_MIDDLE);
            NVG.Text(vg, x + w - h * 0.3f, y + h * 0.5f, units, null);

            NVG.FontSize(vg, 20.0f);
            NVG.FontFace(vg, "sans");
            NVG.FillColor(vg, NVG.RGBA(255, 255, 255, 128));
            NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_RIGHT | NVGAlign.NVG_ALIGN_MIDDLE);
            NVG.Text(vg, x + w - uw - h * 0.5f, y + h * 0.5f, text, null);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: masums/NetCoreNanoVG
        static unsafe void DrawParagraph(NanoVGContext vg, float x, float y, float width, float height, float mx, float my)
        {
            NVGtextRow[]       rows   = new NVGtextRow[3];
            NVGglyphPosition[] glyphs = new NVGglyphPosition[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.🎉";
            string             start;
            string             end;
            int   nrows, i, nglyphs, j, lnum = 0;
            float lineh;
            float caretx, px;

            float[] bounds = new float[4];
            float   a;
            float   gx = 0, gy = 0;
            int     gutter = 0;

            //NVG_NOTUSED(height);

            NVG.Save(vg);

            NVG.FontSize(vg, 18.0f);
            NVG.FontFace(vg, "sans");
            NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_LEFT | NVGAlign.NVG_ALIGN_TOP);
            NVG.TextMetrics(vg, null, null, &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;
            end   = text + text.Length;
            IntPtr rowPtr = NVG.GetIntPtrFromStructArray(rows);

            //GCHandle gch = GCHandle.Alloc(rows, GCHandleType.Normal);
            //IntPtr rowPtr = GCHandle.ToIntPtr(gch);
            nrows = NVG.TextBreakLines(vg, Encoding.UTF8.GetBytes(start), Encoding.UTF8.GetBytes(end), width, ref rowPtr, 3);

            var cRows = NVG.GetStructArrayFromIntPtr <NVGtextRow>(rowPtr, nrows);

            while (nrows > 0)
            {
                for (i = 0; i < nrows; i++)
                {
                    NVGtextRow row = rows[i];
                    bool       hit = mx > x && mx < (x + width) && my >= y && my < (y + lineh);

                    NVG.BeginPath(vg);
                    NVG.FillColor(vg, NVG.RGBA(255, 255, 255, hit ? (byte)64 : (byte)16));
                    NVG.Rect(vg, x, y, row.width, lineh);
                    NVG.Fill(vg);

                    NVG.FillColor(vg, NVG.RGBA(255, 255, 255, 255));
                    NVG.Text(vg, x, y, row.start, row.end);

                    if (hit)
                    {
                        caretx = (mx < x + row.width / 2) ? x : x + row.width;
                        px     = x;
                        IntPtr positions = NVG.MarshalArrayToPointer(glyphs);
                        nglyphs = NVG.TextGlyphPositions(vg, x, y, NVG.StrToByte(row.start), NVG.StrToByte(row.end), positions, 100);
                        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;
                        }
                        NVG.BeginPath(vg);
                        NVG.FillColor(vg, NVG.RGBA(255, 192, 0, 255));
                        NVG.Rect(vg, caretx, y, 1, lineh);
                        NVG.Fill(vg);

                        gutter = lnum + 1;
                        gx     = x - 10;
                        gy     = y + lineh / 2;
                    }
                    lnum++;
                    y += lineh;
                }
                // Keep going...
                start = rows[nrows - 1].next;
                //nrows = NVG.TextBreakLines(vg, Encoding.UTF8.GetBytes(start), Encoding.UTF8.GetBytes(end), width, rowPtr, 3);
            }

            if (gutter > 0)
            {
                string txt;
                txt = "16" + gutter;
                NVG.FontSize(vg, 13.0f);
                NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_RIGHT | NVGAlign.NVG_ALIGN_MIDDLE);

                NVG.TextBounds(vg, gx, gy, Encoding.UTF8.GetBytes(txt), null, NVG.MarshalToPointer(bounds));

                NVG.BeginPath(vg);
                NVG.FillColor(vg, NVG.RGBA(255, 192, 0, 255));
                NVG.RoundedRect(vg, (int)bounds[0] - 4, (int)bounds[1] - 2, (int)(bounds[2] - bounds[0]) + 8, (int)(bounds[3] - bounds[1]) + 4, ((int)(bounds[3] - bounds[1]) + 4) / 2 - 1);
                NVG.Fill(vg);

                NVG.FillColor(vg, NVG.RGBA(32, 32, 32, 255));
                NVG.Text(vg, gx, gy, txt, null);
            }

            y += 20.0f;

            NVG.FontSize(vg, 13.0f);
            NVG.TextAlign(vg, NVGAlign.NVG_ALIGN_LEFT | NVGAlign.NVG_ALIGN_TOP);
            NVG.TextLineHeight(vg, 1.2f);

            IntPtr boundPtr = NVG.MarshalArrayToPointer(glyphs);

            NVG.TextBoxBounds(vg, x, y, 150, Encoding.UTF8.GetBytes("Hover your mouse over the text to see calculated caret position."), null, boundPtr);

            // Fade the tooltip out when close to it.
            gx = Math.Abs((mx - (bounds[0] + bounds[2]) * 0.5f) / (bounds[0] - bounds[2]));
            gy = Math.Abs((my - (bounds[1] + bounds[3]) * 0.5f) / (bounds[1] - bounds[3]));
            a  = Math.Max(gx, gy) - 0.5f;
            a  = Math.Clamp(a, 0, 1);
            NVG.GlobalAlpha(vg, a);

            NVG.BeginPath(vg);
            NVG.FillColor(vg, NVG.RGBA(220, 220, 220, 255));
            NVG.RoundedRect(vg, bounds[0] - 2, bounds[1] - 2, (int)(bounds[2] - bounds[0]) + 4, (int)(bounds[3] - bounds[1]) + 4, 3);
            px = (int)((bounds[2] + bounds[0]) / 2);
            NVG.MoveTo(vg, px, bounds[1] - 10);
            NVG.LineTo(vg, px + 7, bounds[1] + 1);
            NVG.LineTo(vg, px - 7, bounds[1] + 1);
            NVG.Fill(vg);

            NVG.FillColor(vg, NVG.RGBA(0, 0, 0, 220));
            NVG.TextBox(vg, x, y, 150, Encoding.UTF8.GetBytes("Hover your mouse over the text to see calculated caret position."), null);

            NVG.Restore(vg);
        }