From 6738fda55c1e46f97753d28ea909f98260492d63 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Sun, 16 Mar 2025 20:03:08 +0100 Subject: [PATCH] # 0.3.1-1 - Add Deltatime usage for Overscroll as well as a config value for ids multiplier - Add Function to Layout to remove all ID Objects - Add step and precision to DragData as well as setting min and max to their type limits - Use the Address for now for the id of the DragData (cause with tree nodes opened backwars all DragData will share the same data reference) - Add a fix to MaxPosition in Layout to be actually the max Position on X axis --- include/pd/ui7/container/dragdata.hpp | 17 ++++++++++++++--- include/pd/ui7/io.hpp | 3 ++- include/pd/ui7/layout.hpp | 1 + include/pd/ui7/menu.hpp | 9 ++++++--- include/pd/ui7/ui7.hpp | 2 +- source/ui7/container/dragdata.cpp | 26 +++++++++++++++++++------- source/ui7/layout.cpp | 2 +- source/ui7/menu.cpp | 4 ++-- source/ui7/theme.cpp | 4 ++-- source/ui7/ui7.cpp | 7 +++++-- 10 files changed, 53 insertions(+), 22 deletions(-) diff --git a/include/pd/ui7/container/dragdata.hpp b/include/pd/ui7/container/dragdata.hpp index f953681..180f09b 100644 --- a/include/pd/ui7/container/dragdata.hpp +++ b/include/pd/ui7/container/dragdata.hpp @@ -38,17 +38,26 @@ class DragData : public Container { /** * Constructor * @param label Label of the Button - * @param pos Base Position - * @param lr Reference to the Renderer + * @param data Data reference (Supported types can be seen in dragdata.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 */ DragData(const std::string& label, T* data, size_t num_elms, UI7::IO::Ref io, - T min = 0, T max = 100) { + T min = std::numeric_limits::min(), + T max = std::numeric_limits::max(), T step = 1, + int precision = 1) { PD::Assert(data != nullptr, "Input Data Address is null!"); this->label = label; this->data = data; this->elm_count = num_elms; this->min = min; this->max = max; + this->step = step; + this->precision = precision; this->tdim = io->Ren->GetTextDimensions(label); } ~DragData() = default; @@ -74,6 +83,8 @@ class DragData : public Container { size_t elm_count = 0; T min; T max; + T step; + int precision = 1; }; } // namespace UI7 } // namespace PD \ No newline at end of file diff --git a/include/pd/ui7/io.hpp b/include/pd/ui7/io.hpp index e7227c9..9e9a129 100644 --- a/include/pd/ui7/io.hpp +++ b/include/pd/ui7/io.hpp @@ -71,7 +71,8 @@ class IO : public SmartCtor { vec2 FramePadding = 5.f; vec2 ItemSpace = vec2(5.f, 2.f); vec2 MinSliderDragSize = 10.f; // Min height (Vt) and Min Width (Hz) - u64 DoubleClickTime = 500; // Milliseconds + float OverScrollMod = 0.15f; + u64 DoubleClickTime = 500; // Milliseconds std::vector> DrawListRegestry; DrawList::Ref Back; DrawList::Ref Front; diff --git a/include/pd/ui7/layout.hpp b/include/pd/ui7/layout.hpp index ca2c538..5bf95fc 100644 --- a/include/pd/ui7/layout.hpp +++ b/include/pd/ui7/layout.hpp @@ -63,6 +63,7 @@ class Layout : public PD::SmartCtor { void AddObject(Container::Ref obj); Container::Ref FindObject(u32 id); + void ClearIDObjects() { IDObjects.clear(); } vec2 AlignPosition(vec2 pos, vec2 size, vec4 area, UI7Align alignment); diff --git a/include/pd/ui7/menu.hpp b/include/pd/ui7/menu.hpp index 1c0489f..4b9db0b 100644 --- a/include/pd/ui7/menu.hpp +++ b/include/pd/ui7/menu.hpp @@ -92,11 +92,14 @@ class Menu : public SmartCtor { void DragFloat(const std::string& label, float* data, size_t num_elms); template void DragData(const std::string& label, T* data, size_t num_elms = 1, - T min = 0, T max = 100) { - u32 id = Strings::FastHash("dfl" + label + std::to_string(count_btn++)); + T min = std::numeric_limits::min(), + T max = std::numeric_limits::max(), T step = 1, + int precision = 1) { + u32 id = Strings::FastHash("drd" + label + std::to_string((u32)data)); Container::Ref r = Layout->FindObject(id); if (!r) { - r = PD::New>(label, data, num_elms, io, min, max); + r = PD::New>(label, data, num_elms, io, min, max, step, + precision); r->SetID(id); } Layout->AddObject(r); diff --git a/include/pd/ui7/ui7.hpp b/include/pd/ui7/ui7.hpp index 4d88d87..f1510e7 100644 --- a/include/pd/ui7/ui7.hpp +++ b/include/pd/ui7/ui7.hpp @@ -37,7 +37,7 @@ SOFTWARE. * Major Minor Patch Build * 0x01010000 -> 1.1.0-0 */ -#define UI7_VERSION 0x00030100 +#define UI7_VERSION 0x00030101 namespace PD { namespace UI7 { diff --git a/source/ui7/container/dragdata.cpp b/source/ui7/container/dragdata.cpp index 5d1afe4..6dce8a1 100644 --- a/source/ui7/container/dragdata.cpp +++ b/source/ui7/container/dragdata.cpp @@ -48,7 +48,7 @@ void DragData::HandleInput() { for (size_t i = 0; i < elm_count; i++) { std::string p; if constexpr (std::is_floating_point_v) { - p = std::format("{:.1f}", data[i]); + p = std::format("{:.{}f}", data[i], precision); } else { p = std::format("{}", data[i]); } @@ -57,9 +57,9 @@ void DragData::HandleInput() { if (io->DragObject( this->GetID() + i + 1, vec4(FinalPos() + vec2(off_x, 0), tdim + io->FramePadding))) { - data[i] = std::clamp( - T(data[i] + (io->DragPosition[0] - io->DragLastPosition[0])), - this->min, this->max); + data[i] = std::clamp(T(data[i] + (step * (io->DragPosition[0] - + io->DragLastPosition[0]))), + this->min, this->max); } off_x += tdim.x() + io->ItemSpace.x() + io->FramePadding.x(); } @@ -75,7 +75,7 @@ void DragData::Draw() { for (size_t i = 0; i < elm_count; i++) { std::string p; if constexpr (std::is_floating_point_v) { - p = std::format("{:.1f}", data[i]); + p = std::format("{:.{}f}", data[i], precision); } else { p = std::format("{}", data[i]); } @@ -88,14 +88,26 @@ void DragData::Draw() { list->Layer(list->Layer() - 1); off_x += td.x() + io->ItemSpace.x() + io->FramePadding.x(); } - list->AddText(FinalPos() + vec2(off_x, 0), label, + list->AddText(FinalPos() + vec2(off_x, io->FramePadding.y() * 0.5), label, io->Theme->Get(UI7Color_Text)); } template void DragData::Update() { Assert(io.get(), "Did you run Container::Init correctly?"); - this->SetSize(vec2(tdim.x() + io->ItemSpace.x() + 20, 20)); + // Probably need to find a faster solution (caching sizes calculated here) + float off_x = 0; + for (size_t i = 0; i < elm_count; i++) { + std::string p; + if constexpr (std::is_floating_point_v) { + p = std::format("{:.{}f}", data[i], precision); + } else { + p = std::format("{}", data[i]); + } + vec2 tdim = io->Ren->GetTextDimensions(p); + off_x += tdim.x() + io->ItemSpace.x() + io->FramePadding.x(); + } + this->SetSize(vec2(tdim.x() + off_x, tdim.y() + io->FramePadding.y())); } } // namespace UI7 } // namespace PD \ No newline at end of file diff --git a/source/ui7/layout.cpp b/source/ui7/layout.cpp index a3ab0c3..eab58a4 100644 --- a/source/ui7/layout.cpp +++ b/source/ui7/layout.cpp @@ -44,7 +44,7 @@ void Layout::CursorMove(const vec2& size) { Cursor[1] + size[1] + IO->ItemSpace[1]); } // Logical Issue here as x should use a max check - MaxPosition = vec2(SamelineCursor[0], Cursor[1]); + MaxPosition = vec2(std::max(MaxPosition[0], SamelineCursor[0]), Cursor[1]); } bool Layout::ObjectWorkPos(vec2& movpos) { diff --git a/source/ui7/menu.cpp b/source/ui7/menu.cpp index e421fb3..8ef2212 100644 --- a/source/ui7/menu.cpp +++ b/source/ui7/menu.cpp @@ -249,7 +249,7 @@ void UI7::Menu::PostHandler() { if (Layout->ScrollOffset[1] > Layout->MaxPosition[1] - Layout->Size.y() && Layout->MaxPosition[1] != 0.f && Layout->MaxPosition[1] >= Layout->Size.y() - io->MenuPadding[1]) { - Layout->ScrollOffset[1] -= 3.f; + Layout->ScrollOffset[1] -= io->OverScrollMod * io->Delta; if (Layout->ScrollOffset[1] < Layout->MaxPosition[1] - Layout->Size.y()) { Layout->ScrollOffset[1] = Layout->MaxPosition[1] - Layout->Size.y(); @@ -258,7 +258,7 @@ void UI7::Menu::PostHandler() { /// Do the Same as above just for Overscroll back to the top if (Layout->ScrollOffset[1] < 0) { - Layout->ScrollOffset[1] += 3.f; + Layout->ScrollOffset[1] += io->OverScrollMod * io->Delta; if (Layout->ScrollOffset[1] > 0) { Layout->ScrollOffset[1] = 0; } diff --git a/source/ui7/theme.cpp b/source/ui7/theme.cpp index cbd1487..82c628f 100644 --- a/source/ui7/theme.cpp +++ b/source/ui7/theme.cpp @@ -29,7 +29,7 @@ namespace UI7 { void Theme::Default(Theme& theme) { theme.Set(UI7Color_Text, Color("#FFFFFFFF")); theme.Set(UI7Color_TextDead, Color("#AAAAAAFF")); - theme.Set(UI7Color_Background, Color("#222222aa")); + theme.Set(UI7Color_Background, Color("#222222ff")); theme.Set(UI7Color_Button, Color("#111111FF")); theme.Set(UI7Color_ButtonDead, Color("#080808FF")); theme.Set(UI7Color_ButtonActive, Color("#2A2A2AFF")); @@ -52,7 +52,7 @@ void Theme::Flashbang(Theme& theme) { theme.Set(UI7Color_Button, Color("#ccccccFF")); theme.Set(UI7Color_ButtonDead, Color("#bbbbbbFF")); theme.Set(UI7Color_ButtonActive, Color("#ccccccFF")); - theme.Set(UI7Color_ButtonHovered, Color("#cbcbcbFF")); + theme.Set(UI7Color_ButtonHovered, Color("#acacacFF")); theme.Set(UI7Color_Header, Color("#ddddddFF")); theme.Set(UI7Color_HeaderDead, Color("#cdcdcdFF")); theme.Set(UI7Color_Selector, Color("#222222FF")); diff --git a/source/ui7/ui7.cpp b/source/ui7/ui7.cpp index 3e8eddb..0f48afd 100644 --- a/source/ui7/ui7.cpp +++ b/source/ui7/ui7.cpp @@ -260,6 +260,8 @@ void UI7::Context::StyleEditor(bool* show) { m->DragData("FramePadding", (float*)&io->FramePadding, 2, 0.f, 100.f); m->DragData("ItemSpace", (float*)&io->ItemSpace, 2, 0.f, 100.f); m->DragData("MinSliderSize", (float*)&io->MinSliderDragSize, 2, 1.f, 100.f); + m->DragData("OverScroll Modifier", &io->OverScrollMod, 1, 0.01f, + std::numeric_limits::max(), 0.01f, 2); m->SeparatorText("Theme"); if (m->Button("Dark")) { UI7::Theme::Default(*io->Theme.get()); @@ -270,8 +272,9 @@ void UI7::Context::StyleEditor(bool* show) { } /// Small trick to print without prefix #define ts(x) m->ColorEdit(std::string(#x).substr(9), &io->Theme->GetRef(x)); -#define ts2(x) \ - m->DragData(std::string(#x).substr(9), (u8*)&io->Theme->GetRef(x), 4, (u8)0, (u8)255); +#define ts2(x) \ + m->DragData(std::string(#x).substr(9), (u8*)&io->Theme->GetRef(x), 4, (u8)0, \ + (u8)255); ts2(UI7Color_Background); ts2(UI7Color_Button); ts2(UI7Color_ButtonDead);