Add touchscreen look handling and tweaks.

This commit is contained in:
LambdAurora
2019-12-15 00:03:20 +01:00
parent d3100ee974
commit 1d7c48cf59
12 changed files with 113 additions and 24 deletions

View File

@@ -1,7 +1,8 @@
# LambdaControls
![Java 8](https://img.shields.io/badge/language-Java%208-9B599A.svg?style=flat-square)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/LambdAurora/LambdaControls/master/LICENSE)
[![GitHub license](https://img.shields.io/github/license/LambdAurora/LambdaControls?style=flat-square)](https://raw.githubusercontent.com/LambdAurora/LambdaControls/master/LICENSE)
![Environment: Client](https://img.shields.io/badge/environment-client-1976d2?style=flat-square)
A Fabric Minecraft mod which add better controls.
It allows the use of a controller or the touchscreen.

View File

@@ -43,9 +43,9 @@ public class LambdaControls implements ClientModInitializer
InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_KP_4, "key.categories.movement").build();
public static final Identifier CONTROLLER_BUTTONS = new Identifier("lambdacontrols", "textures/gui/controller_buttons.png");
public final Logger logger = LogManager.getLogger("LambdaControls");
public final LambdaControlsConfig config = new LambdaControlsConfig(this);
public final ControllerInput controller_input = new ControllerInput(this);
private ControlsMode previous_controls_mode;
public final LambdaControlsConfig config = new LambdaControlsConfig(this);
public final LambdaInput input = new LambdaInput(this);
private ControlsMode previous_controls_mode;
@Override
public void onInitializeClient()
@@ -88,14 +88,14 @@ public class LambdaControls implements ClientModInitializer
*/
public void on_tick(@NotNull MinecraftClient client)
{
this.controller_input.on_tick(client);
this.input.on_tick(client);
if (this.config.get_controls_mode() == ControlsMode.CONTROLLER)
this.controller_input.on_controller_tick(client);
this.input.on_controller_tick(client);
}
public void on_render(MinecraftClient client)
{
this.controller_input.on_render(client);
this.input.on_render(client);
}
/**

View File

@@ -11,6 +11,7 @@ package me.lambdaurora.lambdacontrols;
import me.lambdaurora.lambdacontrols.gui.LabelWidget;
import me.lambdaurora.lambdacontrols.gui.LambdaControlsControlsScreen;
import me.lambdaurora.lambdacontrols.gui.TouchscreenOverlay;
import me.lambdaurora.lambdacontrols.mixin.EntryListWidgetAccessor;
import me.lambdaurora.lambdacontrols.util.AbstractContainerScreenAccessor;
import me.lambdaurora.lambdacontrols.util.CreativeInventoryScreenAccessor;
@@ -51,9 +52,9 @@ import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_RIGHT_X;
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y;
/**
* Represents the controller input handler.
* Represents the LambdaControls' input handler.
*/
public class ControllerInput
public class LambdaInput
{
private static final Map<Integer, Boolean> BUTTON_STATES = new HashMap<>();
private static final Map<Integer, Integer> BUTTON_COOLDOWNS = new HashMap<>();
@@ -76,11 +77,16 @@ public class ControllerInput
private float mouse_speed_x = 0.F;
private float mouse_speed_y = 0.F;
public ControllerInput(@NotNull LambdaControls mod)
public LambdaInput(@NotNull LambdaControls mod)
{
this.config = mod.config;
}
/**
* This method is called every Minecraft tick.
*
* @param client The client instance.
*/
public void on_tick(@NotNull MinecraftClient client)
{
this.prev_target_yaw = this.target_yaw;
@@ -100,7 +106,7 @@ public class ControllerInput
}
/**
* This method is called every Minecraft tick.
* This method is called every Minecraft tick for controller input update.
*
* @param client The client instance.
*/
@@ -128,6 +134,12 @@ public class ControllerInput
this.ignore_next_a--;
}
/**
* This method is called before the screen is rendered.
*
* @param client The client instance.
* @param screen The screen to render.
*/
public void on_pre_render_screen(@NotNull MinecraftClient client, @NotNull Screen screen)
{
if (!is_screen_interactive(screen)) {
@@ -140,9 +152,15 @@ public class ControllerInput
}
}
/**
* This method is called when Minecraft renders.
*
* @param client The client instance.
*/
public void on_render(@NotNull MinecraftClient client)
{
if (client.currentScreen == null && (this.prev_target_yaw != this.target_yaw || this.prev_target_pitch != this.target_pitch)) {
if ((client.currentScreen == null || client.currentScreen instanceof TouchscreenOverlay) &&
(this.prev_target_yaw != this.target_yaw || this.prev_target_pitch != this.target_pitch)) {
float rotation_yaw = (float) (client.player.prevYaw + (this.target_yaw - client.player.prevYaw) * client.getTickDelta());
float rotation_pitch = (float) (client.player.prevPitch + (this.target_pitch - client.player.prevPitch) * client.getTickDelta());
client.player.yaw = rotation_yaw;
@@ -153,6 +171,13 @@ public class ControllerInput
}
}
/**
* This method is called when a Screen is opened.
*
* @param client The client instance.
* @param window_width The window width.
* @param window_height The window height.
*/
public void on_screen_open(@NotNull MinecraftClient client, int window_width, int window_height)
{
if (client.currentScreen == null) {
@@ -540,7 +565,7 @@ public class ControllerInput
* @param value The value of the look.
* @param state The state.
*/
private void handle_look(@NotNull MinecraftClient client, int axis, float value, int state)
public void handle_look(@NotNull MinecraftClient client, int axis, float value, int state)
{
// Handles the look direction.
if (client.player != null) {

View File

@@ -211,10 +211,10 @@ public class LabelWidget extends DrawableHelper implements Element, Drawable
this.fillGradient(k + i + 2, l - 3 + 1, k + i + 3, l + n + 3 - 1, 1347420415, 1344798847);
this.fillGradient(k - 3, l - 3, k + i + 3, l - 3 + 1, 1347420415, 1347420415);
this.fillGradient(k - 3, l + n + 2, k + i + 3, l + n + 3, 1344798847, 1344798847);
MatrixStack matrixStack = new MatrixStack();
MatrixStack matrix_stack = new MatrixStack();
VertexConsumerProvider.Immediate immediate = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer());
matrixStack.translate(0.0D, 0.0D, this.client.getItemRenderer().zOffset);
Matrix4f matrix4f = matrixStack.peek().getModel();
matrix_stack.translate(0.0D, 0.0D, this.client.getItemRenderer().zOffset);
Matrix4f matrix4f = matrix_stack.peek().getModel();
for (int r = 0; r < text.size(); ++r) {
String string2 = text.get(r);

View File

@@ -41,10 +41,10 @@ public class LambdaControlsHud extends DrawableHelper
x += this.draw_button_tip(x, y, ButtonBinding.INVENTORY, true) + 10;
this.draw_button_tip(x, y, ButtonBinding.SWAP_HANDS, true);
x = 10;
x += this.draw_button_tip(x, (y -= 20), ButtonBinding.DROP_ITEM, !client.player.getMainHandStack().isEmpty()) + 10;
x += this.draw_button_tip(x, (y -= 20), ButtonBinding.DROP_ITEM, !this.client.player.getMainHandStack().isEmpty()) + 10;
this.draw_button_tip(x, y, ButtonBinding.ATTACK.get_button(),
client.crosshairTarget.getType() == HitResult.Type.BLOCK ? "lambdacontrols.action.hit" : ButtonBinding.ATTACK.get_translation_key(),
client.crosshairTarget.getType() != HitResult.Type.MISS);
this.client.crosshairTarget.getType() == HitResult.Type.BLOCK ? "lambdacontrols.action.hit" : ButtonBinding.ATTACK.get_translation_key(),
this.client.crosshairTarget.getType() != HitResult.Type.MISS);
}
}

View File

@@ -61,8 +61,10 @@ public class LambdaControlsSettingsScreen extends Screen
this.mod.config.set_controller(Controller.by_id(current_id));
}, (game_options, option) -> {
String controller_name = this.mod.config.get_controller().get_name();
if (controller_name.equals(String.valueOf(this.mod.config.get_controller().get_id())))
if (!this.mod.config.get_controller().is_connected())
return option.getDisplayPrefix() + Formatting.RED + controller_name;
else if (!this.mod.config.get_controller().is_gamepad())
return option.getDisplayPrefix() + Formatting.GOLD + controller_name;
else
return option.getDisplayPrefix() + controller_name;
});
@@ -81,7 +83,7 @@ public class LambdaControlsSettingsScreen extends Screen
}
}, (game_options, option) -> {
String value = String.valueOf(option.get(options));
return option.getDisplayPrefix() + value.substring(0, value.length() > 5 ? 5 : value.length());
return option.getDisplayPrefix() + value.substring(0, Math.min(value.length(), 5));
});
this.rotation_speed_option = new DoubleOption("lambdacontrols.menu.rotation_speed", 0.0, 50.0, 0.5F, game_options -> this.mod.config.get_rotation_speed(),
(game_options, new_value) -> {

View File

@@ -37,8 +37,11 @@ public class ReloadControllerMappingsOption extends Option implements Nameable
public AbstractButtonWidget createButton(GameOptions options, int x, int y, int width)
{
return new ButtonWidget(x, y, width, 20, this.get_name(), btn -> {
MinecraftClient client = MinecraftClient.getInstance();
Controller.update_mappings();
MinecraftClient.getInstance().getToastManager().add(new SystemToast(SystemToast.Type.TUTORIAL_HINT, new TranslatableText("lambdacontrols.controller.mappings.updated"), null));
if (client.currentScreen != null)
client.currentScreen.init(client, client.getWindow().getScaledWidth(), client.getWindow().getScaledHeight());
client.getToastManager().add(new SystemToast(SystemToast.Type.TUTORIAL_HINT, new TranslatableText("lambdacontrols.controller.mappings.updated"), null));
});
}

View File

@@ -25,6 +25,10 @@ import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_RIGHT_X;
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_RIGHT_Y;
/**
* Represents the touchscreen overlay
@@ -271,4 +275,21 @@ public class TouchscreenOverlay extends Screen
}
return super.mouseClicked(mouse_x, mouse_y, button);
}
@Override
public boolean mouseDragged(double mouse_x, double mouse_y, int button, double delta_x, double delta_y)
{
if (button == GLFW.GLFW_MOUSE_BUTTON_1 && this.minecraft != null) {
if (delta_y > 0.01)
this.mod.input.handle_look(this.minecraft, GLFW_GAMEPAD_AXIS_RIGHT_Y, (float) Math.abs(delta_y / 5.0), 2);
else if (delta_y < 0.01)
this.mod.input.handle_look(this.minecraft, GLFW_GAMEPAD_AXIS_RIGHT_Y, (float) Math.abs(delta_y / 5.0), 1);
if (delta_x > 0.01)
this.mod.input.handle_look(this.minecraft, GLFW_GAMEPAD_AXIS_RIGHT_X, (float) Math.abs(delta_x / 5.0), 2);
else if (delta_x < 0.01)
this.mod.input.handle_look(this.minecraft, GLFW_GAMEPAD_AXIS_RIGHT_X, (float) Math.abs(delta_x / 5.0), 1);
}
return super.mouseDragged(mouse_x, mouse_y, button, delta_x, delta_y);
}
}

View File

@@ -31,6 +31,6 @@ public class GameRendererMixin
private void on_render(float tick_delta, long start_time, boolean full_render, CallbackInfo ci)
{
if (this.client.currentScreen != null && LambdaControls.get().config.get_controls_mode() == ControlsMode.CONTROLLER)
LambdaControls.get().controller_input.on_pre_render_screen(this.client, this.client.currentScreen);
LambdaControls.get().input.on_pre_render_screen(this.client, this.client.currentScreen);
}
}

View File

@@ -62,7 +62,7 @@ public abstract class MinecraftClientMixin
this.skipGameRender = false;
this.currentScreen = screen;
} else if (screen != null) {
mod.controller_input.on_screen_open(((MinecraftClient) (Object) this), this.window.getWidth(), this.window.getHeight());
mod.input.on_screen_open(((MinecraftClient) (Object) this), this.window.getWidth(), this.window.getHeight());
}
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright © 2019 LambdAurora <aurora42lambda@gmail.com>
*
* This file is part of LambdaControls.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/
package me.lambdaurora.lambdacontrols.mixin;
import net.minecraft.client.gui.ParentElement;
import net.minecraft.client.gui.screen.Screen;
import org.lwjgl.glfw.GLFW;
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.CallbackInfoReturnable;
@Mixin(Screen.class)
public abstract class ScreenMixin implements ParentElement
{
@Inject(method = "keyPressed", at = @At("HEAD"), cancellable = true)
private void on_key_pressed(int key_code, int scan_code, int modifiers, CallbackInfoReturnable<Boolean> ci)
{
if (key_code == GLFW.GLFW_KEY_UP || key_code == GLFW.GLFW_KEY_LEFT) {
this.changeFocus(false);
ci.setReturnValue(true);
ci.cancel();
} else if (key_code == GLFW.GLFW_KEY_DOWN || key_code == GLFW.GLFW_KEY_RIGHT) {
this.changeFocus(true);
ci.setReturnValue(true);
ci.cancel();
}
}
}

View File

@@ -12,6 +12,7 @@
"KeyBindingMixin",
"MinecraftClientMixin",
"MouseMixin",
"ScreenMixin",
"SettingsScreenMixin"
],
"injectors": {