Intro :
Parfois nous avons besoin qu’une GUIWidget puisse être déplaçable à volonté tout en restant appuyé dessus la souris.
Prérequis :
– Avoir compris la classe GUIWidget
Contenu :
Voici le contenu GUIDraggableWidget.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 |
#ifndef GUI_DRAGGABLE_WIGET_H #define GUI_DRAGGABLE_WIGET_H #include <iostream> #include <d3dx10math.h> #include "GUIImage.h" #include "GUIButton.h" class GUIDraggableWiget : public GUIImage { public: enum DraggableBoxState { NON_ACTIVE, DRAGGING }; GUIDraggableWiget(const std::string& sName, const std::string& sTextureFileName); virtual ~GUIDraggableWiget(); virtual void Update(float fTimeSinceLastFrame); virtual void SetPosition(unsigned int x, unsigned int y); virtual void SetSize(unsigned int iWidth, unsigned int iHeight); void DisableDragging(); void EnableDragging(); private: bool IsCollidingWithDraggableBox(int x, int y); bool IsCollidingWithCloseBox(int x, int y); void SetBoxesPositions(unsigned int x, unsigned int y); D3DXVECTOR2 GetDraggableBoxPosition(); DraggableBoxState GetDraggableBoxState(); void SetDraggableBoxState(DraggableBoxState state); void SetImageSize(unsigned int iWidth, unsigned int iHeight); private: AABB m_draggableBoxAABB; AABB m_closeBoxAABB; DraggableBoxState m_state; int xPos; int yPos; int sx; int sy; bool m_bDragging; bool m_bDraggingEnabled; }; #endif |
Voici le fichier GUIDraggableWidget.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 |
#include "GUIDraggableWidget.h" #include "InputManager.h" #include "GUIManager.h" GUIDraggableWiget::GUIDraggableWiget(const std::string& sName, const std::string& sTextureFileName) : GUIImage(sName, sTextureFileName), xPos(0), yPos(0), sx(0), sy(0), m_bDragging(false), m_bDraggingEnabled(true) { ZeroMemory(&m_draggableBoxAABB, sizeof(AABB)); ZeroMemory(&m_closeBoxAABB, sizeof(AABB)); m_draggableBoxAABB.w = (float)GetImage()->GetWidth(); m_draggableBoxAABB.h = (float)GetImage()->GetHeight() * 0.3f; m_closeBoxAABB.w = 20; m_closeBoxAABB.h = 20; } GUIDraggableWiget::~GUIDraggableWiget() { } void GUIDraggableWiget::Update(float fTimeSinceLastFrame) { if (!IsVisible()) { return; } GUIImage::Update(fTimeSinceLastFrame); int absX, absY; INPUT_MANAGER->GetAbsMouseLocation(absX, absY); int dX = absX - sx; int dY = absY - sy; if (INPUT_MANAGER->IsMouseButtonDown(MOUSE_LEFT_BUTTON_DOWN) && GUI_MANAGER->DoesWidgetIsOnFront(this) && SYSTEM->HasFocus() && IsActive()) { if (IsCollidingWithDraggableBox(absX, absY) && m_bDraggingEnabled) { m_bDragging = true; } if (IsCollidingWithCloseBox(absX, absY)) { SetVisible(false); } if (m_bDragging) { SetDraggableBoxState(DraggableBoxState::DRAGGING); xPos += dX; yPos += dY; /* Contraintes de bords */ if (yPos <= 0) { yPos = 0; } if (xPos <= 0) { xPos = 0; } unsigned int iWidthOffset = D3D10_RENDERER->GetViewportWidth() - GetImage()->GetWidth(); unsigned int iHeightOffset = D3D10_RENDERER->GetViewportHeight() - GetImage()->GetHeight(); /* Contraintes de bords */ if (xPos >= iWidthOffset) { xPos = iWidthOffset; } if (yPos >= iHeightOffset) { yPos = iHeightOffset; } SetPosition(xPos, yPos); } } else { m_bDragging = false; } sx = absX; sy = absY; } bool GUIDraggableWiget::IsCollidingWithDraggableBox(int x, int y) { return (x >= m_draggableBoxAABB.x) && (x < (m_draggableBoxAABB.x + m_draggableBoxAABB.w)) && (y >= m_draggableBoxAABB.y) && (y < (m_draggableBoxAABB.y + m_draggableBoxAABB.h)) && IsVisible(); } bool GUIDraggableWiget::IsCollidingWithCloseBox(int x, int y) { return (x >= m_closeBoxAABB.x) && (x < (m_closeBoxAABB.x + m_closeBoxAABB.w)) && (y >= m_closeBoxAABB.y) && (y < (m_closeBoxAABB.y + m_closeBoxAABB.h)) && IsVisible(); } void GUIDraggableWiget::SetDraggableBoxState(DraggableBoxState state) { m_state = state; } GUIDraggableWiget::DraggableBoxState GUIDraggableWiget::GetDraggableBoxState() { return m_state; } void GUIDraggableWiget::SetBoxesPositions(unsigned int x, unsigned int y) { m_draggableBoxAABB.x = x; m_draggableBoxAABB.y = y; m_closeBoxAABB.x = (GetImage()->GetWidth() - m_closeBoxAABB.w) + x; m_closeBoxAABB.y = y; } D3DXVECTOR2 GUIDraggableWiget::GetDraggableBoxPosition() { return D3DXVECTOR2(m_draggableBoxAABB.x, m_draggableBoxAABB.y); } void GUIDraggableWiget::SetPosition(unsigned int x, unsigned int y) { SetBoxesPositions(x, y); xPos = x; yPos = y; GUIImage::SetPosition(x, y); } void GUIDraggableWiget::SetSize(unsigned int iWidth, unsigned int iHeight) { GUIImage::SetSize(iWidth, iHeight); m_draggableBoxAABB.w = (float)GetImage()->GetWidth(); m_draggableBoxAABB.h = (float)GetImage()->GetHeight() * 0.3f; } void GUIDraggableWiget::DisableDragging() { m_bDraggingEnabled = false; } void GUIDraggableWiget::EnableDragging() { m_bDraggingEnabled = true; } |
Résumé :
Nous avons décrit une classe qui représente un moyen d’afficher une GUIWidget déplaçable.


