Skip to content

Commit

Permalink
#2: Added preliminary menu creation API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruskiy69 committed Jul 29, 2013
1 parent e0a71b8 commit bd88f39
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 12 deletions.
45 changes: 33 additions & 12 deletions include/Zenderer/GUI/Button.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* @file
* Zenderer/GUI/Font.hpp - A wrapper for TrueType font loading and rendering.
* Zenderer/GUI/Button.hpp - A wrapper for easily creating roll-over button
* effects.
*
* @author George Kudrayvtsev (halcyon)
* @version 2.0
* @version 1.0
* @copyright Apache License v2.0
* Licensed under the Apache License, Version 2.0 (the "License"). \n
* You may not use this file except in compliance with the License. \n
Expand Down Expand Up @@ -33,10 +34,8 @@ namespace gui
{
public:
CButton(gfx::CScene& MenuScene) :
m_Scene(MenuScene),
m_Active(m_Scene.AddEntity()),
m_Normal(m_Scene.AddEntity()),
m_Current(m_Normal) {}
m_Scene(MenuScene), m_Active(m_Scene.AddEntity()),
m_Normal(m_Scene.AddEntity()), m_Current(m_Normal) {}

~CButton()
{
Expand Down Expand Up @@ -93,13 +92,35 @@ namespace gui
mp_Font->Render(m_Active, text);
}

bool Switch()
bool SetActive()
{
if(!mp_Font || mp_Current == nullptr) return false;
if(mp_Current == &m_Normal) mp_Current = &m_Active;
else mp_Current = &m_Normal;

return true;
if(!mp_Font || mp_Current == &m_Normal) return;

mp_Current->Disable();
mp_Current = &m_Active;
mp_Current->Enable();
}

bool IsOver(const math::vector_t& Pos)
{
return this->IsOver(math::aabb_t(
Pos, math::Vector<uint32_t>(2, 2))
);
}

bool IsOver(const math::aabb_t& Box)
{
if(mp_Current == nullptr || !mp_Font) return false;
return mp_Current->GetBox().collides(Box);
}

void SetDefault()
{
if(!mp_Font || mp_Current == &m_Normal) return;

mp_Current->Disable();
mp_Current = &m_Normal;
mp_Current->Enable();
}

private:
Expand Down
157 changes: 157 additions & 0 deletions include/Zenderer/GUI/Menu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* @file
* Zenderer/GUI/Menu.hpp - A high-level menu creating interface.
*
* @author George Kudrayvtsev (halcyon)
* @version 1.0
* @copyright Apache License v2.0
* Licensed under the Apache License, Version 2.0 (the "License"). \n
* You may not use this file except in compliance with the License. \n
* You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0 \n
* Unless required by applicable law or agreed to in writing, software \n
* distributed under the License is distributed on an "AS IS" BASIS, \n
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n
* See the License for the specific language governing permissions and \n
* limitations under the License.
*
* @addtogroup GUI
* @{
**/

#ifndef ZENDERER__GUI__BUTTON_HPP
#define ZENDERER__GUI__BUTTON_HPP

#include "Zenderer/Events/EventHandler.hpp"
#include "Zenderer/Events/Mouse.hpp"
#include "Zenderer/Graphics/Scene.hpp"
#include "Font.hpp"
#include "Button.hpp"

namespace zen
{
namespace gui
{
class ZEN_API CMenu
{
public:
CMenu(gfx::CWindow& Window, asset::CAssetManager& Assets) :
m_Scene(Window.GetWidth(), Window.GetHeight(), Assets),
m_Background(m_Scene.AddEntity()),
m_Title(m_Scene.AddEntity()),
mp_Bg(nullptr), m_spacing(0)
{
m_Scene.Init();
mp_menuButtons.clear();
}

virtual ~CMenu()
{
m_Scene.Destroy();
for(auto& i : mp_menuButtons) delete i;
mp_menuButtons.clear();
}

virtual int16_t HandleEvent(const evt::event_t& Evt)
{
if(Evt.type == evt::EventType::MOUSE_MOTION)
{
math::aabb_t MouseBox(Evt.mouse.position,
math::Vector<uint32_t>(2, 2));

auto i = mp_menuButtons.begin(),
j = mp_menuButtons.end();

for( ; i != j; ++i)
{
if((*i)->IsOver(Box))
{
(*i)->SetActive();
}
else
{
(*i)->SetNormal();
}
}
}

else if(Evt.type == evt::EventType::MOUSE_DOWN &&
Evt.mouse.button == evt::MouseButton::LEFT)
{
math::aabb_t MouseBox(Evt.mouse.position,
math::Vector<uint32_t>(2, 2));

for(size_t i = 0; i < mp_menuButtons.size(): ++i)
{
if(mp_menuButtons[i]->IsOver(Box)) return i;
}
}

return -1;
}

virtual uint16_t AddButton(const string_t& text)
{
CButton* pNew = new CButton(m_Scene);
pNew->SetFont(m_Font);
pNew->SetActiveColor(m_acolor);
pNew->SetNormalColor(m_ncolor);

if(mp_Bg != nullptr) pNew->SetBackground(*mp_Bg);

pNew->Prepare(text);
pNew->Place(m_Position);
pNew->SetNormal();

m_Position.y += m_spacing;
mp_menuButtons.push_back(pNew);
return mp_menuButtons.size() - 1;
}

virtual int Update()
{
m_Scene.Render();
}

void SetButtonBackground(const obj::CEntity& Bg)
{
mp_Bg = Bg;
}

void SetNormalButtonTextColor(const color4f_t& Color);
void SetActiveButtonTextColor(const color4f_t& Color);

void SetInitialButtonPosition(const math::vector_t& Pos)
{
m_Position = Pos;
}

void SetSpacing(const uint16_t vertical_spacing)
{
m_spacing = vertical_spacing;
}

private:
gfx::CScene m_Scene;
obj::CEntity& m_Background;
obj::CEntity& m_Title;
obj::CEntity* mp_Bg;
std::vector<CButton*> mp_menuButtons;

math::vector_t m_Position;
uint16_t m_spacing;
};
} // namespace gui
} // namespace zen

#endif // ZENDERER__GUI__BUTTON_HPP

/**
* @class zen::gui::CMenu
* @details
* A high-level wrapper that facilitates a simple method of creating
* fairly customizable and high-quality menus with custom buttons,
* fonts, backgrounds, and other sections.
**/

/** @} **/
21 changes: 21 additions & 0 deletions src/EngineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ using gfxcore::CRenderer;
P.Create();
P2.Create();
//P3.Create();

gui::CFont* MenuFont = Manager.Create<gui::CFont>();
MenuFont->AttachManager(Manager);
MenuFont->LoadFromFile(ZENDERER_FONT_PATH"default.ttf");
gui::CMenu MainMenu(Window, Manager);
MainMenu.SetFont(MenuFont);
MainMenu.SetNormalButtonTextColor(color4f_t());
MainMenu.SetActiveButtonTextColor(color4f_t(0, 0, 0, 1));
MainMenu.SetSpacing(MenuFont->GetLineHeight() + 32);

MainMenu.AddButton("Play Game");
MainMenu.AddButton("Load Game");
MainMenu.AddButton("Options");
MainMenu.AddButton("Exit");

while(Window.IsOpen())
{
Expand All @@ -211,6 +225,10 @@ using gfxcore::CRenderer;
if(event.key.symbol == 'm') CRenderer::ToggleWireframe();
std::cout << "Printable: " << event.key.symbol << "\n";
break;

default:
Menu.HandleEvent(event);
break;
}
}

Expand Down Expand Up @@ -266,6 +284,9 @@ using gfxcore::CRenderer;
Grass3.Shear(math::vector_t(GrassT.z += GrassDT.z, 0.0));

Scene.Render();

int16_t i = Menu.Update();
if(i == 3) Window.Close();

{
Grass.Enable();
Expand Down

0 comments on commit bd88f39

Please sign in to comment.