diff --git a/BadgeUI.h b/BadgeUI.h index f02491c7bf57439a4d7a95315a55be46f95402ac..2b2e20c691d6f34b4579b41fd7bee0c67613116d 100644 --- a/BadgeUI.h +++ b/BadgeUI.h @@ -2,6 +2,7 @@ #pragma once #define BUFFPIXEL 20 + #include <GPNBadge.hpp> #include "UIThemes.h" @@ -9,452 +10,492 @@ class UIElement { public: - virtual void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY) = 0; - virtual bool isDirty() = 0; - virtual void dispatchInput(JoystickState state) {} - virtual ~UIElement() {} - UIElement* parent = nullptr; - virtual bool isValid() { - return true; - } - virtual bool requiresFullScreen() { - return false; - } + virtual void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY) = 0; + + virtual bool isDirty() = 0; + + virtual void dispatchInput(JoystickState state) {} + + virtual ~UIElement() {} + + UIElement *parent = nullptr; + + virtual bool isValid() { + return true; + } + + virtual bool requiresFullScreen() { + return false; + } }; -class FullScreenStatus: public UIElement { +class FullScreenStatus : public UIElement { public: - void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY); + void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY); + + bool isDirty() { + return dirty; + } - bool isDirty() { - return dirty; - } + void setMain(String main) { + this->main = main; + this->dirty = true; + } - void setMain(String main) { - this->main = main; - this->dirty = true; - } - void setSub(String sub) { - this->sub = sub; - this->dirty = true; - } + void setSub(String sub) { + this->sub = sub; + this->dirty = true; + } - bool requiresFullScreen() { - return true; - } + bool requiresFullScreen() { + return true; + } private: - String main = "FULL"; - String sub ="screen status"; - bool dirty = true; + String main = "FULL"; + String sub = "screen status"; + bool dirty = true; }; -class Overlay: public UIElement { -public: - virtual uint16_t getOffsetX() = 0; - virtual uint16_t getOffsetY() = 0; - bool isValid() { - return true; - } - bool requiresFullScreen() { - return false; - } - virtual void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY) = 0; - virtual bool isDirty() = 0; +class Overlay : public UIElement { +public: + virtual uint16_t getOffsetX() = 0; + + virtual uint16_t getOffsetY() = 0; + + bool isValid() { + return true; + } + + bool requiresFullScreen() { + return false; + } + + virtual void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY) = 0; + + virtual bool isDirty() = 0; }; -class StatusOverlay: public Overlay { +class StatusOverlay : public Overlay { public: - StatusOverlay(uint16_t batCritical, uint16_t batFull): batCritical(batCritical), batFull(batFull), bat(batCritical) {} - bool isDirty() { - return dirty; - } - void draw(TFT_ILI9163C* tft, Theme * Theme, uint16_t offsetX, uint16_t offsetY); - bool isValid() { - return true; - } - void updateBat(uint16_t bat) { - if(this->bat == bat) { - return; - } - this->bat = bat; - this->dirty = true; - } - void updateClock(uint8_t hours, uint8_t minutes) { - if(this->hours == hours && this->minutes == minutes) { - return; - } - this->minutes = minutes; - this->hours = hours; - this->dirty = true; - } - void updateWiFiState(String wifi) { - if(this->wifi.equals(wifi)) { - return; - } - this->wifi = wifi; - this->dirty = true; - } - uint16_t getOffsetX(); - uint16_t getOffsetY(); + StatusOverlay(uint16_t batCritical, uint16_t batFull) : batCritical(batCritical), batFull(batFull), + bat(batCritical) {} + + bool isDirty() { + return dirty; + } + + void draw(TFT_ILI9163C *tft, Theme *Theme, uint16_t offsetX, uint16_t offsetY); + + bool isValid() { + return true; + } + + void updateBat(uint16_t bat) { + if (this->bat == bat) { + return; + } + this->bat = bat; + this->dirty = true; + } + + void updateClock(uint8_t hours, uint8_t minutes) { + if (this->hours == hours && this->minutes == minutes) { + return; + } + this->minutes = minutes; + this->hours = hours; + this->dirty = true; + } + + void updateWiFiState(String wifi) { + if (this->wifi.equals(wifi)) { + return; + } + this->wifi = wifi; + this->dirty = true; + } + + uint16_t getOffsetX(); + + uint16_t getOffsetY(); + private: - String wifi; - uint16_t bat, batCritical, batFull; - uint8_t hours,minutes; - bool dirty = true; + String wifi; + uint16_t bat, batCritical, batFull; + uint8_t hours, minutes; + bool dirty = true; }; -class NotificationScreen: public UIElement { +class NotificationScreen : public UIElement { public: - NotificationScreen(String summary, String location, String description): summary(summary), location(location), description(description){} + NotificationScreen(String summary, String location, String description) : summary(summary), location(location), + description(description) {} - void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY); + void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY); - bool isDirty() { - return dirty; - } + bool isDirty() { + return dirty; + } + + void dispatchInput(JoystickState state) { + if (state == JoystickState::BTN_ENTER) { + valid = false; + } + } - void dispatchInput(JoystickState state) { - if(state == JoystickState::BTN_ENTER) { - valid = false; + bool isValid() { + return valid; } - } - bool isValid() { - return valid; - } + bool requiresFullScreen() { + return true; + } - bool requiresFullScreen() { - return true; - } private: - String summary, location, description; - bool dirty = true; - bool valid = true; + String summary, location, description; + bool dirty = true; + bool valid = true; }; class BMPRender { public: - void bmpDraw(const char *filename, uint8_t x, uint16_t y, TFT_ILI9163C* tft); + void bmpDraw(const char *filename, uint8_t x, uint16_t y, TFT_ILI9163C *tft); }; -class FullScreenBMPStatus: public UIElement, BMPRender { +class FullScreenBMPStatus : public UIElement, BMPRender { public: - void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY); - - bool isDirty() { - return dirty; - } - void setBmp(char* path, uint16_t x, uint16_t y) { - this->bmp = path; - this->bmpx = x; - this->bmpy = y; - this->dirty = true; - } - void setSub(String sub) { - this->setSub(sub, 12, 105); - } - - void setSub(String sub, uint16_t x, uint16_t y) { - if(sub == this->sub && subx == x && suby == y) { - return; - } - this->sub = sub; - this->subx = x; - this->suby = y; - this->dirty = true; - } - - bool requiresFullScreen() { - return true; - } + void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY); + + bool isDirty() { + return dirty; + } + + void setBmp(char *path, uint16_t x, uint16_t y) { + this->bmp = path; + this->bmpx = x; + this->bmpy = y; + this->dirty = true; + } + + void setSub(String sub) { + this->setSub(sub, 12, 105); + } + + void setSub(String sub, uint16_t x, uint16_t y) { + if (sub == this->sub && subx == x && suby == y) { + return; + } + this->sub = sub; + this->subx = x; + this->suby = y; + this->dirty = true; + } + + bool requiresFullScreen() { + return true; + } private: - char* bmp = nullptr; - String sub ="screen status"; - bool dirty = true; - uint16_t bmpx = 0; - uint16_t bmpy = 0; - uint16_t subx = 12; - uint16_t suby = 105; + char *bmp = nullptr; + String sub = "screen status"; + bool dirty = true; + uint16_t bmpx = 0; + uint16_t bmpy = 0; + uint16_t subx = 12; + uint16_t suby = 105; }; -class FullScreenBMPDisplay: public UIElement, BMPRender { +class FullScreenBMPDisplay : public UIElement, BMPRender { public: - void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY); + void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY); - bool isDirty() { - return dirty; - } - void setBmp(char* path) { - this->bmp = path; - this->dirty = true; - } + bool isDirty() { + return dirty; + } - bool requiresFullScreen() { - return true; - } + void setBmp(char *path) { + this->bmp = path; + this->dirty = true; + } - void dispatchInput(JoystickState state) { - if(state == JoystickState::BTN_ENTER) { - valid = false; + bool requiresFullScreen() { + return true; } - } - bool isValid() { - return valid; - } + void dispatchInput(JoystickState state) { + if (state == JoystickState::BTN_ENTER) { + valid = false; + } + } + + bool isValid() { + return valid; + } private: - char* bmp = nullptr; - bool dirty = true; - bool valid = true; + char *bmp = nullptr; + bool dirty = true; + bool valid = true; }; -class SimpleTextDisplay: public UIElement { +class SimpleTextDisplay : public UIElement { public: - void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY); - bool isDirty() { - return dirty; - } - void setText(String text) { - this->text = text; - this->dirty = true; - } + void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY); + + bool isDirty() { + return dirty; + } + + void setText(String text) { + this->text = text; + this->dirty = true; + } private: - String text = "FULL"; - bool dirty = true; + String text = "FULL"; + bool dirty = true; }; -class ClosableTextDisplay: public SimpleTextDisplay { +class ClosableTextDisplay : public SimpleTextDisplay { public: - void dispatchInput(JoystickState state) { - if(state == JoystickState::BTN_ENTER) { - valid = false; - onClose(); + void dispatchInput(JoystickState state) { + if (state == JoystickState::BTN_ENTER) { + valid = false; + onClose(); + } } - } - bool isValid() { - return valid; - } + bool isValid() { + return valid; + } - void setOnClose(std::function<void()> onClose) { - this->onClose = onClose; - } + void setOnClose(std::function<void()> onClose) { + this->onClose = onClose; + } private: - bool valid = true; - std::function<void()> onClose; + bool valid = true; + std::function<void()> onClose; }; class WindowSystem { public: - FullScreenBMPStatus* root; - UIElement* head; - WindowSystem(TFT_ILI9163C* tft): tft(tft) { - head = root = new FullScreenBMPStatus(); - } - - void open(UIElement* element) { - element->parent = head; - head = element; - } - - void closeCurrent() { - UIElement* old = head; - if(!old->parent) { - return; - } - head = old->parent; - forceRedraw = true; - delete old; - } - - void draw() { - if(!head->isValid()) { - closeCurrent(); - return; - } - if(forceRedraw || head->isDirty()) { - if(overlay && !head->requiresFullScreen()) { - head->draw(this->tft, theme, overlay->getOffsetX(), overlay->getOffsetY()); - } else { - head->draw(this->tft, theme,0 ,0); - } - if(overlay && !head->requiresFullScreen()) { - overlay->draw(this->tft, theme, 0, 0); - } - this->tft->writeFramebuffer(); - forceRedraw = false; - } - if(overlay && !head->requiresFullScreen() && overlay->isDirty()) { - overlay->draw(this->tft, theme, 0, 0); - this->tft->writeFramebuffer(); - } - } - - void dispatchInput(JoystickState state){ - if(prevState == state) { - return; - } - prevState = state; - head->dispatchInput(state); - } - - void setTheme(Theme * theme) { - delete this->theme; - this->theme = theme; - forceRedraw = true; - } - - void setOverlay(Overlay * overlay) { - this->overlay = overlay; - forceRedraw = true; - } - - Theme * getTheme() { - return theme; - } + FullScreenBMPStatus *root; + UIElement *head; + + WindowSystem(TFT_ILI9163C *tft) : tft(tft) { + head = root = new FullScreenBMPStatus(); + } + + void open(UIElement *element) { + element->parent = head; + head = element; + } + + void closeCurrent() { + UIElement *old = head; + if (!old->parent) { + return; + } + head = old->parent; + forceRedraw = true; + delete old; + } + + void draw() { + if (!head->isValid()) { + closeCurrent(); + return; + } + if (forceRedraw || head->isDirty()) { + if (overlay && !head->requiresFullScreen()) { + head->draw(this->tft, theme, overlay->getOffsetX(), overlay->getOffsetY()); + } else { + head->draw(this->tft, theme, 0, 0); + } + if (overlay && !head->requiresFullScreen()) { + overlay->draw(this->tft, theme, 0, 0); + } + this->tft->writeFramebuffer(); + forceRedraw = false; + } + if (overlay && !head->requiresFullScreen() && overlay->isDirty()) { + overlay->draw(this->tft, theme, 0, 0); + this->tft->writeFramebuffer(); + } + } + + void dispatchInput(JoystickState state) { + if (prevState == state) { + return; + } + prevState = state; + head->dispatchInput(state); + } + + void setTheme(Theme *theme) { + delete this->theme; + this->theme = theme; + forceRedraw = true; + } + + void setOverlay(Overlay *overlay) { + this->overlay = overlay; + forceRedraw = true; + } + + Theme *getTheme() { + return theme; + } + protected: - TFT_ILI9163C* tft; + TFT_ILI9163C *tft; private: - Overlay * overlay = nullptr; - bool forceRedraw = false; - JoystickState prevState = JoystickState::BTN_NOTHING; - Theme * theme = new ThemeLight(); + Overlay *overlay = nullptr; + bool forceRedraw = false; + JoystickState prevState = JoystickState::BTN_NOTHING; + Theme *theme = new ThemeLight(); }; -class MenuItem: public UIElement { +class MenuItem : public UIElement { public: - friend class Menu; - MenuItem(String text, std::function<void(void)> trigger): text(text), triggerFunc(trigger) { - } - - void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY); - - bool isDirty() { - return true; - } - - void setSelect(bool selected) { - this->selected = selected; - } - - void setText(String text) { - this->text = text; - } - - void setTrigger(std::function<void()> triggr) { - this->triggerFunc = triggr; - } + friend class Menu; + + MenuItem(String text, std::function<void(void)> trigger) : text(text), triggerFunc(trigger) { + } + + void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY); + + bool isDirty() { + return true; + } + + void setSelect(bool selected) { + this->selected = selected; + } + + void setText(String text) { + this->text = text; + } + + void setTrigger(std::function<void()> triggr) { + this->triggerFunc = triggr; + } + private: - String text; - std::function<void(void)> triggerFunc; - bool selected = false; + String text; + std::function<void(void)> triggerFunc; + bool selected = false; protected: - MenuItem* prev = nullptr; - MenuItem* next = nullptr; - void trigger() { - triggerFunc(); - } + MenuItem *prev = nullptr; + MenuItem *next = nullptr; + + void trigger() { + triggerFunc(); + } }; -class Menu: public UIElement { +class Menu : public UIElement { public: - Menu(int itemsPerPage): itemsPerPage(itemsPerPage) {} - Menu():Menu(3) {} - ~Menu(){ - MenuItem * ite = tail; - while(ite) { - MenuItem * pre = ite->prev; - delete ite; - ite = pre; - } - } - - void draw(TFT_ILI9163C* tft, Theme * theme, uint16_t offsetX, uint16_t offsetY); - - void dispatchInput(JoystickState state) { - switch(state) { - case JoystickState::BTN_UP: - if(!focus->prev) { - return; + Menu(int itemsPerPage) : itemsPerPage(itemsPerPage) {} + + Menu() : Menu(3) {} + + ~Menu() { + MenuItem *ite = tail; + while (ite) { + MenuItem *pre = ite->prev; + delete ite; + ite = pre; + } + } + + virtual void draw(TFT_ILI9163C *tft, Theme *theme, uint16_t offsetX, uint16_t offsetY); + + void dispatchInput(JoystickState state) { + switch (state) { + case JoystickState::BTN_UP: + if (!focus->prev) { + return; + } + focus->setSelect(false); + focus = focus->prev; + focus->setSelect(true); + break; + case JoystickState::BTN_DOWN: + if (!focus->next) { + return; + } + focus->setSelect(false); + focus = focus->next; + focus->setSelect(true); + break; + case JoystickState::BTN_ENTER: + focus->trigger(); + break; + default: + return; + } + int distanceDown = 0; + MenuItem *temp = firstVisible; + while (temp != focus) { + if (!temp->next) { + distanceDown = 0; + break; + } + temp = temp->next; + distanceDown++; + + } + if (distanceDown >= itemsPerPage) { + firstVisible = firstVisible->next; } - focus->setSelect(false); - focus = focus->prev; - focus->setSelect(true); - break; - case JoystickState::BTN_DOWN: - if(!focus->next) { - return; + if (focus->next == firstVisible) { + firstVisible = firstVisible->prev; } - focus->setSelect(false); - focus = focus->next; - focus->setSelect(true); - break; - case JoystickState::BTN_ENTER: - focus->trigger(); - break; - default: - return; - } - int distanceDown = 0; - MenuItem * temp = firstVisible; - while(temp != focus) { - if(!temp->next) { - distanceDown = 0; - break; - } - temp = temp->next; - distanceDown++; - - } - if(distanceDown >= itemsPerPage) { - firstVisible = firstVisible->next; - } - if(focus->next == firstVisible) { - firstVisible = firstVisible->prev; - } - dirty = true; - } - - bool isDirty() { - return dirty; - } - - void addMenuItem(MenuItem * item) { - item->prev = tail; - item->next = nullptr; - if(tail) { - tail->next = item; - } else { - firstVisible = item; - focus = item; - item->setSelect(true); - } - tail = item; - dirty = true; - } - void Clear() { - MenuItem* tail = this->tail; - while(tail) { - MenuItem* prev = tail->prev; - delete tail; - tail = prev; - } - dirty = true; - tail = nullptr; - } + dirty = true; + } + + bool isDirty() { + return dirty; + } + + void addMenuItem(MenuItem *item) { + item->prev = tail; + item->next = nullptr; + if (tail) { + tail->next = item; + } else { + firstVisible = item; + focus = item; + item->setSelect(true); + } + tail = item; + dirty = true; + } + + void Clear() { + MenuItem *tail = this->tail; + while (tail) { + MenuItem *prev = tail->prev; + delete tail; + tail = prev; + } + dirty = true; + tail = nullptr; + } + + void setDirty() { this->dirty = true; } + private: - MenuItem * tail = nullptr; - bool dirty = true; - MenuItem * firstVisible = nullptr; - MenuItem * focus = nullptr; - int itemsPerPage; + bool dirty = true; + MenuItem *tail = nullptr; + MenuItem *firstVisible = nullptr; + MenuItem *focus = nullptr; + int itemsPerPage; }; diff --git a/GPN_BadgeLasertag.ino b/GPN_BadgeLasertag.ino index d8c1466751de5a5fe446de87e1d15d30df2d27f2..efd18c69aff79e3e81b6b21290c7903153ef6dc3 100644 --- a/GPN_BadgeLasertag.ino +++ b/GPN_BadgeLasertag.ino @@ -42,7 +42,7 @@ GameServer *server; GameClient *client; char pixIndex = 0; bool changePixels = true; -unsigned long lastChange = 0; +unsigned long lastChange = 0; unsigned long lastNotificationPull = 0; void setup() { @@ -76,6 +76,7 @@ void hostGame() { #endif client = new GameClient(ui, &badge, player); + server->setGamestartCallback([=]() { client->startGame(); }); #ifdef DEBUG Serial.printf("Host - Ende\n"); #endif @@ -165,6 +166,7 @@ void joinGame() { } void loop() { + Serial.println("Loop"); ui->dispatchInput(badge.getJoystickState()); ui->draw(); @@ -185,11 +187,14 @@ void loop() { pixels.show(); if (server) { + Serial.println("Loop - Server"); server->update(); + Serial.println("Loop - Server - Ende"); } if (client) { // client->update(); } + Serial.println("Loop - Ende"); } char *loadNick() { diff --git a/GameClient.cpp b/GameClient.cpp index 7dc1da0fd6d42e48050e0e5e499f75e64b5d0658..3dccb35ee0168869540b8bdcfd195d88bb678ab8 100644 --- a/GameClient.cpp +++ b/GameClient.cpp @@ -6,18 +6,21 @@ void GameClient::createUIGameStart() { if (this->playerList) { - delete this->playerList; + this->playerList->Clear(); + Serial.println("GameClient::createUIGameStart - Clear"); + } else { + this->playerList = new PlayerListMenu(9); } - playerList = new PlayerListMenu(9); + Serial.println("GameClient::createUIGameStart - new PlayerListMenu"); playerList->setLeftAction([=]() { this->player->prevWeapon(); }); playerList->setRightAction([=]() { this->player->nextWeapon(); }); playerList->setDownAction([=]() { this->player->reload(); }); playerList->setEnterAction([=]() { this->player->shot(); }); + Serial.println("GameClient::createUIGameStart - set Lambdas"); LaserMenuItem *healthMI = new LaserMenuItem([]() {}, "/heart.bmp"); LaserMenuItem *armorMI = new LaserMenuItem([]() {}, "/armor.bmp"); - LaserMenuItem *ammoMI = new LaserMenuItem([]() {}, "/ammo.bmp"); + LaserMenuItem *ammoMI = new LaserMenuItem([]() {}, "/ammo1.bmp"); LaserMenuItem *nameMI = new LaserMenuItem([]() {}, nullptr); - nameMI->setText(player->getNickname()); player->setHealthMenuItem(healthMI); player->setArmorMenuItem(armorMI); player->setAmmoMenuItem(ammoMI); @@ -25,15 +28,19 @@ void GameClient::createUIGameStart() { playerList->addMenuItem(armorMI); playerList->addMenuItem(ammoMI); playerList->addMenuItem(nameMI); - ui->open(playerList); + Serial.println("GameClient::createUIGameStart - added Lambdas"); + nameMI->setText(player->getNickname()); + Serial.println("GameClient::createUIGameStart - set Nickname"); + playerList->setDirty(); + reopen = true; + Serial.println("GameClient::createUIGameStart - Ende"); } void GameClient::startGame() { -#ifdef DEBUG - Serial.printf("Start Game!"); -#endif + Serial.println("GameClient::startGame()"); this->createUIGameStart(); + Serial.println("GameClient::startGame() - Ende"); } @@ -53,17 +60,22 @@ void GameClient::joinGame(String ip) { void GameClient::update() { - - String lastHitBy; - int lastDamage; - - if (wasHit(&lastHitBy, &lastDamage)){ - player->hit(lastDamage); - - tft.setCursor(20, 20); - tft.print("Hit by " + lastHitBy); + Serial.println("GameClient - Update"); + if(reopen) { + reopen = false; + ui->open(playerList); } +// String lastHitBy; +// int lastDamage; +// +// if (wasHit(&lastHitBy, &lastDamage)){ +// player->hit(lastDamage); +// +// tft.setCursor(20, 20); +// tft.print("Hit by " + lastHitBy); +// } + Serial.println("GameClient - Update - Ende"); } bool GameClient::wasHit(String* nick, int* damage, bool enable) { diff --git a/GameClient.hpp b/GameClient.hpp index ac33e8a9ce87a72dea0f327cab3e43513fb3fe60..f857f56b07292f3e5367d8cff1123e7678c1fe50 100644 --- a/GameClient.hpp +++ b/GameClient.hpp @@ -33,7 +33,7 @@ private: char *host = "127.0.0.1"; // MaxArraySize PlayerListMenu *playerList = new PlayerListMenu(9); WiFiClient *client; // the client connected on client side - + bool reopen = false; }; #endif //GPN_LASERTAG_GAMECLIENT_HPP diff --git a/GameServer.cpp b/GameServer.cpp index 49c69f1aa20e97d583bdc4345c7053f8112d8029..c2a48f0724fdf77220b45dc1eb40cc1c3f6f64b1 100644 --- a/GameServer.cpp +++ b/GameServer.cpp @@ -22,9 +22,15 @@ void GameServer::secureQuestion() { Serial.printf("Sicherheitsabfrage!"); #endif secureMenu = new PlayerListMenu(4); - secureMenu->addMenuItem(new MenuItem("Start?", [=]() { this->startGame(); })); - secureMenu->addMenuItem(new MenuItem("Cancel?", [=]() { ui->closeCurrent(); })); - secureMenu->setRightAction([=]() { ui->closeCurrent(); }); + secureMenu->addMenuItem(new MenuItem("Start?", [=]() { this->startGame(); Serial.println("Lambda Start - Ende"); })); + secureMenu->addMenuItem(new MenuItem("Cancel?", [=]() { + ui->open(playerList); + ui->draw(); + })); + secureMenu->setRightAction([=]() { + ui->open(playerList); + ui->draw(); + }); secureMenu->setLeftAction([=]() { this->startGame(); }); ui->open(secureMenu); } @@ -46,13 +52,14 @@ void GameServer::update() { handleWelcome(&(serverClients[numConnections]), numConnections); } } else { - for (short i = 0; i <= numConnections; i++) { //iterate throu the slots + Serial.println("Waiting for packets"); + for (short i = 0; i < numConnections; i++) { //iterate throu the slots if (serverClients[i].connected() && serverClients[i].available()) { //skip unconnected slots String message = serverClients[i].readStringUntil('\n'); UrlDecode decode(message.c_str()); String strTyp = String(decode.getKey("typ")); if (strTyp == "hit") { - HitPacket* packet = HitPacket::read(message); + HitPacket *packet = HitPacket::read(message); sendHit(packet); delete packet; } @@ -72,6 +79,7 @@ void GameServer::handleWelcome(WiFiClient *client, short iPlayer) { rPlayers[iPlayer] = player; numConnections++; sendAll(buildPlayerList()); + playerList->addMenuItem(new MenuItem(message, []() {})); } void GameServer::sendAll(String value) { @@ -111,7 +119,7 @@ void GameServer::sendIP() { void GameServer::startGame() { if (!gameStarted) { - Serial.println("Game started"); + Serial.println("Start Game - GameServer"); ConfigPacket config; for (int i = 0; i < 3; i++) { config.magazineSize[i] = Player::magazineSize[i]; @@ -123,12 +131,17 @@ void GameServer::startGame() { config.health = HEALTH; String value = config.serial() + "\n"; sendAll(value); + sendAll(this->buildPlayerList()); this->gameStarted = true; - ui->closeCurrent(); + if (gameStartCallback) { + Serial.println("Start Game - GameServer - call Callback"); + gameStartCallback(); + } + Serial.println("Start Game - GameServer - Ende"); } } -void GameServer::sendHit(HitPacket* hitPacket) { +void GameServer::sendHit(HitPacket *hitPacket) { int pid = hitPacket->playerId; serverClients[pid].print(hitPacket->serial()); serverClients[pid].flush(); diff --git a/GameServer.hpp b/GameServer.hpp index f0a90f3c37787ed5a492c9dba0f6f7e46254f9db..1c2eb3e2813a1905728c8e3d22790575ca6876b5 100644 --- a/GameServer.hpp +++ b/GameServer.hpp @@ -80,6 +80,10 @@ public: virtual void update(); + bool isStarted() { return this->gameStarted; } + + void setGamestartCallback(std::function<void(void)> callback) { this->gameStartCallback = callback; } + protected: void setTeamPlay(char isTeam); @@ -95,6 +99,7 @@ protected: void sendHit(HitPacket *hitPacket); + private: WindowSystem *ui; Badge *badge; @@ -105,6 +110,7 @@ private: WiFiClient *serverClients; // the clients connected to the server short numConnections = 0; bool gameStarted = false; + std::function<void(void)> gameStartCallback; void sendIP(); diff --git a/LaserMenuItem.cpp b/LaserMenuItem.cpp index 0be7442ba0b24b52e24edddf89965fd8dc26edf4..c585f1e38afb47311320a47632e4030506bd3f27 100644 --- a/LaserMenuItem.cpp +++ b/LaserMenuItem.cpp @@ -11,6 +11,7 @@ uint32_t read32(File &f); uint16_t read16(File &f); void LaserMenuItem::bmpDraw(uint8_t x, uint16_t y, TFT_ILI9163C *tft) { + Serial.println("LaserMenuItem::bmpDraw"); if (this->imagePath) { File bmpFile; uint16_t bmpWidth, bmpHeight; // W+H in pixels @@ -96,4 +97,5 @@ void LaserMenuItem::bmpDraw(uint8_t x, uint16_t y, TFT_ILI9163C *tft) { tft->print("file unrecognized!"); } } + Serial.println("LaserMenuItem::bmpDraw - Ende"); } diff --git a/LaserMenuItem.hpp b/LaserMenuItem.hpp index 245c019bc40da131fadf9b853504a67fde826ae4..a7f5c7179ce39f1966d06ca2fd037bdcc55b9b62 100644 --- a/LaserMenuItem.hpp +++ b/LaserMenuItem.hpp @@ -10,9 +10,12 @@ #include "BadgeUI.h" class LaserMenuItem : public MenuItem { public: - LaserMenuItem(std::function<void(void)> trigger, const char *imagePath) : + LaserMenuItem(std::function<void(void)> trigger, const char* imagePath) : MenuItem("100%", trigger), imagePath(imagePath) { }; + + void setImagePath(String imagePath) { this->imagePath = imagePath.c_str(); } + private: const char *imagePath; void bmpDraw(uint8_t, uint16_t, TFT_ILI9163C*); diff --git a/Player.cpp b/Player.cpp index ebf7d59f32a2b2b8f5f05135ee39783dd9f45fbd..0bc89d10ddea48fe8211454002714691fb281073 100644 --- a/Player.cpp +++ b/Player.cpp @@ -49,7 +49,8 @@ void Player::updateArmorMenuItem() { snprintf(buff, sizeof(buff), "%d %", this->currentArmor / this->maxArmor * 100); String percentage = buff; delete[] buff; - this->healthMenuItem->setText(percentage); + this->armorMenuItem->setText(percentage); + this->armorMenuItem->setImagePath(String("/ammo" + String(this->weaponIndex) + ".bmp")); } } diff --git a/PlayerListMenu.hpp b/PlayerListMenu.hpp index 047198e8a0424af7c13602adf4a2691a08699676..74c5eb55f16b494f1bbc1d092eeea0a9a0b5c063 100644 --- a/PlayerListMenu.hpp +++ b/PlayerListMenu.hpp @@ -44,9 +44,6 @@ public: unsigned short getIndex() { return index; } void dispatchInput(JoystickState state) { -#ifdef DEBUG - Serial.println("PlayerListMenu - dispatch"); -#endif switch (state) { case JoystickState::BTN_UP: if (upAction) { @@ -87,9 +84,6 @@ public: Menu::dispatchInput(state); break; } -#ifdef DEBUG - Serial.println("PlayerListMenu - dispatch - Ende"); -#endif }; void addMenuItem(MenuItem *item) {