omg on a un menu

This commit is contained in:
2025-03-23 18:45:34 +01:00
parent 1781b85332
commit e721a71894
4 changed files with 181 additions and 3 deletions

View File

@@ -9,15 +9,19 @@
MainAppMenu::MainAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) : MainAppMenu::MainAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow) :
AppMenu(menuStack, settings, renderWindow) { AppMenu(menuStack, settings, renderWindow),
playerCursor(std::vector<unsigned int>({1, 1, 1})) {
} }
void MainAppMenu::computeFrame() { void MainAppMenu::computeFrame() {
this->updateMetaBinds(); this->updateMetaBinds();
this->playerCursor.updatePosition();
if (this->enterReleased) { if (this->enterReleased) {
this->menuStack->push(std::make_shared<InGameAppMenu>(this->menuStack, this->settings, this->renderWindow)); if (this->playerCursor.getPosition().y == 0) {
this->menuStack->push(std::make_shared<InGameAppMenu>(this->menuStack, this->settings, this->renderWindow));
}
} }
if (this->escReleased) { if (this->escReleased) {
this->menuStack->pop(); this->menuStack->pop();
@@ -25,7 +29,37 @@ void MainAppMenu::computeFrame() {
} }
void MainAppMenu::drawFrame() const { void MainAppMenu::drawFrame() const {
this->renderWindow->clear(sf::Color::Black); this->renderWindow->clear(sf::Color(200, 200, 200));
float sizeMultiplier = this->settings->getWindowSizeMultiplier();
sf::Font font("data/fonts/arial.ttf");
sf::Text text(font, "", this->settings->getWindowSizeMultiplier() * 5);
text.setFillColor(sf::Color(0, 0, 0));
text.setOutlineColor(sf::Color(255, 255, 255));
text.setString("jminos");
text.setOrigin(text.getLocalBounds().getCenter());
text.setPosition(sf::Vector2f({sizeMultiplier * 40, sizeMultiplier * 10}));
this->renderWindow->draw(text);
text.setString("Play");
text.setOutlineThickness((this->playerCursor.getPosition().y == 0) ? (sizeMultiplier / 2) : 0);
text.setOrigin(text.getLocalBounds().getCenter());
text.setPosition(sf::Vector2f({sizeMultiplier * 40, sizeMultiplier * 20}));
this->renderWindow->draw(text);
text.setString("Settings");
text.setOutlineThickness((this->playerCursor.getPosition().y == 1) ? (sizeMultiplier / 2) : 0);
text.setOrigin(text.getLocalBounds().getCenter());
text.setPosition(sf::Vector2f({sizeMultiplier * 40, sizeMultiplier * 30}));
this->renderWindow->draw(text);
text.setString("Info");
text.setOutlineThickness((this->playerCursor.getPosition().y == 2) ? (sizeMultiplier / 2) : 0);
text.setOrigin(text.getLocalBounds().getCenter());
text.setPosition(sf::Vector2f({sizeMultiplier * 40, sizeMultiplier * 40}));
this->renderWindow->draw(text);
this->renderWindow->display(); this->renderWindow->display();
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "AppMenu.h" #include "AppMenu.h"
#include "PlayerCursor.h"
#include <stack> #include <stack>
#include <memory> #include <memory>
@@ -8,6 +9,9 @@
class MainAppMenu : public AppMenu { class MainAppMenu : public AppMenu {
private:
PlayerCursor playerCursor;
public: public:
MainAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow); MainAppMenu(std::shared_ptr<MenuStack> menuStack, std::shared_ptr<Settings> settings, std::shared_ptr<sf::RenderWindow> renderWindow);

View File

@@ -0,0 +1,105 @@
#include "PlayerCursor.h"
#include "../Keybinds.h"
#include "../Settings.h"
#include <vector>
#include <algorithm>
#include <SFML/Graphics.hpp>
static const int MENU_DAS = FRAMES_PER_SECOND / 2;
PlayerCursor::PlayerCursor(std::vector<unsigned int> rows) :
rows(rows) {
this->position = sf::Vector2u({0, 0});
this->leftDAS = 0;
this->rightDAS = 0;
this->upDAS = 0;
this->downDAS = 0;
}
void PlayerCursor::updatePosition() {
(sf::Keyboard::isKeyPressed(sfKey::Left)) ? (this->leftDAS++) : (this->leftDAS = 0);
if (this->shouldMove(this->leftDAS)) {
this->moveLeft();
}
(sf::Keyboard::isKeyPressed(sfKey::Right)) ? (this->rightDAS++) : (this->rightDAS = 0);
if (this->shouldMove(this->rightDAS)) {
this->moveRight();
}
(sf::Keyboard::isKeyPressed(sfKey::Up)) ? (this->upDAS++) : (this->upDAS = 0);
if (this->shouldMove(this->upDAS)) {
this->moveUp();
}
(sf::Keyboard::isKeyPressed(sfKey::Down)) ? (this->downDAS++) : (this->downDAS = 0);
if (this->shouldMove(this->downDAS)) {
this->moveDown();
}
}
void PlayerCursor::goToPosition(const sf::Vector2u& newPosition) {
if (this->rows.size() > newPosition.y) {
if (this->rows.at(newPosition.y) > newPosition.x) {
this->position = newPosition;
}
}
}
const sf::Vector2u& PlayerCursor::getPosition() const {
return this->position;
}
bool PlayerCursor::shouldMove(int DAS) const {
return (DAS == 1
|| (DAS > MENU_DAS && (DAS % 5) == 0)
|| (DAS > (FRAMES_PER_SECOND * 2)));
}
void PlayerCursor::moveLeft() {
if (this->position.x == 0) {
this->position.x = this->rows.at(this->position.y) - 1;
}
else {
this->position.x--;
}
}
void PlayerCursor::moveRight() {
if (this->position.x == this->rows.at(this->position.y) - 1) {
this->position.x = 0;
}
else {
this->position.x++;
}
}
void PlayerCursor::moveUp() {
if (this->position.y == 0) {
this->position.y = this->rows.size() - 1;
}
else {
this->position.y--;
}
if (this->position.x > this->rows.at(this->position.y)) {
this->position.x = this->rows.at(this->position.y) - 1;
}
}
void PlayerCursor::moveDown() {
if (this->position.y == this->rows.size() - 1) {
this->position.y = 0;
}
else {
this->position.y++;
}
if (this->position.x > this->rows.at(this->position.y)) {
this->position.x = this->rows.at(this->position.y) - 1;
}
}

View File

@@ -0,0 +1,35 @@
#pragma once
#include <vector>
#include <SFML/Graphics.hpp>
class PlayerCursor {
private:
std::vector<unsigned int> rows;
sf::Vector2u position;
int leftDAS;
int rightDAS;
int upDAS;
int downDAS;
public:
PlayerCursor(std::vector<unsigned int> rows);
void updatePosition();
void goToPosition(const sf::Vector2u& newPosition);
const sf::Vector2u& getPosition() const;
private:
bool shouldMove(int DAS) const;
void moveLeft();
void moveRight();
void moveUp();
void moveDown();
};