Skip to content

Instantly share code, notes, and snippets.

@jvcleave
Created January 27, 2018 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvcleave/47fc93217a24474abc787dbf5facecb2 to your computer and use it in GitHub Desktop.
Save jvcleave/47fc93217a24474abc787dbf5facecb2 to your computer and use it in GitHub Desktop.
#pragma once
#include "ofMain.h"
#include "ofxImGui.h"
class ListBoxContent
{
public:
ListBoxContent()
{
label = "";
currentIndex = 0;
heightInLines = 0;
items = NULL;
}
~ListBoxContent()
{
if(items)
{
delete items;
items = NULL;
}
clearItemCollection();
}
void addItems(vector<string>& items_)
{
for(size_t i=0; i<items_.size(); i++)
{
addItem(items_[i].c_str());
}
}
void addItems(vector<const char *>& items_)
{
for(size_t i=0; i<items_.size(); i++)
{
addItem(items_[i]);
}
}
void clearItemCollection()
{
for(size_t i=0; i<itemCollection.size(); i++)
{
if(itemCollection[i])
{
//delete itemCollection[i];
itemCollection[i] = NULL;
}
}
itemCollection.clear();
}
void addItem(const char * itemName)
{
int currentSize = itemCollection.size();
itemCollection[currentSize] = itemName;
if(items)
{
delete items;
items = NULL;
}
items = (const char**)malloc(sizeof(const char*) * itemCollection.size());
for(int i=0; i<itemCollection.size(); i++)
{
items[i] = itemCollection[i];
}
}
bool draw()
{
if(!items) return;
bool result = ImGui::ListBox(label.c_str(),
&currentIndex,
items,
itemCollection.size(),
heightInLines);
if(result)
{
ofLogVerbose() << itemCollection[currentIndex];
}
return result;
}
string label;
int currentIndex;
int heightInLines;
const char** items;
map< int, const char *> itemCollection;
};
#include "ofMain.h"
#include "ofApp.h"
#if (OF_VERSION_MINOR != 9) && defined(TARGET_OPENGLES)
#include "ofGLProgrammableRenderer.h"
#endif
//#define FORCE_PROGRAMMMABLE 1
#ifdef FORCE_PROGRAMMMABLE
#include "ofGLProgrammableRenderer.h"
#endif
int main()
{
ofSetLogLevel(OF_LOG_VERBOSE);
#if defined(TARGET_OPENGLES)
#if (OF_VERSION_MINOR == 9)
ofGLESWindowSettings settings;
settings.width = 1280;
settings.height = 720;
settings.setGLESVersion(2);
ofCreateWindow(settings);
#else
ofSetLogLevel("ofThread", OF_LOG_ERROR);
ofSetCurrentRenderer(ofGLProgrammableRenderer::TYPE);
ofSetupOpenGL(1280, 720, OF_WINDOW);
#endif
#else
#ifdef FORCE_PROGRAMMMABLE
ofGLWindowSettings glWindowSettings;
glWindowSettings.width = 1280;
glWindowSettings.height = 720;
glWindowSettings.setGLVersion(3, 2);
ofCreateWindow(glWindowSettings);
#else
ofSetLogLevel("ofThread", OF_LOG_ERROR);
ofSetupOpenGL(1280, 720, OF_WINDOW);
#endif
#endif
ofRunApp( new ofApp());
}
#include "ofApp.h"
#define TEX_ID (ImTextureID)(uintptr_t)
vector<string> strings;
//--------------------------------------------------------------
void ofApp::setup()
{
//ofLogVerbose() << SRCROOT;
ofSetLogLevel(OF_LOG_VERBOSE);
//required call
gui.setup();
ImGui::GetIO().MouseDrawCursor = false;
//backgroundColor is stored as an ImVec4 type but can handle ofColor
backgroundColor = ofColor(114, 144, 154);
show_test_window = true;
show_another_window = false;
floatValue = 0.0f;
imageButtonID = gui.loadImage("of.png");
textureButtonID = gui.loadTexture(textureButton, "of.png");
ofLoadImage(textureButtonPreAllocated, "of.png");
//textureButtonPreAllocatedID = gui.loadTexture(textureButtonPreAllocated);
listBoxContent.currentIndex = 1;
listBoxContent.label = "LISTBOX LABEL";
listBoxContent.heightInLines = 4;
listBoxContent.addItem("Apple");
listBoxContent.addItem("Banana");
listBoxContent.addItem("Cherry");
listBoxContent.addItem("Kiwi");
listBoxContent.addItem("Mango");
vector<const char *> additionalItems = {"Orange", "Pineapple", "Strawberry", "Watermelon"};
strings.push_back("Orange2");
strings.push_back("Pineapple2");
listBoxContent.addItems(additionalItems);
listBoxContent.addItems(strings);
videoButtonID = -1;
videoPlayer.load("fingers.mov");
videoPlayer.play();
floatParam.set("test", (float)10.f);
doShowTestWindow.set("", true);
ofFbo::Settings fboSettings;
fboSettings.width = videoPlayer.getWidth();
fboSettings.height = videoPlayer.getHeight();
fboSettings.internalformat = GL_RGBA;
fboSettings.textureTarget = GL_TEXTURE_2D;
videoFBO.allocate(fboSettings);
videoButtonID = videoFBO.getTexture().texData.textureID;
}
bool doSetTheme = false;
//--------------------------------------------------------------
void ofApp::update(){
videoPlayer.update();
videoFBO.begin();
videoPlayer.draw(0, 0);
videoFBO.end();
if(doSetTheme)
{
doSetTheme = false;
gui.setTheme(new ThemeTest());
}
}
bool ColorEdit3(const char* label, ImVec4& color)
{
return ImGui::ColorEdit3(label, (float*)&color);
}
bool doThemeColorsWindow = false;
//--------------------------------------------------------------
void ofApp::draw(){
//backgroundColor is stored as an ImVec4 type but is converted to ofColor automatically
ofSetBackgroundColor(backgroundColor);
textureButton.draw(0, 0);
int w = videoPlayer.getWidth();
videoFBO.draw(w, 0);
videoPlayer.draw(w*2, 0);
videoFBO.getTexture().draw(w*3, 0);
//required to call this at beginning
gui.begin();
if(listBoxContent.draw())
{
ofLogVerbose() << "listBoxContent changed to " << listBoxContent.currentIndex;
}
bool pressed = ImGui::ImageButton(TEX_ID videoButtonID, ImVec2(200, 141));
pressed = ImGui::ImageButton(TEX_ID imageButtonID, ImVec2(200, 200));
pressed = ImGui::ImageButton(TEX_ID videoButtonID, ImVec2(200, 200));
//pressed = ImGui::ImageButton(TEX_ID textureButtonPreAllocatedID, ImVec2(200, 200));
//In between gui.begin() and gui.end() you can use ImGui as you would anywhere else
// 1. Show a simple window
{
ImGui::Text("Hello, world!");
ImGui::SliderFloat("Float", &floatValue, 0.0f, 1.0f);
ImGui::SliderFloat("floatParam", &floatParam.value, 0.0f, 1.0f);
//this will change the app background color
//ImGui::ColorEdit3("Background Color", (float*)&backgroundColor);
ColorEdit3("Background Color", backgroundColor);
if(ImGui::Button("Test Window"))
{
doShowTestWindow.value = !doShowTestWindow.value;
}
if (ImGui::Button("Another Window"))
{
//bitwise OR
show_another_window ^= 1;
}
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
}
// 2. Show another window, this time using an explicit ImGui::Begin and ImGui::End
if (show_another_window)
{
//note: ofVec2f and ImVec2f are interchangeable
ImGui::SetNextWindowSize(ofVec2f(200,100), ImGuiSetCond_FirstUseEver);
ImGui::Begin("Another Window", &show_another_window);
ImGui::Text("Hello");
ImGui::End();
}
// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
if (doShowTestWindow.value)
{
ImGui::SetNextWindowPos(ofVec2f(650, 20), ImGuiSetCond_FirstUseEver);
ImGui::ShowTestWindow(&doShowTestWindow.value);
}
if(doThemeColorsWindow)
{
gui.openThemeColorWindow();
}
//required to call this at end
gui.end();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
ofLogVerbose(__FUNCTION__) << key;
switch (key)
{
case 't' :
{
doThemeColorsWindow = !doThemeColorsWindow;
break;
}
case 'c' :
{
doSetTheme = !doSetTheme;
break;
}
case 's':
{
break;
}
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
ofLogVerbose(__FUNCTION__) << key;
}
void ofApp::mouseScrolled(float x, float y)
{
ofLogVerbose(__FUNCTION__) << "x: " << x << " y: " << y;
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y){
}
//--------------------------------------------------------------
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){
}
#pragma once
#include "ofMain.h"
#include "ofxImGui.h"
#include "ThemeTest.h"
#include "ListBoxContent.h"
#include "SyncedParameter.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);
void mouseScrolled(float x, float y);
ofxImGui gui;
ImVec4 backgroundColor;
bool show_test_window;
bool show_another_window;
float floatValue;
GLuint imageButtonID;
GLuint textureButtonID;
GLuint textureButtonPreAllocatedID;
ofTexture textureButton;
ofTexture textureButtonPreAllocated;
ListBoxContent listBoxContent;
ofVideoPlayer videoPlayer;
GLuint videoButtonID;
SyncedParameter<float> floatParam;
SyncedParameter<bool> doShowTestWindow;
ofFbo videoFBO;
};
#pragma once
#include "ofMain.h"
template<typename T>
class SyncedParameter
{
public:
ofParameter<T> parameter;
T value;
string name;
SyncedParameter()
{
T temp;
value = temp;
name = "UNDEFINED";
}
void onChange(T& paramValue)
{
ofLogVerbose() << name << " changed " << paramValue << endl;
}
void set(string name_, T value_, bool doUpdateEvent=true)
{
if(!name_.empty())
{
name = name_;
}
value = value_;
parameter.set(name, value);
parameter.addListener(this, &SyncedParameter::onChange);
if(doUpdateEvent)
{
ofAddListener(ofEvents().update, this, &SyncedParameter::onUpdate);
}
}
void onUpdate(ofEventArgs& event)
{
update();
}
void update()
{
if(value != parameter)
{
parameter = value;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment