Skip to content
Snippets Groups Projects
Commit bdcd843d authored by fdai7466's avatar fdai7466
Browse files

add variable position for context menu

parent 3f727f13
Branches
No related tags found
1 merge request!14Refactoring CMakeLists.txt
......@@ -5,13 +5,16 @@
#include <iostream>
#include <string>
#include "ui/pausemenu.hpp"
#include "ui/ContextMenu.hpp"
namespace advanced_wars {
Level::Level(std::string name, int width, int height, std::vector<Tile> tiles,
std::vector<Building> buildings, std::vector<Unit> units)
: name(name), width(width), height(height), buildings(buildings),
units(units) {};
units(units), context_menu(ContextMenu()), context_menu_active(false){
context_menu.setOptions({"Move", "Info", "Wait"});
}
void Level::render(Engine *engine) {
// Iterate over all events
......@@ -20,10 +23,17 @@ void Level::render(Engine *engine) {
engine->events().pop_front();
}
// Set background color for renderer
if (SDL_SetRenderDrawColor(engine->renderer(), 255, 0, 0, 0)) {
if (SDL_SetRenderDrawColor(engine->renderer(), 0, 0, 0, 255)) {
std::cout << "Could not set render draw color: " << SDL_GetError()
<< std::endl;
}
SDL_RenderClear(engine->renderer());
if(context_menu_active) {
context_menu.render(engine);
}
}
void Level::handleEvent(Engine *engine, SDL_Event &event) {
......@@ -36,8 +46,26 @@ void Level::handleEvent(Engine *engine, SDL_Event &event) {
PauseMenu pauseMenu(0, currentTexture);
engine->push_scene(std::make_shared<PauseMenu>(pauseMenu));
}
if(context_menu_active){
if(event.key.keysym.sym == SDLK_DOWN) {
context_menu.handleEvent(event);
}
if(event.key.keysym.sym == SDLK_UP) {
context_menu.handleEvent(event);
}
if(event.key.keysym.sym == SDLK_RETURN) {
if(context_menu.getSelectedOption() == "Wait"){
context_menu_active = false;
}
}
}
}
if(event.type == SDL_MOUSEBUTTONDOWN) {
context_menu.update(event.button.x, event.button.y);
context_menu_active = true;
}
}
......
......@@ -7,6 +7,7 @@
#include <SDL.h>
#include <string>
#include <vector>
#include "ui/ContextMenu.hpp"
namespace advanced_wars {
......@@ -27,6 +28,8 @@ private:
int height;
std::vector<Building> buildings;
std::vector<Unit> units;
ContextMenu context_menu;
bool context_menu_active;
};
} // namespace advanced_wars
......@@ -25,7 +25,7 @@ int main() {
std::string fullPath = basePath + relativePath;
menu->loadBackground(engine.renderer(), fullPath.c_str());
engine.push_scene(context_menu);
engine.push_scene(menu);
/* Level level("Osnabrück", 20, 20, std::vector<Tile>(),
std::vector<Building>(), std::vector<Unit>());
......
......@@ -15,7 +15,6 @@ namespace advanced_wars {
}
void ContextMenu::render(Engine* engine) {
if (!options.empty()) {
if (TTF_Init() == -1) {
std::cerr << "Failed to initialize TTF: " << TTF_GetError() << std::endl;
......@@ -32,14 +31,13 @@ namespace advanced_wars {
}
SDL_Color white = {255, 255, 255, 255};
SDL_Color yellow = {255, 255, 0, 255};
SDL_Color yellow = {192, 255, 0, 255};
int startY = 100; // Startposition für das Menü
int spacing = 20; // Abstand zwischen den Optionen
//box around options
SDL_SetRenderDrawColor(engine->renderer(), 0, 0, 255, 128);
SDL_Rect box = {190, startY - 3, 50, static_cast<int>(options.size() * spacing)};
SDL_SetRenderDrawColor(engine->renderer(), 0, 0, 255, 255);
SDL_Rect box = {x, y - 3, 50, static_cast<int>(options.size() * spacing)};
SDL_RenderFillRect(engine->renderer(), &box);
SDL_SetRenderDrawColor(engine->renderer(), 0, 0, 0, 255);
......@@ -51,7 +49,7 @@ namespace advanced_wars {
}
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(engine->renderer(), textSurface);
SDL_Rect textRect = {200, startY + static_cast<int>(i * spacing), textSurface->w, textSurface->h};
SDL_Rect textRect = {x+10, y + static_cast<int>(i * spacing), textSurface->w, textSurface->h};
SDL_RenderCopy(engine->renderer(), textTexture, nullptr, &textRect);
SDL_DestroyTexture(textTexture);
......@@ -69,10 +67,17 @@ namespace advanced_wars {
selectedOption = (selectedOption + 1) % options.size();
} else if (event.key.keysym.sym == SDLK_UP) {
selectedOption = (selectedOption - 1 + options.size()) % options.size();
} else if (event.key.keysym.sym == SDLK_RETURN) {
std::cout << "Selected option: " << options[selectedOption] << std::endl;
}
}
}
std::string ContextMenu::getSelectedOption() {
return options[selectedOption];
}
void ContextMenu::update(int x, int y) {
this->x = x;
this->y = y;
}
}
......@@ -13,6 +13,8 @@ class ContextMenu : public Scene {
private:
size_t selectedOption;
std::vector<std::string> options;
int x;
int y;
public:
ContextMenu();
......@@ -23,6 +25,10 @@ public:
void handleEvent(SDL_Event& event);
void update(int x, int y);
std::string getSelectedOption();
~ContextMenu();
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment