Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / jexer / bits / Cell.java
index d4c816f41ee88e202bf9e509af30b47630a97482..ed3c202a005a8c8a2a4187d08478cf8d62a6191c 100644 (file)
@@ -40,6 +40,26 @@ public final class Cell extends CellAttributes {
     // Constants --------------------------------------------------------------
     // ------------------------------------------------------------------------
 
+    /**
+     * How this cell needs to be displayed if it is part of a larger glyph.
+     */
+    public enum Width {
+        /**
+         * This cell is an entire glyph on its own.
+         */
+        SINGLE,
+
+        /**
+         * This cell is the left half of a wide glyph.
+         */
+        LEFT,
+
+        /**
+         * This cell is the right half of a wide glyph.
+         */
+        RIGHT,
+    }
+
     /**
      * The special "this cell is unset" (null) value.  This is the Unicode
      * "not a character" value.
@@ -53,7 +73,12 @@ public final class Cell extends CellAttributes {
     /**
      * The character at this cell.
      */
-    private char ch;
+    private int ch = ' ';
+
+    /**
+     * The display width of this cell.
+     */
+    private Width width = Width.SINGLE;
 
     /**
      * The image at this cell.
@@ -69,7 +94,7 @@ public final class Cell extends CellAttributes {
      * The background color used for the area the image portion might not
      * cover.
      */
-    private Color background = null;
+    private Color background = Color.BLACK;
 
     /**
      * hashCode() needs to call image.hashCode(), which can get quite
@@ -94,7 +119,7 @@ public final class Cell extends CellAttributes {
      * @see #reset()
      */
     public Cell() {
-        reset();
+        // NOP
     }
 
     /**
@@ -104,16 +129,43 @@ public final class Cell extends CellAttributes {
      * @param ch character to set to
      * @see #reset()
      */
-    public Cell(final char ch) {
-        reset();
+    public Cell(final int ch) {
         this.ch = ch;
     }
 
+    /**
+     * Public constructor sets the attributes.
+     *
+     * @param attr attributes to use
+     */
+    public Cell(final CellAttributes attr) {
+        super(attr);
+    }
+
+    /**
+     * Public constructor sets the character and attributes.
+     *
+     * @param ch character to set to
+     * @param attr attributes to use
+     */
+    public Cell(final int ch, final CellAttributes attr) {
+        super(attr);
+        this.ch = ch;
+    }
+
+    /**
+     * Public constructor creates a duplicate.
+     *
+     * @param cell the instance to copy
+     */
+    public Cell(final Cell cell) {
+        setTo(cell);
+    }
+
     // ------------------------------------------------------------------------
     // Cell -------------------------------------------------------------------
     // ------------------------------------------------------------------------
 
-
     /**
      * Set the image data for this cell.
      *
@@ -122,6 +174,7 @@ public final class Cell extends CellAttributes {
     public void setImage(final BufferedImage image) {
         this.image = image;
         imageHashCode = image.hashCode();
+        width = Width.SINGLE;
     }
 
     /**
@@ -210,7 +263,7 @@ public final class Cell extends CellAttributes {
      *
      * @return cell character
      */
-    public char getChar() {
+    public int getChar() {
         return ch;
     }
 
@@ -219,10 +272,29 @@ public final class Cell extends CellAttributes {
      *
      * @param ch new cell character
      */
-    public void setChar(final char ch) {
+    public void setChar(final int ch) {
         this.ch = ch;
     }
 
+    /**
+     * Getter for cell width.
+     *
+     * @return Width.SINGLE, Width.LEFT, or Width.RIGHT
+     */
+    public Width getWidth() {
+        return width;
+    }
+
+    /**
+     * Setter for cell width.
+     *
+     * @param width new cell width, one of Width.SINGLE, Width.LEFT, or
+     * Width.RIGHT
+     */
+    public void setWidth(final Width width) {
+        this.width = width;
+    }
+
     /**
      * Reset this cell to a blank.
      */
@@ -230,6 +302,7 @@ public final class Cell extends CellAttributes {
     public void reset() {
         super.reset();
         ch = ' ';
+        width = Width.SINGLE;
         image = null;
         imageHashCode = 0;
         invertedImage = null;
@@ -244,6 +317,7 @@ public final class Cell extends CellAttributes {
     public void unset() {
         super.reset();
         ch = UNSET_VALUE;
+        width = Width.SINGLE;
         image = null;
         imageHashCode = 0;
         invertedImage = null;
@@ -271,6 +345,7 @@ public final class Cell extends CellAttributes {
             && !isProtect()
             && !isRGB()
             && !isImage()
+            && (width == Width.SINGLE)
             && (ch == ' ')
         ) {
             return true;
@@ -327,7 +402,7 @@ public final class Cell extends CellAttributes {
         }
 
         // Normal case: character and attributes must match.
-        if (ch == that.ch) {
+        if ((ch == that.ch) && (width == that.width)) {
             return super.equals(rhs);
         }
         return false;
@@ -344,7 +419,8 @@ public final class Cell extends CellAttributes {
         int B = 23;
         int hash = A;
         hash = (B * hash) + super.hashCode();
-        hash = (B * hash) + (int)ch;
+        hash = (B * hash) + ch;
+        hash = (B * hash) + width.hashCode();
         if (image != null) {
             /*
             hash = (B * hash) + image.hashCode();
@@ -371,11 +447,13 @@ public final class Cell extends CellAttributes {
         this.image = null;
         this.imageHashCode = 0;
         this.backgroundHashCode = 0;
+        this.width = Width.SINGLE;
         super.setTo(thatAttr);
 
         if (rhs instanceof Cell) {
             Cell that = (Cell) rhs;
             this.ch = that.ch;
+            this.width = that.width;
             this.image = that.image;
             this.invertedImage = that.invertedImage;
             this.background = that.background;