Implement VCanvas and Update TextElem
- Text now requires a font and is able to take an individual scale - Container requires &ref for Elements to make sure they always exist - SetViewport now sets the canvas as well - SetBaseViewport Sets the Virtual Canvas Size - Layout Now requires a Drawlist reference in Render function - main.cpp: updated the template
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <pd/lithium/lithium.hpp>
|
#include <pd/lithium/lithium.hpp>
|
||||||
|
#include <pd/ultra/canvas.hpp>
|
||||||
#include <pd/ultra/elems/element.hpp>
|
#include <pd/ultra/elems/element.hpp>
|
||||||
|
|
||||||
namespace PD {
|
namespace PD {
|
||||||
@@ -11,14 +12,18 @@ class Container {
|
|||||||
Container(const PD::Li::Rect& r) : pRect(r) {}
|
Container(const PD::Li::Rect& r) : pRect(r) {}
|
||||||
virtual ~Container() {}
|
virtual ~Container() {}
|
||||||
|
|
||||||
void Push(PD::Ultra::ElementBase* elem) {
|
void Push(PD::Ultra::ElementBase& elem) {
|
||||||
elem->SetParent(this);
|
elem.SetParent(this);
|
||||||
pElems.Push(elem);
|
pElems.Push(&elem);
|
||||||
}
|
}
|
||||||
void Reset() { pElems.ResetFast(); }
|
void Reset() { pElems.ResetFast(); }
|
||||||
const PD::Li::Rect& GetRenderspace() const { return pRect; }
|
const PD::Li::Rect& GetRenderspace() const { return pRect; }
|
||||||
|
|
||||||
void SetViewport(const PD::fvec2& vp) { pRect = PD::fvec4(PD::fvec2(0), vp); }
|
void SetViewport(const PD::fvec2& vp) {
|
||||||
|
pCanvas.SetViewport(vp);
|
||||||
|
pRect = PD::fvec4(PD::fvec2(0), vp);
|
||||||
|
}
|
||||||
|
void SetBaseViewport(const PD::ivec2& vp) { pCanvas.SetVirtualViewport(vp); }
|
||||||
|
|
||||||
const PD::fvec2 GetTopLeft() const { return pRect.TopLeft(); }
|
const PD::fvec2 GetTopLeft() const { return pRect.TopLeft(); }
|
||||||
const PD::fvec2 GetTopRight() const { return pRect.TopRight(); }
|
const PD::fvec2 GetTopRight() const { return pRect.TopRight(); }
|
||||||
@@ -26,12 +31,14 @@ class Container {
|
|||||||
const PD::fvec2 GetBotRight() const { return pRect.BotRight(); }
|
const PD::fvec2 GetBotRight() const { return pRect.BotRight(); }
|
||||||
const PD::fvec2 GetSize() const { return pRect.BotRight() - pRect.TopLeft(); }
|
const PD::fvec2 GetSize() const { return pRect.BotRight() - pRect.TopLeft(); }
|
||||||
const PD::fvec2 GetPosition() const { return pRect.TopLeft(); }
|
const PD::fvec2 GetPosition() const { return pRect.TopLeft(); }
|
||||||
|
const Canvas& GetCanvas() const { return pCanvas; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
PD::Pool<PD::Ultra::ElementBase*>& GetElements() { return pElems; }
|
PD::Pool<PD::Ultra::ElementBase*>& GetElements() { return pElems; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PD::Li::Rect pRect;
|
PD::Li::Rect pRect;
|
||||||
|
Canvas pCanvas;
|
||||||
PD::Pool<PD::Ultra::ElementBase*> pElems;
|
PD::Pool<PD::Ultra::ElementBase*> pElems;
|
||||||
};
|
};
|
||||||
} // namespace Ultra
|
} // namespace Ultra
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <pd/lithium/lithium.hpp>
|
#include <pd/lithium/lithium.hpp>
|
||||||
#include <pd/ultra/flags.hpp>
|
#include <pd/ultra/flags.hpp>
|
||||||
|
|
||||||
@@ -7,6 +8,7 @@ namespace PD {
|
|||||||
namespace Ultra {
|
namespace Ultra {
|
||||||
class Canvas;
|
class Canvas;
|
||||||
class Container;
|
class Container;
|
||||||
|
using EventFunc = std::function<void()>;
|
||||||
class PD_API ElementBase {
|
class PD_API ElementBase {
|
||||||
public:
|
public:
|
||||||
ElementBase() {}
|
ElementBase() {}
|
||||||
@@ -17,24 +19,36 @@ class PD_API ElementBase {
|
|||||||
* Reset Function (for PD::Pool::FastReset)
|
* Reset Function (for PD::Pool::FastReset)
|
||||||
*/
|
*/
|
||||||
virtual void Reset() {}
|
virtual void Reset() {}
|
||||||
void Update();
|
virtual void Update();
|
||||||
|
|
||||||
void SetAlignment(UltraAlignment a) { pAlignment = a; }
|
void SetAlignment(UltraAlignment a) { pAlignment = a; }
|
||||||
void SetPosition(const PD::fvec2& pos) { pPos = pos; }
|
void SetPosition(const PD::fvec2& pos) { pPos = pos; }
|
||||||
void SetPosition(float x, float y) { pPos = PD::fvec2(x, y); }
|
void SetPosition(float x, float y) { pPos = PD::fvec2(x, y); }
|
||||||
void SetSize(const PD::fvec2& size) { pSize = size; }
|
void SetSize(const PD::fvec2& size) { pSize = size; }
|
||||||
void SetSize(float w, float h) { pSize = PD::fvec2(w, h); }
|
void SetSize(float w, float h) { pSize = PD::fvec2(w, h); }
|
||||||
|
/**
|
||||||
|
* Executed every frame
|
||||||
|
* Elemnents can override / discard this func
|
||||||
|
*/
|
||||||
|
virtual void OnHover(EventFunc func) { pHover = func; }
|
||||||
|
/**
|
||||||
|
* Executrd on KeyUp event
|
||||||
|
* Elemnents can override / discard this func
|
||||||
|
*/
|
||||||
|
virtual void OnPress(EventFunc func) { pPress = func; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend class Container;
|
friend class Container;
|
||||||
void SetParent(Container* c) { pParent = c; }
|
void SetParent(Container* c) { pParent = c; }
|
||||||
bool RevisionUpdate(PD::u32 req);
|
bool RevisionUpdate(PD::u32 req);
|
||||||
Container* pParent;
|
Container* pParent = nullptr;
|
||||||
PD::u32 pCanvasRev = 0;
|
PD::u32 pCanvasRev = 0;
|
||||||
UltraAlignment pAlignment = 0;
|
UltraAlignment pAlignment = 0;
|
||||||
PD::fvec2 pPos;
|
PD::fvec2 pPos;
|
||||||
PD::fvec2 pSize;
|
PD::fvec2 pSize;
|
||||||
PD::Li::Rect pRenderspace;
|
PD::Li::Rect pRenderspace;
|
||||||
|
EventFunc pHover = nullptr;
|
||||||
|
EventFunc pPress = nullptr;
|
||||||
};
|
};
|
||||||
} // namespace Ultra
|
} // namespace Ultra
|
||||||
} // namespace PD
|
} // namespace PD
|
||||||
@@ -11,13 +11,18 @@ class PD_API Text : public ElementBase {
|
|||||||
~Text() {}
|
~Text() {}
|
||||||
|
|
||||||
void Draw(PD::Li::Drawlist& l) override;
|
void Draw(PD::Li::Drawlist& l) override;
|
||||||
|
void Update() override;
|
||||||
|
|
||||||
void SetColor(const PD::Color& color) { pColor = color; }
|
void SetColor(const PD::Color& color) { pColor = color; }
|
||||||
void SetText(const std::string& text) { pText = text; }
|
void SetText(const std::string& text) { pText = text; }
|
||||||
|
void SetFont(PD::Li::Font& font) { pFont = &font; }
|
||||||
|
void SetScale(float scale) { pScale = scale; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PD::Color pColor;
|
PD::Color pColor;
|
||||||
|
float pScale = 1.f;
|
||||||
std::string pText;
|
std::string pText;
|
||||||
|
PD::Li::Font* pFont = nullptr;
|
||||||
};
|
};
|
||||||
} // namespace Ultra
|
} // namespace Ultra
|
||||||
|
|
||||||
|
|||||||
@@ -11,14 +11,7 @@ class PD_API Layout : public Container {
|
|||||||
Layout() {}
|
Layout() {}
|
||||||
~Layout() {}
|
~Layout() {}
|
||||||
|
|
||||||
void SetFont(PD::Li::Font& font);
|
void Render(PD::Li::Drawlist& list);
|
||||||
|
|
||||||
void Render();
|
|
||||||
|
|
||||||
const PD::Li::Drawlist& Data() const { return pList; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
PD::Li::Drawlist pList;
|
|
||||||
};
|
};
|
||||||
} // namespace Ultra
|
} // namespace Ultra
|
||||||
} // namespace PD
|
} // namespace PD
|
||||||
@@ -13,8 +13,10 @@ PD_API bool ElementBase::RevisionUpdate(PD::u32 req) {
|
|||||||
}
|
}
|
||||||
PD_API void ElementBase::Update() {
|
PD_API void ElementBase::Update() {
|
||||||
if (!pParent) pRenderspace = PD::fvec4(pPos, pPos + pSize);
|
if (!pParent) pRenderspace = PD::fvec4(pPos, pPos + pSize);
|
||||||
pRenderspace = PD::fvec4(pParent->GetTopLeft() + pPos,
|
pRenderspace = pParent->GetCanvas().VTranslateObject(
|
||||||
pParent->GetTopLeft() + pPos + pSize);
|
pParent->GetTopLeft() + pPos, pSize, pAlignment);
|
||||||
|
/*pRenderspace = PD::fvec4(pParent->GetTopLeft() + pPos,
|
||||||
|
pParent->GetTopLeft() + pPos + pSize);*/
|
||||||
}
|
}
|
||||||
} // namespace Ultra
|
} // namespace Ultra
|
||||||
} // namespace PD
|
} // namespace PD
|
||||||
@@ -1,9 +1,22 @@
|
|||||||
|
#include <pd/ultra/container.hpp>
|
||||||
#include <pd/ultra/elems/text.hpp>
|
#include <pd/ultra/elems/text.hpp>
|
||||||
|
|
||||||
namespace PD {
|
namespace PD {
|
||||||
namespace Ultra {
|
namespace Ultra {
|
||||||
PD_API void Text::Draw(PD::Li::Drawlist& l) {
|
PD_API void Text::Draw(PD::Li::Drawlist& l) {
|
||||||
|
if (!pFont) return;
|
||||||
|
l.SetFont(pFont);
|
||||||
l.DrawText(pRenderspace.TopLeft(), pText.c_str(), pColor);
|
l.DrawText(pRenderspace.TopLeft(), pText.c_str(), pColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PD_API void Text::Update() {
|
||||||
|
if (!pFont) return;
|
||||||
|
if (!pParent) ElementBase::Update();
|
||||||
|
pRenderspace = pParent->GetCanvas().VTranslateObject(
|
||||||
|
pParent->GetTopLeft() + pPos,
|
||||||
|
pFont->GetTextBounds(pText.c_str(),
|
||||||
|
pParent->GetCanvas().VTranslateFontscale(pScale)),
|
||||||
|
pAlignment, true);
|
||||||
|
}
|
||||||
} // namespace Ultra
|
} // namespace Ultra
|
||||||
} // namespace PD
|
} // namespace PD
|
||||||
@@ -3,17 +3,16 @@
|
|||||||
|
|
||||||
namespace PD {
|
namespace PD {
|
||||||
namespace Ultra {
|
namespace Ultra {
|
||||||
PD_API void Layout::SetFont(PD::Li::Font& font) { pList.SetFont(&font); }
|
PD_API void Layout::Render(PD::Li::Drawlist& list) {
|
||||||
|
float fc = list.GetFontScale();
|
||||||
PD_API void Layout::Render() {
|
list.SetFontscale(GetCanvas().VTranslateFontscale(fc));
|
||||||
pList.Clear();
|
|
||||||
for (auto& it : GetElements()) {
|
for (auto& it : GetElements()) {
|
||||||
it->Update();
|
it->Update();
|
||||||
it->Draw(pList);
|
it->Draw(list);
|
||||||
}
|
}
|
||||||
pList.DrawText(PD::fvec2(5, GetBotLeft().y - 40),
|
list.DrawText(PD::fvec2(5, GetBotLeft().y - 40),
|
||||||
std::format("Lyt: [{}]", GetRenderspace()).c_str(),
|
std::format("Lyt: [{}]", GetRenderspace()).c_str(), 0xffff00ff);
|
||||||
0xffff00ff);
|
list.SetFontscale(fc);
|
||||||
}
|
}
|
||||||
} // namespace Ultra
|
} // namespace Ultra
|
||||||
} // namespace PD
|
} // namespace PD
|
||||||
+40
-36
@@ -22,6 +22,43 @@ const char* ResourcePath(const char* in) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MainMenu : public PD::Ultra::Layout {
|
||||||
|
public:
|
||||||
|
MainMenu(PD::Li::Font& font) {
|
||||||
|
SetBaseViewport(PD::ivec2(1280, 720));
|
||||||
|
pBackground.SetSize(800, 450);
|
||||||
|
pBackground.SetColor(PD::Color("#ffffffff"));
|
||||||
|
pBackground.SetAlignment(UltraAlignment_CenterHorizontal |
|
||||||
|
UltraAlignment_CenterVertical);
|
||||||
|
Push(pBackground);
|
||||||
|
pText.SetColor(PD::Color("#000000"));
|
||||||
|
pText.SetText("Hello World");
|
||||||
|
pText.SetAlignment(UltraAlignment_CenterHorizontal |
|
||||||
|
UltraAlignment_CenterVertical);
|
||||||
|
pText.SetFont(font);
|
||||||
|
Push(pText);
|
||||||
|
}
|
||||||
|
~MainMenu() {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
PD::Ultra::Rect pBackground;
|
||||||
|
PD::Ultra::Text pText;
|
||||||
|
};
|
||||||
|
|
||||||
|
class App {
|
||||||
|
public:
|
||||||
|
App(PD::Li::Font& font) : main(font) {}
|
||||||
|
~App() {}
|
||||||
|
|
||||||
|
void Update(PD::ivec2 vp, PD::Li::Drawlist& list) {
|
||||||
|
main.SetViewport(vp);
|
||||||
|
main.Render(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
MainMenu main;
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
// PD::LogFilter(PD::LogLevel::Warning);
|
// PD::LogFilter(PD::LogLevel::Warning);
|
||||||
Driver drv = Driver::OpenGL3;
|
Driver drv = Driver::OpenGL3;
|
||||||
@@ -49,46 +86,13 @@ int main(int argc, char** argv) {
|
|||||||
PD::Li::Font font;
|
PD::Li::Font font;
|
||||||
font.LoadTTF(ResourcePath("default.ttf"), 64);
|
font.LoadTTF(ResourcePath("default.ttf"), 64);
|
||||||
pList.SetFont(&font);
|
pList.SetFont(&font);
|
||||||
PD::Ultra::Layout lyt;
|
App app(font);
|
||||||
lyt.SetViewport(PD::fvec2(1280, 720));
|
|
||||||
lyt.SetFont(font);
|
|
||||||
PD::Ultra::Rect r;
|
|
||||||
r.SetColor(0xff0000ff);
|
|
||||||
r.SetRounding(10.f);
|
|
||||||
r.SetLined(true);
|
|
||||||
r.SetPosition(250, 150);
|
|
||||||
r.SetSize(100, 70);
|
|
||||||
PD::Ultra::Rect rr;
|
|
||||||
rr.SetColor(0x880000ff);
|
|
||||||
rr.SetRounding(10.f);
|
|
||||||
rr.SetPosition(250, 150);
|
|
||||||
rr.SetSize(100, 70);
|
|
||||||
lyt.Push(&rr);
|
|
||||||
lyt.Push(&r);
|
|
||||||
PD::Ultra::Text txt;
|
|
||||||
txt.SetPosition(PD::fvec2(5, 200));
|
|
||||||
txt.SetText("OpenGL");
|
|
||||||
txt.SetColor(PD::Color("#ffffffff"));
|
|
||||||
lyt.Push(&txt);
|
|
||||||
while (pOs->Mainloop()) {
|
while (pOs->Mainloop()) {
|
||||||
pOs->ClearViewPort();
|
pOs->ClearViewPort();
|
||||||
lyt.SetViewport(pOs->GetViewport());
|
PD::Li::ResetPools(); // Move to other place (or refactor this)
|
||||||
PD::Li::ResetPools();
|
app.Update(pOs->GetViewport(), pList);
|
||||||
pList.DrawRectFilled(150, 50, 0x88ffffff);
|
|
||||||
pList.DrawRect(150, 50, 0xffffffff);
|
|
||||||
lyt.Render();
|
|
||||||
pList.DrawText(
|
|
||||||
5,
|
|
||||||
std::format(
|
|
||||||
"Font Scale: {}\nVP: [{}]\nVIDC: [{}, {}, {}, {}]\nGfxDriver: {}",
|
|
||||||
pList.GetFontScale(), pOs->GetViewport(), PD::Gfx::GetNumVertices(),
|
|
||||||
PD::Gfx::GetNumIndices(), PD::Gfx::GetNumDrawcalls(),
|
|
||||||
PD::Gfx::GetNumCommands(), PD::Gfx::GetDriverName())
|
|
||||||
.c_str(),
|
|
||||||
0xffffffff);
|
|
||||||
PD::Gfx::Reset();
|
PD::Gfx::Reset();
|
||||||
PD::Gfx::Draw(pList);
|
PD::Gfx::Draw(pList);
|
||||||
PD::Gfx::Draw(lyt.Data());
|
|
||||||
pList.Clear();
|
pList.Clear();
|
||||||
pOs->SwapBuffers();
|
pOs->SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user