From 85f4017bf18d13b3d001280682739bdf3d0730c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Thu, 29 Mar 2012 20:19:50 +0000 Subject: [PATCH] paysages: Color gradation editor (WIP). git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@280 b1fd45b6-86a6-48da-8261-f70d1f35bdcc --- gui_qt/dialogcolorgradation.cpp | 71 ++++++++++++++++++++++++---- gui_qt/dialogcolorgradation.h | 6 +++ gui_qt/widgetcurveeditor.cpp | 84 ++++++++++++++++++++++++++++++++- gui_qt/widgetcurveeditor.h | 7 +++ i18n/paysages_fr.ts | 17 ++++++- lib_paysages/tools.c | 8 ++++ lib_paysages/tools.h | 1 + 7 files changed, 184 insertions(+), 10 deletions(-) diff --git a/gui_qt/dialogcolorgradation.cpp b/gui_qt/dialogcolorgradation.cpp index 08eac79..7b5d965 100644 --- a/gui_qt/dialogcolorgradation.cpp +++ b/gui_qt/dialogcolorgradation.cpp @@ -1,6 +1,7 @@ #include "dialogcolorgradation.h" #include +#include #include #include #include @@ -16,6 +17,8 @@ DialogColorGradation::DialogColorGradation(QWidget *parent, ColorGradation* gradation): QDialog(parent) { + QWidget* buttons; + _base = gradation; _current = colorGradationCreate(); colorGradationCopy(_base, _current); @@ -24,31 +27,44 @@ DialogColorGradation::DialogColorGradation(QWidget *parent, ColorGradation* grad _curve_editor = new WidgetCurveEditor(this); layout()->addWidget(_curve_editor); + connect(_curve_editor, SIGNAL(liveChanged()), this, SLOT(updateColors())); - _preview_red = new PreviewColorGradation(this, gradation, COLORGRADATIONBAND_RED); + _preview_red = new PreviewColorGradation(this, _current, COLORGRADATIONBAND_RED); _preview_red->setMinimumHeight(50); connect(_preview_red, SIGNAL(clicked()), this, SLOT(selectRed())); layout()->addWidget(_preview_red); - _preview_green = new PreviewColorGradation(this, gradation, COLORGRADATIONBAND_GREEN); + _preview_green = new PreviewColorGradation(this, _current, COLORGRADATIONBAND_GREEN); _preview_green->setMinimumHeight(50); connect(_preview_green, SIGNAL(clicked()), this, SLOT(selectGreen())); layout()->addWidget(_preview_green); - _preview_blue = new PreviewColorGradation(this, gradation, COLORGRADATIONBAND_BLUE); + _preview_blue = new PreviewColorGradation(this, _current, COLORGRADATIONBAND_BLUE); _preview_blue->setMinimumHeight(50); connect(_preview_blue, SIGNAL(clicked()), this, SLOT(selectBlue())); layout()->addWidget(_preview_blue); - _preview_final = new PreviewColorGradation(this, gradation, COLORGRADATIONBAND_FINAL); + _preview_final = new PreviewColorGradation(this, _current, COLORGRADATIONBAND_FINAL); _preview_final->setMinimumHeight(50); layout()->addWidget(_preview_final); - _curve = curveCreate(); + buttons = new QWidget(this); + layout()->addWidget(buttons); + buttons->setLayout(new QHBoxLayout()); - /*QObject::connect(button, SIGNAL(clicked()), this, SLOT(accept())); - QObject::connect(button, SIGNAL(clicked()), this, SLOT(revert())); - QObject::connect(button, SIGNAL(clicked()), this, SLOT(reject()));*/ + _button_accept = new QPushButton(tr("Validate"), buttons); + buttons->layout()->addWidget(_button_accept); + QObject::connect(_button_accept, SIGNAL(clicked()), this, SLOT(accept())); + + _button_revert = new QPushButton(tr("Revert"), buttons); + buttons->layout()->addWidget(_button_revert); + QObject::connect(_button_revert, SIGNAL(clicked()), this, SLOT(revert())); + + _button_cancel = new QPushButton(tr("Cancel"), buttons); + buttons->layout()->addWidget(_button_cancel); + QObject::connect(_button_cancel, SIGNAL(clicked()), this, SLOT(reject())); + + _curve = curveCreate(); setWindowTitle(tr("Paysages 3D - Color gradation editor")); @@ -86,6 +102,7 @@ void DialogColorGradation::accept() void DialogColorGradation::revert() { + _selected = 0; colorGradationCopy(_base, _current); revertToCurrent(); } @@ -94,21 +111,59 @@ void DialogColorGradation::selectRed() { colorGradationGetRedCurve(_current, _curve); _curve_editor->setCurve(_curve); + _selected = 1; } void DialogColorGradation::selectGreen() { colorGradationGetGreenCurve(_current, _curve); _curve_editor->setCurve(_curve); + _selected = 2; } void DialogColorGradation::selectBlue() { colorGradationGetBlueCurve(_current, _curve); _curve_editor->setCurve(_curve); + _selected = 3; +} + +void DialogColorGradation::updateColors() +{ + Curve* curve; + + curve = curveCreate(); + _curve_editor->getCurve(curve); + + switch (_selected) + { + case 0: + _preview_red->update(); + _preview_green->update(); + _preview_blue->update(); + break; + case 1: + colorGradationSetRedCurve(_current, curve); + _preview_red->update(); + break; + case 2: + colorGradationSetGreenCurve(_current, curve); + _preview_green->update(); + break; + case 3: + colorGradationSetBlueCurve(_current, curve); + _preview_blue->update(); + break; + default: + ; + } + _preview_final->update(); + + curveDelete(curve); } void DialogColorGradation::revertToCurrent() { + updateColors(); selectRed(); } diff --git a/gui_qt/dialogcolorgradation.h b/gui_qt/dialogcolorgradation.h index 2e445fb..dd9cb0e 100644 --- a/gui_qt/dialogcolorgradation.h +++ b/gui_qt/dialogcolorgradation.h @@ -2,6 +2,7 @@ #define _PAYSAGES_QT_DIALOGCOLORGRADATION_H_ #include +#include #include "baseform.h" #include "widgetcurveeditor.h" #include "previewcolorgradation.h" @@ -24,6 +25,7 @@ public slots: void selectRed(); void selectGreen(); void selectBlue(); + void updateColors(); protected: virtual void closeEvent(QCloseEvent* e); @@ -40,6 +42,10 @@ private: PreviewColorGradation* _preview_green; PreviewColorGradation* _preview_blue; PreviewColorGradation* _preview_final; + int _selected; + QPushButton* _button_accept; + QPushButton* _button_revert; + QPushButton* _button_cancel; }; #endif diff --git a/gui_qt/widgetcurveeditor.cpp b/gui_qt/widgetcurveeditor.cpp index 3ae9582..8693295 100644 --- a/gui_qt/widgetcurveeditor.cpp +++ b/gui_qt/widgetcurveeditor.cpp @@ -1,10 +1,13 @@ #include "widgetcurveeditor.h" #include +#include +#include "../lib_paysages/tools.h" WidgetCurveEditor::WidgetCurveEditor(QWidget *parent) : QWidget(parent) { _curve = curveCreate(); + _dragged = -1; setMinimumSize(500, 500); setMaximumSize(500, 500); @@ -47,6 +50,85 @@ void WidgetCurveEditor::paintEvent(QPaintEvent* event) for (i = 0; i < n; i++) { curveGetPoint(_curve, i, &point); - painter.drawEllipse(QPoint((int)(point.position * 499.0), 499 - (int)(point.value * 499.0)), 4.0, 4.0); + painter.drawEllipse(QPointF((int)(point.position * 499.0), 499 - (int)(point.value * 499.0)), 4.0, 4.0); } } + +void WidgetCurveEditor::mousePressEvent(QMouseEvent* event) +{ + int i, n; + int nearest; + double mousex, mousey; + double distance, ndistance; + CurvePoint point; + + n = curveGetPointCount(_curve); + if (n < 1) + { + return; + } + + mousex = ((double)event->x()) / 499.0; + mousey = 1.0 - ((double)event->y()) / 499.0; + nearest = -1; + + // Find nearest point + for (i = 0; i < n; i++) + { + curveGetPoint(_curve, i, &point); + ndistance = toolsGetDistance2D(point.position, point.value, mousex, mousey); + if (nearest < 0 || ndistance < distance) + { + distance = ndistance; + nearest = i; + } + } + + if (distance < 0.015) + { + _dragged = nearest; + } + else + { + _dragged = -1; + } + + event->accept(); +} + +void WidgetCurveEditor::mouseMoveEvent(QMouseEvent* event) +{ + CurvePoint point; + + if (_dragged >= 0) + { + point.position = ((double)event->x()) / 499.0; + point.value = 1.0 - ((double)event->y()) / 499.0; + + point.position = (point.position < 0.0) ? 0.0 : point.position; + point.position = (point.position > 1.0) ? 1.0 : point.position; + point.value = (point.value < 0.0) ? 0.0 : point.value; + point.value = (point.value > 1.0) ? 1.0 : point.value; + + curveSetPoint(_curve, _dragged, &point); + + update(); + + emit liveChanged(); + } + + event->accept(); +} + +void WidgetCurveEditor::mouseReleaseEvent(QMouseEvent* event) +{ + if (_dragged >= 0) + { + _dragged = -1; + curveValidate(_curve); + + update(); + } + + event->accept(); +} diff --git a/gui_qt/widgetcurveeditor.h b/gui_qt/widgetcurveeditor.h index 7e395c3..8844510 100644 --- a/gui_qt/widgetcurveeditor.h +++ b/gui_qt/widgetcurveeditor.h @@ -15,11 +15,18 @@ public: void setCurve(Curve* curve); void getCurve(Curve* curve); +signals: + void liveChanged(); + protected: void paintEvent(QPaintEvent* event); + void mousePressEvent(QMouseEvent* event); + void mouseMoveEvent(QMouseEvent* event); + void mouseReleaseEvent(QMouseEvent* event); private: Curve* _curve; + int _dragged; }; #endif diff --git a/i18n/paysages_fr.ts b/i18n/paysages_fr.ts index 1270b12..96b01b6 100644 --- a/i18n/paysages_fr.ts +++ b/i18n/paysages_fr.ts @@ -37,7 +37,22 @@ DialogColorGradation - + + Validate + Valider + + + + Revert + Recommencer + + + + Cancel + Annuler + + + Paysages 3D - Color gradation editor Paysages 3D - Editeur de gradients de couleur diff --git a/lib_paysages/tools.c b/lib_paysages/tools.c index 57be3db..6c7d2d2 100644 --- a/lib_paysages/tools.c +++ b/lib_paysages/tools.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "color.h" #include "euclid.h" @@ -119,6 +120,13 @@ Vector3 toolsGetNormalFromTriangle(Vector3 center, Vector3 bottom, Vector3 right return v3Normalize(v3Cross(dz, dx)); } +double toolsGetDistance2D(double x1, double y1, double x2, double y2) +{ + double dx = x2 - x1; + double dy = y2 - y1; + return sqrt(dx * dx + dy * dy); +} + void toolsSaveDouble(FILE* f, double* value) { uint64_t servalue; diff --git a/lib_paysages/tools.h b/lib_paysages/tools.h index 68cece3..b6b747f 100644 --- a/lib_paysages/tools.h +++ b/lib_paysages/tools.h @@ -12,6 +12,7 @@ double toolsRandom(); double toolsBicubicInterpolate(double stencil[16], double x, double y); void toolsFloat2DMapCopy(double* src, double* dest, int src_xstart, int src_ystart, int dest_xstart, int dest_ystart, int xsize, int ysize, int src_xstep, int src_ystep, int dest_xstep, int dest_ystep); Vector3 toolsGetNormalFromTriangle(Vector3 center, Vector3 bottom, Vector3 right); +double toolsGetDistance2D(double x1, double y1, double x2, double y2); void toolsSaveDouble(FILE* f, double* value); void toolsLoadDouble(FILE* f, double* value); void toolsSaveInt(FILE* f, int* value);