display

Layout and Rendering TUI library
git clone git://git.dimitrijedobrota.com/display.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 17bfa4acf63943274ef2fd63879394793aa2a55c
parent 2479b140bae99f942c5f3ea57bffa321a9e42c6d
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Wed, 12 Feb 2025 11:26:29 +0100

Pos now 2d, get rid of unnecessary stuff

Diffstat:
M CMakeLists.txt | + -
M example/example.cpp | ++ -
M include/display/layout_free.hpp | --
M include/display/types.hpp | +++++ ------
M include/display/window.hpp | + ----------
M include/display/window_pivot.hpp | ++++++ ---
M source/layout_free.cpp | + ------------------
M source/window_pivot.cpp | + -

8 files changed, 17 insertions(+), 42 deletions(-)


diff --git a/ CMakeLists.txt b/ CMakeLists.txt

@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)


project(
display
VERSION 0.1.16
VERSION 0.1.17
DESCRIPTION "TUI library"
HOMEPAGE_URL "https://example.com/"
LANGUAGES CXX

diff --git a/ example/example.cpp b/ example/example.cpp

@@ -59,7 +59,6 @@ int main()

using namespace display; // NOLINT

auto& display = Display::display();
auto& layout = display.screen().set_layout<LayoutRigid>(nullptr);

const auto recalc = [](std::size_t start, LayoutFree& layout)
{

@@ -78,6 +77,8 @@ int main()

layout.get<WindowPivot>(8).pos() = {midw, midh};
};

auto& layout = display.screen().set_layout<LayoutRigid>(nullptr);

auto& layout1 =
layout.screen1().set_layout<LayoutFree>(std::bind(recalc, 4U, _1));
fill(layout1);

diff --git a/ include/display/layout_free.hpp b/ include/display/layout_free.hpp

@@ -28,7 +28,6 @@ public:

T& append(Args&&... args)
{
m_wins.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
m_is_sorted = false;
return get<T>(m_wins.size() - 1);
}

@@ -51,7 +50,6 @@ private:

recalc_f m_recalc;

std::vector<std::unique_ptr<Window>> m_wins;
mutable bool m_is_sorted = true;
};

} // namespace display

diff --git a/ include/display/types.hpp b/ include/display/types.hpp

@@ -28,23 +28,22 @@ struct dim_t


struct pos_t
{
pos_t(sz_t xpos = 0, sz_t ypos = 0, sz_t zpos = 0) // NOLINT
pos_t(sz_t xpos = 0, sz_t ypos = 0) // NOLINT
: x(xpos)
, y(ypos)
, z(zpos)
{
}

pos_t operator+(pos_t rhs) const
{
return {static_cast<std::uint16_t>(x + rhs.x),
static_cast<std::uint16_t>(y + rhs.y),
static_cast<std::uint16_t>(z + rhs.z)};
return {
static_cast<std::uint16_t>(x + rhs.x),
static_cast<std::uint16_t>(y + rhs.y),
};
}

sz_t x;
sz_t y;
sz_t z;
};

struct place_t

diff --git a/ include/display/window.hpp b/ include/display/window.hpp

@@ -10,10 +10,7 @@ namespace display

class Window
{
public:
Window(pos_t pos = {}) // NOLINT
: m_pos(pos)
{
}
Window() = default;

Window(const Window&) = delete;
Window& operator=(const Window&) = delete;

@@ -23,14 +20,8 @@ public:


virtual ~Window() = default;

const auto& pos() const { return m_pos; }
auto& pos() { return m_pos; }

virtual std::optional<place_t> place(dim_t bounds) = 0;
virtual void render(place_t place) const = 0;

private:
pos_t m_pos;
};

} // namespace display

diff --git a/ include/display/window_pivot.hpp b/ include/display/window_pivot.hpp

@@ -1,5 +1,4 @@

#pragma once

#include <functional>

#include "display/types.hpp"

@@ -14,13 +13,16 @@ public:

using render_f = std::function<void(const WindowPivot&, place_t place)>;

WindowPivot(render_f frender, pos_t pos, dim_t dim, piv_t piv = {})
: Window(pos)
, m_renderer(std::move(frender))
: m_renderer(std::move(frender))
, m_pos(pos)
, m_dim(dim)
, m_piv(piv)
{
}

const auto& pos() const { return m_pos; }
auto& pos() { return m_pos; }

const auto& dim() const { return m_dim; }
auto& dim() { return m_dim; }

@@ -32,6 +34,7 @@ public:


private:
render_f m_renderer;
pos_t m_pos;
dim_t m_dim;
piv_t m_piv;
};

diff --git a/ source/layout_free.cpp b/ source/layout_free.cpp

@@ -1,6 +1,3 @@

#include <algorithm>
#include <numeric>

#include "display/layout_free.hpp"

namespace display

@@ -16,21 +13,7 @@ void LayoutFree::resize(dim_t dim)


int LayoutFree::render(pos_t pos) const
{
static std::vector<std::uint8_t> idxs;

if (!m_is_sorted) {
idxs.resize(m_wins.size());
std::iota(idxs.begin(), idxs.end(), 0);
std::stable_sort(
idxs.begin(),
idxs.end(),
[&](auto left, auto right)
{ return m_wins[left]->pos().z < m_wins[right]->pos().z; });
m_is_sorted = true;
}

for (const auto idx : idxs) {
const auto& win = m_wins[idx];
for (const auto& win : m_wins) {
const auto plc = win->place(this->dim());

if (!plc.has_value()) {

diff --git a/ source/window_pivot.cpp b/ source/window_pivot.cpp

@@ -15,7 +15,7 @@ void WindowPivot::render(place_t place) const

std::optional<place_t> WindowPivot::place(dim_t bounds)
{
const auto [cols, rows] = bounds;
const auto [posx, posy, _] = pos();
const auto [posx, posy] = pos();

if (posx > cols || posy > rows) {
return {};