Add Input functionality to Ultra
- Add point InSpace check to PD::Li::Math - Add Universal AlignmentCenter flag for Horizontal and and Vertical Alignment - Add Fallbackfont to Layout (if you dont want to set font per object) - Add Button Object WIP - Rename OnHover to OnFocus and add OnUnfocus - Move font and FontScale to ElementBase (for fallback logic etc) - Add UpdateInput func to ElementBase - Corectly Set fontScale in Text Rendering - Update ecample
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
#include <pd/drivers/drivers.hpp>
|
||||
#include <pd/ultra/container.hpp>
|
||||
#include <pd/ultra/elems/button.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace Ultra {
|
||||
PD_API void Button::Draw(PD::Li::Drawlist& l) {
|
||||
float r = pRounding;
|
||||
if (pParent) r = pParent->GetCanvas().VTranslateSize(r).x;
|
||||
l.PathRect(pRenderspace.TopLeft(), pRenderspace.BotRight(), r);
|
||||
if (pLined) {
|
||||
l.PathStroke(pRenderColor, pThickness, LiDrawFlags_Close);
|
||||
} else {
|
||||
l.PathFill(pRenderColor);
|
||||
}
|
||||
if (!pFont) return; // discard here if we dont have a font
|
||||
PD::fvec2 off = 0.f;
|
||||
if (pParent) off = pParent->GetCanvas().VTranslateSize(pAsp) * 0.5;
|
||||
l.SetFont(pFont);
|
||||
l.SetFontscale(pParent->GetCanvas().VTranslateFontscale(pFontScale));
|
||||
l.DrawText(pRenderspace.TopLeft() + off, pText.c_str(), pTextColor);
|
||||
}
|
||||
|
||||
PD_API void Button::Update() {
|
||||
pRenderColor = pColor;
|
||||
if (!pParent || !pFont) ElementBase::Update();
|
||||
PD::fvec2 size = pSize;
|
||||
if (size == PD::fvec2(0)) {
|
||||
size = pFont->GetTextBounds(
|
||||
pText.c_str(),
|
||||
pParent->GetCanvas().VTranslateFontscale(pFontScale)) +
|
||||
pParent->GetCanvas().VTranslateSize(pAsp);
|
||||
} else {
|
||||
size = pParent->GetCanvas().VTranslateSize(size);
|
||||
}
|
||||
pRenderspace = pParent->GetCanvas().VTranslateObject(
|
||||
pParent->GetTopLeft() + pPos, size, pAlignment, true);
|
||||
if (PD::Li::Math::InSpace(PD::Hid::MousePos(), pRenderspace)) {
|
||||
pRenderColor = pHovered;
|
||||
} else {
|
||||
pRenderColor = pColor;
|
||||
}
|
||||
UpdateInput();
|
||||
}
|
||||
} // namespace Ultra
|
||||
} // namespace PD
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <pd/drivers/drivers.hpp>
|
||||
#include <pd/ultra/container.hpp>
|
||||
#include <pd/ultra/elems/element.hpp>
|
||||
|
||||
@@ -12,11 +13,35 @@ PD_API bool ElementBase::RevisionUpdate(PD::u32 req) {
|
||||
}
|
||||
}
|
||||
PD_API void ElementBase::Update() {
|
||||
if (!pParent) pRenderspace = PD::fvec4(pPos, pPos + pSize);
|
||||
pRenderspace = pParent->GetCanvas().VTranslateObject(
|
||||
pParent->GetTopLeft() + pPos, pSize, pAlignment);
|
||||
if (!pParent)
|
||||
pRenderspace = PD::fvec4(pPos, pPos + pSize);
|
||||
else
|
||||
pRenderspace = pParent->GetCanvas().VTranslateObject(
|
||||
pParent->GetTopLeft() + pPos, pSize, pAlignment);
|
||||
UpdateInput();
|
||||
/*pRenderspace = PD::fvec4(pParent->GetTopLeft() + pPos,
|
||||
pParent->GetTopLeft() + pPos + pSize);*/
|
||||
}
|
||||
|
||||
PD_API void ElementBase::UpdateInput() {
|
||||
if (PD::Li::Math::InSpace(
|
||||
PD::Hid::MousePos(),
|
||||
PD::fvec4(pRenderspace.TopLeft(), pRenderspace.BotRight()))) {
|
||||
if (pHover && !pFocued) {
|
||||
pHover();
|
||||
pFocued = true;
|
||||
}
|
||||
if (PD::Hid::IsEvent(PD::Hid::Event::Up, PD::Hid::Gamepad::Touch)) {
|
||||
if (pPress) {
|
||||
pPress();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (pUnHover && pFocued) {
|
||||
pUnHover();
|
||||
pFocued = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace Ultra
|
||||
} // namespace PD
|
||||
@@ -6,6 +6,7 @@ namespace Ultra {
|
||||
PD_API void Text::Draw(PD::Li::Drawlist& l) {
|
||||
if (!pFont) return;
|
||||
l.SetFont(pFont);
|
||||
l.SetFontscale(pParent->GetCanvas().VTranslateFontscale(pFontScale));
|
||||
l.DrawText(pRenderspace.TopLeft(), pText.c_str(), pColor);
|
||||
}
|
||||
|
||||
@@ -14,8 +15,8 @@ PD_API void Text::Update() {
|
||||
if (!pParent) ElementBase::Update();
|
||||
pRenderspace = pParent->GetCanvas().VTranslateObject(
|
||||
pParent->GetTopLeft() + pPos,
|
||||
pFont->GetTextBounds(pText.c_str(),
|
||||
pParent->GetCanvas().VTranslateFontscale(pScale)),
|
||||
pFont->GetTextBounds(
|
||||
pText.c_str(), pParent->GetCanvas().VTranslateFontscale(pFontScale)),
|
||||
pAlignment, true);
|
||||
}
|
||||
} // namespace Ultra
|
||||
|
||||
Reference in New Issue
Block a user