libui: Add a GUI and graphics library

This commit is contained in:
apio 2023-08-03 12:19:45 +02:00
parent 7076ff2f6b
commit 8f170ff1f0
Signed by: apio
GPG Key ID: B8A7D06E42258954
11 changed files with 175 additions and 1 deletions

View File

@ -45,6 +45,7 @@ endif()
add_subdirectory(libluna) add_subdirectory(libluna)
add_subdirectory(libos) add_subdirectory(libos)
add_subdirectory(libui)
add_subdirectory(libc) add_subdirectory(libc)
add_subdirectory(kernel) add_subdirectory(kernel)
add_subdirectory(apps) add_subdirectory(apps)

15
libui/CMakeLists.txt Normal file
View File

@ -0,0 +1,15 @@
# The UI and graphics library for Luna.
file(GLOB HEADERS include/ui/*.h)
set(SOURCES
${HEADERS}
src/Canvas.cpp
src/Color.cpp
src/Rect.cpp
)
add_library(ui ${SOURCES})
target_compile_options(ui PRIVATE ${COMMON_FLAGS})
target_include_directories(ui PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/)
target_include_directories(ui PUBLIC ${LUNA_BASE}/usr/include)

30
libui/include/ui/Canvas.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <luna/Result.h>
#include <luna/Types.h>
#include <ui/Color.h>
#include <ui/Point.h>
#include <ui/Rect.h>
namespace ui
{
/**
* @brief A drawable surface.
*/
struct Canvas
{
int width;
int height;
int stride;
u8* ptr;
static Canvas create(u8* ptr, int width, int height);
Result<Canvas> subcanvas(Point begin, int width, int height, bool clamp);
Rect rect()
{
return Rect { .begin = { 0, 0 }, .width = width, .height = height };
}
void fill(Color color);
};
};

34
libui/include/ui/Color.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include <luna/Types.h>
namespace ui
{
/**
* @brief A 32-bit RGBA color.
*/
struct Color
{
union {
u32 raw;
u8 colors[4];
};
static constexpr Color from_u32(u32 raw)
{
return Color { .raw = raw };
}
static constexpr Color from_rgba(u8 red, u8 green, u8 blue, u8 alpha)
{
return Color { .colors = { blue, green, red, alpha } };
}
static constexpr Color from_rgb(u8 red, u8 green, u8 blue)
{
return from_rgba(red, green, blue, 0xff);
}
};
static constexpr Color WHITE = Color::from_rgb(0xff, 0xff, 0xff);
static constexpr Color BLACK = Color::from_rgb(0x00, 0x00, 0x00);
};

13
libui/include/ui/Point.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
namespace ui
{
/**
* @brief A point in 2D space.
*/
struct Point
{
int x { 0 };
int y { 0 };
};
}

18
libui/include/ui/Rect.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <ui/Point.h>
namespace ui
{
/**
* @brief A simple rectangle.
*/
struct Rect
{
Point begin;
int width;
int height;
bool contains(Point point);
Point normalize(Point point);
};
}

43
libui/src/Canvas.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <string.h>
#include <ui/Canvas.h>
namespace ui
{
Canvas Canvas::create(u8* ptr, int width, int height)
{
return Canvas { .width = width, .height = height, .stride = width, .ptr = ptr };
}
Result<Canvas> Canvas::subcanvas(Point begin, int w, int h, bool clamp)
{
if (!clamp)
{
if (begin.x + w > width) return err(ERANGE);
if (begin.y + h > height) return err(ERANGE);
}
else
{
if (begin.x + w > width) w = width - begin.x;
if (begin.y + h > height) h = height - begin.y;
}
u8* p = ptr + begin.x * sizeof(Color) + (begin.y * sizeof(Color) * stride);
return Canvas { .width = w, .height = h, .stride = stride, .ptr = p };
}
void Canvas::fill(Color color)
{
u8* p = ptr;
for (int i = 0; i < height; i++)
{
u32* colorp = (u32*)p;
for (int j = 0; j < width; j++)
{
*colorp = color.raw;
colorp++;
}
p += stride * sizeof(Color);
}
}
}

0
libui/src/Color.cpp Normal file
View File

18
libui/src/Rect.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <ui/Rect.h>
namespace ui
{
bool Rect::contains(Point point)
{
return point.x >= begin.x && point.y >= begin.y && point.x <= begin.x + width && point.y <= begin.y + height;
}
Point Rect::normalize(Point point)
{
if (point.x < begin.x) point.x = begin.x;
if (point.y < begin.y) point.y = begin.y;
if (point.x > begin.x + width) point.x = begin.x + width;
if (point.y > begin.y + height) point.y = begin.y + height;
return point;
}
};

View File

@ -8,10 +8,12 @@ cd $LUNA_ROOT
mkdir -p $LUNA_BASE mkdir -p $LUNA_BASE
mkdir -p $LUNA_BASE/usr/include mkdir -p $LUNA_BASE/usr/include
mkdir -p $LUNA_BASE/usr/include/luna mkdir -p $LUNA_BASE/usr/include/luna
mkdir -p $LUNA_BASE/usr/include/ui
mkdir -p $LUNA_BASE/usr/include/os mkdir -p $LUNA_BASE/usr/include/os
mkdir -p $LUNA_BASE/usr/include/moon mkdir -p $LUNA_BASE/usr/include/moon
cp --preserve=timestamps -RT libc/include/ $LUNA_BASE/usr/include cp --preserve=timestamps -RT libc/include/ $LUNA_BASE/usr/include
cp --preserve=timestamps -RT libluna/include/luna/ $LUNA_BASE/usr/include/luna cp --preserve=timestamps -RT libluna/include/luna/ $LUNA_BASE/usr/include/luna
cp --preserve=timestamps -RT libui/include/ui/ $LUNA_BASE/usr/include/ui
cp --preserve=timestamps -RT libos/include/os/ $LUNA_BASE/usr/include/os cp --preserve=timestamps -RT libos/include/os/ $LUNA_BASE/usr/include/os
cp --preserve=timestamps -RT kernel/src/api/ $LUNA_BASE/usr/include/moon cp --preserve=timestamps -RT kernel/src/api/ $LUNA_BASE/usr/include/moon

View File

@ -4,7 +4,7 @@ source $(dirname $0)/env.sh
cd $LUNA_ROOT cd $LUNA_ROOT
FOLDERS=(kernel libc libos libluna apps shell tests) FOLDERS=(kernel libc libos libui libluna apps shell tests)
SOURCES=($(find ${FOLDERS[@]} -type f -name "*.cpp")) SOURCES=($(find ${FOLDERS[@]} -type f -name "*.cpp"))
SOURCES+=($(find ${FOLDERS[@]} -type f -name "*.h")) SOURCES+=($(find ${FOLDERS[@]} -type f -name "*.h"))