import ddf.minim.*; import ddf.minim.signals.*; Minim minim; SoundSample ring_sound; color background_color; PImage background_picture; PImage []tick_animation; PImage []mask_tick_animation; TimeSteps []time; Buttons timer_25_button; Buttons timer_5_button; int start_time; int last_time; boolean timer_on_25; boolean timer_on_5; int tick; int tick_alarm; void setup() { /* Set the size of the area */ size(449, 463); background_picture = loadImage("basic_pomodoro.png"); minim = new Minim(this); /* Set the speed of animation */ frameRate(30); /* create GUI items */ timer_25_button = new Buttons(0, 413, 449 / 2, 50, color(0,100,0), color(255), "Stop", "Start 25min"); timer_5_button = new Buttons(449 / 2, 413, 449 / 2, 50, color(100,0,0), color(255), "Stop", "Start 5min Break"); /* initialize the game data structures */ init_ds(); //start_time = second(); start_time = minute(); last_time = start_time; timer_on_25 = false; timer_on_5 = false; tick = 26; tick_alarm = 0; ring_sound = new SoundSample("ring.mp3"); } void draw() { timer_25_button.button_draw(); timer_5_button.button_draw(); if (tick == tick_alarm) { tick ++; ring_sound.play_sound(); ring_sound.stop_sound(); timer_on_25 = false; timer_25_button.button_on = false; timer_on_5 = false; timer_5_button.button_on = false; } else if (last_time != minute() && (timer_on_25 == true || timer_on_5 == true)) // else if (last_time != second() && (timer_on_25 == true || timer_on_5 == true)) { last_time = minute(); //last_time = second(); tick ++; /* new time so move the coordinates of the numbers */ for (int i = 0; i < 6; i++) { time[i].update_time(); if (time[i].is_next()) { time[(i+5)%6].reset_index(); } } } image(background_picture, 0, 0); /* moves the animation through the cycle of 5 every time step */ image(tick_animation[((last_time%5)+(5-start_time))%5], 0, 0); for (int i = 0; i < 6; i++) { image(time[i].image, time[i].x, time[i].y); } } void mouseClicked() { if (!(timer_on_25 || timer_on_5)) { timer_on_25 = timer_25_button.update_clicked(); timer_on_5 = timer_5_button.update_clicked(); } else if (timer_on_25) { timer_on_25 = timer_25_button.update_clicked(); } else if (timer_on_5) { timer_on_5 = timer_5_button.update_clicked(); } if (timer_on_25 == true) { tick_alarm = 25; start_time = minute()%5; last_time = minute(); //start_time = second()%5; //last_time = second(); time[5].set_index(5); time[4].set_index(0); time[0].set_index(10); time[1].set_index(-1); time[2].set_index(-1); time[3].set_index(-1); tick = 0; } else if (timer_on_5 == true) { tick_alarm = 5; start_time = minute()%5; last_time = minute(); //start_time = second()%5; //last_time = second(); time[5].set_index(-1); time[4].set_index(-1); time[0].set_index(0); time[1].set_index(5); time[2].set_index(10); time[3].set_index(-1); tick = 0; } } void stop() { minim.stop(); super.stop(); } void init_ds() { tick_animation = new PImage[5]; mask_tick_animation = new PImage[5]; time = new TimeSteps[6]; time[0] = new TimeSteps(5, "t0.gif"); time[1] = new TimeSteps(10, "t5.gif"); time[2] = new TimeSteps(-1, "t10.gif"); time[3] = new TimeSteps(-1, "t15.gif"); time[4] = new TimeSteps(-1, "t20.gif"); time[5] = new TimeSteps(0, "t25.gif"); tick_animation[0] = loadImage("tick1.png"); tick_animation[1] = loadImage("tick2.png"); tick_animation[2] = loadImage("tick3.png"); tick_animation[3] = loadImage("tick4.png"); tick_animation[4] = loadImage("tick5.png"); mask_tick_animation[0] = loadImage("tick1_mask.png"); mask_tick_animation[1] = loadImage("tick2_mask.png"); mask_tick_animation[2] = loadImage("tick3_mask.png"); mask_tick_animation[3] = loadImage("tick4_mask.png"); mask_tick_animation[4] = loadImage("tick5_mask.png"); tick_animation[0].mask(mask_tick_animation[0]); tick_animation[1].mask(mask_tick_animation[1]); tick_animation[2].mask(mask_tick_animation[2]); tick_animation[3].mask(mask_tick_animation[3]); tick_animation[4].mask(mask_tick_animation[4]); } class TimeSteps { int x; int y; PImage image; int index; TimeSteps(int index, String image_string) { this.index = index; convert_to_coords(index); image = loadImage (image_string); } boolean is_next() { if (index == 5) return true; else return false; } void reset_index() { index = 0; convert_to_coords(index); } void set_index(int index) { this.index = index; convert_to_coords(index); } void update_time() { if (index == 12) { index = -1; } else if (index != -1) { index ++; } convert_to_coords(index); } void convert_to_coords(int index) { switch(index) { case 0: this.x = 95; this.y = 160; break; case 1: this.x = 115; this.y = 160; break; case 2: this.x = 134; this.y = 161; break; case 3: this.x = 154; this.y = 161; break; case 4: this.x = 165; this.y = 169; break; case 5: this.x = 188; this.y = 178; break; case 6: this.x = 215; this.y = 178; break; case 7: this.x = 228; this.y = 177; break; case 8: this.x = 235; this.y = 176; break; case 9: this.x = 247; this.y = 185; break; case 10: this.x = 262; this.y = 164; break; case 11: this.x = 272; this.y = 164; break; case 12: this.x = 313; this.y = 165; break; default: this.x = -100; this.y = -100; } } } class Buttons { int x_top, y_top; int x_size, y_size; color button_color; color font_color; String button_text_on; String button_text_off; boolean button_on; boolean over; Buttons(int x_top, int y_top, int x_size, int y_size, color button_color, color font_color, String button_text_on, String button_text_off) { this.x_top = x_top; this.y_top = y_top; this.x_size = x_size; this.y_size = y_size; this.button_color = button_color; this.font_color = font_color; this.button_text_on = button_text_on; this.button_text_off = button_text_off; this.button_on = false; } boolean is_button_on() { return button_on; } boolean update_clicked() { if(is_over()) { over = true; } else { over = false; } if(over) { if (this.button_on == true) { button_on = false; } else { button_on = true; } } button_draw(); return button_on; } void button_draw() { /* create the rectangle button */ fill(button_color); stroke(color(0)); rect(x_top, y_top, x_size, y_size); /* put the writing on */ fill(font_color); if (button_on) { text(button_text_on, x_top+3, y_top+3, x_size, y_size); } else { text(button_text_off, x_top+3, y_top+3, x_size, y_size); } } boolean is_over() { if(mouseX > x_top && mouseX < x_top+x_size && mouseY > y_top && mouseY < y_top+y_size) { return true; } else { return false; } } } class SoundSample { AudioPlayer sound; String file; SoundSample(String sound_file) { this.file = sound_file; } void play_sound() { this.sound = minim.loadFile(file); this.sound.play(); delay (9000); } void stop_sound() { if (this.sound.isPlaying()) this.sound.pause(); this.sound.close(); minim.stop(); } }