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 :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#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 :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
#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).


