166 lines
4.4 KiB
C++
166 lines
4.4 KiB
C++
#include "JGL/JGL.h"
|
|
#include <JUI/Widgets/Button.hpp>
|
|
#include <jlog/Logger.hpp>
|
|
|
|
namespace JUI {
|
|
Button::Button(): Rect(), Clickable() {
|
|
Name("Button");
|
|
BGColor(BaseBGColor());
|
|
BorderColor(BaseBorderColor());
|
|
}
|
|
|
|
Button::Button(Widget *parent): Button() {
|
|
this->Parent(parent);
|
|
};
|
|
|
|
void Button::OnClick(const Vector2& mouse_pos, const MouseButton& btn) {
|
|
if (disabled)
|
|
return;
|
|
|
|
Clickable::OnClick(mouse_pos, btn);
|
|
}
|
|
|
|
void Button::OnRelease(const J3ML::LinearAlgebra::Vector2 &mouse_pos, const JUI::MouseButton &btn,
|
|
bool still_hovering) {
|
|
|
|
if (disabled)
|
|
return;
|
|
|
|
Clickable::OnRelease(mouse_pos, btn, still_hovering);
|
|
}
|
|
|
|
void Button::UpdateVisualState() {
|
|
if (Disabled()) {
|
|
BGColor(DisabledBGColor());
|
|
BorderColor(DisabledBorderColor());
|
|
} else if (IsClicked()) {
|
|
BGColor(PressedBGColor());
|
|
BorderColor(PressedBorderColor());
|
|
} else if (IsHovered()) {
|
|
BGColor(HoveredBGColor());
|
|
BorderColor(HoveredBorderColor());
|
|
} else {
|
|
BGColor(BaseBGColor());
|
|
BorderColor(BaseBorderColor());
|
|
}
|
|
}
|
|
|
|
void Button::Update(float delta) {
|
|
|
|
Hoverable::Update(IsMouseInside(), delta);
|
|
|
|
// TODO: This is duplicated between here and Window.cpp
|
|
// Will be abstracted away into Clickable shortly.
|
|
if (IsHovered() && mb_state && !prev_mb_state)
|
|
{
|
|
OnClick(last_known_mouse_pos, mbtn);
|
|
}
|
|
|
|
if (IsClicked() && !mb_state)
|
|
{
|
|
OnRelease(last_known_mouse_pos, mbtn, IsHovered());
|
|
}
|
|
|
|
UpdateVisualState();
|
|
|
|
prev_mb_state = mb_state;
|
|
|
|
Rect::Update(delta);
|
|
}
|
|
|
|
void Button::Enable() {
|
|
SetEnabled(true);
|
|
}
|
|
|
|
void Button::Disable() {
|
|
SetEnabled(false);
|
|
}
|
|
|
|
bool Button::Enabled() const {
|
|
return !disabled;
|
|
}
|
|
|
|
bool Button::Disabled() const {
|
|
return disabled;
|
|
}
|
|
|
|
void Button::SetEnabled(bool enabled) {
|
|
|
|
// if state actually changed
|
|
if (disabled != enabled)
|
|
return;
|
|
|
|
if (disabled)
|
|
OnDisabled.Invoke();
|
|
else
|
|
OnEnabled.Invoke();
|
|
|
|
disabled = !enabled;
|
|
|
|
BGColor(DisabledBGColor());
|
|
BorderColor(DisabledBorderColor());
|
|
}
|
|
|
|
void Button::OnHover(const Vector2 &MousePos) {
|
|
Hoverable::OnHover(MousePos);
|
|
|
|
if (Disabled() || IsClicked())
|
|
return;
|
|
|
|
}
|
|
|
|
void Button::OnExit(const Vector2 &MousePos) {
|
|
Hoverable::OnExit(MousePos);
|
|
|
|
if (Disabled() || IsClicked())
|
|
return;
|
|
}
|
|
|
|
Color4 Button::HoveredBGColor() const { return hover_bg; }
|
|
|
|
Color4 Button::BaseBGColor() const { return base_bg; }
|
|
|
|
Color4 Button::PressedBGColor() const { return pressed_bg; }
|
|
|
|
Color4 Button::DisabledBGColor() const { return disabled_bg;}
|
|
|
|
Color4 Button::HoveredBorderColor() const { return hover_border;}
|
|
|
|
Color4 Button::BaseBorderColor() const { return base_border;}
|
|
|
|
Color4 Button::PressedBorderColor() const { return pressed_border;}
|
|
|
|
Color4 Button::DisabledBorderColor() const { return disabled_border;}
|
|
|
|
void Button::HoveredBGColor(const Color4 &color) { hover_bg = color;}
|
|
|
|
void Button::BaseBGColor(const Color4 &color) { base_bg = color;}
|
|
|
|
void Button::PressedBGColor(const Color4 &color) { pressed_bg = color;}
|
|
|
|
void Button::DisabledBGColor(const Color4 &color) { disabled_bg = color;}
|
|
|
|
void Button::HoveredBorderColor(const Color4 &color) { hover_border = color;}
|
|
|
|
void Button::BaseBorderColor(const Color4 &color) {base_border = color; }
|
|
|
|
void Button::PressedBorderColor(const Color4 &color) { pressed_border = color;}
|
|
|
|
void Button::DisabledBorderColor(const Color4 &color) { disabled_border = color; }
|
|
|
|
void Button::BorderColors(const Color4 &base, const Color4 &hover, const Color4 &pressed, const Color4 &disabled) {
|
|
BaseBorderColor(base);
|
|
HoveredBorderColor(hover);
|
|
PressedBorderColor(pressed);
|
|
DisabledBGColor(disabled);
|
|
UpdateVisualState();
|
|
}
|
|
|
|
void Button::BGColors(const Color4 &base, const Color4 &hover, const Color4 &pressed, const Color4 &disabled) {
|
|
BaseBGColor(base);
|
|
HoveredBGColor(hover);
|
|
PressedBGColor(pressed);
|
|
DisabledBGColor(disabled);\
|
|
UpdateVisualState();
|
|
}
|
|
} |