feat(parameter): add clamp
This commit is contained in:
@@ -26,6 +26,8 @@ namespace enzo
|
|||||||
// basic types types
|
// basic types types
|
||||||
namespace bt
|
namespace bt
|
||||||
{
|
{
|
||||||
|
using floatT = double;
|
||||||
|
using intT = int64_t;
|
||||||
using Vector3 = Eigen::Vector3d;
|
using Vector3 = Eigen::Vector3d;
|
||||||
using Vector4 = Eigen::Vector4d;
|
using Vector4 = Eigen::Vector4d;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +1,73 @@
|
|||||||
#include "Gui/Parameters/AbstractSliderParm.h"
|
#include "Gui/Parameters/AbstractSliderParm.h"
|
||||||
|
#include "Engine/Types.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QPaintEvent>
|
#include <QPaintEvent>
|
||||||
|
#include <QLabel>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <qboxlayout.h>
|
||||||
|
#include <qnamespace.h>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
enzo::ui::AbstractSliderParm::AbstractSliderParm(QWidget *parent, Qt::WindowFlags f)
|
enzo::ui::AbstractSliderParm::AbstractSliderParm(QWidget *parent, Qt::WindowFlags f)
|
||||||
: QWidget(parent, f)
|
: QWidget(parent, f)
|
||||||
{
|
{
|
||||||
setFixedHeight(20);
|
setFixedHeight(24);
|
||||||
value_ = defaultValue_;
|
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)
|
void enzo::ui::AbstractSliderParm::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
painter.setRenderHint(QPainter::Antialiasing, true);
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
||||||
painter.setPen(Qt::black);
|
painter.setPen(Qt::NoPen);
|
||||||
painter.setBrush(Qt::red);
|
painter.setBrush(QColor("#383838"));
|
||||||
|
|
||||||
QRectF fillRect = event->rect();
|
QRectF fillRect = event->rect();
|
||||||
float fillPercent = value_/maxValue_;
|
float fillPercent = value_/maxValue_;
|
||||||
std::cout << "fill percent" << fillPercent << "\n";
|
std::cout << "fill percent" << fillPercent << "\n";
|
||||||
fillRect.adjust(0, 0, -fillRect.width()*(1-fillPercent), 0);
|
float margin = 3;
|
||||||
painter.drawRoundedRect(fillRect, 8, 8);
|
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)
|
void enzo::ui::AbstractSliderParm::mouseMoveEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
value_ = static_cast<float>(event->pos().x())/rect().width() * maxValue_;
|
setValue(static_cast<float>(event->pos().x())/rect().width() * maxValue_);
|
||||||
update();
|
}
|
||||||
// std::cout << "pos x: " << fillPercent << "\n";
|
|
||||||
|
void enzo::ui::AbstractSliderParm::mousePressEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
setValue(static_cast<float>(event->pos().x())/rect().width() * maxValue_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Engine/Types.h"
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
#include <QLabel>
|
||||||
|
|
||||||
namespace enzo::ui
|
namespace enzo::ui
|
||||||
{
|
{
|
||||||
@@ -11,14 +14,21 @@ public:
|
|||||||
AbstractSliderParm(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
|
AbstractSliderParm(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float value_;
|
void setValue(bt::floatT value);
|
||||||
|
|
||||||
|
QVBoxLayout* mainLayout_;
|
||||||
|
QLabel* valueLabel_;
|
||||||
|
bt::floatT value_;
|
||||||
float defaultValue_=0;
|
float defaultValue_=0;
|
||||||
float minValue_;
|
bool clampMin_ = true;
|
||||||
float maxValue_=10;
|
bool clampMax_ = true;
|
||||||
|
bt::floatT minValue_=-5;
|
||||||
|
bt::floatT maxValue_=10;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *event) override;
|
void paintEvent(QPaintEvent *event) override;
|
||||||
void mouseMoveEvent(QMouseEvent *event) override;
|
void mouseMoveEvent(QMouseEvent *event) override;
|
||||||
|
void mousePressEvent(QMouseEvent *event) override;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user