Skip to content

Commit

Permalink
fix callback issue, fix input issues (maybe)
Browse files Browse the repository at this point in the history
  • Loading branch information
nextdayy committed Nov 26, 2024
1 parent 0204fca commit c830f6c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,20 @@ public final void set(@Nullable T value) {
*/
public void setReferential(@Nullable T value) {
if (callbacks != null) {
T prev = get();
set0(value);
for (Predicate<T> p : callbacks) {
try {
if (p.test(value)) {
LOGGER.info("property {} set cancelled by {}", this.getID(), p);
set0(prev);
return;
}
} catch (Throwable t) {
LOGGER.error("failed to call cancellable callback {} on property {}", p, this.getID(), t);
}
}
}
set0(value);
} else set0(value);
}

protected abstract void set0(@Nullable T value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ object HudManager {
LOGGER.info("Initializing HUD...")
val now = System.nanoTime()
Runtime.getRuntime().addShutdownHook(Thread {
LOGGER.info("Saving size lock as ${polyUI.size}")
ConfigManager.internal().folder.resolve("size.lock").writeText(polyUI.size.value.toString())
})
val sizeFile = ConfigManager.internal().folder.resolve("size.lock")
val size = Vec2(if (sizeFile.exists()) sizeFile.readText().toLongOrNull() ?: 0L else 0L)
val prevSize: Vec2
if (size.isPositive) {
LOGGER.info("Size to restore: $size")
prevSize = polyUI.size
polyUI.resize(size.x, size.y)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,29 @@ object KeybindManager {
@JvmStatic
fun translateKey(inputManager: InputManager, key: Int, character: Char, state: Boolean) {
if (character != '\u0000' && !character.isISOControl() && character.isDefined()) {
inputManager.keyTyped(character)
if (state) {
inputManager.keyTyped(character)
inputManager.keyDown(character.code)
} else inputManager.keyUp(character.code)
return
}

if (state) {
keysMap[key]?.let { inputManager.keyDown(it); return }
val m = modsMap[key]
if (m != 0) {
inputManager.addModifier(m.toByte())
return
}
// modern fix because glfwModCharCallback doesn't work correctly
if (inputManager.mods > 1 && key < 255) {
inputManager.keyTyped((key + 32).toChar())
} else inputManager.keyDown(key)
} else {
keysMap[key]?.let { inputManager.keyUp(it); return }
val m = modsMap[key]
if (m != 0) {
inputManager.removeModifier(m.toByte())
return
}
inputManager.keyUp(key)
val k = keysMap[key]
if (k != null) {
if (state) inputManager.keyDown(k)
else inputManager.keyUp(k)
return
}

val m = modsMap[key].toByte()
if (m != 0.toByte()) {
if (state) inputManager.addModifier(m)
else inputManager.removeModifier(m)
return
}

val raw = if (inputManager.mods > 1) key + 48 else key
if (state) inputManager.keyDown(raw)
else inputManager.keyUp(raw)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@

package org.polyfrost.oneconfig.test;

import kotlin.Unit;
import org.polyfrost.oneconfig.api.config.v1.Config;
import org.polyfrost.oneconfig.api.config.v1.annotations.Number;
import org.polyfrost.oneconfig.api.config.v1.annotations.*;
import org.polyfrost.oneconfig.api.ui.v1.Notifications;
import org.polyfrost.oneconfig.api.ui.v1.keybind.OCKeybindHelper;
import org.polyfrost.polyui.color.ColorUtils;
import org.polyfrost.polyui.color.PolyColor;
import org.polyfrost.polyui.input.KeyBinder;
import org.polyfrost.polyui.input.KeyModifiers;
import org.polyfrost.polyui.unit.Align;
import org.polyfrost.polyui.color.ColorUtils;
import org.polyfrost.universal.UChat;

@SuppressWarnings("unused")
Expand All @@ -47,7 +52,7 @@ public class TestConfig_Test extends Config {
@Number(title = "number", unit = "px", category = "bob")
public static int number = 50;

// @Keybind(title = "keybinding", description = "please send help")
// @Keybind(title = "keybinding", description = "please send help")
// KeyBinder.Bind bind0 = new KeyBinder.Bind('A', null, null, Modifiers.mods(Modifiers.LCONTROL, Modifiers.LSHIFT), () -> {
// UChat.chat("you pressed a bind");
// return true;
Expand Down Expand Up @@ -87,6 +92,12 @@ public class TestConfig_Test extends Config {
@Color(title = "color", category = "bob")
PolyColor color = ColorUtils.rgba(255, 0, 100, 1f);

@Keybind(title = "keybind")
private KeyBinder.Bind bind = ((OCKeybindHelper) OCKeybindHelper.builder().mods(KeyModifiers.CONTROL).chars('g').does((a) -> {
Notifications.enqueue(Notifications.Type.Info, "state: " + a);
return Unit.INSTANCE;
})).register();

public TestConfig_Test() {
super("test_mod.json", "Test Mod", Category.QOL);
addDependency("c3ow", "c2ow");
Expand Down

0 comments on commit c830f6c

Please sign in to comment.