Compare commits
2
Commits
0db4953125
...
e03670a0c5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e03670a0c5
|
||
|
|
931e02aefb
|
@@ -24,6 +24,7 @@ SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/core.hpp>
|
||||
#include <pd/lithium/pd_p_api.hpp>
|
||||
#include <pd/lithium/texture.hpp>
|
||||
#include <pd/lithium/vertex.hpp>
|
||||
|
||||
@@ -71,82 +72,26 @@ class Command {
|
||||
Texture::Ref Tex;
|
||||
};
|
||||
|
||||
class CmdPool {
|
||||
class PD_LITHIUM_API CmdPool {
|
||||
public:
|
||||
CmdPool() {}
|
||||
~CmdPool() {}
|
||||
|
||||
Command::Ref NewCmd() {
|
||||
if (pPoolIdx >= pPool.size()) {
|
||||
Resize(pPool.size() + 128);
|
||||
}
|
||||
Command::Ref nu = pPool[pPoolIdx++];
|
||||
nu->Layer = Layer;
|
||||
nu->Index = pPoolIdx - 1;
|
||||
return nu;
|
||||
}
|
||||
|
||||
void Init(size_t initial_size) { Resize(initial_size); }
|
||||
|
||||
void Deinit() {
|
||||
for (auto it : pPool) {
|
||||
Command::Delete(it);
|
||||
}
|
||||
pPool.clear();
|
||||
}
|
||||
|
||||
void Resize(size_t nulen) {
|
||||
if (nulen <= pPool.size()) {
|
||||
return; // no idea yet
|
||||
}
|
||||
size_t oldlen = pPool.size();
|
||||
pPool.resize(nulen);
|
||||
for (size_t i = oldlen; i < pPool.size(); i++) {
|
||||
pPool[i] = Command::New();
|
||||
}
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
for (u32 i = 0; i < pPoolIdx; i++) {
|
||||
pPool[i]->Clear();
|
||||
}
|
||||
pPoolIdx = 0;
|
||||
}
|
||||
|
||||
Command::Ref GetCmd(size_t idx) const { return pPool[idx]; }
|
||||
Command::Ref GetCmd(size_t idx) { return pPool[idx]; }
|
||||
|
||||
size_t Size() const { return pPoolIdx; }
|
||||
size_t Cap() const { return pPool.size(); }
|
||||
|
||||
void Merge(CmdPool& p) {
|
||||
Copy(p);
|
||||
p.Reset();
|
||||
}
|
||||
|
||||
void Copy(CmdPool& p) {
|
||||
if (pPoolIdx + p.Size() > pPool.size()) {
|
||||
Resize(pPoolIdx + p.Size());
|
||||
}
|
||||
for (size_t i = 0; i < p.Size(); i++) {
|
||||
size_t idx = pPoolIdx++;
|
||||
*pPool[idx] = *p.GetCmd(i);
|
||||
pPool[idx]->Index = idx;
|
||||
pPool[idx]->Layer += Layer;
|
||||
}
|
||||
}
|
||||
|
||||
void Sort() {
|
||||
std::sort(begin(), end(),
|
||||
[](const Command::Ref& a, const Command::Ref& b) -> bool {
|
||||
if (a->Layer == b->Layer) {
|
||||
return a->Tex < b->Tex;
|
||||
}
|
||||
return a->Layer < b->Layer;
|
||||
});
|
||||
}
|
||||
Command::Ref NewCmd();
|
||||
void Init(size_t initial_size);
|
||||
void Deinit();
|
||||
void Resize(size_t nulen);
|
||||
void Reset();
|
||||
Command::Ref GetCmd(size_t idx) const;
|
||||
Command::Ref GetCmd(size_t idx);
|
||||
size_t Size() const;
|
||||
size_t Cap() const;
|
||||
void Merge(CmdPool& p);
|
||||
void Copy(CmdPool& p);
|
||||
void Sort();
|
||||
|
||||
private:
|
||||
static bool pTheOrder(const Command::Ref& a, const Command::Ref& b);
|
||||
friend class DrawList;
|
||||
Command::Ref* begin() { return &pPool[0]; }
|
||||
Command::Ref* end() { return &pPool[pPoolIdx]; }
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/container.hpp>
|
||||
#include <pd/ui7/io.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
/**
|
||||
* Slider Object can take a datatype or a list
|
||||
* and modifys these by moving left or right when dragging
|
||||
*/
|
||||
template <typename T>
|
||||
class PD_UI7_API Slider : public Container {
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
* @param label Label of the Button
|
||||
* @param data Data reference (Supported types can be seen in Slider.cpp)
|
||||
* @param num_elms Number of Array elements (for exaple with use ofvec4)
|
||||
* @param io IO Reference
|
||||
* @param min minimum number using Minimum limit
|
||||
* @param max Maximum number set by max limit by default
|
||||
* @param step To set the modifier for drag movement
|
||||
* @param precision for float and double to set precision
|
||||
*/
|
||||
Slider(const std::string& label, T* data, UI7::IO::Ref io,
|
||||
T min = std::numeric_limits<T>::min(),
|
||||
T max = std::numeric_limits<T>::max(), int precision = 1) {
|
||||
// PD::Assert(data != nullptr, "Input Data Address is null!");
|
||||
this->label = label;
|
||||
this->data = data;
|
||||
this->min = min;
|
||||
this->max = max;
|
||||
this->precision = precision;
|
||||
this->width = io->CurrentViewPort.z * 0.3f;
|
||||
this->tdim = io->Font->GetTextBounds(label, io->FontScale);
|
||||
}
|
||||
~Slider() = default;
|
||||
|
||||
/** Als ob das funktioniert... */
|
||||
PD_SHARED(Slider<T>);
|
||||
/**
|
||||
* Override for the Input Handler
|
||||
* @note This function is usally called by Menu::Update
|
||||
*/
|
||||
void HandleInput() override;
|
||||
/**
|
||||
* Override for the Rendering Handler
|
||||
* @note This function is usally called by Menu::Update
|
||||
* */
|
||||
void Draw() override;
|
||||
|
||||
/** Function to Update Size if framepadding changes */
|
||||
void Update() override;
|
||||
|
||||
private:
|
||||
fvec2 tdim; ///< Text size
|
||||
float width; ///< Size
|
||||
std::string label; ///< Label of the Button
|
||||
T* data;
|
||||
T min;
|
||||
T max;
|
||||
int precision = 1;
|
||||
float slw = 0.f; // silider drag width (calculated in update)
|
||||
float slp = 0.f; // silider drag pos (calculated in update)
|
||||
};
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -30,3 +30,4 @@ SOFTWARE.
|
||||
#include <pd/ui7/container/dynobj.hpp>
|
||||
#include <pd/ui7/container/image.hpp>
|
||||
#include <pd/ui7/container/label.hpp>
|
||||
#include <pd/ui7/container/slider.hpp>
|
||||
|
||||
@@ -25,6 +25,7 @@ SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/core.hpp>
|
||||
#include <pd/ui7/containers.hpp>
|
||||
#include <pd/ui7/io.hpp>
|
||||
#include <pd/ui7/layout.hpp>
|
||||
#include <pd/ui7/pd_p_api.hpp>
|
||||
@@ -92,6 +93,18 @@ class PD_UI7_API Menu {
|
||||
}
|
||||
pLayout->AddObject(r);
|
||||
}
|
||||
template <typename T>
|
||||
void Slider(const std::string &label, T *data,
|
||||
T min = std::numeric_limits<T>::min(),
|
||||
T max = std::numeric_limits<T>::max(), int precision = 1) {
|
||||
u32 id = Strings::FastHash("drd" + label + std::to_string((uintptr_t)data));
|
||||
Container::Ref r = pLayout->FindObject(id);
|
||||
if (!r) {
|
||||
r = UI7::Slider<T>::New(label, data, pIO, min, max, precision);
|
||||
r->SetID(id);
|
||||
}
|
||||
pLayout->AddObject(r);
|
||||
}
|
||||
void SameLine() { pLayout->SameLine(); }
|
||||
void Separator();
|
||||
void SeparatorText(const std::string &label);
|
||||
|
||||
@@ -5,6 +5,7 @@ project(pd-lithium LANGUAGES CXX VERSION 0.6.0)
|
||||
option(PD_LI_INCLUDE_FONTS "Include Fonts" OFF)
|
||||
|
||||
set(SRC
|
||||
source/command.cpp
|
||||
source/drawlist.cpp
|
||||
source/font.cpp
|
||||
source/fonts.cpp
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
#include <pd/lithium/command.hpp>
|
||||
|
||||
PD_LITHIUM_API PD::Li::Command::Ref PD::Li::CmdPool::NewCmd() {
|
||||
if (pPoolIdx >= pPool.size()) {
|
||||
Resize(pPool.size() + 128);
|
||||
}
|
||||
Command::Ref nu = pPool[pPoolIdx++];
|
||||
nu->Layer = Layer;
|
||||
nu->Index = pPoolIdx - 1;
|
||||
return nu;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Init(size_t initial_size) {
|
||||
Resize(initial_size);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Deinit() {
|
||||
for (auto it : pPool) {
|
||||
Command::Delete(it);
|
||||
}
|
||||
pPool.clear();
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Resize(size_t nulen) {
|
||||
if (nulen <= pPool.size()) {
|
||||
return; // no idea yet
|
||||
}
|
||||
size_t oldlen = pPool.size();
|
||||
pPool.resize(nulen);
|
||||
for (size_t i = oldlen; i < pPool.size(); i++) {
|
||||
pPool[i] = Command::New();
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Reset() {
|
||||
for (u32 i = 0; i < pPoolIdx; i++) {
|
||||
pPool[i]->Clear();
|
||||
}
|
||||
pPoolIdx = 0;
|
||||
}
|
||||
|
||||
PD::Li::Command::Ref PD::Li::CmdPool::GetCmd(size_t idx) const {
|
||||
return pPool[idx];
|
||||
}
|
||||
PD::Li::Command::Ref PD::Li::CmdPool::GetCmd(size_t idx) { return pPool[idx]; }
|
||||
|
||||
size_t PD::Li::CmdPool::Size() const { return pPoolIdx; }
|
||||
size_t PD::Li::CmdPool::Cap() const { return pPool.size(); }
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Merge(CmdPool& p) {
|
||||
Copy(p);
|
||||
p.Reset();
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Copy(CmdPool& p) {
|
||||
if (pPoolIdx + p.Size() > pPool.size()) {
|
||||
Resize(pPoolIdx + p.Size());
|
||||
}
|
||||
for (size_t i = 0; i < p.Size(); i++) {
|
||||
size_t idx = pPoolIdx++;
|
||||
*pPool[idx] = *p.GetCmd(i);
|
||||
pPool[idx]->Index = idx;
|
||||
pPool[idx]->Layer += Layer;
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Sort() {
|
||||
std::sort(begin(), end(), pTheOrder);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API bool PD::Li::CmdPool::pTheOrder(const Command::Ref& a,
|
||||
const Command::Ref& b) {
|
||||
if (a->Layer == b->Layer) {
|
||||
if (a->Tex == b->Tex) {
|
||||
return a->Index < b->Index;
|
||||
}
|
||||
return a->Tex < b->Tex;
|
||||
}
|
||||
return a->Layer < b->Layer;
|
||||
}
|
||||
@@ -17,6 +17,7 @@ set(SRC
|
||||
source/container/dynobj.cpp
|
||||
source/container/image.cpp
|
||||
source/container/label.cpp
|
||||
source/container/slider.cpp
|
||||
)
|
||||
|
||||
if(PD_BUILD_SHARED)
|
||||
@@ -25,4 +26,4 @@ else()
|
||||
pd_add_lib(pd-ui7 SRC_FILES ${SRC})
|
||||
endif()
|
||||
|
||||
target_link_libraries(pd-ui7 PUBLIC pd-lithium pd-core)
|
||||
target_link_libraries(pd-ui7 PUBLIC pd-lithium pd-core)
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/label.hpp>
|
||||
#include <pd/ui7/container/slider.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
// Setup Supported Datatypes (Probably making this Object
|
||||
// header only to not care about datatype support)
|
||||
template class PD_UI7_API Slider<float>;
|
||||
template class PD_UI7_API Slider<int>;
|
||||
template class PD_UI7_API Slider<double>;
|
||||
template class PD_UI7_API Slider<u8>;
|
||||
template class PD_UI7_API Slider<u16>;
|
||||
template class PD_UI7_API Slider<u32>;
|
||||
template class PD_UI7_API Slider<u64>;
|
||||
template <typename T>
|
||||
PD_UI7_API void Slider<T>::HandleInput() {
|
||||
/// Ensure to only check input once
|
||||
if (inp_done) {
|
||||
return;
|
||||
}
|
||||
// Assert(screen.get(), "Screen is not set up!");
|
||||
std::string p;
|
||||
if constexpr (std::is_floating_point_v<T>) {
|
||||
p = std::format("{:.{}f}", *data, precision);
|
||||
} else {
|
||||
p = std::format("{}", *data);
|
||||
}
|
||||
// Unsafe but is the fastest solution
|
||||
float xps = FinalPos().x;
|
||||
if (io->InputHandler->DragObject(
|
||||
this->GetID(),
|
||||
fvec4(FinalPos() + fvec2(2, 0), fvec2(width, GetSize().y)))) {
|
||||
if (!io->InputHandler->DragReleasedAW) {
|
||||
*data = std::clamp(
|
||||
T(max * (std::clamp(io->InputHandler->DragLastPosition.x - xps, 0.f,
|
||||
width) /
|
||||
width)),
|
||||
this->min, this->max);
|
||||
}
|
||||
}
|
||||
inp_done = true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PD_UI7_API void Slider<T>::Draw() {
|
||||
// Assert(io.get() && list.get(), "Did you run Container::Init correctly?");
|
||||
// io->Ren->OnScreen(screen);
|
||||
std::string p;
|
||||
if constexpr (std::is_floating_point_v<T>) {
|
||||
p = std::format("{:.{}f}", *data, precision);
|
||||
} else {
|
||||
p = std::format("{}", *data);
|
||||
}
|
||||
fvec2 td = io->Font->GetTextBounds(p, io->FontScale);
|
||||
list->PathRect(FinalPos(), FinalPos() + fvec2(width, td.y) + io->FramePadding,
|
||||
io->FrameRounding);
|
||||
list->PathFill(io->Theme->Get(UI7Color_Button));
|
||||
list->PathRect(FinalPos() + 2 + PD::fvec2(slp, 0),
|
||||
FinalPos() + fvec2(slp + slw - 2, td.y - 2) + io->FramePadding,
|
||||
io->FrameRounding);
|
||||
list->PathFill(io->Theme->Get(UI7Color_ButtonActive));
|
||||
list->LayerUp();
|
||||
list->DrawTextEx(FinalPos(), p, io->Theme->Get(UI7Color_Text),
|
||||
LiTextFlags_AlignMid, fvec2(width, td.y) + io->FramePadding);
|
||||
list->LayerDown();
|
||||
list->DrawText(FinalPos() + fvec2(width + io->FramePadding.x * 2.f,
|
||||
io->FramePadding.y * 0.5),
|
||||
label, io->Theme->Get(UI7Color_Text));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PD_UI7_API void Slider<T>::Update() {
|
||||
// Assert(io.get(), "Did you run Container::Init correctly?");
|
||||
// Probably need to find a faster solution (caching sizes calculated here)
|
||||
slw = std::clamp(static_cast<float>(width / max), 3.f, width);
|
||||
slp = static_cast<float>((float)*data / (float)max) * (width - slw);
|
||||
fvec2 tdim = io->Font->GetTextBounds(label, io->FontScale);
|
||||
|
||||
this->SetSize(
|
||||
fvec2(width + tdim.x + io->ItemSpace.x * 2, tdim.y + io->FramePadding.y));
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
+9
-11
@@ -80,6 +80,7 @@ int main() {
|
||||
int wx, wy;
|
||||
glfwGetWindowSize(win, &wx, &wy);
|
||||
PD::Gfx::pGfx->ViewPort = PD::ivec2(wx, wy);
|
||||
ui7->GetIO()->CurrentViewPort = PD::ivec4(0, 0, wx, wy);
|
||||
glViewport(0, 0, wx, wy);
|
||||
glClearColor(0, 0, 0, 0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
@@ -102,15 +103,14 @@ int main() {
|
||||
/** Draw text */
|
||||
List->DrawText(
|
||||
20,
|
||||
"Test String oder So\nPalladium 0.5.0\n" +
|
||||
"Test String oder So\nPalladium 0.6.0\n" +
|
||||
std::format("{}\nMousePos: {}",
|
||||
PD::Strings::FormatNanos(
|
||||
PD::OS::GetTraceRef("REN")->GetLastDiff()),
|
||||
PD::Hid::MousePos()) +
|
||||
"\nUI7 Version: " + PD::UI7::GetVersion(),
|
||||
0xff000000);
|
||||
if (ui7->BeginMenu("Test")) {
|
||||
auto menu = ui7->pCurrent;
|
||||
if (auto menu = ui7->BeginMenu("Test")) {
|
||||
menu->Label("Hello");
|
||||
menu->Label("World!");
|
||||
menu->Checkbox("About Menu", AboutOderSo);
|
||||
@@ -125,17 +125,16 @@ int main() {
|
||||
menu->Label(
|
||||
std::format("Left: {}", PD::Hid::IsHeld(PD::Hid::Key::Touch)));
|
||||
menu->DragData("Value", &v, 1);
|
||||
menu->Slider("Value 2", &v);
|
||||
ui7->EndMenu();
|
||||
}
|
||||
if (ui7->BeginMenu("Yet another Window")) {
|
||||
auto menu = ui7->pCurrent;
|
||||
if (auto menu = ui7->BeginMenu("Yet another Window")) {
|
||||
menu->Label(std::format("this->Pos: {}", menu->pLayout->GetPosition()));
|
||||
menu->Label(std::format("Vertices: {}", PD::Gfx::pGfx->VertexCounter));
|
||||
menu->Label(std::format("Indices: {}", PD::Gfx::pGfx->IndexCounter));
|
||||
ui7->EndMenu();
|
||||
}
|
||||
if (ui7->BeginMenu("#Debug (UI7)")) {
|
||||
auto menu = ui7->pCurrent;
|
||||
if (auto menu = ui7->BeginMenu("#Debug (UI7)")) {
|
||||
menu->Label(std::format("Framerate: {:.1f} [{:.2f}]", ui7->pIO->Framerate,
|
||||
ui7->pIO->Delta));
|
||||
menu->SeparatorText("Input");
|
||||
@@ -149,8 +148,7 @@ int main() {
|
||||
}
|
||||
ui7->EndMenu();
|
||||
}
|
||||
if (ui7->BeginMenu("NoDebug")) {
|
||||
auto m = ui7->pCurrent;
|
||||
if (auto m = ui7->BeginMenu("NoDebug")) {
|
||||
if (m->BeginTreeNode("Test")) {
|
||||
m->Label("Hello World!");
|
||||
if (m->BeginTreeNode("AnotherNode")) {
|
||||
@@ -176,8 +174,8 @@ int main() {
|
||||
#endif
|
||||
PD::TT::Beg("REN");
|
||||
PD::Gfx::NewFrame();
|
||||
PD::Gfx::RenderDrawData(List->pDrawList);
|
||||
PD::Gfx::RenderDrawData(ui7->GetDrawData()->pDrawList);
|
||||
PD::Gfx::RenderDrawData(List->Data());
|
||||
PD::Gfx::RenderDrawData(ui7->GetDrawData()->Data());
|
||||
/** Clear The List */
|
||||
List->Clear();
|
||||
PD::TT::End("REN");
|
||||
|
||||
Reference in New Issue
Block a user