Files
sphaira/sphaira/include/ui/object.hpp
ITotalJustice 54d73a6d3b optimise: pass all large objects (std::function, std::string, structs) by const ref rather than value.
really, these functions should be passed by && and using std::forward on assignment.
however, this means writing a lot of extra code for every single class, as well as explicitly calling move
in a lot of cases.

In the case of std::string, passing by value and calling std::move is the correct approach, especially if the
string is created as an rvalue, then it is only created once and moved into the dest.
Whereas with a const ref, the string is created and then copied into the dst, basically creating 2 copies.
The same thing happens std::function, and well any object.

However, accepting everything by value sucks if you call a constructor from within a constructor, as now you need to create 2 impls
that accept by value and the other by rvalue. All of this extra code to have a more efficent impl just isn't worth it when, going by
the benchmarks, makes no measurable difference (i count anything within >= 1ms as measurable).
2025-08-04 18:58:20 +01:00

89 lines
1.6 KiB
C++

#pragma once
#include "types.hpp"
#include <stop_token>
namespace sphaira::ui {
class Object {
public:
Object() = default;
virtual ~Object() {
m_stop_source.request_stop();
}
virtual auto Draw(NVGcontext* vg, Theme* theme) -> void = 0;
auto GetPos() const noexcept {
return m_pos;
}
auto GetX() const noexcept {
return m_pos.x;
}
auto GetY() const noexcept {
return m_pos.y;
}
auto GetW() const noexcept {
return m_pos.w;
}
auto GetH() const noexcept {
return m_pos.h;
}
auto SetX(float a) noexcept {
return m_pos.x = a;
}
auto SetY(float a) noexcept {
return m_pos.y = a;
}
auto SetW(float a) noexcept {
return m_pos.w = a;
}
auto SetH(float a) noexcept {
return m_pos.h = a;
}
auto SetPos(float x, float y, float w, float h) noexcept -> void {
m_pos = { x, y, w, h };
}
auto SetPos(const Vec4& v) noexcept -> void {
m_pos = v;
}
auto InXBounds(float x) const -> bool {
return x >= m_pos.x && x <= m_pos.x + m_pos.w;
}
auto InYBounds(float y) const -> bool {
return y >= m_pos.y && y <= m_pos.y + m_pos.h;
}
auto IsHidden() const noexcept -> bool {
return m_hidden;
}
auto SetHidden(bool value = true) noexcept -> void {
m_hidden = value;
}
auto GetToken() const {
return m_stop_source.get_token();
}
protected:
Vec4 m_pos{};
// used for lifetime management across threads.
std::stop_source m_stop_source{};
bool m_hidden{false};
};
} // namespace sphaira::ui