From fdda6244d329652cca64ea9c9dca7dda6579d417 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Tue, 28 Jun 2016 19:38:14 +0200 Subject: [PATCH] Optimize toasts --- deps/mongoose | 2 +- deps/protobuf | 2 +- src/Components/Modules/Toast.cpp | 59 +++++++++++++++----------------- src/Utils/Utils.cpp | 6 ++++ src/Utils/Utils.hpp | 1 + 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/deps/mongoose b/deps/mongoose index 4d65cb1c..a478dc59 160000 --- a/deps/mongoose +++ b/deps/mongoose @@ -1 +1 @@ -Subproject commit 4d65cb1c0ff6c8e50127afd6d9e2a88fe5f30972 +Subproject commit a478dc59354deb19d01a9e9138bcfac76a0347e3 diff --git a/deps/protobuf b/deps/protobuf index dc0aeaa9..e0016c5b 160000 --- a/deps/protobuf +++ b/deps/protobuf @@ -1 +1 @@ -Subproject commit dc0aeaa9030bdac264b44d56d07b6839a1ae94e9 +Subproject commit e0016c5b6ae61ad60d260a78c5834cc537b46d10 diff --git a/src/Components/Modules/Toast.cpp b/src/Components/Modules/Toast.cpp index 1ede6d84..fdeecd9c 100644 --- a/src/Components/Modules/Toast.cpp +++ b/src/Components/Modules/Toast.cpp @@ -23,19 +23,22 @@ namespace Components int duration = toast->Length; int startTime = toast->Start; - int aCorners = 0; // Adjust the corners. They seem to have a 1px border + int border = 1; int cornerSize = 15; int bHeight = 74; - int bWidth = 200; int imgDim = 60; - Game::Material* circle = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MATERIAL, "circle").material; + float fontSize = 0.9f; + float descSize = 0.9f; + Game::Material* white = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MATERIAL, "white").material; Game::Material* image = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MATERIAL, toast->Image.data()).material; - Game::Font* font = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_FONT, "fonts/normalFont").font; - Game::vec4_t color = { 0, 0, 0, 0.7f }; + Game::Font* font = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_FONT, "fonts/objectiveFont").font; + Game::Font* descfont = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_FONT, "fonts/normalFont").font; Game::vec4_t wColor = { 1.0f, 1.0f, 1.0f, 1.0f }; + Game::vec4_t bgColor = { 0.0f, 0.0f, 0.0f, 0.5f }; + Game::vec4_t borderColor = { 1.0f, 1.0f, 1.0f, 0.2f }; height /= 5; height *= 4; @@ -48,7 +51,7 @@ namespace Components int diffH = Renderer::Height() / 5; int diff = Game::Com_Milliseconds() - startTime; double scale = 1.0 - ((1.0 * diff) / (1.0 * slideTime)); - diffH = static_cast(diffH* scale); + diffH = static_cast(diffH * scale); height += diffH; } @@ -58,48 +61,42 @@ namespace Components int diffH = Renderer::Height() / 5; int diff = (startTime + duration) - Game::Com_Milliseconds(); double scale = 1.0 - ((1.0 * diff) / (1.0 * slideTime)); - diffH = static_cast(diffH* scale); + diffH = static_cast(diffH * scale); height += diffH; } - height += bHeight / 2 + aCorners - cornerSize; + height += bHeight / 2 - cornerSize; // Calculate width data - int iOffset = (bHeight - imgDim) / 2 + aCorners; + int iOffset = (bHeight - imgDim) / 2; int iOffsetLeft = iOffset * 2; - int titleSize = Game::R_TextWidth(toast->Title.data(), 0x7FFFFFFF, font); - int descrSize = Game::R_TextWidth(toast->Desc.data(), 0x7FFFFFFF, font); - bWidth = iOffsetLeft * 3 + imgDim + std::max(titleSize, descrSize); + float titleSize = Game::R_TextWidth(toast->Title.data(), 0x7FFFFFFF, font) * fontSize; + float descrSize = Game::R_TextWidth(toast->Desc.data(), 0x7FFFFFFF, descfont) * descSize; + float bWidth = iOffsetLeft * 3 + imgDim + std::max(titleSize, descrSize); // Make stuff divisible by 2 // Otherwise there are overlapping images // and I'm too lazy to figure out the actual problem :P - bWidth += (bWidth % 2); + bWidth = (static_cast(bWidth) + (static_cast(bWidth) % 2)) * 1.0f; bHeight += (bHeight % 2); - // Corners - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2), static_cast(height - bHeight / 2), cornerSize * 1.0f, cornerSize * 1.0f, 0, 0, 0.5f, 0.5f, color, circle); // Top-Left - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 + bWidth / 2 - cornerSize), static_cast(height - bHeight / 2), cornerSize * 1.0f, cornerSize * 1.0f, 0.5f, 0, 0, 0.5f, color, circle); // Top-Right - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2), static_cast(height + bHeight / 2 - cornerSize), cornerSize * 1.0f, cornerSize * 1.0f, 0, 0.5f, 0.5f, 0, color, circle); // Bottom-Left - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 + bWidth / 2 - cornerSize), static_cast(height + bHeight / 2 - cornerSize), cornerSize * 1.0f, cornerSize * 1.0f, 0.5f, 0.5f, 0, 0, color, circle); // Bottom-Right + // Background + Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2), static_cast(height - bHeight / 2), bWidth * 1.0f, bHeight * 1.0f, 0, 0, 1.0f, 1.0f, bgColor, white); // Border - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2 + cornerSize), static_cast(height - bHeight / 2 + aCorners), static_cast(bWidth - cornerSize * 2), static_cast(cornerSize - aCorners), 0, 0, 1.0f, 1.0f, color, white); // Top - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2 + cornerSize), static_cast(height + bHeight / 2 - cornerSize), static_cast(bWidth - cornerSize * 2), static_cast(cornerSize - aCorners), 0, 0, 1.0f, 1.0f, color, white); // Bottom - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2 + aCorners), static_cast(height - bHeight / 2 + cornerSize), static_cast(cornerSize - aCorners), static_cast(bHeight - cornerSize * 2), 0, 0, 1.0f, 1.0f, color, white); // Left - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 + bWidth / 2 - cornerSize), static_cast(height - bHeight / 2 + cornerSize), static_cast(cornerSize - aCorners), static_cast(bHeight - cornerSize * 2), 0, 0, 1.0f, 1.0f, color, white); // Right + Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2 - border), static_cast(height - bHeight / 2 - border), border * 1.0f, bHeight + (border * 2.0f), 0, 0, 1.0f, 1.0f, borderColor, white); // Left + Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2 + bWidth), static_cast(height - bHeight / 2 - border), border * 1.0f, bHeight + (border * 2.0f), 0, 0, 1.0f, 1.0f, borderColor, white); // Right + Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2), static_cast(height - bHeight / 2 - border), bWidth * 1.0f, border * 1.0f, 0, 0, 1.0f, 1.0f, borderColor, white); // Top + Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2), static_cast(height + bHeight / 2), bWidth * 1.0f, border * 1.0f, 0, 0, 1.0f, 1.0f, borderColor, white); // Bottom - // Center - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - (bWidth - cornerSize * 2) / 2), static_cast(height - (bHeight - cornerSize * 2) / 2), static_cast(bWidth - cornerSize * 2), static_cast(bHeight - cornerSize * 2), 0, 0, 1.0f, 1.0f, color, white); - - // Image - Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2 + iOffsetLeft), static_cast(height - bHeight / 2 + iOffset), imgDim * 1.0f, imgDim * 1.0f, 0, 0, 1.0f, 1.0f, wColor, image); + // Image + Game::CL_DrawStretchPicPhysical(static_cast(width / 2 - bWidth / 2 + iOffsetLeft), static_cast(height - bHeight / 2 + iOffset), imgDim * 1.0f, imgDim * 1.0f, 0, 0, 1.0f, 1.0f, wColor, image); // Text - int leftText = width / 2 - bWidth / 2 - cornerSize + iOffsetLeft * 2 + imgDim; - int rightText = width / 2 + bWidth / 2 - cornerSize - aCorners - iOffsetLeft; - Game::R_AddCmdDrawText(toast->Title.data(), 0x7FFFFFFF, font, static_cast(leftText + (rightText - leftText) / 2 - titleSize / 2 + cornerSize), static_cast(height - bHeight / 2 + cornerSize * 2 + 7), 1.0f, 1.0f, 0, wColor, 0); // Title - Game::R_AddCmdDrawText(toast->Desc.data(), 0x7FFFFFFF, font, static_cast(leftText + (rightText - leftText) / 2 - descrSize / 2 + cornerSize), static_cast(height - bHeight / 2 + cornerSize * 2 + 33), 1.0f, 1.0f, 0, wColor, 0); // Description + float leftText = width / 2 - bWidth / 2 - cornerSize + iOffsetLeft * 2 + imgDim; + float rightText = width / 2 + bWidth / 2 - cornerSize - iOffsetLeft; + Game::R_AddCmdDrawText(Utils::StrToUpper(toast->Title).data(), 0x7FFFFFFF, font, static_cast(leftText + (rightText - leftText) / 2 - titleSize / 2 + cornerSize), static_cast(height - bHeight / 2 + cornerSize * 2 + 7), fontSize, fontSize, 0, wColor, Game::ITEM_TEXTSTYLE_SHADOWED); // Title + Game::R_AddCmdDrawText(toast->Desc.data(), 0x7FFFFFFF, descfont, leftText + (rightText - leftText) / 2 - descrSize / 2 + cornerSize, static_cast(height - bHeight / 2 + cornerSize * 2 + 33), descSize, descSize, 0, wColor, Game::ITEM_TEXTSTYLE_SHADOWED); // Description } void Toast::Handler() diff --git a/src/Utils/Utils.cpp b/src/Utils/Utils.cpp index 1ff08653..e65e991d 100644 --- a/src/Utils/Utils.cpp +++ b/src/Utils/Utils.cpp @@ -39,6 +39,12 @@ namespace Utils return input; } + std::string StrToUpper(std::string input) + { + std::transform(input.begin(), input.end(), input.begin(), ::toupper); + return input; + } + bool EndsWith(std::string haystack, std::string needle) { return (strstr(haystack.data(), needle.data()) == (haystack.data() + haystack.size() - needle.size())); diff --git a/src/Utils/Utils.hpp b/src/Utils/Utils.hpp index dd78d04a..b1662d2b 100644 --- a/src/Utils/Utils.hpp +++ b/src/Utils/Utils.hpp @@ -5,6 +5,7 @@ namespace Utils std::string GetMimeType(std::string url); const char *VA(const char *fmt, ...); std::string StrToLower(std::string input); + std::string StrToUpper(std::string input); bool EndsWith(std::string haystack, std::string needle); std::vector Explode(const std::string& str, char delim); void Replace(std::string &string, std::string find, std::string replace);