Skip to content

Commit

Permalink
Update to latest zwidget
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Jan 20, 2024
1 parent 9a0247f commit 5d56920
Show file tree
Hide file tree
Showing 22 changed files with 586 additions and 165 deletions.
2 changes: 2 additions & 0 deletions SurrealEngine/DebuggerApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "UI/WidgetResourceData.h"
#include "VM/Frame.h"
#include "UTF16.h"
#include <zwidget/core/theme.h>
#include <iostream>

#ifndef WIN32
Expand All @@ -28,6 +29,7 @@
int DebuggerApp::Main(std::vector<std::string> args)
{
InitWidgetResources();
WidgetTheme::SetTheme(std::make_unique<DarkWidgetTheme>());

WriteOutput(ColorEscape(96) + "Welcome to the Surreal Engine debugger!" + ResetEscape() + NewLine());
WriteOutput(NewLine());
Expand Down
2 changes: 2 additions & 0 deletions SurrealEngine/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#include "Engine.h"
#include "UI/Editor/EditorMainWindow.h"
#include "UI/WidgetResourceData.h"
#include <zwidget/core/theme.h>

int EditorApp::main(std::vector<std::string> args)
{
InitWidgetResources();
WidgetTheme::SetTheme(std::make_unique<LightWidgetTheme>());

CommandLine cmd(args);
commandline = &cmd;
Expand Down
2 changes: 2 additions & 0 deletions SurrealEngine/GameApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
#include "VM/NativeFuncExtractor.h"
#include "UI/WidgetResourceData.h"
#include "File.h"
#include <zwidget/core/theme.h>

int GameApp::main(std::vector<std::string> args)
{
InitWidgetResources();
WidgetTheme::SetTheme(std::make_unique<DarkWidgetTheme>());

CommandLine cmd(args);
commandline = &cmd;
Expand Down
4 changes: 0 additions & 4 deletions SurrealEngine/UI/Launcher/LauncherWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ int LauncherWindow::ExecModal(const std::vector<GameLaunchInfo>& games)

LauncherWindow::LauncherWindow(const std::vector<GameLaunchInfo>& games) : Widget(nullptr, WidgetType::Window)
{
SetWindowBackground(Colorf::fromRgba8(51, 51, 51));
SetWindowBorderColor(Colorf::fromRgba8(51, 51, 51));
SetWindowCaptionColor(Colorf::fromRgba8(33, 33, 33));
SetWindowCaptionTextColor(Colorf::fromRgba8(226, 223, 219));
SetWindowTitle("Surreal Engine");

Banner = new LauncherBanner(this);
Expand Down
2 changes: 2 additions & 0 deletions Thirdparty/ZWidget/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(ZWIDGET_SOURCES
src/core/span_layout.cpp
src/core/timer.cpp
src/core/widget.cpp
src/core/theme.cpp
src/core/utf8reader.cpp
src/core/pathfill.cpp
src/core/truetypefont.cpp
Expand Down Expand Up @@ -44,6 +45,7 @@ set(ZWIDGET_INCLUDES
include/zwidget/core/span_layout.h
include/zwidget/core/timer.h
include/zwidget/core/widget.h
include/zwidget/core/theme.h
include/zwidget/core/utf8reader.h
include/zwidget/core/resourcedata.h
include/zwidget/widgets/lineedit/lineedit.h
Expand Down
81 changes: 81 additions & 0 deletions Thirdparty/ZWidget/include/zwidget/core/theme.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include <memory>
#include <string>
#include <variant>
#include <unordered_map>
#include "rect.h"
#include "colorf.h"

class Widget;
class Canvas;

class WidgetStyle
{
public:
WidgetStyle(WidgetStyle* parentStyle = nullptr) : ParentStyle(parentStyle) { }
virtual ~WidgetStyle() = default;
virtual void Paint(Widget* widget, Canvas* canvas, Size size) = 0;

void SetBool(const std::string& state, const std::string& propertyName, bool value);
void SetInt(const std::string& state, const std::string& propertyName, int value);
void SetDouble(const std::string& state, const std::string& propertyName, double value);
void SetString(const std::string& state, const std::string& propertyName, const std::string& value);
void SetColor(const std::string& state, const std::string& propertyName, const Colorf& value);

void SetBool(const std::string& propertyName, bool value) { SetBool(std::string(), propertyName, value); }
void SetInt(const std::string& propertyName, int value) { SetInt(std::string(), propertyName, value); }
void SetDouble(const std::string& propertyName, double value) { SetDouble(std::string(), propertyName, value); }
void SetString(const std::string& propertyName, const std::string& value) { SetString(std::string(), propertyName, value); }
void SetColor(const std::string& propertyName, const Colorf& value) { SetColor(std::string(), propertyName, value); }

private:
// Note: do not call these directly. Use widget->GetStyleXX instead since a widget may explicitly override a class style
bool GetBool(const std::string& state, const std::string& propertyName) const;
int GetInt(const std::string& state, const std::string& propertyName) const;
double GetDouble(const std::string& state, const std::string& propertyName) const;
std::string GetString(const std::string& state, const std::string& propertyName) const;
Colorf GetColor(const std::string& state, const std::string& propertyName) const;

WidgetStyle* ParentStyle = nullptr;
typedef std::variant<bool, int, double, std::string, Colorf> PropertyVariant;
std::unordered_map<std::string, std::unordered_map<std::string, PropertyVariant>> StyleProperties;

const PropertyVariant* FindProperty(const std::string& state, const std::string& propertyName) const;

friend class Widget;
};

class BasicWidgetStyle : public WidgetStyle
{
public:
using WidgetStyle::WidgetStyle;
void Paint(Widget* widget, Canvas* canvas, Size size) override;
};

class WidgetTheme
{
public:
virtual ~WidgetTheme() = default;

WidgetStyle* RegisterStyle(std::unique_ptr<WidgetStyle> widgetStyle, const std::string& widgetClass);
WidgetStyle* GetStyle(const std::string& widgetClass);

static void SetTheme(std::unique_ptr<WidgetTheme> theme);
static WidgetTheme* GetTheme();

private:
std::unordered_map<std::string, std::unique_ptr<WidgetStyle>> Styles;
};

class DarkWidgetTheme : public WidgetTheme
{
public:
DarkWidgetTheme();
};

class LightWidgetTheme : public WidgetTheme
{
public:
LightWidgetTheme();
};
41 changes: 28 additions & 13 deletions Thirdparty/ZWidget/include/zwidget/core/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <string>
#include <memory>
#include <variant>
#include <unordered_map>
#include "canvas.h"
#include "rect.h"
#include "colorf.h"
Expand Down Expand Up @@ -39,16 +41,32 @@ class Widget : DisplayWindowHost

// Widget noncontent area
void SetNoncontentSizes(double left, double top, double right, double bottom);
double GetNoncontentLeft() const { return Noncontent.Left; }
double GetNoncontentTop() const { return Noncontent.Top; }
double GetNoncontentRight() const { return Noncontent.Right; }
double GetNoncontentBottom() const { return Noncontent.Bottom; }
double GetNoncontentLeft() const { return GetStyleDouble("noncontent-left"); }
double GetNoncontentTop() const { return GetStyleDouble("noncontent-top"); }
double GetNoncontentRight() const { return GetStyleDouble("noncontent-right"); }
double GetNoncontentBottom() const { return GetStyleDouble("noncontent-bottom"); }

// Widget frame box
Rect GetFrameGeometry() const;
void SetFrameGeometry(const Rect& geometry);
void SetFrameGeometry(double x, double y, double width, double height) { SetFrameGeometry(Rect::xywh(x, y, width, height)); }

// Style properties
void SetStyleClass(const std::string& styleClass);
const std::string& GetStyleClass() const { return StyleClass; }
void SetStyleState(const std::string& state);
const std::string& GetStyleState() const { return StyleState; }
void SetStyleBool(const std::string& propertyName, bool value);
void SetStyleInt(const std::string& propertyName, int value);
void SetStyleDouble(const std::string& propertyName, double value);
void SetStyleString(const std::string& propertyName, const std::string& value);
void SetStyleColor(const std::string& propertyName, const Colorf& value);
bool GetStyleBool(const std::string& propertyName) const;
int GetStyleInt(const std::string& propertyName) const;
double GetStyleDouble(const std::string& propertyName) const;
std::string GetStyleString(const std::string& propertyName) const;
Colorf GetStyleColor(const std::string& propertyName) const;

void SetWindowBackground(const Colorf& color);
void SetWindowBorderColor(const Colorf& color);
void SetWindowCaptionColor(const Colorf& color);
Expand Down Expand Up @@ -112,7 +130,7 @@ class Widget : DisplayWindowHost
static Size GetScreenSize();

protected:
virtual void OnPaintFrame(Canvas* canvas) { }
virtual void OnPaintFrame(Canvas* canvas);
virtual void OnPaint(Canvas* canvas) { }
virtual bool OnMouseDown(const Point& pos, InputKey key) { return false; }
virtual bool OnMouseDoubleclick(const Point& pos, InputKey key) { return false; }
Expand Down Expand Up @@ -167,14 +185,6 @@ class Widget : DisplayWindowHost

Colorf WindowBackground = Colorf::fromRgba8(240, 240, 240);

struct
{
double Left = 0.0;
double Top = 0.0;
double Right = 0.0;
double Bottom = 0.0;
} Noncontent;

std::string WindowTitle;
std::unique_ptr<DisplayWindow> DispWindow;
std::unique_ptr<Canvas> DispCanvas;
Expand All @@ -185,6 +195,11 @@ class Widget : DisplayWindowHost

StandardCursor CurrentCursor = StandardCursor::arrow;

std::string StyleClass = "widget";
std::string StyleState;
typedef std::variant<bool, int, double, std::string, Colorf> PropertyVariant;
std::unordered_map<std::string, PropertyVariant> StyleProperties;

Widget(const Widget&) = delete;
Widget& operator=(const Widget&) = delete;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class LineEdit : public Widget
std::function<void()> FuncEnterPressed;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnPaint(Canvas* canvas) override;
void OnMouseMove(const Point& pos) override;
bool OnMouseDown(const Point& pos, InputKey key) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ListView : public Widget

protected:
void OnPaint(Canvas* canvas) override;
void OnPaintFrame(Canvas* canvas) override;
bool OnMouseDown(const Point& pos, InputKey key) override;
bool OnMouseDoubleclick(const Point& pos, InputKey key) override;
bool OnMouseWheel(const Point& pos, InputKey key) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class PushButton : public Widget
std::function<void()> OnClick;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnPaint(Canvas* canvas) override;
bool OnMouseDown(const Point& pos, InputKey key) override;
bool OnMouseUp(const Point& pos, InputKey key) override;
Expand All @@ -30,6 +29,4 @@ class PushButton : public Widget

private:
std::string text;
bool buttonDown = false;
bool hot = false;
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class TabWidget : public Widget
std::function<void()> OnCurrentChanged;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnGeometryChanged() override;

private:
Expand Down Expand Up @@ -67,7 +66,6 @@ class TabBar : public Widget
std::function<void()> OnCurrentChanged;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnGeometryChanged() override;

private:
Expand All @@ -92,7 +90,6 @@ class TabBarTab : public Widget
std::function<void()> OnClick;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnGeometryChanged() override;
bool OnMouseDown(const Point& pos, InputKey key) override;
bool OnMouseUp(const Point& pos, InputKey key) override;
Expand All @@ -116,7 +113,6 @@ class TabWidgetStack : public Widget
Widget* GetCurrentWidget() const { return CurrentWidget; }

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnGeometryChanged() override;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class TextEdit : public Widget
std::function<void()> FuncEnterPressed;

protected:
void OnPaintFrame(Canvas* canvas) override;
void OnPaint(Canvas* canvas) override;
void OnMouseMove(const Point& pos) override;
bool OnMouseDown(const Point& pos, InputKey key) override;
Expand Down
16 changes: 8 additions & 8 deletions Thirdparty/ZWidget/src/core/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ int BitmapCanvas::getClipMaxY() const

void BitmapCanvas::fillTile(float left, float top, float width, float height, Colorf color)
{
if (width <= 0.0f || height <= 0.0f)
if (width <= 0.0f || height <= 0.0f || color.a <= 0.0f)
return;

int dwidth = this->width;
Expand Down Expand Up @@ -558,7 +558,7 @@ void BitmapCanvas::fillTile(float left, float top, float width, float height, Co
{
uint32_t c = (calpha << 24) | (cred << 16) | (cgreen << 8) | cblue;
#ifdef USE_SSE2
__m128i crgba = _mm_set1_epi32(c);
__m128i cargb = _mm_set1_epi32(c);
#endif

for (int y = y0; y < y1; y++)
Expand All @@ -570,7 +570,7 @@ void BitmapCanvas::fillTile(float left, float top, float width, float height, Co
int ssex1 = x0 + (((x1 - x0) >> 2) << 2);
while (x < ssex1)
{
_mm_storeu_si128((__m128i*)(dline + x), crgba);
_mm_storeu_si128((__m128i*)(dline + x), cargb);
x += 4;
}
#endif
Expand All @@ -589,7 +589,7 @@ void BitmapCanvas::fillTile(float left, float top, float width, float height, Co
cblue <<= 8;
calpha <<= 8;
#ifdef USE_SSE2
__m128i crgba = _mm_set_epi16(calpha, cblue, cgreen, cred, calpha, cblue, cgreen, cred);
__m128i cargb = _mm_set_epi16(calpha, cred, cgreen, cblue, calpha, cred, cgreen, cblue);
__m128i cinvalpha = _mm_set1_epi16(invalpha);
#endif

Expand All @@ -606,7 +606,7 @@ void BitmapCanvas::fillTile(float left, float top, float width, float height, Co
dpixel = _mm_unpacklo_epi8(dpixel, _mm_setzero_si128());

// dest.rgba = color.rgba + dest.rgba * (1-color.a)
__m128i result = _mm_srli_epi16(_mm_add_epi16(_mm_add_epi16(crgba, _mm_mullo_epi16(dpixel, cinvalpha)), _mm_set1_epi16(127)), 8);
__m128i result = _mm_srli_epi16(_mm_add_epi16(_mm_add_epi16(cargb, _mm_mullo_epi16(dpixel, cinvalpha)), _mm_set1_epi16(127)), 8);
_mm_storel_epi64((__m128i*)(dline + x), _mm_packus_epi16(result, _mm_setzero_si128()));
x += 2;
}
Expand Down Expand Up @@ -634,7 +634,7 @@ void BitmapCanvas::fillTile(float left, float top, float width, float height, Co

void BitmapCanvas::drawTile(CanvasTexture* texture, float left, float top, float width, float height, float u, float v, float uvwidth, float uvheight, Colorf color)
{
if (width <= 0.0f || height <= 0.0f)
if (width <= 0.0f || height <= 0.0f || color.a <= 0.0f)
return;

int swidth = texture->Width;
Expand Down Expand Up @@ -662,7 +662,7 @@ void BitmapCanvas::drawTile(CanvasTexture* texture, float left, float top, float
uint32_t cblue = (int32_t)clamp(color.b * 256.0f, 0.0f, 256.0f);
uint32_t calpha = (int32_t)clamp(color.a * 256.0f, 0.0f, 256.0f);
#ifdef USE_SSE2
__m128i crgba = _mm_set_epi16(calpha, cblue, cgreen, cred, calpha, cblue, cgreen, cred);
__m128i cargb = _mm_set_epi16(calpha, cred, cgreen, cblue, calpha, cred, cgreen, cblue);
#endif

float uscale = uvwidth / width;
Expand Down Expand Up @@ -690,7 +690,7 @@ void BitmapCanvas::drawTile(CanvasTexture* texture, float left, float top, float
dpixel = _mm_unpacklo_epi8(dpixel, _mm_setzero_si128());

// Pixel shade
spixel = _mm_srli_epi16(_mm_add_epi16(_mm_mullo_epi16(spixel, crgba), _mm_set1_epi16(127)), 8);
spixel = _mm_srli_epi16(_mm_add_epi16(_mm_mullo_epi16(spixel, cargb), _mm_set1_epi16(127)), 8);

// Rescale from [0,255] to [0,256]
__m128i sa = _mm_shufflehi_epi16(_mm_shufflelo_epi16(spixel, _MM_SHUFFLE(3, 3, 3, 3)), _MM_SHUFFLE(3, 3, 3, 3));
Expand Down
Loading

0 comments on commit 5d56920

Please sign in to comment.