2020-12-03 15:16:36 +01:00
|
|
|
#include "Paddle.h"
|
|
|
|
#include "../DisplayApp.h"
|
|
|
|
#include "../LittleVgl.h"
|
|
|
|
|
|
|
|
using namespace Pinetime::Applications::Screens;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
Paddle::Paddle(Pinetime::Applications::DisplayApp* app, Pinetime::Components::LittleVgl& lvgl) : Screen(app), lvgl {lvgl} {
|
2020-12-03 15:16:36 +01:00
|
|
|
app->SetTouchMode(DisplayApp::TouchModes::Polling);
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-06-24 12:33:15 +02:00
|
|
|
background = lv_obj_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_size(background, LV_HOR_RES + 1, LV_VER_RES);
|
|
|
|
lv_obj_set_pos(background, -1, 0);
|
|
|
|
lv_obj_set_style_local_radius(background, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
|
|
|
|
lv_obj_set_style_local_bg_color(background, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
|
|
|
|
lv_obj_set_style_local_border_color(background, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
|
|
|
|
lv_obj_set_style_local_border_width(background, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 1);
|
|
|
|
|
2020-12-08 05:36:30 +01:00
|
|
|
points = lv_label_create(lv_scr_act(), nullptr);
|
2021-06-24 12:33:15 +02:00
|
|
|
lv_obj_set_style_local_text_font(points, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
|
2021-04-04 04:08:51 +02:00
|
|
|
lv_label_set_text(points, "0000");
|
2021-06-24 12:33:15 +02:00
|
|
|
lv_obj_align(points, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 10);
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-06-24 12:33:15 +02:00
|
|
|
paddle = lv_obj_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_style_local_bg_color(paddle, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
|
|
|
|
lv_obj_set_style_local_radius(paddle, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, 0);
|
|
|
|
lv_obj_set_size(paddle, 4, 60);
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-06-24 12:33:15 +02:00
|
|
|
ball = lv_obj_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_style_local_bg_color(ball, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
|
|
|
|
lv_obj_set_style_local_radius(ball, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
|
|
|
|
lv_obj_set_size(ball, ballSize, ballSize);
|
2020-12-03 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Paddle::~Paddle() {
|
|
|
|
// Reset the touchmode
|
|
|
|
app->SetTouchMode(DisplayApp::TouchModes::Gestures);
|
|
|
|
lv_obj_clean(lv_scr_act());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Paddle::Refresh() {
|
2021-06-24 12:33:15 +02:00
|
|
|
ballX += dx;
|
|
|
|
ballY += dy;
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-06-24 12:33:15 +02:00
|
|
|
lv_obj_set_pos(ball, ballX, ballY);
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-06-24 12:33:15 +02:00
|
|
|
// checks if it has touched the sides (floor and ceiling)
|
|
|
|
if (ballY <= 1 || ballY >= LV_VER_RES - ballSize - 2) {
|
|
|
|
dy *= -1;
|
|
|
|
}
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-06-24 12:33:15 +02:00
|
|
|
// checks if it has touched the side (left side)
|
|
|
|
if (ballX >= LV_VER_RES - ballSize - 1) {
|
|
|
|
dx *= -1;
|
|
|
|
}
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-06-24 12:33:15 +02:00
|
|
|
// checks if it is in the position of the paddle
|
|
|
|
if (dx < 0 && ballX <= 4) {
|
|
|
|
if (ballX >= -ballSize / 4) {
|
|
|
|
if (ballY <= (paddlePos + 30 - ballSize / 4) && ballY >= (paddlePos - 30 - ballSize + ballSize / 4)) {
|
2021-04-18 19:28:14 +02:00
|
|
|
dx *= -1;
|
|
|
|
score++;
|
|
|
|
}
|
2020-12-03 15:16:36 +01:00
|
|
|
}
|
2021-04-18 19:28:14 +02:00
|
|
|
// checks if it has gone behind the paddle
|
2021-06-24 12:33:15 +02:00
|
|
|
else if (ballX <= -ballSize * 2) {
|
|
|
|
ballX = (LV_HOR_RES - ballSize) / 2;
|
|
|
|
ballY = (LV_VER_RES - ballSize) / 2;
|
2021-04-18 19:28:14 +02:00
|
|
|
score = 0;
|
2021-04-04 04:08:51 +02:00
|
|
|
}
|
2021-04-18 19:28:14 +02:00
|
|
|
}
|
2021-06-24 12:33:15 +02:00
|
|
|
lv_label_set_text_fmt(points, "%04d", score);
|
2020-12-03 15:16:36 +01:00
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
bool Paddle::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
|
|
|
|
return true;
|
2020-12-03 15:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Paddle::OnTouchEvent(uint16_t x, uint16_t y) {
|
2021-06-24 12:33:15 +02:00
|
|
|
// sets the center paddle pos. (30px offset) with the the y_coordinate of the finger
|
|
|
|
lv_obj_set_pos(paddle, 0, y - 30);
|
|
|
|
paddlePos = y;
|
2020-12-03 15:16:36 +01:00
|
|
|
return true;
|
|
|
|
}
|