| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #include "common/config.h" |
| 22 | #include "Deprecations.h" |
| 23 | #include "Graphics.h" |
| 24 | #include "Font.h" |
| 25 | #include "common/deprecation.h" |
| 26 | #include "timer/Timer.h" |
| 27 | |
| 28 | #include <algorithm> |
| 29 | |
| 30 | namespace love |
| 31 | { |
| 32 | namespace graphics |
| 33 | { |
| 34 | |
| 35 | Deprecations::Deprecations() |
| 36 | : currentDeprecationCount(0) |
| 37 | , lastUpdatedTime(0.0) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | Deprecations::~Deprecations() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void Deprecations::draw(Graphics *gfx) |
| 46 | { |
| 47 | if (!isDeprecationOutputEnabled()) |
| 48 | return; |
| 49 | |
| 50 | GetDeprecated deprecations; |
| 51 | |
| 52 | if (deprecations.all.empty()) |
| 53 | return; |
| 54 | |
| 55 | int total = (int) deprecations.all.size(); |
| 56 | |
| 57 | if (total != currentDeprecationCount) |
| 58 | { |
| 59 | currentDeprecationCount = total; |
| 60 | lastUpdatedTime = timer::Timer::getTime(); |
| 61 | } |
| 62 | |
| 63 | double showTime = 20.0; |
| 64 | double fadeTime = 1.0; |
| 65 | |
| 66 | double delta = timer::Timer::getTime() - lastUpdatedTime; |
| 67 | |
| 68 | float alpha = 1.0f; |
| 69 | if (delta > (showTime - fadeTime)) |
| 70 | alpha = (float) (1.0 - (delta - (showTime - fadeTime)) / fadeTime); |
| 71 | |
| 72 | if (alpha <= 0.0f) |
| 73 | return; |
| 74 | |
| 75 | if (font.get() == nullptr) |
| 76 | { |
| 77 | auto hinting = font::TrueTypeRasterizer::HINTING_NORMAL; |
| 78 | |
| 79 | if (!isGammaCorrect() && gfx->getScreenDPIScale() <= 1.0) |
| 80 | hinting = font::TrueTypeRasterizer::HINTING_LIGHT; |
| 81 | |
| 82 | font.set(gfx->newDefaultFont(9, hinting), Acquire::NORETAIN); |
| 83 | } |
| 84 | |
| 85 | gfx->flushStreamDraws(); |
| 86 | |
| 87 | gfx->push(Graphics::STACK_ALL); |
| 88 | gfx->reset(); |
| 89 | |
| 90 | int maxcount = 4; |
| 91 | int remaining = std::max(0, total - maxcount); |
| 92 | |
| 93 | std::vector<Font::ColoredString> strings; |
| 94 | Colorf white(1, 1, 1, 1); |
| 95 | |
| 96 | // Grab the newest deprecation notices first. |
| 97 | for (int i = total - 1; i >= remaining; --i) |
| 98 | { |
| 99 | if (!strings.empty()) |
| 100 | strings.back().str += "\n" ; |
| 101 | |
| 102 | const DeprecationInfo *info = deprecations.all[i]; |
| 103 | strings.push_back({getDeprecationNotice(*info, true), white}); |
| 104 | } |
| 105 | |
| 106 | if (remaining > 0) |
| 107 | strings.push_back({"\n(And " + std::to_string(remaining) + " more)" , white}); |
| 108 | |
| 109 | int padding = 5; |
| 110 | int width = 600; |
| 111 | |
| 112 | for (const auto &coloredstr : strings) |
| 113 | width = std::max(width, font->getWidth(coloredstr.str) + padding * 2); |
| 114 | |
| 115 | float wraplimit = std::min(gfx->getWidth(), width - padding * 2); |
| 116 | |
| 117 | std::vector<std::string> wrappedlines; |
| 118 | font->getWrap(strings, wraplimit, wrappedlines); |
| 119 | |
| 120 | int linecount = std::min((int) wrappedlines.size(), maxcount); |
| 121 | int height = font->getHeight() * linecount + padding * 2; |
| 122 | |
| 123 | int x = 0; |
| 124 | int y = std::max(gfx->getHeight() - height, 0); |
| 125 | |
| 126 | gfx->setColor(Colorf(0, 0, 0, 0.85 * alpha)); |
| 127 | gfx->rectangle(Graphics::DRAW_FILL, x, y, width, height); |
| 128 | |
| 129 | gfx->setColor(Colorf(1, 0.9, 0.8, 1 * alpha)); |
| 130 | gfx->setScissor({x, y, width, height}); |
| 131 | |
| 132 | Matrix4 textm(x + padding, y + padding, 0, 1, 1, 0, 0, 0, 0); |
| 133 | gfx->printf(strings, font.get(), wraplimit, Font::ALIGN_LEFT, textm); |
| 134 | |
| 135 | gfx->pop(); |
| 136 | } |
| 137 | |
| 138 | } // graphics |
| 139 | } // love |
| 140 | |