Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
Eyedropper
Browse files Browse the repository at this point in the history
  • Loading branch information
yvt committed Feb 19, 2015
1 parent 42f0b46 commit 8ed69bf
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Terravox.pro
Expand Up @@ -45,7 +45,8 @@ SOURCES += main.cpp\
erosioneditor.cpp \
terrainview_p.cpp \
terrainview_p_context.cpp \
terrainview_p_ao.cpp
terrainview_p_ao.cpp \
colorsamplerview.cpp

HEADERS += mainwindow.h \
brusheditor.h \
Expand Down Expand Up @@ -74,7 +75,8 @@ HEADERS += mainwindow.h \
erosioneffect.h \
erosioneffectcontroller.h \
erosioneditor.h \
terrainview_p.h
terrainview_p.h \
colorsamplerview.h

FORMS += mainwindow.ui \
brusheditor.ui \
Expand Down
15 changes: 14 additions & 1 deletion brushtoolview.cpp
Expand Up @@ -5,6 +5,7 @@
#include "terrain.h"
#include "session.h"
#include "terrainedit.h"
#include <QApplication>

static const QPoint InvalidPoint(-1000, -1000);

Expand All @@ -13,7 +14,8 @@ BrushToolView::BrushToolView(TerrainView *view, Session *session, BrushTool *too
tool(tool),
cursor(InvalidPoint),
lastCursor(InvalidPoint),
session(session)
session(session),
otherActive(false)
{
connect(view, SIGNAL(clientMousePressed(QMouseEvent*)),
SLOT(clientMousePressed(QMouseEvent*)));
Expand Down Expand Up @@ -42,6 +44,10 @@ BrushToolView::~BrushToolView()

void BrushToolView::clientMousePressed(QMouseEvent *e)
{
if (e->isAccepted() || e->button() != Qt::LeftButton) {
otherActive = true;
return;
}
e->accept();

if (cursor == InvalidPoint) {
Expand Down Expand Up @@ -95,6 +101,9 @@ void BrushToolView::clientMouseReleased(QMouseEvent *e)
timer->stop();;
session->endEdit();
}
if (e->buttons() == 0) {
otherActive = false;
}
}

void BrushToolView::clientMouseMoved(QMouseEvent *e)
Expand Down Expand Up @@ -138,6 +147,10 @@ void BrushToolView::terrainPaint(TerrainViewDrawingContext *ctx)
if (currentEdit) {
return;
}
if (otherActive || QApplication::keyboardModifiers() & Qt::AltModifier) {
// eyedropper active
return;
}
if (tipImage.isNull()) {
Terrain *t = tool->tip().data();
int width = t->size().width();
Expand Down
1 change: 1 addition & 0 deletions brushtoolview.h
Expand Up @@ -29,6 +29,7 @@ class BrushToolView : public ToolView
QPoint cursor;
QPoint lastCursor;
Session *session;
bool otherActive;

QPoint dragStartPos;
QElapsedTimer elapsedTimer;
Expand Down
159 changes: 159 additions & 0 deletions colorsamplerview.cpp
@@ -0,0 +1,159 @@
#include "colorsamplerview.h"
#include <QPainter>
#include "terrainview.h"
#include <QTimer>
#include <QApplication>
#include <QPixmap>
#include "terrain.h"

ColorSamplerView::ColorSamplerView(TerrainView *view, QObject *parent) :
QObject(parent),
view(view),
active(false),
hover(false),
otherActive(false),
mouseHover(false)
{
connect(view, SIGNAL(clientMousePressed(QMouseEvent*)),
SLOT(clientMousePressed(QMouseEvent*)));
connect(view, SIGNAL(clientMouseReleased(QMouseEvent*)),
SLOT(clientMouseReleased(QMouseEvent*)));
connect(view, SIGNAL(clientMouseMoved(QMouseEvent*)),
SLOT(clientMouseMoved(QMouseEvent*)));
connect(view, SIGNAL(clientPaint(QPaintEvent*)),
SLOT(clientPaint(QPaintEvent*)));
connect(view, SIGNAL(clientEnter(QEvent*)),
SLOT(clientEnter(QEvent*)));
connect(view, SIGNAL(clientLeave(QEvent*)),
SLOT(clientLeave(QEvent*)));
connect(view, SIGNAL(terrainPaint(TerrainViewDrawingContext*)),
SLOT(terrainPaint(TerrainViewDrawingContext*)));

timer.reset(new QTimer());
timer->setInterval(30);
connect(timer.data(), SIGNAL(timeout()), SLOT(checkModifier()));

cursor = QCursor(QPixmap(":/Terravox/cursorEyedropper.png"), 5, 26);
}

ColorSamplerView::~ColorSamplerView()
{

}

void ColorSamplerView::clientMousePressed(QMouseEvent *e)
{
if (e->isAccepted()) {
otherActive = true;
}
checkModifier();

if (active) {
return;
}
if (hover && e->button() == Qt::LeftButton) {
active = true;
e->accept();
clientMouseMoved(e);
} else {
otherActive = true;
}
}

void ColorSamplerView::clientMouseReleased(QMouseEvent *e)
{
if (e->buttons() == 0) {
otherActive = false;
}
if (e->button() == Qt::LeftButton) {
active = false;
}
checkModifier();
}

void ColorSamplerView::clientMouseMoved(QMouseEvent *e)
{
cursorPos = e->pos();
checkModifier();
if (hover) {
// sample
lastSampledColor = QColor();
if (QRect(QPoint(0, 0), view->size()).contains(e->pos())) {
auto p = view->rayCast(e->pos());
Terrain *t = view->terrain().data();
auto tsz = t->size();
if (p.x() >= 0 && p.y() >= 0 && p.x() < tsz.width() && p.y() < tsz.height()) {
lastSampledColor = QColor::fromRgb(t->color(p.x(), p.y()) | 0xff000000);

}
}

if (active && lastSampledColor.isValid()) {
emit sampled(lastSampledColor);
}
view->update();
}
}

void ColorSamplerView::clientPaint(QPaintEvent *)
{
if (!hover)
return;
QPainter p(view);
p.setRenderHint(QPainter::Antialiasing);
if (lastSampledColor.isValid()) {
p.setPen(Qt::NoPen);
p.setBrush(QColor(qRgba(0, 0, 0, 128)));
p.drawEllipse(cursorPos, 7, 7);
p.setBrush(QColor(qRgba(255, 255, 255, 255)));
p.drawEllipse(cursorPos, 6, 6);
p.setBrush(lastSampledColor);
p.drawEllipse(cursorPos, 5, 5);
}
}

void ColorSamplerView::clientEnter(QEvent *)
{
//timer->start(); // FIXME: find better way...
mouseHover = true;
checkModifier();
}

void ColorSamplerView::clientLeave(QEvent *)
{
//timer->stop();
mouseHover = false;
checkModifier();
}

void ColorSamplerView::terrainPaint(TerrainViewDrawingContext *dc)
{

}

void ColorSamplerView::checkModifier(Qt::KeyboardModifiers km)
{
bool state = km & Qt::AltModifier;
state = state && mouseHover;
if (active) {
state = true;
}
if (otherActive) {
state = false;
}
if (state != hover) {
if (state) {
view->addCursorOverride(&cursor);
} else {
view->removeCursorOverride(&cursor);
}
view->update();
hover = state;
}
}

void ColorSamplerView::checkModifier()
{
checkModifier(QApplication::keyboardModifiers());
}

48 changes: 48 additions & 0 deletions colorsamplerview.h
@@ -0,0 +1,48 @@
#ifndef COLORSAMPLERVIEW_H
#define COLORSAMPLERVIEW_H

#include <QObject>
#include <QScopedPointer>
#include <QInputEvent>
#include <QCursor>
#include <QColor>
#include <QPoint>

class TerrainViewDrawingContext;
class TerrainView;

class ColorSamplerView : public QObject
{
Q_OBJECT
public:
explicit ColorSamplerView(TerrainView *view, QObject *parent = 0);
~ColorSamplerView();

signals:
void sampled(QColor);

private slots:
void clientMousePressed(QMouseEvent *);
void clientMouseReleased(QMouseEvent *);
void clientMouseMoved(QMouseEvent *);
void clientPaint(QPaintEvent *);
void clientEnter(QEvent *);
void clientLeave(QEvent *);
void terrainPaint(TerrainViewDrawingContext *);

void checkModifier(Qt::KeyboardModifiers);
void checkModifier();

private:
TerrainView *view;
QScopedPointer<QTimer> timer;
bool mouseHover;
bool hover;
bool active;
bool otherActive; // other tool is using mouse
QPoint cursorPos;
QCursor cursor;
QColor lastSampledColor;
};

#endif // COLORSAMPLERVIEW_H
5 changes: 5 additions & 0 deletions mainwindow.cpp
Expand Up @@ -12,6 +12,7 @@
#include "session.h"
#include "colorpickerwindow.h"
#include "colorpicker.h"
#include "colorsamplerview.h"

#include "toolcontroller.h"
#include "brushtoolcontroller.h"
Expand Down Expand Up @@ -75,6 +76,10 @@ MainWindow::MainWindow(QWidget *parent) :
addTool(new BrushToolController(BrushType::Paint), ui->toolPaint);
addTool(new BrushToolController(BrushType::Blur), ui->toolBlur);

colorSampler.reset(new ColorSamplerView(ui->terrainView));
connect(colorSampler.data(), SIGNAL(sampled(QColor)),
ui->primaryColorView, SLOT(setValue(QColor)));

QSharedPointer<Session> s = QSharedPointer<Session>::create();
s->setTerrain(QSharedPointer<Terrain>(TerrainGenerator(QSize(512, 512)).generateRandomLandform()));
s->terrain()->quantize();
Expand Down
3 changes: 3 additions & 0 deletions mainwindow.h
Expand Up @@ -19,6 +19,7 @@ class MainWindow;
class ToolController;
class EffectController;
class ColorPickerWindow;
class ColorSamplerView;

class MainWindow : public QMainWindow
{
Expand All @@ -34,6 +35,8 @@ class MainWindow : public QMainWindow
QSharedPointer<Session> session;
QString currentFilePath;

QScopedPointer<ColorSamplerView> colorSampler;

QWidget *currentToolEditor;
QSharedPointer<ToolView> currentToolView;

Expand Down
Binary file added res/cursorEyedropper.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/cursorHand.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions res/resources.qrc
Expand Up @@ -6,5 +6,7 @@
<file>toolRaise.png</file>
<file>toolBlur.png</file>
<file>toolSmoothen.png</file>
<file>cursorEyedropper.png</file>
<file>cursorHand.png</file>
</qresource>
</RCC>
25 changes: 24 additions & 1 deletion terrainview.cpp
Expand Up @@ -67,12 +67,25 @@ const TerrainViewOptions &TerrainView::viewOptions()
return d->viewOptions;
}

void TerrainView::addCursorOverride(QCursor *cursor)
{
setCursor(*cursor);
cursorOverride = cursor;
}

void TerrainView::removeCursorOverride(QCursor *cursor)
{
if (cursor == cursorOverride) {
setCursor(QCursor());
cursorOverride = nullptr;
}
}

void TerrainView::paintEvent(QPaintEvent *e)
{
Q_D(TerrainView);
auto size = this->size();


SceneDefinition def;
def.cameraDir = QVector3D(std::cos(d->yaw_) * std::cos(d->pitch_),
std::sin(d->yaw_) * std::cos(d->pitch_),
Expand Down Expand Up @@ -177,6 +190,16 @@ void TerrainView::wheelEvent(QWheelEvent *e)
e->accept();
}

void TerrainView::enterEvent(QEvent *e)
{
emit clientEnter(e);
}

void TerrainView::leaveEvent(QEvent *e)
{
emit clientLeave(e);
}

void TerrainView::showOptionsWindow()
{
if (!optionsWindow) {
Expand Down

0 comments on commit 8ed69bf

Please sign in to comment.