Rework Project Ultra
- Added container - Made container the base of Layout - Set Containers theirself as pParent for Elements on Push
This commit is contained in:
38
include/pd/ultra/container.hpp
Normal file
38
include/pd/ultra/container.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <pd/lithium/lithium.hpp>
|
||||
#include <pd/ultra/elems/element.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace Ultra {
|
||||
class Container {
|
||||
public:
|
||||
Container() {}
|
||||
Container(const PD::Li::Rect& r) : pRect(r) {}
|
||||
virtual ~Container() {}
|
||||
|
||||
void Push(PD::Ultra::ElementBase* elem) {
|
||||
elem->SetParent(this);
|
||||
pElems.Push(elem);
|
||||
}
|
||||
void Reset() { pElems.ResetFast(); }
|
||||
const PD::Li::Rect& GetRenderspace() const { return pRect; }
|
||||
|
||||
void SetViewport(const PD::fvec2& vp) { pRect = PD::fvec4(PD::fvec2(0), vp); }
|
||||
|
||||
const PD::fvec2 GetTopLeft() const { return pRect.TopLeft(); }
|
||||
const PD::fvec2 GetTopRight() const { return pRect.TopRight(); }
|
||||
const PD::fvec2 GetBotLeft() const { return pRect.BotLeft(); }
|
||||
const PD::fvec2 GetBotRight() const { return pRect.BotRight(); }
|
||||
const PD::fvec2 GetSize() const { return pRect.BotRight() - pRect.TopLeft(); }
|
||||
const PD::fvec2 GetPosition() const { return pRect.TopLeft(); }
|
||||
|
||||
protected:
|
||||
PD::Pool<PD::Ultra::ElementBase*>& GetElements() { return pElems; }
|
||||
|
||||
private:
|
||||
PD::Li::Rect pRect;
|
||||
PD::Pool<PD::Ultra::ElementBase*> pElems;
|
||||
};
|
||||
} // namespace Ultra
|
||||
} // namespace PD
|
||||
@@ -6,6 +6,7 @@
|
||||
namespace PD {
|
||||
namespace Ultra {
|
||||
class Canvas;
|
||||
class Container;
|
||||
class PD_API ElementBase {
|
||||
public:
|
||||
ElementBase() {}
|
||||
@@ -16,7 +17,6 @@ class PD_API ElementBase {
|
||||
* Reset Function (for PD::Pool::FastReset)
|
||||
*/
|
||||
virtual void Reset() {}
|
||||
void Update(Canvas& c);
|
||||
|
||||
void SetAlignment(UltraAlignment a) { pAlignment = a; }
|
||||
void SetPosition(const PD::fvec2& pos) { pPos = pos; }
|
||||
@@ -25,13 +25,14 @@ class PD_API ElementBase {
|
||||
void SetSize(float w, float h) { pSize = PD::fvec2(w, h); }
|
||||
|
||||
protected:
|
||||
friend class Container;
|
||||
void SetParent(Container* c) { pParent = c; }
|
||||
bool RevisionUpdate(PD::u32 req);
|
||||
Container* pParent;
|
||||
PD::u32 pCanvasRev = 0;
|
||||
PD::Li::Rect pRenderspace;
|
||||
UltraAlignment pAlignment = 0;
|
||||
PD::fvec2 pPos;
|
||||
PD::fvec2 pSize;
|
||||
ElementBase* pParent = nullptr;
|
||||
};
|
||||
} // namespace Ultra
|
||||
} // namespace PD
|
||||
@@ -28,10 +28,14 @@ class PD_API Rect : public ElementBase {
|
||||
|
||||
void SetColor(const PD::Color& color) { pColor = color; }
|
||||
void SetRounding(float r) { pRounding = r; }
|
||||
void SetLined(bool v) { pLined = v; }
|
||||
void SetThickness(int v) { pThickness = v; }
|
||||
|
||||
private:
|
||||
PD::Color pColor;
|
||||
float pRounding = 0.f;
|
||||
bool pLined = false;
|
||||
int pThickness = 1.f;
|
||||
};
|
||||
} // namespace Ultra
|
||||
|
||||
|
||||
@@ -1,28 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <pd/ultra/canvas.hpp>
|
||||
#include <pd/ultra/container.hpp>
|
||||
#include <pd/ultra/elems/element.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace Ultra {
|
||||
class PD_API Layout {
|
||||
class PD_API Layout : public Container {
|
||||
public:
|
||||
Layout() {}
|
||||
~Layout() {}
|
||||
|
||||
void Add(ElementBase& elem);
|
||||
|
||||
void SetFont(PD::Li::Font& font);
|
||||
|
||||
void Render();
|
||||
|
||||
const PD::Li::Drawlist& Data() const { return pList; }
|
||||
Canvas& GetCanvas() { return pCanvas; }
|
||||
const Canvas& GetCanvas() const { return pCanvas; }
|
||||
|
||||
private:
|
||||
Canvas pCanvas;
|
||||
PD::Pool<ElementBase*> pElements;
|
||||
PD::Li::Drawlist pList;
|
||||
};
|
||||
} // namespace Ultra
|
||||
|
||||
@@ -11,16 +11,5 @@ PD_API bool ElementBase::RevisionUpdate(PD::u32 req) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
PD_API void ElementBase::Update(Canvas& c) {
|
||||
if (RevisionUpdate(c.GetRevision())) {
|
||||
if (pParent) {
|
||||
pRenderspace = c.VTranslateObject(
|
||||
pParent->pRenderspace.TopLeft(),
|
||||
pRenderspace.BotRight() - pRenderspace.TopLeft(), pAlignment, true);
|
||||
} else {
|
||||
pRenderspace = c.VTranslateObject(pPos, pSize, pAlignment);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace Ultra
|
||||
} // namespace PD
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace PD {
|
||||
namespace Ultra {
|
||||
PD_API void Image::Draw(PD::Li::Drawlist& l) {
|
||||
l.BindTexture(*pTex);
|
||||
l.PathRect(pRenderspace.TopLeft(), pRenderspace.BotRight(), pRounding);
|
||||
l.PathFill(pColor);
|
||||
// l.BindTexture(*pTex);
|
||||
// l.PathRect(pRenderspace.TopLeft(), pRenderspace.BotRight(), pRounding);
|
||||
// l.PathFill(pColor);
|
||||
}
|
||||
} // namespace Ultra
|
||||
} // namespace PD
|
||||
@@ -3,10 +3,12 @@
|
||||
namespace PD {
|
||||
namespace Ultra {
|
||||
PD_API void Rect::Draw(PD::Li::Drawlist& l) {
|
||||
l.PathRect(pRenderspace.TopLeft(), pRenderspace.BotRight(), pRounding);
|
||||
l.PathFill(pColor);
|
||||
l.DrawText(pRenderspace.BotRight(), std::to_string(pCanvasRev).c_str(),
|
||||
0xff0000ff);
|
||||
l.PathRect(pPos, pPos + pSize, pRounding);
|
||||
if (pLined) {
|
||||
l.PathStroke(pColor, pThickness, LiDrawFlags_Close);
|
||||
} else {
|
||||
l.PathFill(pColor);
|
||||
}
|
||||
}
|
||||
} // namespace Ultra
|
||||
} // namespace PD
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace PD {
|
||||
namespace Ultra {
|
||||
PD_API void Text::Draw(PD::Li::Drawlist& l) {
|
||||
l.DrawText(pRenderspace.TopLeft(), pText.c_str(), pColor);
|
||||
l.DrawText(pPos, pText.c_str(), pColor);
|
||||
}
|
||||
} // namespace Ultra
|
||||
} // namespace PD
|
||||
@@ -2,14 +2,11 @@
|
||||
|
||||
namespace PD {
|
||||
namespace Ultra {
|
||||
PD_API void Layout::Add(ElementBase& elem) { *pElements.Allocate() = &elem; }
|
||||
|
||||
PD_API void Layout::SetFont(PD::Li::Font& font) { pList.SetFont(&font); }
|
||||
|
||||
PD_API void Layout::Render() {
|
||||
pList.Clear();
|
||||
for (auto& it : pElements) {
|
||||
it->Update(pCanvas);
|
||||
for (auto& it : GetElements()) {
|
||||
it->Draw(pList);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#include <pd/ultra/elems/rect.hpp>
|
||||
#include <pd/ultra/elems/text.hpp>
|
||||
#include <pd/ultra/layout.hpp>
|
||||
|
||||
////
|
||||
|
||||
PD::OsCtx* pOs = nullptr;
|
||||
@@ -23,20 +22,6 @@ const char* ResourcePath(const char* in) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void DrawTextHaxxed(const char* text, PD::Li::Font& font, PD::Li::Drawlist& l,
|
||||
const PD::Ultra::Canvas& c, const PD::fvec2& pos,
|
||||
const PD::Color& color, UltraAlignment align) {
|
||||
PD::fvec2 bounds = font.GetTextBounds(text, l.GetFontScale());
|
||||
PD::Li::Rect _b = c.VTranslateObject(pos, bounds, align, true);
|
||||
l.UnbindTexture();
|
||||
l.PathRect(_b.TopLeft(), _b.BotRight());
|
||||
l.PathFill(0x30ff00ff);
|
||||
l.PathRect(_b.TopLeft(), _b.BotRight());
|
||||
l.PathStroke(0xffff00ff, 1, LiDrawFlags_Close);
|
||||
l.DrawText(c.VTranslateObject(pos, bounds, align, true).TopLeft(), text,
|
||||
color);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// PD::LogFilter(PD::LogLevel::Warning);
|
||||
Driver drv = Driver::OpenGL3;
|
||||
@@ -65,48 +50,41 @@ int main(int argc, char** argv) {
|
||||
font.LoadTTF(ResourcePath("default.ttf"), 64);
|
||||
pList.SetFont(&font);
|
||||
PD::Ultra::Layout lyt;
|
||||
lyt.GetCanvas().SetVirtualViewport(PD::fvec2(400, 240));
|
||||
lyt.SetViewport(PD::fvec2(1280, 720));
|
||||
lyt.SetFont(font);
|
||||
PD::Ultra::Rect rr(
|
||||
0, 0, 90, 60, PD::Color("#ffff00"), 12.f,
|
||||
UltraAlignment_CenterHorizontal | UltraAlignment_CenterVertical);
|
||||
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;
|
||||
r.SetColor(0xff0000ff);
|
||||
r.SetRounding(10.f);
|
||||
r.SetPosition(250, 150);
|
||||
r.SetSize(100, 70);
|
||||
lyt.Push(&rr);
|
||||
lyt.Push(&r);
|
||||
PD::Ultra::Text txt;
|
||||
PD::Ultra::Image _img(&pTex, 10, 10, 5.f, UltraAlignment_BotRight);
|
||||
_img.SetSize(90); // Override size
|
||||
lyt.Add(rr);
|
||||
lyt.Add(_img);
|
||||
txt.SetPosition(0);
|
||||
txt.SetColor(0xffffffff);
|
||||
txt.SetText("const std::string &text");
|
||||
txt.SetAlignment(UltraAlignment_TopLeft);
|
||||
lyt.Add(txt);
|
||||
txt.SetPosition(PD::fvec2(5, 200));
|
||||
txt.SetText("OpenGL");
|
||||
txt.SetColor(PD::Color("#ffffffff"));
|
||||
lyt.Push(&txt);
|
||||
while (pOs->Mainloop()) {
|
||||
lyt.GetCanvas().SetViewport(pOs->GetViewport());
|
||||
pOs->ClearViewPort();
|
||||
PD::Li::ResetPools();
|
||||
pList.SetFontscale(lyt.GetCanvas().VTranslateFontscale(0.7f));
|
||||
pList.DrawRectFilled(150, 50, 0x88ffffff);
|
||||
pList.DrawRect(150, 50, 0xffffffff);
|
||||
lyt.Render();
|
||||
/*auto t = lyt.GetCanvas().VTranslateObject(
|
||||
PD::fvec2(5, 30), PD::fvec2(170, 110), UltraAlignment_TopLeft);
|
||||
auto icnpos = lyt.GetCanvas().VTranslateObject(
|
||||
PD::fvec2(5, 5), PD::fvec2(60), UltraAlignment_TopRight);
|
||||
pList.PathRect(t.TopLeft(), t.BotRight(),
|
||||
lyt.GetCanvas().VTranslateSize(10).x);
|
||||
pList.PathFill(0xff00ffff);
|
||||
pList.BindTexture(pTex);
|
||||
pList.PathRect(icnpos.TopLeft(), icnpos.BotRight());
|
||||
pList.PathFill(0xffffffff);
|
||||
DrawTextHaxxed("Hello World!", font, pList, lyt.GetCanvas(),
|
||||
PD::fvec2(5, 5), 0xff0000ff, UltraAlignment_TopLeft);*/
|
||||
DrawTextHaxxed(
|
||||
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(),
|
||||
font, pList, lyt.GetCanvas(), PD::fvec2(5, 5), 0xffffffff,
|
||||
UltraAlignment_BotLeft);
|
||||
0xffffffff);
|
||||
PD::Gfx::Reset();
|
||||
PD::Gfx::Draw(pList);
|
||||
PD::Gfx::Draw(lyt.Data());
|
||||
|
||||
Reference in New Issue
Block a user