Main menu finished
This commit is contained in:
+45
-15
@@ -36,6 +36,7 @@ public:
|
||||
: EventBroker(eventBroker)
|
||||
, ResourceManager(resourceManager)
|
||||
, Rectangle()
|
||||
, m_Parent(nullptr)
|
||||
, m_Name("UIParent")
|
||||
, m_Layer(0)
|
||||
, m_Hidden(false)
|
||||
@@ -46,12 +47,28 @@ public:
|
||||
: m_Name(name)
|
||||
, m_Layer(0)
|
||||
, m_Hidden(false)
|
||||
{ SetParent(std::shared_ptr<Frame>(parent)); Initialize(); }
|
||||
{ SetParent(parent); Initialize(); }
|
||||
|
||||
~Frame()
|
||||
{
|
||||
/*for (auto layer : m_Children)
|
||||
{
|
||||
for (auto child : layer.second)
|
||||
{
|
||||
delete child.second;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_Parent)
|
||||
{
|
||||
m_Parent->RemoveChild(this);
|
||||
}*/
|
||||
}
|
||||
|
||||
::RenderQueuePair RenderQueue;
|
||||
|
||||
std::shared_ptr<Frame> Parent() const { return m_Parent; }
|
||||
void SetParent(std::shared_ptr<Frame> parent)
|
||||
Frame* Parent() const { return m_Parent; }
|
||||
void SetParent(Frame* parent)
|
||||
{
|
||||
if (parent == nullptr)
|
||||
{
|
||||
@@ -62,13 +79,13 @@ public:
|
||||
Width = parent->Width;
|
||||
Height = parent->Height;
|
||||
m_Layer = parent->Layer() + 1;
|
||||
parent->AddChild(std::shared_ptr<Frame>(this));
|
||||
parent->AddChild(this);
|
||||
m_Parent = parent;
|
||||
EventBroker = parent->EventBroker;
|
||||
ResourceManager = parent->ResourceManager;
|
||||
}
|
||||
|
||||
void AddChild(std::shared_ptr<Frame> child)
|
||||
void AddChild(Frame* child)
|
||||
{
|
||||
m_Children[child->m_Layer].insert(std::make_pair(child->Name(), child));
|
||||
if (m_Parent)
|
||||
@@ -77,7 +94,19 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
typedef std::map<std::string, std::shared_ptr<Frame>>::const_iterator FrameChildrenIterator;
|
||||
void RemoveChild(Frame* child)
|
||||
{
|
||||
auto it = m_Children.find(child->m_Layer);
|
||||
if (it != m_Children.end())
|
||||
{
|
||||
m_Children.erase(it);
|
||||
}
|
||||
|
||||
if (m_Parent)
|
||||
{
|
||||
m_Parent->RemoveChild(child);
|
||||
}
|
||||
}
|
||||
|
||||
std::string Name() const { return m_Name; }
|
||||
void SetName(std::string val) { m_Name = val; }
|
||||
@@ -89,8 +118,14 @@ public:
|
||||
else
|
||||
return m_Hidden;
|
||||
}
|
||||
void Hide() { m_Hidden = true; }
|
||||
void Show() { m_Hidden = false; }
|
||||
|
||||
bool Visible() const
|
||||
{
|
||||
return !Hidden();
|
||||
}
|
||||
|
||||
virtual void Hide() { m_Hidden = true; }
|
||||
virtual void Show() { m_Hidden = false; }
|
||||
|
||||
int Left() const override
|
||||
{
|
||||
@@ -144,9 +179,6 @@ public:
|
||||
|
||||
void UpdateLayered(double dt)
|
||||
{
|
||||
if (this->Hidden())
|
||||
return;
|
||||
|
||||
// Update ourselves
|
||||
this->Update(dt);
|
||||
|
||||
@@ -157,8 +189,6 @@ public:
|
||||
for (auto &pairChild : children)
|
||||
{
|
||||
auto child = pairChild.second;
|
||||
if (child->Hidden())
|
||||
continue;
|
||||
child->Update(dt);
|
||||
}
|
||||
}
|
||||
@@ -200,8 +230,8 @@ protected:
|
||||
int m_Layer;
|
||||
bool m_Hidden;
|
||||
|
||||
std::shared_ptr<Frame> m_Parent;
|
||||
typedef std::multimap<std::string, std::shared_ptr<Frame>> Children_t; // name -> frame
|
||||
Frame* m_Parent;
|
||||
typedef std::multimap<std::string, Frame*> Children_t; // name -> frame
|
||||
std::map<int, Children_t> m_Children; // layer -> Children_t
|
||||
|
||||
virtual bool OnKeyDown(const Events::KeyDown &event) { return false; }
|
||||
|
||||
+35
-11
@@ -2,13 +2,15 @@
|
||||
#define GUI_GameFrame_h__
|
||||
|
||||
#include "GUI/Frame.h"
|
||||
#include "GUI/MainMenuFrame.h"
|
||||
#include "GUI/WorldFrame.h"
|
||||
#include "GUI/Viewport.h"
|
||||
#include "GUI/TextureFrame.h"
|
||||
#include "GUI/PlayerHUD.h"
|
||||
#include "VehicleSelection.h"
|
||||
|
||||
#include "GameWorld.h"
|
||||
#include "Events/GameStart.h"
|
||||
#include "Events/GameOver.h"
|
||||
|
||||
namespace GUI
|
||||
{
|
||||
@@ -18,37 +20,59 @@ class GameFrame : public Frame
|
||||
public:
|
||||
GameFrame(Frame* parent, std::string name)
|
||||
: Frame(parent, name)
|
||||
, m_GameOver(false)
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &GUI::GameFrame::OnGameStart);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EGameOver, &GUI::GameFrame::OnGameOver);
|
||||
|
||||
m_World = std::make_shared<GameWorld>(EventBroker, ResourceManager);
|
||||
auto worldFrame = new WorldFrame(this, "GameWorldFrame", m_World);
|
||||
m_WorldFrame = new WorldFrame(this, "GameWorldFrame", m_World);
|
||||
{
|
||||
vp1 = new Viewport(worldFrame, "Viewport1", m_World);
|
||||
vp1 = new Viewport(m_WorldFrame, "Viewport1", m_World);
|
||||
vp1->X = 0;
|
||||
vp1->Width = this->Width / 2.f;
|
||||
//new PlayerHUD(vp1, "PlayerHUD", m_World, 1);
|
||||
new VehicleSelection(vp1, "VehicleSelection", m_World, 1);
|
||||
new PlayerHUD(vp1, "PlayerHUD", m_World, 1);
|
||||
|
||||
vp2 = new Viewport(worldFrame, "Viewport2", m_World);
|
||||
vp2 = new Viewport(m_WorldFrame, "Viewport2", m_World);
|
||||
vp2->X = vp1->Right();
|
||||
vp2->Width = this->Width / 2.f;
|
||||
//new PlayerHUD(vp2, "PlayerHUD", m_World, 2);
|
||||
new VehicleSelection(vp2, "VehicleSelection", m_World, 2);
|
||||
new PlayerHUD(vp2, "PlayerHUD", m_World, 2);
|
||||
|
||||
|
||||
m_FreeCamViewport = new Viewport(worldFrame, "ViewportFreeCam", m_World);
|
||||
m_FreeCamViewport = new Viewport(m_WorldFrame, "ViewportFreeCam", m_World);
|
||||
m_FreeCamViewport->Hide();
|
||||
}
|
||||
m_WorldFrame->Hide();
|
||||
m_MainMenuFrame = new MainMenuFrame(this, "MainMenu");
|
||||
|
||||
m_World->Initialize();
|
||||
}
|
||||
|
||||
private:
|
||||
EventRelay<Frame, Events::KeyUp> m_EKeyUp;
|
||||
EventRelay<Frame, Events::GameStart> m_EGameStart;
|
||||
bool OnGameStart(const Events::GameStart &event)
|
||||
{
|
||||
m_WorldFrame->Show();
|
||||
|
||||
return true;
|
||||
}
|
||||
EventRelay<Frame, Events::GameOver> m_EGameOver;
|
||||
bool OnGameOver(const Events::GameOver &event)
|
||||
{
|
||||
m_GameOver = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<GameWorld> m_World;
|
||||
|
||||
MainMenuFrame* m_MainMenuFrame;
|
||||
WorldFrame* m_WorldFrame;
|
||||
Viewport* vp1;
|
||||
Viewport* vp2;
|
||||
Viewport* m_FreeCamViewport;
|
||||
|
||||
bool m_GameOver;
|
||||
|
||||
bool OnKeyUp(const Events::KeyUp &event) override
|
||||
{
|
||||
if (event.KeyCode == GLFW_KEY_1)
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef GUI_GameOverFrame_h__
|
||||
#define GUI_GameOverFrame_h__
|
||||
|
||||
#include "GUI/Frame.h"
|
||||
#include "GUI/HealthOverlay.h"
|
||||
|
||||
namespace GUI
|
||||
{
|
||||
|
||||
class GameOverFrame : public Frame
|
||||
{
|
||||
public:
|
||||
GameOverFrame(Frame* parent, std::string name)
|
||||
: Frame(parent, name)
|
||||
{
|
||||
//Hide();
|
||||
m_Background = new TextureFrame(this, "Background");
|
||||
m_Background->SetTexture("Textures/GUI/black.png");
|
||||
m_Background->SetColor(glm::vec4(1.f, 1.f, 1.f, 0.6f));
|
||||
m_Text = new TextureFrame(this, "Message");
|
||||
m_Text->SetTexture("Textures/GUI/GloriousVictory.png");
|
||||
}
|
||||
|
||||
void Show(bool victory)
|
||||
{
|
||||
if (victory)
|
||||
m_Text->SetTexture("Textures/GUI/GloriousVictory.png");
|
||||
else
|
||||
m_Text->SetTexture("Textures/GUI/ShamefulDefeat.png");
|
||||
|
||||
Frame::Show();
|
||||
}
|
||||
|
||||
protected:
|
||||
bool m_Victory;
|
||||
TextureFrame* m_Background;
|
||||
TextureFrame* m_Text;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GUI_GameOverFrame_h__
|
||||
@@ -0,0 +1,100 @@
|
||||
#ifndef GUI_MainMenuFrame_h__
|
||||
#define GUI_MainMenuFrame_h__
|
||||
|
||||
#include "GUI/Frame.h"
|
||||
#include "GUI/HealthOverlay.h"
|
||||
|
||||
#include "Events/GameStart.h"
|
||||
|
||||
namespace GUI
|
||||
{
|
||||
|
||||
class MainMenuFrame : public Frame
|
||||
{
|
||||
public:
|
||||
MainMenuFrame(Frame* parent, std::string name)
|
||||
: Frame(parent, name)
|
||||
{
|
||||
m_CurrentSelection = 0;
|
||||
|
||||
//Hide();
|
||||
m_Background = new TextureFrame(this, "Background");
|
||||
m_Background->SetTexture("Textures/GUI/MainMenu/BackGround.png");
|
||||
|
||||
m_Buttons = new TextureFrame(this, "Message");
|
||||
m_Buttons->SetTexture("Textures/GUI/MainMenu/Buttons.png");
|
||||
m_CurrentCoordinate = -m_SelectionCoordinates[0] * Scale();
|
||||
m_TargetCoordinate = m_CurrentCoordinate;
|
||||
m_Buttons->X = m_CurrentCoordinate.x;
|
||||
m_Buttons->Y = m_CurrentCoordinate.y;
|
||||
}
|
||||
|
||||
void Update(double dt) override
|
||||
{
|
||||
if (Hidden())
|
||||
return;
|
||||
|
||||
glm::vec2 posDiff = m_TargetCoordinate - m_CurrentCoordinate;
|
||||
m_CurrentCoordinate += posDiff * 4.f * (float)dt;
|
||||
|
||||
m_Buttons->X = m_CurrentCoordinate.x;
|
||||
m_Buttons->Y = m_CurrentCoordinate.y;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool m_Victory;
|
||||
TextureFrame* m_Background;
|
||||
TextureFrame* m_Buttons;
|
||||
|
||||
int m_CurrentSelection;
|
||||
static glm::vec2 m_SelectionCoordinates[2];
|
||||
glm::vec2 m_CurrentCoordinate;
|
||||
glm::vec2 m_TargetCoordinate;
|
||||
|
||||
bool OnCommand(const Events::InputCommand &event) override
|
||||
{
|
||||
if (Hidden())
|
||||
return false;
|
||||
|
||||
const float deadzone = 0.5f;
|
||||
|
||||
if (event.Command == "interface_horizontal")
|
||||
{
|
||||
if (event.Value > deadzone)
|
||||
m_CurrentSelection++;
|
||||
else if (event.Value < -deadzone)
|
||||
m_CurrentSelection--;
|
||||
}
|
||||
m_CurrentSelection = (m_CurrentSelection < 0) ? 0 : ((m_CurrentSelection > 1) ? 1 : m_CurrentSelection);
|
||||
m_TargetCoordinate = -m_SelectionCoordinates[m_CurrentSelection] * Scale();
|
||||
|
||||
if (event.Command == "interface_confirm" && event.Value > 0)
|
||||
{
|
||||
// Play
|
||||
if (m_CurrentSelection == 0)
|
||||
{
|
||||
Hide();
|
||||
Events::GameStart e;
|
||||
EventBroker->Publish(e);
|
||||
}
|
||||
// Quit
|
||||
else if (m_CurrentSelection == 1)
|
||||
{
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
glm::vec2 MainMenuFrame::m_SelectionCoordinates[2] = {
|
||||
glm::vec2(0, 0),
|
||||
glm::vec2(193, 0),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // GUI_MainMenuFrame_h__
|
||||
@@ -3,6 +3,11 @@
|
||||
|
||||
#include "GUI/Frame.h"
|
||||
#include "GUI/HealthOverlay.h"
|
||||
#include "GUI/VehicleSelection.h"
|
||||
#include "GUI/GameOverFrame.h"
|
||||
|
||||
#include "Events/GameOver.h"
|
||||
#include "Components/Player.h"
|
||||
|
||||
namespace GUI
|
||||
{
|
||||
@@ -15,7 +20,10 @@ namespace GUI
|
||||
, m_World(world)
|
||||
, m_PlayerID(playerID)
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EGameOver, &GUI::PlayerHUD::OnGameOver)
|
||||
|
||||
m_HealthOverlay = new HealthOverlay(this, "HealthOverlay", m_World, m_PlayerID);
|
||||
|
||||
m_Crosshair = new TextureFrame(this, "Crosshair");
|
||||
m_Crosshair->Width = 45;
|
||||
m_Crosshair->Height = 45;
|
||||
@@ -23,6 +31,10 @@ namespace GUI
|
||||
m_Crosshair->Y = this->Height / 2.f - m_Crosshair->Height / 2.f;
|
||||
m_Crosshair->SetTexture("Textures/GUI/crosshair.png");
|
||||
m_Crosshair->SetColor(glm::vec4(1.f, 1.f, 1.f, 0.75f));
|
||||
|
||||
m_VehicleSelection = new VehicleSelection(this, "VehicleSelection", m_World, m_PlayerID);
|
||||
|
||||
m_GameOverFrame = new GameOverFrame(this, "GameOver");
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -31,6 +43,42 @@ namespace GUI
|
||||
|
||||
HealthOverlay* m_HealthOverlay;
|
||||
TextureFrame* m_Crosshair;
|
||||
VehicleSelection* m_VehicleSelection;
|
||||
GameOverFrame* m_GameOverFrame;
|
||||
|
||||
EventRelay<Frame, Events::GameOver> m_EGameOver;
|
||||
bool OnGameOver(const Events::GameOver &event)
|
||||
{
|
||||
auto playerComponent = m_World->GetComponent<Components::Player>(event.Winner);
|
||||
if (!playerComponent)
|
||||
{
|
||||
LOG_ERROR("Winner is not a valid player!");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_PlayerID == playerComponent->ID)
|
||||
m_GameOverFrame->Show(true);
|
||||
else
|
||||
m_GameOverFrame->Show(false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OnCommand(const Events::InputCommand &event)
|
||||
{
|
||||
if (event.PlayerID != m_PlayerID)
|
||||
return false;
|
||||
|
||||
if (event.Command == "interface_confirm" && event.Value > 0)
|
||||
{
|
||||
if (m_GameOverFrame->Visible())
|
||||
{
|
||||
m_GameOverFrame->Hide();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ namespace GUI
|
||||
|
||||
void Update(double dt) override
|
||||
{
|
||||
if (Hidden())
|
||||
return;
|
||||
|
||||
glm::vec2 posDiff = m_TargetCoordinate - m_CurrentCoordinate;
|
||||
m_CurrentCoordinate += posDiff * 2.f * (float)dt;
|
||||
|
||||
@@ -77,28 +80,30 @@ namespace GUI
|
||||
if (event.PlayerID != m_PlayerID)
|
||||
return false;
|
||||
|
||||
const float deadzone = 0.5f;
|
||||
|
||||
// Menu movement
|
||||
if (event.Command == "interface_horizontal" || event.Command == "interface_vertical")
|
||||
{
|
||||
// Horizontal scrolling
|
||||
if (event.Command == "interface_horizontal")
|
||||
{
|
||||
if (event.Value > 0)
|
||||
if (event.Value > deadzone)
|
||||
m_CurrentSelection++;
|
||||
else if (event.Value < 0)
|
||||
else if (event.Value < -deadzone)
|
||||
m_CurrentSelection--;
|
||||
}
|
||||
// Vertical scrolling to switch between "rows" in an intuitive way
|
||||
else if (event.Command == "interface_vertical")
|
||||
{
|
||||
if (event.Value > 0)
|
||||
if (event.Value > deadzone)
|
||||
{
|
||||
if (m_CurrentSelection == 0)
|
||||
m_CurrentSelection++;
|
||||
else if (m_CurrentSelection == 3)
|
||||
m_CurrentSelection--;
|
||||
}
|
||||
else if (event.Value < 0)
|
||||
else if (event.Value < -deadzone)
|
||||
{
|
||||
if (m_CurrentSelection == 1)
|
||||
m_CurrentSelection--;
|
||||
|
||||
@@ -145,7 +145,7 @@ private:
|
||||
auto itpair = m_Children[m_Layer + 1].equal_range(event.ViewportFrame);
|
||||
for (auto it = itpair.first; it != itpair.second; ++it)
|
||||
{
|
||||
auto viewportFrame = std::dynamic_pointer_cast<GUI::Viewport>(it->second);
|
||||
auto viewportFrame = dynamic_cast<GUI::Viewport*>(it->second);
|
||||
if (!viewportFrame)
|
||||
continue;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user