feat(parameter): add clamp

This commit is contained in:
parker
2025-07-18 04:41:47 +01:00
parent c3ea851e49
commit 9a05c1a8bb
3 changed files with 59 additions and 11 deletions

View File

@@ -26,6 +26,8 @@ namespace enzo
// basic types types
namespace bt
{
using floatT = double;
using intT = int64_t;
using Vector3 = Eigen::Vector3d;
using Vector4 = Eigen::Vector4d;
}

View File

@@ -1,37 +1,73 @@
#include "Gui/Parameters/AbstractSliderParm.h"
#include "Engine/Types.h"
#include <QPainter>
#include <QPaintEvent>
#include <QLabel>
#include <iostream>
#include <qboxlayout.h>
#include <qnamespace.h>
#include <algorithm>
#include <string>
enzo::ui::AbstractSliderParm::AbstractSliderParm(QWidget *parent, Qt::WindowFlags f)
: QWidget(parent, f)
{
setFixedHeight(20);
setFixedHeight(24);
value_ = defaultValue_;
mainLayout_ = new QVBoxLayout();
setLayout(mainLayout_);
valueLabel_ = new QLabel();
valueLabel_->setAlignment(Qt::AlignCenter);
valueLabel_->setStyleSheet("background-color: transparent;");
setStyleSheet("border-radius: 6px;");
mainLayout_->addWidget(valueLabel_);
setValue(value_);
}
void enzo::ui::AbstractSliderParm::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(Qt::black);
painter.setBrush(Qt::red);
painter.setPen(Qt::NoPen);
painter.setBrush(QColor("#383838"));
QRectF fillRect = event->rect();
float fillPercent = value_/maxValue_;
std::cout << "fill percent" << fillPercent << "\n";
fillRect.adjust(0, 0, -fillRect.width()*(1-fillPercent), 0);
painter.drawRoundedRect(fillRect, 8, 8);
float margin = 3;
fillRect.adjust(margin, margin, std::max<float>(-fillRect.width()+margin, -fillRect.width()*(1-fillPercent)-margin), -margin);
painter.drawRoundedRect(fillRect, 6, 6);
}
void enzo::ui::AbstractSliderParm::setValue(bt::floatT value)
{
if(value_==value)
return;
if(clampMin_ && value<minValue_) { value = minValue_; }
if(clampMax_ && value>maxValue_) { value = maxValue_; }
value_ = value;
update();
QString valStr = QString::number(value);
valStr.truncate(4);
valueLabel_->setText(valStr);
}
void enzo::ui::AbstractSliderParm::mouseMoveEvent(QMouseEvent *event)
{
value_ = static_cast<float>(event->pos().x())/rect().width() * maxValue_;
update();
// std::cout << "pos x: " << fillPercent << "\n";
setValue(static_cast<float>(event->pos().x())/rect().width() * maxValue_);
}
void enzo::ui::AbstractSliderParm::mousePressEvent(QMouseEvent *event)
{
setValue(static_cast<float>(event->pos().x())/rect().width() * maxValue_);
}

View File

@@ -1,5 +1,8 @@
#pragma once
#include "Engine/Types.h"
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
namespace enzo::ui
{
@@ -11,14 +14,21 @@ public:
AbstractSliderParm(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
private:
float value_;
void setValue(bt::floatT value);
QVBoxLayout* mainLayout_;
QLabel* valueLabel_;
bt::floatT value_;
float defaultValue_=0;
float minValue_;
float maxValue_=10;
bool clampMin_ = true;
bool clampMax_ = true;
bt::floatT minValue_=-5;
bt::floatT maxValue_=10;
protected:
void paintEvent(QPaintEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
};