libui: Add a basic Label component
This commit is contained in:
parent
67eac983b5
commit
4cf0fac16e
@ -16,6 +16,7 @@ set(SOURCES
|
||||
src/Alignment.cpp
|
||||
src/Container.cpp
|
||||
src/Button.cpp
|
||||
src/Label.cpp
|
||||
)
|
||||
|
||||
add_library(ui ${SOURCES})
|
||||
|
41
libui/include/ui/Label.h
Normal file
41
libui/include/ui/Label.h
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @file Label.h
|
||||
* @author apio (cloudapio.eu)
|
||||
* @brief A simple one-line text widget.
|
||||
*
|
||||
* @copyright Copyright (c) 2023, the Luna authors.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <ui/Alignment.h>
|
||||
#include <ui/Font.h>
|
||||
#include <ui/Widget.h>
|
||||
|
||||
namespace ui
|
||||
{
|
||||
/**
|
||||
* @brief Displays one line of text.
|
||||
*
|
||||
* This component does not handle newlines.
|
||||
*/
|
||||
class Label final : public Widget
|
||||
{
|
||||
public:
|
||||
Label(StringView text, VerticalAlignment valign = VerticalAlignment::Center,
|
||||
HorizontalAlignment halign = HorizontalAlignment::Center, SharedPtr<Font> font = Font::default_font());
|
||||
|
||||
void set_text(StringView text)
|
||||
{
|
||||
m_text = text;
|
||||
}
|
||||
|
||||
Result<void> draw(Canvas& canvas) override;
|
||||
|
||||
private:
|
||||
StringView m_text;
|
||||
VerticalAlignment m_valign;
|
||||
HorizontalAlignment m_halign;
|
||||
SharedPtr<Font> m_font;
|
||||
};
|
||||
}
|
36
libui/src/Label.cpp
Normal file
36
libui/src/Label.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @file Label.cpp
|
||||
* @author apio (cloudapio.eu)
|
||||
* @brief A simple one-line text widget.
|
||||
*
|
||||
* @copyright Copyright (c) 2023, the Luna authors.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <luna/Utf8.h>
|
||||
#include <ui/Label.h>
|
||||
|
||||
namespace ui
|
||||
{
|
||||
Label::Label(StringView text, VerticalAlignment valign, HorizontalAlignment halign, SharedPtr<Font> font)
|
||||
: m_text(text), m_valign(valign), m_halign(halign), m_font(font)
|
||||
{
|
||||
}
|
||||
|
||||
Result<void> Label::draw(Canvas& canvas)
|
||||
{
|
||||
ui::Rect contained;
|
||||
contained.pos = { 0, 0 };
|
||||
contained.width = static_cast<int>(m_text.length() * m_font->width());
|
||||
contained.height = m_font->height();
|
||||
auto subcanvas =
|
||||
canvas.subcanvas(ui::align({ 0, 0, m_rect.width, m_rect.height }, contained, m_valign, m_halign));
|
||||
|
||||
Utf8StringDecoder decoder(m_text.chars());
|
||||
wchar_t buf[4096];
|
||||
TRY(decoder.decode(buf, sizeof(buf)));
|
||||
|
||||
m_font->render(buf, ui::BLACK, subcanvas);
|
||||
return {};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user