Ejemplo n.º 1
0
        public unsafe ImGuiHelper(Game game)
        {
            this.game                = game;
            game.Keyboard.KeyDown   += Keyboard_KeyDown;
            game.Keyboard.KeyUp     += Keyboard_KeyUp;
            game.Keyboard.TextInput += Keyboard_TextInput;
            context = ImGui.CreateContext();
            ImGui.SetCurrentContext(context);
            SetKeyMappings();

            var io = ImGui.GetIO();

            //IniFilename?
            io.WantSaveIniSettings = false;
            Default = io.Fonts.AddFontDefault();
            using (var stream = typeof(ImGuiHelper).Assembly.GetManifestResourceStream("LibreLancer.ImUI.Roboto-Medium.ttf"))
            {
                var ttf = new byte[stream.Length];
                stream.Read(ttf, 0, ttf.Length);
                ttfPtr = Marshal.AllocHGlobal(ttf.Length);
                Marshal.Copy(ttf, 0, ttfPtr, ttf.Length);
                Noto = io.Fonts.AddFontFromMemoryTTF(ttfPtr, ttf.Length, 15);
            }
            using (var stream = typeof(ImGuiHelper).Assembly.GetManifestResourceStream("LibreLancer.ImUI.checkerboard.png"))
            {
                checkerboard   = LibreLancer.ImageLib.Generic.FromStream(stream);
                CheckerboardId = RegisterTexture(checkerboard);
            }
            ImGuiExt.BuildFontAtlas((IntPtr)io.Fonts.NativePtr);
            byte *fontBytes;
            int   fontWidth, fontHeight;

            io.Fonts.GetTexDataAsAlpha8(out fontBytes, out fontWidth, out fontHeight);
            io.Fonts.TexUvWhitePixel = new Vector2(10, 10);
            fontTexture = new Texture2D(fontWidth, fontHeight, false, SurfaceFormat.R8);
            var bytes = new byte[fontWidth * fontHeight];

            Marshal.Copy((IntPtr)fontBytes, bytes, 0, fontWidth * fontHeight);
            fontTexture.SetData(bytes);
            fontTexture.SetFiltering(TextureFiltering.Linear);
            io.Fonts.SetTexID((IntPtr)FONT_TEXTURE_ID);
            io.Fonts.ClearTexData();
            textShader  = new Shader(vertex_source, text_fragment_source);
            colorShader = new Shader(vertex_source, color_fragment_source);
            dot         = new Texture2D(1, 1, false, SurfaceFormat.Color);
            var c = new Color4b[] { Color4b.White };

            dot.SetData(c);
            Theme.Apply();
            //Required for clipboard function on non-Windows platforms
            utf8buf    = Marshal.AllocHGlobal(8192);
            instance   = this;
            setTextDel = SetClipboardText;
            getTextDel = GetClipboardText;

            io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(getTextDel);
            io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(setTextDel);
        }
Ejemplo n.º 2
0
        public unsafe ImGuiHelper(Game game)
        {
            this.game                = game;
            game.Keyboard.KeyDown   += Keyboard_KeyDown;
            game.Keyboard.KeyUp     += Keyboard_KeyUp;
            game.Keyboard.TextInput += Keyboard_TextInput;
            context = ImGui.CreateContext();
            ImGui.SetCurrentContext(context);
            SetKeyMappings();
            var io = ImGui.GetIO();

            io.WantSaveIniSettings    = false;
            io.NativePtr->IniFilename = (byte *)0; //disable ini!!
            var fontConfigA = new ImFontConfigPtr(ImFontConfig_ImFontConfig());
            var fontConfigB = new ImFontConfigPtr(ImFontConfig_ImFontConfig());
            var fontConfigC = new ImFontConfigPtr(ImFontConfig_ImFontConfig());

            ushort[] glyphRangesFull = new ushort[]
            {
                0x0020, 0x00FF, //Basic Latin + Latin Supplement,
                0x0400, 0x052F, //Cyrillic + Cyrillic Supplement
                0x2DE0, 0x2DFF, //Cyrillic Extended-A
                0xA640, 0xA69F, //Cyrillic Extended-B
                ImGuiExt.ReplacementHash, ImGuiExt.ReplacementHash,
                0
            };
            var rangesPtrFull = Marshal.AllocHGlobal(sizeof(short) * glyphRangesFull.Length);

            for (int i = 0; i < glyphRangesFull.Length; i++)
            {
                ((ushort *)rangesPtrFull)[i] = glyphRangesFull[i];
            }
            ushort[] glyphRangesLatin = new ushort[]
            {
                0x0020, 0x00FF, //Basic Latin + Latin Supplement
                ImGuiExt.ReplacementHash, ImGuiExt.ReplacementHash,
                0
            };
            var rangesPtrLatin = Marshal.AllocHGlobal(sizeof(short) * glyphRangesLatin.Length);

            for (int i = 0; i < glyphRangesLatin.Length; i++)
            {
                ((ushort *)rangesPtrLatin)[i] = glyphRangesLatin[i];
            }
            fontConfigA.GlyphRanges = rangesPtrLatin;
            fontConfigB.GlyphRanges = rangesPtrFull;
            fontConfigC.GlyphRanges = rangesPtrFull;
            Default = io.Fonts.AddFontDefault(fontConfigA);
            using (var stream = typeof(ImGuiHelper).Assembly.GetManifestResourceStream("LibreLancer.ImUI.Roboto-Medium.ttf"))
            {
                var ttf = new byte[stream.Length];
                stream.Read(ttf, 0, ttf.Length);
                ttfPtr = Marshal.AllocHGlobal(ttf.Length);
                Marshal.Copy(ttf, 0, ttfPtr, ttf.Length);
                Noto = io.Fonts.AddFontFromMemoryTTF(ttfPtr, ttf.Length, 15, fontConfigB);
            }
            using (var stream = typeof(ImGuiHelper).Assembly.GetManifestResourceStream("LibreLancer.ImUI.checkerboard.png"))
            {
                checkerboard   = LibreLancer.ImageLib.Generic.FromStream(stream);
                CheckerboardId = RegisterTexture(checkerboard);
            }
            var monospace = Platform.GetMonospaceBytes();

            fixed(byte *mmPtr = monospace)
            {
                SystemMonospace = io.Fonts.AddFontFromMemoryTTF((IntPtr)mmPtr, monospace.Length, 16, fontConfigC);
            }

            ImGuiExt.BuildFontAtlas((IntPtr)io.Fonts.NativePtr);
            byte *fontBytes;
            int   fontWidth, fontHeight;

            io.Fonts.GetTexDataAsAlpha8(out fontBytes, out fontWidth, out fontHeight);
            io.Fonts.TexUvWhitePixel = new Vector2(10, 10);
            fontTexture = new Texture2D(fontWidth, fontHeight, false, SurfaceFormat.R8);
            var bytes = new byte[fontWidth * fontHeight];

            Marshal.Copy((IntPtr)fontBytes, bytes, 0, fontWidth * fontHeight);
            fontTexture.SetData(bytes);
            fontTexture.SetFiltering(TextureFiltering.Linear);
            io.Fonts.SetTexID((IntPtr)FONT_TEXTURE_ID);
            io.Fonts.ClearTexData();
            textShader  = new Shader(vertex_source, text_fragment_source);
            colorShader = new Shader(vertex_source, color_fragment_source);
            dot         = new Texture2D(1, 1, false, SurfaceFormat.Color);
            var c = new Color4b[] { Color4b.White };

            dot.SetData(c);
            Theme.Apply();
            //Required for clipboard function on non-Windows platforms
            utf8buf    = Marshal.AllocHGlobal(8192);
            instance   = this;
            setTextDel = SetClipboardText;
            getTextDel = GetClipboardText;

            io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(getTextDel);
            io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(setTextDel);
        }
Ejemplo n.º 3
0
        public unsafe ImGuiHelper(Game game, float scale)
        {
            Scale                    = scale;
            this.game                = game;
            game.Keyboard.KeyDown   += Keyboard_KeyDown;
            game.Keyboard.KeyUp     += Keyboard_KeyUp;
            game.Keyboard.TextInput += Keyboard_TextInput;
            context                  = ImGui.CreateContext();
            ImGui.SetCurrentContext(context);
            SetKeyMappings();
            var io = ImGui.GetIO();

            io.WantSaveIniSettings    = false;
            io.NativePtr->IniFilename = (byte *)0; //disable ini!!
            var fontConfigA = new ImFontConfigPtr(ImFontConfig_ImFontConfig());
            var fontConfigB = new ImFontConfigPtr(ImFontConfig_ImFontConfig());
            var fontConfigC = new ImFontConfigPtr(ImFontConfig_ImFontConfig());

            ushort[] glyphRangesFull = new ushort[]
            {
                0x0020, 0x00FF, //Basic Latin + Latin Supplement,
                0x0400, 0x052F, //Cyrillic + Cyrillic Supplement
                0x2DE0, 0x2DFF, //Cyrillic Extended-A
                0xA640, 0xA69F, //Cyrillic Extended-B
                ImGuiExt.ReplacementHash, ImGuiExt.ReplacementHash,
                0
            };
            var rangesPtrFull = Marshal.AllocHGlobal(sizeof(short) * glyphRangesFull.Length);

            for (int i = 0; i < glyphRangesFull.Length; i++)
            {
                ((ushort *)rangesPtrFull)[i] = glyphRangesFull[i];
            }
            ushort[] glyphRangesLatin = new ushort[]
            {
                0x0020, 0x00FF, //Basic Latin + Latin Supplement
                ImGuiExt.ReplacementHash, ImGuiExt.ReplacementHash,
                0
            };
            var rangesPtrLatin = Marshal.AllocHGlobal(sizeof(short) * glyphRangesLatin.Length);

            for (int i = 0; i < glyphRangesLatin.Length; i++)
            {
                ((ushort *)rangesPtrLatin)[i] = glyphRangesLatin[i];
            }
            fontConfigA.GlyphRanges = rangesPtrLatin;
            fontConfigB.GlyphRanges = rangesPtrFull;
            fontConfigC.GlyphRanges = rangesPtrFull;

            Default = io.Fonts.AddFontDefault(fontConfigA);
            using (var stream = typeof(ImGuiHelper).Assembly.GetManifestResourceStream("LibreLancer.ImUI.Roboto-Medium.ttf"))
            {
                var ttf = new byte[stream.Length];
                stream.Read(ttf, 0, ttf.Length);
                ttfPtr = Marshal.AllocHGlobal(ttf.Length);
                Marshal.Copy(ttf, 0, ttfPtr, ttf.Length);
                Noto = io.Fonts.AddFontFromMemoryTTF(ttfPtr, ttf.Length, (int)(15 * Scale), fontConfigB);
            }

            using (var stream =
                       typeof(ImGuiHelper).Assembly.GetManifestResourceStream("LibreLancer.ImUI.fa-solid-900.ttf"))
            {
                var iconFontConfig = new ImFontConfigPtr(ImFontConfig_ImFontConfig());
                iconFontConfig.MergeMode        = true;
                iconFontConfig.GlyphMinAdvanceX = iconFontConfig.GlyphMaxAdvanceX = (int)(20 * Scale);
                var glyphs = new List <ushort>();
                foreach (var chars in Icons.GetChars())
                {
                    glyphs.Add(chars);
                    glyphs.Add(chars);
                }
                glyphs.Add(0);
                var rangesPtrIcon = Marshal.AllocHGlobal(sizeof(short) * glyphs.Count);
                for (int i = 0; i < glyphs.Count; i++)
                {
                    ((ushort *)rangesPtrIcon)[i] = glyphs[i];
                }
                iconFontConfig.GlyphRanges = rangesPtrIcon;
                var ttf = new byte[stream.Length];
                stream.Read(ttf, 0, ttf.Length);
                ttfPtr = Marshal.AllocHGlobal(ttf.Length);
                Marshal.Copy(ttf, 0, ttfPtr, ttf.Length);
                io.Fonts.AddFontFromMemoryTTF(ttfPtr, ttf.Length, (int)(15 * Scale), iconFontConfig);
            }

            using (var stream =
                       typeof(ImGuiHelper).Assembly.GetManifestResourceStream("LibreLancer.ImUI.empty-bullet.ttf"))
            {
                var iconFontConfig = new ImFontConfigPtr(ImFontConfig_ImFontConfig());
                iconFontConfig.MergeMode = true;
                var glyphs        = new ushort[] { Icons.BulletEmpty, Icons.BulletEmpty, 0 };
                var rangesPtrIcon = Marshal.AllocHGlobal(sizeof(short) * glyphs.Length);
                for (int i = 0; i < glyphs.Length; i++)
                {
                    ((ushort *)rangesPtrIcon)[i] = glyphs[i];
                }
                iconFontConfig.GlyphRanges = rangesPtrIcon;
                var ttf = new byte[stream.Length];
                stream.Read(ttf, 0, ttf.Length);
                ttfPtr = Marshal.AllocHGlobal(ttf.Length);
                Marshal.Copy(ttf, 0, ttfPtr, ttf.Length);
                io.Fonts.AddFontFromMemoryTTF(ttfPtr, ttf.Length, (int)(15 * Scale), iconFontConfig);
            }

            using (var stream = typeof(ImGuiHelper).Assembly.GetManifestResourceStream("LibreLancer.ImUI.checkerboard.png"))
            {
                checkerboard   = (Texture2D)LibreLancer.ImageLib.Generic.FromStream(stream);
                CheckerboardId = RegisterTexture(checkerboard);
            }
            var monospace = Platform.GetMonospaceBytes();

            fixed(byte *mmPtr = monospace)
            {
                SystemMonospace = io.Fonts.AddFontFromMemoryTTF((IntPtr)mmPtr, monospace.Length, (int)(16 * Scale), fontConfigC);
            }

            ImGuiExt.BuildFontAtlas((IntPtr)io.Fonts.NativePtr);
            byte *fontBytes;
            int   fontWidth, fontHeight;

            io.Fonts.GetTexDataAsRGBA32(out fontBytes, out fontWidth, out fontHeight);
            io.Fonts.TexUvWhitePixel = new Vector2(10, 10);
            Icons.TintGlyphs(fontBytes, fontWidth, fontHeight, Noto);
            fontTexture = new Texture2D(fontWidth, fontHeight, false, SurfaceFormat.Color);
            var bytes = new byte[fontWidth * fontHeight * 4];

            Marshal.Copy((IntPtr)fontBytes, bytes, 0, fontWidth * fontHeight * 4);
            fontTexture.SetData(bytes);
            fontTexture.SetFiltering(TextureFiltering.Linear);
            io.Fonts.SetTexID((IntPtr)FONT_TEXTURE_ID);
            io.Fonts.ClearTexData();
            string glslVer = RenderContext.GLES ? "300 es\nprecision mediump float;" : "140";

            textShader  = new Shader(vertex_source.Replace("{0}", glslVer), text_fragment_source.Replace("{0}", glslVer));
            colorShader = new Shader(vertex_source.Replace("{0}", glslVer), color_fragment_source.Replace("{0}", glslVer));
            dot         = new Texture2D(1, 1, false, SurfaceFormat.Color);
            var c = new Color4b[] { Color4b.White };

            dot.SetData(c);
            Theme.Apply(scale);
            //Required for clipboard function on non-Windows platforms
            utf8buf    = Marshal.AllocHGlobal(8192);
            instance   = this;
            setTextDel = SetClipboardText;
            getTextDel = GetClipboardText;

            io.GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(getTextDel);
            io.SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(setTextDel);
        }