Skip to content

Commit

Permalink
⚗️ finished dearimgui lib swap
Browse files Browse the repository at this point in the history
thanks @Deftu for helping me a lot aaaaaaaaaaaaaaaaaaaaaaaaa
  • Loading branch information
asoji committed Jul 2, 2024
1 parent ac8000e commit afc5d55
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

@FunctionalInterface
public interface KeyInputEvent {
Event<KeyInputEvent> EVENT = EventFactory.createArrayBacked(KeyInputEvent.class, (listeners) -> (key, scancode, action, mods) -> {
Event<KeyInputEvent> EVENT = EventFactory.createArrayBacked(KeyInputEvent.class, (listeners) -> (key, action, mods) -> {
for (KeyInputEvent listener : listeners) {
listener.onKeyInput(key, scancode, action, mods);
listener.onKeyInput(key, action, mods);
}
});

void onKeyInput(int key, int scancode, InputAction action, int mods);
void onKeyInput(int key, InputAction action, int mods);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private void onKeyPress(long handle, int key, int scancode, int action, int mods
return;

InputAction inputAction = InputAction.from(action);
KeyInputEvent.EVENT.invoker().onKeyInput(key, scancode, inputAction, mods);
KeyInputEvent.EVENT.invoker().onKeyInput(key, inputAction, mods);
}


Expand Down

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/java/gay/asoji/innerpastels/mixins/MinecraftMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gay.asoji.innerpastels.mixins;

import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl;
import net.minecraft.client.Minecraft;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Minecraft.class)
public class MinecraftMixin {
@Inject(method = "close", at = @At(value = "INVOKE", target = "Lnet/minecraft/Util;shutdownExecutors()V", shift = At.Shift.BEFORE))
public void close(CallbackInfo ci) {
InnerPastelsImGuiImpl.INSTANCE.destroy(); // destroying your fathers bed since 2024
}
}
21 changes: 21 additions & 0 deletions src/main/java/gay/asoji/innerpastels/mixins/MouseHandlerMixin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gay.asoji.innerpastels.mixins;

import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl;
import gay.asoji.innerpastels.events.InputAction;
import gay.asoji.innerpastels.events.MouseInputEvent;
import gay.asoji.innerpastels.events.MouseScrollInputEvent;
Expand All @@ -11,6 +12,7 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(MouseHandler.class)
public class MouseHandlerMixin {
Expand All @@ -33,4 +35,23 @@ private void onScroll(long windowPointer, double xOffset, double yOffset, Callba

MouseScrollInputEvent.EVENT.invoker().onScroll(xOffset, yOffset);
}

@Inject(method = "grabMouse", at = @At("HEAD"))
public void grabMouse(CallbackInfo ci) {
InnerPastelsImGuiImpl.INSTANCE.mouseFocus();
}

@Inject(method = "xpos", at = @At("HEAD"), cancellable = true)
public void cancelMouseX(CallbackInfoReturnable<Double> cir) {
if (InnerPastelsImGuiImpl.INSTANCE.isMouseHidden()) {
cir.setReturnValue(Double.MIN_VALUE);
}
}

@Inject(method = "ypos", at = @At("HEAD"), cancellable = true)
public void cancelMouseY(CallbackInfoReturnable<Double> cir) {
if (InnerPastelsImGuiImpl.INSTANCE.isMouseHidden()) {
cir.setReturnValue(Double.MIN_VALUE);
}
}
}
56 changes: 21 additions & 35 deletions src/main/kotlin/gay/asoji/innerpastels/client/InnerPastelsClient.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package gay.asoji.innerpastels.client

import com.mojang.blaze3d.platform.InputConstants
import gay.asoji.innerpastels.InnerPastels
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.implGl3
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.implGlfw
import gay.asoji.innerpastels.client.imgui.ImGuiPanel
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.endFrame
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.imGui
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.windowHandle
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.initialize
import gay.asoji.innerpastels.client.imgui.InnerPastelsImGuiImpl.startFrame
import gay.asoji.innerpastels.events.InputAction
import gay.asoji.innerpastels.events.KeyInputEvent
import gay.asoji.innerpastels.events.MouseInputEvent
import gay.asoji.innerpastels.events.MouseScrollInputEvent
import imgui.ImGui
import imgui.type.ImBoolean
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
Expand All @@ -23,7 +19,6 @@ import net.fabricmc.loader.api.FabricLoader
import net.minecraft.client.KeyMapping
import net.minecraft.client.Minecraft
import org.lwjgl.glfw.GLFW
import java.util.*

class InnerPastelsClient : ClientModInitializer {
private var isImGuiRenderEnabled: Boolean = false
Expand All @@ -32,7 +27,8 @@ class InnerPastelsClient : ClientModInitializer {
"key.innerpastels.developerui",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_M,
"category.innerpastels.developer")
"category.innerpastels.developer"
)

fun initializeDevKeybinds() {
val toggleUIKeybind = KeyBindingHelper.registerKeyBinding(DEVELOPER_UI_BINDING)
Expand All @@ -44,56 +40,46 @@ class InnerPastelsClient : ClientModInitializer {
while (DEVELOPER_UI_BINDING.consumeClick()) {
if (client.player != null && client.screen == null) {
isImGuiRenderEnabled = !isImGuiRenderEnabled

implGl3.dispose()
implGlfw.dispose()
}
}
}

override fun onInitializeClient() {
if (FabricLoader.getInstance().isDevelopmentEnvironment) {
// THIS IS PROBABLY NOT CORRECT, IF IT ISNT, FUCKING REMOVE IT LATER, ALL CONTEXT CREATION SHOULD PROBABLY BE FUCKING LEFT TO THE IMPL [OR THE PANEL ITSELF]?
ImGui.createContext()
initializeDevKeybinds()
}

HudRenderCallback.EVENT.register { gui, tickDelta ->
if (!isImGuiRenderEnabled) {
return@register
}
implGlfw.newFrame()
ImGui.newFrame()
initialize(Minecraft.getInstance().window.window)
startFrame()

panels.forEach {
it.render(ImBoolean())
if (isImGuiRenderEnabled) {
panels.forEach {
it.render(ImBoolean())
}
}

implGl3.renderDrawData(ImGui.getDrawData())
ImGui.render()
endFrame(windowHandle)
endFrame()
}

KeyInputEvent.EVENT.register { key, scancode, action, mods ->
when (action) {
InputAction.PRESS -> InnerPastelsImGuiImpl.keyPressed(key, scancode, mods)
InputAction.RELEASE -> InnerPastelsImGuiImpl.keyReleased(key, scancode, mods)
KeyInputEvent.EVENT.register { key, action, mods ->
when (action ?: return@register) {
InputAction.PRESS -> InnerPastelsImGuiImpl.keyPress(key, mods)
InputAction.RELEASE -> InnerPastelsImGuiImpl.keyRelease(key, mods)
}
}

MouseInputEvent.EVENT.register { button, action, mods ->
val mouseX = Minecraft.getInstance().mouseHandler.xpos()
val mouseY = Minecraft.getInstance().mouseHandler.ypos()
when (action) {
InputAction.PRESS -> InnerPastelsImGuiImpl.mouseClicked(mouseX, mouseY, button)
InputAction.RELEASE -> InnerPastelsImGuiImpl.mouseReleased(mouseX, mouseY, button)
when (action ?: return@register) {
InputAction.PRESS -> InnerPastelsImGuiImpl.mouseClick(mouseX, mouseY, button)
InputAction.RELEASE -> InnerPastelsImGuiImpl.mouseRelease(mouseX, mouseY, button)
}
}

MouseScrollInputEvent.EVENT.register{ xOffset, yOffset ->
val mouseX = Minecraft.getInstance().mouseHandler.xpos()
val mouseY = Minecraft.getInstance().mouseHandler.ypos()
InnerPastelsImGuiImpl.mouseScrolled(mouseX, mouseY, xOffset, yOffset)
MouseScrollInputEvent.EVENT.register { xOffset, yOffset ->
InnerPastelsImGuiImpl.mouseScroll(xOffset, yOffset)
}
}

Expand Down
Loading

0 comments on commit afc5d55

Please sign in to comment.