I was copying Frame, MainMenuFrame, TextureFrame...
Here you go.
This commit is contained in:
@@ -39,8 +39,8 @@ public:
|
||||
void SetFullscreen(bool fullscreen) { m_Fullscreen = fullscreen; }
|
||||
bool VSYNC() const { return m_VSYNC; }
|
||||
void SetVSYNC(bool vsync) { m_VSYNC = vsync; }
|
||||
//void SetViewport(const Rectangle& viewport) { m_Viewport = viewport; }
|
||||
//void SetScissor(const Rectangle& scissor) { m_Scissor = scissor; }
|
||||
void SetViewport(const Rectangle& viewport) { m_Viewport = viewport; }
|
||||
void SetScissor(const Rectangle& scissor) { m_Scissor = scissor; }
|
||||
const dd::Camera* Camera() const { return m_Camera; }
|
||||
void SetCamera(const dd::Camera* camera)
|
||||
{
|
||||
@@ -53,13 +53,14 @@ public:
|
||||
|
||||
void Initialize();
|
||||
void Draw(RenderQueueCollection& rq);
|
||||
void DrawFrame(RenderQueuePair& rq);
|
||||
|
||||
private:
|
||||
Rectangle m_Resolution = Rectangle(1280, 720);
|
||||
bool m_Fullscreen = false;
|
||||
bool m_VSYNC = false;
|
||||
//Rectangle m_Viewport;
|
||||
//Rectangle m_Scissor;
|
||||
Rectangle m_Viewport;
|
||||
Rectangle m_Scissor;
|
||||
std::unique_ptr<dd::Camera> m_DefaultCamera = nullptr;
|
||||
|
||||
const dd::Camera* m_Camera = nullptr;
|
||||
|
||||
@@ -0,0 +1,258 @@
|
||||
//
|
||||
// Created by Adniklastrator on 2015-09-17.
|
||||
//
|
||||
|
||||
#ifndef DAYDREAM_FRAME_H
|
||||
#define DAYDREAM_FRAME_H
|
||||
|
||||
#include "Core/Util/Rectangle.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/EKeyDown.h"
|
||||
#include "Core/EKeyUp.h"
|
||||
#include "Core/ResourceManager.h"
|
||||
#include "Core/Renderer.h"
|
||||
#include "Core/RenderQueue.h"
|
||||
#include "Core/Texture.h"
|
||||
#include "Input/EInputCommand.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
class Frame : public Rectangle
|
||||
{
|
||||
public:
|
||||
enum class Anchor
|
||||
{
|
||||
Left,
|
||||
Right,
|
||||
Top,
|
||||
Bottom
|
||||
};
|
||||
|
||||
static const int BaseWidth = 1280;
|
||||
static const int BaseHeight = 720;
|
||||
|
||||
// Set up a base frame with an event broker
|
||||
Frame(std::shared_ptr<dd::EventBroker> eventBroker, std::shared_ptr<dd::ResourceManager> resourceManager)
|
||||
: EventBroker(eventBroker)
|
||||
, ResourceManager(resourceManager)
|
||||
, Rectangle()
|
||||
, m_Parent(nullptr)
|
||||
, m_Name("UIParent")
|
||||
, m_Layer(0)
|
||||
, m_Hidden(false)
|
||||
{ Initialize(); }
|
||||
|
||||
// Create a frame as a child
|
||||
Frame(Frame* parent, std::string name)
|
||||
: m_Name(name)
|
||||
, m_Layer(0)
|
||||
, m_Hidden(false)
|
||||
{ SetParent(parent); Initialize(); }
|
||||
|
||||
~Frame()
|
||||
{
|
||||
/*for (auto layer : m_Children)
|
||||
{
|
||||
for (auto child : layer.second)
|
||||
{
|
||||
delete child.second;
|
||||
}
|
||||
}
|
||||
if (m_Parent)
|
||||
{
|
||||
m_Parent->RemoveChild(this);
|
||||
}*/
|
||||
}
|
||||
|
||||
dd::RenderQueuePair RenderQueue;
|
||||
|
||||
Frame* Parent() const { return m_Parent; }
|
||||
void SetParent(Frame* parent)
|
||||
{
|
||||
if (parent == nullptr)
|
||||
{
|
||||
LOG_ERROR("Failed to create frame \"%s\": Invalid parent", m_Name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
Width = parent->Width;
|
||||
Height = parent->Height;
|
||||
m_Layer = parent->Layer() + 1;
|
||||
parent->AddChild(this);
|
||||
m_Parent = parent;
|
||||
EventBroker = parent->EventBroker;
|
||||
ResourceManager = parent->ResourceManager;
|
||||
}
|
||||
|
||||
void AddChild(Frame* child)
|
||||
{
|
||||
m_Children[child->m_Layer].insert(std::make_pair(child->Name(), child));
|
||||
if (m_Parent)
|
||||
{
|
||||
m_Parent->AddChild(child);
|
||||
}
|
||||
}
|
||||
|
||||
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; }
|
||||
int Layer() const { return m_Layer; }
|
||||
bool Hidden() const
|
||||
{
|
||||
if (m_Parent)
|
||||
return m_Parent->Hidden() || m_Hidden;
|
||||
else
|
||||
return m_Hidden;
|
||||
}
|
||||
|
||||
bool Visible() const
|
||||
{
|
||||
return !Hidden();
|
||||
}
|
||||
|
||||
virtual void Hide() { m_Hidden = true; }
|
||||
virtual void Show() { m_Hidden = false; }
|
||||
|
||||
int Left() const override
|
||||
{
|
||||
if (m_Parent)
|
||||
return m_Parent->Left() + X;
|
||||
else
|
||||
return X;
|
||||
}
|
||||
int Right() const override
|
||||
{
|
||||
if (m_Parent)
|
||||
return std::min(m_Parent->Right(), Left() + Width);
|
||||
else
|
||||
return Left() + Width;
|
||||
}
|
||||
int Top() const override
|
||||
{
|
||||
if (m_Parent)
|
||||
return m_Parent->Top() + Y;
|
||||
else
|
||||
return Y;
|
||||
}
|
||||
int Bottom() const override
|
||||
{
|
||||
if (m_Parent)
|
||||
return std::min(m_Parent->Bottom(), Top() + Height);
|
||||
else
|
||||
return Top() + Height;
|
||||
}
|
||||
|
||||
glm::vec2 Scale()
|
||||
{
|
||||
if (m_Parent)
|
||||
return m_Parent->Scale();
|
||||
else
|
||||
return glm::vec2(Width, Height) / glm::vec2(BaseWidth, BaseHeight);
|
||||
}
|
||||
|
||||
Rectangle AbsoluteRectangle()
|
||||
{
|
||||
int left = Left();
|
||||
if (m_Parent)
|
||||
left = std::max(left, m_Parent->Left());
|
||||
int top = Top();
|
||||
if (m_Parent)
|
||||
top = std::max(top, m_Parent->Top());
|
||||
int width = Right() - left;
|
||||
int height = Bottom() - top;
|
||||
return Rectangle(left, top, width, height);
|
||||
}
|
||||
|
||||
void UpdateLayered(double dt)
|
||||
{
|
||||
// Update ourselves
|
||||
this->Update(dt);
|
||||
|
||||
// Update children
|
||||
for (auto &pairLayer : m_Children)
|
||||
{
|
||||
auto children = pairLayer.second;
|
||||
for (auto &pairChild : children)
|
||||
{
|
||||
auto child = pairChild.second;
|
||||
child->Update(dt);
|
||||
}
|
||||
}
|
||||
}
|
||||
virtual void Update(double dt) { }
|
||||
|
||||
void DrawLayered(std::shared_ptr<Renderer> renderer)
|
||||
{
|
||||
if (this->Hidden())
|
||||
return;
|
||||
|
||||
// Draw ourselves
|
||||
renderer->SetScissor(AbsoluteRectangle());
|
||||
this->Draw(renderer);
|
||||
|
||||
// Draw children
|
||||
for (auto &pairLayer : m_Children)
|
||||
{
|
||||
auto children = pairLayer.second;
|
||||
for (auto &pairChild : children)
|
||||
{
|
||||
auto child = pairChild.second;
|
||||
if (child->Hidden())
|
||||
continue;
|
||||
Rectangle rect = child->AbsoluteRectangle();
|
||||
renderer->SetScissor(rect);
|
||||
child->Draw(renderer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Draw(std::shared_ptr<Renderer> renderer) { }
|
||||
|
||||
protected:
|
||||
std::shared_ptr<dd::EventBroker> EventBroker;
|
||||
std::shared_ptr<dd::ResourceManager> ResourceManager;
|
||||
|
||||
std::string m_Name;
|
||||
int m_Layer;
|
||||
bool m_Hidden;
|
||||
|
||||
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 dd::Events::KeyDown &event) { return false; }
|
||||
virtual bool OnKeyUp(const dd::Events::KeyUp &event) { return false; }
|
||||
//virtual bool OnMouseDown(const Events::KeyDown &event) { }
|
||||
//virtual bool OnMouseUp(const Events::KeyDown &event) { }
|
||||
virtual bool OnCommand(const dd::Events::InputCommand &event) { return false; }
|
||||
|
||||
private:
|
||||
EventRelay<Frame, dd::Events::KeyDown> m_EKeyDown;
|
||||
EventRelay<Frame, dd::Events::KeyUp> m_EKeyUp;
|
||||
EventRelay<Frame, dd::Events::InputCommand> m_EInputCommand;
|
||||
|
||||
void Initialize()
|
||||
{
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Frame::OnKeyDown);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Frame::OnKeyUp);
|
||||
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Frame::OnCommand);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //DAYDREAM_FRAME_H
|
||||
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// Created by Administrator on 2015-09-17.
|
||||
//
|
||||
|
||||
#ifndef DAYDREAM_MAINMENUFRAME_H
|
||||
#define DAYDREAM_MAINMENUFRAME_H
|
||||
|
||||
#include "GUI/Frame.h"
|
||||
#include "GUI/TextureFrame.h"
|
||||
|
||||
#include "Game/EGameStart.h"
|
||||
//#include "Events/PlayBGM.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
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;
|
||||
|
||||
/*Events::PlayBGM e;
|
||||
e.Resource = "Sounds/BGM/DiesIrae.mp3";
|
||||
EventBroker->Publish(e);*/
|
||||
}
|
||||
|
||||
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 e1;
|
||||
EventBroker->Publish(e1);
|
||||
/*Events::PlayBGM e2;
|
||||
e2.Resource = "Sounds/BGM/FlightOfTheBumblebee.mp3";
|
||||
EventBroker->Publish(e2);*/
|
||||
}
|
||||
// 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 //DAYDREAM_MAINMENUFRAME_H
|
||||
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// Created by Administrator on 2015-09-17.
|
||||
//
|
||||
|
||||
#ifndef DAYDREAM_TEXTUREFRAME_H
|
||||
#define DAYDREAM_TEXTUREFRAME_H
|
||||
|
||||
#include <glm/detail/type_vec.hpp>
|
||||
#include <glm/detail/type_vec4.hpp>
|
||||
#include "GUI/Frame.h"
|
||||
#include "Core/Texture.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
class TextureFrame : public Frame {
|
||||
public:
|
||||
TextureFrame(Frame *parent, std::string name)
|
||||
: Frame(parent, name), m_Texture(nullptr), m_FadeTexture(nullptr), m_Color(glm::vec4(1.f, 1.f, 1.f, 1.f)),
|
||||
m_FadeDuration(0.f), m_CurrentFade(1.f) { }
|
||||
|
||||
void Draw(std::shared_ptr<Renderer> renderer) override {
|
||||
if (m_Texture == nullptr)
|
||||
return;
|
||||
|
||||
RenderQueue.Clear();
|
||||
|
||||
// Main texture
|
||||
{
|
||||
SpriteJob job;
|
||||
job.TextureID = m_Texture->ResourceID;
|
||||
job.DiffuseTexture = *m_Texture;
|
||||
job.Color = glm::vec4(m_Color.r, m_Color.g, m_Color.b, m_Color.a * m_CurrentFade);
|
||||
RenderQueue.Forward.Add(job);
|
||||
}
|
||||
|
||||
// Texture while fading
|
||||
if (m_FadeTexture && m_CurrentFade < 1) {
|
||||
SpriteJob job;
|
||||
job.TextureID = m_FadeTexture->ResourceID;
|
||||
job.DiffuseTexture = *m_FadeTexture;
|
||||
job.Color = glm::vec4(m_Color.r, m_Color.g, m_Color.b, m_Color.a);
|
||||
RenderQueue.Forward.Add(job);
|
||||
}
|
||||
|
||||
renderer->SetCamera(nullptr);
|
||||
renderer->SetViewport(Rectangle(Left(), Top(), Width, Height));
|
||||
renderer->DrawFrame(RenderQueue);
|
||||
}
|
||||
|
||||
dd::Texture *Texture() const { return m_Texture; }
|
||||
|
||||
void SetTexture(std::string resourceName) {
|
||||
m_Texture = ResourceManager->Load<dd::Texture>("Texture", resourceName);
|
||||
}
|
||||
|
||||
void FadeToTexture(std::string resourceName, double duration) {
|
||||
m_FadeTexture = m_Texture;
|
||||
SetTexture(resourceName);
|
||||
m_FadeDuration = duration;
|
||||
m_CurrentFade = 0.f;
|
||||
}
|
||||
|
||||
void Update(double dt) override {
|
||||
if (m_CurrentFade < 1) {
|
||||
m_CurrentFade += dt / m_FadeDuration;
|
||||
if (m_CurrentFade > 1) {
|
||||
m_FadeTexture = nullptr;
|
||||
m_CurrentFade = 1;
|
||||
m_FadeDuration = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glm::vec4 Color() const { return m_Color; }
|
||||
|
||||
void SetColor(glm::vec4 val) { m_Color = val; }
|
||||
|
||||
protected:
|
||||
dd::Texture *m_Texture;
|
||||
dd::Texture *m_FadeTexture;
|
||||
glm::vec4 m_Color;
|
||||
float m_FadeDuration;
|
||||
float m_CurrentFade;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //DAYDREAM_TEXTUREFRAME_H
|
||||
@@ -75,7 +75,7 @@ set(SOURCE_FILES
|
||||
${SOURCE_FILES_Rendering}
|
||||
${SOURCE_FILES_Transform}
|
||||
${SOURCE_FILES_Physics}
|
||||
${SOURCE_FILES_Game})
|
||||
${SOURCE_FILES_Game} ../../include/GUI/Frame.h ../../include/GUI/MainMenuFrame.h ../../include/GUI/TextureFrame.h)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
||||
|
||||
@@ -474,3 +474,8 @@ void dd::Renderer::DebugKeys()
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Renderer::DrawFrame(RenderQueuePair &rq)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user