|
| 1 | +package net.fexcraft.app.fmt.ui.editor; |
| 2 | + |
| 3 | +import org.newdawn.slick.Color; |
| 4 | + |
| 5 | +import net.fexcraft.app.fmt.ui.Element; |
| 6 | +import net.fexcraft.app.fmt.ui.generic.Button; |
| 7 | +import net.fexcraft.app.fmt.ui.generic.TextField; |
| 8 | +import net.fexcraft.app.fmt.utils.TextureManager; |
| 9 | +import net.fexcraft.lib.common.math.RGB; |
| 10 | + |
| 11 | +public class TextureEditor extends Editor { |
| 12 | + |
| 13 | + public static RGB CURRENTCOLOR = new RGB(RGB.WHITE); |
| 14 | + private static final int rows = 9, colls = 32; |
| 15 | + private static RGB[] pallete = new RGB[rows * rows]; |
| 16 | + private static RGB[] hopall = new RGB[36]; |
| 17 | + public static boolean BUCKETMODE; |
| 18 | + private static RGB buttonhover; |
| 19 | + |
| 20 | + public TextureEditor(){ |
| 21 | + super("texture_editor"); |
| 22 | + TextureManager.loadTexture("ui/pbwhite"); |
| 23 | + final RGB rgb = new RGB(127, 127, 255); |
| 24 | + // |
| 25 | + for(int i = 0; i < 3; i++){ |
| 26 | + final int j = i; |
| 27 | + this.elements.put("rgb" + i + "-", new Button(this, "rgb" + i + "-", 12, 26, 4 + (98 * i), 30, rgb){ |
| 28 | + @Override protected boolean processButtonClick(int x, int y, boolean left){ return updateRGB(false, j); } |
| 29 | + }.setText(" < ", true).setTexture("ui/background").setLevel(-1)); |
| 30 | + this.elements.put("rgb" + i, new TextField(this, "rgb" + i, 70, 16 + (98 * i), 30){ |
| 31 | + @Override public void updateNumberField(){ updateRGB(null, j); } |
| 32 | + @Override protected boolean processScrollWheel(int wheel){ return updateRGB(wheel > 0, j); } |
| 33 | + }.setAsNumberfield(0, 255, true).setLevel(-1)); |
| 34 | + this.elements.put("rgb" + i + "+", new Button(this, "rgb" + i + "+", 12, 26, 86 + (98 * i), 30, rgb){ |
| 35 | + @Override protected boolean processButtonClick(int x, int y, boolean left){ return updateRGB(true, j); } |
| 36 | + }.setText(" > ", true).setTexture("ui/background").setLevel(-1)); |
| 37 | + } |
| 38 | + // |
| 39 | + this.elements.put("large_color_palette", new LargePallette(this, 4, 80)); |
| 40 | + this.elements.put("horizontal_color_palette", new HorizontalPallette(this, 4, 410)); |
| 41 | + this.elements.put("button", new Button(this, "button", 294, 28, 4, 460, buttonhover = new RGB(CURRENTCOLOR)){ |
| 42 | + @Override |
| 43 | + protected boolean processButtonClick(int x, int y, boolean left){ |
| 44 | + toggleBucketMode(); return true; |
| 45 | + } |
| 46 | + }.setText("Paint Bucket [OFF]", true).setTexture("ui/pbwhite")); |
| 47 | + // |
| 48 | + this.updateFields(); |
| 49 | + } |
| 50 | + |
| 51 | + public static void toggleBucketMode(){ |
| 52 | + BUCKETMODE = !BUCKETMODE; Editor.get("texture_editor").getButton("button").setText("Paint Bucket [" + (BUCKETMODE ? "ON" : "OFF") + "]", true); |
| 53 | + } |
| 54 | + |
| 55 | + private void updateFields(){ |
| 56 | + byte[] arr = CURRENTCOLOR.toByteArray(); |
| 57 | + this.getField("rgb0").applyChange(arr[0] + 128); |
| 58 | + this.getField("rgb1").applyChange(arr[1] + 128); |
| 59 | + this.getField("rgb2").applyChange(arr[2] + 128); |
| 60 | + // |
| 61 | + for(int x = 0; x < rows; x++){ |
| 62 | + for(int z = 0; z < rows; z++){ |
| 63 | + int y = x * rows + z; |
| 64 | + float e = (1f / (rows * rows)) * y, f = (1f / rows) * z, h = (255 / rows) * x; |
| 65 | + int r = (int)Math.abs((e * (arr[0] + 128)) + ((1 - f) * h)); |
| 66 | + int g = (int)Math.abs((e * (arr[1] + 128)) + ((1 - f) * h)); |
| 67 | + int l = (int)Math.abs((e * (arr[2] + 128)) + ((1 - f) * h)); |
| 68 | + pallete[x + (z * rows)] = new RGB(r, g, l); |
| 69 | + } |
| 70 | + } |
| 71 | + // |
| 72 | + buttonhover.packed = CURRENTCOLOR.packed; |
| 73 | + } |
| 74 | + |
| 75 | + protected boolean updateRGB(Boolean apply, int j){ |
| 76 | + TextField field = (TextField)getElement("rgb" + j); |
| 77 | + if(apply != null) field.applyChange(field.tryChange(apply, 8)); |
| 78 | + if(CURRENTCOLOR == null) CURRENTCOLOR = new RGB(RGB.WHITE); |
| 79 | + byte[] arr = CURRENTCOLOR.toByteArray(); |
| 80 | + byte colorr = (byte)(field.getIntegerValue() - 128); |
| 81 | + switch(j){ |
| 82 | + case 0: CURRENTCOLOR = new RGB(colorr, arr[1], arr[2]); break; |
| 83 | + case 1: CURRENTCOLOR = new RGB(arr[0], colorr, arr[2]); break; |
| 84 | + case 2: CURRENTCOLOR = new RGB(arr[0], arr[1], colorr); break; |
| 85 | + } this.updateFields(); return true; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void renderSelf(int rw, int rh){ |
| 90 | + super.renderSelf(rw, rh); TextureManager.unbind(); |
| 91 | + font.drawString(4, 40, "Manual RGB Input", Color.black); |
| 92 | + font.drawString(4, 90, "Large Palette", Color.black); |
| 93 | + font.drawString(4, 418, "Horizontal Palette", Color.black); |
| 94 | + RGB.glColorReset(); |
| 95 | + } |
| 96 | + |
| 97 | + public static class LargePallette extends Element { |
| 98 | + |
| 99 | + public LargePallette(Element parent, int x, int y){ |
| 100 | + super(parent, "large_color_palette"); this.height = width = 294; |
| 101 | + this.x = parent.x + x; this.y = parent.y + y; z = -1; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void renderSelf(int rw, int rh){ |
| 106 | + super.renderQuad(x, y, width, height, "white"); |
| 107 | + for(int i = 0; i < rows; i++){ |
| 108 | + for(int j = 0; j < rows; j++){ |
| 109 | + pallete[i + (j * rows)].glColorApply(); |
| 110 | + super.renderQuad(x + 3 + (i * colls), y + 3 + (j * colls), colls, colls, "white"); |
| 111 | + RGB.glColorReset(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected boolean processButtonClick(int x, int y, boolean left){ |
| 118 | + int xx = (x - this.x - 3) / colls, yy = (y - this.y - 3) / colls; int zz = xx + (yy * rows); |
| 119 | + if(zz >= 0 && zz < pallete.length){ |
| 120 | + CURRENTCOLOR = pallete[zz]; ((TextureEditor)parent).updateFields(); |
| 121 | + } return true; |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + public static class HorizontalPallette extends Element { |
| 127 | + |
| 128 | + public HorizontalPallette(Element parent, int x, int y){ |
| 129 | + super(parent, "horizontal_color_palette"); this.height = 40; this.width = 294; |
| 130 | + this.x = parent.x + x; this.y = parent.y + y; z = -1; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void renderSelf(int rw, int rh){ |
| 135 | + super.renderQuad(x, y, width, height, "white"); |
| 136 | + if(hopall[0] == null){ |
| 137 | + for(int i = 0; i < hopall.length; i ++){ |
| 138 | + float c = i * (1f / hopall.length); |
| 139 | + int r, g, b; |
| 140 | + // |
| 141 | + if(c >= 0 && c <= (1/6.f)){ |
| 142 | + r = 255; |
| 143 | + g = (int)(1530 * c); |
| 144 | + b = 0; |
| 145 | + } |
| 146 | + else if( c > (1/6.f) && c <= (1/3.f) ){ |
| 147 | + r = (int)(255 - (1530 * (c - 1/6f))); |
| 148 | + g = 255; |
| 149 | + b = 0; |
| 150 | + } |
| 151 | + else if( c > (1/3.f) && c <= (1/2.f)){ |
| 152 | + r = 0; |
| 153 | + g = 255; |
| 154 | + b = (int)(1530 * (c - 1/3f)); |
| 155 | + } |
| 156 | + else if(c > (1/2f) && c <= (2/3f)) { |
| 157 | + r = 0; |
| 158 | + g = (int)(255 - ((c - 0.5f) * 1530)); |
| 159 | + b = 255; |
| 160 | + } |
| 161 | + else if( c > (2/3f) && c <= (5/6f) ){ |
| 162 | + r = (int)((c - (2/3f)) * 1530); |
| 163 | + g = 0; |
| 164 | + b = 255; |
| 165 | + } |
| 166 | + else if(c > (5/6f) && c <= 1 ){ |
| 167 | + r = 255; |
| 168 | + g = 0; |
| 169 | + b = (int)(255 - ((c - (5/6f)) * 1530)); |
| 170 | + } |
| 171 | + else{ r = 127; g = 127; b = 127; } |
| 172 | + hopall[i] = new RGB(r, g, b); |
| 173 | + } |
| 174 | + } |
| 175 | + for(int i = 0; i < hopall.length; i++){ |
| 176 | + hopall[i].glColorApply(); super.renderQuad(x + 3 + (i * 8), y, 8, 40, "white"); RGB.glColorReset(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + protected boolean processButtonClick(int x, int y, boolean left){ |
| 182 | + int xx = (x - this.x - 3) / 8; |
| 183 | + if(xx >= 0 && xx < hopall.length){ |
| 184 | + CURRENTCOLOR = hopall[xx]; ((TextureEditor)parent).updateFields(); |
| 185 | + } return true; |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments