c_cpp 简单的ofEvent,ofNotifyEvent

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 简单的ofEvent,ofNotifyEvent相关的知识,希望对你有一定的参考价值。

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{
    
public:
    void setup();
    void update();
    void draw();
    
    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y );
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
    
    
    ofVec2f pos;
    ofVec2f size;
    bool positionIn;
    
    ofEvent<bool> inSide;
    void onRect( bool & e );
    
    

};
#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    
    ofRectangle test;
    
    pos = ofVec2f( 300, 300 );
    size = ofVec2f( 200, 200 );
    

    ofAddListener(inSide, this, &ofApp::onRect);

}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){

    if (positionIn) {
        ofSetColor(255,0,0);
    } else {
        ofSetColor(0,0,255);
    }
    ofRect(pos, size.x, size.y);
    

}

void ofApp::onRect(bool & e){
    
    cout << "bang" << endl;
    ofRemoveListener(inSide, this, &ofApp::onRect);

}



//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

    if (((x>pos.x)&&(x<pos.x+size.x))&&((y>pos.y)&&(y<pos.y+size.y))) {
        positionIn = true;
        ofNotifyEvent(inSide, positionIn, this);
    } else {
        positionIn = false;
        ofAddListener(inSide, this, &ofApp::onRect);
    }

    
}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}

以上是关于c_cpp 简单的ofEvent,ofNotifyEvent的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp c ++的简单模板

c_cpp 简单的触发器

c_cpp C中的简单示例

c_cpp 简单的位置检测器

c_cpp 简单的外壳在C中

c_cpp 一个简单的@keypath marco