Skip to content

Commit

Permalink
Vars: Support functions and add FunctionButton widget for activating …
Browse files Browse the repository at this point in the history
…closures etc.
  • Loading branch information
stevenlovegrove committed Jun 27, 2014
1 parent b14e932 commit 2e33718
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 10 deletions.
20 changes: 12 additions & 8 deletions examples/SimpleDisplay/main.cpp
Expand Up @@ -30,9 +30,9 @@ std::istream& operator>> (std::istream& is, CustomType& o){
return is;
}

void GlobalKeyHook()
void SampleMethod()
{
cout << "You pushed ctrl-r" << endl;
cout << "You typed ctrl-r or pushed reset" << endl;
}

int main( int /*argc*/, char* argv[] )
Expand Down Expand Up @@ -68,12 +68,6 @@ int main( int /*argc*/, char* argv[] )
pangolin::CreatePanel("ui")
.SetBounds(0.0, 1.0, 0.0, Attach::Pix(UI_WIDTH));

// Demonstration of how we can register a keyboard hook to alter a Var
pangolin::RegisterKeyPressCallback( PANGO_CTRL + 'b', SetVarFunctor<double>("ui.A Double", 3.5) );

// Demonstration of how we can register a keyboard hook to trigger a method
pangolin::RegisterKeyPressCallback( PANGO_CTRL + 'r', GlobalKeyHook );

// Safe and efficient binding of named variables.
// Specialisations mean no conversions take place for exact types
// and conversions between scalar types are cheap.
Expand All @@ -90,6 +84,16 @@ int main( int /*argc*/, char* argv[] )

Var<bool> record_teapot("ui.Record Teapot",false,false);

// boost::function / std::function objects can be used for Var's too.
// In C++11, these work great with closures.
Var<boostd::function<void(void)> > reset("ui.Reset", SampleMethod);

// Demonstration of how we can register a keyboard hook to alter a Var
pangolin::RegisterKeyPressCallback(PANGO_CTRL + 'b', SetVarFunctor<double>("ui.A Double", 3.5));

// Demonstration of how we can register a keyboard hook to trigger a method
pangolin::RegisterKeyPressCallback(PANGO_CTRL + 'r', SampleMethod);

// Default hooks for exiting (Esc) and fullscreen (tab).
while( !pangolin::ShouldQuit() )
{
Expand Down
22 changes: 22 additions & 0 deletions include/pangolin/type_convert.h
Expand Up @@ -31,6 +31,7 @@
#include <iostream>
#include <sstream>

#include <pangolin/compat/function.h>
#include <pangolin/compat/type_traits.h>

namespace pangolin
Expand All @@ -40,6 +41,27 @@ struct BadInputException : std::exception {
char const* what() const throw() { return "Failed to serialise type"; }
};

// Dummy methods to serialise functions / functors / lambdas etc
#ifdef CALLEE_HAS_CPP11
template<typename Ret, typename... Args>
std::istream& operator>>(std::istream& is, std::function<Ret(Args...)>& f) {
throw BadInputException();
}
template<typename Ret, typename... Args>
std::ostream& operator<<(std::ostream& os, const std::function<Ret(Args...)>& f) {
throw BadInputException();
}
#else
template<typename Ret, typename Arg>
std::istream& operator>>(std::istream& is, boostd::function<Ret(Args...)>& f) {
throw BadInputException();
}
template<typename Ret, typename Args>
std::ostream& operator<<(std::ostream& os, const boostd::function<Ret(Args...)>& f) {
throw BadInputException();
}
#endif

template<typename T, typename S, typename Enable=void>
struct Convert;

Expand Down
15 changes: 15 additions & 0 deletions include/pangolin/widgets.h
Expand Up @@ -31,6 +31,7 @@
#include <pangolin/view.h>
#include <pangolin/var/var.h>
#include <pangolin/handler.h>
#include <pangolin/compat/function.h>

namespace pangolin
{
Expand Down Expand Up @@ -76,6 +77,20 @@ struct PANGOLIN_EXPORT Button : public Widget<bool>
bool down;
};

struct PANGOLIN_EXPORT FunctionButton : public Widget<boostd::function<void(void)> >
{
FunctionButton(std::string title, VarValueGeneric& tv);
void Mouse(View&, MouseButton button, int x, int y, bool pressed, int mouse_state);
void Render();

//Cache params on resize
void ResizeChildren();
int text_width;
GLfloat raster[2];
Viewport vinside;
bool down;
};

struct PANGOLIN_EXPORT Checkbox : public Widget<bool>
{
Checkbox(std::string title, VarValueGeneric& tv);
Expand Down
45 changes: 43 additions & 2 deletions src/widgets.cpp
Expand Up @@ -161,8 +161,10 @@ void Panel::AddVariable(void* data, const std::string& name, VarValueGeneric& va
View* nv = NULL;
if( !strcmp(reg_type_name, typeid(bool).name()) ) {
nv = var.Meta().flags ? (View*)new Checkbox(title,var) : (View*)new Button(title,var);
}else if( !strcmp(reg_type_name, typeid(double).name()) || !strcmp(reg_type_name, typeid(float).name()) || !strcmp(reg_type_name, typeid(int).name()) || !strcmp(reg_type_name, typeid(unsigned int).name()) ) {
nv = new Slider(title,var);
} else if (!strcmp(reg_type_name, typeid(double).name()) || !strcmp(reg_type_name, typeid(float).name()) || !strcmp(reg_type_name, typeid(int).name()) || !strcmp(reg_type_name, typeid(unsigned int).name())) {
nv = new Slider(title, var);
} else if (!strcmp(reg_type_name, typeid(boostd::function<void(void)>).name() ) ) {
nv = (View*)new FunctionButton(title, var);
}else{
nv = new TextInput(title,var);
}
Expand Down Expand Up @@ -261,6 +263,45 @@ void Button::ResizeChildren()
vinside = v.Inset(border);
}

FunctionButton::FunctionButton(string title, VarValueGeneric& tv)
: Widget<boostd::function<void(void)> >(title, tv), down(false)
{
top = 1.0; bottom = Attach::Pix(-tab_h);
left = 0.0; right = 1.0;
hlock = LockLeft;
vlock = LockBottom;
text_width = glutBitmapLength(font, (unsigned char*)title.c_str());
}

void FunctionButton::Mouse(View&, MouseButton button, int x, int y, bool pressed, int mouse_state)
{
if (button == MouseButtonLeft)
{
down = pressed;
if (!pressed) {
var->Get()();
GuiVarChanged(*this);
}
}
}

void FunctionButton::Render()
{
glColor4fv(colour_fg);
glRect(v);
glColor4fv(colour_tx);
glRasterPos2f(raster[0], raster[1] - down);
glutBitmapString(font, (unsigned char*)title.c_str());
DrawShadowRect(v, down);
}

void FunctionButton::ResizeChildren()
{
raster[0] = v.l + (v.w - text_width) / 2.0f;
raster[1] = v.b + (v.h - text_height) / 2.0f;
vinside = v.Inset(border);
}

Checkbox::Checkbox(std::string title, VarValueGeneric& tv)
: Widget<bool>(title,tv)
{
Expand Down

0 comments on commit 2e33718

Please sign in to comment.