Skip to content

Commit

Permalink
added key event + input field component
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrycat committed Oct 14, 2022
1 parent 993adb3 commit 4ba5737
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 35 deletions.
39 changes: 30 additions & 9 deletions src/main/java/io/github/kurrycat/mpkmod/compatability/API.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.kurrycat.mpkmod.compatability;

import io.github.kurrycat.mpkmod.compatability.MCClasses.FunctionHolder;
import io.github.kurrycat.mpkmod.compatability.MCClasses.Keyboard;
import io.github.kurrycat.mpkmod.compatability.MCClasses.Minecraft;
import io.github.kurrycat.mpkmod.compatability.MCClasses.Renderer3D;
import io.github.kurrycat.mpkmod.discord.DiscordRPC;
Expand All @@ -10,7 +11,6 @@
import io.github.kurrycat.mpkmod.gui.components.Component;
import io.github.kurrycat.mpkmod.gui.screens.LandingBlockGuiScreen;
import io.github.kurrycat.mpkmod.gui.screens.main_gui.MainGuiScreen;
import io.github.kurrycat.mpkmod.gui.screens.main_gui.MapOverviewPane;
import io.github.kurrycat.mpkmod.landingblock.LandingBlock;
import io.github.kurrycat.mpkmod.save.Serializer;
import io.github.kurrycat.mpkmod.util.JSONConfig;
Expand Down Expand Up @@ -87,14 +87,14 @@ public static void init(String mcVersion) {
new EventAPI.EventListener<OnRenderWorldOverlayEvent>(
e -> {
LandingBlockGuiScreen.lbs.forEach(lb -> {
if(lb.shouldRender && lb.boundingBox != null)
Renderer3D.drawBox(
lb.boundingBox.expand(0.005D),
lb.highlight ?
new Color(98, 255, 74, 157) :
new Color(255, 68, 68, 157),
e.partialTicks
);
if (lb.shouldRender && lb.boundingBox != null)
Renderer3D.drawBox(
lb.boundingBox.expand(0.005D),
lb.highlight ?
new Color(98, 255, 74, 157) :
new Color(255, 68, 68, 157),
e.partialTicks
);
}
);
},
Expand All @@ -119,6 +119,23 @@ public static void init(String mcVersion) {
})
)
);

/*EventAPI.addListener(
new EventAPI.EventListener<OnKeyInputEvent>(
e -> {
System.out.println(
"KeyCode: " + e.keyCode +
" Key: " + e.key +
" Pressed: " + e.pressed
);
System.out.println(
Keyboard.getPressedButtons()
);
},
Event.EventType.KEY_INPUT
)
);*/
}

/**
Expand Down Expand Up @@ -177,5 +194,9 @@ public static void onServerDisconnect() {
Minecraft.updateWorldState(Event.EventType.SERVER_DISCONNECT, false);
DiscordRPC.updateWorldAndPlayState();
}

public static void onKeyInput(int keyCode, String key, boolean pressed) {
EventAPI.postEvent(new OnKeyInputEvent(keyCode, key, pressed));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,33 @@
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Keyboard;

public class EventListener {
@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
public void onEvent(InputEvent.KeyInputEvent event) {
int keyCode = Keyboard.getEventKey();
String key = Keyboard.getKeyName(keyCode);
boolean pressed = Keyboard.getEventKeyState();

API.Events.onKeyInput(keyCode, key, pressed);

MPKMod_1_8.keyBindingMap.forEach((id, keyBinding) -> {
if (keyBinding.isPressed() && API.guiScreenMap.containsKey(id)) {
Minecraft.getMinecraft().displayGuiScreen(
new MPKGuiScreen_1_8(API.guiScreenMap.get(id))
);
}
});
}


@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.util.BlockPos;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

import java.awt.*;
Expand All @@ -32,7 +33,8 @@ public class FunctionCompatibility implements FunctionHolder,
Renderer3D.Interface,
Renderer2D.Interface,
FontRenderer.Interface,
io.github.kurrycat.mpkmod.compatability.MCClasses.Minecraft.Interface {
io.github.kurrycat.mpkmod.compatability.MCClasses.Minecraft.Interface,
io.github.kurrycat.mpkmod.compatability.MCClasses.Keyboard.Interface {
/**
* Is called in {@link SoundManager.Interface}
*/
Expand Down Expand Up @@ -164,14 +166,31 @@ public Vector2D getStringSize(String text) {
);
}

/**
* Is called in {@link io.github.kurrycat.mpkmod.compatability.MCClasses.Minecraft.Interface Minecraft.Interface}
*/
public String getIP() {
ServerData d = Minecraft.getMinecraft().getCurrentServerData();
if (d == null) return "Multiplayer";
else return d.serverIP;
}

@Override
/**
* Is called in {@link io.github.kurrycat.mpkmod.compatability.MCClasses.Minecraft.Interface Minecraft.Interface}
*/
public String getFPS() {
return String.valueOf(Minecraft.getDebugFPS());
}


/**
* Is called in {@link io.github.kurrycat.mpkmod.compatability.MCClasses.Keyboard.Interface Keyboard.Interface}
*/
public List<String> getPressedButtons() {
List<String> keysDown = new ArrayList<>();
for (int i = 0; i < Keyboard.getKeyCount(); i++)
if (Keyboard.isKeyDown(i))
keysDown.add(Keyboard.getKeyName(i));
return keysDown;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.github.kurrycat.mpkmod.gui.MPKGuiScreen;
import io.github.kurrycat.mpkmod.util.Vector2D;
import net.minecraft.client.gui.GuiListExtended;
import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
Expand All @@ -26,7 +25,8 @@ public void initGui() {
repeatEventsEnabled = Keyboard.areRepeatEventsEnabled();
Keyboard.enableRepeatEvents(true);
super.initGui();
eventReceiver.onGuiInit();
if (!eventReceiver.isInitialized() || eventReceiver.resetOnOpen())
eventReceiver.onGuiInit();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class MPKMod_1_8 {
//public static final String GUI_FACTORY = "io.github.kurrycat.mpkmod.config.GuiFactory";

public Map<String, KeyBinding> keyBindingMap = new HashMap<>();
public static Map<String, KeyBinding> keyBindingMap = new HashMap<>();

@EventHandler
public void init(FMLInitializationEvent event) {
Expand Down Expand Up @@ -73,16 +73,4 @@ public void init(FMLInitializationEvent event) {
public void loadComplete(FMLLoadCompleteEvent e) {
API.Events.onLoadComplete();
}

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
public void onEvent(InputEvent.KeyInputEvent event) {
keyBindingMap.forEach((id, keyBinding) -> {
if (keyBinding.isPressed() && API.guiScreenMap.containsKey(id)) {
Minecraft.getMinecraft().displayGuiScreen(
new MPKGuiScreen_1_8(API.guiScreenMap.get(id))
);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.kurrycat.mpkmod.compatability.MCClasses;

import io.github.kurrycat.mpkmod.compatability.API;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class Keyboard {
public static List<String> getPressedButtons() {
return Interface.get().map(Interface::getPressedButtons).orElseGet(ArrayList::new);
}

public enum Modifier {
CTRL,
SHIFT,
ALT,
RCTRL,
RSHIFT;
}

public interface Interface extends FunctionHolder {
static Optional<Interface> get() {
return API.getFunctionHolder(Interface.class);
}

List<String> getPressedButtons();
}
}
3 changes: 2 additions & 1 deletion src/main/java/io/github/kurrycat/mpkmod/events/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public enum EventType {
RENDER_OVERLAY(OnRenderOverlayEvent.class),
RENDER_WORLD_OVERLAY(OnRenderWorldOverlayEvent.class),
SERVER_CONNECT(OnServerConnect.class),
SERVER_DISCONNECT(OnServerDisconnect.class);
SERVER_DISCONNECT(OnServerDisconnect.class),
KEY_INPUT(OnKeyInputEvent.class);

public final Class<? extends Event> eventClass;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.kurrycat.mpkmod.events;

public class OnKeyInputEvent extends Event {
public int keyCode;
public String key;
public boolean pressed;

public OnKeyInputEvent(int keyCode, String key, boolean pressed) {
super(EventType.KEY_INPUT);
this.keyCode = keyCode;
this.key = key;
this.pressed = pressed;
}
}
11 changes: 10 additions & 1 deletion src/main/java/io/github/kurrycat/mpkmod/gui/ComponentScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.Set;
import java.util.stream.Collectors;

public abstract class ComponentScreen extends MPKGuiScreen implements PaneHolder, MouseInputListener, MouseScrollListener, MessageReceiver {
public abstract class ComponentScreen extends MPKGuiScreen implements PaneHolder, MouseInputListener, MouseScrollListener, KeyInputListener, MessageReceiver {
public ArrayList<Component> components = new ArrayList<>();
public ArrayList<Pane> openPanes = new ArrayList<>();

Expand Down Expand Up @@ -53,6 +53,8 @@ public void onGuiClosed() {
public void onKeyEvent(int keyCode, String key, boolean pressed) {
super.onKeyEvent(keyCode, key, pressed);

if(handleKeyInput(keyCode, key, pressed)) return;

if (pressed && !selected.isEmpty()) {
Vector2D arrowKeyMove = Vector2D.ZERO;
switch (key) {
Expand Down Expand Up @@ -185,6 +187,13 @@ public boolean handleMouseScroll(Vector2D mousePos, int delta) {
);
}

public boolean handleKeyInput(int keyCode, String key, boolean pressed) {
return ArrayListUtil.orMap(
ArrayListUtil.getAllOfType(KeyInputListener.class, components, movableComponents),
b -> b.handleKeyInput(keyCode, key, pressed)
);
}

public ArrayList<Component> findContainPos(Vector2D p) {
return movableComponents.stream().filter(c -> c.contains(p)).collect(Collectors.toCollection(ArrayList::new));
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/kurrycat/mpkmod/gui/MPKGuiScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

@SuppressWarnings("unused")
public abstract class MPKGuiScreen {
private boolean initialized = false;

public void onGuiInit() {
initialized = true;
}

public void onGuiClosed() {
Expand Down Expand Up @@ -46,4 +48,12 @@ public void drawDefaultBackground() {
public boolean shouldCreateKeyBind() {
return false;
}

public boolean resetOnOpen() {
return true;
}

public boolean isInitialized() {
return initialized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ public class CheckButton extends Component implements MouseInputListener {
private boolean isChecked = false;

public CheckButton(Vector2D pos, CheckButtonCallback checkButtonCallback) {
super(pos);
super(pos.round());
this.checkButtonCallback = checkButtonCallback;
this.setSize(new Vector2D(11, 11));
}

public CheckButton(Vector2D pos) {
super(pos);
this.checkButtonCallback = null;
this.setSize(new Vector2D(11, 11));
this(pos, null);
}

public boolean isChecked() {
Expand All @@ -38,9 +36,9 @@ public void render(Vector2D mouse) {
Renderer2D.drawRectWithEdge(getDisplayPos(), getSize(), 1, normalColor, normalColor);

if (isChecked)
FontRenderer.drawCenteredString(
FontRenderer.drawString(
"x",
getDisplayPos().add(getSize().div(2)).add(new Vector2D(0.3, 0)),
getDisplayPos().add(new Vector2D(2, 1)),
Color.WHITE,
false
);
Expand Down
Loading

0 comments on commit 4ba5737

Please sign in to comment.