Compare commits

..

No commits in common. "4b47a24bb1db568f21cc4865409567167fb621dc" and "49d59309129f70445b60a028f2a46df8652fbf29" have entirely different histories.

10 changed files with 5 additions and 155 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1004 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -7,7 +7,6 @@ set(SOURCES
src/Canvas.cpp
src/Rect.cpp
src/Font.cpp
src/Image.cpp
)
add_library(ui ${SOURCES})

View File

@ -52,13 +52,5 @@ namespace ui
* @param color The color to use.
*/
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);
};
};

View File

@ -1,71 +0,0 @@
#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;
};
}

View File

@ -33,21 +33,4 @@ namespace ui
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);
}
}
}

View File

@ -1,24 +0,0 @@
#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;
}
}

View File

@ -1,23 +1,17 @@
#include "Mouse.h"
#include <os/File.h>
#include <ui/Image.h>
static SharedPtr<ui::Image> g_mouse_cursor;
Mouse::Mouse(ui::Canvas& screen)
{
m_position.x = screen.width / 2;
m_position.y = screen.height / 2;
m_screen_rect = screen.rect();
g_mouse_cursor = ui::Image::load("/usr/share/cursors/default.tga").value_or({});
}
void Mouse::draw(ui::Canvas& screen)
{
if (!g_mouse_cursor) return;
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());
auto canvas = screen.subcanvas(ui::Rect { m_position, 10, 10 });
canvas.fill(ui::WHITE);
}
void Mouse::update(const moon::MousePacket& packet)
@ -49,29 +43,19 @@ void Mouse::update(const moon::MousePacket& packet)
for (Window* window = g_windows.last().value_or(nullptr); window;
window = g_windows.previous(window).value_or(nullptr))
{
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))
if (window->surface.absolute(window->titlebar).contains(m_position))
{
m_dragging_window = window;
m_initial_drag_position = window->surface.relative(m_position);
m_initial_drag_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,
window->surface.pos.y, window->surface.width, window->surface.height,
m_initial_drag_position.x, m_initial_drag_position.y);
window->focus();
break;
}
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
// titlebar is underneath another window's contents!
}
}
}
}

View File

@ -2,12 +2,9 @@
#include <luna/Utf8.h>
#include <os/File.h>
#include <ui/Font.h>
#include <ui/Image.h>
LinkedList<Window> g_windows;
static SharedPtr<ui::Image> g_close_icon;
void Window::draw(ui::Canvas& screen)
{
auto window = screen.subcanvas(surface);
@ -24,12 +21,6 @@ void Window::draw(ui::Canvas& screen)
auto textarea = titlebar_canvas.subcanvas(ui::Rect { 10, 10, titlebar_canvas.width - 10, titlebar_canvas.height });
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()
@ -43,9 +34,6 @@ Window::Window(ui::Rect r, ui::Color c, StringView n) : surface(r), color(c), na
{
auto font = ui::Font::default_font();
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) };
g_windows.append(this);
if (!g_close_icon) g_close_icon = ui::Image::load("/usr/share/icons/16x16/app-close.tga").value_or({});
}

View File

@ -9,7 +9,6 @@ struct Window : public LinkedListNode<Window>
{
ui::Rect surface;
ui::Rect titlebar;
ui::Rect close_button;
ui::Rect contents;
ui::Color color;
StringView name;