Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_GFX.h> // Core graphics library
- #include <Adafruit_TFTLCD.h> // Hardware-specific library
- #include <TouchScreen.h> //Touch Screen
- #define YP A4 // must be an analog pin, use "An" notation!
- #define XM A5 // must be an analog pin, use "An" notation!
- #define YM 31 // can be a digital pin
- #define XP 30 // can be a digital pin
- #define MINPRESSURE 1
- #define MAXPRESSURE 1000
- // For better pressure precision, we need to know the resistance
- // between X+ and X- Use any multimeter to read it
- // For the one we're using, its 300 ohms across the X plate
- TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
- #define LCD_CS A3 // Chip Select goes to Analog 3
- #define LCD_CD A2 // Command/Data goes to Analog 2
- #define LCD_WR A1 // LCD Write goes to Analog 1
- #define LCD_RD A0 // LCD Read goes to Analog 0
- #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
- Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
- //COLORS
- int HAIR_COL = 0x0000, SKIN_COL = 0xE658, JACKET_COL[3] = {160, 160, 165}, ARMR_COL = 0xC596, ARML_COL = 0xCE38, OUTLINE_COL = 0x0000,
- BG_COL = 0xD79E;
- //MENU
- int MAINMEN_COL[] = {180, 90, 255},
- MAINMEN_SHADE1[] = {MAINMEN_COL[0] - 10, MAINMEN_COL[1] - 15, MAINMEN_COL[2] - 40},
- MAINMEN_SHADE2[] = {MAINMEN_COL[0] - 20, MAINMEN_COL[1] - 20, MAINMEN_COL[2] - 60};
- int MAINMEN_COL_ONE[] = {180, 90, 255},
- MAINMEN_COL_TWO[] = {150, 150, 160};
- int dialog = 0, quote = 0;
- bool pauseDialog = false;
- static const String menu_apps[] = {"Fight", "Scores", "Options"};
- static const int menu_length = 3;
- static const int menu_colors[][menu_length] = {{180, 220, 180}, {215, 198, 165}, {80, 80, 80}};
- static int menu_pos[menu_length][2];
- const int menu_fight = 0, menu_scores = 1, menu_options = 2;
- static int OPTION_BRIGHTNESS = 255, OPTION_SOUND = 255;
- static const int MENU_TEXT[] = {210, 210, 210};
- static const unsigned int BUTTON_SIZE = 25, BUTTON_BORDER = 12, BUTTON_SCALE = 4;
- unsigned int buttonTick = 0, choice = 0;
- static uint8_t sequence = 0;
- static const unsigned int SEQ_MENU = 1;
- void calcMenuShades()
- {
- MAINMEN_SHADE1[0] = MAINMEN_COL[0] - 10;
- MAINMEN_SHADE1[1] = MAINMEN_COL[1] - 15;
- MAINMEN_SHADE1[2] = MAINMEN_COL[2] - 20;
- MAINMEN_SHADE2[0] = MAINMEN_COL[0] - 20;
- MAINMEN_SHADE2[1] = MAINMEN_COL[1] - 20;
- MAINMEN_SHADE2[2] = MAINMEN_COL[2] - 30;
- if (MAINMEN_SHADE1[0] < 0) MAINMEN_SHADE1[0] = 0;
- if (MAINMEN_SHADE1[1] < 0) MAINMEN_SHADE1[1] = 0;
- if (MAINMEN_SHADE1[2] < 0) MAINMEN_SHADE1[2] = 0;
- if (MAINMEN_SHADE2[0] < 0) MAINMEN_SHADE2[0] = 0;
- if (MAINMEN_SHADE2[1] < 0) MAINMEN_SHADE2[1] = 0;
- if (MAINMEN_SHADE2[2] < 0) MAINMEN_SHADE2[2] = 0;
- }
- void setMenuCol(int col[2])
- {
- MAINMEN_COL[0] = col[0];
- MAINMEN_COL[1] = col[1];
- MAINMEN_COL[2] = col[2];
- calcMenuShades();
- }
- void setup(void)
- {
- Serial.begin(9600);
- pinMode(53, OUTPUT);
- //Screen setup
- tft.reset();
- uint16_t identifier = tft.readID();
- if (identifier == 0x8357) Serial.println(F("Found HX8357D LCD driver"));
- else
- {
- Serial.print(F("Unknown LCD driver chip: "));
- Serial.println(identifier, HEX);
- return;
- }
- tft.begin(identifier);
- Serial.println(F("Screen initialized. Loading background..."));
- tft.setRotation(1);
- sequence = SEQ_MENU;
- }
- void loop(void)
- {
- unsigned long current_time = millis();
- if (sequence == SEQ_MENU) tick_menu(current_time);
- }
- void tick_menu(long current_time)
- {
- if (buttonTick > 1 && !isTouch()) unpushButtons();
- if (current_time - old_time >= 3000)
- {
- old_time = current_time;
- pingTimer++;
- }
- if (current_time - old_half_time >= 500)
- {
- old_half_time = current_time;
- if (buttonTick > 0) buttonTick++;
- }
- TSPoint p = ts.getPoint();
- int touchX = p.y;
- int touchY = tft.width() - map(p.x, tft.width(), 0, tft.height(), 0) + (tft.width());
- if (isTouch() && buttonTick == 0)
- {
- for (int i = 0; i < menu_length; i++)
- if (touchX > menu_pos[i][0] * 2 && touchY > menu_pos[i][1] * 4)
- if (touchX < (menu_pos[i][0] * 2 + (BUTTON_SIZE * BUTTON_SCALE * 2)) && touchY < (menu_pos[i][1] * 4 + (BUTTON_SIZE * BUTTON_SCALE * 2) + 4))
- {
- pushButton(i, true);
- break;
- }
- }
- }
- void pushButton(int i, bool on)
- {
- int butCol = getColor(clampCol(menu_colors[i][0] / 2), clampCol(menu_colors[i][1] / 2), clampCol(menu_colors[i][2] / 2));
- if (!on) butCol = getColor(MAINMEN_SHADE1[0], MAINMEN_SHADE1[1], MAINMEN_SHADE1[2]);
- else
- {
- choice = i;
- buttonTick = 1;
- }
- if (sequence == SEQ_MENU)
- {
- tft.drawRoundRect(menu_pos[i][0] - 1, menu_pos[i][1] - 1, (BUTTON_SIZE * BUTTON_SCALE) + 2, (BUTTON_SIZE * BUTTON_SCALE) + 6, 6, butCol);
- tft.drawRoundRect(menu_pos[i][0] - 2, menu_pos[i][1] - 2, (BUTTON_SIZE * BUTTON_SCALE) + 4, (BUTTON_SIZE * BUTTON_SCALE) + 8, 8, butCol);
- tft.drawRoundRect(menu_pos[i][0] - 3, menu_pos[i][1] - 3, (BUTTON_SIZE * BUTTON_SCALE) + 6, (BUTTON_SIZE * BUTTON_SCALE) + 10, 10, butCol);
- tft.drawRoundRect(menu_pos[i][0] - 4, menu_pos[i][1] - 4, (BUTTON_SIZE * BUTTON_SCALE) + 8, (BUTTON_SIZE * BUTTON_SCALE) + 12, 12, butCol);
- tft.drawRoundRect(menu_pos[i][0] - 5, menu_pos[i][1] - 5, (BUTTON_SIZE * BUTTON_SCALE) + 10, (BUTTON_SIZE * BUTTON_SCALE) + 14, 14, butCol);
- tft.drawRoundRect(menu_pos[i][0] - 6, menu_pos[i][1] - 6, (BUTTON_SIZE * BUTTON_SCALE) + 12, (BUTTON_SIZE * BUTTON_SCALE) + 16, 16, butCol);
- if (on) playNote(0, 1, NOTE_E6, 32);
- else playNote(0, 1, NOTE_D6, 32);
- }
- if (!on && i == choice)
- {
- buttonTick = 0;
- if (choice == menu_options)
- {
- sequence = SEQ_OPTIONS;
- drawBG(sequence);
- }
- else if (choice == menu_fight)
- {
- sequence = SEQ_CUTSCENE;
- drawBG(sequence);
- }
- else if (choice == menu_scores)
- {
- sequence = SEQ_SCORES;
- drawBG(sequence);
- }
- }
- }
- int clamp(int val, int mini, int maxi)
- {
- if (val > maxi) return maxi;
- else if (val < mini) return mini;
- else return val;
- }
- int clampCol(int val)
- {
- return clamp(val, 0, 255);
- }
- void unpushButtons()
- {
- for (int i = 0; i < menu_length; i++) unpushButton(i);
- }
- void unpushButton(int i)
- {
- pushButton(i, false);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement