package com.googlecode.lanterna.gui2;
import com.googlecode.lanterna.*;
import com.googlecode.lanterna.graphics.ThemeDefinition;
import com.googlecode.lanterna.graphics.ThemedTextGraphics;
public class WindowShadowRenderer implements WindowPostRenderer {
@Override
public void postRender(
ThemedTextGraphics textGraphics,
TextGUI textGUI,
Window window) {
TerminalPosition windowPosition = window.getPosition();
TerminalSize decoratedWindowSize = window.getDecoratedSize();
ThemeDefinition themeDefinition = window.getTheme().getDefinition(WindowShadowRenderer.class);
textGraphics.applyThemeStyle(themeDefinition.getNormal());
char filler = themeDefinition.getCharacter("FILLER", ' ');
boolean useDoubleWidth = themeDefinition.getBooleanProperty("DOUBLE_WIDTH", true);
boolean useTransparency = themeDefinition.getBooleanProperty("TRANSPARENT", false);
TerminalPosition lowerLeft = windowPosition.withRelativeColumn(useDoubleWidth ? 2 : 1).withRelativeRow(decoratedWindowSize.getRows());
TerminalPosition lowerRight = lowerLeft.withRelativeColumn(decoratedWindowSize.getColumns() - (useDoubleWidth ? 3 : 2));
for(int column = lowerLeft.getColumn(); column <= lowerRight.getColumn() + 1; column++) {
char characterToDraw = filler;
if(useTransparency) {
TextCharacter tc = textGraphics.getCharacter(column, lowerLeft.getRow());
if (tc != null) {
characterToDraw = tc.getCharacter();
}
}
textGraphics.setCharacter(column, lowerLeft.getRow(), characterToDraw);
if (TerminalTextUtils.isCharDoubleWidth(characterToDraw)) {
column++;
}
}
lowerRight = lowerRight.withRelativeColumn(1);
TerminalPosition upperRight = lowerRight.withRelativeRow(-decoratedWindowSize.getRows() + 1);
boolean hasDoubleWidthShadow = false;
for(int row = upperRight.getRow(); row < lowerRight.getRow(); row++) {
char characterToDraw = filler;
if(useTransparency) {
TextCharacter tc = textGraphics.getCharacter(upperRight.getColumn(), row);
if (tc != null) {
characterToDraw = tc.getCharacter();
}
}
textGraphics.setCharacter(upperRight.getColumn(), row, characterToDraw);
if (TerminalTextUtils.isCharDoubleWidth(characterToDraw)) {
hasDoubleWidthShadow = true;
}
}
textGraphics.applyThemeStyle(themeDefinition.getNormal());
if(useDoubleWidth || hasDoubleWidthShadow) {
upperRight = upperRight.withRelativeColumn(1);
for(int row = upperRight.getRow(); row <= lowerRight.getRow(); row++) {
char characterToDraw = filler;
if(useTransparency) {
TextCharacter tc = textGraphics.getCharacter(upperRight.getColumn(), row);
if (tc != null && !tc.isDoubleWidth()) {
characterToDraw = tc.getCharacter();
}
}
TextCharacter neighbour = textGraphics.getCharacter(upperRight.getColumn() - 1, row);
if (neighbour != null && !neighbour.isDoubleWidth()) {
textGraphics.setCharacter(upperRight.getColumn(), row, characterToDraw);
}
}
}
}
}