-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
167 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
#include <le/core/signal.hpp> | ||
#include <le/scene/ui/interactable.hpp> | ||
#include <le/scene/ui/quad.hpp> | ||
#include <le/scene/ui/text.hpp> | ||
#include <le/scene/ui/view.hpp> | ||
|
||
namespace le::ui { | ||
class Button : public View, public Interactable { | ||
public: | ||
using OnTrigger = Signal<>; | ||
|
||
explicit Button(Ptr<View> parent_view = {}); | ||
|
||
auto on_trigger(OnTrigger::Callback callback) { return m_on_trigger.connect(std::move(callback)); } | ||
|
||
template <typename PointerT, typename MemberFuncT> | ||
requires(std::is_invocable_v<MemberFuncT, PointerT>) | ||
auto on_trigger(PointerT object, MemberFuncT member_func) { | ||
return m_on_trigger.connect(object, member_func); | ||
} | ||
|
||
[[nodiscard]] auto get_quad() const -> Quad& { return *m_quad; } | ||
[[nodiscard]] auto get_text() const -> Text& { return *m_text; } | ||
|
||
graphics::Rgba no_focus{graphics::white_v}; | ||
graphics::Rgba in_focus{graphics::yellow_v}; | ||
|
||
protected: | ||
void tick(Duration dt) override; | ||
|
||
void on_focus_gained() override { m_quad->set_tint(in_focus); } | ||
void on_focus_lost() override { m_quad->set_tint(no_focus); } | ||
void on_release() override { m_on_trigger(); } | ||
|
||
private: | ||
Ptr<Quad> m_quad{}; | ||
Ptr<Text> m_text{}; | ||
OnTrigger m_on_trigger{}; | ||
}; | ||
} // namespace le::ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
#include <le/core/polymorphic.hpp> | ||
#include <le/core/time.hpp> | ||
#include <le/graphics/rect.hpp> | ||
|
||
namespace le::ui { | ||
class Interactable : public Polymorphic { | ||
public: | ||
[[nodiscard]] auto in_focus() const -> bool { return m_in_focus; } | ||
|
||
void update(graphics::Rect2D<> const& hitbox); | ||
|
||
protected: | ||
virtual void on_focus_gained() {} | ||
virtual void on_focus_lost() {} | ||
virtual void on_press() {} | ||
virtual void on_release() {} | ||
|
||
private: | ||
bool m_in_focus{}; | ||
}; | ||
} // namespace le::ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <le/scene/ui/button.hpp> | ||
|
||
namespace le::ui { | ||
Button::Button(Ptr<View> parent_view) : View(parent_view), m_quad(&push_element<Quad>()), m_text(&push_element<Text>()) { | ||
transform.extent = {200, 100}; | ||
m_text->set_text("button"); | ||
m_text->set_tint(graphics::black_v); | ||
m_text->set_align({.vert = VertAlign::eMid}); | ||
} | ||
|
||
void Button::tick(Duration dt) { | ||
m_quad->transform.extent = transform.extent; | ||
View::tick(dt); | ||
auto const hitbox = graphics::Rect2D<>::from_extent(transform.extent, global_position()); | ||
Interactable::update(hitbox); | ||
} | ||
} // namespace le::ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <le/engine.hpp> | ||
#include <le/scene/ui/interactable.hpp> | ||
|
||
namespace le::ui { | ||
void Interactable::update(graphics::Rect2D<> const& hitbox) { | ||
auto const extent = hitbox.extent(); | ||
auto const hit_test = [hitbox, extent](glm::vec2 const point) { | ||
if (extent.x <= 0.0f || extent.y <= 0.0f) { return false; }; | ||
return hitbox.contains(point); | ||
}; | ||
|
||
auto const& input_state = Engine::self().input_state(); | ||
auto const in_focus = hit_test(input_state.cursor_position); | ||
|
||
if (!m_in_focus && in_focus) { | ||
on_focus_gained(); | ||
} else if (!in_focus && m_in_focus) { | ||
on_focus_lost(); | ||
} | ||
m_in_focus = in_focus; | ||
|
||
if (!m_in_focus) { return; } | ||
|
||
auto const mb1_state = input_state.mouse_buttons.at(GLFW_MOUSE_BUTTON_1); | ||
using enum input::Action; | ||
switch (mb1_state) { | ||
case ePress: on_press(); break; | ||
case eRelease: on_release(); break; | ||
default: break; | ||
} | ||
} | ||
} // namespace le::ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters