This commit is contained in:
2026-03-03 21:14:39 +03:00
parent bf8e3bad6f
commit ff28dbb2f5
9 changed files with 376 additions and 58 deletions
+177 -1
View File
@@ -1,3 +1,4 @@
#pragma execution_character_set("utf-8")
#include "UI.hpp"
#include <iostream>
@@ -86,7 +87,25 @@ void UI::draw(sf::RenderWindow& window) {
return;
}
float leftWidth = 350.0f;
float maxWidth = 0.0f;
sf::FloatRect algoBounds = algorithmText_.getLocalBounds();
sf::FloatRect stateBounds = stateText_.getLocalBounds();
sf::FloatRect timeBounds = timeComplexityText_.getLocalBounds();
sf::FloatRect spaceBounds = spaceComplexityText_.getLocalBounds();
sf::FloatRect compBounds = comparisonsText_.getLocalBounds();
sf::FloatRect swapBounds = swapsText_.getLocalBounds();
sf::FloatRect speedBounds = speedText_.getLocalBounds();
maxWidth = std::max(maxWidth, algoBounds.width);
maxWidth = std::max(maxWidth, stateBounds.width);
maxWidth = std::max(maxWidth, timeBounds.width);
maxWidth = std::max(maxWidth, spaceBounds.width);
maxWidth = std::max(maxWidth, compBounds.width);
maxWidth = std::max(maxWidth, swapBounds.width);
maxWidth = std::max(maxWidth, speedBounds.width);
float leftWidth = maxWidth + 24.0f;
float leftHeight = 215.0f;
leftBackground_.setSize(sf::Vector2f(leftWidth, leftHeight));
leftBackground_.setPosition(5.0f, 5.0f);
@@ -113,3 +132,160 @@ void UI::draw(sf::RenderWindow& window) {
window.draw(speedText_);
window.draw(controlsText_);
}
void UI::drawInfoOverlay(sf::RenderWindow& window) {
if (!fontLoaded_) {
return;
}
auto toSfStr = [](const char* utf8) -> sf::String {
std::string s(utf8);
return sf::String::fromUtf8(s.begin(), s.end());
};
float windowWidth = static_cast<float>(window.getSize().x);
float windowHeight = static_cast<float>(window.getSize().y);
sf::RectangleShape overlay(sf::Vector2f(windowWidth, windowHeight));
overlay.setPosition(0.0f, 0.0f);
overlay.setFillColor(sf::Color(8, 10, 18, 230));
window.draw(overlay);
sf::Text title;
title.setFont(font_);
title.setCharacterSize(36);
title.setFillColor(sf::Color::White);
title.setString(toSfStr(u8"SortLab — визуализатор алгоритмов сортировки"));
sf::Text subtitle;
subtitle.setFont(font_);
subtitle.setCharacterSize(18);
subtitle.setFillColor(sf::Color(160, 160, 160));
subtitle.setString(toSfStr(u8"Учебный инструмент для наглядного изучения алгоритмов сортировки"));
sf::Text section1;
section1.setFont(font_);
section1.setCharacterSize(20);
section1.setFillColor(sf::Color(0, 220, 255));
section1.setString(toSfStr(u8"О программе"));
sf::Text body1;
body1.setFont(font_);
body1.setCharacterSize(16);
body1.setFillColor(sf::Color(200, 200, 200));
body1.setString(toSfStr(u8"SortLab визуализирует работу алгоритмов сортировки в реальном времени.\nКаждый столбик — это элемент массива. Высота = значение элемента.\nЦвета: серый — обычный, голубой — сравнение, оранжевый — перестановка, зелёный — готово."));
sf::Text section2;
section2.setFont(font_);
section2.setCharacterSize(20);
section2.setFillColor(sf::Color(0, 220, 255));
section2.setString(toSfStr(u8"Алгоритмы и сложность"));
sf::Text body2;
body2.setFont(font_);
body2.setCharacterSize(16);
body2.setFillColor(sf::Color(200, 200, 200));
body2.setString(toSfStr(u8"[1] Bubble Sort Время: O(n²) / O(n²) / O(n²) Память: O(1)\n[2] Selection Sort Время: O(n²) / O(n²) / O(n²) Память: O(1)\n[3] Insertion Sort Время: O(n) / O(n²) / O(n²) Память: O(1)\n[4] Merge Sort Время: O(n log n) / O(n log n) / ... Память: O(n)\n[5] Quick Sort Время: O(n log n) / O(n log n) / ... Память: O(log n)"));
sf::Text section3;
section3.setFont(font_);
section3.setCharacterSize(20);
section3.setFillColor(sf::Color(0, 220, 255));
section3.setString(toSfStr(u8"Δ Гистограмма (внизу слева)"));
sf::Text body3;
body3.setFont(font_);
body3.setCharacterSize(16);
body3.setFillColor(sf::Color(200, 200, 200));
body3.setString(toSfStr(u8"Показывает количество сравнений на каждом шаге сортировки.\nВысокий пик = много сравнений за один шаг.\nBubble Sort даёт ровную линию, Quick Sort — резкие пики в начале."));
sf::Text section4;
section4.setFont(font_);
section4.setCharacterSize(20);
section4.setFillColor(sf::Color(0, 220, 255));
section4.setString(toSfStr(u8"Карта прогресса (внизу справа)"));
sf::Text body4;
body4.setFont(font_);
body4.setCharacterSize(16);
body4.setFillColor(sf::Color(200, 200, 200));
body4.setString(toSfStr(u8"Миниатюрная копия всего массива в виде сетки цветных квадратиков.\nПозволяет видеть глобальный прогресс сортировки даже при большом массиве.\nЗелёные квадраты = отсортированные элементы."));
sf::Text footer;
footer.setFont(font_);
footer.setCharacterSize(15);
footer.setFillColor(sf::Color(100, 100, 100));
footer.setString(toSfStr(u8"Нажми [I] чтобы закрыть | [Q] выход | [Space] старт/пауза | [R] перемешать"));
float totalHeight = 0.0f;
totalHeight += title.getLocalBounds().height + 10.0f;
totalHeight += subtitle.getLocalBounds().height + 30.0f;
totalHeight += section1.getLocalBounds().height + 6.0f;
totalHeight += body1.getLocalBounds().height + 24.0f;
totalHeight += section2.getLocalBounds().height + 6.0f;
totalHeight += body2.getLocalBounds().height + 24.0f;
totalHeight += section3.getLocalBounds().height + 6.0f;
totalHeight += body3.getLocalBounds().height + 24.0f;
totalHeight += section4.getLocalBounds().height + 6.0f;
totalHeight += body4.getLocalBounds().height + 30.0f;
totalHeight += footer.getLocalBounds().height;
float startY = (windowHeight - totalHeight) * 0.5f;
if (startY < 20.0f) startY = 20.0f;
float currentY = startY;
sf::FloatRect titleBounds = title.getLocalBounds();
title.setPosition((windowWidth - titleBounds.width) * 0.5f, currentY);
window.draw(title);
currentY += titleBounds.height + 10.0f;
sf::FloatRect subtitleBounds = subtitle.getLocalBounds();
subtitle.setPosition((windowWidth - subtitleBounds.width) * 0.5f, currentY);
window.draw(subtitle);
currentY += subtitleBounds.height + 30.0f;
sf::FloatRect section1Bounds = section1.getLocalBounds();
section1.setPosition((windowWidth - section1Bounds.width) * 0.5f, currentY);
window.draw(section1);
currentY += section1Bounds.height + 6.0f;
sf::FloatRect body1Bounds = body1.getLocalBounds();
body1.setPosition((windowWidth - body1Bounds.width) * 0.5f, currentY);
window.draw(body1);
currentY += body1Bounds.height + 24.0f;
sf::FloatRect section2Bounds = section2.getLocalBounds();
section2.setPosition((windowWidth - section2Bounds.width) * 0.5f, currentY);
window.draw(section2);
currentY += section2Bounds.height + 6.0f;
sf::FloatRect body2Bounds = body2.getLocalBounds();
body2.setPosition((windowWidth - body2Bounds.width) * 0.5f, currentY);
window.draw(body2);
currentY += body2Bounds.height + 24.0f;
sf::FloatRect section3Bounds = section3.getLocalBounds();
section3.setPosition((windowWidth - section3Bounds.width) * 0.5f, currentY);
window.draw(section3);
currentY += section3Bounds.height + 6.0f;
sf::FloatRect body3Bounds = body3.getLocalBounds();
body3.setPosition((windowWidth - body3Bounds.width) * 0.5f, currentY);
window.draw(body3);
currentY += body3Bounds.height + 24.0f;
sf::FloatRect section4Bounds = section4.getLocalBounds();
section4.setPosition((windowWidth - section4Bounds.width) * 0.5f, currentY);
window.draw(section4);
currentY += section4Bounds.height + 6.0f;
sf::FloatRect body4Bounds = body4.getLocalBounds();
body4.setPosition((windowWidth - body4Bounds.width) * 0.5f, currentY);
window.draw(body4);
currentY += body4Bounds.height + 30.0f;
sf::FloatRect footerBounds = footer.getLocalBounds();
footer.setPosition((windowWidth - footerBounds.width) * 0.5f, currentY);
window.draw(footer);
}