int []check={0, 0, 0};
Acontroller c;
color []status = {color(255), color(255), color(255), color(255), color(255)};
int run = 0;
int countFrame = 0;
void setup() {
size(700, 380);
Amaker m = new Amaker (18, 10);
Aplayer p = new Aplayer (m);
Aviewer v = new Aviewer (m);
c = new Acontroller(m, v, p);
}
void draw() {
c.window();
c.button();
stroke(0);
}
class Amaker {
int w, h, tile, wide, count;
String [][] size;
String [][][] frame;
Amaker(int width_x, int height_y) {
w = width_x;
h = height_y;
tile = 0;
count = 0;
wide = 30;
size = new String [h][w];
frame = new String [100][h][w];
for (int i = 0; i<h; i++) {
for (int j = 0; j<w; j++) {
size[i][j] = "#";
}
}
}
void add_tile(int x, int y) {
size[y][x] = "0";
tile = tile+1;
}
void remove_tile(int x, int y) {
size[y][x] = "#";
tile = tile-1;
}
void remove_all_tile() {
for (int y = 0; y<h; y++) {
for (int x = 0; x<w; x++) {
size[y][x] = "#";
tile = 0;
}
}
}
void move_tile(int x1, int y1, int x2, int y2) {
if (size[y1][x1] == "0") {
size[y1][x1] = "#";
size[y2][x2] = "1";
}
}
void add_frame() {
for (int i = 0; i<h; i++) {
for (int j = 0; j<w; j++) {
frame[count][i][j] = size[i][j];
if (size[i][j] == "1") {
size[i][j] = "0";
}
}
}
count = count+1;
}
void remove_frame() {
if (count <= 0) {
count = 0;
} else {
count=count-1;
}
if (count>0) {
for (int i=0; i<h; i++) {
for (int j=0; j<w; j++) {
size[i][j]=frame[count-1][i][j];
}
}
}
}
int get_total_num_tile() {
int num = 0;
for (int y = 0; y<h; y++) {
for (int x = 0; x<w; x++) {
if (size[y][x] == "0" || size[y][x] == "1") {
num = num+1;
}
}
}
return num;
}
int get_total_num_frame() {
return count;
}
int get_width() {
return w;
}
int get_height() {
return h;
}
String [][] get_size() {
return size;
}
int get_wide() {
return wide;
}
String [][][] get_frame() {
return frame;
}
}
class Aviewer {
int w, h, wide;
String [][] size = new String [h][w];
Amaker m;
Aviewer(Amaker make) {
m = make;
w = m.get_width();
h = m.get_height();
size = m.get_size();
wide = m.get_wide();
}
void display(int x, int y) {
for (int i = 0; i<h; i++) {
for (int j = 0; j<w; j++) {
if (size[i][j] == "0" || size[i][j] == "1") {
fill(255);
} else if (size[i][j] == "#") {
fill(156, 145, 135);
}
rect(x+(j*wide), y+(i*wide), wide, wide);
}
}
}
}
class Acontroller {
int w, h, wide, x1, x2, y1, y2;
int posX = 20;
int posY = 20;
int posText = 425;
int move = 0;
String [][] size = new String [w][h];
int mode = 0;
Amaker m;
Aviewer v;
Aplayer p;
Acontroller(Amaker make, Aviewer view, Aplayer play) {
m = make;
v = view;
p = play;
w = make.get_width();
h = make.get_height();
size = m.get_size();
wide = m.get_wide();
}
void mouse_position() {
//add frame
if (positionMouseClick(575, 625, 245, 295)) {
m.add_frame();
}
//remove frame
if (positionMouseClick(575, 625, 310, 360)) {
m.remove_frame();
}
//remove tile
if (mouseButton == LEFT && positionMouseClick(575, 625, 95, 135)) {
m.remove_all_tile();
}
//play
if (mousePressed && positionMouseClick(430, 470, 330, 370) && m.get_total_num_frame() != 0) {
run = 1;
}
//stop
if (mousePressed && positionMouseClick(500, 540, 330, 370)) {
run = 0;
countFrame = 0;
}
}
void window() {
background(100);
textAlign(CENTER);
for (int y = 0; y < m.get_height(); y++) {
for (int x = 0; x < m.get_width(); x++) {
if (mousePressed && positionMouseClick(575, 625, 20, 70)) {
mode = 1;
check[0] = 1;
check[1] = 0;
check[2] = 0;
} else if (mousePressed && positionMouseClick(575, 625, 95, 135)) {
check[1] = 1;
check[0] = 0;
check[2] = 0;
} else if (mousePressed && positionMouseClick(575, 625, 150, 200)) {
check[2] = 1;
check[1] = 0;
check[0] = 0;
}
for (int i=0; i<3; i++) {
if (check[i] == 1) {
status[i] = #00FF00;
} else {
status[i] = 255;
}
}
//add tile
if (mousePressed && mouseButton == LEFT && run == 0 && check[0] ==1 && positionMouseClick(575,625,20,70)) {
m.add_tile(x, y);
}
//remove tile
else if (mousePressed && mouseButton == RIGHT && run == 0 && check[0] ==1 && positionMouseClick(575,625,95,135)) {
m.remove_tile(x, y);
}
//move tile
if (mousePressed && mouseButton == LEFT && run == 0 && check[2] ==1 && positionMouseClick(575,625,150,200)) {
if (move == 0 && size[y][x] == "0") {
x1 = x;
y1 = y;
move = 1;
}
if (move == 1) {
x2 = x;
y2 = y;
if ((abs(y2-y1) == 1 && x2 == x1) || (abs(x2-x1) == 1 && y2 == y1) && size[y][x] != "0" && size[y][x] != "1") {
m.move_tile(x1, y1, x2, y2);
move = 0;
}
}
}
}
}
if (mousePressed && positionMouseClick(380, 505, 450, 490)) {
status[3] = #00FF00;
} else status[3]= 255;
if (mousePressed && positionMouseClick(505, 630, 450, 490)) {
status[4] = #00FF00;
} else status[4]= 255;
if (run == 0) {
v.display(posX, posY);
frameRate(120);
} else {
p.play_frame(posX, posY);
frameRate(5);
}
if (mode == 0) {
textSize(30);
}
fill(0);
textSize(12);
text("add\ntile", 650, 40);
text("remove\ntile", 650, 105);
text("move\ntile", 650, 170);
text("add\nframe", 650, 265);
text("remove\nframe", 650, 330);
textSize(15);
fill(0);
text("Tile(s) : "+m.get_total_num_tile(), 60, 350);
text("Frame : "+m.get_total_num_frame(), 250, 350);
}
void button() {
noStroke();
//play
fill(255);
ellipse(450, 350, 40, 40);
fill(#00FF00);
triangle(440, 340, 440, 360, 460, 350);
//stop
fill(255);
ellipse(520, 350, 40, 40);
fill(#FF0000);
rect(510, 340, 20, 20);
fill(status[0]);
ellipse(600, 45, 50, 50); //add tile
fill(status[1]);
ellipse(600, 110, 50, 50); //remove tile
fill(status[2]);
ellipse(600, 175, 50, 50); //move tile
fill(status[3]);
ellipse(600, 270, 50, 50); //add frame
fill(status[4]);
ellipse(600, 335, 50, 50); //remove frame
fill(0);
//add tile
rect(592.5, 25, 15, 40);
rect(580, 37.5, 40, 15);
//remove tile
quad(580,100,590,90,620,120,610,130);
quad(590,130,580,120,610,90,620,100);
//move tile
triangle(585,160,595,160,585,170);
triangle(605,160,615,160,615,170);
triangle(615,180,615,190,605,190);
triangle(595,190,585,190,585,180);
stroke(5);
line(590,165,610,185);
line(610,165,590,185);
//add frame
rect(592.5, 250, 15, 40);
rect(580, 262.5, 40, 15);
//remove frame
quad(580,325,590,315,620,345,610,355);
quad(590,355,580,345,610,315,620,325);
}
}
class Aplayer {
int w, h, wide;
int posX = 215;
int posY = 75;
String [][] size = new String [w][h];
String [][][] frame = new String [100][w][h];
Amaker m;
Aplayer(Amaker make) {
m = make;
w = m.get_width();
h = m.get_height();
size = m.get_size();
wide = m.get_wide();
frame = m.get_frame();
}
void play_frame(int x, int y) {
for (int i = 0; i<h; i++) {
for (int j = 0; j<w; j++) {
if (frame[countFrame][i][j] == "0" || frame[countFrame][i][j] == "1") {
fill(255);
} else if (frame[countFrame][i][j] == "#") {
fill(156, 145, 135);
}
rect(x+(j*wide), y+(i*wide), wide, wide);
}
}
countFrame++;
countFrame = countFrame%m.get_total_num_frame();
}
}
boolean positionMouseClick(int morethanPosX, int lessthanPosX, int morethanPosY, int lessthanPosY) {
boolean check = (mouseX >= morethanPosX && mouseX <= lessthanPosX && mouseY >= morethanPosY && mouseY <= lessthanPosY);
return check;
}
void mousePressed() {
c.mouse_position();
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น