Skip to content

Commit

Permalink
Use the correct default foreground/background
Browse files Browse the repository at this point in the history
Before we were always defaulting to black, which meant the text
was shown on a black background (and so was invisible).

Closes cc-tweaked/CC-Tweaked#126
  • Loading branch information
SquidDev committed Feb 23, 2019
1 parent 045130c commit 7959ab5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
Expand Up @@ -13,6 +13,19 @@
* @author apemanzilla
*/
public final class PaletteAdapter<C> {
/**
* The default color index for backgrounds
*
* @see dan200.computercraft.shared.util.Colour#Black
*/
public static final int DEFAULT_BACKGROUND = 0;

/**
* The default color index for foregrounds
*
* @see dan200.computercraft.shared.util.Colour#White
*/
public static final int DEFAULT_FOREGROUND = 15;

/**
* An adapter used to generate a color object from RGB values
Expand Down Expand Up @@ -55,9 +68,20 @@ public C getColor(double r, double g, double b) {
* @return The converted color
*/
public C getColor(int c) {
return getColor(c, DEFAULT_FOREGROUND);
}

/**
* Creates a color object using the given color from the palette
*
* @param c The numeric index of the terminal color
* @param def The default color index if none exists for {@code c}.
* @return The converted color
*/
public C getColor(int c, int def) {
double[] col;
if ((col = palette.getColour(15 - c)) == null) {
col = palette.getColour(0);
col = palette.getColour(def);
}
col = Utils.clampColor(col);
return getColor(col[0], col[1], col[2]);
Expand All @@ -73,6 +97,17 @@ public C getColor(char c) {
return getColor(Utils.base16ToInt(c));
}

/**
* Creates a color object using the given color from the palette
*
* @param c A hexadecimal terminal color
* @param def The default color index if none exists for {@code c}.
* @return The converted color
*/
public C getColor(char c, int def) {
return getColor(Utils.base16ToInt(c), def);
}

public PaletteAdapter(@Nonnull Palette palette, @Nonnull ColorAdapter<C> adapter) {
this.palette = palette;
this.adapter = adapter;
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/net/clgd/ccemux/rendering/awt/TerminalComponent.java
Expand Up @@ -27,21 +27,21 @@
class TerminalComponent extends Canvas {
private static final long serialVersionUID = -5043543826280613143L;

public static final ColorAdapter<Color> AWT_COLOR_ADAPTER = (r, g, b) -> new Color((float) r, (float) g, (float) b);
private static final ColorAdapter<Color> AWT_COLOR_ADAPTER = (r, g, b) -> new Color((float) r, (float) g, (float) b);

private final PaletteAdapter<Color> paletteCacher;

public final Terminal terminal;
public final int pixelWidth;
public final int pixelHeight;
public final int margin;
private final Terminal terminal;
private final int pixelWidth;
private final int pixelHeight;
final int margin;

public char cursorChar = '_';
private static final char cursorChar = '_';

public boolean blinkLocked = false;
boolean blinkLocked = false;

@Value
public static class CharImageRequest {
private static class CharImageRequest {
private char character;
private Color color;
}
Expand All @@ -67,11 +67,12 @@ public void resizeTerminal(int width, int height) {
}

private void drawChar(AWTTerminalFont font, Graphics g, char c, int x, int y, int color) {
if (c == '\0' || Character.isSpaceChar(c))
if (c == '\0' || Character.isSpaceChar(c)) {
return; // nothing to do here
}

Rectangle r = font.getCharCoords(c);
Color colour = paletteCacher.getColor(color);
Color colour = paletteCacher.getColor(color, PaletteAdapter.DEFAULT_FOREGROUND);

BufferedImage charImg = null;

Expand Down Expand Up @@ -104,7 +105,7 @@ private void drawChar(AWTTerminalFont font, Graphics g, char c, int x, int y, in
g.drawImage(charImg, x, y, pixelWidth, pixelHeight, null);
}

private void renderTerminal(AWTTerminalFont font, double dt) {
private void renderTerminal(AWTTerminalFont font) {
synchronized (terminal) {
Graphics g = getBufferStrategy().getDrawGraphics();

Expand All @@ -121,7 +122,7 @@ private void renderTerminal(AWTTerminalFont font, double dt) {
for (int x = 0; x < terminal.getWidth(); x++) {
int width = (x == 0 || x == terminal.getWidth() - 1) ? pixelWidth + margin : pixelWidth;

g.setColor(paletteCacher.getColor((bgLine == null) ? 'f' : bgLine.charAt(x)));
g.setColor(paletteCacher.getColor(bgLine == null ? 'f' : bgLine.charAt(x), PaletteAdapter.DEFAULT_BACKGROUND));
g.fillRect(dx, dy, width, height);

char character = (textLine == null) ? ' ' : textLine.charAt(x);
Expand Down Expand Up @@ -156,7 +157,7 @@ public void render(AWTTerminalFont font, double dt) {

do {
do {
renderTerminal(font, dt);
renderTerminal(font);
} while (getBufferStrategy().contentsRestored());

getBufferStrategy().show();
Expand Down
Expand Up @@ -171,11 +171,11 @@ private void redraw() {
width = cw + ((x == 0 || x == tw - 1) ? m : 0);

// draw background
g.setFill(paletteAdapter.getColor(bg.charAt(x)));
g.setFill(paletteAdapter.getColor(bg.charAt(x), PaletteAdapter.DEFAULT_BACKGROUND));
g.fillRect(ox, oy, width, height);

// draw character
charImg = font.getCharImage(text.charAt(x), paletteAdapter.getColor(fg.charAt(x)), fontScale);
charImg = font.getCharImage(text.charAt(x), paletteAdapter.getColor(fg.charAt(x), PaletteAdapter.DEFAULT_FOREGROUND), fontScale);
g.drawImage(charImg, ox + (x == 0 ? m : 0), oy + (y == 0 ? m : 0), cw, ch);

ox += width;
Expand All @@ -188,7 +188,7 @@ private void redraw() {
// draw cursor
if (cursorBlink()) {
g.drawImage(
font.getCharImage('_', paletteAdapter.getColor(computer.terminal.getTextColour()), fontScale),
font.getCharImage('_', paletteAdapter.getColor(computer.terminal.getTextColour(), PaletteAdapter.DEFAULT_FOREGROUND), fontScale),
m + (cw * computer.terminal.getCursorX()), m + (ch * computer.terminal.getCursorY()), cw, ch);
}

Expand Down

0 comments on commit 7959ab5

Please sign in to comment.