Example #1
0
        public static void DrawSpinner(ImGuiController controller, int count, uint colour)
        {
            if (controller._fontTexture is null)
            {
                return;
            }

            ImFontGlyphPtr glyph = controller.UnicodeFont.FindGlyph(ImGuiController.FA_ICON_ROTATION);

            ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Vector2.Zero);
            IntPtr CPUframeBufferTextureId = controller.GetOrCreateImGuiBinding(controller.GraphicsDevice.ResourceFactory, controller._fontTexture, "Spinner");

            ImGui.SetCursorPos(ImGui.GetCursorPos() + new Vector2(3, 3));
            Vector2 size   = new Vector2(18, 18);
            Vector2 corner = ImGui.GetCursorScreenPos() + new Vector2(0, size.Y);
            Vector2 center = corner + new Vector2(size.X * 0.5f, size.Y * -0.5f);

            float rotation = -1 * ((float)DateTime.Now.TimeOfDay.TotalMilliseconds / 360);
            float cos_a    = (float)Math.Cos(rotation);
            float sin_a    = (float)Math.Sin(rotation);

            Vector2[] pos = new Vector2[]
            {
                center + ImRotate(new Vector2(-size.X, -size.Y) * 0.5f, cos_a, sin_a),
                center + ImRotate(new Vector2(+size.X, -size.Y) * 0.5f, cos_a, sin_a),
                center + ImRotate(new Vector2(+size.X, +size.Y) * 0.5f, cos_a, sin_a),
                center + ImRotate(new Vector2(-size.X, +size.Y) * 0.5f, cos_a, sin_a)
            };

            ImGui.GetWindowDrawList().AddImageQuad(CPUframeBufferTextureId, pos[0], pos[1], pos[2], pos[3],
                                                   new Vector2(glyph.U0, glyph.V0), new Vector2(glyph.U1, glyph.V0), new Vector2(glyph.U1, glyph.V1), new Vector2(glyph.U0, glyph.V1), colour);
            if (count > 1)
            {
                ImGui.SetCursorPos(ImGui.GetCursorPos() + new Vector2(size.X, 4));
                ImGui.Text($"{count}");
                ImGui.SetCursorPosY(ImGui.GetCursorPosY() - size.Y);
                ImGui.InvisibleButton($"#invisBtn{rotation}", new Vector2(18, 18));
            }
            else
            {
                //tooltip hover target
                ImGui.InvisibleButton($"#invisBtn{rotation}", new Vector2(18, 24));
            }

            ImGui.PopStyleVar();
        }
Example #2
0
        public void OnGUI()
        {
            var fonts = ImGui.GetIO().Fonts.Fonts;

            for (var i = 0; i < fonts.Size; i++)
            {
                var font = fonts[i];
                ImGui.PushFont(font);//临时换字体
                ImFontGlyphPtr fontGlyphPtr = font.FindGlyphNoFallback('中');
                unsafe
                {
                    if (fontGlyphPtr.NativePtr != null)
                    {
                        ImGui.Text("这是一个中文 \"关于\" 窗口。");
                    }
                    else
                    {
                        ImGui.Text("this is a english \"about\" window.");
                    }
                }
                ImGui.PopFont();
            }
        }
Example #3
0
        public unsafe IconFont(GraphicsDevice device, Action <ImFontAtlasPtr> addFontToAtlas)
        {
            var atlas = new ImFontAtlasPtr(ImGuiNative.ImFontAtlas_ImFontAtlas());

            addFontToAtlas(atlas);
            atlas.Flags = ImFontAtlasFlags.NoMouseCursors;
            if (!atlas.Build())
            {
                throw new InvalidProgramException("Building ImFontAtlas failed");
            }
            if (atlas.Fonts.Size != 1)
            {
                throw new InvalidProgramException("ImFontAtlas does not contain a font after building");
            }
            var font = *(ImFont **)atlas.Fonts.Data.ToPointer();

            if (font->Glyphs.Size <= 0)
            {
                throw new InvalidProgramException("ImFont does not contain at least one glyph after building");
            }

            IntPtr texPixels;
            int    texWidth, texHeight;

            atlas.GetTexDataAsAlpha8(out texPixels, out texWidth, out texHeight);
            Texture = device.ResourceFactory.CreateTexture(new TextureDescription(
                                                               width: (uint)texWidth,
                                                               height: (uint)texHeight,
                                                               format: PixelFormat.R8_UNorm,
                                                               usage: TextureUsage.Sampled,
                                                               type: TextureType.Texture2D,
                                                               depth: 1,
                                                               mipLevels: 1,
                                                               arrayLayers: 1));
            Texture.Name = "IconFontTexture";
            device.UpdateTexture(Texture, texPixels, (uint)(texWidth * texHeight * 1), 0, 0, 0, (uint)texWidth, (uint)texHeight, 1, 0, 0);

            var glyphsPtr = (ImFontGlyph *)font->Glyphs.Data.ToPointer();
            var glyphs    = new Dictionary <string, Rect>();

            for (int i = 0; i < font->Glyphs.Size; i++)
            {
                var glyph     = new ImFontGlyphPtr(glyphsPtr + i);
                var uvMin     = new Vector2(glyph.U0, glyph.V1); // switch v to prevent vflip
                var uvMax     = new Vector2(glyph.U1, glyph.V0);
                var codepoint = (int)(glyph.Codepoint);
                glyphs[char.ConvertFromUtf32(codepoint)] = new Rect((uvMin + uvMax) / 2, uvMax - uvMin);
            }
            Glyphs = glyphs;
            atlas.Destroy();

            Sampler = device.ResourceFactory.CreateSampler(new SamplerDescription(
                                                               addressModeU: SamplerAddressMode.Border,
                                                               addressModeV: SamplerAddressMode.Border,
                                                               addressModeW: SamplerAddressMode.Border,
                                                               filter: SamplerFilter.MinLinear_MagLinear_MipLinear,
                                                               comparisonKind: null,
                                                               maximumAnisotropy: 0,
                                                               minimumLod: 0,
                                                               maximumLod: 1,
                                                               lodBias: 0,
                                                               borderColor: SamplerBorderColor.TransparentBlack
                                                               ));
        }