Updated readability of the code.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / textEditorComponent / TextEditComponent.java
index ec54b33..0cd379c 100755 (executable)
@@ -1,20 +1,15 @@
 /*
- * Sixth 3D engine. Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- *
+ * 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.geometry.Point2D;
-import eu.svjatoslav.sixth.e3d.geometry.Transform;
 import eu.svjatoslav.sixth.e3d.gui.GuiComponent;
 import eu.svjatoslav.sixth.e3d.gui.TextPointer;
-import eu.svjatoslav.sixth.e3d.gui.ViewContext;
-import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
+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.shapes.composite.textcanvas.TextCanvas;
 
 import java.awt.*;
@@ -27,31 +22,47 @@ import java.util.Set;
 public class TextEditComponent extends GuiComponent implements ClipboardOwner {
 
     private static final long serialVersionUID = -7118833957783600630L;
-    // lines that need to be repainted
+
+    /**
+     * Text rows that need to be repainted.
+     */
     private final Set<Integer> dirtyRows = new HashSet<>();
+
+
     private final TextCanvas textCanvas;
     public int scrolledCharacters = 0, scrolledLines = 0;
     public boolean selecting = false;
+
+    /**
+     * Selection start and end pointers.
+     */
     public TextPointer selectionStart = new TextPointer(0, 0);
     public TextPointer selectionEnd = new TextPointer(0, 0);
-    public TextPointer cursorLocation;
+
+
+    public TextPointer cursorLocation = new TextPointer(0, 0);
     Page page = new Page();
-    ColorConfig colorConfig = new ColorConfig();
+    LookAndFeel lookAndFeel;
+
+    /**
+     * If true, the page will be repainted on the next update.
+     */
     boolean repaintPage = false;
 
     public TextEditComponent(final Transform transform,
-                             final ViewContext viewContext, final Point2D size) {
-        super(transform, viewContext, size.to3D());
-
-        cursorLocation = new TextPointer(0, 0);
-
-        // initialize visual panel
+                             final ViewPanel viewPanel,
+                             final Point2D sizeInWorldCoordinates,
+                             LookAndFeel lookAndFeel) {
+        super(transform, viewPanel, sizeInWorldCoordinates.to3D());
 
-        final int columns = (int) (size.x / TextCanvas.FONT_CHAR_WIDTH);
-        final int rows = (int) (size.y / TextCanvas.FONT_CHAR_HEIGHT);
+        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);
 
@@ -249,8 +260,8 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner {
      * Parse key presses.
      */
     @Override
-    public void keyPressed(final KeyEvent event, final ViewContext viewContext) {
-        super.keyPressed(event, viewContext);
+    public boolean keyPressed(final KeyEvent event, final ViewPanel viewPanel) {
+        super.keyPressed(event, viewPanel);
 
         processKeyEvent(event);
 
@@ -259,6 +270,7 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner {
         checkCursorBoundaries();
 
         repaintWhatNeeded();
+        return true;
     }
 
     /**
@@ -360,7 +372,6 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner {
                 }
 
             cursorLocation.column = 0;
-            return;
         }
     }
 
@@ -398,17 +409,16 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner {
     }
 
     private void processKeyEvent(final KeyEvent event) {
-        final int modifiers = event.getModifiers();
-
+        final int modifiers = event.getModifiersEx();
         final int keyCode = event.getKeyCode();
         final char keyChar = event.getKeyChar();
 
         // System.out.println("Keycode:" + keyCode s+ ", keychar:" + keyChar);
 
-        if (KeyboardHelper.isAlt(modifiers))
+        if (KeyboardHelper.isAltPressed(modifiers))
             return;
 
-        if (KeyboardHelper.isCtrl(modifiers)) {
+        if (KeyboardHelper.isCtrlPressed(modifiers)) {
             processCtrlCombinations(keyCode);
             return;
         }
@@ -425,10 +435,7 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner {
             return;
         }
 
-        // System.out.println("Co:" + String.valueOf(code) + "  Ch:" +
-        // String.valueOf(keyChar));
-
-        if (KeyboardHelper.isShift(modifiers)) {
+        if (KeyboardHelper.isShiftPressed(modifiers)) {
             if (!selecting)
                 attemptSelectionStart:{
 
@@ -439,9 +446,6 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner {
                             | (keyChar == 8) | (keyChar == 9))
                         break attemptSelectionStart;
 
-                    // System.out.println("Selection started:" + keyChar + " "
-                    // + keyCode);
-
                     selectionStart = new TextPointer(cursorLocation);
                     selectionEnd = selectionStart;
                     selecting = true;
@@ -491,20 +495,19 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner {
         if (keyCode == KeyboardHelper.PGUP) {
             cursorLocation.row -= textCanvas.getSize().row;
             repaintPage = true;
-            return;
         }
 
     }
 
     private void processTab(final int modifiers) {
-        if (KeyboardHelper.isShift(modifiers)) {
+        if (KeyboardHelper.isShiftPressed(modifiers)) {
             if (selectionStart.compareTo(selectionEnd) != 0) {
                 // dedent multiple lines
                 ensureSelectionOrder();
 
                 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);
 
@@ -544,44 +547,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();
     }
 
@@ -596,13 +599,6 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner {
         dirtyRows.clear();
     }
 
-    // public void setCaret(final int x, final int y) {
-    // selecting = false;
-    // cursorLocation.column = (x / characterWidth) + scrolledCharacters;
-    // cursorLocation.row = (y / characterHeight) + scrolledLines;
-    // repaintPage();
-    // }
-
     /**
      * Scroll full page to given amount of lines or charancters.
      */