mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
refactor
This commit is contained in:
parent
d74b6ca91c
commit
3d0e31b102
11 changed files with 194 additions and 153 deletions
|
|
@ -1,15 +1,12 @@
|
|||
#include "ProgressBar.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <cmath>
|
||||
#include <utility>
|
||||
|
||||
#include "graphics/core/Batch2D.hpp"
|
||||
#include "graphics/core/DrawContext.hpp"
|
||||
#include "graphics/core/Font.hpp"
|
||||
#include "assets/Assets.hpp"
|
||||
#include "maths/voxmaths.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
using namespace gui;
|
||||
|
||||
|
|
@ -17,22 +14,19 @@ ProgressBar::ProgressBar(
|
|||
GUI& gui,
|
||||
double min,
|
||||
double max,
|
||||
double value,
|
||||
int barThickness
|
||||
double value
|
||||
) : UINode(gui, glm::vec2(100, 16)),
|
||||
min(min),
|
||||
max(max),
|
||||
value(value),
|
||||
displayValue(value),
|
||||
barThickness(barThickness)
|
||||
displayValue(value)
|
||||
{
|
||||
if (barThickness <= 0) barThickness = static_cast<int>(size.y);
|
||||
setColor(glm::vec4());
|
||||
setColor(glm::vec4(0.2f, 0.6f, 1.0f, 0.8f));
|
||||
setInteractive(false);
|
||||
}
|
||||
|
||||
void ProgressBar::act(float delta) {
|
||||
if (supplier) value = supplier();
|
||||
UINode::act(delta);
|
||||
if (smoothTransition) {
|
||||
double diff = value - displayValue;
|
||||
if (std::fabs(diff) < 2.0) {
|
||||
|
|
@ -61,12 +55,14 @@ void ProgressBar::draw(const DrawContext& pctx, const Assets& assets) {
|
|||
float t = 0.0f;
|
||||
if (max > min) {
|
||||
t = glm::clamp(
|
||||
static_cast<float>((displayValue - min) / (max - min)), 0.0f, 1.0f
|
||||
static_cast<float>((displayValue - min) / (max - min)),
|
||||
0.0f,
|
||||
1.0f
|
||||
);
|
||||
}
|
||||
|
||||
if (t > 0.0f) {
|
||||
batch->setColor(barColor);
|
||||
batch->setColor(color);
|
||||
if (orientation == Orientation::HORIZONTAL) {
|
||||
batch->rect(pos.x, pos.y, size.x * t, size.y);
|
||||
} else {
|
||||
|
|
@ -74,27 +70,19 @@ void ProgressBar::draw(const DrawContext& pctx, const Assets& assets) {
|
|||
}
|
||||
}
|
||||
|
||||
if (textVisible && !textFormat.empty()) {
|
||||
auto font = assets.get<Font>("normal");
|
||||
if (!text.empty()) {
|
||||
auto font = assets.getShared<Font>(fontName);
|
||||
if (font) {
|
||||
double pct = (max > min) ? (displayValue - min) / (max - min) * 100.0 : 0.0;
|
||||
char buf[64];
|
||||
snprintf(buf, sizeof(buf), textFormat.c_str(), displayValue, pct);
|
||||
auto wtext = util::str2wstr_utf8(buf);
|
||||
int textWidth = font->calcWidth(wtext);
|
||||
int textWidth = font->calcWidth(text);
|
||||
int lineHeight = font->getLineHeight();
|
||||
int tx = static_cast<int>(pos.x + (size.x - textWidth) * 0.5f);
|
||||
int ty = static_cast<int>(pos.y + (size.y - lineHeight) * 0.5f);
|
||||
batch->setColor(textColor);
|
||||
font->draw(*batch, wtext, tx, ty, nullptr, 0);
|
||||
font->draw(*batch, text, tx, ty, nullptr, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProgressBar::setSupplier(doublesupplier supplier) {
|
||||
this->supplier = std::move(supplier);
|
||||
}
|
||||
|
||||
double ProgressBar::getValue() const {
|
||||
return value;
|
||||
}
|
||||
|
|
@ -111,8 +99,8 @@ double ProgressBar::getProgress() const {
|
|||
return (max - min != 0.0) ? (value - min) / (max - min) : 0.0;
|
||||
}
|
||||
|
||||
glm::vec4 ProgressBar::getBarColor() const {
|
||||
return barColor;
|
||||
double ProgressBar::getDisplayValue() const {
|
||||
return displayValue;
|
||||
}
|
||||
|
||||
glm::vec4 ProgressBar::getBgColor() const {
|
||||
|
|
@ -123,14 +111,18 @@ glm::vec4 ProgressBar::getTextColor() const {
|
|||
return textColor;
|
||||
}
|
||||
|
||||
const std::string& ProgressBar::getTextFormat() const {
|
||||
return textFormat;
|
||||
const std::wstring& ProgressBar::getText() const {
|
||||
return text;
|
||||
}
|
||||
|
||||
bool ProgressBar::isSmooth() const {
|
||||
return smoothTransition;
|
||||
}
|
||||
|
||||
float ProgressBar::getSmoothSpeed() const {
|
||||
return smoothSpeed;
|
||||
}
|
||||
|
||||
Orientation ProgressBar::getOrientation() const {
|
||||
return orientation;
|
||||
}
|
||||
|
|
@ -147,14 +139,6 @@ void ProgressBar::setMax(double x) {
|
|||
max = x;
|
||||
}
|
||||
|
||||
void ProgressBar::setBarThickness(int thickness) {
|
||||
barThickness = thickness;
|
||||
}
|
||||
|
||||
void ProgressBar::setBarColor(glm::vec4 color) {
|
||||
barColor = color;
|
||||
}
|
||||
|
||||
void ProgressBar::setBgColor(glm::vec4 color) {
|
||||
bgColor = color;
|
||||
}
|
||||
|
|
@ -163,15 +147,26 @@ void ProgressBar::setTextColor(glm::vec4 color) {
|
|||
textColor = color;
|
||||
}
|
||||
|
||||
void ProgressBar::setTextFormat(const std::string& fmt) {
|
||||
textFormat = fmt;
|
||||
textVisible = !fmt.empty();
|
||||
void ProgressBar::setText(std::wstring text) {
|
||||
this->text = std::move(text);
|
||||
}
|
||||
|
||||
void ProgressBar::setFontName(std::string name) {
|
||||
fontName = std::move(name);
|
||||
}
|
||||
|
||||
const std::string& ProgressBar::getFontName() const {
|
||||
return fontName;
|
||||
}
|
||||
|
||||
void ProgressBar::setSmooth(bool flag) {
|
||||
smoothTransition = flag;
|
||||
}
|
||||
|
||||
void ProgressBar::setSmoothSpeed(float speed) {
|
||||
smoothSpeed = speed;
|
||||
}
|
||||
|
||||
void ProgressBar::setOrientation(Orientation ori) {
|
||||
orientation = ori;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue