Compare commits
4 Commits
49d5930912
...
4b47a24bb1
Author | SHA1 | Date | |
---|---|---|---|
4b47a24bb1 | |||
2c1e476e4b | |||
6138e8ef66 | |||
747c720159 |
BIN
base/usr/share/cursors/default.tga
Normal file
BIN
base/usr/share/cursors/default.tga
Normal file
Binary file not shown.
After Width: | Height: | Size: 1004 B |
BIN
base/usr/share/icons/16x16/app-close.tga
Normal file
BIN
base/usr/share/icons/16x16/app-close.tga
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.0 KiB |
@ -7,6 +7,7 @@ set(SOURCES
|
|||||||
src/Canvas.cpp
|
src/Canvas.cpp
|
||||||
src/Rect.cpp
|
src/Rect.cpp
|
||||||
src/Font.cpp
|
src/Font.cpp
|
||||||
|
src/Image.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(ui ${SOURCES})
|
add_library(ui ${SOURCES})
|
||||||
|
@ -52,5 +52,13 @@ namespace ui
|
|||||||
* @param color The color to use.
|
* @param color The color to use.
|
||||||
*/
|
*/
|
||||||
void fill(Color color);
|
void fill(Color color);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Fill the canvas with pixels.
|
||||||
|
*
|
||||||
|
* @param pixels The array of pixels (must be at least width*height).
|
||||||
|
* @param stride The number of pixels to skip to go to the next line.
|
||||||
|
*/
|
||||||
|
void fill(u32* pixels, int stride);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
71
libui/include/ui/Image.h
Normal file
71
libui/include/ui/Image.h
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <luna/Buffer.h>
|
||||||
|
#include <luna/SharedPtr.h>
|
||||||
|
#include <os/Path.h>
|
||||||
|
|
||||||
|
namespace ui
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @brief An image in the TGA file format.
|
||||||
|
*/
|
||||||
|
class Image : public Shareable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Load a new TGA image from a file.
|
||||||
|
*
|
||||||
|
* @param path The path to open.
|
||||||
|
* @return Result<SharedPtr<Image>> An error, or a new Image object.
|
||||||
|
*/
|
||||||
|
static Result<SharedPtr<Image>> load(const os::Path& path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Return the array of pixels contained in the image.
|
||||||
|
*
|
||||||
|
* @return u32* The array of pixels.
|
||||||
|
*/
|
||||||
|
u32* pixels()
|
||||||
|
{
|
||||||
|
return (u32*)m_image_data.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Return the width of the image.
|
||||||
|
*
|
||||||
|
* @return u16 The width.
|
||||||
|
*/
|
||||||
|
u16 width()
|
||||||
|
{
|
||||||
|
return m_tga_header.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Return the height of the image.
|
||||||
|
*
|
||||||
|
* @return u16 The height.
|
||||||
|
*/
|
||||||
|
u16 height()
|
||||||
|
{
|
||||||
|
return m_tga_header.h;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct [[gnu::packed]] TGAHeader
|
||||||
|
{
|
||||||
|
u8 idlen;
|
||||||
|
u8 colormap;
|
||||||
|
u8 encoding;
|
||||||
|
u16 cmaporig, cmaplen;
|
||||||
|
u8 cmapent;
|
||||||
|
u16 x;
|
||||||
|
u16 y;
|
||||||
|
u16 w;
|
||||||
|
u16 h;
|
||||||
|
u8 bpp;
|
||||||
|
u8 pixeltype;
|
||||||
|
};
|
||||||
|
|
||||||
|
TGAHeader m_tga_header;
|
||||||
|
Buffer m_image_data;
|
||||||
|
};
|
||||||
|
}
|
@ -33,4 +33,21 @@ namespace ui
|
|||||||
p += stride * sizeof(Color);
|
p += stride * sizeof(Color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Canvas::fill(u32* pixels, int _stride)
|
||||||
|
{
|
||||||
|
u8* p = ptr;
|
||||||
|
for (int i = 0; i < height; i++)
|
||||||
|
{
|
||||||
|
u32* colorp = (u32*)p;
|
||||||
|
for (int j = 0; j < width; j++)
|
||||||
|
{
|
||||||
|
u32 pix = pixels[j];
|
||||||
|
if (Color::from_u32(pix).alpha() == 0xff) *colorp = pix;
|
||||||
|
colorp++;
|
||||||
|
}
|
||||||
|
pixels += _stride;
|
||||||
|
p += stride * sizeof(Color);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
24
libui/src/Image.cpp
Normal file
24
libui/src/Image.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <os/File.h>
|
||||||
|
#include <ui/Image.h>
|
||||||
|
|
||||||
|
namespace ui
|
||||||
|
{
|
||||||
|
Result<SharedPtr<Image>> Image::load(const os::Path& path)
|
||||||
|
{
|
||||||
|
auto image = TRY(make_shared<Image>());
|
||||||
|
auto file = TRY(os::File::open(path, os::File::ReadOnly));
|
||||||
|
|
||||||
|
TRY(file->read_typed(image->m_tga_header));
|
||||||
|
|
||||||
|
if (image->m_tga_header.encoding != 2) todo();
|
||||||
|
if (image->m_tga_header.bpp != 32) todo();
|
||||||
|
|
||||||
|
Buffer image_id;
|
||||||
|
TRY(file->read(image_id, image->m_tga_header.idlen));
|
||||||
|
|
||||||
|
TRY(file->read(image->m_image_data,
|
||||||
|
image->m_tga_header.w * image->m_tga_header.h * (image->m_tga_header.bpp / 8)));
|
||||||
|
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,23 @@
|
|||||||
#include "Mouse.h"
|
#include "Mouse.h"
|
||||||
#include <os/File.h>
|
#include <os/File.h>
|
||||||
|
#include <ui/Image.h>
|
||||||
|
|
||||||
|
static SharedPtr<ui::Image> g_mouse_cursor;
|
||||||
|
|
||||||
Mouse::Mouse(ui::Canvas& screen)
|
Mouse::Mouse(ui::Canvas& screen)
|
||||||
{
|
{
|
||||||
m_position.x = screen.width / 2;
|
m_position.x = screen.width / 2;
|
||||||
m_position.y = screen.height / 2;
|
m_position.y = screen.height / 2;
|
||||||
m_screen_rect = screen.rect();
|
m_screen_rect = screen.rect();
|
||||||
|
|
||||||
|
g_mouse_cursor = ui::Image::load("/usr/share/cursors/default.tga").value_or({});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mouse::draw(ui::Canvas& screen)
|
void Mouse::draw(ui::Canvas& screen)
|
||||||
{
|
{
|
||||||
auto canvas = screen.subcanvas(ui::Rect { m_position, 10, 10 });
|
if (!g_mouse_cursor) return;
|
||||||
canvas.fill(ui::WHITE);
|
auto canvas = screen.subcanvas(ui::Rect { m_position, g_mouse_cursor->width(), g_mouse_cursor->height() });
|
||||||
|
canvas.fill(g_mouse_cursor->pixels(), g_mouse_cursor->width());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mouse::update(const moon::MousePacket& packet)
|
void Mouse::update(const moon::MousePacket& packet)
|
||||||
@ -43,19 +49,29 @@ void Mouse::update(const moon::MousePacket& packet)
|
|||||||
for (Window* window = g_windows.last().value_or(nullptr); window;
|
for (Window* window = g_windows.last().value_or(nullptr); window;
|
||||||
window = g_windows.previous(window).value_or(nullptr))
|
window = g_windows.previous(window).value_or(nullptr))
|
||||||
{
|
{
|
||||||
if (window->surface.absolute(window->titlebar).contains(m_position))
|
if (window->surface.absolute(window->close_button).contains(m_position))
|
||||||
|
{
|
||||||
|
// Close button pressed
|
||||||
|
g_windows.remove(window);
|
||||||
|
delete window;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (window->surface.absolute(window->titlebar).contains(m_position))
|
||||||
{
|
{
|
||||||
m_dragging_window = window;
|
m_dragging_window = window;
|
||||||
m_initial_drag_position =
|
m_initial_drag_position = window->surface.relative(m_position);
|
||||||
ui::Point { m_position.x - window->surface.pos.x, m_position.y - window->surface.pos.y };
|
|
||||||
os::println("Started drag: window at (%d,%d,%d,%d) with offset (%d,%d)", window->surface.pos.x,
|
os::println("Started drag: window at (%d,%d,%d,%d) with offset (%d,%d)", window->surface.pos.x,
|
||||||
window->surface.pos.y, window->surface.width, window->surface.height,
|
window->surface.pos.y, window->surface.width, window->surface.height,
|
||||||
m_initial_drag_position.x, m_initial_drag_position.y);
|
m_initial_drag_position.x, m_initial_drag_position.y);
|
||||||
window->focus();
|
window->focus();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else if (window->surface.absolute(window->contents).contains(m_position))
|
else if (window->surface.absolute(window->contents).contains(m_position))
|
||||||
|
{
|
||||||
|
window->focus();
|
||||||
break; // We don't want to continue iterating, otherwise this would take into account windows whose
|
break; // We don't want to continue iterating, otherwise this would take into account windows whose
|
||||||
// titlebar is underneath another window's contents!
|
// titlebar is underneath another window's contents!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -2,9 +2,12 @@
|
|||||||
#include <luna/Utf8.h>
|
#include <luna/Utf8.h>
|
||||||
#include <os/File.h>
|
#include <os/File.h>
|
||||||
#include <ui/Font.h>
|
#include <ui/Font.h>
|
||||||
|
#include <ui/Image.h>
|
||||||
|
|
||||||
LinkedList<Window> g_windows;
|
LinkedList<Window> g_windows;
|
||||||
|
|
||||||
|
static SharedPtr<ui::Image> g_close_icon;
|
||||||
|
|
||||||
void Window::draw(ui::Canvas& screen)
|
void Window::draw(ui::Canvas& screen)
|
||||||
{
|
{
|
||||||
auto window = screen.subcanvas(surface);
|
auto window = screen.subcanvas(surface);
|
||||||
@ -21,6 +24,12 @@ void Window::draw(ui::Canvas& screen)
|
|||||||
|
|
||||||
auto textarea = titlebar_canvas.subcanvas(ui::Rect { 10, 10, titlebar_canvas.width - 10, titlebar_canvas.height });
|
auto textarea = titlebar_canvas.subcanvas(ui::Rect { 10, 10, titlebar_canvas.width - 10, titlebar_canvas.height });
|
||||||
font->render(buffer, ui::BLACK, textarea);
|
font->render(buffer, ui::BLACK, textarea);
|
||||||
|
|
||||||
|
if (g_close_icon)
|
||||||
|
{
|
||||||
|
auto close_area = window.subcanvas(close_button);
|
||||||
|
close_area.fill(g_close_icon->pixels(), g_close_icon->width());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::focus()
|
void Window::focus()
|
||||||
@ -34,6 +43,9 @@ Window::Window(ui::Rect r, ui::Color c, StringView n) : surface(r), color(c), na
|
|||||||
{
|
{
|
||||||
auto font = ui::Font::default_font();
|
auto font = ui::Font::default_font();
|
||||||
titlebar = ui::Rect { 0, 0, surface.width, font->height() + 20 };
|
titlebar = ui::Rect { 0, 0, surface.width, font->height() + 20 };
|
||||||
|
close_button = ui::Rect { surface.width - 26, 10, 16, 16 };
|
||||||
contents = ui::Rect { 0, font->height() + 20, surface.width, surface.height - (font->height() + 20) };
|
contents = ui::Rect { 0, font->height() + 20, surface.width, surface.height - (font->height() + 20) };
|
||||||
g_windows.append(this);
|
g_windows.append(this);
|
||||||
|
|
||||||
|
if (!g_close_icon) g_close_icon = ui::Image::load("/usr/share/icons/16x16/app-close.tga").value_or({});
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ struct Window : public LinkedListNode<Window>
|
|||||||
{
|
{
|
||||||
ui::Rect surface;
|
ui::Rect surface;
|
||||||
ui::Rect titlebar;
|
ui::Rect titlebar;
|
||||||
|
ui::Rect close_button;
|
||||||
ui::Rect contents;
|
ui::Rect contents;
|
||||||
ui::Color color;
|
ui::Color color;
|
||||||
StringView name;
|
StringView name;
|
||||||
|
Loading…
Reference in New Issue
Block a user