Skip to content

Commit

Permalink
Update ZWidget with latest VKDoom changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Dec 31, 2023
1 parent d5f1d0c commit 30e1c84
Show file tree
Hide file tree
Showing 28 changed files with 5,784 additions and 47 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ set(ZWIDGET_SOURCES
src/core/utf8reader.cpp
src/core/schrift/schrift.cpp
src/core/schrift/schrift.h
src/core/picopng/picopng.cpp
src/core/picopng/picopng.h
src/core/nanosvg/nanosvg.cpp
src/core/nanosvg/nanosvg.h
src/core/nanosvg/nanosvgrast.h
src/widgets/lineedit/lineedit.cpp
src/widgets/mainwindow/mainwindow.cpp
src/widgets/menubar/menubar.cpp
Expand Down Expand Up @@ -71,6 +76,8 @@ set(EXAMPLE_SOURCES
source_group("src" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/.+")
source_group("src\\core" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/core/.+")
source_group("src\\core\\schrift" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/core/schrift/.+")
source_group("src\\core\\picopng" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/core/picopng/.+")
source_group("src\\core\\nanosvg" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/core/nanosvg/.+")
source_group("src\\widgets" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/widgets/.+")
source_group("src\\widgets\\lineedit" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/widgets/lineedit/.+")
source_group("src\\widgets\\mainwindow" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/src/widgets/mainwindow/.+")
Expand Down
2 changes: 2 additions & 0 deletions include/zwidget/core/image.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <memory>
#include <string>

enum class ImageFormat
{
Expand All @@ -19,4 +20,5 @@ class Image
virtual void* GetData() const = 0;

static std::shared_ptr<Image> Create(int width, int height, ImageFormat format, const void* data);
static std::shared_ptr<Image> LoadResource(const std::string& resourcename, double dpiscale = 1.0);
};
1 change: 1 addition & 0 deletions include/zwidget/core/resourcedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
#include <string>

std::vector<uint8_t> LoadWidgetFontData(const std::string& name);
std::vector<uint8_t> LoadWidgetImageData(const std::string& name);
2 changes: 2 additions & 0 deletions include/zwidget/core/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ class Timer
Timer* PrevTimerObj = nullptr;
Timer* NextTimerObj = nullptr;

void* TimerId = nullptr;

friend class Widget;
};
3 changes: 3 additions & 0 deletions include/zwidget/core/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class Widget : DisplayWindowHost
Point MapToGlobal(const Point& pos) const;
Point MapToParent(const Point& pos) const { return MapTo(Parent(), pos); }

static Size GetScreenSize();

protected:
virtual void OnPaintFrame(Canvas* canvas) { }
virtual void OnPaint(Canvas* canvas) { }
Expand Down Expand Up @@ -173,6 +175,7 @@ class Widget : DisplayWindowHost
std::unique_ptr<Canvas> DispCanvas;
Widget* FocusWidget = nullptr;
Widget* CaptureWidget = nullptr;
Widget* HoverWidget = nullptr;

Widget(const Widget&) = delete;
Widget& operator=(const Widget&) = delete;
Expand Down
10 changes: 10 additions & 0 deletions include/zwidget/widgets/checkboxlabel/checkboxlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ class CheckboxLabel : public Widget
void SetText(const std::string& value);
const std::string& GetText() const;

void SetChecked(bool value);
bool GetChecked() const;
void Toggle();

double GetPreferredHeight() const;

protected:
void OnPaint(Canvas* canvas) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseUp(const Point& pos, int key) override;
void OnMouseLeave() override;
void OnKeyUp(EInputKey key) override;

private:
std::string text;
bool checked = false;
bool mouseDownActive = false;
};
16 changes: 16 additions & 0 deletions include/zwidget/widgets/listview/listview.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@
#pragma once

#include "../../core/widget.h"
#include <vector>
#include <functional>

class ListView : public Widget
{
public:
ListView(Widget* parent = nullptr);

void AddItem(const std::string& text);
int GetSelectedItem() const { return selectedItem; }

void Activate();

std::function<void()> OnActivated;

protected:
void OnPaint(Canvas* canvas) override;
void OnPaintFrame(Canvas* canvas) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseDoubleclick(const Point& pos, int key) override;
void OnKeyDown(EInputKey key) override;

std::vector<std::string> items;
int selectedItem = 0;
};
13 changes: 13 additions & 0 deletions include/zwidget/widgets/pushbutton/pushbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include "../../core/widget.h"
#include <functional>

class PushButton : public Widget
{
Expand All @@ -13,10 +14,22 @@ class PushButton : public Widget

double GetPreferredHeight() const;

void Click();

std::function<void()> OnClick;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnPaint(Canvas* canvas) override;
void OnMouseMove(const Point& pos) override;
void OnMouseDown(const Point& pos, int key) override;
void OnMouseUp(const Point& pos, int key) override;
void OnMouseLeave() override;
void OnKeyDown(EInputKey key) override;
void OnKeyUp(EInputKey key) override;

private:
std::string text;
bool buttonDown = false;
bool hot = false;
};
11 changes: 11 additions & 0 deletions include/zwidget/widgets/textlabel/textlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

#include "../../core/widget.h"

enum TextLabelAlignment
{
Left,
Center,
Right
};

class TextLabel : public Widget
{
public:
Expand All @@ -11,11 +18,15 @@ class TextLabel : public Widget
void SetText(const std::string& value);
const std::string& GetText() const;

void SetTextAlignment(TextLabelAlignment alignment);
TextLabelAlignment GetTextAlignment() const;

double GetPreferredHeight() const;

protected:
void OnPaint(Canvas* canvas) override;

private:
std::string text;
TextLabelAlignment textAlignment = TextLabelAlignment::Left;
};
9 changes: 9 additions & 0 deletions include/zwidget/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <memory>
#include <string>
#include <functional>
#include "../core/rect.h"

class Engine;
Expand Down Expand Up @@ -141,6 +142,11 @@ class DisplayWindow
static void RunLoop();
static void ExitLoop();

static void* StartTimer(int timeoutMilliseconds, std::function<void()> onTimer);
static void StopTimer(void* timerID);

static Size GetScreenSize();

virtual ~DisplayWindow() = default;

virtual void SetWindowTitle(const std::string& text) = 0;
Expand Down Expand Up @@ -170,4 +176,7 @@ class DisplayWindow
virtual void SetCaptionTextColor(uint32_t bgra8) = 0;

virtual void PresentBitmap(int width, int height, const uint32_t* pixels) = 0;

virtual std::string GetClipboardText() = 0;
virtual void SetClipboardText(const std::string& text) = 0;
};
50 changes: 15 additions & 35 deletions src/core/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>
#include <unordered_map>
#include <stdexcept>
#include <cstring>

class CanvasTexture
{
Expand Down Expand Up @@ -103,44 +104,23 @@ class CanvasFont
{
uint8_t* sline = grayscale + y * w;
uint32_t* dline = dest + y * destwidth;
for (int x = 2; x < w; x += 3)
for (int x = 0; x < w; x += 3)
{
uint32_t red = sline[x - 2];
uint32_t green = sline[x - 1];
uint32_t blue = sline[x];
uint32_t values[5] =
{
x > 0 ? sline[x - 1] : 0U,
sline[x],
x + 1 < w ? sline[x + 1] : 0U,
x + 2 < w ? sline[x + 2] : 0U,
x + 3 < w ? sline[x + 3] : 0U
};

uint32_t red = (values[0] + values[1] + values[1] + values[2] + 2) >> 2;
uint32_t green = (values[1] + values[2] + values[2] + values[3] + 2) >> 2;
uint32_t blue = (values[2] + values[3] + values[3] + values[4] + 2) >> 2;
uint32_t alpha = (red | green | blue) ? 255 : 0;

//red = (red + green) / 2;
//green = (red + green + blue) / 3;
//blue = (green + blue) / 2;

dline[x / 3] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
if (w % 3 == 1)
{
uint32_t red = sline[w - 1];
uint32_t green = 0;
uint32_t blue = 0;
uint32_t alpha = (red | green | blue) ? 255 : 0;

//red = (red + green) / 2;
//green = (red + green + blue) / 3;
//blue = (green + blue) / 2;

dline[(w - 1) / 3] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
else if (w % 3 == 2)
{
uint32_t red = sline[w - 2];
uint32_t green = sline[w - 1];
uint32_t blue = 0;
uint32_t alpha = (red | green | blue) ? 255 : 0;

//red = (red + green) / 2;
//green = (red + green + blue) / 3;
//blue = (green + blue) / 2;

dline[(w - 1) / 3] = (alpha << 24) | (red << 16) | (green << 8) | blue;
*(dline++) = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
}

Expand Down
69 changes: 68 additions & 1 deletion src/core/image.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@

#include "core/image.h"
#include "core/resourcedata.h"
#include "picopng/picopng.h"
#include "nanosvg/nanosvg.h"
#include "nanosvg/nanosvgrast.h"
#include <cstring>
#include <stdexcept>

class ImageImpl : public Image
{
public:
ImageImpl(int width, int height, ImageFormat format, const void* data) : Width(width), Height(height), Format(format)
{
Data = std::make_unique<uint32_t[]>(width * height);
memcpy(Data.get(), data, width * height * sizeof(uint32_t));
if (data)
memcpy(Data.get(), data, width * height * sizeof(uint32_t));
}

int GetWidth() const override
Expand Down Expand Up @@ -41,3 +47,64 @@ std::shared_ptr<Image> Image::Create(int width, int height, ImageFormat format,
{
return std::make_shared<ImageImpl>(width, height, format, data);
}

std::shared_ptr<Image> Image::LoadResource(const std::string& resourcename, double dpiscale)
{
size_t extensionpos = resourcename.find_last_of("./\\");
if (extensionpos == std::string::npos || resourcename[extensionpos] != '.')
throw std::runtime_error("Unsupported image format");
std::string extension = resourcename.substr(extensionpos + 1);
for (char& c : extension)
{
if (c >= 'A' && c <= 'Z')
c = c - 'A' + 'a';
}

if (extension == "png")
{
auto filedata = LoadWidgetImageData(resourcename);

std::vector<unsigned char> pixels;
unsigned long width = 0, height = 0;
int result = decodePNG(pixels, width, height, (const unsigned char*)filedata.data(), filedata.size(), true);
if (result != 0)
throw std::runtime_error("Could not decode PNG file");

return Image::Create(width, height, ImageFormat::R8G8B8A8, pixels.data());
}
else if (extension == "svg")
{
auto filedata = LoadWidgetImageData(resourcename);
filedata.push_back(0);

NSVGimage* svgimage = nsvgParse((char*)filedata.data(), "px", (float)(96.0 * dpiscale));
if (!svgimage)
throw std::runtime_error("Could not parse SVG file");

try
{
int width = (int)(svgimage->width * dpiscale);
int height = (int)(svgimage->height * dpiscale);
std::shared_ptr<Image> image = Image::Create(width, height, ImageFormat::R8G8B8A8, nullptr);

NSVGrasterizer* rast = nsvgCreateRasterizer();
if (!rast)
throw std::runtime_error("Could not create SVG rasterizer");

nsvgRasterize(rast, svgimage, 0.0f, 0.0f, (float)dpiscale, (unsigned char*)image->GetData(), width, height, width * 4);

nsvgDeleteRasterizer(rast);
nsvgDelete(svgimage);
return image;
}
catch (...)
{
nsvgDelete(svgimage);
throw;
}
}
else
{
throw std::runtime_error("Unsupported image format");
}
}
6 changes: 6 additions & 0 deletions src/core/nanosvg/nanosvg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#define NANOSVG_IMPLEMENTATION
#include "nanosvg.h"

#define NANOSVGRAST_IMPLEMENTATION
#include "nanosvgrast.h"
Loading

0 comments on commit 30e1c84

Please sign in to comment.