Advertisement
Guest User

Ma classe Button !

a guest
Mar 28th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. //-- HEADER :
  2. #pragma once
  3.  
  4. #include <functional>
  5.  
  6. namespace ml {
  7.    
  8.     class Button {
  9.        
  10.         public :
  11.            
  12.             using OnClickedCallback = std::function<void()>;
  13.            
  14.             Button();
  15.              ~Button();
  16.            
  17.             const NzBoxf& GetBounds() const;
  18.             const NzFont& GetFont() const;
  19.             const NzString& GetText() const;
  20.             const NzColor& GetColor() const;
  21.             const NzColor& GetDefaultColor() const;
  22.             unsigned int GetCharacterSize() const;
  23.            
  24.             void OnMouseMoved(const NzVector2f& mouse);
  25.             void OnMousePressed(const NzVector2f& mouse, NzMouse::Button button);
  26.            
  27.             void SetMousePressedCallback(OnClickedCallback callback);
  28.            
  29.             void SetFont(NzFontRef font);
  30.             void SetText(const NzString& text);
  31.             void SetTextColor(const NzColor& color);
  32.             void SetDefaultTextColor(const NzColor& color);
  33.             void SetCharaceterSize(unsgined int characterSize);
  34.            
  35.             void Register(NzScene& scene);
  36.              
  37.         private :
  38.        
  39.             OnClickedCallback m_clickedCallback;
  40.            
  41.             NzSimpleDrawer m_drawer;
  42.             NzTextSprite *m_text;
  43.            
  44.             NzColor m_defaultColor;
  45.            
  46.      };
  47. }
  48.  
  49. // -- SOURCE :
  50.  
  51. #include "Button.h"
  52.  
  53. namespace ml {
  54.     Button::Button() : m_text(nullptr) { m_drawer.SetCharacterSize(45); }
  55.     Button::~Button() {}
  56.    
  57.     const NzBoxf& Button::GetBounds() const { return m_text->GetBoundingVolume().aabb; }
  58.     const NzFont& Button::GetFont() const { return m_drawer.GetFont(); }
  59.     const NzString& Button::GetText() const { return m_drawer.GetText(); }
  60.     const NzColor& Button::GetColor() const { return m_drawer.GetColor(); }
  61.     const NzColor& Button::GetColor() const { return m_defaultColor; }
  62.     unsigned int Button::GetCharacterSize() const { return m_drawer.GetCharacterSize(); }
  63.            
  64.     void Button::OnMouseMoved(const NzVector2f& mouse) {
  65.         if (GetBounds().contains(mouse)) {
  66.             NzColor newColor(GetColor());
  67.             newColor.r /= 1.5;
  68.             newColor.g /= 1.5;
  69.             newColor.b /= 1.5;
  70.             SetTextColor(newColor);
  71.         } else
  72.             SetTextColor(m_defaultColor);
  73.            
  74.     }
  75.     void Button::OnMousePressed(const NzVector2f& mouse, NzMouse::Button button) {
  76.         if (m_clickedCallback) {
  77.             if (GetBounds().contains(mouse) && button == NzMouse::Left)
  78.                 m_clickedCallback();
  79.         }
  80.     }
  81.            
  82.     void Button::SetMousePressedCallback(OnClickedCallback callback) { if (callback) m_clickedCallback =  callback; }
  83.    
  84.     void Button::SetFont(NzFontRef font) { m_drawer.SetFont(std::move(font)); }
  85.     void Button::SetText(const NzString& text) { m_drawer.SetText(text); }
  86.     void Button::SetTextColor(const NzColor& color) { m_drawer.SetColor(color); }
  87.     void Button::SetDefaultColor(const NzColor& color) { m_defaultColor = color; SetTextColor(color); }
  88.     void Button::SetCharaceterSize(unsigned int characterSize) { m_drawer.SetCharacterSize(characterSize); }
  89.    
  90.     void Button::Register(NzScene& scene) { m_text  = scene.CreateNode<NzTextSprite>(); }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement