@robotPatrol wrote:
hi,
i added the simple events example to the viewports example because i would like to add mouse events to a viewport. in trying to align the mouse location and viewport, i receive errors that instruct me to use a static variable when i access the viewport from my nested class. however, when i use the static tag, i get an error about a misused static variable.
.h:
#pragma once #include "ofMain.h" class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void randomize(ofRectangle & viewport); void randomizeViewports(); void drawViewportOutline(const ofRectangle & viewport); void keyPressed(int key); ofRectangle viewport2D; ofColor rColor; float size; float offset; class circleEvent { public: circleEvent(); ~circleEvent(); ofEvent<ofVec2f> clickedOnCircle; void setup(float x, float y, int size, ofColor rColor); void draw(); void clear(); void mouseMoved(ofMouseEventArgs & args); void mouseDragged(ofMouseEventArgs & args); void mousePressed(ofMouseEventArgs & args); void mouseReleased(ofMouseEventArgs & args); void mouseScrolled(ofMouseEventArgs & args); void mouseEntered(ofMouseEventArgs & args); void mouseExited(ofMouseEventArgs & args); bool inside(float _x, float _y); int radius; float x, y; ofColor color; protected: bool bRegisteredEvents; }; void clickOnCircle(ofVec2f & e); ofColor highlightCircle; ofVec2f clickedCircle; circleEvent circles[10][10]; };
.cpp:
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ ofBackground(90); randomizeViewports(); ofEnableSmoothing(); for(int i = 0; i < 10; i ++) { for(int j = 0; j < 10; j ++) { ofAddListener(circles[i][j].clickedOnCircle, this, &ofApp::clickOnCircle); } } } //-------------------------------------------------------------- void ofApp::update(){} //-------------------------------------------------------------- void ofApp::draw(){ drawViewportOutline(viewport2D); ofPushView(); ofViewport(viewport2D); ofSetupScreen(); ofFill(); ofDrawRectangle(0, 0, ofGetViewportWidth(), ofGetViewportHeight()); offset = (ofGetViewportWidth()/10)/2.; for(int i = 0; i < 10; i ++){ for(int j = 0; j < 10; j ++){ rColor = ofColor(i * 100, j * 200, (i * j) * 255); float x = ofMap(i, 0, 10, (2 * ofGetViewportWidth()/10), 9 * ofGetViewportWidth()/10); float y = ofMap(j, 0, 10, (2 * ofGetViewportHeight()/10), 9 * ofGetViewportHeight()/10); if((j % 2) != 0) x = x - offset; float size = 10; circles[i][j].setup(x, y, size, ofColor(rColor)); circles[i][j].draw(); } } ofPushStyle(); ofDrawCircle(clickedCircle, size * 0.75); ofPopStyle(); ofPopView(); } //-------------------------------------------------------------- void ofApp::randomize(ofRectangle & viewport){ viewport.x = 200; viewport.y = 200; viewport.width = 500; viewport.height = 500; } //-------------------------------------------------------------- void ofApp::randomizeViewports(){ randomize(viewport2D); } //-------------------------------------------------------------- void ofApp::drawViewportOutline(const ofRectangle & viewport){ ofPushStyle(); ofFill(); ofSetColor(50); ofSetLineWidth(0); ofDrawRectangle(viewport); ofNoFill(); ofSetColor(25); ofSetLineWidth(1.0f); ofDrawRectangle(viewport); ofPopStyle(); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ if(key == ' '){ randomizeViewports(); } } //-------------------------------------------------------------- ofApp::circleEvent::circleEvent(){ bRegisteredEvents = false; } //-------------------------------------------------------------- ofApp::circleEvent::~circleEvent(){ clear(); } //-------------------------------------------------------------- void ofApp::circleEvent::setup(float x, float y, int size, ofColor rColor) { this -> x = x; this -> y = y; this -> radius = size; this -> color = rColor; if(!bRegisteredEvents) { ofRegisterMouseEvents(this); bRegisteredEvents = true; } } //-------------------------------------------------------------- void ofApp::circleEvent::draw(){ ofPushStyle(); ofSetColor(color); ofDrawCircle(x, y, radius); ofPopStyle(); } //-------------------------------------------------------------- void ofApp::circleEvent::clear(){ if(bRegisteredEvents) { ofUnregisterMouseEvents(this); bRegisteredEvents = false; } } //-------------------------------------------------------------- void ofApp::circleEvent::mouseMoved(ofMouseEventArgs & args){} //-------------------------------------------------------------- void ofApp::circleEvent::mouseDragged(ofMouseEventArgs & args){} //-------------------------------------------------------------- void ofApp::circleEvent::mousePressed(ofMouseEventArgs & args){} //-------------------------------------------------------------- void ofApp::circleEvent::mouseReleased(ofMouseEventArgs & args){ if(inside(args.x, args.y)) { ofVec2f mousePos = ofVec2f(args.x, args.y); ofNotifyEvent(clickedOnCircle, mousePos, this); //cout << "x: " << args.x << ", y: " << args.y << "\n"; } } //-------------------------------------------------------------- void ofApp::circleEvent::mouseScrolled(ofMouseEventArgs & args){} //-------------------------------------------------------------- void ofApp::circleEvent::mouseEntered(ofMouseEventArgs & args){} //-------------------------------------------------------------- void ofApp::circleEvent::mouseExited(ofMouseEventArgs & args){} //-------------------------------------------------------------- bool ofApp::circleEvent::inside(float _x, float _y){ // cout << "_x: " << _x << ", _y: " << _y << "\n"; // cout << "x: " << x << ", y: " << y << "\n"; // cout << "distanc: " << (ofVec2f(_x, _y).distance(ofVec2f(x, y))) << "\n"; // cout << "radius: " << radius << "\n"; // return (ofVec2f(ofMap(_x, 0, ofGetWidth(), ofApp::viewport2D.getPosition().x, // ofApp::viewport2D.getPosition().x + ofApp::viewport2D.getWidth()), // ofMap(_y, 0, ofGetHeight(), ofApp::viewport2D.getPosition().y, // ofApp::viewport2D.getPosition().y + ofApp::viewport2D.getHeight())).distance(ofVec2f(x, y)) < radius); return (ofVec2f(_x, _y).distance(ofVec2f(x, y)) < radius); } //-------------------------------------------------------------- void ofApp::clickOnCircle(ofVec2f & e){ highlightCircle.set(rColor.r - 100, rColor.g - 100, rColor.b - 100); clickedCircle.set(e); }
what is suppose to happen is that when a circle is clicked, another smaller circle is drawn on top of it in another color.
i think the misalignment of the mouse location to the viewport is hiding the drawing of the circle, but is apparent if i exaggerate the size of the drawn circle and the radius of the target mouse event:
ofPushStyle(); ofDrawCircle(clickedCircle, size * 10); ofPopStyle();
and
return (ofVec2f(_x, _y).distance(ofVec2f(x, y)) < radius * 10.0);
questions:
how do i align the mouse events to the viewport here?
how do i retain the color i assigned to the newly drawn circle in my event function?thanks.
01
Posts: 1
Participants: 1