shoko/MainUI.cs

113 lines
3.0 KiB
C#
Raw Permalink Normal View History

2023-10-02 18:49:24 +00:00
using System.Reflection;
using ImGuiNET;
2023-11-10 21:45:01 +00:00
using Raylib_cs;
2023-10-02 18:49:24 +00:00
namespace Shoko;
static class MainUI
{
static string txtURL = "";
public static List<Tab> tabs = new List<Tab>();
2023-10-03 19:52:26 +00:00
public static ImFontPtr Font;
public static List<ImFontPtr> HeadingFonts = new List<ImFontPtr>();
public static ImFontPtr MonospaceFont;
2023-10-02 18:49:24 +00:00
public static void NewTab(string url)
{
tabs.Add(new Tab(url));
}
2023-10-03 19:52:26 +00:00
public static unsafe void AddFontFromResource(string resource, Action<IntPtr, int> f)
{
using(var m = new MemoryStream())
{
Assembly.GetExecutingAssembly().GetManifestResourceStream(resource).CopyTo(m);
var fontBuffer = m.ToArray();
fixed(byte* buffer = fontBuffer)
{
f(new IntPtr(buffer), fontBuffer.Length);
}
}
}
public static bool Load(string[] args)
2023-10-03 19:52:26 +00:00
{
var io = ImGui.GetIO();
ImGui.StyleColorsDark();
io.ConfigFlags |= ImGuiConfigFlags.DockingEnable;
2023-11-10 21:45:01 +00:00
nint fontrange;
ImFontGlyphRangesBuilderPtr builder;
unsafe {
fixed(ushort* chars = new ushort[]{1, 0xffff, 0})
fontrange = new nint(chars);
builder = new ImFontGlyphRangesBuilderPtr(ImGuiNative.ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder());
}
builder.AddRanges(fontrange);
builder.BuildRanges(out ImVector ranges);
2023-10-03 19:52:26 +00:00
AddFontFromResource("RobotoFlex.ttf", (buf, len)=>{
2023-11-10 21:45:01 +00:00
Font = io.Fonts.AddFontFromMemoryTTF(buf, len, 16f, null, ranges.Data);
HeadingFonts.Add(io.Fonts.AddFontFromMemoryTTF(buf, len, 32f, null, ranges.Data));
HeadingFonts.Add(io.Fonts.AddFontFromMemoryTTF(buf, len, 24f, null, ranges.Data));
HeadingFonts.Add(io.Fonts.AddFontFromMemoryTTF(buf, len, 20f, null, ranges.Data));
2023-10-03 19:52:26 +00:00
});
AddFontFromResource("RobotoMono.ttf", (buf, len)=>{
2023-11-10 21:45:01 +00:00
MonospaceFont = io.Fonts.AddFontFromMemoryTTF(buf, len, 16f, null, ranges.Data);
2023-10-03 19:52:26 +00:00
});
2023-11-10 21:45:01 +00:00
io.Fonts.Build();
if(args.Length > 0)
foreach (var arg in args)
NewTab(arg);
2023-10-03 19:52:26 +00:00
return true;
}
2023-11-10 21:45:01 +00:00
public static void PreRender()
{
Raylib.ClearBackground(Color.WHITE);
}
2023-10-02 18:49:24 +00:00
public static bool Render()
{
bool quit = true;
2023-10-03 19:52:26 +00:00
ImGui.PushFont(Font);
2023-10-02 18:49:24 +00:00
ImGui.DockSpaceOverViewport();
Gui.MainMenuBar(()=>
{
Gui.Menu("File", ()=>
{
Gui.MenuItem("Quit", null, ()=> quit = false);
});
Gui.Menu("Help", ()=>
{
Gui.MenuItem("About", null, ()=> NewTab("about:"));
});
ImGui.InputText("##url", ref txtURL, 1024);
Gui.Button("Go", ()=>{
NewTab(txtURL);
txtURL = "";
});
});
tabs = tabs.Where(x=>x.IsOpen).ToList();
foreach (var tab in tabs)
{
tab.Render();
}
2023-10-03 19:52:26 +00:00
ImGui.PopFont();
2023-10-02 18:49:24 +00:00
return quit;
}
}