Commit 2fc21b19 authored by Lky's avatar Lky

init setup

parent 98bff820
#ifndef MenuItem_h
#define MenuItem_h
#pragma once
#include "ofMain.h"
class MenuItem
{
public:
MenuItem(std::string path, float _x, float _y, std::string _name)
{
name = _name;
if (ofLoadImage(tex, path))
{
if (tex.isAllocated())
{
draw_pos.x = _x;
draw_pos.y = _y;
float rect_x = draw_pos.x - (tex.getWidth() / 2);
float rect_y = draw_pos.y - (tex.getHeight() / 2);
hit_zone = ofRectangle(rect_x, rect_y, tex.getWidth(), tex.getHeight());
tex.setAnchorPercent(0.5, 0.5);
}
}
b_hovered = false;
b_selected = false;
}
~MenuItem()
{
if (tex.isAllocated())
{
tex.clear();
}
}
void draw()
{
if (!tex.isAllocated())
return;
ofPushStyle();
if (b_hovered) ofSetColor(ofColor::lightGrey);
else if (b_selected) ofSetColor(ofColor::orange);
else ofSetColor(ofColor::white);
tex.draw(draw_pos);
ofPopStyle();
}
bool inside(ofVec2f hit)
{
bool _hit = hit_zone.inside(hit);
if (_hit)
{
if (b_selected) return _hit;
else
{
b_hovered = true;
return _hit;
}
}
else
{
b_hovered = false;
return false;
}
}
void select()
{
b_selected = !b_selected;
b_hovered = !b_selected;
}
void unselect()
{
b_selected = false;
b_hovered = false;
}
std::string& getName()
{
return name;
}
private:
ofVec2f draw_pos;
ofTexture tex;
ofRectangle hit_zone;
std::string name;
bool b_hovered;
bool b_selected;
};
#endif /* MenuItem_h */
#ifndef ParamSliderItem_h
#define ParamSliderItem_h
#pragma once
#include "ofMain.h"
class ParamSliderItem
{
public:
ParamSliderItem(float _x, float _y, float _width, float _height, float _min, float _max, std::string _name)
{
name = _name;
draw_pos.x = _x;
draw_pos.y = _y;
float rect_x = draw_pos.x - (_width / 2);
float rect_y = draw_pos.y - (_height / 2);
hit_zone = ofRectangle(rect_x, rect_y, _width, _height);
min_val = _min;
max_val = _max;
b_selected = false;
b_hovered = false;
reset();
}
ParamSliderItem()
{
}
void draw()
{
ofPushStyle();
if (b_hovered) ofSetColor(ofColor::lightGrey);
else ofSetColor(ofColor::white);
float line_x = hit_zone.x;
float line_y = hit_zone.y + (hit_zone.height / 2);
/* Main transport line */
ofDrawLine(line_x, line_y, line_x+hit_zone.width, line_y);
/* Cursor */
ofSetLineWidth(3.0f);
ofDrawLine(line_x+(cur_pos*hit_zone.width), line_y-10, line_x+(cur_pos*hit_zone.width), line_y+10);
ofPopStyle();
}
bool inside(const ofVec2f& hit)
{
if (hit_zone.inside(hit))
{
if (b_selected) return true;
else
{
b_hovered = true;
return true;
}
}
else
{
b_hovered = false;
return false;
}
}
void select()
{
}
void unselect()
{
}
std::string& getName()
{
}
void mouseDown(const ofVec2f& _mouse)
{
cur_pos = (_mouse.x - hit_zone.x) / hit_zone.width;
cur_val = ofMap(cur_pos, 0.0f, 1.0f, min_val, max_val);
}
/*** ADDING THIS TO GET VALUES FROM ARDUINO */
void setValue(float val)
{
cur_val = val;
}
/*******/
float getValue()
{
return cur_val;
}
ofRectangle& getRect()
{
return hit_zone;
}
void reset()
{
cur_val = 1.0f;
cur_pos = ofMap(cur_val, min_val, max_val, 0.0f, 1.0f);
}
private:
ofVec2f draw_pos;
ofRectangle hit_zone;
float cur_pos;
float cur_val;
float min_val;
float max_val;
std::string name;
bool b_hovered;
bool b_selected;
};
#endif /* ParamSlider_h */
#ifndef RangeSliderItem_h
#define RangeSliderItem_h
#pragma once
#include "ofMain.h"
class RangeSliderItem
{
public:
RangeSliderItem(float _x, float _y, float _width, float _height, std::string _name)
{
name = _name;
draw_pos.x = _x;
draw_pos.y = _y;
float rect_x = draw_pos.x - (_width / 2);
float rect_y = draw_pos.y - (_height / 2);
hit_zone = ofRectangle(rect_x, rect_y, _width, _height);
b_selected = false;
b_hovered = false;
range_in = 0.f;
range_out = 1.f;
}
RangeSliderItem()
{
}
void draw()
{
ofPushStyle();
if (b_hovered) ofSetColor(ofColor::lightGrey);
else ofSetColor(ofColor::white);
float line_x = hit_zone.x;
float line_y = hit_zone.y + (hit_zone.height / 2);
/* Main transport line */
ofDrawLine(line_x, line_y, line_x+hit_zone.width, line_y);
/* Cursor */
ofSetLineWidth(3.0f);
ofDrawLine(line_x+cur_pos, line_y-10, line_x+cur_pos, line_y+10);
if (range_in > 0.0f || range_out < 1.0f)
{
/* Loop range */
ofSetColor(255,174,0,100);
float range_start = line_x + (hit_zone.width * range_in);
float range_end = hit_zone.width * (range_out-range_in);
ofDrawRectangle(range_start, line_y-10, range_end, 20);
}
ofPopStyle();
}
bool inside(const ofVec2f& hit)
{
if (hit_zone.inside(hit))
{
if (b_selected) return true;
else
{
b_hovered = true;
return true;
}
}
else
{
b_hovered = false;
return false;
}
}
void select()
{
}
void unselect()
{
}
std::string& getName()
{
}
void setPlayhead(float _cur_pos)
{
cur_pos = _cur_pos*hit_zone.width;
}
void mouseDown(const ofVec2f& _cursor)
{
cur_pos = (_cursor.x - (hit_zone.x + hit_zone.width * range_in)) / ((range_out - range_in) * hit_zone.width);
}
void setRangeIn(float _in)
{
range_in = (_in - hit_zone.x) / hit_zone.width;
if (range_in > range_out)
{
range_in = MAX(0.0f,range_out-0.1f);
}
}
void setRangeOut(float _out)
{
range_out = (_out - hit_zone.x) / hit_zone.width;
if (range_out < range_in)
{
range_out = MIN(1.0f,range_in+0.1f);
}
}
void clearRange()
{
range_in = 0.f;
range_out = 1.f;
}
float getValue()
{
return cur_pos;
}
ofVec2f getSelectedRange()
{
return ofVec2f(range_in, range_out);
}
ofRectangle& getRect()
{
return hit_zone;
}
private:
ofVec2f draw_pos;
ofRectangle hit_zone;
float cur_pos;
float range_in;
float range_out;
std::string name;
bool b_hovered;
bool b_selected;
};
#endif /* RangeSliderItem_h */
#include "ofMain.h"
#include "ofApp.h"
int main( )
{
ofGLWindowSettings settings;
settings.setSize(1024*2, 512*2);
settings.windowMode = OF_WINDOW;
settings.setGLVersion(4, 1);
ofCreateWindow(settings);
ofRunApp(new ofApp());
}
#include "ofApp.h"
static int prev_frame = 0;
static bool db = false;
//--------------------------------------------------------------
void ofApp::setup()
{
fbo.allocate(ofGetWidth(), ofGetHeight(), GL_RGB);
recording = false;
ofxTextureRecorder::Settings settings(fbo.getTexture());
settings.imageFormat = OF_IMAGE_FORMAT_JPEG;
settings.numThreads = 12;
settings.maxMemoryUsage = 9000000000;
recorder.setup(settings);
// INIT ARDUINO
ard.connect("COM4", 57600);
ofAddListener(ard.EInitialized, this, &ofApp::setupArduino);
bSetupArduino = false; // flag so we setup arduino when its ready, you don't need to touch this :)
// INIT VARIABLES
opacity_1 = 255;
opacity_2 = 255;
// GETTING FILES NAMES
ofDirectory dir;
dir.allowExt("hpv");
string input_dir = "hpv_files";
dir.listDir(input_dir); // could it be ||
for (int i = 0; i < dir.size(); ++i) {
string videoPath = dir.getPath(i);
video_names.push_back(videoPath);
}
// INIT HPV
HPV::InitHPVEngine();
HPV::AddEventListener(this, &ofApp::onHPVEvent);
b_draw_stats = b_draw_gui = true;
hpvPlayer.init(HPV::NewPlayer());
hpvChannel_2.init(HPV::NewPlayer());
if (hpvPlayer.load("equi_export.hpv"))//"bbb_export.hpv"))
{
hpvPlayer.setLoopState(OF_LOOP_NORMAL);
hpvPlayer.setDoubleBuffered(db);
hpvPlayer.play();
if (hpvPlayer.getFrameRate() > 60)
{
ofSetVerticalSync(false);
ofSetFrameRate(120);
}
else
{
ofSetVerticalSync(true);
}
}
if (hpvChannel_2.load("bbb_export.hpv"))
{
hpvChannel_2.setLoopState(OF_LOOP_NORMAL);
hpvChannel_2.setDoubleBuffered(db);
hpvChannel_2.play();
if (hpvChannel_2.getFrameRate() > 60)
{
ofSetVerticalSync(false);
ofSetFrameRate(120);
}
else
{
ofSetVerticalSync(true);
}
}
this->setupUI();
}
//--------------------------------------------------------------
void ofApp::update()
{
HPV::Update();
updateArduino();
range_slider->setPlayhead(hpvPlayer.getCurrentFrame()/(float)hpvPlayer.getTotalNumFrames());
//ofEnableBlendMode(OF_BLENDMODE_SCREEN);
fbo.begin();
ofSetColor(255,255,255,opacity_1);
hpvPlayer.draw(0,0,ofGetWidth(), ofGetHeight());
ofSetColor(255,255,255,opacity_1);
hpvChannel_2.draw(0,0,ofGetWidth(), ofGetHeight());
fbo.end();
if(recording){
recorder.save(fbo.getTexture());
}
//ofEnableBlendMode(OF_BLENDMODE_DISABLED);
}
//--------------------------------------------------------------
void ofApp::draw()
{
ofBackground(0);
ofSetWindowTitle(ofToString(ofGetFrameRate()));
fbo.draw(0,0);
if (b_draw_gui)
{
float y = ofGetHeight() - 150;
ofPushStyle();
ofSetColor(125);
ofDrawLine(0, y, ofGetWidth(), y);
ofSetColor(0, 0, 0, 100);
ofDrawRectangle(0, y, ofGetWidth(), y);
ofPopStyle();
for (auto& item : ui_items)
{
item.second->draw();
}
speed_slider->draw();
range_slider->draw();
gui_font.drawStringShadowed("speed\n(middle mouse to reset)", (ofGetWidth() / 2) + 310, speed_slider->getRect().y + (speed_slider->getRect().height/2) + 3, ofColor(225));
gui_font.drawStringShadowed("scrub + range\npress 'b' and 'n' while clicking on the\nrange to define loop points\n(middle mouse to reset)", (ofGetWidth() / 2) + 310, range_slider->getRect().y + (range_slider->getRect().height/2) + 3, ofColor(225));
gui_font.drawStringShadowed(hpvPlayer.getFileSummary(), (ofGetWidth() / 2) - 250, range_slider->getRect().y + 60, ofColor(225));
}
if (b_draw_stats)
{
std::stringstream ss;
ss << "DOUBLE BUFFER: " << (db ? "ON" : "OFF")
<< " ('d' toggles)"
<< std::endl
<< "Val-0:speed:: " << potar_0
<< std::endl
<< "Val-1:opacity_channel_1:: " << potar_1
<< std::endl
<< "Val-2:opacity_channel_2:: " << potar_2
<< std::endl
<< "Val-4: " << potar_4
<< std::endl
<< "Val-10: " << potar_10
<< std::endl
<< "Val-11: " << potar_11
// << std::endl
// << "HDD: " << hpvPlayer.getDecodeStatsPtr()->hdd_read_time / 1e6 << "ms"
// << std::endl
// << "L4Z: " << hpvPlayer.getDecodeStatsPtr()->l4z_decode_time / 1e6 << "ms"
// << std::endl
// << "GPU: " << hpvPlayer.getDecodeStatsPtr()->gpu_upload_time / 1e6 << "ms"
// << std::endl
// << "TOT: " << (hpvPlayer.getDecodeStatsPtr()->hdd_read_time+hpvPlayer.getDecodeStatsPtr()->l4z_decode_time+hpvPlayer.getDecodeStatsPtr()->gpu_upload_time) / 1e6
<< std::endl
<< "FRAME: " << hpvPlayer.getCurrentFrame()
<< std::endl
<< "SPEED: " << hpvPlayer.getSpeed()
<< std::endl
<< "DURATION: " << hpvPlayer.getDuration()
<< std::endl
<< "DONE: " << hpvPlayer.getIsMovieDone();
gui_font.drawStringShadowed(ss.str(), ofVec2f(25,ofGetHeight()-130));
}
}
//--------------------------------------------------------------
void ofApp::exit()
{
HPV::DestroyHPVEngine();
}
//--------------------------------------------------------------
void ofApp::setupUI()
{
float half_width = ofGetWidth() / 2;
MenuItem * play = new MenuItem("ui/button_play.png", half_width - 270, ofGetHeight()-100, "PLAY");
MenuItem * pause = new MenuItem("ui/button_pause.png", half_width - 210, ofGetHeight()-100, "PAUSE");
MenuItem * stop = new MenuItem("ui/button_stop.png", half_width - 150, ofGetHeight()-100, "STOP");
MenuItem * single_play = new MenuItem("ui/button_single.png", half_width - 70, ofGetHeight()-100, "SINGLE");
MenuItem * loop_normal = new MenuItem("ui/button_loop.png", half_width + 30, ofGetHeight()-100, "LOOP");
MenuItem * loop_palindrome = new MenuItem("ui/button_palindrome.png", half_width + 130, ofGetHeight()-100, "PALINDROME");
ui_items["PLAY"] = play;
ui_items["STOP"] = stop;
ui_items["PAUSE"] = pause;
ui_items["SINGLE"] = single_play;
ui_items["LOOP"] = loop_normal;
ui_items["PALINDROME"] = loop_palindrome;
range_slider = new RangeSliderItem(ofGetWidth()/2, ofGetHeight()-50, 600, 50, "RANGE_SLIDER");
speed_slider = new ParamSliderItem(half_width + 240, ofGetHeight()-100, 100, 50, -10.0f, 10.0f, "SPEED_SLIDER");
gui_font.load("ui/DIN.otf", 9);
play->select();
loop_normal->select();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == 'f')
{
ofToggleFullscreen();
}
else if (key == 's')
{
b_draw_stats = !b_draw_stats;
}
else if (key == 'g')
{
b_draw_gui = !b_draw_gui;
}
else if (key == 'r')
{
hpvPlayer.firstFrame();
if (!hpvPlayer.isPlaying()) hpvPlayer.play();
}
else if (key == '+')
{
hpvPlayer.nextFrame();
}
else if (key == '-')
{
hpvPlayer.previousFrame();
}
else if (key == 'd')
{
db = !db;
hpvPlayer.setDoubleBuffered(db);
}
else if (key == 'P')
{
cout << "recording" << endl;
recording = !recording;
}
else if (key == 'b')
{
hpvPlayer.close();
int rand = (int)ofRandom(video_names.size()-1);
cout << "got val " << rand << endl;
if (hpvPlayer.load(video_names[rand]))
{
hpvPlayer.setLoopState(OF_LOOP_NORMAL);
hpvPlayer.setDoubleBuffered(db);
hpvPlayer.play();
if (hpvPlayer.getFrameRate() > 60)
{
ofSetVerticalSync(false);
ofSetFrameRate(120);
}
else
{
ofSetVerticalSync(true);
}
}
}
}
void ofApp::onHPVEvent(const HPVEvent& event)
{
switch (event.type)
{
case HPV::HPVEventType::HPV_EVENT_PLAY:
cout << "'" << event.player->getFilename() << "': play event" << endl;
break;
case HPV::HPVEventType::HPV_EVENT_PAUSE:
cout << "'" << event.player->getFilename() << "': pause event" << endl;
break;
case HPV::HPVEventType::HPV_EVENT_RESUME:
cout << "'" << event.player->getFilename() << "': resume event" << endl;
break;
case HPV::HPVEventType::HPV_EVENT_STOP:
cout << "'" << event.player->getFilename() << "': stop event" << endl;
ui_items["PLAY"]->unselect();
ui_items["PAUSE"]->unselect();
ui_items["STOP"]->select();
break;
case HPV::HPVEventType::HPV_EVENT_LOOP:
cout << "'" << event.player->getFilename() << "': loop event" << endl;
break;
case HPV::HPVEventType::HPV_EVENT_NUM_TYPES:
default:
break;
}
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
ofVec2f mouse(x,y);
for (auto& item : ui_items)
{
item.second->inside(mouse);
}
range_slider->inside(mouse);
speed_slider->inside(mouse);
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button)
{
ofVec2f mouse(x,y);
if (range_slider->inside(mouse))
{
range_slider->mouseDown(mouse);
int frame = hpvPlayer.getLoopIn() + (range_slider->getValue() * (hpvPlayer.getLoopOut()-hpvPlayer.getLoopIn()));
if (frame != prev_frame)
{
hpvPlayer.seekToFrame(frame);
prev_frame = frame;
}
}
if (speed_slider->inside(mouse))
{
speed_slider->mouseDown(mouse);
if (ofGetMousePressed(OF_MOUSE_BUTTON_MIDDLE) || ofGetKeyPressed('c'))
{
speed_slider->reset();
}
hpvPlayer.setSpeed(speed_slider->getValue());
}
}