Add clickable link, more options, and fixes!

This commit is contained in:
LambdAurora
2019-12-12 12:04:18 +01:00
parent c5f480a598
commit 50227e3868
10 changed files with 262 additions and 21 deletions

View File

@@ -468,7 +468,6 @@ public class ButtonBinding implements Nameable
*/
public @NotNull String get_translated_name()
{
System.out.println(id.toString());
if (this.id.get_namespace().equals("minecraft"))
return I18n.translate(this.id.get_name());
else

View File

@@ -80,7 +80,7 @@ public class Controller implements Nameable
*/
public boolean is_gamepad()
{
return GLFW.glfwJoystickIsGamepad(this.id);
return this.is_connected() && GLFW.glfwJoystickIsGamepad(this.id);
}
/**
@@ -147,7 +147,7 @@ public class Controller implements Nameable
if (Files.isReadable(path)) {
try (SeekableByteChannel fc = Files.newByteChannel(path)) {
buffer = createByteBuffer((int) fc.size() + 2);
while (fc.read(buffer) != -1);
while (fc.read(buffer) != -1) ;
buffer.put((byte) 0);
}
}
@@ -165,6 +165,7 @@ public class Controller implements Nameable
File mappings_file = new File("config/gamecontrollerdb.txt");
if (!mappings_file.exists())
return;
LambdaControls.get().log("Updating controller mappings...");
ByteBuffer buffer = io_resource_to_buffer(mappings_file.getPath(), 1024);
GLFW.glfwUpdateGamepadMappings(buffer);
} catch (IOException e) {

View File

@@ -34,6 +34,7 @@ public class LambdaControls implements ClientModInitializer
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;
@Override
public void onInitializeClient()
@@ -59,6 +60,8 @@ public class LambdaControls implements ClientModInitializer
client.getToastManager().add(new SystemToast(SystemToast.Type.TUTORIAL_HINT, new TranslatableText("lambdacontrols.controller.disconnected", jid),
null));
}
this.switch_controls_mode();
});
}
@@ -78,6 +81,25 @@ public class LambdaControls implements ClientModInitializer
this.controller_input.on_render(client);
}
/**
* Switches the controls mode if the auto switch is enabled.
*/
public void switch_controls_mode()
{
if (this.config.has_auto_switch_mode()) {
if (this.config.get_controller().is_gamepad()) {
this.previous_controls_mode = this.config.get_controls_mode();
this.config.set_controls_mode(ControlsMode.CONTROLLER);
} else {
if (this.previous_controls_mode == null) {
this.previous_controls_mode = ControlsMode.DEFAULT;
}
this.config.set_controls_mode(this.previous_controls_mode);
}
}
}
/**
* Prints a message to the terminal.
*

View File

@@ -89,6 +89,26 @@ public class LambdaControlsConfig
this.config.set("controls", controls_mode.get_name());
}
/**
* Returns whether the auto switch mode is enabled or not.
*
* @return True if the auto switch mode is enabled, else false.
*/
public boolean has_auto_switch_mode()
{
return this.config.getOrElse("auto_switch_mode", false);
}
/**
* Sets whether the auto switch mode is enabled or not.
*
* @param auto_switch_mode True if the auto switch mode is enabled, else false.
*/
public void set_auto_switch_mode(boolean auto_switch_mode)
{
this.config.set("auto_switch_mode", auto_switch_mode);
}
/**
* Returns whether the HUD is enabled.
*

View File

@@ -0,0 +1,128 @@
/*
* 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.gui;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Drawable;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.Element;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
import java.util.function.Consumer;
/**
* Represents a label widget.
*/
// @TODO move this to a GUI library.
public class LabelWidget extends DrawableHelper implements Element, Drawable
{
public static final Consumer<LabelWidget> DEFAULT_ACTION = label -> {
};
private final MinecraftClient client = MinecraftClient.getInstance();
private final Consumer<LabelWidget> click_action;
private final int x;
private final int y;
private final int max_width;
//private final int max_height;
private String text;
public boolean visible;
private int width;
private int height;
private boolean centered;
public LabelWidget(int x, int y, @NotNull String text, int max_width, @NotNull Consumer<LabelWidget> click_action, boolean centered)
{
this.visible = true;
this.x = x;
this.y = y;
this.max_width = max_width;
this.click_action = click_action;
this.centered = centered;
this.set_text(text);
}
public LabelWidget(int x, int y, @NotNull String text, int max_width, @NotNull Consumer<LabelWidget> click_action)
{
this(x, y, text, max_width, click_action, false);
}
public LabelWidget(int x, int y, @NotNull String text, int max_width, boolean centered)
{
this(x, y, text, max_width, DEFAULT_ACTION, centered);
}
public LabelWidget(int x, int y, @NotNull String text, int max_width)
{
this(x, y, text, max_width, DEFAULT_ACTION);
}
/**
* Sets the text of this label.
*
* @param text The text to set.
*/
public void set_text(@NotNull String text)
{
int width = this.client.textRenderer.getStringWidth(text);
if (width > this.max_width) {
while (width > this.max_width) {
text = text.substring(0, text.length() - 1);
width = this.client.textRenderer.getStringWidth(text);
}
}
this.text = text;
this.width = width;
this.height = this.client.textRenderer.fontHeight;
}
/**
* Gets the width of this label widget.
*
* @return The width of this label widget.
*/
public int get_width()
{
return this.width;
}
/**
* Gets the height of this label widget.
*
* @return The height of this label widget.
*/
public int get_height()
{
return this.height;
}
@Override
public void render(int mouse_x, int mouse_y, float delta)
{
if (this.centered)
this.drawCenteredString(this.client.textRenderer, this.text, this.x, this.y, 10526880);
else
this.drawString(this.client.textRenderer, this.text, this.x, this.y, 10526880);
}
@Override
public boolean mouseClicked(double mouse_x, double mouse_y, int button)
{
if (this.visible && button == GLFW.GLFW_MOUSE_BUTTON_1) {
if (mouse_x >= (double) this.x && mouse_y >= (double) this.y && mouse_x < (double) (this.x + this.width) && mouse_y < (double) (this.y + this.height)) {
this.click_action.accept(this);
return true;
}
}
return false;
}
}

View File

@@ -20,6 +20,7 @@ import net.minecraft.client.options.*;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.Util;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
@@ -28,17 +29,21 @@ import org.lwjgl.glfw.GLFW;
*/
public class LambdaControlsSettingsScreen extends Screen
{
final LambdaControls mod;
private final Screen parent;
private final GameOptions options;
private final Option controller_option;
private final Option controller_type_option;
private final Option hud_enable_option;
private final Option hud_side_option;
private final Option dead_zone_option;
private final Option rotation_speed_option;
private final Option mouse_speed_option;
private ButtonListWidget list;
public static final String GAMEPAD_TOOL_URL = "http://generalarcade.com/gamepadtool/";
final LambdaControls mod;
private final Screen parent;
private final GameOptions options;
private final Option auto_switch_mode_option;
private final Option controller_option;
private final Option controller_type_option;
private final Option dead_zone_option;
private final Option hud_enable_option;
private final Option hud_side_option;
private final Option mouse_speed_option;
private final Option rotation_speed_option;
private final String controller_mappings_url_text = I18n.translate("lambdacontrols.controller.mappings.2", Formatting.GOLD.toString(), GAMEPAD_TOOL_URL, Formatting.RESET.toString());
private ButtonListWidget list;
private LabelWidget gamepad_tool_url_label;
public LambdaControlsSettingsScreen(Screen parent, @NotNull GameOptions options)
{
@@ -46,6 +51,8 @@ public class LambdaControlsSettingsScreen extends Screen
this.mod = LambdaControls.get();
this.parent = parent;
this.options = options;
this.auto_switch_mode_option = new BooleanOption("lambdacontrols.menu.auto_switch_mode", game_options -> this.mod.config.has_auto_switch_mode(),
(game_options, new_value) -> this.mod.config.set_auto_switch_mode(new_value));
this.controller_option = new CyclingOption("lambdacontrols.menu.controller", (game_options, amount) -> {
int current_id = this.mod.config.get_controller().get_id();
current_id += amount;
@@ -134,8 +141,14 @@ public class LambdaControlsSettingsScreen extends Screen
this.list.addOptionEntry(this.controller_type_option, this.dead_zone_option);
this.list.addOptionEntry(this.hud_enable_option, this.hud_side_option);
this.list.addOptionEntry(this.rotation_speed_option, this.mouse_speed_option);
this.list.addSingleOptionEntry(this.auto_switch_mode_option);
this.list.addSingleOptionEntry(new ReloadControllerMappingsOption());
this.children.add(this.list);
this.gamepad_tool_url_label = new LabelWidget(this.width / 2, this.height - 29 - (5 + this.font.fontHeight) * 2, this.controller_mappings_url_text, this.width,
label -> Util.getOperatingSystem().open(GAMEPAD_TOOL_URL), true);
this.children.add(this.gamepad_tool_url_label);
this.addButton(new ButtonWidget(this.width / 2 - 155, this.height - 29, 300, button_height, I18n.translate("gui.done"),
(buttonWidget) -> this.minecraft.openScreen(this.parent)));
}
@@ -148,7 +161,7 @@ public class LambdaControlsSettingsScreen extends Screen
super.render(mouse_x, mouse_y, delta);
this.drawCenteredString(this.font, I18n.translate("lambdacontrols.menu.title"), this.width / 2, 8, 16777215);
this.drawCenteredString(this.font, I18n.translate("lambdacontrols.controller.mappings.1", Formatting.GREEN.toString(), Formatting.RESET.toString()), this.width / 2, this.height - 29 - (5 + this.font.fontHeight) * 3, 10526880);
this.drawCenteredString(this.font, I18n.translate("lambdacontrols.controller.mappings.2", Formatting.GOLD.toString(), Formatting.RESET.toString()), this.width / 2, this.height - 29 - (5 + this.font.fontHeight) * 2, 10526880);
this.gamepad_tool_url_label.render(mouse_x, mouse_y, delta);
this.drawCenteredString(this.font, I18n.translate("lambdacontrols.controller.mappings.3", Formatting.GREEN.toString(), Formatting.RESET.toString()), this.width / 2, this.height - 29 - (5 + this.font.fontHeight), 10526880);
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.gui;
import me.lambdaurora.lambdacontrols.Controller;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.AbstractButtonWidget;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.options.GameOptions;
import net.minecraft.client.options.Option;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.text.TranslatableText;
import org.aperlambda.lambdacommon.utils.Nameable;
import org.jetbrains.annotations.NotNull;
/**
* Represents the option to reload the controller mappings.
*/
public class ReloadControllerMappingsOption extends Option implements Nameable
{
private static final String KEY = "lambdacontrols.menu.reload_controller_mappings";
public ReloadControllerMappingsOption()
{
super(KEY);
}
@Override
public AbstractButtonWidget createButton(GameOptions options, int x, int y, int width)
{
return new ButtonWidget(x, y, width, 20, this.get_name(), btn -> {
Controller.update_mappings();
MinecraftClient.getInstance().getToastManager().add(new SystemToast(SystemToast.Type.TUTORIAL_HINT, new TranslatableText("lambdacontrols.controller.mappings.updated"), null));
});
}
@Override
public @NotNull String get_name()
{
return I18n.translate(KEY);
}
}