Intro :
Parfois nous avons besoin d’afficher une image pour rendre l’interface utilisateur (GUI) plus jolie.
Prérequis :
– Avoir lu la classe GUIWidget
Contenu :
Voici le fichier GUIImage.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 |
#ifndef GUI_IMAGE_H #define GUI_IMAGE_H #include "GUIWidget.h" class GUIImage : public GUIWidget { public: GUIImage(const std::string& sName, const std::string& sTextureFileName); virtual ~GUIImage(); virtual void SetTextureImage(const std::string& sTextureName); virtual void Update(float fTimeSinceLastFrame); virtual void SetPosition(unsigned int x, unsigned int y); virtual void SetSize(unsigned int iWidth, unsigned int iHeight); virtual void SetVisible(bool bVisible); Sprite2D* GetImage(); private: Sprite2D* m_pImage; }; #endif |
Voici le fichier GUIImage.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 |
#include "GUIImage.h" #include "Sprite2D.h" #include "Defines.h" GUIImage::GUIImage(const std::string& sName, const std::string& sTextureFileName) : GUIWidget(sName), m_pImage(nullptr) { m_pImage = new Sprite2D(sTextureFileName); m_pImage->Initialize(); unsigned int iImageWidth = m_pImage->GetWidth(); unsigned int iImageHeight = m_pImage->GetHeight(); SetSize(iImageWidth, iImageHeight); } GUIImage::~GUIImage() { SAFE_DELETE(m_pImage); } void GUIImage::Update(float fTimeSinceLastFrame) { if (IsVisible()) { m_pImage->Render(); } GUIWidget::Update(fTimeSinceLastFrame); } void GUIImage::SetTextureImage(const std::string& sTextureName) { m_pImage->SetTextureImage(sTextureName); // On restaure les dimensions d'avant unsigned int iWidth = 0; unsigned int iHeight = 0; GetSize(iWidth, iHeight); m_pImage->SetImageSize(iWidth, iHeight); } void GUIImage::SetPosition(unsigned int x, unsigned int y) { m_pImage->SetPosition(x, y); GUIWidget::SetPosition(x, y); } void GUIImage::SetSize(unsigned int iWidth, unsigned int iHeight) { m_pImage->SetImageSize(iWidth, iHeight); GUIWidget::SetSize(iWidth, iHeight); } void GUIImage::SetVisible(bool bVisible) { if (bVisible) { m_pImage->Show(); } else { m_pImage->Hide(); } GUIWidget::SetVisible(bVisible); } Sprite2D* GUIImage::GetImage() { return m_pImage; } |
Résumé :


