From e6ae1efcbc521c382022d3b218b10f55939f02a5 Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Fri, 4 Feb 2022 19:08:48 +0000 Subject: [PATCH] New overlay system --- .../som/hibiscus/features/FeatureList.kt | 14 ---- .../som/hibiscus/features/FeaturesRegistry.kt | 22 ++++++ .../features/overlay/FeatureListOverlay.kt | 48 ------------- .../som/hibiscus/features/overlay/Overlay.kt | 69 +++++++++++++++++++ .../som/hibiscus/gui/ModuleControlsUI.kt | 54 ++++++++++----- 5 files changed, 128 insertions(+), 79 deletions(-) delete mode 100644 src/main/kotlin/codes/som/hibiscus/features/overlay/FeatureListOverlay.kt create mode 100644 src/main/kotlin/codes/som/hibiscus/features/overlay/Overlay.kt diff --git a/src/main/kotlin/codes/som/hibiscus/features/FeatureList.kt b/src/main/kotlin/codes/som/hibiscus/features/FeatureList.kt index 95e905e..0049937 100644 --- a/src/main/kotlin/codes/som/hibiscus/features/FeatureList.kt +++ b/src/main/kotlin/codes/som/hibiscus/features/FeatureList.kt @@ -1,16 +1,2 @@ package codes.som.hibiscus.features -import codes.som.hibiscus.api.feature.Feature -import codes.som.hibiscus.features.combat.Criticals -import codes.som.hibiscus.features.movement.Flight -import codes.som.hibiscus.features.movement.Speed -import codes.som.hibiscus.features.overlay.FeatureListOverlay -import codes.som.hibiscus.features.player.NoFallDamage - -val ALL_FEATURES: Array<() -> Feature> = arrayOf( - ::NoFallDamage, - ::Flight, - ::FeatureListOverlay, - ::Speed, - ::Criticals, -) diff --git a/src/main/kotlin/codes/som/hibiscus/features/FeaturesRegistry.kt b/src/main/kotlin/codes/som/hibiscus/features/FeaturesRegistry.kt index 2aac70c..d1277fc 100644 --- a/src/main/kotlin/codes/som/hibiscus/features/FeaturesRegistry.kt +++ b/src/main/kotlin/codes/som/hibiscus/features/FeaturesRegistry.kt @@ -1,6 +1,28 @@ package codes.som.hibiscus.features +import codes.som.hibiscus.api.feature.Feature +import codes.som.hibiscus.features.combat.Criticals +import codes.som.hibiscus.features.movement.Flight +import codes.som.hibiscus.features.movement.Speed +import codes.som.hibiscus.features.overlay.Overlay +import codes.som.hibiscus.features.player.NoFallDamage + +val ALL_FEATURES: Array<() -> Feature> = arrayOf( + ::NoFallDamage, + ::Flight, + ::Overlay, + ::Speed, + ::Criticals, +) + class FeaturesRegistry { private val features = ALL_FEATURES.map { it() }.sortedBy { it.name } fun getAllFeatures() = features.asSequence() + + inline fun getFeature() = + getFeature(T::class.java) + + @Suppress("UNCHECKED_CAST") + fun getFeature(type: Class) = + features.first { it.javaClass == type } as T } diff --git a/src/main/kotlin/codes/som/hibiscus/features/overlay/FeatureListOverlay.kt b/src/main/kotlin/codes/som/hibiscus/features/overlay/FeatureListOverlay.kt deleted file mode 100644 index 5e40adf..0000000 --- a/src/main/kotlin/codes/som/hibiscus/features/overlay/FeatureListOverlay.kt +++ /dev/null @@ -1,48 +0,0 @@ -package codes.som.hibiscus.features.overlay - -import codes.som.hibiscus.HibiscusMod -import codes.som.hibiscus.api.feature.Feature -import codes.som.hibiscus.api.feature.FeatureCategory -import codes.som.hibiscus.events.PostRenderAllEvent -import imgui.ImGui -import imgui.flag.ImGuiCol -import imgui.flag.ImGuiStyleVar -import imgui.flag.ImGuiWindowFlags - -class FeatureListOverlay : Feature("Feature List", FeatureCategory.OVERLAY) { - init { - on { _: PostRenderAllEvent -> - val viewport = ImGui.getMainViewport() - ImGui.setNextWindowPos(viewport.posX, viewport.posY) - - val enabledFeatures = - HibiscusMod.features.getAllFeatures() - .filter { it.category != FeatureCategory.OVERLAY } - .filter { it.enabled }.toList() - if (enabledFeatures.isEmpty()) - return@on - - ImGui.pushStyleColor(ImGuiCol.WindowBg, 10, 10, 10, 170) - ImGui.pushStyleVar(ImGuiStyleVar.WindowPadding, 8f, 8f) - ImGui.pushStyleVar(ImGuiStyleVar.WindowMinSize, 0f, 0f) - - if (ImGui.begin( - "Feature List", - ImGuiWindowFlags.NoDecoration + ImGuiWindowFlags.NoInputs + - ImGuiWindowFlags.NoMove + ImGuiWindowFlags.AlwaysAutoResize - ) - ) { - ImGui.pushStyleColor(ImGuiCol.Text, 244, 161, 255, 255) - for (feature in enabledFeatures) - ImGui.text(feature.name) - ImGui.popStyleColor() - } - ImGui.end() - - ImGui.popStyleVar(2) - ImGui.popStyleColor() - } - - enabled = true - } -} diff --git a/src/main/kotlin/codes/som/hibiscus/features/overlay/Overlay.kt b/src/main/kotlin/codes/som/hibiscus/features/overlay/Overlay.kt new file mode 100644 index 0000000..2ec4ccc --- /dev/null +++ b/src/main/kotlin/codes/som/hibiscus/features/overlay/Overlay.kt @@ -0,0 +1,69 @@ +package codes.som.hibiscus.features.overlay + +import codes.som.hibiscus.HibiscusMod +import codes.som.hibiscus.api.feature.Feature +import codes.som.hibiscus.api.feature.FeatureCategory +import codes.som.hibiscus.events.PostRenderAllEvent +import imgui.ImGui +import imgui.flag.ImGuiCol +import imgui.flag.ImGuiStyleVar +import imgui.flag.ImGuiWindowFlags + +class Overlay : Feature("Overlay", FeatureCategory.OVERLAY) { + init { + val enabledFeatures by values.bool("Enabled Features", true) + + on { _: PostRenderAllEvent -> + val viewport = ImGui.getMainViewport() + ImGui.setNextWindowPos(viewport.posX, viewport.posY) + ImGui.setNextWindowSize(viewport.sizeX, viewport.sizeY) + + ImGui.pushStyleColor(ImGuiCol.WindowBg, 0, 0, 0, 0) + ImGui.begin( + "Overlay##Overlay", + ImGuiWindowFlags.NoMove + ImGuiWindowFlags.NoInputs + ImGuiWindowFlags.NoDecoration + ) + + if (enabledFeatures) + drawEnabledFeatures() + + ImGui.end() + ImGui.popStyleColor() + } + + enabled = true + } + + private fun drawEnabledFeatures() { + val enabledFeatures = + HibiscusMod.features.getAllFeatures() + .filter { it.category != FeatureCategory.OVERLAY } + .filter { it.enabled }.toList() + + if (enabledFeatures.isEmpty()) + return + + val padX = 8f + val padY = 8f + + val pos = ImGui.getCursorScreenPos() + ImGui.setCursorScreenPos(pos.x + padX, pos.y + padY) + + ImGui.beginGroup() + + ImGui.pushStyleColor(ImGuiCol.Text, 244, 161, 255, 255) + for (feature in enabledFeatures) + ImGui.text(feature.name) + ImGui.popStyleColor() + + ImGui.endGroup() + val min = ImGui.getItemRectMin() + val max = ImGui.getItemRectMax() + ImGui.getBackgroundDrawList().addRectFilled( + min.x - padX, min.y - padY, max.x + padX, + max.y + padY, + 0xAA0A0A0AL.toInt(), + 0F + ) + } +} diff --git a/src/main/kotlin/codes/som/hibiscus/gui/ModuleControlsUI.kt b/src/main/kotlin/codes/som/hibiscus/gui/ModuleControlsUI.kt index 3d0c14b..9d28fcf 100644 --- a/src/main/kotlin/codes/som/hibiscus/gui/ModuleControlsUI.kt +++ b/src/main/kotlin/codes/som/hibiscus/gui/ModuleControlsUI.kt @@ -3,6 +3,7 @@ package codes.som.hibiscus.gui import codes.som.hibiscus.HibiscusMod import codes.som.hibiscus.api.feature.Feature import codes.som.hibiscus.api.feature.FeatureCategory +import codes.som.hibiscus.features.overlay.Overlay import imgui.ImGui import imgui.flag.ImGuiCol import imgui.flag.ImGuiCond.Once @@ -12,25 +13,21 @@ import imgui.type.ImBoolean object ModuleControlsUI { private val moduleValueWindows = mutableMapOf() - - private fun categoryPanel(category: FeatureCategory, initialX: Float, initialY: Float) { - ImGui.setNextWindowPos(initialX, initialY, Once) - ImGui.setNextWindowCollapsed(true, Once) - ImGui.setNextWindowSize(300f, 0f, Once) - + + private fun drawCategoryPanel(category: FeatureCategory, ) { ImGui.begin(category.humanName, AlwaysVerticalScrollbar) for (feature in HibiscusMod.features.getAllFeatures().filter { it.category == category }) { - featureControls(feature) + drawFeatureControls(feature) } ImGui.end() } - private fun featureControls(feature: Feature) { + private fun drawFeatureControls(feature: Feature) { if (feature.values.exist()) { ImGui.columns(2, "Features Columns", false) - ImGui.setColumnWidth(0, ImGui.getWindowContentRegionMaxX() - ImGui.getWindowContentRegionMinX() - 24) + ImGui.setColumnWidth(0, ImGui.getWindowContentRegionMaxX() - ImGui.getWindowContentRegionMinX() - 30) } if (feature.enabled) { @@ -66,15 +63,38 @@ object ModuleControlsUI { } } } - - + + private fun drawOverlaySettings() { + val overlayFeature = HibiscusMod.features.getFeature() + if (ImGui.begin("${overlayFeature.name}##Feature Controls")) + for (value in overlayFeature.values) + value.drawUIControl() + ImGui.end() + } + fun render(delta: Float) { - for ((index, category) in FeatureCategory.values().withIndex()) { - categoryPanel( - category, - ImGui.getMainViewport().posX + 10f, - ImGui.getMainViewport().posY + 10f + index * 30f - ) + var yOffset = 0f + + fun pushInitialPanelConfig() { + val initialX = ImGui.getMainViewport().posX + 10f + val initialY = ImGui.getMainViewport().posY + 10f + yOffset + + ImGui.setNextWindowPos(initialX, initialY, Once) + ImGui.setNextWindowCollapsed(true, Once) + ImGui.setNextWindowSize(300f, 0f, Once) } + + for (category in FeatureCategory.values()) { + if (category == FeatureCategory.OVERLAY) + continue + + pushInitialPanelConfig() + drawCategoryPanel(category) + + yOffset += 30f + } + + pushInitialPanelConfig() + drawOverlaySettings() } }