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:
2026-04-03 15:17:43 +02:00
parent af7fc026df
commit 679de3ae94
12 changed files with 195 additions and 19 deletions
+56
View File
@@ -0,0 +1,56 @@
#pragma once
#include <pd/core/core.hpp>
#include <pd/ultra/elems/element.hpp>
namespace PD {
namespace Ultra {
class PD_API Button : public ElementBase {
public:
Button() {}
Button(const PD::fvec2& pos, const PD::fvec2& size, const PD::Color& color,
float rounding = 0.f, UltraAlignment align = 0)
: pColor(color), pRounding(rounding) {
this->pAlignment = align;
this->pPos = pos;
this->pSize = size;
}
Button(float x, float y, float w, float h, const PD::Color& color,
float rounding = 0.f, UltraAlignment align = 0)
: pColor(color), pRounding(rounding) {
this->pAlignment = align;
this->pPos = PD::fvec2(x, y);
this->pSize = PD::fvec2(w, h);
}
~Button() {}
void Draw(PD::Li::Drawlist& l) override;
void Update() override;
void SetColor(const PD::Color& color) { pColor = color; }
void SetFocusedColor(const PD::Color& color) { pHovered = color; }
void SetTextColor(const PD::Color& color) { pTextColor = color; }
void SetRounding(float r) { pRounding = r; }
void SetLined(bool v) { pLined = v; }
void SetThickness(int v) { pThickness = v; }
void SetText(const std::string& text) { pText = text; }
void SetAutoSizePadding(const PD::fvec2& size) { pAsp = size; }
// Discard these funcs
void OnFocus(EventFunc func) override {}
void OnUnFocus(EventFunc func) override {}
private:
std::string pText;
PD::Color pRenderColor;
PD::Color pColor;
PD::Color pHovered;
PD::Color pTextColor;
float pRounding = 0.f;
bool pLined = false;
int pThickness = 1.f;
// Auto-Size-Padding
PD::fvec2 pAsp = PD::fvec2(30, 10);
};
} // namespace Ultra
} // namespace PD
+23 -2
View File
@@ -27,16 +27,32 @@ class PD_API ElementBase {
void SetSize(const PD::fvec2& size) { pSize = size; }
void SetSize(float w, float h) { pSize = PD::fvec2(w, h); }
/**
* Executed every frame
* Executed on Hovering
* Elemnents can override / discard this func
*/
virtual void OnHover(EventFunc func) { pHover = func; }
virtual void OnFocus(EventFunc func) { pHover = func; }
/**
* Executed on Mocing out of the space
* Elemnents can override / discard this func
*/
virtual void OnUnFocus(EventFunc func) { pUnHover = func; }
/**
* Executrd on KeyUp event
* Elemnents can override / discard this func
*/
virtual void OnPress(EventFunc func) { pPress = func; }
void SetFontScale(float s = 1.f) { pFontScale = 1.f; }
float GetFontScale() const { return pFontScale; }
void SetFont(PD::Li::Font& font) { pFont = &font; }
void SetFontIfNull(PD::Li::Font& font) {
if (!pFont) pFont = &font;
}
virtual void UpdateInput();
protected:
friend class Container;
void SetParent(Container* c) { pParent = c; }
@@ -48,7 +64,12 @@ class PD_API ElementBase {
PD::fvec2 pSize;
PD::Li::Rect pRenderspace;
EventFunc pHover = nullptr;
EventFunc pUnHover = nullptr;
EventFunc pPress = nullptr;
bool pFocued = false;
// Not used by every object btw
PD::Li::Font* pFont = nullptr;
float pFontScale = 1.f;
};
} // namespace Ultra
} // namespace PD
-4
View File
@@ -15,14 +15,10 @@ class PD_API Text : public ElementBase {
void SetColor(const PD::Color& color) { pColor = color; }
void SetText(const std::string& text) { pText = text; }
void SetFont(PD::Li::Font& font) { pFont = &font; }
void SetScale(float scale) { pScale = scale; }
private:
PD::Color pColor;
float pScale = 1.f;
std::string pText;
PD::Li::Font* pFont = nullptr;
};
} // namespace Ultra