diff --git a/samples/main.cpp b/samples/main.cpp index bfb60bc..5fd507b 100644 --- a/samples/main.cpp +++ b/samples/main.cpp @@ -31,7 +31,7 @@ static constexpr Id RootPanelId = 1; static constexpr Id NextPanelId = 100; -int quit(uint32_t, void *instance) { +int quit(Id, void *instance) { if (instance == nullptr) { return ErrorCode; } @@ -46,7 +46,7 @@ static uint32_t LastTick = 0; static uint32_t Diff = 0; static constexpr uint32_t TimeDiff = 10; -int updateProgressbar(uint32_t, void *instance) { +int updateProgressbar(Id, void *instance) { if (instance == nullptr) { return ErrorCode; } diff --git a/src/tinyui.cpp b/src/tinyui.cpp index ff25acc..3720e55 100644 --- a/src/tinyui.cpp +++ b/src/tinyui.cpp @@ -41,7 +41,7 @@ static Style DefaultStyle { { "Arial.ttf", 35, nullptr } }; -static const char *SeverityToken[] = { +static constexpr char const *SeverityToken[] = { "", "*TRACE*", "*DEBUG*", @@ -57,7 +57,7 @@ void log_message(LogSeverity severity, const char *message) { Context *gCtx = nullptr; -Context *Context::create(const char *title, Style &style) { +Context *Context::create(const char *title, const Style &style) { Context *ctx = new Context; ctx->mLogger = log_message; ctx->mAppTitle = title; @@ -71,7 +71,7 @@ void Context::destroy(Context *ctx) { delete ctx; } -bool TinyUi::createContext(const char *title, Style &style) { +bool TinyUi::createContext(const char *title, const Style &style) { if (gCtx != nullptr) { return false; } diff --git a/src/tinyui.h b/src/tinyui.h index 44b8643..d6a30b2 100644 --- a/src/tinyui.h +++ b/src/tinyui.h @@ -74,6 +74,9 @@ struct Widget; // Type declarations ---------------------------------------------------------- +/// @brief This enum is used to describe the alignment of a widget. +using Id = uint64_t; + /// @brief The return code type used in the ui library. using ret_code = int32_t; @@ -319,7 +322,7 @@ struct EventPayload { /// @brief This interface is used to store all neede message handlers. struct CallbackI { /// The function callback - typedef int (*funcCallback) (uint32_t id, void *data); + typedef int (*funcCallback) (Id id, void *data); /// The function callback array, not handled callbacks are marked as a nullptr. funcCallback mfuncCallback[Events::NumEvents]; /// The data instance. @@ -336,7 +339,7 @@ struct CallbackI { /// @param callbackFunc The callback function. /// @param instance The instance to use. /// @param eventType The event type to use. - CallbackI(funcCallback callbackFunc, void *instance, size_t eventType = Events::MouseButtonDownEvent) : + CallbackI(funcCallback callbackFunc, void *instance, size_t eventType = Events::MouseButtonDownEvent) : mfuncCallback{ nullptr }, mInstance(instance) { clear(); mfuncCallback[eventType] = callbackFunc; @@ -411,7 +414,7 @@ struct Context { /// @param title The title of the context. /// @param style The style to use. /// @return The created context. - static Context *create(const char *title, Style &style); + static Context *create(const char *title, const Style &style); /// @brief Will destroy a valid tinyui context. /// @param ctx The context to destroy. @@ -431,7 +434,7 @@ struct TinyUi { /// @param title The app title. /// @param style The style to use. /// @return true if successful. - static bool createContext(const char *title, Style &style); + static bool createContext(const char *title, const Style &style); /// @brief Will destroy the context. /// @return true if successful. diff --git a/src/widgets.cpp b/src/widgets.cpp index 535ae63..802cabb 100644 --- a/src/widgets.cpp +++ b/src/widgets.cpp @@ -82,8 +82,7 @@ static Image *loadIntoImageCache(Context &ctx, const char *filename) { static void releaseImageCache(Context &ctx) { for (auto it = ctx.mImageCache.begin(); it != ctx.mImageCache.end(); ++it) { - Image *image = it->second; - if (image != nullptr) { + if (Image *image = it->second; image != nullptr) { Renderer::releaseSurfaceImpl(image->mSurfaceImpl); delete image; } @@ -125,10 +124,10 @@ static Widget *createWidget(Context &ctx, Id id, Id parentId, const Rect &rect, if (widget != nullptr) { if (widget->mType == type) { return widget; - } else { - ctx.mLogger(LogSeverity::Error, "A widget with the same id but different type already exists."); - return nullptr; - } + } + + ctx.mLogger(LogSeverity::Error, "A widget with the same id but different type already exists."); + return nullptr; } widget = new Widget; @@ -344,7 +343,7 @@ ret_code Widgets::imageBox(Id id, Id parentId, const char* image, const Rect& re if (ctx.mRoot == nullptr) { return InvalidRenderHandle; } - Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::Box); + Widget *child = createWidget(ctx, id, parentId, rect, WidgetType::ImageBox); if (child == nullptr) { return ErrorCode; } @@ -371,7 +370,7 @@ ret_code Widgets::panel(Id id, Id parentId, const char *title, const Rect &rect, return ResultOk; } -static int onTreeViewItemClicked(uint32_t id, void *data) { +static int onTreeViewItemClicked(Id id, void *data) { Widget *treeView = Widgets::findWidget(id, TinyUi::getContext().mRoot); if (treeView == nullptr) { return ErrorCode; @@ -437,7 +436,8 @@ ret_code Widgets::treeItem(Id id, Id parentItemId, const char *text) { const int32_t w = parentRect.width; const int32_t h = parentRect.height; size_t numChildren = parentWidget->mChildren.size() + 1; - const Rect rect(parentRect.top.x + margin, parentRect.top.y + numChildren * margin + numChildren * h, w, h); + const Rect rect(parentRect.top.x + margin, parentRect.top.y + numChildren * margin + + static_cast(numChildren) * h, w, h); Widget *child = createWidget(ctx, id, parentItemId, rect, WidgetType::Label); if (child == nullptr) { return ErrorCode; @@ -568,6 +568,15 @@ static void render(Context &ctx, Widget *currentWidget) { Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, currentWidget->mFilledRect, ctx.mStyle.mBorder); } break; + + case WidgetType::ImageBox: + { + Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, currentWidget->mFilledRect, ctx.mStyle.mBorder); + if (currentWidget->mImage != nullptr) { + Renderer::drawImage(ctx, r.top.x, r.top.y, r.width, r.height, currentWidget->mImage); + } + } + break; } for (auto &child : currentWidget->mChildren) { diff --git a/src/widgets.h b/src/widgets.h index 4e897a1..08e224f 100644 --- a/src/widgets.h +++ b/src/widgets.h @@ -36,10 +36,11 @@ enum class WidgetType { Container = 0, ///< A container widget Button, ///< A button widget Label, ///< A label widget - InputField, ///< + InputField, ///< An input field widget Panel, ///< A panel widget Box, ///< A box widget - TreeView, ///< A treeeview widget + ImageBox, ///< An image box widget + TreeView, ///< A treeview widget ProgressBar, ///< A status bar widget Count ///< The number of widgets }; @@ -56,10 +57,7 @@ struct FilledState { uint32_t filledState{0}; ///< The filled state in percent (0-100) }; -/// @brief This enum is used to descripe the alignment of a widget. -using Id = uint64_t; - -/// @brief This enum is used to descripe the alignment of a widget. +/// @brief This enum is used to describe the alignment of a widget. enum class WidgetStyle { Invalid = -1, BorderStyle, ///< The widget has a border