Intro :
Dans un jeu vidéo, nous avons besoin de créer des interfaces graphiques afin que le joueur puisse communiquer ou sélectionner des items, cliquer sur des boutons, déplacer une armure, etc…
Prérequis :
– Savoir initialiser DirectX 10
– Savoir lire du C++
– Connaître la classe Sprite2D
Contenu :
Voici le fichier GUIWidget.h :
#ifndef GUI_WIDGET_H
#define GUI_WIDGET_H
#include <iostream>
#include <vector>
#include <d3dx10math.h>
#include "DataParameters.h"
class Sprite2D;
typedef DataParameters GUIParameters;
struct AABB
{
unsigned int x;
unsigned int y;
unsigned int w;
unsigned int h;
};
class GUIEventListener
{
public:
GUIEventListener() {}
virtual ~GUIEventListener() {}
virtual void OnGUIEvent(GUIParameters& param) = 0;
};
class GUIWidget
{
public:
GUIWidget(const std::string& sName);
virtual ~GUIWidget();
virtual void Update(float fTimeSinceLastFrame);
D3DXVECTOR2 GetPosition();
virtual void SetPosition(unsigned int x, unsigned int y);
void SetRelativePosition(unsigned int x, unsigned int y);
bool IsCollidingWithMousePointer(unsigned int x, unsigned int y);
void SetVisible(bool bVisible);
bool IsVisible();
void AddEventListener(GUIEventListener* pEventListener);
virtual void SetSize(unsigned int iWidth, unsigned int iHeight);
void GetSize(unsigned int& iWidth, unsigned int& iHeight);
std::string GetName();
void AddChild(GUIWidget* pChild);
void SetParent(GUIWidget* pParent);
bool HasParent();
void SetCanBeOverlaped(bool bCanBeOverlaped);
bool CanOverlap();
void GetChildren(std::map<std::string, GUIWidget*>& children);
GUIWidget* GetParent();
bool HasChildren();
virtual void SetActive(bool bActive);
bool IsActive();
bool IsChildrenLocked();
void SetChildrenLocked(bool bActive);
protected:
std::vector<GUIEventListener*> m_eventListeners;
void SendEventToListeners(GUIParameters& param);
private:
AABB m_aabox;
bool m_bVisible;
unsigned int m_iPosX;
unsigned int m_iPosY;
std::string m_sName;
GUIWidget* m_pParent;
std::map<std::string, GUIWidget*> m_children;
bool m_bCanBeOverlaped;
bool m_bActive;
bool m_bIsChildrenLocked;
};
#endif
Voici le fichier GUIWidget.cpp :
#include "GUIWidget.h"
#include "Sprite2D.h"
#include "Defines.h"
#include "GUIManager.h"
#include "System.h"
#include "InputManager.h"
#include "GUIManager.h"
GUIWidget::GUIWidget(const std::string& sName) :
m_iPosX(0),
m_iPosY(0),
m_sName(sName),
m_pParent(nullptr),
m_bCanBeOverlaped(true),
m_bVisible(true),
m_bActive(true),
m_bIsChildrenLocked(false)
{
ZeroMemory(&m_aabox, sizeof(AABB));
}
GUIWidget::~GUIWidget()
{
}
void GUIWidget::Update(float fTimeSinceLastFrame)
{
}
void GUIWidget::SetCanBeOverlaped(bool bCanBeOverlaped)
{
m_bCanBeOverlaped = bCanBeOverlaped;
if (HasChildren())
{
for (auto it = m_children.begin(); it != m_children.end(); it++)
{
GUIWidget* pChild = it->second;
pChild->SetCanBeOverlaped(bCanBeOverlaped);
}
}
}
bool GUIWidget::CanOverlap()
{
return m_bCanBeOverlaped;
}
std::string GUIWidget::GetName()
{
return m_sName;
}
D3DXVECTOR2 GUIWidget::GetPosition()
{
return D3DXVECTOR2(m_iPosX, m_iPosY);
}
void GUIWidget::SetPosition(unsigned int x, unsigned int y)
{
m_iPosX = x;
m_iPosY = y;
m_aabox.x = x;
m_aabox.y = y;
}
bool GUIWidget::IsCollidingWithMousePointer(unsigned int x, unsigned int y)
{
return (x >= m_aabox.x) && (x < (m_aabox.x + m_aabox.w))
&& (y >= m_aabox.y) && (y < (m_aabox.y + m_aabox.h));
}
void GUIWidget::SetVisible(bool bVisible)
{
m_bVisible = bVisible;
if (HasChildren())
{
for (auto it = m_children.begin(); it != m_children.end(); it++)
{
GUIWidget* pChild = it->second;
pChild->SetVisible(bVisible);
if (!bVisible)
{
SetActive(false);
}
}
}
}
bool GUIWidget::IsVisible()
{
return m_bVisible;
}
void GUIWidget::AddEventListener(GUIEventListener* pEventListener)
{
m_eventListeners.push_back(pEventListener);
}
void GUIWidget::SendEventToListeners(GUIParameters& param)
{
for (unsigned int i = 0; i < m_eventListeners.size(); i++)
{
GUIEventListener* pEventListener = m_eventListeners[i];
pEventListener->OnGUIEvent(param);
}
}
void GUIWidget::SetSize(unsigned int iWidth, unsigned int iHeight)
{
m_aabox.w = iWidth;
m_aabox.h = iHeight;
}
void GUIWidget::GetSize(unsigned int& iWidth, unsigned int& iHeight)
{
iWidth = m_aabox.w;
iHeight = m_aabox.h;
}
void GUIWidget::AddChild(GUIWidget* pChild)
{
pChild->SetParent(this);
m_children[pChild->GetName()] = pChild;
}
void GUIWidget::SetParent(GUIWidget* pParent)
{
m_pParent = pParent;
}
void GUIWidget::GetChildren(std::map<std::string, GUIWidget*>& children)
{
children = m_children;
}
GUIWidget* GUIWidget::GetParent()
{
return m_pParent;
}
bool GUIWidget::HasParent()
{
return m_pParent != nullptr;
}
void GUIWidget::SetRelativePosition(unsigned int x, unsigned int y)
{
if (HasParent())
{
GUIWidget* pParent = GetParent();
D3DXVECTOR2 pos = pParent->GetPosition();
SetPosition(pos.x + x, pos.y + y);
}
}
bool GUIWidget::HasChildren()
{
return m_children.size() > 0;
}
void GUIWidget::SetActive(bool bActive)
{
if (HasChildren())
{
for (auto it = m_children.begin(); it != m_children.end(); it++)
{
GUIWidget* pChild = it->second;
pChild->SetActive(bActive);
}
}
m_bActive = bActive;
}
bool GUIWidget::IsActive()
{
return m_bActive;
}
bool GUIWidget::IsChildrenLocked()
{
return m_bIsChildrenLocked;
}
void GUIWidget::SetChildrenLocked(bool bActive)
{
m_bIsChildrenLocked = bActive;
}
Résumé :
Nous avons présenter la première classe nommée GUIWidget qui servira de base à toutes les autres classes graphiques (GUI).

