Skip to content

Commit 286f6c3

Browse files
authored
Merge pull request #2 from AndNowWhat/true_type
true type
2 parents 9a58744 + 711b800 commit 286f6c3

4 files changed

Lines changed: 93 additions & 23 deletions

File tree

cli/src/main/java/com/botwithus/bot/cli/gui/ImGuiApp.java

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import com.botwithus.bot.cli.stream.StreamManager;
1212
import com.botwithus.bot.core.config.ScriptProfileStore;
1313

14+
import imgui.ImFontAtlas;
1415
import imgui.ImFontConfig;
1516
import imgui.ImGui;
16-
import imgui.ImGuiIO;
1717
import imgui.app.Application;
1818
import imgui.app.Configuration;
1919
import imgui.flag.ImGuiConfigFlags;
@@ -68,8 +68,8 @@ public class ImGuiApp extends Application {
6868
private boolean editorMode = false;
6969
private BlueprintEditor blueprintEditor;
7070

71-
// Script config panel (floating window)
72-
private ScriptConfigPanel configPanel;
71+
// Script custom UI window (floating window)
72+
private ScriptUIWindow scriptUIWindow;
7373

7474
// GLFW window handle for title updates
7575
private long glfwWindow;
@@ -94,18 +94,24 @@ protected void initImGui(Configuration config) {
9494
}
9595
float dpiScale = Math.max(xScale[0], 1.0f);
9696

97-
// Rebuild font atlas at scaled pixel size so text is crisp on HiDPI.
98-
ImGuiIO io = ImGui.getIO();
99-
io.getFonts().clear();
100-
ImFontConfig fontConfig = new ImFontConfig();
101-
fontConfig.setSizePixels(14f * dpiScale);
102-
fontConfig.setOversampleH(2);
103-
fontConfig.setOversampleV(2);
104-
io.getFonts().addFontDefault(fontConfig);
105-
io.getFonts().build();
106-
fontConfig.destroy();
97+
float uiSize = (float) Math.round(19f * dpiScale);
98+
ImFontAtlas atlas = ImGui.getIO().getFonts();
99+
atlas.clear();
100+
byte[] ttf = loadSystemFont("segoeui.ttf", "arial.ttf", "verdana.ttf");
101+
ImFontConfig cfg = new ImFontConfig();
102+
cfg.setOversampleH(3);
103+
cfg.setOversampleV(3);
104+
cfg.setPixelSnapH(true);
105+
if (ttf != null) {
106+
atlas.addFontFromMemoryTTF(ttf, uiSize, cfg);
107+
} else {
108+
cfg.setSizePixels(uiSize);
109+
atlas.addFontDefault(cfg);
110+
}
111+
cfg.destroy();
112+
atlas.build();
107113

108-
io.addConfigFlags(ImGuiConfigFlags.ViewportsEnable);
114+
ImGui.getIO().addConfigFlags(ImGuiConfigFlags.ViewportsEnable);
109115

110116
ImGuiTheme.apply(dpiScale);
111117

@@ -193,9 +199,9 @@ public void completeWithError(Object handle, String message) {
193199
// Initialize blueprint editor
194200
blueprintEditor = new BlueprintEditor();
195201

196-
// Initialize config panel and wire opener
197-
configPanel = new ScriptConfigPanel();
198-
ctx.setConfigPanelOpener(runner -> configPanel.open(runner));
202+
// Initialize script UI window and wire opener
203+
scriptUIWindow = new ScriptUIWindow();
204+
ctx.setConfigPanelOpener(runner -> scriptUIWindow.open(runner));
199205

200206
// Initialize panels
201207
panels.add(new ConsolePanel(outputBuffer, registry, executor, this::shutdown));
@@ -208,8 +214,10 @@ public void completeWithError(Object handle, String message) {
208214

209215
statusBar = new StatusBar();
210216

211-
// Grab GLFW window handle for title updates
212217
glfwWindow = GLFW.glfwGetCurrentContext();
218+
219+
var oldSizeCb = GLFW.glfwSetWindowSizeCallback(glfwWindow, null);
220+
if (oldSizeCb != null) oldSizeCb.free();
213221
}
214222

215223
@Override
@@ -273,15 +281,28 @@ public void process() {
273281

274282
ImGui.end();
275283

276-
// Render config panel as floating window (outside the main window)
277-
if (configPanel != null && configPanel.isOpen()) {
278-
configPanel.render();
284+
// Render script custom UI as a floating window (outside the main window)
285+
if (scriptUIWindow != null && scriptUIWindow.isOpen()) {
286+
scriptUIWindow.render();
279287
}
280288

281289
// Update window title based on connection state
282290
updateTitle();
283291
}
284292

293+
private static byte[] loadSystemFont(String... candidates) {
294+
String windir = System.getenv("WINDIR");
295+
if (windir == null) windir = "C:\\Windows";
296+
java.nio.file.Path fontsDir = java.nio.file.Paths.get(windir, "Fonts");
297+
for (String name : candidates) {
298+
java.nio.file.Path p = fontsDir.resolve(name);
299+
if (java.nio.file.Files.exists(p)) {
300+
try { return java.nio.file.Files.readAllBytes(p); } catch (Exception ignored) {}
301+
}
302+
}
303+
return null;
304+
}
305+
285306
private void updateTitle() {
286307
if (glfwWindow == 0) return;
287308
boolean connected = ctx.hasActiveConnection();

cli/src/main/java/com/botwithus/bot/cli/gui/ScriptConfigPanel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public boolean isOpen() {
6767
return open.get();
6868
}
6969

70+
public void close() {
71+
open.set(false);
72+
}
73+
7074
/** Call from the main ImGui render loop. */
7175
public void render() {
7276
if (!open.get() || runner == null || fields == null || fields.isEmpty()) return;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.botwithus.bot.cli.gui;
2+
3+
import com.botwithus.bot.api.ui.ScriptUI;
4+
import com.botwithus.bot.core.runtime.ScriptRunner;
5+
6+
import imgui.ImGui;
7+
import imgui.flag.ImGuiCond;
8+
import imgui.flag.ImGuiWindowFlags;
9+
import imgui.type.ImBoolean;
10+
11+
public class ScriptUIWindow {
12+
13+
private ScriptRunner runner;
14+
private final ImBoolean open = new ImBoolean(false);
15+
16+
public void open(ScriptRunner runner) {
17+
this.runner = runner;
18+
open.set(true);
19+
ImGui.setNextWindowSize(925, 690, ImGuiCond.FirstUseEver);
20+
}
21+
22+
public boolean isOpen() {
23+
return open.get();
24+
}
25+
26+
public void render() {
27+
if (!open.get() || runner == null) return;
28+
29+
ScriptUI ui = runner.getScript().getUI();
30+
if (ui == null) { open.set(false); return; }
31+
32+
ImGui.setNextWindowSize(925, 690, ImGuiCond.FirstUseEver);
33+
if (ImGui.begin(runner.getScriptName() + " Config###scriptUIWindow", open,
34+
ImGuiWindowFlags.NoCollapse)) {
35+
try {
36+
ui.render();
37+
} catch (Exception e) {
38+
ImGui.textColored(ImGuiTheme.RED_R, ImGuiTheme.RED_G, ImGuiTheme.RED_B, 1f,
39+
"UI error: " + e.getMessage());
40+
}
41+
}
42+
ImGui.end();
43+
}
44+
}

cli/src/main/java/com/botwithus/bot/cli/gui/ScriptsPanel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,10 @@ public void render(CliContext ctx) {
162162
}
163163
}
164164

165-
// Config button
165+
// Config button — show for ConfigField-based or custom ImGui UI scripts
166166
var configFields = runner.getConfigFields();
167-
if (configFields != null && !configFields.isEmpty()) {
167+
boolean hasConfig = (configFields != null && !configFields.isEmpty()) || runner.getScript().getUI() != null;
168+
if (hasConfig) {
168169
ImGui.sameLine();
169170
if (ImGui.smallButton("Config")) {
170171
ctx.openConfigPanel(runner);

0 commit comments

Comments
 (0)