Skip to content

Commit

Permalink
cla-137: added support for center layers (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebasconp authored Jan 9, 2025
1 parent 34b9322 commit 9b82eb6
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG #

### 0.1.2-alpha
* 20250109 added support for center layers
* 20250109 added support for modal layers
* 20250108 added `control_factory` and `layer_factory`
* 20250107 added `layer_mode` enum

Expand Down
46 changes: 46 additions & 0 deletions code/classeine-demo/demo_layers_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace clsn::demo
demo_layers_control::demo_layers_control(ui::layer_base& _layer)
: base{_layer}
{
// Normal layer
auto create_normal_layer_btn =
this->add(ui::control_factory::make<ui::button>(_layer), draw::point{100, 100});

Expand All @@ -27,6 +28,7 @@ namespace clsn::demo
create_normal_layer_btn->set_font(
create_normal_layer_btn->get_actual_font().add_size(6));

// Self-destroyable layer
auto create_destroyable_layer_btn =
this->add(ui::control_factory::make<ui::button>(_layer), draw::point{300, 100});

Expand All @@ -39,6 +41,7 @@ namespace clsn::demo
create_destroyable_layer_btn->set_font(
create_destroyable_layer_btn->get_actual_font().add_size(6));

// Modal layer
auto create_modal_layer_btn =
this->add(ui::control_factory::make<ui::button>(_layer), draw::point{100, 200});

Expand All @@ -50,6 +53,21 @@ namespace clsn::demo
});
create_modal_layer_btn->set_font(
create_modal_layer_btn->get_actual_font().add_size(6));

// Modal centered layer
auto create_modal_centered_layer_btn = this->add(
ui::control_factory::make<ui::button>(_layer),
draw::point{300, 200});

create_modal_centered_layer_btn->set_caption("Modal centered layer");
create_modal_centered_layer_btn->set_preferred_size(
draw::dimension{190, 40});
create_modal_centered_layer_btn->add_action_listener([this](auto&)
{
create_modal_centered_layer();
});
create_modal_centered_layer_btn->set_font(
create_modal_centered_layer_btn->get_actual_font().add_size(6));
}

void demo_layers_control::create_normal_layer()
Expand Down Expand Up @@ -156,4 +174,32 @@ namespace clsn::demo
e.get_ui_manager().get_layer_manager().remove_layer(e.get_layer());
});
}

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

auto b = ui::layer_factory::make_and_add<ui::button>
(get_ui_manager(), ui::layer_mode::modal);

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

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

const auto cr = core::system::get_random_int(0, 2) * 255;
const auto cg = core::system::get_random_int(0, 2) * 255;
const auto cb = core::system::get_random_int(0, 2) * 255;

b->get_typed_control().set_background_color(draw::color{cr, cg, cb});
b->get_typed_control().set_foreground_color(draw::color{80, 80, 80});

b->set_position_at_center();

b->get_typed_control().add_action_listener([](auto& e)
{
e.get_ui_manager().get_layer_manager().remove_layer(e.get_layer());
});
}
}
1 change: 1 addition & 0 deletions code/classeine-demo/demo_layers_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ namespace clsn::demo
void create_normal_layer();
void create_self_destroyable_layer();
void create_modal_layer();
void create_modal_centered_layer();
};
}
1 change: 1 addition & 0 deletions code/classeine-lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ add_library(classeine_lib
clsn/draw/color.cpp
clsn/draw/colors.cpp
clsn/draw/dimension.cpp
clsn/draw/draw_utils.cpp
clsn/draw/font_info.cpp
clsn/draw/point.cpp
clsn/draw/region.cpp
Expand Down
24 changes: 24 additions & 0 deletions code/classeine-lib/clsn/draw/draw_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file belongs to the Classeine project
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: © 2024-2025 Ernesto Bascón Pantoja

#include <clsn/draw/draw_utils.h>

#include <clsn/draw/dimension.h>

namespace clsn::draw
{
auto draw_utils::calculate_position_at_center(
const dimension& target,
const dimension& element_to_center) -> point
{
const auto center = draw::point{ target.get_width() / 2, target.get_height() / 2 };

return {
center.get_x() - element_to_center.get_width() / 2,
center.get_y() - element_to_center.get_height() / 2
};

}
}
23 changes: 23 additions & 0 deletions code/classeine-lib/clsn/draw/draw_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This file belongs to the Classeine project
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: © 2024-2025 Ernesto Bascón Pantoja

#pragma once

#include <clsn/core/non_instantiable.h>

#include <clsn/draw/point.h>

namespace clsn::draw
{
class dimension;

class draw_utils : public core::non_instantiable
{
public:
static auto calculate_position_at_center(
const dimension& target,
const dimension& element_to_center) -> point;
};
}
42 changes: 26 additions & 16 deletions code/classeine-lib/clsn/ui/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: © 2024 Ernesto Bascón Pantoja

#include <clsn/draw/draw_utils.h>

#include <clsn/ui/layer.h>

#include <clsn/ui/control.h>
Expand All @@ -22,17 +24,14 @@ namespace clsn::ui
: m_ui_manager{ui_mgr}
, m_layer_mode{mode}
{
m_theme_change_scope_guard = m_ui_manager
.get_theme_manager()
.add_theme_changed_listener([this](auto&) { update_theme(); });
m_theme_change_scope_guard =
m_ui_manager.get_theme_manager().add_theme_changed_listener(
[this](auto&) { update_theme(); });

init_layer_base_events();
}

auto layer_base::get_ui_manager() -> ui_manager&
{
return m_ui_manager;
}
auto layer_base::get_ui_manager() -> ui_manager& { return m_ui_manager; }

auto layer_base::get_ui_manager() const -> const ui_manager&
{
Expand All @@ -44,7 +43,7 @@ namespace clsn::ui
get_control().notify_mouse_click_event(e);
}

void layer_base::process_mouse_moved_event(events::mouse_moved_event &e)
void layer_base::process_mouse_moved_event(events::mouse_moved_event& e)
{
get_control().notify_mouse_moved_event(e);
}
Expand All @@ -57,7 +56,7 @@ namespace clsn::ui

void layer_base::load_window_defaults()
{
//set_minimum_size({50, 50});
// set_minimum_size({50, 50});
}

void layer_base::update_theme()
Expand Down Expand Up @@ -110,23 +109,25 @@ namespace clsn::ui

void layer_base::init_layer_base_events()
{
std::ignore = add_position_changed_listener([this](auto& e)
{
get_control().set_actual_position(e.get_new_value());
});
std::ignore = add_position_changed_listener(
[this](auto& e)
{ get_control().set_actual_position(e.get_new_value()); });
}

void layer_base::set_next_layer(const std::shared_ptr<layer_base>& next_layer)
void layer_base::set_next_layer(
const std::shared_ptr<layer_base>& next_layer)
{
m_next_layer = next_layer;
}

void layer_base::set_previous_layer(core::opt_ref<layer_base> previous_layer)
void layer_base::set_previous_layer(
core::opt_ref<layer_base> previous_layer)
{
m_previous_layer = previous_layer;
}

auto layer_base::get_next_layer() const -> const std::shared_ptr<layer_base>&
auto layer_base::get_next_layer() const
-> const std::shared_ptr<layer_base>&
{
return m_next_layer;
}
Expand All @@ -140,5 +141,14 @@ namespace clsn::ui
{
return m_layer_mode;
}

void layer_base::set_position_at_center()
{
const auto& window_size = m_ui_manager.get_system_backend().get_size();
const auto& layer_size = m_size.get();

set_position(draw::draw_utils::calculate_position_at_center(
window_size, layer_size));
}
}

2 changes: 2 additions & 0 deletions code/classeine-lib/clsn/ui/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ namespace clsn::ui

auto get_layer_mode() const -> layer_mode;

void set_position_at_center();

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

Expand Down

0 comments on commit 9b82eb6

Please sign in to comment.