renderer: restrict maximum dot amount w.r.t to input width (#56)

This commit is contained in:
Maurice Kraus 2024-02-24 15:14:10 +01:00 committed by GitHub
parent 57e8000660
commit 0ad65916b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -137,21 +137,21 @@ bool CPasswordInputField::draw(const SRenderData& data) {
const int PASS_SIZE = std::nearbyint(inputFieldBox.h * dt_size * 0.5f) * 2.f; const int PASS_SIZE = std::nearbyint(inputFieldBox.h * dt_size * 0.5f) * 2.f;
const int PASS_SPACING = std::floor(PASS_SIZE * dt_space); const int PASS_SPACING = std::floor(PASS_SIZE * dt_space);
const int DOT_AREA_WIDTH = inputFieldBox.w - PASS_SPACING * 2; // avail width for dots
const int MAX_DOTS = DOT_AREA_WIDTH / (PASS_SIZE + PASS_SPACING); // max amount of dots that can fit in the area
// Calculate the total width required for all dots including spaces between them
const int TOTAL_DOTS_WIDTH = (PASS_SIZE + PASS_SPACING) * dots.currentAmount - PASS_SPACING;
if (!hiddenInputState.enabled) { if (!hiddenInputState.enabled) {
int xback = dots.center ? (PASS_SIZE + PASS_SPACING) * std::ceil(dots.currentAmount) / 2.0 - inputFieldBox.w / 2.0 + PASS_SPACING * 2 : 0; // Calculate starting x-position to ensure dots stay centered within the input field
int xstart = dots.center ? (DOT_AREA_WIDTH - TOTAL_DOTS_WIDTH) / 2 : PASS_SPACING;
for (size_t i = 0; i < std::floor(dots.currentAmount); ++i) { // Clamp xstart to be no less than PASS_SPACING to avoid drawing outside
Vector2D currentPos = inputFieldBox.pos() + Vector2D{PASS_SPACING * 2 - xback, inputFieldBox.h / 2.f - PASS_SIZE / 2.f} + Vector2D{(PASS_SIZE + PASS_SPACING) * i, 0}; xstart = std::max(xstart, PASS_SPACING);
CBox box{currentPos, Vector2D{PASS_SIZE, PASS_SIZE}};
g_pRenderer->renderRect(box, fontCol, PASS_SIZE / 2.0);
}
if (dots.currentAmount != std::floor(dots.currentAmount)) { for (size_t i = 0; i < dots.currentAmount && i < MAX_DOTS; ++i) {
Vector2D currentPos = inputFieldBox.pos() + Vector2D{PASS_SPACING * 2 - xback, inputFieldBox.h / 2.f - PASS_SIZE / 2.f} + Vector2D dotPosition = inputFieldBox.pos() + Vector2D{xstart + i * (PASS_SIZE + PASS_SPACING), inputFieldBox.h / 2.f - PASS_SIZE / 2.f};
Vector2D{(PASS_SIZE + PASS_SPACING) * std::floor(dots.currentAmount), 0}; CBox box{dotPosition, Vector2D{PASS_SIZE, PASS_SIZE}};
CBox box{currentPos, Vector2D{PASS_SIZE, PASS_SIZE}};
fontCol.a *= (dots.currentAmount - std::floor(dots.currentAmount)) * data.opacity;
g_pRenderer->renderRect(box, fontCol, PASS_SIZE / 2.0); g_pRenderer->renderRect(box, fontCol, PASS_SIZE / 2.0);
} }
} }