Skip to content

Commit

Permalink
modified for working with ofxVisualProgramming addon
Browse files Browse the repository at this point in the history
  • Loading branch information
d3cod3 committed Jan 23, 2019
1 parent 973c167 commit 1f2082c
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 8 deletions.
228 changes: 223 additions & 5 deletions src/ofxWarp/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ namespace ofxWarp
Controller::Controller()
: focusedIndex(-1)
{
ofAddListener(ofEvents().mouseMoved, this, &Controller::onMouseMoved);
/*ofAddListener(ofEvents().mouseMoved, this, &Controller::onMouseMoved);
ofAddListener(ofEvents().mousePressed, this, &Controller::onMousePressed);
ofAddListener(ofEvents().mouseDragged, this, &Controller::onMouseDragged);
ofAddListener(ofEvents().mouseReleased, this, &Controller::onMouseReleased);
ofAddListener(ofEvents().keyPressed, this, &Controller::onKeyPressed);
ofAddListener(ofEvents().keyReleased, this, &Controller::onKeyReleased);
ofAddListener(ofEvents().windowResized, this, &Controller::onWindowResized);
ofAddListener(ofEvents().windowResized, this, &Controller::onWindowResized);*/
}

//--------------------------------------------------------------
Controller::~Controller()
{
ofRemoveListener(ofEvents().mouseMoved, this, &Controller::onMouseMoved);
/*ofRemoveListener(ofEvents().mouseMoved, this, &Controller::onMouseMoved);
ofRemoveListener(ofEvents().mousePressed, this, &Controller::onMousePressed);
ofRemoveListener(ofEvents().mouseDragged, this, &Controller::onMouseDragged);
ofRemoveListener(ofEvents().mouseReleased, this, &Controller::onMouseReleased);
Expand All @@ -36,9 +36,24 @@ namespace ofxWarp
ofRemoveListener(ofEvents().windowResized, this, &Controller::onWindowResized);
this->warps.clear();
this->warps.clear();*/
}

//--------------------------------------------------------------
void Controller::removeListeners(){
ofRemoveListener(ofEvents().mouseMoved, this, &Controller::onMouseMoved);
ofRemoveListener(ofEvents().mousePressed, this, &Controller::onMousePressed);
ofRemoveListener(ofEvents().mouseDragged, this, &Controller::onMouseDragged);
ofRemoveListener(ofEvents().mouseReleased, this, &Controller::onMouseReleased);

ofRemoveListener(ofEvents().keyPressed, this, &Controller::onKeyPressed);
ofRemoveListener(ofEvents().keyReleased, this, &Controller::onKeyReleased);

ofRemoveListener(ofEvents().windowResized, this, &Controller::onWindowResized);

this->warps.clear();
}

//--------------------------------------------------------------
bool Controller::saveSettings(const std::string & filePath)
{
Expand Down Expand Up @@ -113,6 +128,7 @@ namespace ofxWarp
if (warp)
{
warp->deserialize(jsonWarp);
warp->setSize(warp->getWidth(),warp->getHeight());
this->warps.push_back(warp);
}
}
Expand Down Expand Up @@ -209,6 +225,13 @@ namespace ofxWarp
this->selectClosestControlPoint(args);
}

void Controller::onMouseMoved(int x, int y){
ofMouseEventArgs temp;
temp.x = x;
temp.y = y;
this->selectClosestControlPoint(temp);
}

//--------------------------------------------------------------
void Controller::onMousePressed(ofMouseEventArgs & args)
{
Expand All @@ -221,6 +244,19 @@ namespace ofxWarp
}
}

void Controller::onMousePressed(int x, int y){
ofMouseEventArgs temp;
temp.x = x;
temp.y = y;

this->selectClosestControlPoint(temp);

if (this->focusedIndex < this->warps.size())
{
this->warps[this->focusedIndex]->handleCursorDown(temp);
}
}

//--------------------------------------------------------------
void Controller::onMouseDragged(ofMouseEventArgs & args)
{
Expand All @@ -230,10 +266,25 @@ namespace ofxWarp
}
}

void Controller::onMouseDragged(int x, int y){
ofMouseEventArgs temp;
temp.x = x;
temp.y = y;

if (this->focusedIndex < this->warps.size())
{
this->warps[this->focusedIndex]->handleCursorDrag(temp);
}
}

//--------------------------------------------------------------
void Controller::onMouseReleased(ofMouseEventArgs & args)
{}

void Controller::onMouseReleased(int x, int y){

}

//--------------------------------------------------------------
void Controller::onKeyPressed(ofKeyEventArgs & args)
{
Expand Down Expand Up @@ -395,10 +446,173 @@ namespace ofxWarp
}
}

void Controller::onKeyPressed(int key){
if (key == 'w')
{
for (auto warp : this->warps)
{
warp->toggleEditing();
}
}
else if (this->focusedIndex < this->warps.size())
{
auto warp = this->warps[this->focusedIndex];

if (key == '-')
{
warp->setBrightness(MAX(0.0f, warp->getBrightness() - 0.01f));
}
else if (key == '+')
{
warp->setBrightness(MIN(1.0f, warp->getBrightness() + 0.01f));
}
else if (key == 'r')
{
warp->reset();
}
else if (key == OF_KEY_TAB)
{
// Select the next of previous (+ SHIFT) control point.
size_t nextIndex;
auto selectedIndex = warp->getSelectedControlPoint();
if (ofGetKeyPressed(OF_KEY_SHIFT))
{
if (selectedIndex == 0)
{
nextIndex = warp->getNumControlPoints() - 1;
}
else
{
nextIndex = selectedIndex - 1;
}
}
else
{
nextIndex = (selectedIndex + 1) % warp->getNumControlPoints();
}
warp->selectControlPoint(nextIndex);
}
else if (key == OF_KEY_UP || key == OF_KEY_DOWN || key == OF_KEY_LEFT || key == OF_KEY_RIGHT)
{
auto step = ofGetKeyPressed(OF_KEY_SHIFT) ? 10.0f : 0.5f;
auto shift = glm::vec2(0.0f);
if (key == OF_KEY_UP)
{
shift.y = -step / (float)ofGetHeight();
}
else if (key == OF_KEY_DOWN)
{
shift.y = step / (float)ofGetHeight();
}
else if (key == OF_KEY_LEFT)
{
shift.x = -step / (float)ofGetWidth();
}
else
{
shift.x = step / (float)ofGetWidth();
}
warp->moveControlPoint(warp->getSelectedControlPoint(), shift);
}
else if (key == OF_KEY_F9)
{
warp->rotateCounterclockwise();
}
else if (key == OF_KEY_F10)
{
warp->rotateClockwise();
}
else if (key == OF_KEY_F11)
{
warp->flipHorizontal();
}
else if (key == OF_KEY_F12)
{
warp->flipVertical();
}
else if (warp->getType() == WarpBase::TYPE_BILINEAR || warp->getType() == WarpBase::TYPE_PERSPECTIVE_BILINEAR)
{
// The rest of the controls only apply to Bilinear warps.
auto warpBilinear = std::dynamic_pointer_cast<WarpBilinear>(warp);
if (warpBilinear)
{
if (key == OF_KEY_F1)
{
// Reduce the number of horizontal control points.
if (ofGetKeyPressed(OF_KEY_SHIFT))
{
warpBilinear->setNumControlsX(warpBilinear->getNumControlsX() - 1);
}
else
{
warpBilinear->setNumControlsX((warpBilinear->getNumControlsX() + 1) / 2);
}
}
else if (key == OF_KEY_F2)
{
// Increase the number of horizontal control points.
if (ofGetKeyPressed(OF_KEY_SHIFT))
{
warpBilinear->setNumControlsX(warpBilinear->getNumControlsX() + 1);
}
else
{
warpBilinear->setNumControlsX(warpBilinear->getNumControlsX() * 2 - 1);
}
}
else if (key == OF_KEY_F3)
{
// Reduce the number of vertical control points.
if (ofGetKeyPressed(OF_KEY_SHIFT))
{
warpBilinear->setNumControlsY(warpBilinear->getNumControlsY() - 1);
}
else
{
warpBilinear->setNumControlsY((warpBilinear->getNumControlsY() + 1) / 2);
}
}
else if (key == OF_KEY_F4)
{
// Increase the number of vertical control points.
if (ofGetKeyPressed(OF_KEY_SHIFT))
{
warpBilinear->setNumControlsY(warpBilinear->getNumControlsY() + 1);
}
else
{
warpBilinear->setNumControlsY(warpBilinear->getNumControlsY() * 2 - 1);
}
}
else if (key == OF_KEY_F5)
{
warpBilinear->decreaseResolution();
}
else if (key == OF_KEY_F6)
{
warpBilinear->increaseResolution();
}
else if (key == OF_KEY_F7)
{
warpBilinear->setAdaptive(!warpBilinear->getAdaptive());
}
else if (key == 'm')
{
warpBilinear->setLinear(!warpBilinear->getLinear());
}
}
}
}
}

//--------------------------------------------------------------
void Controller::onKeyReleased(ofKeyEventArgs & args)
{}

void Controller::onKeyReleased(int key){

}

//--------------------------------------------------------------
void Controller::onWindowResized(ofResizeEventArgs & args)
{
Expand All @@ -407,4 +621,8 @@ namespace ofxWarp
warp->handleWindowResize(args.width, args.height);
}
}
}

void Controller::onWindowResized(int w, int h){

}
}
10 changes: 10 additions & 0 deletions src/ofxWarp/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace ofxWarp
Controller();
~Controller();


void removeListeners();

//! write a settings json file
bool saveSettings(const std::string & filePath);
//! read a settings json file
Expand Down Expand Up @@ -45,20 +48,27 @@ namespace ofxWarp

//! handle mouseMoved events for multiple warps
void onMouseMoved(ofMouseEventArgs & args);
void onMouseMoved(int x, int y);
//! handle mousePressed events for multiple warps
void onMousePressed(ofMouseEventArgs & args);
void onMousePressed(int x, int y);
//! handle mouseDragged events for multiple warps
void onMouseDragged(ofMouseEventArgs & args);
void onMouseDragged(int x, int y);
//! handle mouseReleased events for multiple warps
void onMouseReleased(ofMouseEventArgs & args);
void onMouseReleased(int x, int y);

//! handle keyPressed events for multiple warps
void onKeyPressed(ofKeyEventArgs & args);
void onKeyPressed(int key);
//! handle keyReleased events for multiple warps
void onKeyReleased(ofKeyEventArgs & args);
void onKeyReleased(int key);

//! handle windowResized events for multiple warps
void onWindowResized(ofResizeEventArgs & args);
void onWindowResized(int w, int h);

protected:
//! check all warps and select the closest control point
Expand Down
7 changes: 6 additions & 1 deletion src/ofxWarp/WarpBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace ofxWarp
{
//--------------------------------------------------------------
std::filesystem::path WarpBase::shaderPath = std::filesystem::path("shaders") / "ofxWarp";
//std::filesystem::path WarpBase::shaderPath = std::filesystem::path("shaders") / "ofxWarp";
std::filesystem::path WarpBase::shaderPath = std::filesystem::path("ofxWarp");

//--------------------------------------------------------------
void WarpBase::setShaderPath(const std::filesystem::path shaderPath)
Expand Down Expand Up @@ -47,6 +48,8 @@ namespace ofxWarp
void WarpBase::serialize(nlohmann::json & json)
{
// Main parameters.
json["width"] = this->width;
json["height"] = this->height;
json["type"] = this->type;
json["brightness"] = this->brightness;

Expand Down Expand Up @@ -91,6 +94,8 @@ namespace ofxWarp
void WarpBase::deserialize(const nlohmann::json & json)
{
// Main parameters.
this->width = json["width"];
this->height = json["height"];
int typeAsInt = json["type"];
this->type = (Type)typeAsInt;
this->brightness = json["brightness"];
Expand Down
2 changes: 1 addition & 1 deletion src/ofxWarp/WarpBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,4 @@ namespace ofxWarp
ofVboMesh controlMesh;
ofShader controlShader;
};
}
}
4 changes: 3 additions & 1 deletion src/ofxWarp/WarpPerspectiveBilinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ namespace ofxWarp
{
if (!this->editing || this->selectedIndex >= this->controlPoints.size()) return false;

//ofLog(OF_LOG_NOTICE,"controlPoint: %f:%f",this->getControlPoint(this->selectedIndex).x,this->getControlPoint(this->selectedIndex).y);

// Depending on selected control point, let perspective or bilinear warp handle it.
if (this->isCorner(this->selectedIndex))
{
Expand Down Expand Up @@ -256,4 +258,4 @@ namespace ofxWarp

return index;
}
}
}

0 comments on commit 1f2082c

Please sign in to comment.