Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-- HEADER :
- #pragma once
- #include <functional>
- namespace ml {
- class Button {
- public :
- using OnClickedCallback = std::function<void()>;
- Button();
- ~Button();
- const NzBoxf& GetBounds() const;
- const NzFont& GetFont() const;
- const NzString& GetText() const;
- const NzColor& GetColor() const;
- const NzColor& GetDefaultColor() const;
- unsigned int GetCharacterSize() const;
- void OnMouseMoved(const NzVector2f& mouse);
- void OnMousePressed(const NzVector2f& mouse, NzMouse::Button button);
- void SetMousePressedCallback(OnClickedCallback callback);
- void SetFont(NzFontRef font);
- void SetText(const NzString& text);
- void SetTextColor(const NzColor& color);
- void SetDefaultTextColor(const NzColor& color);
- void SetCharaceterSize(unsgined int characterSize);
- void Register(NzScene& scene);
- private :
- OnClickedCallback m_clickedCallback;
- NzSimpleDrawer m_drawer;
- NzTextSprite *m_text;
- NzColor m_defaultColor;
- };
- }
- // -- SOURCE :
- #include "Button.h"
- namespace ml {
- Button::Button() : m_text(nullptr) { m_drawer.SetCharacterSize(45); }
- Button::~Button() {}
- const NzBoxf& Button::GetBounds() const { return m_text->GetBoundingVolume().aabb; }
- const NzFont& Button::GetFont() const { return m_drawer.GetFont(); }
- const NzString& Button::GetText() const { return m_drawer.GetText(); }
- const NzColor& Button::GetColor() const { return m_drawer.GetColor(); }
- const NzColor& Button::GetColor() const { return m_defaultColor; }
- unsigned int Button::GetCharacterSize() const { return m_drawer.GetCharacterSize(); }
- void Button::OnMouseMoved(const NzVector2f& mouse) {
- if (GetBounds().contains(mouse)) {
- NzColor newColor(GetColor());
- newColor.r /= 1.5;
- newColor.g /= 1.5;
- newColor.b /= 1.5;
- SetTextColor(newColor);
- } else
- SetTextColor(m_defaultColor);
- }
- void Button::OnMousePressed(const NzVector2f& mouse, NzMouse::Button button) {
- if (m_clickedCallback) {
- if (GetBounds().contains(mouse) && button == NzMouse::Left)
- m_clickedCallback();
- }
- }
- void Button::SetMousePressedCallback(OnClickedCallback callback) { if (callback) m_clickedCallback = callback; }
- void Button::SetFont(NzFontRef font) { m_drawer.SetFont(std::move(font)); }
- void Button::SetText(const NzString& text) { m_drawer.SetText(text); }
- void Button::SetTextColor(const NzColor& color) { m_drawer.SetColor(color); }
- void Button::SetDefaultColor(const NzColor& color) { m_defaultColor = color; SetTextColor(color); }
- void Button::SetCharaceterSize(unsigned int characterSize) { m_drawer.SetCharacterSize(characterSize); }
- void Button::Register(NzScene& scene) { m_text = scene.CreateNode<NzTextSprite>(); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement