Skip to content

Commit 905f3ec

Browse files
committed
GUI scaling
1 parent 0bd5872 commit 905f3ec

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

src/main/kotlin/com/lambda/config/settings/numeric/IntegerSetting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class IntegerSetting(
5151
sameLine()
5252
helpMarker(description)
5353

54-
inputInt("##$name", ::value)
54+
slider("##$name", ::value, range.start, range.endInclusive, "%d$unit")
5555
}
5656

5757
override fun CommandBuilder.buildCommand(registry: CommandRegistryAccess) {

src/main/kotlin/com/lambda/gui/DearImGui.kt

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.lambda.gui
2020
import com.lambda.Lambda.mc
2121
import com.lambda.core.Loadable
2222
import com.lambda.gui.dsl.ImGuiBuilder
23+
import com.lambda.module.modules.client.GuiSettings
2324
import com.lambda.util.path
2425
import com.mojang.blaze3d.opengl.GlStateManager
2526
import com.mojang.blaze3d.systems.RenderSystem
@@ -32,6 +33,7 @@ import net.minecraft.client.gl.GlBackend
3233
import net.minecraft.client.texture.GlTexture
3334
import org.lwjgl.opengl.GL11.glViewport
3435
import org.lwjgl.opengl.GL30.GL_FRAMEBUFFER
36+
import kotlin.math.abs
3537

3638
object DearImGui : Loadable {
3739
val implGlfw = ImGuiImplGlfw()
@@ -42,8 +44,46 @@ object DearImGui : Loadable {
4244
ImGuiConfigFlags.NavEnableSetMousePos or // Move the cursor using the keyboard
4345
ImGuiConfigFlags.DockingEnable
4446

47+
private var lastScale = 0f
48+
private var lastScaleChangeTimestamp = 0L
49+
private var scaleChanged = false
50+
private var targetScale = 0f
51+
52+
private fun updateScale(scale: Float) {
53+
val scaleFactor = if (lastScale > 0) scale / lastScale else scale
54+
ImGui.getStyle().scaleAllSizes(scaleFactor)
55+
56+
io.fonts.clear()
57+
val baseFontSize = 13f
58+
io.fonts.addFontFromFileTTF("fonts/FiraSans-Regular.ttf".path, baseFontSize * scale)
59+
io.fonts.build()
60+
61+
implGl3.createFontsTexture()
62+
}
63+
4564
fun render(block: ImGuiBuilder.() -> Unit) {
46-
// Minecraft will not bind the framebuffer unless it is needed, so do it manually and hope Vulcan never gets real:tm:
65+
val scale = (GuiSettings.scaleSetting / 100.0).toFloat()
66+
67+
if (lastScale == 0f) {
68+
targetScale = scale
69+
updateScale(targetScale)
70+
lastScale = targetScale
71+
}
72+
73+
if (scale > 0 && abs(scale - lastScale) > 0.001f) {
74+
if (abs(scale - targetScale) > 0.001f) {
75+
lastScaleChangeTimestamp = System.currentTimeMillis()
76+
scaleChanged = true
77+
targetScale = scale
78+
}
79+
}
80+
81+
if (scaleChanged && (lastScaleChangeTimestamp + 1000 < System.currentTimeMillis())) {
82+
updateScale(targetScale)
83+
lastScale = targetScale
84+
scaleChanged = false
85+
}
86+
4787
val framebuffer = mc.framebuffer
4888
val prevFramebuffer = (framebuffer.getColorAttachment() as GlTexture).getOrCreateFramebuffer((RenderSystem.getDevice() as GlBackend).framebufferManager, null)
4989

@@ -73,11 +113,6 @@ object DearImGui : Loadable {
73113

74114
io.configFlags = DEFAULT_FLAGS
75115
io.iniFilename = "lambda.ini"
76-
(13..24).forEach { size ->
77-
io.fonts.addFontFromFileTTF("fonts/FiraSans-Bold.ttf".path, size.toFloat())
78-
io.fonts.addFontFromFileTTF("fonts/FiraSans-Regular.ttf".path, size.toFloat())
79-
}
80-
io.fonts.build()
81116

82117
implGlfw.init(mc.window.handle, true)
83118
implGl3.init()

src/main/kotlin/com/lambda/module/modules/client/GuiSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object GuiSettings : Module(
2828
tag = ModuleTag.CLIENT,
2929
) {
3030
// General
31-
private val scaleSetting by setting("Scale", 100, 50..300, 1, unit = "%").group(Group.General)
31+
internal val scaleSetting by setting("Scale", 100, 50..300, 1, unit = "%").group(Group.General)
3232

3333
// Colors
3434
val primaryColor by setting("Primary Color", Color(130, 200, 255)).group(Group.Colors)

0 commit comments

Comments
 (0)