Handle mouse clicks in radial menu

This commit is contained in:
DBotThePony 2022-09-23 23:38:33 +07:00
parent fa647a4b5c
commit accfe1ebc5
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 36 additions and 5 deletions

View File

@ -180,6 +180,7 @@ public final class OverdriveThatMatters {
}
EVENT_BUS.addListener(EventPriority.NORMAL, AndroidMenuKeyMapping.INSTANCE::onRenderGuiEvent);
EVENT_BUS.addListener(EventPriority.NORMAL, AndroidMenuKeyMapping.INSTANCE::onMouseClick);
event.enqueueWork(GlobalEventHandlerKt::recordClientThread);

View File

@ -5,6 +5,7 @@ import com.mojang.blaze3d.systems.RenderSystem
import it.unimi.dsi.fastutil.objects.Object2FloatArrayMap
import it.unimi.dsi.fastutil.objects.Object2FloatFunction
import net.minecraft.client.KeyMapping
import net.minecraftforge.client.event.InputEvent
import net.minecraftforge.client.event.RegisterKeyMappingsEvent
import net.minecraftforge.client.event.RenderGuiEvent
import net.minecraftforge.client.settings.KeyConflictContext
@ -41,6 +42,10 @@ object AndroidMenuKeyMapping : KeyMapping("key.otm.android_menu", KeyConflictCon
private var lastSelectedDegree: Double? = null
private val lastSelectProgress = Object2FloatArrayMap<AndroidFeature>()
private var holdIt: AndroidSwitchableFeature? = null
private var clickOnce = false
override fun setDown(isDown: Boolean) {
val old = this.isDown
super.setDown(isDown)
@ -60,10 +65,12 @@ object AndroidMenuKeyMapping : KeyMapping("key.otm.android_menu", KeyConflictCon
if (minecraft.screen == null) {
minecraft.mouseHandler.grabMouse()
val selectedFeature = selectedFeature
if (!clickOnce) {
val selectedFeature = selectedFeature
if (selectedFeature != null) {
MatteryPlayerNetworkChannel.sendToServer(SwitchAndroidFeaturePacket(selectedFeature.type, !selectedFeature.isActive))
if (selectedFeature != null) {
MatteryPlayerNetworkChannel.sendToServer(SwitchAndroidFeaturePacket(selectedFeature.type, !selectedFeature.isActive))
}
}
}
}
@ -72,6 +79,29 @@ object AndroidMenuKeyMapping : KeyMapping("key.otm.android_menu", KeyConflictCon
lastSelectedFeature = null
lastSelectedDegree = null
lastSelectProgress.clear()
clickOnce = false
holdIt = null
}
}
fun onMouseClick(event: InputEvent.MouseButton.Pre) {
if (grabbedInput) {
clickOnce = true
event.isCanceled = true
if (event.button == InputConstants.MOUSE_BUTTON_LEFT && event.action == 1) {
if (selectedFeature != null) {
playGuiClickSound()
holdIt = selectedFeature
}
} else if (event.button == InputConstants.MOUSE_BUTTON_LEFT && event.action == 0) {
holdIt = null
val selectedFeature = selectedFeature
if (selectedFeature != null) {
MatteryPlayerNetworkChannel.sendToServer(SwitchAndroidFeaturePacket(selectedFeature.type, !selectedFeature.isActive))
}
}
}
}
@ -114,7 +144,7 @@ object AndroidMenuKeyMapping : KeyMapping("key.otm.android_menu", KeyConflictCon
val length = (mouseX * mouseX + mouseY * mouseY).pow(0.5)
if ((length / minecraft.window.guiScale) in (size * 0.3f) .. size) {
if ((length / minecraft.window.guiScale) in (size * 0.3f) .. size || holdIt != null) {
var deg = acos(mouseX / length)
// opengl
@ -127,7 +157,7 @@ object AndroidMenuKeyMapping : KeyMapping("key.otm.android_menu", KeyConflictCon
deg += PI * 2
}
val index = (deg / degreePerSlice).toInt()
val index = if (holdIt != null) features.indexOf(holdIt) else (deg / degreePerSlice).toInt()
selectedFeature = features[index]
lastSelectedFeature = features[index]