From 7f270188082e5c49b42f3b11bf8ebf0c54ed8a76 Mon Sep 17 00:00:00 2001 From: zetaPRIME Date: Sat, 18 Mar 2017 19:29:32 -0400 Subject: [PATCH] UIContainer::AddNew, Add uses std::move, Optional type, button styles and glyphs, lower memory footprint for default text style on Label --- .../source/starlight/datatypes/Optional.h | 63 ++++++++++++++++++ libstarlight/source/starlight/dialog/OSK.cpp | 6 +- .../source/starlight/gfx/DrawableImage.cpp | 2 +- libstarlight/source/starlight/ui/Button.cpp | 15 +++-- libstarlight/source/starlight/ui/Button.h | 17 ++++- libstarlight/source/starlight/ui/Label.cpp | 17 +++-- libstarlight/source/starlight/ui/Label.h | 6 +- .../source/starlight/ui/UIContainer.cpp | 4 +- .../source/starlight/ui/UIContainer.h | 5 ++ libstarlight/todo.txt | 25 +++++-- testbed/source/Core.cpp | 14 ++-- themes/default/glyphs/backspace.large.png | Bin 0 -> 309 bytes themes/default/glyphs/backspace.small.png | Bin 0 -> 295 bytes themes/default/glyphs/backspace.xcf | Bin 0 -> 3906 bytes themes/default/glyphs/enter.large.png | Bin 0 -> 267 bytes themes/default/glyphs/enter.small.png | Bin 0 -> 237 bytes themes/default/glyphs/enter.xcf | Bin 0 -> 3046 bytes 17 files changed, 146 insertions(+), 28 deletions(-) create mode 100644 libstarlight/source/starlight/datatypes/Optional.h create mode 100644 themes/default/glyphs/backspace.large.png create mode 100644 themes/default/glyphs/backspace.small.png create mode 100644 themes/default/glyphs/backspace.xcf create mode 100644 themes/default/glyphs/enter.large.png create mode 100644 themes/default/glyphs/enter.small.png create mode 100644 themes/default/glyphs/enter.xcf diff --git a/libstarlight/source/starlight/datatypes/Optional.h b/libstarlight/source/starlight/datatypes/Optional.h new file mode 100644 index 0000000..00c921c --- /dev/null +++ b/libstarlight/source/starlight/datatypes/Optional.h @@ -0,0 +1,63 @@ +#pragma once +#include "starlight/_global.h" + +#include + +namespace starlight { + template + class Optional { + private: + std::unique_ptr p = nullptr; + std::function* getdef = nullptr; + + inline void initp() { + if (!p) { + p = std::make_unique(); + if (getdef) *p = (*getdef)(); + } + } + + public: + Optional() = default; + Optional(std::function* getDefault) : getdef(getDefault) { } + Optional(nullptr_t) : p(nullptr) { } + Optional(const Optional& o) { // copy operator *actually copies the inner object* + if (o.p) { + p = std::make_unique(); + *p = *o.p; + } + getdef = o.getdef; + } + + Optional& operator=(const nullptr_t&) { p.reset(); } + Optional& operator=(const T& o) { // assign by type's assignment operator if passed a "value" + if (!p) p = std::make_unique(); + *p = o; + return *this; + } + + T& operator *() { + initp(); + return *p; + } + + T* operator ->() { + initp(); + return &*p; + } + + inline T& Get(T& defaultRef) { + if (p) return *p; + return defaultRef; + } + + inline T& ROGet() { + if (p) return *p; + if (getdef) return (*getdef)(); + static T fb; return fb; // meh, hackish but you shouldn't do this without a getdef anyway + // todo: clean this up somehow ^ (throw instead? or maybe have a static unique_ptr instead so we save memory when this is never called for a type) + } + + // + }; +} diff --git a/libstarlight/source/starlight/dialog/OSK.cpp b/libstarlight/source/starlight/dialog/OSK.cpp index bf1ccdf..e8234fa 100644 --- a/libstarlight/source/starlight/dialog/OSK.cpp +++ b/libstarlight/source/starlight/dialog/OSK.cpp @@ -96,7 +96,8 @@ OSK::OSK(osk::InputHandler* handler) : Form(true), handler(handler) { bpen = bpstart + bs * Vector2(linestart[3] + 10, 3); auto key = std::make_shared