Created ICanvas for use in deffering rendering

This commit is contained in:
Rebekah 2022-04-23 14:35:49 -04:00
parent 6a74df3391
commit 9cbc0a088d
Signed by: oneechanhax
GPG Key ID: 183EB7902964DAE5
48 changed files with 271 additions and 106 deletions

View File

@ -23,10 +23,11 @@
#include <glez/color.hpp> #include <glez/color.hpp>
#include <glez/font.hpp> #include <glez/font.hpp>
#include "gui/icanvas.hpp"
#include "gui/tooltip.hpp" #include "gui/tooltip.hpp"
#include "gui/widgets/basewindow.hpp" #include "gui/widgets/basewindow.hpp"
class Canvas : public CBaseWindow { class Canvas : public CBaseWindow, public ICanvas {
public: public:
Canvas(); Canvas();
void Setup(); void Setup();
@ -38,13 +39,14 @@ public:
std::chrono::steady_clock::time_point m_iPressedFrame[CatKey::CATKEY_COUNT]; std::chrono::steady_clock::time_point m_iPressedFrame[CatKey::CATKEY_COUNT];
std::chrono::steady_clock::time_point m_iSentFrame[CatKey::CATKEY_COUNT]; std::chrono::steady_clock::time_point m_iSentFrame[CatKey::CATKEY_COUNT];
bool m_bKeysInit = false; bool m_bKeysInit = false;
int m_iMouseX; int m_iMouseX;
int m_iMouseY; int m_iMouseY;
[[deprecated]] int mouse_dx; [[deprecated]] int mouse_dx;
[[deprecated]] int mouse_dy; [[deprecated]] int mouse_dy;
bool fake_scroll = false; bool fake_scroll = false;
glez::rgba GetColor() const; glez::rgba GetColor() const override;
bool gui_rainbow = true; bool gui_rainbow = true;
glez::rgba gui_color; glez::rgba gui_color;
@ -52,9 +54,18 @@ public:
virtual void Update() override; virtual void Update() override;
virtual void OnKeyPress(CatKey key, bool repeat) override; virtual void OnKeyPress(CatKey key, bool repeat) override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
inline virtual void MoveChildren() override {}; inline virtual void MoveChildren() override {};
void Line(TranslationMatrix tm, glez::rgba color) override;
void Rect(TranslationMatrix tm, glez::rgba color, RectType rt = RectType::Filled) override;
void Rect(TranslationMatrix tm, glez::rgba color, glez::texture& tx) override;
void Circle(std::pair<int, int> center, float radius, glez::rgba color, int steps = 16) override;
std::pair<int, int> String(std::pair<int, int> src, const std::string& str, glez::rgba color, std::optional<glez::rgba> outline = glez::color::black) override;
std::pair<int, int> StringSize(const std::string& str) override;
const ICanvas& GetBackground() const override { return *this; }
const ICanvas& GetForeground() const override { return *this; }
private: private:
glez::font font; glez::font font;
}; };

View File

@ -0,0 +1,91 @@
/*
* Libpdw: Primitives Done Well!
* Copyright (C) 2022 Rebekah Rowe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
#include <glez/color.hpp>
#include <glez/font.hpp>
#include <glez/texture.hpp>
#include "gui/tooltip.hpp"
#include "gui/widgets/basewindow.hpp"
class CanvasLayer {
public:
using TranslationMatrix = std::pair<std::pair<int, int>, std::pair<int, int>>; // src, dest
public:
enum class RectType { Filled,
Outline };
virtual void Line(TranslationMatrix, glez::rgba color) = 0;
virtual void Rect(TranslationMatrix, glez::rgba color, RectType = RectType::Filled) = 0;
virtual void Rect(TranslationMatrix, glez::rgba color, glez::texture&) = 0;
virtual void Circle(std::pair<int, int> center, float radius, glez::rgba color, int steps = 16) = 0;
virtual std::pair<int, int> String(std::pair<int, int> src, const std::string& str, glez::rgba color, std::optional<glez::rgba> outline = glez::color::black) = 0;
virtual std::pair<int, int> StringSize(const std::string& str) = 0;
virtual glez::rgba GetColor() const = 0;
};
class ICanvas : public CanvasLayer {
public:
// The plan is to "traverse" the canvas tree. Super easy way to manage themes maybe. We'll see!
virtual const ICanvas& GetBackground() const = 0;
virtual const ICanvas& GetForeground() const = 0;
};
inline std::pair<int, int> operator+(std::pair<int, int> v1, std::pair<int, int> v2) {
return { v1.first + v2.first, v1.second + v2.second };
}
class CanvasOffset : public ICanvas {
ICanvas* parent;
std::pair<int, int> offset;
TranslationMatrix Offset(TranslationMatrix tm) {
return { this->offset + tm.first, tm.second };
}
public:
CanvasOffset(ICanvas* parent, std::pair<int, int> offset)
: parent(parent)
, offset(offset) { }
virtual void Line(TranslationMatrix tm, glez::rgba color) override {
this->parent->Line(this->Offset(tm), color);
}
virtual void Rect(TranslationMatrix tm, glez::rgba color, RectType rt = RectType::Filled) override {
this->parent->Rect(this->Offset(tm), color, rt);
}
virtual void Rect(TranslationMatrix tm, glez::rgba color, glez::texture& tx) override {
this->parent->Rect(this->Offset(tm), color, tx);
}
virtual void Circle(std::pair<int, int> center, float radius, glez::rgba color, int steps = 16) override {
this->parent->Circle(this->offset + center, radius, color, steps);
}
virtual std::pair<int, int> String(std::pair<int, int> src, const std::string& str, glez::rgba color, std::optional<glez::rgba> outline = glez::color::black) override {
return this->parent->String(this->offset + src, str, color, outline);
}
virtual std::pair<int, int> StringSize(const std::string& str) override {
return this->parent->StringSize(str);
}
virtual glez::rgba GetColor() const override { return this->parent->GetColor(); }
virtual const ICanvas& GetBackground() const override { return this->parent->GetBackground(); }
virtual const ICanvas& GetForeground() const override { return this->parent->GetForeground(); }
public:
decltype(offset) GetOffset() const { return offset; }
};

View File

@ -35,7 +35,7 @@ public:
Item(std::string name = "ncc_menu_item"); Item(std::string name = "ncc_menu_item");
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void HandleCustomEvent(std::string_view event) override; virtual void HandleCustomEvent(std::string_view event) override;
}; };

View File

@ -35,7 +35,7 @@ public:
virtual void SetParent(IWidget*) override; virtual void SetParent(IWidget*) override;
virtual bool IsHovered() const override; virtual bool IsHovered() const override;
virtual void Update() override; virtual void Update() override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void OnKeyPress(CatKey code, bool repeated) override; virtual void OnKeyPress(CatKey code, bool repeated) override;
virtual void OnMouseEnter(); virtual void OnMouseEnter();
virtual void OnMouseLeave(); virtual void OnMouseLeave();

View File

@ -29,7 +29,7 @@ class ItemTitle : public Item {
public: public:
ItemTitle(std::string title); ItemTitle(std::string title);
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
public: public:
const std::string title; const std::string title;

View File

@ -38,7 +38,7 @@ public:
virtual void OnMousePress() override; virtual void OnMousePress() override;
virtual void OnFocusLose() override; virtual void OnFocusLose() override;
virtual void OnKeyPress(CatKey key, bool repeat) override; virtual void OnKeyPress(CatKey key, bool repeat) override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
public: public:
ui::BaseVar& catvar; ui::BaseVar& catvar;

View File

@ -53,7 +53,7 @@ public:
virtual void OnMouseEnter() override; virtual void OnMouseEnter() override;
virtual void OnMouseLeave() override; virtual void OnMouseLeave() override;
virtual void OnMouseMove(std::pair<int, int>) override; virtual void OnMouseMove(std::pair<int, int>) override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void Update() override; virtual void Update() override;
virtual void MoveChildren() override; virtual void MoveChildren() override;
virtual void SetParent(IWidget* parent) override; virtual void SetParent(IWidget* parent) override;

View File

@ -42,7 +42,7 @@ public:
Background(); Background();
~Background(); ~Background();
virtual bool AlwaysVisible() const override; virtual bool AlwaysVisible() const override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void Update() override; virtual void Update() override;
void MakeParticle(); void MakeParticle();
void KillParticle(Particle* flake); void KillParticle(Particle* flake);

View File

@ -29,7 +29,7 @@ class Logo : public CBaseWidget {
public: public:
Logo(IWidget*); Logo(IWidget*);
virtual bool AlwaysVisible() const override; virtual bool AlwaysVisible() const override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void OnMouseMove(std::pair<int, int>) override; virtual void OnMouseMove(std::pair<int, int>) override;
glez::texture texture; glez::texture texture;
}; };

View File

@ -29,6 +29,6 @@ public:
virtual inline void SortByZIndex() override {}; virtual inline void SortByZIndex() override {};
virtual void MoveChildren() override; virtual void MoveChildren() override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
int columns; int columns;
}; };

View File

@ -36,7 +36,7 @@ public:
CBaseButton(std::string name = "unnamed", IWidget* parent = nullptr, std::string text = "", ButtonCallbackFn_t callback = nullptr) CBaseButton(std::string name = "unnamed", IWidget* parent = nullptr, std::string text = "", ButtonCallbackFn_t callback = nullptr)
: CBaseButton(parent, name, text, callback) { } : CBaseButton(parent, name, text, callback) { }
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void OnMousePress() override; virtual void OnMousePress() override;
void SetCallback(ButtonCallbackFn_t callback); void SetCallback(ButtonCallbackFn_t callback);

View File

@ -40,7 +40,7 @@ public:
virtual IWidget* ChildByPoint(int x, int y); virtual IWidget* ChildByPoint(int x, int y);
virtual bool ConsumesKey(CatKey key); virtual bool ConsumesKey(CatKey key);
virtual void Draw(int x, int y); virtual void Draw(ICanvas*);
virtual void DrawBounds(int x, int y); virtual void DrawBounds(int x, int y);
virtual void Hide(); virtual void Hide();
virtual void OnFocusLose(); virtual void OnFocusLose();

View File

@ -34,7 +34,7 @@ public:
CBaseWidget(std::string name = "unnamed", IWidget* parent = nullptr); CBaseWidget(std::string name = "unnamed", IWidget* parent = nullptr);
virtual void Update(); virtual void Update();
inline virtual void Draw(int x, int y) {}; virtual void Draw(ICanvas*) { }
virtual void DrawBounds(int x, int y); virtual void DrawBounds(int x, int y);
inline virtual KeyValues* Props() const { inline virtual KeyValues* Props() const {

View File

@ -29,6 +29,6 @@ public:
virtual void OnFocusGain() override; virtual void OnFocusGain() override;
virtual void OnFocusLose() override; virtual void OnFocusLose() override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void MoveChildren() override; virtual void MoveChildren() override;
}; };

View File

@ -37,7 +37,7 @@ public:
void SetCallback(CheckboxCallbackFn_t callback); void SetCallback(CheckboxCallbackFn_t callback);
virtual void OnMousePress(); virtual void OnMousePress();
virtual void Draw(int x, int y); virtual void Draw(ICanvas*);
CheckboxCallbackFn_t m_pCallback; CheckboxCallbackFn_t m_pCallback;
bool checked; bool checked;

View File

@ -43,7 +43,7 @@ public:
void ShowList(); void ShowList();
void SetCallback(DropdownCallbackFn_t callback); void SetCallback(DropdownCallbackFn_t callback);
virtual void Draw(int x, int y); virtual void Draw(ICanvas*);
virtual void OnFocusLose(); virtual void OnFocusLose();
DropdownCallbackFn_t m_pDropdownCallback; DropdownCallbackFn_t m_pDropdownCallback;

View File

@ -29,7 +29,7 @@ public:
CDropdownList(std::string name = "unnamed", CDropdown* menu = nullptr, int offset = 0); CDropdownList(std::string name = "unnamed", CDropdown* menu = nullptr, int offset = 0);
~CDropdownList(); ~CDropdownList();
virtual void Draw(int x, int y); virtual void Draw(ICanvas*);
virtual void MoveChildren(); virtual void MoveChildren();
inline virtual void SortByZIndex() override {}; inline virtual void SortByZIndex() override {};
inline virtual bool DoesStealFocus() { return false; } inline virtual bool DoesStealFocus() { return false; }

View File

@ -34,12 +34,13 @@ enum PositionMode {
class KeyValues; class KeyValues;
class Canvas; class Canvas;
class ICanvas;
class IWidget { class IWidget {
public: public:
virtual ~IWidget(); virtual ~IWidget();
virtual void Update() = 0; virtual void Update() = 0;
virtual void Draw(int x, int y) = 0; virtual void Draw(ICanvas*) = 0;
virtual void DrawBounds(int x, int y) = 0; virtual void DrawBounds(int x, int y) = 0;
virtual KeyValues* Props() const = 0; virtual KeyValues* Props() const = 0;

View File

@ -38,7 +38,7 @@ public:
KeyInputCallbackFn_t m_pCallback; KeyInputCallbackFn_t m_pCallback;
virtual void OnKeyPress(CatKey key, bool repeat) override; virtual void OnKeyPress(CatKey key, bool repeat) override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void OnMousePress() override; virtual void OnMousePress() override;
virtual void OnFocusLose() override; virtual void OnFocusLose() override;
virtual bool ConsumesKey(CatKey key) const override; virtual bool ConsumesKey(CatKey key) const override;

View File

@ -38,7 +38,7 @@ public:
void SetCallback(SliderCallbackFn_t callback); void SetCallback(SliderCallbackFn_t callback);
virtual void Update(); virtual void Update();
virtual void Draw(int x, int y); virtual void Draw(ICanvas*);
SliderCallbackFn_t m_pCallback; SliderCallbackFn_t m_pCallback;

View File

@ -32,7 +32,7 @@ public:
: CTextInput(parent, name) { } : CTextInput(parent, name) { }
virtual void OnKeyPress(CatKey key, bool repeat); virtual void OnKeyPress(CatKey key, bool repeat);
virtual void Draw(int x, int y); virtual void Draw(ICanvas*);
virtual bool ConsumesKey(CatKey key); virtual bool ConsumesKey(CatKey key);
void PutChar(char ch); void PutChar(char ch);

View File

@ -34,7 +34,7 @@ public:
void SetAutoSize(bool autosize); void SetAutoSize(bool autosize);
void SetCentered(bool centered); void SetCentered(bool centered);
virtual void Draw(int x, int y); virtual void Draw(ICanvas*);
private: private:
bool autosize; bool autosize;

View File

@ -28,7 +28,7 @@ class CTitleBar : public CBaseWidget {
public: public:
CTitleBar(IWidget* parent, std::string title); CTitleBar(IWidget* parent, std::string title);
virtual void Draw(int x, int y); virtual void Draw(ICanvas*);
virtual void OnMouseMove(std::pair<int, int>); virtual void OnMouseMove(std::pair<int, int>);
virtual void Update(); virtual void Update();

View File

@ -170,7 +170,7 @@ void Canvas::Update() {
tooltip->Hide(); tooltip->Hide();
CBaseWindow::Update(); CBaseWindow::Update();
this->Draw(0, 0); this->Draw((ICanvas*)this);
// Draw Mouse // Draw Mouse
glez::draw::rect(m_iMouseX - 5, m_iMouseY - 5, 10, 10, Transparent(glez::color::black)); glez::draw::rect(m_iMouseX - 5, m_iMouseY - 5, 10, 10, Transparent(glez::color::black));
@ -180,11 +180,11 @@ void Canvas::Update() {
this->DrawBounds(0, 0); this->DrawBounds(0, 0);
} }
void Canvas::Draw(int x, int y) { void Canvas::Draw(ICanvas* canvas) {
if (tooltip->IsVisible()) { if (tooltip->IsVisible()) {
tooltip->SetOffset(this->m_iMouseX + 24, this->m_iMouseY + 8); tooltip->SetOffset(this->m_iMouseX + 24, this->m_iMouseY + 8);
} }
CBaseContainer::Draw(x, y); CBaseContainer::Draw(canvas);
} }
static auto start_time = std::chrono::steady_clock::now(); static auto start_time = std::chrono::steady_clock::now();

52
src/gui/icanvas.cpp Normal file
View File

@ -0,0 +1,52 @@
/*
* Libpdw: Primitives Done Well!
* Copyright (C) 2022 Rebekah Rowe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <glez/draw.hpp>
#include "gui/icanvas.hpp"
#include "gui/canvas.hpp"
void Canvas::Line(TranslationMatrix tm, glez::rgba color) {
glez::draw::line(tm.first.first, tm.first.second, tm.second.first, tm.second.second, color, 1);
}
void Canvas::Rect(TranslationMatrix tm, glez::rgba color, RectType rt) {
if (rt == RectType::Filled)
glez::draw::rect(tm.first.first, tm.first.second, tm.second.first, tm.second.second, color);
else if (rt == RectType::Outline)
glez::draw::rect_outline(tm.first.first, tm.first.second, tm.second.first, tm.second.second, color, 1);
}
void Canvas::Rect(TranslationMatrix tm, glez::rgba color, glez::texture& tx) {
glez::draw::rect_textured(tm.first.first, tm.first.second, tm.second.first, tm.second.second, color, tx, 0, 0, tx.width, tx.height, 0.0f);
}
void Canvas::Circle(std::pair<int, int> center, float radius, glez::rgba color, int steps) {
glez::draw::circle(center.first, center.second, radius, color, 1, steps);
}
std::pair<int, int> Canvas::String(std::pair<int, int> src, const std::string& str, glez::rgba color, std::optional<glez::rgba> outline) {
std::pair<float, float> ret;
if (outline)
glez::draw::outlined_string(src.first, src.second, str, this->GetFont(), color, *outline, &ret.first, &ret.second);
else
glez::draw::string(src.first, src.second, str, this->GetFont(), color, &ret.first, &ret.second);
return ret;
}
std::pair<int, int> Canvas::StringSize(const std::string& str) {
std::pair<float, float> ret;
this->GetFont().stringSize(str, &ret.first, &ret.second);
return ret;
}

View File

@ -35,12 +35,13 @@ Item::Item(std::string name)
SetMaxSize(psize_x, psize_y); SetMaxSize(psize_x, psize_y);
} }
void Item::Draw(int x, int y) { void Item::Draw(ICanvas* canvas) {
const auto& size = GetSize(); const auto& size = GetSize();
// draw::DrawRect(x, y, size.first, size.second, colors::red); // draw::DrawRect(x, y, size.first, size.second, colors::red);
glez::draw::rect(x, y, size.first, size.second, glez::rgba(0, 0, 0, 55)); auto zero = std::pair<int, int> { 0, 0 };
canvas->Rect({ zero, size }, glez::rgba(0, 0, 0, 55));
if (IsHovered()) { if (IsHovered()) {
glez::draw::rect(x, y, size.first, size.second, Transparent(this->GetCanvas()->GetColor(), 0.32f)); canvas->Rect({ zero, size }, Transparent(this->GetCanvas()->GetColor(), 0.32f));
} }
} }

View File

@ -59,15 +59,15 @@ void ItemSublist::Update() {
} }
} }
void ItemSublist::Draw(int x, int y) { void ItemSublist::Draw(ICanvas* canvas) {
Item::Draw(x, y); Item::Draw(canvas);
List* parent = dynamic_cast<List*>(GetParent()); List* parent = dynamic_cast<List*>(GetParent());
if (!parent) if (!parent)
throw std::runtime_error("Sublist parent can't be casted to List!"); throw std::runtime_error("Sublist parent can't be casted to List!");
const auto& size = GetSize(); const auto& size = GetSize();
if (parent->open_sublist == list) if (parent->open_sublist == list)
glez::draw::rect(x, y, size.first, size.second, Transparent(this->GetCanvas()->GetColor(), 0.5f)); canvas->Rect({ { 0, 0 }, size }, Transparent(this->GetCanvas()->GetColor(), 0.5f));
glez::draw::string(x + 2, y, (IsHovered() ? "[-] " : "[+] ") + title, this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); canvas->String({ 2, 0 }, (IsHovered() ? "[-] " : "[+] ") + title, glez::color::white);
} }
void ItemSublist::OnKeyPress(CatKey code, bool repeated) { void ItemSublist::OnKeyPress(CatKey code, bool repeated) {

View File

@ -33,14 +33,14 @@ ItemTitle::ItemTitle(std::string title)
this->brackets = false; this->brackets = false;
} }
void ItemTitle::Draw(int x, int y) { void ItemTitle::Draw(ICanvas* canvas) {
Item::Draw(x, y); Item::Draw(canvas);
// nailed it // nailed it
bool brackets3 = this->brackets; bool brackets3 = this->brackets;
std::string str = (brackets3 ? ">>> " : ">> ") + title + (brackets3 ? " <<<" : " <<"); std::string str = (brackets3 ? ">>> " : ">> ") + title + (brackets3 ? " <<<" : " <<");
std::pair<float, float> size; std::pair<float, float> size;
this->GetCanvas()->GetFont().stringSize(str, &size.first, &size.second); this->GetCanvas()->GetFont().stringSize(str, &size.first, &size.second);
glez::draw::string(x + ((Item::size_x - size.first) / 2), y, str, this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); canvas->String({ ((Item::size_x - size.first) / 2), 0 }, str, glez::color::white);
} }
} }

View File

@ -114,8 +114,8 @@ void ItemVariable::OnKeyPress(CatKey key, bool repeat) {
} }
} }
void ItemVariable::Draw(int x, int y) { void ItemVariable::Draw(ICanvas* canvas) {
Item::Draw(x, y); Item::Draw(canvas);
std::string val = "[UNDEFINED]"; std::string val = "[UNDEFINED]";
switch (catvar.type) { switch (catvar.type) {
case ui::BaseVar::Type::kBool: case ui::BaseVar::Type::kBool:
@ -137,7 +137,7 @@ void ItemVariable::Draw(int x, int y) {
} }
} break; } break;
} }
glez::draw::string(x + 2, y, (std::string(catvar.gui_name) + ": " + val), this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); canvas->String({ 2, 0 }, (std::string(catvar.gui_name) + ": " + val), glez::color::white);
} }
} }

View File

@ -230,11 +230,11 @@ void List::OnMouseLeave() {
} }
} }
void List::Draw(int x, int y) { void List::Draw(ICanvas* canvas) {
// const auto& size = GetSize(); // const auto& size = GetSize();
glez::draw::rect_outline(x, y, 2 + Item::size_x, this->items * Item::size_y + 2, this->GetCanvas()->GetColor(), 1); canvas->Rect({ { 0, 0 }, { 2 + Item::size_x, this->items * Item::size_y + 2 } }, this->GetCanvas()->GetColor(), CanvasLayer::RectType::Outline);
for (int i = 1; i < this->items; i++) { for (int i = 1; i < this->items; i++) {
glez::draw::line(x + 1, y + Item::size_y * i, Item::size_x, 0, this->GetCanvas()->GetColor(), 1); canvas->Line({ { 1, Item::size_y * i }, { Item::size_x, 0 } }, this->GetCanvas()->GetColor());
} }
// CBaseContainer::Draw(x, y); // CBaseContainer::Draw(x, y);
for (int i = 0; i < ChildCount(); i++) { for (int i = 0; i < ChildCount(); i++) {
@ -245,11 +245,13 @@ void List::Draw(int x, int y) {
throw std::runtime_error("Invalid cast in NCC-List:Draw!"); throw std::runtime_error("Invalid cast in NCC-List:Draw!");
} }
const auto& offset = item->GetOffset(); const auto& offset = item->GetOffset();
item->Draw(x + offset.first, y + offset.second); CanvasOffset offsetted_canvas(canvas, offset);
item->Draw(&offsetted_canvas);
} }
if (dynamic_cast<List*>(open_sublist)) { if (dynamic_cast<List*>(open_sublist)) {
const auto& offset = open_sublist->GetOffset(); const auto& offset = open_sublist->GetOffset();
open_sublist->Draw(x + offset.first, y + offset.second); CanvasOffset offsetted_canvas(canvas, offset);
open_sublist->Draw(&offsetted_canvas);
} }
} }

View File

@ -89,7 +89,7 @@ Background::~Background() {
} }
} }
void Background::Draw(int x, int y) { void Background::Draw(ICanvas* canvas) {
if (!particles) if (!particles)
return; return;
Particle* current = list; Particle* current = list;

View File

@ -39,9 +39,9 @@ bool Logo::AlwaysVisible() const {
return (int)logo == 2; return (int)logo == 2;
} }
void Logo::Draw(int x, int y) { void Logo::Draw(ICanvas* canvas) {
if (logo) if (logo)
glez::draw::rect_textured(x, y, embeded_logo_png_rgba.width, embeded_logo_png_rgba.height, this->GetCanvas()->GetColor(), this->texture, 0, 0, embeded_logo_png_rgba.width, embeded_logo_png_rgba.height, 0.0f); canvas->Rect({ { 0, 0 }, { embeded_logo_png_rgba.width, embeded_logo_png_rgba.height } }, this->GetCanvas()->GetColor(), this->texture);
} }
void Logo::OnMouseMove(std::pair<int, int> delta) { void Logo::OnMouseMove(std::pair<int, int> delta) {

View File

@ -48,9 +48,9 @@ void CMenuContainer::MoveChildren() {
this->columns = columns; this->columns = columns;
} }
void CMenuContainer::Draw(int x, int y) { void CMenuContainer::Draw(ICanvas* canvas) {
CBaseContainer::Draw(x, y); CBaseContainer::Draw(canvas);
for (int i = 0; i < this->columns; i++) { for (int i = 0; i < this->columns; i++) {
glez::draw::line(x + (350 + 3) * (i + 1), y, 0, GetMaxSize().second, this->GetCanvas()->GetColor(), 1); canvas->Line({ { (350 + 3) * (i + 1), 0 }, { 0, GetMaxSize().second } }, this->GetCanvas()->GetColor());
} }
} }

View File

@ -38,19 +38,21 @@ bool CMenuListEntry::IsSelected() {
return (dynamic_cast<CMenuList*>(GetParent())->m_pSelected == this); return (dynamic_cast<CMenuList*>(GetParent())->m_pSelected == this);
} }
void CMenuListEntry::Draw(int x, int y) { void CMenuListEntry::Draw(ICanvas* canvas) {
std::pair<float, float> texts; std::pair<float, float> texts;
this->GetCanvas()->GetFont().stringSize(GetText(), &texts.first, &texts.second); this->GetCanvas()->GetFont().stringSize(GetText(), &texts.first, &texts.second);
auto size = GetSize(); auto size = GetSize();
auto gui_color = canvas->GetColor();
const auto zero = std::pair<int, int> { 0, 0 };
if (IsSelected()) { if (IsSelected()) {
glez::draw::line(x, y, size.first, 0, this->GetCanvas()->GetColor(), 1); canvas->Line({ zero, { size.first, 0 } }, gui_color);
glez::draw::line(x, y + size.second, size.first, 0, this->GetCanvas()->GetColor(), 1); canvas->Line({ { 0, size.second }, { size.first, 0 } }, gui_color);
glez::draw::line(x, y, 0, size.second, this->GetCanvas()->GetColor(), 1); canvas->Line({ zero, { 0, size.second } }, gui_color);
} else { } else {
glez::draw::rect_outline(x, y, size.first, size.second, this->GetCanvas()->GetColor(), 1); canvas->Rect({ zero, size }, gui_color, CanvasLayer::RectType::Outline);
} }
if (IsHovered()) { if (IsHovered()) {
glez::draw::rect(x, y, size.first, size.second, Transparent(this->GetCanvas()->GetColor(), 0.25)); canvas->Rect({ zero, size }, Transparent(gui_color, 0.25));
} }
glez::draw::string(x + (size.first - texts.first) / 2, y + (size.second - texts.second) / 2, GetText().c_str(), this->GetCanvas()->GetFont(), IsSelected() ? glez::color::white : this->GetCanvas()->GetColor(), nullptr, nullptr); canvas->String({ (size.first - texts.first) / 2, (size.second - texts.second) / 2 }, GetText(), IsSelected() ? glez::color::white : gui_color);
} }

View File

@ -30,7 +30,7 @@ public:
bool IsSelected(); bool IsSelected();
virtual void SetMaxSize(int x, int y) override; virtual void SetMaxSize(int x, int y) override;
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
public: public:
std::string entry; std::string entry;

View File

@ -50,10 +50,11 @@ void Tooltip::HandleCustomEvent(std::string_view event) {
}*/ }*/
} }
void Tooltip::Draw(int x, int y) { void Tooltip::Draw(ICanvas* canvas) {
const auto& size = GetSize(); const auto& size = GetSize();
int originx = x; auto offset = this->GetOffset();
int originy = y; int originx = offset.first;
int originy = offset.second;
auto root_size = this->GetCanvas()->GetSize(); auto root_size = this->GetCanvas()->GetSize();
if (originx + size.first > root_size.first) if (originx + size.first > root_size.first)
originx -= size.first; originx -= size.first;
@ -61,9 +62,9 @@ void Tooltip::Draw(int x, int y) {
originy -= size.second; originy -= size.second;
static auto bgcolor = glez::rgba(0, 0, 0, 77); // colors::Create(70, 86, 47, 28); static auto bgcolor = glez::rgba(0, 0, 0, 77); // colors::Create(70, 86, 47, 28);
static auto fgcolor = glez::rgba(200, 200, 190, 255); static auto fgcolor = glez::rgba(200, 200, 190, 255);
glez::draw::rect(x, y, size.first, size.second, bgcolor); canvas->Rect({ { 0, 0 }, size }, bgcolor);
glez::draw::rect_outline(x, y, size.first, size.second, this->GetCanvas()->GetColor(), 1); canvas->Rect({ { 0, 0 }, size }, this->GetCanvas()->GetColor(), CanvasLayer::RectType::Outline);
glez::draw::string(x + this->padding.first, y + this->padding.second, GetText(), this->GetCanvas()->GetFont(), fgcolor, nullptr, nullptr); canvas->String(this->padding, GetText(), fgcolor);
} }
} }

View File

@ -28,7 +28,7 @@ class Tooltip : public CTextLabel {
public: public:
Tooltip(); Tooltip();
virtual void Draw(int x, int y) override; virtual void Draw(ICanvas*) override;
virtual void HandleCustomEvent(std::string_view event) override; virtual void HandleCustomEvent(std::string_view event) override;
inline virtual PositionMode GetPositionMode() const override { return PositionMode::FLOATING; } inline virtual PositionMode GetPositionMode() const override { return PositionMode::FLOATING; }
std::pair<int, int> padding; std::pair<int, int> padding;

View File

@ -35,15 +35,17 @@ void CBaseButton::SetCallback(ButtonCallbackFn_t callback) {
m_pCallback = callback; m_pCallback = callback;
} }
void CBaseButton::Draw(int x, int y) { void CBaseButton::Draw(ICanvas* canvas) {
glez::rgba textcolor = this->GetCanvas()->GetColor(); auto gui_color = canvas->GetColor();
auto textcolor = gui_color;
auto size = GetSize(); auto size = GetSize();
const auto zero = std::pair<int, int> { 0, 0 };
if (IsPressed()) { if (IsPressed()) {
glez::draw::rect(x, y, size.first, size.second, this->GetCanvas()->GetColor()); canvas->Rect({ zero, size }, gui_color);
textcolor = glez::color::white; textcolor = glez::color::white;
} }
glez::draw::rect_outline(x, y, size.first, size.second, this->GetCanvas()->GetColor(), 1); canvas->Rect({ zero, size }, gui_color, CanvasLayer::RectType::Outline);
glez::draw::string(x + this->padding.first, y + this->padding.second, GetText().c_str(), this->GetCanvas()->GetFont(), textcolor, nullptr, nullptr); canvas->String(this->padding, GetText(), textcolor);
} }
void CBaseButton::OnMousePress() { void CBaseButton::OnMousePress() {

View File

@ -77,16 +77,17 @@ int CBaseContainer::ChildCount() {
return m_children.size(); return m_children.size();
} }
void CBaseContainer::Draw(int x, int y) { void CBaseContainer::Draw(ICanvas* canvas) {
for (auto child : m_children) { for (auto child : m_children) {
if (child->IsVisible()) { if (child->IsVisible()) {
auto off = child->GetOffset(); auto off = child->GetOffset();
if (AlwaysVisible() || this->GetCanvas()->IsVisible() || child->AlwaysVisible()) if (AlwaysVisible() || this->GetCanvas()->IsVisible() || child->AlwaysVisible()) {
child->Draw(x + off.first, y + off.second); CanvasOffset offsetted_canvas(canvas, off);
child->Draw(&offsetted_canvas);
}
} }
} }
} }
void CBaseContainer::DrawBounds(int x, int y) { void CBaseContainer::DrawBounds(int x, int y) {
for (auto child : m_children) { for (auto child : m_children) {
if (child->IsVisible()) { if (child->IsVisible()) {

View File

@ -55,10 +55,10 @@ void CBaseWindow::OnFocusLose() {
CBaseContainer::OnFocusLose(); CBaseContainer::OnFocusLose();
} }
void CBaseWindow::Draw(int x, int y) { void CBaseWindow::Draw(ICanvas* canvas) {
auto abs = AbsolutePosition(); auto abs = AbsolutePosition();
auto size = GetSize(); auto size = GetSize();
glez::draw::rect(abs.first, abs.second, size.first, size.second, Transparent(glez::color::black, 0.9)); glez::draw::rect(abs.first, abs.second, size.first, size.second, Transparent(glez::color::black, 0.9));
glez::draw::rect_outline(abs.first, abs.second, size.first, size.second, this->GetCanvas()->GetColor(), 1); glez::draw::rect_outline(abs.first, abs.second, size.first, size.second, this->GetCanvas()->GetColor(), 1);
CBaseContainer::Draw(x, y); CBaseContainer::Draw(canvas);
} }

View File

@ -34,11 +34,11 @@ void CCheckbox::SetWidth(int _width) {
SetSize(_width, _width); SetSize(_width, _width);
} }
void CCheckbox::Draw(int x, int y) { void CCheckbox::Draw(ICanvas* canvas) {
auto size = GetSize(); auto size = GetSize();
glez::draw::rect_outline(x, y, size.first, size.second, this->GetCanvas()->GetColor(), 1); canvas->Rect({ { 0, 0 }, size }, this->GetCanvas()->GetColor(), CanvasLayer::RectType::Outline);
if (Value()) { if (Value()) {
glez::draw::rect(x + 3, y + 3, size.first - 6, size.second - 6, this->GetCanvas()->GetColor()); canvas->Rect({ { 3, 3 }, { size.first - 6, size.second - 6 } }, this->GetCanvas()->GetColor());
} }
} }

View File

@ -55,16 +55,16 @@ std::string CDropdown::ValueName(int idx) {
return m_values.at(idx); return m_values.at(idx);
} }
void CDropdown::Draw(int x, int y) { void CDropdown::Draw(ICanvas* canvas) {
auto size = GetSize(); auto size = GetSize();
std::pair<float, float> ssize; std::pair<float, float> ssize;
this->GetCanvas()->GetFont().stringSize(ValueName(Value() - this->offset), &ssize.first, &ssize.second); this->GetCanvas()->GetFont().stringSize(ValueName(Value() - this->offset), &ssize.first, &ssize.second);
glez::draw::rect(x, y, size.first, size.second, Transparent(glez::color::black)); canvas->Rect({ { 0, 0 }, { size } }, Transparent(glez::color::black));
glez::draw::rect_outline(x, y, size.first, size.second, this->GetCanvas()->GetColor(), 1); canvas->Rect({ { 0, 0 }, { size } }, this->GetCanvas()->GetColor(), CanvasLayer::RectType::Outline);
glez::draw::string(x + (size.first - ssize.first) / 2, y + (size.second - ssize.second) / 2, ValueName(Value() - this->offset), this->GetCanvas()->GetFont(), this->GetCanvas()->GetColor(), nullptr, nullptr); canvas->String({ (size.first - ssize.first) / 2, (size.second - ssize.second) / 2 }, ValueName(Value() - this->offset), this->GetCanvas()->GetColor());
std::pair<float, float> asize; std::pair<float, float> asize;
this->GetCanvas()->GetFont().stringSize(">", &asize.first, &asize.second); this->GetCanvas()->GetFont().stringSize(">", &asize.first, &asize.second);
glez::draw::string(x + size.first - asize.first - 2, y + (size.second - asize.second) / 2, ">", this->GetCanvas()->GetFont(), this->GetCanvas()->GetColor(), nullptr, nullptr); canvas->String({ size.first - asize.first - 2, (size.second - asize.second) / 2 }, ">", this->GetCanvas()->GetColor());
} }
void CDropdown::OnFocusLose() { void CDropdown::OnFocusLose() {

View File

@ -52,11 +52,12 @@ void CDropdownList::SetValue(int value) {
Hide(); Hide();
} }
void CDropdownList::Draw(int x, int y) { void CDropdownList::Draw(ICanvas* canvas) {
auto size = GetSize(); auto size = GetSize();
glez::draw::rect(x, y, size.first, size.second, Transparent(glez::color::black, 0.85));
glez::draw::rect_outline(x, y, size.first, size.second, this->GetCanvas()->GetColor(), 1); canvas->Rect({ { 0, 0 }, size }, Transparent(glez::color::black, 0.85));
CBaseContainer::Draw(x, y); canvas->Rect({ { 0, 0 }, size }, this->GetCanvas()->GetColor(), CanvasLayer::RectType::Outline);
CBaseContainer::Draw(canvas);
} }
void CDropdownList::MoveChildren() { void CDropdownList::MoveChildren() {

View File

@ -38,7 +38,7 @@ void CKeyInput::SetValue(int value) {
this->value = value; this->value = value;
} }
void CKeyInput::Draw(int x, int y) { void CKeyInput::Draw(ICanvas* canvas) {
std::string key = ""; std::string key = "";
glez::rgba color = glez::color::white; glez::rgba color = glez::color::white;
if (this->capturing) { if (this->capturing) {
@ -56,7 +56,7 @@ void CKeyInput::Draw(int x, int y) {
auto size = GetSize(); auto size = GetSize();
std::pair<float, float> ss; std::pair<float, float> ss;
this->GetCanvas()->GetFont().stringSize(key, &ss.first, &ss.second); this->GetCanvas()->GetFont().stringSize(key, &ss.first, &ss.second);
glez::draw::string(x + (size.first - ss.first) / 2, y + (size.second - ss.second) / 2, key, this->GetCanvas()->GetFont(), color, nullptr, nullptr); canvas->String({ (size.first - ss.first) / 2, (size.second - ss.second) / 2 }, key, color);
} }
void CKeyInput::SetCallback(KeyInputCallbackFn_t callback) { void CKeyInput::SetCallback(KeyInputCallbackFn_t callback) {

View File

@ -90,14 +90,14 @@ void CSlider::Update() {
m_bDragInit = false; m_bDragInit = false;
} }
void CSlider::Draw(int x, int y) { void CSlider::Draw(ICanvas* canvas) {
auto size = GetSize(); auto size = GetSize();
glez::draw::rect(x, y, size.first, size.second, glez::color::black); canvas->Rect({ { 0, 0 }, size }, glez::color::black);
glez::draw::rect(x, y, m_nSliderPos, size.second, this->GetCanvas()->GetColor()); canvas->Rect({ { 0, 0 }, { m_nSliderPos, size.second } }, this->GetCanvas()->GetColor());
char s[256]; char s[256];
snprintf(s, sizeof(s), "%.2f", Value()); snprintf(s, sizeof(s), "%.2f", Value());
std::string str(s); std::string str(s);
std::pair<float, float> sl; std::pair<float, float> sl;
this->GetCanvas()->GetFont().stringSize(str, &sl.first, &sl.second); this->GetCanvas()->GetFont().stringSize(str, &sl.first, &sl.second);
glez::draw::string(x + (size.first - sl.first) / 2, y + (size.second - sl.second) / 2, str, this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); canvas->String({ (size.first - sl.first) / 2, (size.second - sl.second) / 2 }, str, glez::color::white);
} }

View File

@ -51,15 +51,15 @@ void CTextInput::SetValue(std::string value) {
this->value = value; this->value = value;
} }
void CTextInput::Draw(int x, int y) { void CTextInput::Draw(ICanvas* canvas) {
std::pair<float, float> wsize; std::pair<float, float> wsize;
this->GetCanvas()->GetFont().stringSize("W", &wsize.first, &wsize.second); this->GetCanvas()->GetFont().stringSize("W", &wsize.first, &wsize.second);
auto size = GetSize(); auto size = GetSize();
auto color = glez::rgba(0, 0, 0, 80); auto color = glez::rgba(0, 0, 0, 80);
if (IsFocused()) if (IsFocused())
color = Transparent(this->GetCanvas()->GetColor(), 0.25); color = Transparent(this->GetCanvas()->GetColor(), 0.25);
glez::draw::rect(x, y, size.first, size.second, color); canvas->Rect({ { 0, 0 }, { size } }, color);
glez::draw::rect_outline(x, y, size.first, size.second, this->GetCanvas()->GetColor(), 1); canvas->Rect({ { 0, 0 }, { size } }, this->GetCanvas()->GetColor(), CanvasLayer::RectType::Outline);
int ml = 0; int ml = 0;
int md = 0; int md = 0;
std::pair<float, float> dotssize; // TODO static? std::pair<float, float> dotssize; // TODO static?
@ -74,9 +74,9 @@ void CTextInput::Draw(int x, int y) {
ml = i; ml = i;
} }
if (ml) { if (ml) {
glez::draw::string(x + 2, y + 2, "..." + value.substr(md), this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); canvas->String({ 2, 2 }, "..." + value.substr(md), glez::color::white);
} else { } else {
glez::draw::string(x + 2, y + 2, value, this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); // TODO recalc on update canvas->String({ 2, 2 }, value, glez::color::white); // TODO recalc on update
} }
} }

View File

@ -123,12 +123,12 @@ std::string CTextLabel::GetText() {
return this->text; return this->text;
} }
void CTextLabel::Draw(int x, int y) { void CTextLabel::Draw(ICanvas* canvas) {
if (this->centered) { if (this->centered) {
auto size = GetSize(); auto size = GetSize();
std::pair<float, float> ssize; std::pair<float, float> ssize;
this->GetCanvas()->GetFont().stringSize(GetText(), &ssize.first, &ssize.second); this->GetCanvas()->GetFont().stringSize(GetText(), &ssize.first, &ssize.second);
glez::draw::string(x + (size.first - ssize.first) / 2, y + (size.second - ssize.second) / 2, GetText(), this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); canvas->String({ (size.first - ssize.first) / 2, (size.second - ssize.second) / 2 }, GetText(), glez::color::white);
} else } else
glez::draw::string(x, y, GetText(), this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); canvas->String({ 0, 0 }, GetText(), glez::color::white);
} }

View File

@ -29,12 +29,12 @@ CTitleBar::CTitleBar(IWidget* parent, std::string title)
SetPositionMode(ABSOLUTE); SetPositionMode(ABSOLUTE);
} }
void CTitleBar::Draw(int x, int y) { void CTitleBar::Draw(ICanvas* canvas) {
auto size = GetSize(); auto size = GetSize();
glez::draw::rect(x, y, size.first, size.second, this->GetCanvas()->GetColor()); canvas->Rect({ { 0, 0 }, { size } }, this->GetCanvas()->GetColor());
float l, h; float l, h;
this->GetCanvas()->GetFont().stringSize(m_strTitle, &l, &h); this->GetCanvas()->GetFont().stringSize(m_strTitle, &l, &h);
glez::draw::string(x + (size.first - l) / 2, y + TITLEBAR_PADDING_H, m_strTitle, this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); canvas->String({ (size.first - l) / 2, TITLEBAR_PADDING_H }, m_strTitle, glez::color::white);
} }
void CTitleBar::OnMouseMove(std::pair<int, int> delta) { void CTitleBar::OnMouseMove(std::pair<int, int> delta) {