2012-01-16 21:29:21 +00:00
|
|
|
#include "dialognoise.h"
|
|
|
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QImage>
|
2012-01-18 16:20:14 +00:00
|
|
|
#include <QLabel>
|
2012-01-16 21:29:21 +00:00
|
|
|
#include <QColor>
|
|
|
|
#include <QPainter>
|
2012-01-19 16:03:34 +00:00
|
|
|
#include <QSlider>
|
2012-01-16 21:29:21 +00:00
|
|
|
#include <QScrollArea>
|
2012-01-19 16:03:34 +00:00
|
|
|
#include <QPushButton>
|
2012-08-27 20:40:35 +00:00
|
|
|
#include <math.h>
|
2012-08-30 20:28:55 +00:00
|
|
|
#include <qt4/QtGui/qwidget.h>
|
2012-01-16 21:29:21 +00:00
|
|
|
|
2012-01-18 16:20:14 +00:00
|
|
|
/**************** Previews ****************/
|
2012-02-21 13:41:02 +00:00
|
|
|
class PreviewLevel:public BasePreview
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-02-21 13:41:02 +00:00
|
|
|
PreviewLevel(QWidget* parent, NoiseGenerator* noise): BasePreview(parent)
|
2012-01-20 16:25:09 +00:00
|
|
|
{
|
2012-01-25 22:32:06 +00:00
|
|
|
_noise_original = noise;
|
|
|
|
_noise_preview = noiseCreateGenerator();
|
2012-01-20 16:25:09 +00:00
|
|
|
_level = -1;
|
2012-04-12 20:02:31 +00:00
|
|
|
|
2012-08-26 20:10:06 +00:00
|
|
|
configScaling(0.15, 6.0, 0.09, 6.0);
|
2012-01-20 16:25:09 +00:00
|
|
|
}
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
void setLevel(int row)
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-01-20 16:25:09 +00:00
|
|
|
_level = row;
|
|
|
|
redraw();
|
2012-01-18 16:20:14 +00:00
|
|
|
}
|
|
|
|
protected:
|
2012-01-25 22:32:06 +00:00
|
|
|
void updateData()
|
|
|
|
{
|
|
|
|
noiseCopy(_noise_original, _noise_preview);
|
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
QColor getColor(double x, double y)
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
if ((_level >= 0) && (-y > noiseGet1DLevel(_noise_preview, _level, x)))
|
2012-01-20 16:25:09 +00:00
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
return Qt::black;
|
2012-01-20 16:25:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
return Qt::white;
|
2012-01-20 16:25:09 +00:00
|
|
|
}
|
2012-01-18 16:20:14 +00:00
|
|
|
}
|
|
|
|
private:
|
2012-01-25 22:32:06 +00:00
|
|
|
NoiseGenerator* _noise_original;
|
|
|
|
NoiseGenerator* _noise_preview;
|
2012-01-20 16:25:09 +00:00
|
|
|
int _level;
|
2012-01-18 16:20:14 +00:00
|
|
|
};
|
|
|
|
|
2012-02-21 13:41:02 +00:00
|
|
|
class PreviewTotal:public BasePreview
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-02-21 13:41:02 +00:00
|
|
|
PreviewTotal(QWidget* parent, NoiseGenerator* noise): BasePreview(parent)
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-01-25 22:32:06 +00:00
|
|
|
_noise_original = noise;
|
|
|
|
_noise_preview = noiseCreateGenerator();
|
2012-04-12 20:02:31 +00:00
|
|
|
|
2012-08-26 20:10:06 +00:00
|
|
|
configScaling(0.15, 6.0, 0.09, 6.0);
|
2012-01-18 16:20:14 +00:00
|
|
|
}
|
|
|
|
protected:
|
2012-01-25 22:32:06 +00:00
|
|
|
void updateData()
|
|
|
|
{
|
|
|
|
noiseCopy(_noise_original, _noise_preview);
|
|
|
|
}
|
2012-06-17 09:40:40 +00:00
|
|
|
QColor getColor(double x, double y)
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
if (-y > noiseGet1DTotal(_noise_preview, x))
|
2012-01-18 16:20:14 +00:00
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
return Qt::black;
|
2012-01-18 16:20:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
return Qt::white;
|
2012-01-18 16:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
2012-01-25 22:32:06 +00:00
|
|
|
NoiseGenerator* _noise_original;
|
|
|
|
NoiseGenerator* _noise_preview;
|
2012-01-18 16:20:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**************** Dialog form ****************/
|
2012-01-16 21:29:21 +00:00
|
|
|
DialogNoise::DialogNoise(QWidget *parent, NoiseGenerator* value):
|
2012-06-02 14:17:01 +00:00
|
|
|
DialogWithPreview(parent)
|
2012-01-16 21:29:21 +00:00
|
|
|
{
|
2012-08-27 20:40:35 +00:00
|
|
|
QWidget* function;
|
2012-01-19 16:03:34 +00:00
|
|
|
QWidget* previews;
|
|
|
|
QWidget* form;
|
|
|
|
QWidget* buttons;
|
|
|
|
QPushButton* button;
|
2012-04-14 15:52:16 +00:00
|
|
|
QLabel* label;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-16 21:29:21 +00:00
|
|
|
_base = value;
|
|
|
|
_current = noiseCreateGenerator();
|
|
|
|
|
2012-01-19 16:03:34 +00:00
|
|
|
setLayout(new QHBoxLayout());
|
|
|
|
|
|
|
|
previews = new QWidget(this);
|
|
|
|
previews->setLayout(new QVBoxLayout());
|
|
|
|
layout()->addWidget(previews);
|
2012-04-14 15:52:16 +00:00
|
|
|
layout()->setAlignment(previews, Qt::AlignTop);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-19 16:03:34 +00:00
|
|
|
previewLevel = new PreviewLevel(previews, _current);
|
2012-04-14 15:52:16 +00:00
|
|
|
label = new QLabel(tr("Level preview"));
|
|
|
|
label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
|
|
previews->layout()->addWidget(label);
|
2012-01-19 16:03:34 +00:00
|
|
|
previews->layout()->addWidget(previewLevel);
|
2012-04-14 15:52:16 +00:00
|
|
|
|
2012-01-19 16:03:34 +00:00
|
|
|
previewTotal = new PreviewTotal(previews, _current);
|
2012-04-14 15:52:16 +00:00
|
|
|
label = new QLabel(tr("Total preview"));
|
|
|
|
label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
|
|
|
|
previews->layout()->addWidget(label);
|
2012-01-19 16:03:34 +00:00
|
|
|
previews->layout()->addWidget(previewTotal);
|
|
|
|
|
|
|
|
form = new QWidget(this);
|
|
|
|
form->setLayout(new QVBoxLayout());
|
|
|
|
layout()->addWidget(form);
|
2012-08-27 20:40:35 +00:00
|
|
|
|
|
|
|
function = new QWidget(form);
|
|
|
|
function->setLayout(new QHBoxLayout());
|
|
|
|
form->layout()->addWidget(function);
|
|
|
|
|
|
|
|
function->layout()->addWidget(new QLabel(tr("Noise function")));
|
|
|
|
function_algo = new QComboBox(function);
|
2012-08-30 20:28:55 +00:00
|
|
|
function_algo->addItems(QStringList(tr("Simplex (best)")) << tr("Perlin (fast)") << tr("Naive (slow)"));
|
2012-08-27 20:40:35 +00:00
|
|
|
function->layout()->addWidget(function_algo);
|
|
|
|
function->layout()->addWidget(new QLabel(tr("Ridge factor")));
|
|
|
|
function_ridge = new QSlider(Qt::Horizontal, function);
|
|
|
|
function_ridge->setRange(-10, 10);
|
|
|
|
function_ridge->setTickInterval(10);
|
|
|
|
function_ridge->setTickPosition(QSlider::TicksBelow);
|
2012-08-30 20:28:55 +00:00
|
|
|
function_ridge->setMinimumWidth(150);
|
2012-08-27 20:40:35 +00:00
|
|
|
function->layout()->addWidget(function_ridge);
|
|
|
|
QObject::connect(function_algo, SIGNAL(currentIndexChanged(int)), this, SLOT(functionChanged()));
|
|
|
|
QObject::connect(function_ridge, SIGNAL(valueChanged(int)), this, SLOT(functionChanged()));
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-02-28 13:45:11 +00:00
|
|
|
form->layout()->addWidget(new QLabel(tr("Noise components")));
|
2012-01-19 16:03:34 +00:00
|
|
|
levels = new QListWidget(form);
|
|
|
|
form->layout()->addWidget(levels);
|
2012-01-20 16:25:09 +00:00
|
|
|
QObject::connect(levels, SIGNAL(currentRowChanged(int)), this, SLOT(levelChanged(int)));
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
buttons = new QWidget(form);
|
|
|
|
buttons->setLayout(new QHBoxLayout());
|
|
|
|
form->layout()->addWidget(buttons);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-02-28 13:45:11 +00:00
|
|
|
button = new QPushButton(tr("Add component"), buttons);
|
2012-01-20 16:25:09 +00:00
|
|
|
buttons->layout()->addWidget(button);
|
|
|
|
QObject::connect(button, SIGNAL(clicked()), this, SLOT(addLevel()));
|
|
|
|
|
2012-02-28 13:45:11 +00:00
|
|
|
button = new QPushButton(tr("Remove component"), buttons);
|
2012-01-20 16:25:09 +00:00
|
|
|
buttons->layout()->addWidget(button);
|
|
|
|
QObject::connect(button, SIGNAL(clicked()), this, SLOT(removeLevel()));
|
|
|
|
|
2012-02-28 13:45:11 +00:00
|
|
|
form->layout()->addWidget(new QLabel(tr("Component height")));
|
2012-01-20 16:25:09 +00:00
|
|
|
slider_height = new QSlider(form);
|
|
|
|
slider_height->setOrientation(Qt::Horizontal);
|
|
|
|
slider_height->setMinimumWidth(150);
|
2012-04-14 15:52:16 +00:00
|
|
|
//slider_height->setMaximumWidth(400);
|
2012-01-20 16:25:09 +00:00
|
|
|
slider_height->setMinimum(0);
|
|
|
|
slider_height->setMaximum(1000);
|
|
|
|
slider_height->setTickInterval(100);
|
|
|
|
slider_height->setTickPosition(QSlider::TicksBelow);
|
|
|
|
form->layout()->addWidget(slider_height);
|
|
|
|
QObject::connect(slider_height, SIGNAL(valueChanged(int)), this, SLOT(heightChanged(int)));
|
|
|
|
|
2012-02-28 13:45:11 +00:00
|
|
|
form->layout()->addWidget(new QLabel(tr("Component scaling")));
|
2012-01-20 16:25:09 +00:00
|
|
|
slider_scaling = new QSlider(form);
|
|
|
|
slider_scaling->setOrientation(Qt::Horizontal);
|
|
|
|
slider_scaling->setMinimumWidth(150);
|
2012-04-14 15:52:16 +00:00
|
|
|
//slider_scaling->setMaximumWidth(400);
|
2012-01-20 16:25:09 +00:00
|
|
|
slider_scaling->setMinimum(1);
|
|
|
|
slider_scaling->setMaximum(1000);
|
|
|
|
slider_scaling->setTickInterval(100);
|
|
|
|
slider_scaling->setTickPosition(QSlider::TicksBelow);
|
|
|
|
form->layout()->addWidget(slider_scaling);
|
|
|
|
QObject::connect(slider_scaling, SIGNAL(valueChanged(int)), this, SLOT(scalingChanged(int)));
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-19 16:03:34 +00:00
|
|
|
buttons = new QWidget(form);
|
|
|
|
buttons->setLayout(new QHBoxLayout());
|
|
|
|
form->layout()->addWidget(buttons);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-08-13 19:19:19 +00:00
|
|
|
button = new QPushButton(tr("Cancel"), buttons);
|
|
|
|
button->setIcon(QIcon("images/cancel.png"));
|
2012-01-19 16:03:34 +00:00
|
|
|
buttons->layout()->addWidget(button);
|
2012-08-13 19:19:19 +00:00
|
|
|
QObject::connect(button, SIGNAL(clicked()), this, SLOT(reject()));
|
2012-01-19 16:03:34 +00:00
|
|
|
|
2012-08-13 19:19:19 +00:00
|
|
|
button = new QPushButton(tr("Revert"), buttons);
|
|
|
|
button->setIcon(QIcon("images/revert.png"));
|
2012-01-19 16:03:34 +00:00
|
|
|
buttons->layout()->addWidget(button);
|
2012-01-20 16:25:09 +00:00
|
|
|
QObject::connect(button, SIGNAL(clicked()), this, SLOT(revert()));
|
2012-01-18 16:20:14 +00:00
|
|
|
|
2012-08-13 19:19:19 +00:00
|
|
|
button = new QPushButton(tr("Validate"), buttons);
|
|
|
|
button->setIcon(QIcon("images/apply.png"));
|
2012-01-19 16:03:34 +00:00
|
|
|
buttons->layout()->addWidget(button);
|
2012-08-13 19:19:19 +00:00
|
|
|
QObject::connect(button, SIGNAL(clicked()), this, SLOT(accept()));
|
2012-01-16 21:29:21 +00:00
|
|
|
|
2012-02-28 13:45:11 +00:00
|
|
|
setWindowTitle(tr("Paysages 3D - Noise editor"));
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
revert();
|
2012-01-16 21:29:21 +00:00
|
|
|
}
|
|
|
|
|
2012-01-18 16:20:14 +00:00
|
|
|
DialogNoise::~DialogNoise()
|
|
|
|
{
|
|
|
|
delete previewLevel;
|
|
|
|
delete previewTotal;
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-18 16:20:14 +00:00
|
|
|
noiseDeleteGenerator(_current);
|
|
|
|
}
|
|
|
|
|
2012-01-16 21:29:21 +00:00
|
|
|
bool DialogNoise::getNoise(QWidget* parent, NoiseGenerator* noise)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
DialogNoise* dialog = new DialogNoise(parent, noise);
|
|
|
|
result = dialog->exec();
|
|
|
|
|
|
|
|
delete dialog;
|
|
|
|
|
|
|
|
return (result != 0) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogNoise::closeEvent(QCloseEvent* e)
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogNoise::accept()
|
|
|
|
{
|
|
|
|
noiseCopy(_current, _base);
|
2012-01-18 16:20:14 +00:00
|
|
|
QDialog::accept();
|
2012-01-16 21:29:21 +00:00
|
|
|
}
|
2012-01-20 16:25:09 +00:00
|
|
|
|
|
|
|
void DialogNoise::revert()
|
|
|
|
{
|
|
|
|
noiseCopy(_base, _current);
|
|
|
|
|
|
|
|
revertToCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogNoise::revertToCurrent()
|
|
|
|
{
|
|
|
|
int i, n;
|
2012-04-14 15:52:16 +00:00
|
|
|
int selected;
|
2012-08-27 20:40:35 +00:00
|
|
|
NoiseFunction function;
|
2012-04-14 15:52:16 +00:00
|
|
|
|
|
|
|
selected = levels->currentRow();
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
levels->clear();
|
|
|
|
n = noiseGetLevelCount(_current);
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2012-02-28 13:45:11 +00:00
|
|
|
levels->addItem(QString(tr("Component %1")).arg(i + 1));
|
2012-01-20 16:25:09 +00:00
|
|
|
}
|
2012-04-14 15:52:16 +00:00
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
if (selected < 0)
|
|
|
|
{
|
|
|
|
selected = 0;
|
|
|
|
}
|
|
|
|
if (selected >= n)
|
|
|
|
{
|
|
|
|
selected = n - 1;
|
|
|
|
}
|
|
|
|
levels->setCurrentRow(selected);
|
|
|
|
}
|
2012-08-27 20:40:35 +00:00
|
|
|
|
|
|
|
function = noiseGetFunction(_current);
|
|
|
|
function_algo->setCurrentIndex((int)function.algorithm);
|
|
|
|
function_ridge->setValue(round(function.ridge_factor * 20.0));
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
previewLevel->redraw();
|
|
|
|
previewTotal->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogNoise::addLevel()
|
|
|
|
{
|
|
|
|
NoiseLevel level;
|
2012-02-06 20:54:23 +00:00
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
level.height = 0.1;
|
|
|
|
level.scaling = 0.1;
|
|
|
|
level.xoffset = 0.0;
|
|
|
|
level.yoffset = 0.0;
|
|
|
|
level.zoffset = 0.0;
|
|
|
|
noiseAddLevel(_current, level);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
revertToCurrent();
|
2012-02-06 20:54:23 +00:00
|
|
|
|
|
|
|
levels->setCurrentRow(levels->count() - 1);
|
2012-01-20 16:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DialogNoise::removeLevel()
|
|
|
|
{
|
2012-02-06 20:54:23 +00:00
|
|
|
int row;
|
|
|
|
|
|
|
|
row = levels->currentRow();
|
|
|
|
|
2012-01-22 18:39:42 +00:00
|
|
|
noiseRemoveLevel(_current, _current_level);
|
|
|
|
|
|
|
|
revertToCurrent();
|
2012-02-06 20:54:23 +00:00
|
|
|
|
|
|
|
if (row >= levels->count())
|
|
|
|
{
|
|
|
|
row--;
|
|
|
|
}
|
|
|
|
levels->setCurrentRow(row);
|
2012-01-20 16:25:09 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 20:40:35 +00:00
|
|
|
void DialogNoise::functionChanged()
|
|
|
|
{
|
|
|
|
NoiseFunction function;
|
|
|
|
|
|
|
|
function.algorithm = (NoiseFunctionAlgorithm)function_algo->currentIndex();
|
|
|
|
function.ridge_factor = (double)function_ridge->value() * 0.05;
|
|
|
|
|
|
|
|
noiseSetFunction(_current, &function);
|
|
|
|
noiseValidate(_current);
|
|
|
|
|
|
|
|
previewLevel->redraw();
|
|
|
|
previewTotal->redraw();
|
|
|
|
}
|
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
void DialogNoise::levelChanged(int row)
|
|
|
|
{
|
|
|
|
if (noiseGetLevel(_current, row, &_current_level_params))
|
|
|
|
{
|
|
|
|
_current_level = row;
|
|
|
|
((PreviewLevel*)previewLevel)->setLevel(row);
|
2012-01-22 18:39:42 +00:00
|
|
|
|
2012-01-20 16:25:09 +00:00
|
|
|
slider_height->setValue(_current_level_params.height * 1000.0);
|
|
|
|
slider_scaling->setValue(_current_level_params.scaling * 1000.0);
|
|
|
|
}
|
|
|
|
// TODO else ...
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogNoise::heightChanged(int value)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
_current_level_params.height = ((double)value) / 1000.0;
|
2012-01-20 16:25:09 +00:00
|
|
|
noiseSetLevel(_current, _current_level, _current_level_params);
|
|
|
|
previewLevel->redraw();
|
|
|
|
previewTotal->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogNoise::scalingChanged(int value)
|
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
_current_level_params.scaling = ((double)value) / 1000.0;
|
2012-01-20 16:25:09 +00:00
|
|
|
noiseSetLevel(_current, _current_level, _current_level_params);
|
|
|
|
previewLevel->redraw();
|
|
|
|
previewTotal->redraw();
|
|
|
|
}
|