feat: Handle small terminals
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
package cz.jzitnik.events;
|
||||||
|
|
||||||
|
import cz.jzitnik.utils.events.Event;
|
||||||
|
|
||||||
|
public class TerminalTooSmallEvent implements Event {
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import cz.jzitnik.annotations.injectors.InjectDependency;
|
|||||||
import cz.jzitnik.annotations.injectors.InjectState;
|
import cz.jzitnik.annotations.injectors.InjectState;
|
||||||
import cz.jzitnik.events.FullRoomDraw;
|
import cz.jzitnik.events.FullRoomDraw;
|
||||||
import cz.jzitnik.events.RerenderScreen;
|
import cz.jzitnik.events.RerenderScreen;
|
||||||
|
import cz.jzitnik.events.TerminalTooSmallEvent;
|
||||||
import cz.jzitnik.game.GameRoom;
|
import cz.jzitnik.game.GameRoom;
|
||||||
import cz.jzitnik.game.GameState;
|
import cz.jzitnik.game.GameState;
|
||||||
import cz.jzitnik.game.Player;
|
import cz.jzitnik.game.Player;
|
||||||
@@ -62,6 +63,7 @@ public class FullRoomDrawHandler extends AbstractEventHandler<FullRoomDraw> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(FullRoomDraw event) {
|
public void handle(FullRoomDraw event) {
|
||||||
|
try {
|
||||||
log.debug("Rendering full room");
|
log.debug("Rendering full room");
|
||||||
TerminalScreen terminalScreen = terminalState.getTerminalScreen();
|
TerminalScreen terminalScreen = terminalState.getTerminalScreen();
|
||||||
List<RerenderScreen.ScreenPart> partsToRerender = new ArrayList<>();
|
List<RerenderScreen.ScreenPart> partsToRerender = new ArrayList<>();
|
||||||
@@ -113,5 +115,9 @@ public class FullRoomDrawHandler extends AbstractEventHandler<FullRoomDraw> {
|
|||||||
} else {
|
} else {
|
||||||
eventManager.emitEvent(new RerenderScreen(partsToRerender.toArray(RerenderScreen.ScreenPart[]::new)));
|
eventManager.emitEvent(new RerenderScreen(partsToRerender.toArray(RerenderScreen.ScreenPart[]::new)));
|
||||||
}
|
}
|
||||||
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
|
// Screen too small to fit the room
|
||||||
|
eventManager.emitEvent(new TerminalTooSmallEvent());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package cz.jzitnik.events.handlers;
|
||||||
|
|
||||||
|
import com.googlecode.lanterna.SGR;
|
||||||
|
import com.googlecode.lanterna.TerminalSize;
|
||||||
|
import com.googlecode.lanterna.TextCharacter;
|
||||||
|
import com.googlecode.lanterna.TextColor;
|
||||||
|
import com.googlecode.lanterna.screen.Screen;
|
||||||
|
import com.googlecode.lanterna.screen.TerminalScreen;
|
||||||
|
import cz.jzitnik.annotations.EventHandler;
|
||||||
|
import cz.jzitnik.annotations.injectors.InjectState;
|
||||||
|
import cz.jzitnik.events.TerminalTooSmallEvent;
|
||||||
|
import cz.jzitnik.states.TerminalState;
|
||||||
|
import cz.jzitnik.utils.DependencyManager;
|
||||||
|
import cz.jzitnik.utils.events.AbstractEventHandler;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@EventHandler(TerminalTooSmallEvent.class)
|
||||||
|
public class TerminalTooSmallEventHandler extends AbstractEventHandler<TerminalTooSmallEvent> {
|
||||||
|
public TerminalTooSmallEventHandler(DependencyManager dm) {
|
||||||
|
super(dm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@InjectState
|
||||||
|
private TerminalState terminalState;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(TerminalTooSmallEvent event) {
|
||||||
|
// Directly render message for the user
|
||||||
|
TerminalScreen terminalScreen = terminalState.getTerminalScreen();
|
||||||
|
terminalScreen.clear();
|
||||||
|
|
||||||
|
TerminalSize terminalSize = terminalScreen.getTerminalSize();
|
||||||
|
String text = "Terminal too small!";
|
||||||
|
String subText = "Please resize your terminal";
|
||||||
|
|
||||||
|
int startY = terminalSize.getRows() / 2;
|
||||||
|
|
||||||
|
int textStartX = (terminalSize.getColumns() / 2) - (text.length() / 2);
|
||||||
|
for (char character : text.toCharArray()) {
|
||||||
|
terminalScreen.setCharacter(textStartX++, startY, new TextCharacter(character, TextColor.ANSI.RED, TextColor.ANSI.DEFAULT, SGR.BOLD));
|
||||||
|
}
|
||||||
|
int subTextStartX = (terminalSize.getColumns() / 2) - (subText.length() / 2);
|
||||||
|
for (char character : subText.toCharArray()) {
|
||||||
|
terminalScreen.setCharacter(subTextStartX++, startY + 1, new TextCharacter(character, TextColor.ANSI.BLACK_BRIGHT, TextColor.ANSI.DEFAULT));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
terminalScreen.refresh(Screen.RefreshType.COMPLETE);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user