Intro :
Parfois nous avons besoin d’afficher un bouton pour rendre l’interface utilisateur (GUI) plus dynamique. Le bouton peut afficher une autre image lorsqu’il est reste cliqué dessus !
Prérequis :
– Avoir lu la classe GUIWidget
Contenu :
Voici le fichier GUIButton.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 |
#ifndef GUI_BUTTON_H #define GUI_BUTTON_H #include <d3dx10math.h> #include <iostream> #include "Sprite2D.h" #include "GUIImage.h" #include "Defines.h" class GUIButton : public GUIImage { public: enum ButtonState { NON_ACTIVE, HOVER, CLICKED, PUSHED_OUT }; GUIButton(const std::string& sName, const std::string& sTextureName, const std::string& sTextureClickedName = NONE, const std::string& sTextureHoverName = NONE); virtual ~GUIButton(); virtual void Update(float fTimeSinceLastFrame); ButtonState GetState(); void SetState(ButtonState state); void SetHoverOnImage(); void SetClickedOnImage(); void SetNormalImage(); bool IsClickedOn(); bool IsPushedOut(); bool IsNonActive(); private: ButtonState m_state; std::string m_sNormalTextureFile; std::string m_sClickedOnTextureFile; std::string m_sHoverOnTextureFile; bool m_bClickedOn; bool m_bHover; bool m_bHasClickedOnImage; bool m_bHasHoverdOnImage; bool m_bMouseButtonReleased; }; #endif |
Voici le fichier GUIButton.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 |
#include "GUIButton.h" #include "InputManager.h" #include "GUIManager.h" #include "Utils.h" GUIButton::GUIButton(const std::string& sName, const std::string& sTextureName, const std::string& sTextureClickedName, const std::string& sTextureHoverName) : GUIImage(sName, sTextureName), m_state(NON_ACTIVE), m_sNormalTextureFile(sTextureName), m_sClickedOnTextureFile(sTextureClickedName), m_sHoverOnTextureFile(sTextureHoverName), m_bClickedOn(false), m_bHover(false), m_bMouseButtonReleased(false), m_bHasClickedOnImage(false), m_bHasHoverdOnImage(false) { // Image quand on appuie if (Utils::IsFileReadable(sTextureClickedName)) { m_bHasClickedOnImage = true; } // Image quand on survole if (Utils::IsFileReadable(sTextureHoverName)) { m_bHasHoverdOnImage = true; } } GUIButton::~GUIButton() { } void GUIButton::Update(float fTimeSinceLastFrame) { if (!IsVisible()) { return; } int x, y; INPUT_MANAGER->GetAbsMouseLocation(x, y); if (IsActive()) { if (IsCollidingWithMousePointer(x, y) && GUI_MANAGER->IsWidgetFirstOverlapping(this)) { SetState(ButtonState::HOVER); m_bHover = true; SetHoverOnImage(); if (INPUT_MANAGER->IsMouseButtonDown(MOUSE_LEFT_BUTTON_DOWN)) { m_bClickedOn = true; SetState(ButtonState::CLICKED); GUIParameters param; param << GetState(); SendEventToListeners(param); SetClickedOnImage(); } else if (m_bClickedOn) { SetNormalImage(); m_bClickedOn = false; SetState(ButtonState::PUSHED_OUT); } } else if (m_bHover) { SetState(ButtonState::NON_ACTIVE); SetNormalImage(); m_bHover = false; } else { m_bClickedOn = false; } } GUIImage::Update(fTimeSinceLastFrame); } GUIButton::ButtonState GUIButton::GetState() { return m_state; } void GUIButton::SetState(ButtonState state) { m_state = state; } bool GUIButton::IsClickedOn() { return GetState() == GUIButton::ButtonState::CLICKED && IsVisible(); } bool GUIButton::IsPushedOut() { return GetState() == GUIButton::ButtonState::PUSHED_OUT && IsVisible(); } bool GUIButton::IsNonActive() { return GetState() == GUIButton::ButtonState::NON_ACTIVE && IsVisible(); } void GUIButton::SetClickedOnImage() { if (m_bHasClickedOnImage) { SetTextureImage(m_sClickedOnTextureFile); } } void GUIButton::SetNormalImage() { SetTextureImage(m_sNormalTextureFile); } void GUIButton::SetHoverOnImage() { if (m_bHasHoverdOnImage) { SetTextureImage(m_sHoverOnTextureFile); } } |
Résumé :
Voici une classe qui permet d’afficher un bouton cliquable.


