Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created December 29, 2013 14:19
Show Gist options
  • Save roxlu/8170860 to your computer and use it in GitHub Desktop.
Save roxlu/8170860 to your computer and use it in GitHub Desktop.
Minimal AntTweakBar integration with GLFW
#include <assert.h>
#include <GLFW/glfw3.h>
#include <swnt/GUI.h>
GUI::GUI()
:win_w(0)
,win_h(0)
{
}
bool GUI::setup(int w, int h) {
assert(w);
assert(h);
win_w = w;
win_h = h;
TwInit(TW_OPENGL_CORE, NULL);
bar = TwNewBar("SWNT");
TwAddVarRW(bar, "speed", TW_TYPE_DOUBLE, &test, "min=0 max=10 step=0.1 help='test'");
TwWindowSize(win_w, win_h);
return true;
}
void GUI::onResize(int w, int h) {
win_w = w;
win_h = h;
TwWindowSize(w, h);
}
void GUI::onMouseMoved(double x, double y) {
TwMouseMotion(x, y);
}
void GUI::onMouseClicked(int bt, int action) {
TwMouseButtonID btn = (bt == 0) ? TW_MOUSE_LEFT : TW_MOUSE_RIGHT;
TwMouseAction ma = (action == GLFW_PRESS) ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
TwMouseButton(ma, btn);
}
void GUI::onKeyPressed(int key, int mod) {
switch(key) {
case GLFW_KEY_LEFT: key = TW_KEY_LEFT; break;
case GLFW_KEY_RIGHT: key = TW_KEY_RIGHT; break;
case GLFW_KEY_UP: key = TW_KEY_UP; break;
case GLFW_KEY_DOWN: key = TW_KEY_DOWN; break;
default: break;
}
int tw_mod = TW_KMOD_NONE;
if(mod & GLFW_MOD_SHIFT) {
tw_mod |= TW_KMOD_SHIFT;
}
if(mod & GLFW_MOD_CONTROL) {
tw_mod |= TW_KMOD_CTRL;
}
if(mod & GLFW_MOD_ALT) {
tw_mod |= TW_KMOD_ALT;
}
TwKeyPressed(key, TW_KMOD_NONE);
}
void GUI::draw() {
TwDraw();
}
#ifndef SWNT_GUI_H
#define SWNT_GUI_H
#include <stdio.h>
#include <AntTweakBar.h>
class GUI {
public:
GUI();
bool setup(int w, int h);
void onMouseMoved(double x, double y);
void onMouseClicked(int bt, int action);
void onKeyPressed(int key, int mod);
void onResize(int w, int h);
void draw();
public:
int win_w;
int win_h;
TwBar* bar;
double test;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment