Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- final int rectSize = 200;
- final int[] xArray = {0, 1, 2, 3, 0, 0};
- final int[] yArray = {1, 0, 0, 0, 2, 3};
- final int[] xSolution = {1, 2, 3, 1, 2, 1};
- final int[] ySolution = {1, 1, 1, 2, 2, 3};
- final boolean[] turns = {false, true, true, true, false, false};
- final int[] indizes = {0, 1, 2, 3, 3, 2};
- TextField[][] textFields;
- final float offsetX = 200;
- final float offsetY = 200;
- String[][] texts;
- int fileCounter = 0;
- SaveFileButton button;
- SolutionsBlock[][] solutionBlocks;
- int[][] solution;
- LoadLevelInput loadLevel;
- void setup() {
- fullScreen();
- texts = new String[4][5];
- for (int i=0; i<texts.length; i++) {
- for (int j=0; j<texts[i].length; j++) {
- texts[i][j] = "";
- }
- }
- textFields = new TextField[6][5];
- for (int i=0; i<textFields.length; i++) {
- for (int j=0; j<textFields[i].length; j++) {
- textFields[i][j] = new TextField(rectSize * (xArray[i] + (turns[i] ? 0.2 * j : 0)), rectSize * (yArray[i] +(turns[i] ? 0 : 0.2 * j )), rectSize, turns[i], indizes[i], j, i, j);
- }
- }
- textSize(25);
- button = new SaveFileButton(width * 0.75, height/2, 50);
- solutionBlocks = new SolutionsBlock[6][25];
- for (int i=0; i<solutionBlocks.length; i++) {
- for (int j=0; j<solutionBlocks[i].length; j++) {
- solutionBlocks[i][j] = new SolutionsBlock((xSolution[i]+0.2*(j%5))*rectSize, (ySolution[i]+0.2*floor(j/5))*rectSize, rectSize/5, rectSize/5, i, j);
- }
- }
- solution = new int[6][25];
- loadLevel = new LoadLevelInput(width *0.75, height/3);
- }
- void draw() {
- background(255);
- pushMatrix();
- translate(offsetX, offsetY);
- for (int x=0; x<4; x++) {
- for (int y=0; y<4; y++) {
- strokeWeight(2);
- fill(255);
- rect(rectSize*x, rectSize * y, rectSize, rectSize);
- }
- }
- for (int i=0; i<textFields.length; i++) {
- for (int j=0; j<textFields[i].length; j++) {
- textFields[i][j].draw();
- }
- }
- for (int i=0; i<solutionBlocks.length; i++) {
- for (int j=0; j<solutionBlocks[i].length; j++) {
- solutionBlocks[i][j].draw();
- }
- }
- popMatrix();
- button.draw();
- loadLevel.draw();
- }
- class TextField {
- float x;
- float y;
- float h;
- float b;
- boolean active;
- int index1;
- int index2;
- final boolean turn;
- final int[] otherIndex;
- TextField(float x, float y, float rSize, boolean turn, int index1, int index2, int otherIndex1, int otherIndex2) {
- this.x = x;
- this.y = y;
- this.h = turn ? rSize : rSize /5;
- this.b = turn ? rSize / 5 : rSize ;
- this.index1 = index1;
- this.index2 = index2;
- this.turn = turn;
- otherIndex= new int[]{otherIndex1, otherIndex2};
- }
- void mousePressed(float mx, float my) {
- if (mx > x && mx < x + b && my > y && my < y + h) {
- active = true;
- nowIamActive(otherIndex[0], otherIndex[1]);
- }
- }
- void draw() {
- fill(255);
- rect(x, y, b, h);
- textAlign(CENTER, CENTER);
- fill(0);
- pushMatrix();
- translate( x + b/2, y + h/2);
- rotate(radians((turn) ? 270 : 0));
- text(texts[index1][index2], 0, 0);
- popMatrix();
- }
- void keyPressed(char k, int code) {
- if (active) {
- switch(k) {
- case BACKSPACE:
- if (texts[index1][index2].length() > 0) {
- texts[index1][index2] = texts[index1][index2].substring(0, texts[index1][index2].length()-1);
- }
- return;
- case SHIFT:
- return;
- }
- if (key == CODED) return;
- texts[index1][index2] += k;
- }
- }
- void setActive(boolean active) {
- this.active = active;
- }
- }
- class SaveFileButton {
- float x;
- float y;
- float r;
- SaveFileButton(float x, float y, float r) {
- this.x = x;
- this.y = y;
- this.r = r;
- }
- void draw() {
- fill(color(255, 0, 0));
- ellipse(x, y, r*2, r*2);
- }
- void mousePressed(float mx, float my) {
- PVector v = new PVector(mx - x, my - y);
- if (v.mag() < r) {
- generateJSON();
- }
- }
- }
- class SolutionsBlock {
- float x;
- float y;
- float b;
- float h;
- int index1;
- int index2;
- SolutionsBlock(float x, float y, float b, float h, int index1, int index2) {
- this.x = x;
- this.y = y;
- this.b = b;
- this.h = h;
- this.index1 = index1;
- this.index2 = index2;
- }
- void draw() {
- int status = solution[index1][index2];
- fill(255);
- rect(x, y, b, h);
- fill((status == 1) ? color (255, 0, 0) : (status == 2 ? color(0, 0, 255) : 255));
- textAlign(CENTER, CENTER);
- text(status == 1 ? "X": (status == 2 ? "O": " "), x+b/2, y+h/2);
- }
- void mousePressed(float mx, float my) {
- if (mx > x && mx < x + b && my > y && my < y + h) {
- solution[index1][index2]++;
- solution[index1][index2] %= 3;
- }
- }
- }
- class LoadLevelInput {
- float x;
- float y;
- float b;
- float h;
- float r;
- boolean active;
- String text;
- LoadLevelInput(float x, float y) {
- this.x = x;
- this.y = y;
- this.b = 300;
- this.h = 50;
- this.r = 25;
- text = "";
- }
- void draw() {
- fill(255);
- rect(x, y, b, h);
- textAlign(CENTER, CENTER);
- fill(0);
- text(text, x+b/2, y+ h/2);
- fill(0, 255, 0);
- ellipse(x+b*1.25, y + h/2, r*2, r*2);
- }
- void mousePressed(float mx, float my) {
- if (mx > x && mx < x + b && my > y && my < y + h) {
- active = true;
- nowIamActive(-1, -1);
- } else {
- PVector dis = new PVector(mx-x-1.25*b, my-y);
- if (dis.mag() < r) {
- loadLevel(text);
- }
- }
- }
- void keyPressed(char k, int code) {
- if (active) {
- switch(k) {
- case BACKSPACE:
- if (text.length() > 0) {
- text = text.substring(0, text.length()-1);
- }
- return;
- case SHIFT:
- return;
- }
- if (key == CODED) return;
- text += k;
- }
- }
- void setActive(boolean active) {
- this.active = active;
- }
- void loadLevel(String input) {
- String levelPath = "output/" + input;
- try {
- JSONObject in = loadJSONObject(levelPath);
- JSONArray sol = in.getJSONArray("solution");
- int[][] newSolution = new int[solution.length][solution[0].length];
- for (int i=0; i<newSolution.length; i++) {
- JSONArray s = sol.getJSONArray(i);
- newSolution[i] = s.getIntArray();
- }
- for (int i=0; i<newSolution.length; i++) {
- for (int j=0; j<newSolution[i].length; j++) {
- newSolution[i][j]++;
- }
- }
- solution = newSolution;
- JSONArray tex = in.getJSONArray("items");
- String[][] newTexts = new String[texts.length][texts[0].length];
- for (int i=0; i<newTexts.length; i++) {
- JSONArray t = tex.getJSONArray(i);
- newTexts[i] = t.getStringArray();
- }
- texts = newTexts;
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
- void mousePressed() {
- for (int i=0; i<textFields.length; i++) {
- for (int j=0; j<textFields[i].length; j++) {
- textFields[i][j].mousePressed(mouseX-offsetX, mouseY-offsetY);
- }
- }
- for (int i=0; i<solutionBlocks.length; i++) {
- for (int j=0; j<solutionBlocks[i].length; j++) {
- solutionBlocks[i][j].mousePressed(mouseX - offsetX, mouseY - offsetY);
- }
- }
- button.mousePressed(mouseX, mouseY);
- loadLevel.mousePressed(mouseX, mouseY);
- }
- void keyPressed() {
- for (int i=0; i<textFields.length; i++) {
- for (int j=0; j<textFields[i].length; j++) {
- textFields[i][j].keyPressed(key, keyCode);
- }
- }
- loadLevel.keyPressed(key, keyCode);
- }
- void nowIamActive(int i1, int i2) {
- for (int i=0; i<textFields.length; i++) {
- for (int j=0; j<textFields[i].length; j++) {
- if (!(i == i1 && j == i2)) {
- textFields[i][j].setActive(false);
- }
- }
- }
- if (!(i1 == -1 && i2 == -1)) {
- loadLevel.setActive(false);
- }
- }
- void generateJSON() {
- String jsonFileName = "output/" + str(fileCounter) + ".json";
- JSONObject level = new JSONObject();
- level.put("items", texts);
- int[][] sol = solution.clone();
- for (int i=0; i<sol.length; i++) {
- for (int j=0; j<sol[i].length; j++) {
- sol[i][j] -= 1;
- }
- }
- level.put("solution", solution);
- saveJSONObject(level, jsonFileName);
- println("JSON GENERATED! fileName: " + jsonFileName);
- fileCounter++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement