Skip to content

Instantly share code, notes, and snippets.

@elliotwoods
Created March 1, 2014 07:20
Show Gist options
  • Save elliotwoods/9286438 to your computer and use it in GitHub Desktop.
Save elliotwoods/9286438 to your computer and use it in GitHub Desktop.
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofxAssets::AssetRegister.addAddon("ofxCvGui");
ofSetWindowShape(512, 512);
ofSetFrameRate(60.0f);
this->x = 50.0f;
this->min = 0.0f;
this->max = 100.0f;
this->caption = "Tilt";
this->units = "%";
this->zoom = 1.0f;
this->startMouseHoldTime = 0;
this->mouseHoldState = None;
{
auto & marker = this->marker;
marker.addVertex(-5, -10);
marker.addVertex(-5, -5);
marker.addVertex(0, 0);
marker.addVertex(+5, -5);
marker.addVertex(+5, -10);
marker.bezierTo(+5, -25, +5, -25, 0, -25);
marker.bezierTo(-5, -25, -5, -25, -5, -10);
marker.close();
marker.setClosed(true);
}
{
auto & ticks = this->ticks;
for(float x = 0.0f; x<= 1.0f; x+= 0.01f) {
ticks.addVertex(ofVec3f(x, 0.0f, 0.0f));
}
ticks.setMode(OF_PRIMITIVE_POINTS);
}
this->width = 300.0f;
this->editBounds = ofRectangle(width - 16,1,15, 15);
}
//--------------------------------------------------------------
void ofApp::update(){
this->x = ofClamp(this->x, this->min, this->max);
if(this->mouseHoldState == Holding) {
const auto mouseHoldTime = float(ofGetElapsedTimeMillis() - startMouseHoldTime) / 1000.0f;
if (mouseHoldTime > 1.0f) {
zoom = mouseHoldTime;
}
} else if (this->mouseHoldState == None) {
zoom = (zoom - 1.0f) * 0.9f + 1.0f;
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofBackground(50);
auto & font = ofxAssets::AssetRegister.getFont("ofxCvGui::swisop3", 12);
ofxAssets::AssetRegister.getImage("ofxCvGui::edit").draw(this->editBounds);
font.drawString(this->caption + " : " + ofToString(x) + this->units, 5, 15);
const auto center = this->startMouseHoldX;
const auto centerPx = ofMap(center, this->min, this->max, 0, this->width);
const auto xPx = (this->x - center) * (zoom * width) / (this->max - this->min)+ centerPx;
ofPushMatrix();
ofTranslate(0.0f, 40.0f);
ofPushMatrix();
ofTranslate(xPx, 0.0f);
this->marker.draw();
ofPopMatrix();
ofTranslate(+centerPx, 0.0f);
ofScale(zoom, 1.0f);
ofTranslate(-centerPx, 0.0f);
ofScale(width, 1.0f);
this->ticks.draw();
ofPopMatrix();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
this->mouseHoldState = Dragging;
float dNormX = (x - this->startMouseHoldMouseX) / (this->width * zoom);
this->x = dNormX * (this->max - this->min) + startMouseHoldX;
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
if (y > 15) {
if (ofGetElapsedTimeMillis() - startMouseHoldTime < 500) {
this->x = ofMap(x, 0, this->width, this->min, this->max, true);
}
this->startMouseHoldTime = ofGetElapsedTimeMillis();
this->startMouseHoldX = this->x;
this->startMouseHoldMouseX = x;
this->mouseHoldState = Holding;
} else if (this->editBounds.inside(x, y)) {
auto result = ofSystemTextBoxDialog(this->caption + " (" + ofToString(this->x) + ")");
if (!result.empty()) {
this->x = ofToFloat(result);
}
}
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
this->mouseHoldState = None;
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment