mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 22:41:48 +01:00
Update Screen system
Added FuelScreen (Working buttons) Added SellScreen (Nothing working)
This commit is contained in:
@@ -1 +1,37 @@
|
||||
#include "Button.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "colors.h"
|
||||
#include "utils.h"
|
||||
Button::Button(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager): m_Position(pos), m_Size(size) {
|
||||
m_Texture = manager->GetTexture(filePath);
|
||||
if(size.x == 0 && size.y == 0) {
|
||||
m_Size = Point2f{float(m_Texture->GetWidth()), float(m_Texture->GetHeight())};
|
||||
}
|
||||
std::cout << "Button created" << '\n';
|
||||
}
|
||||
Button::~Button() {
|
||||
std::cout << "Button destroyed" << '\n';
|
||||
}
|
||||
void Button::Draw() const {
|
||||
Rectf dest = Rectf(m_Position, m_Size);
|
||||
Rectf src = Rectf(0, 0, m_Texture->GetWidth(), m_Texture->GetHeight());
|
||||
if(m_IsHovered) {
|
||||
m_Texture->Draw(dest, src, false);
|
||||
}
|
||||
}
|
||||
void Button::Update(float elapsedSec) {
|
||||
Point2f mousePos = utils::GetMousePos();
|
||||
Rectf buttonRect = Rectf(m_Position, m_Size);
|
||||
|
||||
this->m_IsHovered = utils::IsPointInRect(mousePos, buttonRect);
|
||||
m_IsPressed = m_IsHovered && utils::IsMouseButtonDown(SDL_BUTTON_LEFT);
|
||||
|
||||
if(m_IsPressed) {
|
||||
m_OnClick();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,22 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
#include "Texture.h"
|
||||
#include "../TextureManager.h"
|
||||
|
||||
class Button
|
||||
{
|
||||
public:
|
||||
Button() = default;
|
||||
Button(const std::string& filePath, Point2f pos, Point2f size);
|
||||
Button(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager);
|
||||
~Button();
|
||||
void Draw() const;
|
||||
void Update(float elapsedSec);
|
||||
|
||||
void SetOnClick(std::function<void(void)> onClick) {
|
||||
m_OnClick = onClick;
|
||||
}
|
||||
|
||||
private:
|
||||
Texture* m_Texture{ nullptr };
|
||||
@@ -16,4 +25,6 @@ private:
|
||||
|
||||
bool m_IsHovered{ false };
|
||||
bool m_IsPressed{ false };
|
||||
|
||||
std::function<void(void)> m_OnClick{ []() { std::cout << "Button not implemented" << std::endl; } };
|
||||
};
|
||||
|
||||
@@ -6,9 +6,16 @@ Screen::Screen(const std::string& filePath, Point2f pos, Point2f size,TextureMan
|
||||
Screen::~Screen() {
|
||||
}
|
||||
void Screen::Update(float elapsedSecs) {
|
||||
for(Button* b : m_Buttons) {
|
||||
b->Update(elapsedSecs);
|
||||
}
|
||||
}
|
||||
void Screen::Draw() const {
|
||||
Rectf dest = Rectf(m_Position, m_Size);
|
||||
Rectf src = Rectf(0,0, m_Background->GetWidth(), m_Background->GetHeight());
|
||||
m_Background->Draw(dest, src, false);
|
||||
|
||||
for(Button* b: m_Buttons) {
|
||||
b->Draw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
#include "Button.h"
|
||||
#include "structs.h"
|
||||
#include "Texture.h"
|
||||
#include "../TextureManager.h"
|
||||
@@ -9,18 +12,17 @@ public:
|
||||
Screen() = default;
|
||||
Screen(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager);
|
||||
|
||||
~Screen();
|
||||
virtual ~Screen();
|
||||
|
||||
void setActive(bool active) { m_Active = active; }
|
||||
void toggleActive() { m_Active = !m_Active; }
|
||||
void AddButton(Button* button) { m_Buttons.push_back(button); }
|
||||
|
||||
void Update(float elapsedSecs);
|
||||
void Draw() const;
|
||||
virtual void Update(float elapsedSecs);
|
||||
virtual void Draw() const;
|
||||
private:
|
||||
Point2f m_Position;
|
||||
Point2f m_Size;
|
||||
|
||||
Texture* m_Background{ nullptr };
|
||||
|
||||
bool m_Active{ false };
|
||||
std::vector<Button*> m_Buttons;
|
||||
};
|
||||
|
||||
10
Game/Gui/Screens/FuelScreen.cpp
Normal file
10
Game/Gui/Screens/FuelScreen.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "FuelScreen.h"
|
||||
FuelScreen::FuelScreen(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager): Screen(filePath, pos, size, manager)
|
||||
{
|
||||
}
|
||||
void FuelScreen::Draw() const {
|
||||
Screen::Draw();
|
||||
}
|
||||
void FuelScreen::Update(float elapsedSecs) {
|
||||
Screen::Update(elapsedSecs);
|
||||
}
|
||||
13
Game/Gui/Screens/FuelScreen.h
Normal file
13
Game/Gui/Screens/FuelScreen.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "../Screen.h"
|
||||
|
||||
class FuelScreen : public Screen
|
||||
{
|
||||
public:
|
||||
|
||||
FuelScreen(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager);
|
||||
|
||||
virtual void Draw() const override;
|
||||
virtual void Update(float elapsedSecs) override;
|
||||
|
||||
};
|
||||
77
Game/Gui/Screens/ScreenManager.cpp
Normal file
77
Game/Gui/Screens/ScreenManager.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "ScreenManager.h"
|
||||
|
||||
#include "FuelScreen.h"
|
||||
#include "utils.h"
|
||||
ScreenManager* ScreenManager::m_pInstance = nullptr;
|
||||
|
||||
Screen* ScreenManager::Fuel{ nullptr };
|
||||
Screen* ScreenManager::SellScreen{ nullptr };
|
||||
|
||||
ScreenManager* ScreenManager::GetInstance() {
|
||||
|
||||
if(m_pInstance == nullptr) {
|
||||
m_pInstance = new ScreenManager();
|
||||
}
|
||||
if(!m_pInstance->m_AreScreensInitialized) {
|
||||
m_pInstance->InitializeScreens(); //TODO: Ask if this a hack
|
||||
m_pInstance->m_AreScreensInitialized = true;
|
||||
}
|
||||
return m_pInstance;
|
||||
}
|
||||
|
||||
void ScreenManager::OpenScreen(Screen* screen) {
|
||||
if(m_IsScreenOpen == false) {
|
||||
m_currentScreen = screen;
|
||||
m_IsScreenOpen = true;
|
||||
}
|
||||
}
|
||||
void ScreenManager::CloseScreen() {
|
||||
if(m_IsScreenOpen == true) {
|
||||
m_IsScreenOpen = false;
|
||||
m_currentScreen = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
void ScreenManager::InitializeScreens() {
|
||||
Point2f fuelScreenSize = Point2f { 492, 396 };
|
||||
Point2f fuelScreenCenter = Point2f { utils::GetViewport().x / 2 - fuelScreenSize.x / 2, utils::GetViewport().y / 2 - fuelScreenSize.y / 2 };
|
||||
Fuel = new FuelScreen { "gui/fuel/background.png", fuelScreenCenter, fuelScreenSize, TextureManager::GetInstance() };
|
||||
|
||||
Point2f closeButtonOffset = Point2f { 460, 396 - 14 };
|
||||
Point2f closeButtonPos = fuelScreenCenter + closeButtonOffset;
|
||||
closeButtonPos.y -= 18;
|
||||
Button* closeFuelButton = new Button { "gui/close.png", closeButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
closeFuelButton->SetOnClick([this]() { CloseScreen(); });
|
||||
Fuel->AddButton(closeFuelButton);
|
||||
|
||||
Point2f oneDollarButtonPos = Point2f { 451, 287 };
|
||||
oneDollarButtonPos += fuelScreenCenter;
|
||||
Button* fiveDollarButton = new Button { "gui/fuel/5dollars.png", oneDollarButtonPos , Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(fiveDollarButton);
|
||||
|
||||
Point2f tenDollarButtonPos = oneDollarButtonPos + Point2f { 113, -1 };
|
||||
tenDollarButtonPos += fuelScreenCenter;
|
||||
Button* tenDollarButton = new Button { "gui/fuel/10dollars.png", tenDollarButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(tenDollarButton);
|
||||
|
||||
Point2f twentyFiveDollarButtonPos = oneDollarButtonPos + Point2f { 0, -89 };
|
||||
twentyFiveDollarButtonPos += fuelScreenCenter;
|
||||
Button* twentyFiveDollarButton = new Button { "gui/fuel/25dollars.png", twentyFiveDollarButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(twentyFiveDollarButton);
|
||||
|
||||
Point2f fiftyDollarButtonPos = twentyFiveDollarButtonPos + Point2f { 114, 0 };
|
||||
Button* fiftyDollarButton = new Button { "gui/fuel/50dollars.png", fiftyDollarButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(fiftyDollarButton);
|
||||
|
||||
Point2f fillTankButtonPos = Point2f { 450, 108 };
|
||||
fillTankButtonPos += fuelScreenCenter;
|
||||
Button* fillTankButton = new Button { "gui/fuel/fillTank.png", fillTankButtonPos, Point2f{0,0}, TextureManager::GetInstance() };
|
||||
Fuel->AddButton(fillTankButton);
|
||||
|
||||
Point2f sellScreenSize = Point2f { 533, 398 };
|
||||
Point2f sellScreenCenter = Point2f { utils::GetViewport().x / 2 - sellScreenSize.x / 2, utils::GetViewport().y / 2 - sellScreenSize.y / 2 };
|
||||
SellScreen = new Screen { "gui/sell/background.png", sellScreenCenter, sellScreenSize, TextureManager::GetInstance() };
|
||||
|
||||
//m_Button = Button { "gui/close.png", closeButtonPos, closeButtonSize, TextureManager::GetInstance() };
|
||||
}
|
||||
|
||||
31
Game/Gui/Screens/ScreenManager.h
Normal file
31
Game/Gui/Screens/ScreenManager.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include "../Screen.h"
|
||||
|
||||
|
||||
|
||||
class ScreenManager
|
||||
{
|
||||
public:
|
||||
static ScreenManager* GetInstance();
|
||||
|
||||
void OpenScreen(Screen* screen);
|
||||
void CloseScreen();
|
||||
|
||||
Screen* GetCurrentScreen() { return m_currentScreen; }
|
||||
|
||||
static ScreenManager* m_pInstance;
|
||||
|
||||
|
||||
static Screen* Fuel;
|
||||
static Screen* SellScreen;
|
||||
private:
|
||||
ScreenManager() = default;
|
||||
|
||||
void InitializeScreens();
|
||||
|
||||
bool m_IsScreenOpen{ false };
|
||||
Screen* m_currentScreen;
|
||||
|
||||
bool m_AreScreensInitialized{ false };
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user