🚧 More touchscreen work.

This commit is contained in:
LambdAurora
2019-11-27 23:03:06 +01:00
parent eeaf35564a
commit 6f09036814
3 changed files with 115 additions and 16 deletions

View File

@@ -51,4 +51,12 @@ public class TouchscreenButtonWidget extends TexturedButtonWidget
} else } else
return false; return false;
} }
@Override
protected void onDrag(double mouseX, double mouseY, double deltaX, double deltaY)
{
super.onDrag(mouseX, mouseY, deltaX, deltaY);
if (this.active && !this.isHovered)
this.on_change_state.accept(false);
}
} }

View File

@@ -31,8 +31,16 @@ import org.jetbrains.annotations.NotNull;
*/ */
public class TouchscreenOverlay extends Screen public class TouchscreenOverlay extends Screen
{ {
public static final Identifier WIDGETS_LOCATION = new Identifier("lambdacontrols", "textures/gui/widgets.png"); public static final Identifier WIDGETS_LOCATION = new Identifier("lambdacontrols", "textures/gui/widgets.png");
private LambdaControls mod; private LambdaControls mod;
private TouchscreenButtonWidget jump_button;
private TouchscreenButtonWidget fly_button;
private TouchscreenButtonWidget fly_up_button;
private TouchscreenButtonWidget fly_down_button;
private int fly_button_enable_ticks = 0;
private int forward_button_tick = 0;
private TouchscreenButtonWidget forward_left_button;
private TouchscreenButtonWidget forward_right_button;
private TouchscreenButtonWidget start_sneak_button; private TouchscreenButtonWidget start_sneak_button;
private TouchscreenButtonWidget end_sneak_button; private TouchscreenButtonWidget end_sneak_button;
@@ -43,12 +51,6 @@ public class TouchscreenOverlay extends Screen
this.passEvents = true; this.passEvents = true;
} }
@Override
public boolean shouldCloseOnEsc()
{
return false;
}
@Override @Override
public boolean isPauseScreen() public boolean isPauseScreen()
{ {
@@ -56,9 +58,9 @@ public class TouchscreenOverlay extends Screen
} }
@Override @Override
public void onClose() public boolean keyPressed(int keyCode, int scanCode, int modifiers)
{ {
super.onClose(); return false;
} }
private void pause_game(boolean bl) private void pause_game(boolean bl)
@@ -74,7 +76,68 @@ public class TouchscreenOverlay extends Screen
} }
} }
/**
* Updates the forward button ticks cooldown.
*
* @param state The button state.
*/
private void update_forward_buttons_state(boolean state)
{
if (state)
this.forward_button_tick = -1;
else
this.forward_button_tick = 20;
}
/**
* Updates the jump buttons.
*/
private void update_jump_buttons()
{
if (this.minecraft == null)
return;
if (this.minecraft.player.abilities.allowFlying && this.minecraft.player.abilities.flying) {
boolean old_state_fly = this.fly_button.visible;
this.jump_button.visible = false;
this.fly_button.visible = true;
this.fly_up_button.visible = true;
this.fly_down_button.visible = true;
if (old_state_fly != this.fly_button.visible)
this.fly_button_enable_ticks = 5;
else if (this.fly_button_enable_ticks > 0)
this.fly_button_enable_ticks--;
} else {
this.jump_button.visible = true;
this.fly_button.visible = false;
this.fly_up_button.visible = false;
this.fly_down_button.visible = false;
}
}
/**
* Handles the jump button.
*
* @param state The state of the jump button.
*/
private void handle_jump(boolean state)
{
((LambdaKeyBinding) this.minecraft.options.keyJump).handle_press_state(state);
}
@Override @Override
public void tick()
{
if (this.forward_button_tick > 0) {
this.forward_button_tick--;
} else if (this.forward_button_tick == 0) {
if (this.forward_left_button.visible)
this.forward_left_button.visible = false;
if (this.forward_right_button.visible)
this.forward_right_button.visible = false;
}
this.update_jump_buttons();
}
protected void init() protected void init()
{ {
super.init(); super.init();
@@ -105,15 +168,15 @@ public class TouchscreenOverlay extends Screen
int sneak_button_y = scaled_height - 10 - 40 - 5; int sneak_button_y = scaled_height - 10 - 40 - 5;
if (this.mod.config.get_hud_side() == HudSide.LEFT) { if (this.mod.config.get_hud_side() == HudSide.LEFT) {
jump_button_x = scaled_width - 20 - 20; jump_button_x = scaled_width - 20 - 20;
swap_hands_x = jump_button_x - 5 - 20; swap_hands_x = jump_button_x - 5 - 40;
sneak_button_x = 10 + 20 + 5; sneak_button_x = 10 + 20 + 5;
} else { } else {
jump_button_x = 20; jump_button_x = 20;
swap_hands_x = jump_button_x + 5 + 20; swap_hands_x = jump_button_x + 5 + 40;
sneak_button_x = scaled_width - 10 - 40 - 5; sneak_button_x = scaled_width - 10 - 40 - 5;
} }
// Swap items hand. // Swap items hand.
this.addButton(new TouchscreenButtonWidget(swap_hands_x, scaled_height - 5 - 20 - 40, 20, 20, 0, 0, 20, WIDGETS_LOCATION, this.addButton(new TouchscreenButtonWidget(swap_hands_x, sneak_button_y, 20, 20, 0, 160, 20, WIDGETS_LOCATION,
state -> { state -> {
if (state) { if (state) {
if (!this.minecraft.player.isSpectator()) { if (!this.minecraft.player.isSpectator()) {
@@ -122,11 +185,20 @@ public class TouchscreenOverlay extends Screen
} }
})); }));
// Drop // Drop
this.addButton(new TouchscreenButtonWidget(swap_hands_x, scaled_height - 40, 20, 20, 0, 0, 20, WIDGETS_LOCATION, this.addButton(new TouchscreenButtonWidget(swap_hands_x, sneak_button_y + 5 + 20, 20, 20, 20, 160, 20, WIDGETS_LOCATION,
state -> ((LambdaKeyBinding) this.minecraft.options.keyDrop).handle_press_state(state))); state -> ((LambdaKeyBinding) this.minecraft.options.keyDrop).handle_press_state(state)));
// Jump keys // Jump keys
this.addButton(new TouchscreenButtonWidget(jump_button_x, scaled_height - 40, 20, 20, 0, 40, 20, WIDGETS_LOCATION, this.addButton(this.jump_button = new TouchscreenButtonWidget(jump_button_x, sneak_button_y, 20, 20, 0, 40, 20, WIDGETS_LOCATION,
state -> ((LambdaKeyBinding) this.minecraft.options.keyJump).handle_press_state(state))); this::handle_jump));
this.addButton(this.fly_button = new TouchscreenButtonWidget(jump_button_x, sneak_button_y, 20, 20, 20, 40, 20, WIDGETS_LOCATION,
state -> {
if (this.fly_button_enable_ticks == 0) this.minecraft.player.abilities.flying = false;
}));
this.addButton(this.fly_up_button = new TouchscreenButtonWidget(jump_button_x, sneak_button_y - 5 - 20, 20, 20, 40, 40, 20, WIDGETS_LOCATION,
this::handle_jump));
this.addButton(this.fly_down_button = new TouchscreenButtonWidget(jump_button_x, sneak_button_y + 20 + 5, 20, 20, 60, 40, 20, WIDGETS_LOCATION,
state -> ((LambdaKeyBinding) this.minecraft.options.keySneak).handle_press_state(state)));
this.update_jump_buttons();
// Movements keys // Movements keys
this.addButton((this.start_sneak_button = new TouchscreenButtonWidget(sneak_button_x, sneak_button_y, 20, 20, 0, 120, 20, WIDGETS_LOCATION, this.addButton((this.start_sneak_button = new TouchscreenButtonWidget(sneak_button_x, sneak_button_y, 20, 20, 0, 120, 20, WIDGETS_LOCATION,
state -> { state -> {
@@ -145,8 +217,27 @@ public class TouchscreenOverlay extends Screen
} }
}))); })));
this.end_sneak_button.visible = false; this.end_sneak_button.visible = false;
this.addButton(this.forward_left_button = new TouchscreenButtonWidget(sneak_button_x - 20 - 5, sneak_button_y - 5 - 20, 20, 20, 80, 80, 20, WIDGETS_LOCATION,
state -> {
((LambdaKeyBinding) this.minecraft.options.keyForward).handle_press_state(state);
((LambdaKeyBinding) this.minecraft.options.keyLeft).handle_press_state(state);
this.update_forward_buttons_state(state);
}));
this.forward_left_button.visible = false;
this.addButton(new TouchscreenButtonWidget(sneak_button_x, sneak_button_y - 5 - 20, 20, 20, 0, 80, 20, WIDGETS_LOCATION, this.addButton(new TouchscreenButtonWidget(sneak_button_x, sneak_button_y - 5 - 20, 20, 20, 0, 80, 20, WIDGETS_LOCATION,
state -> ((LambdaKeyBinding) this.minecraft.options.keyForward).handle_press_state(state))); state -> {
((LambdaKeyBinding) this.minecraft.options.keyForward).handle_press_state(state);
this.update_forward_buttons_state(state);
this.forward_left_button.visible = true;
this.forward_right_button.visible = true;
}));
this.addButton(this.forward_right_button = new TouchscreenButtonWidget(sneak_button_x + 20 + 5, sneak_button_y - 5 - 20, 20, 20, 100, 80, 20, WIDGETS_LOCATION,
state -> {
((LambdaKeyBinding) this.minecraft.options.keyForward).handle_press_state(state);
((LambdaKeyBinding) this.minecraft.options.keyRight).handle_press_state(state);
this.update_forward_buttons_state(state);
}));
this.forward_right_button.visible = true;
this.addButton(new TouchscreenButtonWidget(sneak_button_x + 20 + 5, sneak_button_y, 20, 20, 20, 80, 20, WIDGETS_LOCATION, this.addButton(new TouchscreenButtonWidget(sneak_button_x + 20 + 5, sneak_button_y, 20, 20, 20, 80, 20, WIDGETS_LOCATION,
state -> ((LambdaKeyBinding) this.minecraft.options.keyRight).handle_press_state(state))); state -> ((LambdaKeyBinding) this.minecraft.options.keyRight).handle_press_state(state)));
this.addButton(new TouchscreenButtonWidget(sneak_button_x, sneak_button_y + 20 + 5, 20, 20, 40, 80, 20, WIDGETS_LOCATION, this.addButton(new TouchscreenButtonWidget(sneak_button_x, sneak_button_y + 20 + 5, 20, 20, 40, 80, 20, WIDGETS_LOCATION,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

After

Width:  |  Height:  |  Size: 13 KiB