Commits

Anonymous committed 70d8ba7

putString does alignment now

Comments (0)

Files changed (2)

src/main/java/net/fishbulb/jcod/Console.java

     }
 
     private Cell cellAt(int x, int y) {
-        if (x < width && y < height)
+        if (x >= 0 && x < width && y >= 0 && y < height)
             return cells[y * width + x];
         else
             return null;
         c.c(ch).fg(fore).bg(back);
     }
 
+    // for compatibility
     public BlendFunction getBackgroundFlag() {
         return getBlendFunction();
     }
 
+    // for compatibility
     public void setBackgroundFlag(BlendFunction func) {
         setBlendFunction(func);
     }
     }
 
 
-    /** places a string as-is without consideration of alignment, newlines, or colors */
-    public void putString(int x, int y, String s, Color fg, Color bg, BlendFunction func) {
+    /** JCOD extension.  places a string as-is without consideration of alignment, newlines, or colors */
+    public void putString(int x, int y, String s, Alignment align, Color fg, Color bg, BlendFunction func) {
         int len = s.length();
+        switch (align) {
+            case Left: /* no change */ break;
+            case Right: x -= len - 1; break;
+            case Center: x -= len/2; break;
+        }
         for (int i = 0; i < len; i++) {
+            Cell cell = cellAt(x++, y);
+            if (cell == null) continue;   // went off the edge
+
             char c = s.charAt(i);
-            Cell cell = cellAt(x++, y);
-            if (cell == null) return;   // went off the edge, no need to continue
-
             cell.c(c).fg(fg);
 
             if (func == null) {
         }
     }
 
-    public void putString(int x, int y, String s, Color fg, Color bg) {
-        putString(x, y, s, fg, bg, blendFunction);
+    public void putString(int x, int y, String s, Alignment align, Color fg, Color bg) {
+        putString(x, y, s, align, fg, bg, blendFunction);
+    }
+
+
+    public void putString(int x, int y, String s, Alignment align) {
+        putString(x, y, s, align, defaultForeground, defaultBackground, blendFunction);
     }
 
     protected void putString(int x, int y, String s) {
-        putString(x, y, s, defaultForeground, defaultBackground, blendFunction);
+        putString(x, y, s, alignment, defaultForeground, defaultBackground, blendFunction);
     }
 
 

src/main/java/net/fishbulb/jcod/demo/Demo.java

 
     private void renderConsole() {
         // tweak colors
+//        float delta = 0.01f;
+        float delta = 0.001f;
         for (int c = 0; c < 4; c++) {
             // move each corner color
             int component = rng.nextInt(3);
             switch (component) {
                 case 0:
-                    cols[c].r += 0.01f * dirr[c];
+                    cols[c].r += delta * dirr[c];
                     if (cols[c].r >= 1) dirr[c] = -1;
                     else if (cols[c].r <= 0) dirr[c] = 1;
                     break;
                 case 1:
-                    cols[c].g += 0.01f * dirg[c];
+                    cols[c].g += delta * dirg[c];
                     if (cols[c].g >= 1) dirg[c] = -1;
                     else if (cols[c].g <= 0) dirg[c] = 1;
                     break;
                 case 2:
-                    cols[c].b += 0.01f * dirb[c];
+                    cols[c].b += delta * dirb[c];
                     if (cols[c].b >= 1) dirb[c] = -1;
                     else if (cols[c].b <= 0) dirb[c] = 1;
                     break;
             }
         }
 
-        sampleConsole.putString(10, 10, "Hello World", Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+
+        // testing alignments
+        sampleConsole.putString(10, 3, "0123456789", Console.Alignment.Right, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+        sampleConsole.putString(10, 4, "0123456789", Console.Alignment.Center, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+        sampleConsole.putString(10, 5, "0123456789", Console.Alignment.Left, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+
+        sampleConsole.putString(20, 10, "0123456789", Console.Alignment.Right, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+        sampleConsole.putString(20, 11, "----------|----------", Console.Alignment.Center, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+        sampleConsole.putChar(20, 12, 'X');
+        sampleConsole.putString(20, 13, "----------|----------", Console.Alignment.Center, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+        sampleConsole.putString(20, 14, "0123456789", Console.Alignment.Left, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+
+
+        sampleConsole.putString(23, 17, "-=- Hello World -=-", Console.Alignment.Right, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+        sampleConsole.putString(23, 18, "-=- Hello World -=-", Console.Alignment.Center, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
+        sampleConsole.putString(23, 19, "-=- Hello World -=-", Console.Alignment.Left, Color.YELLOW, matte, BlendMode.AddWithSourceAlpha);
 
         sampleConsole.flush();
     }