From: Svjatoslav Agejenko Date: Sat, 29 May 2021 00:23:00 +0000 (+0300) Subject: Refactoring. Ability to specify look and feel for text editor. X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=d571801d69962883e194a306ee2b7110d6adc270;p=sixth-3d.git Refactoring. Ability to specify look and feel for text editor. --- diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/ColorConfig.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/ColorConfig.java deleted file mode 100644 index 7bf7f1b..0000000 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/ColorConfig.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Sixth 3D engine. Author: Svjatoslav Agejenko. - * This project is released under Creative Commons Zero (CC0) license. - */ -package eu.svjatoslav.sixth.e3d.gui.textEditorComponent; - -import eu.svjatoslav.sixth.e3d.renderer.raster.Color; - -public class ColorConfig { - - public Color normalText = new Color(255, 255, 255); - public Color normalBack = new Color(20, 20, 20, 255); - - public Color tabulatorBack = new Color(25, 25, 25, 255); - - public Color cursorText = new Color(255, 255, 255); - public Color cursorBack = new Color(255, 0, 0); - - public Color selectedText = new Color(255, 255, 255); - public Color selectedBack = new Color(0, 80, 80); - - public Color pageEnd = new Color(70, 70, 0); - -} diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/LookAndFeel.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/LookAndFeel.java new file mode 100644 index 0000000..a7bff24 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/LookAndFeel.java @@ -0,0 +1,22 @@ +/* + * Sixth 3D engine. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. + */ +package eu.svjatoslav.sixth.e3d.gui.textEditorComponent; + +import eu.svjatoslav.sixth.e3d.renderer.raster.Color; + +public class LookAndFeel { + + public Color foreground = new Color(255, 255, 255); + public Color background = new Color(20, 20, 20, 255); + + public Color tabStopBackground = new Color(25, 25, 25, 255); + + public Color cursorForeground = new Color(255, 255, 255); + public Color cursorBackground = new Color(255, 0, 0); + + public Color selectionForeground = new Color(255, 255, 255); + public Color selectionBackground = new Color(0, 80, 80); + +} diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/Page.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/Page.java index 4b251c0..c52fbd7 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/Page.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/Page.java @@ -9,40 +9,40 @@ import java.util.List; public class Page { - public List lines = new ArrayList<>(); + public List rows = new ArrayList<>(); public void ensureMaxTextLine(final int row) { - while (lines.size() <= row) - lines.add(new TextLine()); + while (rows.size() <= row) + rows.add(new TextLine()); } public char getChar(final int row, final int column) { - if (lines.size() <= row) + if (rows.size() <= row) return ' '; - return lines.get(row).getCharForLocation(column); + return rows.get(row).getCharForLocation(column); } public TextLine getLine(final int row) { ensureMaxTextLine(row); - return lines.get(row); + return rows.get(row); } public int getLineLength(final int row) { - if (lines.size() <= row) + if (rows.size() <= row) return 0; - return lines.get(row).getLength(); + return rows.get(row).getLength(); } public int getLinesCount() { pack(); - return lines.size(); + return rows.size(); } public String getText() { pack(); final StringBuilder result = new StringBuilder(); - for (final TextLine textLine : lines) { + for (final TextLine textLine : rows) { if (result.length() > 0) result.append("\n"); result.append(textLine.toString()); @@ -55,34 +55,34 @@ public class Page { } public void insertLine(final int row, final TextLine textLine) { - lines.add(row, textLine); + rows.add(row, textLine); } private void pack() { int newLength = 0; - for (int i = lines.size() - 1; i >= 0; i--) - if (!lines.get(i).isEmpty()) { + for (int i = rows.size() - 1; i >= 0; i--) + if (!rows.get(i).isEmpty()) { newLength = i + 1; break; } - if (newLength == lines.size()) + if (newLength == rows.size()) return; - lines = lines.subList(0, newLength); + rows = rows.subList(0, newLength); } public void removeCharacter(final int row, final int col) { - if (lines.size() <= row) + if (rows.size() <= row) return; getLine(row).removeCharacter(col); } public void removeLine(final int row) { - if (lines.size() <= row) + if (rows.size() <= row) return; - lines.remove(row); + rows.remove(row); } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java index 9ee9109..93c0535 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java @@ -10,7 +10,6 @@ import eu.svjatoslav.sixth.e3d.gui.TextPointer; import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.gui.humaninput.KeyboardHelper; import eu.svjatoslav.sixth.e3d.math.Transform; -import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas; import java.awt.*; @@ -36,20 +35,23 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner { public TextPointer selectionEnd = new TextPointer(0, 0); public TextPointer cursorLocation = new TextPointer(0, 0); Page page = new Page(); - ColorConfig colorConfig = new ColorConfig(); + LookAndFeel lookAndFeel; boolean repaintPage = false; public TextEditComponent(final Transform transform, - final ViewPanel viewPanel, final Point2D sizeInWorldCoordinates) { + final ViewPanel viewPanel, + final Point2D sizeInWorldCoordinates, + LookAndFeel lookAndFeel) { super(transform, viewPanel, sizeInWorldCoordinates.to3D()); - // initialize visual panel - + this.lookAndFeel = lookAndFeel; final int columns = (int) (sizeInWorldCoordinates.x / TextCanvas.FONT_CHAR_WIDTH); final int rows = (int) (sizeInWorldCoordinates.y / TextCanvas.FONT_CHAR_HEIGHT); - textCanvas = new TextCanvas(new Transform(), new TextPointer(rows, - columns), Color.WHITE, colorConfig.normalBack); + textCanvas = new TextCanvas( + new Transform(), + new TextPointer(rows, columns), + lookAndFeel.foreground, lookAndFeel.background); textCanvas.setMouseInteractionController(this); @@ -495,7 +497,7 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner { identSelection: { - // check that identation is possible + // check that indentation is possible for (int y = selectionStart.row; y < selectionEnd.row; y++) { final TextLine textLine = page.getLine(y); @@ -535,44 +537,44 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner { public void repaintPage() { - final int chXe = textCanvas.getSize().column + 2; - final int chYe = textCanvas.getSize().row + 2; + final int columnCount = textCanvas.getSize().column + 2; + final int rowCount = textCanvas.getSize().row + 2; - for (int cy = 0; cy < chYe; cy++) - for (int cx = 0; cx < chXe; cx++) { - final boolean isTabMargin = ((cx + scrolledCharacters) % 4) == 0; + for (int row = 0; row < rowCount; row++) + for (int column = 0; column < columnCount; column++) { + final boolean isTabMargin = ((column + scrolledCharacters) % 4) == 0; - if ((cx == (cursorLocation.column - scrolledCharacters)) - & (cy == (cursorLocation.row - scrolledLines))) { + if ((column == (cursorLocation.column - scrolledCharacters)) + & (row == (cursorLocation.row - scrolledLines))) { // cursor - textCanvas.setBackgroundColor(colorConfig.cursorBack); - textCanvas.setForegroundColor(colorConfig.cursorText); - } else if (new TextPointer(cy + scrolledLines, cx).isBetween( + textCanvas.setBackgroundColor(lookAndFeel.cursorBackground); + textCanvas.setForegroundColor(lookAndFeel.cursorForeground); + } else if (new TextPointer(row + scrolledLines, column).isBetween( selectionStart, selectionEnd)) { // selected text - textCanvas.setBackgroundColor(colorConfig.selectedBack); - textCanvas.setForegroundColor(colorConfig.selectedText); + textCanvas.setBackgroundColor(lookAndFeel.selectionBackground); + textCanvas.setForegroundColor(lookAndFeel.selectionForeground); } else { // normal text - textCanvas.setBackgroundColor(colorConfig.normalBack); - textCanvas.setForegroundColor(colorConfig.normalText); + textCanvas.setBackgroundColor(lookAndFeel.background); + textCanvas.setForegroundColor(lookAndFeel.foreground); if (isTabMargin) textCanvas - .setBackgroundColor(colorConfig.tabulatorBack); + .setBackgroundColor(lookAndFeel.tabStopBackground); } - final char charUnderCursor = page.getChar(cy + scrolledLines, - cx + scrolledCharacters); + final char charUnderCursor = page.getChar(row + scrolledLines, + column + scrolledCharacters); - textCanvas.putChar(cy, cx, charUnderCursor); + textCanvas.putChar(row, column, charUnderCursor); } } public void repaintRow(final int rowNumber) { - // TODO: fix this + // TODO: Optimize this. No need to repaint entire page. repaintPage(); }