Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update controller code for key detection and add setting for controller enablement #10

Merged
merged 13 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 135 additions & 5 deletions src/client/java/minicraft/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import minicraft.util.Quest;
import minicraft.util.Quest.QuestSeries;
import org.intellij.lang.annotations.MagicConstant;
import org.jetbrains.annotations.NotNull;

import javax.imageio.ImageIO;

Expand All @@ -58,6 +59,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

public class Renderer extends Game {
private Renderer() {
Expand Down Expand Up @@ -202,6 +204,8 @@ public static class AppStatusBar {
private static final int DURATION_ON_UPDATE = 90; // 1.5s

public final AppStatusElement HARDWARE_ACCELERATION_STATUS = new HardwareAccelerationElementStatus();
public final AppStatusElement CONTROLLER_STATUS = new ControllerElementStatus();
public final AppStatusElement INPUT_METHOD_STATUS = new InputMethodElementStatus();

private AppStatusBar() {}

Expand All @@ -220,12 +224,18 @@ private void render() {

// Hardware Acceleration Status (width = 16)
HARDWARE_ACCELERATION_STATUS.render(Screen.w - 16 * 8 + 5, 2, sheet);
// Controller Status (width = 14)
CONTROLLER_STATUS.render(Screen.w - 16 * 8 + 21 - 1, 2, sheet);
// Input Method Status (width = 14)
INPUT_METHOD_STATUS.render(Screen.w - 16 * 8 + 35 - 1, 2, sheet);
}

void tick() {
if (duration > 0)
duration--;
HARDWARE_ACCELERATION_STATUS.tick();
CONTROLLER_STATUS.tick();
INPUT_METHOD_STATUS.tick();
}

void show(int duration) {
Expand Down Expand Up @@ -257,7 +267,8 @@ private AppStatusElement(int size) {
protected void render(int x, int y, MinicraftImage sheet) {
if (durationUpdated > 0) {
if (blinking) {
screen.render(x, y, 10, 3 + size, 0, sheet);
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 10 + xx, 3 + size, 0, sheet);
}
}
}
Expand All @@ -281,15 +292,17 @@ protected void updateStatus() {

public abstract void updateStatus(int status);

public void notifyStatusIf(UnaryOperator<@NotNull Integer> operator) {}

protected void initialize() {}
}

public class HardwareAccelerationElementStatus extends AppStatusElement {
public final int ACCELERATION_ON = 0;
public final int ACCELERATION_OFF = 1;
public static final int ACCELERATION_ON = 0;
public static final int ACCELERATION_OFF = 1;

@MagicConstant(intValues = {ACCELERATION_ON, ACCELERATION_OFF})
private int status = ACCELERATION_ON;
private int status;

private HardwareAccelerationElementStatus() {
super(0);
Expand All @@ -307,7 +320,7 @@ protected void render(int x, int y, MinicraftImage sheet) {
if (status == ACCELERATION_ON) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, xx, 4, 0, sheet);
} else {
} else if (status == ACCELERATION_OFF) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 2 + xx, 4, 0, sheet);
}
Expand All @@ -319,6 +332,123 @@ public void updateStatus(int status) {
this.status = status;
}
}

public class ControllerElementStatus extends AppStatusElement {
public static final int CONTROLLER_CONNECTED = 0; // Normal state, usable controller
public static final int CONTROLLER_UNAVAILABLE = 1; // Current controller becoming unusable
public static final int CONTROLLER_DISCONNECTED = 2; // System normal, no controller connected
public static final int CONTROLLER_PENDING = 3; // Temporary unusable or controller hanging/delaying
public static final int CONTROLLER_UNKNOWN = 4; // Unknown state or unknown controller (unsupported)
// TODO #614 tacking and referring this state
public static final int CONTROLLER_FUNCTION_UNAVAILABLE = 5; // System not available/malfunctioning
// public static final int CONTROLLER_UNDER_CONFIGURATION = 6; // Alternating 0 and 3 // Reserved

@MagicConstant(intValues = {CONTROLLER_CONNECTED, CONTROLLER_UNAVAILABLE, CONTROLLER_DISCONNECTED,
CONTROLLER_PENDING, CONTROLLER_UNKNOWN, CONTROLLER_FUNCTION_UNAVAILABLE})
private int status;

private ControllerElementStatus() {
super(1);
status = CONTROLLER_DISCONNECTED; // Default state
}

@Override
protected void render(int x, int y, MinicraftImage sheet) {
super.render(x, y, sheet);
if (status == CONTROLLER_CONNECTED) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, xx, 2, 0, sheet);
} else if (status == CONTROLLER_UNAVAILABLE) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 2 + xx, 2, 0, sheet);
} else if (status == CONTROLLER_DISCONNECTED) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 4 + xx, 2, 0, sheet);
} else if (status == CONTROLLER_PENDING) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 6 + xx, 2, 0, sheet);
} else if (status == CONTROLLER_UNKNOWN) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 8 + xx, 2, 0, sheet);
} else if (status == CONTROLLER_FUNCTION_UNAVAILABLE) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 10 + xx, 2, 0, sheet);
}
}

@Override
protected void tick() {
super.tick();
// Reserved
}

@Override
public void updateStatus(int status) {
super.updateStatus();
this.status = status;
}

@Override
public void notifyStatusIf(UnaryOperator<@NotNull Integer> operator) {
int status = this.status;
//noinspection MagicConstant
if ((status = operator.apply(status)) != this.status)
updateStatus(status);
}
}

public class InputMethodElementStatus extends AppStatusElement { // Only 2 methods: keyboard and controller
public static final int INPUT_KEYBOARD_ONLY = 0; // Only keyboard is accepted
public static final int INPUT_CONTROLLER_PRIOR = 1; // Both accepted, but controller is used priorly
public static final int INPUT_KEYBOARD_PRIOR = 2; // Both accepted, but keyboard is used priorly
// Controller is enabled, but only keyboard can be used as controller is currently unusable.
public static final int INPUT_CONTROLLER_UNUSABLE = 3;

@MagicConstant(intValues = {INPUT_KEYBOARD_ONLY, INPUT_CONTROLLER_PRIOR,
INPUT_KEYBOARD_PRIOR, INPUT_CONTROLLER_UNUSABLE})
private int status;

private InputMethodElementStatus() {
super(1);
status = INPUT_KEYBOARD_ONLY;
}

@Override
protected void render(int x, int y, MinicraftImage sheet) {
super.render(x, y, sheet);
if (status == INPUT_KEYBOARD_ONLY) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, xx, 5, 0, sheet);
} else if (status == INPUT_CONTROLLER_PRIOR) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 2 + xx, 5, 0, sheet);
} else if (status == INPUT_KEYBOARD_PRIOR) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 4 + xx, 5, 0, sheet);
} else if (status == INPUT_CONTROLLER_UNUSABLE) {
for (int xx = 0; xx < 2; ++xx)
screen.render(x + xx * 8, y, 6 + xx, 5, 0, sheet);
}
}

@Override
protected void tick() {
super.tick();
if (status == INPUT_CONTROLLER_PRIOR || status == INPUT_KEYBOARD_PRIOR) {
if (!input.isControllerInUse())
updateStatus(INPUT_CONTROLLER_UNUSABLE);
} else if (status == INPUT_CONTROLLER_UNUSABLE) {
if (input.isControllerInUse())
updateStatus(INPUT_CONTROLLER_PRIOR); // As controller was enabled.
}
}

@Override
public void updateStatus(int status) {
super.updateStatus();
this.status = status;
}
}
}


Expand Down
18 changes: 18 additions & 0 deletions src/client/java/minicraft/core/WatcherThreadController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package minicraft.core;

/**
* This provides an ability to control over a watcher thread to ensure content synchronization and thread safety.
*/
public interface WatcherThreadController {
/**
* Suspends the watcher thread from running.
* @throws IllegalStateException if the thread has already been suspended
*/
void suspend();

/**
* Resumes the watcher thread from suspended.
* @throws IllegalStateException if the thread has already been resumed or not suspended
*/
void resume();
}
Loading