Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

How to drag a rectangle while using ofEasyCam?

$
0
0

@lethalrush wrote:

I’m trying to figure out how to drag a rectangle around with the mouse, while ofEasyCam is on.

In the code below, the rectangle moves with the cursor just fine. However, when you zoom or pan/tilt around the scene with ofEasyCam, the rectangle no longer stays on the cursor while moving it. I think I understand why, but I don’t know how to fix it. I assume I’m applying the screen perspective to the rectangle cam’s world perspective?

Anyway, how can i make this work?

Thanks in advance to whomever gives this a look-over. :slight_smile:

To try the code I’ve written below, pressing ‘c’ toggles ofEasyCam mouse control on or off, so that you can move the rectangle without affecting the camera, and vice versa.

ofApp.h

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

public:
	static constexpr int X = 0;
	static constexpr int Y = 0;
	static constexpr int sX = 60;
	static constexpr int sY = 88;

	void setup();
	void update();
	void draw();

	void mouseDragged(int x, int y, int button);
	void mousePressed(int x, int y, int button);
	void keyPressed(int key);

	ofEasyCam cam;

	ofRectangle rectangle1;

	bool isMousePressingOnSelection = false;
	ofVec3f mousePressLocation;

	ofPolyline getPolyLineFromPanel(ofRectangle & rectangle);

	// inverse state with panel move mode.
	bool isCameraMoveMode = true;
	int screenWidth;
	int screenHeight;
};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    rectangle1.set(X, Y, sX, sY);
    screenWidth = ofGetWidth();
    screenHeight = ofGetHeight();
}

//--------------------------------------------------------------
void ofApp::draw(){
    cam.begin();
    ofSetColor(0, 255, 0);
    ofDrawRectangle(rectangle1);
    cam.end();
}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
    if ( !isCameraMoveMode ) {
        ofPolyline p = getPolyLineFromPanel(rectangle1);

        if (p.inside(ofVec3f(x, y, 0))) {
            cout << "Clicked on rectangle" << endl;
            isMousePressingOnSelection = true;
            mousePressLocation.x = x;
            mousePressLocation.y = y;
        } else {
            isMousePressingOnSelection = false;
        }
    }
}

void ofApp::update() {
    if ( isCameraMoveMode ) {
        cam.enableMouseInput();
    }
    else {
        cam.disableMouseInput();
    }
}

void ofApp::mouseDragged(int x, int y, int button){
    if ( isMousePressingOnSelection ) {
        rectangle1.setPosition(glm::vec3(x - (screenWidth/2), screenHeight - y - (screenHeight/2), 0));
    }
}

ofPolyline ofApp::getPolyLineFromPanel(ofRectangle & rectangle) {
    ofVec3f rectToScreen_botLeft = cam.worldToScreen(rectangle1.getBottomLeft());
    ofVec3f rectToScreen_botRight = cam.worldToScreen(rectangle1.getBottomRight());
    ofVec3f rectToScreen_topRight = cam.worldToScreen(rectangle1.getTopRight());
    ofVec3f rectToScreen_topLeft = cam.worldToScreen(rectangle1.getTopLeft());

    ofPolyline p;
    p.addVertex(rectToScreen_botLeft);
    p.addVertex(rectToScreen_botRight);
    p.addVertex(rectToScreen_topRight);
    p.addVertex(rectToScreen_topLeft);
    return p;
}

void ofApp::keyPressed(int key) {
    if ( key == 'c' ) {
        isCameraMoveMode = !isCameraMoveMode;
    }
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles