Skip to content

Commit

Permalink
Feature/cla 133 make layers auto destroyable (#101)
Browse files Browse the repository at this point in the history
* cla-133: added layer_mode

* cla-133: added support for self destroyable layers
  • Loading branch information
ebasconp authored Jan 6, 2025
1 parent d612fb4 commit 68d3331
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 23 deletions.
86 changes: 73 additions & 13 deletions code/classeine-demo/demo_layers_control.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "demo_layers_control.h"

#include <clsn/core/system.h>
#include <clsn/draw/colors.h>

#include <clsn/ui/button.h>
#include <clsn/ui/label.h>
#include <clsn/ui/ui_layer_manager.h>
#include <clsn/ui/ui_manager.h>
#include <clsn/ui/ui_system_backend.h>
Expand All @@ -12,41 +14,99 @@ namespace clsn::demo
demo_layers_control::demo_layers_control(ui::layer_base& _layer)
: base{_layer}
{
auto create_layer_btn =
auto create_normal_layer_btn =
this->add(control::make<ui::button>(_layer), draw::point{100, 100});

create_layer_btn->set_caption("Create layer");
create_layer_btn->set_preferred_size(draw::dimension{200, 40});
create_layer_btn->add_action_listener([this](auto&)
create_normal_layer_btn->set_caption("Normal layer");
create_normal_layer_btn->set_preferred_size(draw::dimension{200, 40});
create_normal_layer_btn->add_action_listener([this](auto&)
{
create_layer();
create_normal_layer();
});
create_layer_btn->set_font(create_layer_btn->get_actual_font().add_size(6));
create_normal_layer_btn->set_font(
create_normal_layer_btn->get_actual_font().add_size(6));

auto create_destroyable_layer_btn =
this->add(control::make<ui::button>(_layer), draw::point{300, 100});

create_destroyable_layer_btn->set_caption("Destroyable layer");
create_destroyable_layer_btn->set_preferred_size(draw::dimension{200, 40});
create_destroyable_layer_btn->add_action_listener([this](auto&)
{
create_self_destroyable_layer();
});
create_destroyable_layer_btn->set_font(
create_destroyable_layer_btn->get_actual_font().add_size(6));
}

void demo_layers_control::create_layer()
void demo_layers_control::create_normal_layer()
{
static int n = 0;

auto b = get_ui_manager().get_layer_manager().make_and_add<ui::button>();
b->get_typed_control().set_caption(core::strings::format("Click me ({})", ++n));
auto b = get_ui_manager()
.get_layer_manager()
.make_and_add<ui::button>(ui::layer_mode::normal);

b->get_typed_control()
.set_caption(
core::strings::format("Click me ({})", ++n));

b->set_size({100, 50});

const auto window_size = get_ui_manager().get_system_backend().get_size();
const auto window_size = get_ui_manager()
.get_system_backend().get_size();

const int random_x = core::system::get_random_int(window_size.get_width() - 100);
const int random_y = core::system::get_random_int(window_size.get_height() - 50);
const int random_x = core::system::get_random_int(
window_size.get_width() - 100);
const int random_y = core::system::get_random_int(
window_size.get_height() - 50);

const int random_red = core::system::get_random_int(200, 255);
const int random_green = core::system::get_random_int(200, 255);
const int random_blue = core::system::get_random_int(200, 255);

b->get_typed_control().set_background_color(draw::color{random_red, random_green, random_blue});
b->get_typed_control().set_background_color(
draw::color{random_red, random_green, random_blue});
b->set_position({random_x, random_y});

b->get_typed_control().add_action_listener([](auto& e)
{
e.get_ui_manager().get_layer_manager().remove_layer(e.get_layer());
});
}

void demo_layers_control::create_self_destroyable_layer()
{
static int n = 0;

auto b = get_ui_manager()
.get_layer_manager()
.make_and_add<ui::label>(
ui::layer_mode::self_destroyable);

b->get_typed_control().set_caption(
core::strings::format("Layer ({})", ++n));

b->set_size({100, 100});

const auto window_size = get_ui_manager()
.get_system_backend().get_size();

const int random_x = core::system::get_random_int(
window_size.get_width() - 100);
const int random_y = core::system::get_random_int(
window_size.get_height() - 100);

const int random_red = core::system::get_random_int(100);
const int random_green = core::system::get_random_int(100);
const int random_blue = core::system::get_random_int(100);

b->get_typed_control().set_foreground_color(draw::colors::make_white());

b->get_typed_control()
.set_background_color(
draw::color{random_red, random_green, random_blue});

b->set_position({random_x, random_y});
}
}
3 changes: 2 additions & 1 deletion code/classeine-demo/demo_layers_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace clsn::demo
explicit demo_layers_control(ui::layer_base&);

private:
void create_layer();
void create_normal_layer();
void create_self_destroyable_layer();
};
}
12 changes: 9 additions & 3 deletions code/classeine-lib/clsn/ui/default_ui_layer_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,26 @@ namespace clsn::ui
// To avoid iterator invalidation if new layers created / destroyed
auto current_layers = get_all_layers();

for (auto& layer : std::ranges::views::reverse(current_layers))
for (auto& layer_ref : std::ranges::views::reverse(current_layers))
{
if (e.is_consumed())
return;

auto& ctrl = layer.get_ref().get_control();
auto& _layer = layer_ref.get_ref();
auto& ctrl = _layer.get_control();

if (ctrl.contains_point(e.get_point()))
{
core::console::debug("Control with point: {}", ctrl);
ctrl.notify_mouse_click_event(e);
if (!layer.get_ref().is_transparent())
if (!_layer.is_transparent())
return;
}

if (_layer.get_layer_mode() == layer_mode::self_destroyable)
{
remove_layer(_layer);
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion code/classeine-lib/clsn/ui/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

namespace clsn::ui
{
layer_base::layer_base(ui_manager& ui_mgr)
layer_base::layer_base(ui_manager& ui_mgr, layer_mode mode)
: m_ui_manager{ui_mgr}
, m_layer_mode{mode}
{
m_theme_change_scope_guard = m_ui_manager
.get_theme_manager()
Expand Down Expand Up @@ -134,4 +135,10 @@ namespace clsn::ui
{
return m_previous_layer;
}

auto layer_base::get_layer_mode() const -> layer_mode
{
return m_layer_mode;
}
}

11 changes: 8 additions & 3 deletions code/classeine-lib/clsn/ui/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include <clsn/ui/control.h>
#include <clsn/ui/layer_mode.h>
#include <clsn/ui/ui_manager.h>
#include <clsn/ui/ui_mouse_manager.h>
#include <clsn/ui/ui_theme_manager.h>
Expand All @@ -24,6 +25,8 @@ namespace clsn::ui
{
ui_manager& m_ui_manager;

layer_mode m_layer_mode;

std::shared_ptr<layer_base> m_next_layer;
core::opt_ref<layer_base> m_previous_layer;

Expand All @@ -37,7 +40,7 @@ namespace clsn::ui
core::event_listener_scope_guard m_theme_change_scope_guard;

protected:
explicit layer_base(ui_manager& ui_mgr);
explicit layer_base(ui_manager& ui_mgr, layer_mode mode);

public:
virtual ~layer_base() = default;
Expand All @@ -57,6 +60,8 @@ namespace clsn::ui
auto get_next_layer() const -> const std::shared_ptr<layer_base>&;
auto get_previous_layer() const -> core::opt_ref<layer_base>;

auto get_layer_mode() const -> layer_mode;

[[nodiscard]] virtual auto get_control() const -> const control& = 0;
[[nodiscard]] virtual auto get_control() -> control& = 0;

Expand All @@ -79,8 +84,8 @@ namespace clsn::ui

public:
template <typename... Args>
explicit layer(ui_manager& ui_mgr, Args&&... args)
: base{ui_mgr}
explicit layer(ui_manager& ui_mgr, layer_mode mode, Args&&... args)
: base{ui_mgr, mode}
, m_control{*this, std::forward<Args>(args)...}
{
init_main_window_visibility();
Expand Down
11 changes: 11 additions & 0 deletions code/classeine-lib/clsn/ui/layer_mode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace clsn::ui
{
enum class layer_mode
{
normal,
modal,
self_destroyable
};
}
4 changes: 2 additions & 2 deletions code/classeine-lib/clsn/ui/ui_layer_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ namespace clsn::ui
virtual void remove_layer(layer_base& _layer) = 0;

template <typename ControlType, typename... Args>
auto make_and_add(Args&&... args)
auto make_and_add(layer_mode mode, Args&&... args)
{
auto& ui_manager = get_ui_manager();

auto _layer = std::make_shared<layer<ControlType>>(
ui_manager, std::forward<Args>(args)...);
ui_manager, mode, std::forward<Args>(args)...);

ui_manager.get_layer_manager().add_layer(_layer);
return _layer;
Expand Down
3 changes: 3 additions & 0 deletions code/classeine-lib/clsn/ui/ui_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <clsn/core/entity.h>

#include <clsn/ui/layer_mode.h>

#include <memory>

namespace clsn::ui
Expand Down Expand Up @@ -43,6 +45,7 @@ namespace clsn::ui

auto layer = ui_mgr.get_layer_manager()
.template make_and_add<ControlType>(
layer_mode::normal,
std::forward<Args>(args)...);

layer->set_size(
Expand Down

0 comments on commit 68d3331

Please sign in to comment.