Skip to content

Commit 98e8bcb

Browse files
committed
added support for icons in imgui text
1 parent b222bca commit 98e8bcb

6 files changed

Lines changed: 43 additions & 17 deletions

File tree

src/client/kotlin/com/github/fdh911/MySecondModClient.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.github.fdh911.modules.ModuleGardenMacro
77
import com.github.fdh911.modules.macro.KeySimulator
88
import com.github.fdh911.modules.macro.MouseLock
99
import com.github.fdh911.render.opengl.GLState2
10+
import imgui.ImGui
1011
import net.fabricmc.api.ClientModInitializer
1112
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
1213
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext
@@ -53,7 +54,9 @@ object MySecondModClient : ClientModInitializer {
5354

5455
UserInterface.MCScreen.onRender {
5556
val state = GLState2().apply { saveAll() }
56-
UserInterface.render(ModuleList::renderUI)
57+
UserInterface.render {
58+
ModuleList.renderUI()
59+
}
5760
state.restoreAll()
5861
}
5962
}

src/client/kotlin/com/github/fdh911/modules/ModuleList.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.fdh911.modules
22

3+
import com.github.fdh911.render.Unicodes
34
import com.github.fdh911.render.UserInterface
45
import com.github.fdh911.state.SkyblockState
56
import imgui.ImGui
@@ -27,9 +28,9 @@ object ModuleList {
2728
module.renderUpdate(ctx)
2829
}
2930

30-
fun renderUI() = UserInterface.newWindow("Modules") {
31+
fun renderUI() = UserInterface.newWindow("${Unicodes.DUPLICATE} Modules") {
3132
for(module in modules)
32-
if(ImGui.selectable(module.name)) {
33+
if(ImGui.selectable("${Unicodes.REMOVE} ${module.name}")) {
3334
if(toDisplay.contains(module))
3435
toDisplay.remove(module)
3536
else
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.fdh911.render
2+
3+
enum class Unicodes(val s: String) {
4+
REMOVE("\uf00d"),
5+
DUPLICATE("\uf24d");
6+
7+
companion object {
8+
override fun toString() = entries.joinToString { it.s }
9+
}
10+
11+
override fun toString() = s
12+
}

src/client/kotlin/com/github/fdh911/render/UserInterface.kt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.github.fdh911.render
22

33
import com.github.fdh911.render.opengl.GLState2
44
import imgui.ImFont
5+
import imgui.ImFontConfig
6+
import imgui.ImFontGlyphRangesBuilder
57
import imgui.ImGui
68
import imgui.ImVec4
79
import imgui.flag.ImGuiCond
@@ -12,7 +14,6 @@ import net.minecraft.client.MinecraftClient
1214
import net.minecraft.client.gui.DrawContext
1315
import net.minecraft.client.gui.screen.Screen
1416
import net.minecraft.text.Text
15-
import java.io.File
1617

1718
object UserInterface {
1819
init {
@@ -34,19 +35,28 @@ object UserInterface {
3435
private val largeFont: ImFont
3536

3637
init {
37-
val classpathSmall = UserInterface::class.java.getResourceAsStream("/fonts/0xProtoNerdFont-Regular.ttf")
38-
?: throw RuntimeException("Could not open small font file")
39-
val tempfileSmall = File.createTempFile("imgui", "smallfont")
40-
tempfileSmall.writeBytes(classpathSmall.readAllBytes())
41-
smallFont = io.fonts.addFontFromFileTTF(tempfileSmall.absolutePath, 20.0f)
42-
tempfileSmall.delete()
43-
44-
val classpathLarge = UserInterface::class.java.getResourceAsStream("/fonts/0xProtoNerdFont-Bold.ttf")
45-
?: throw RuntimeException("Could not open large font file")
46-
val tempfileLarge = File.createTempFile("imgui", "largefont")
47-
tempfileLarge.writeBytes(classpathLarge.readAllBytes())
48-
largeFont = io.fonts.addFontFromFileTTF(tempfileLarge.absolutePath, 28.0f)
49-
tempfileLarge.delete()
38+
fun loadMergedFont(baseFont: String, iconsFont: String, size: Float, range: ShortArray): ImFont {
39+
val cfg = ImFontConfig()
40+
val baseIS = object{}::class.java.getResourceAsStream("/fonts/$baseFont.ttf")
41+
?: throw RuntimeException("Could not open base font file $baseFont")
42+
val iconsIS = object{}::class.java.getResourceAsStream("/fonts/$iconsFont.ttf")
43+
?: throw RuntimeException("Could not open icons font file $iconsFont")
44+
val font = io.fonts.addFontFromMemoryTTF(baseIS.readAllBytes(), size, cfg, range)
45+
cfg.mergeMode = true
46+
io.fonts.addFontFromMemoryTTF(iconsIS.readAllBytes(), size, cfg, range)
47+
baseIS.close()
48+
iconsIS.close()
49+
return font
50+
}
51+
52+
val customRange = with(ImFontGlyphRangesBuilder()) {
53+
addRanges(io.fonts.glyphRangesDefault)
54+
addText(Unicodes.toString())
55+
buildRanges()
56+
}
57+
58+
smallFont = loadMergedFont("0xProtoNerdFont-Regular", "fa-regular-400", 20.0f, customRange)
59+
largeFont = loadMergedFont("0xProtoNerdFont-Bold", "fa-solid-900", 28.0f, customRange)
5060

5161
io.fonts.build()
5262

66.5 KB
Binary file not shown.
416 KB
Binary file not shown.

0 commit comments

Comments
 (0)