ON OFF スイッチボタン

on-off-switch.png

実装する方法は3つ。

  • TwAddVarCB(…,TW_TYPE_BOOLCPP,…);…ON,OFFの操作した時に、他にも付随してしたいことがあるならこれ。
  • TwAddVarRW(…,TW_TYPE_BOOLCPP,…);…一番簡単!
  • TwAddButton…..見た目上ONになったか、OFFになったかわからないからおすすめしない

実装してみた結果
switch.png
ソースコード

#include <iostream>
using namespace std;
#include <AntTweakBar.h>
#include <GL/freeglut.h>
#include <GL/glfw.h>
#include <stdio.h>
#pragma comment(lib, "GLFW.lib")
#pragma comment(lib, "AntTweakBar.lib")
#pragma comment(lib, "opengl32.lib")
 
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstdio>
 
#include <sstream>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#if !defined _MSC_VER
#   define _snprintf snprintf
#endif
bool readwrite=false;
int val=0;
 
//std::vector<std::string> g_BarTitles;
 
void OnDisplay(void)
{
    // Clear frame buffer
    glClearColor(0.0,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    cout<<"val="<<val<<endl;
    // App drawing here
    // ...
    cout<<"TwAddVarRW"<<readwrite<<endl;
    // Draw tweak bars
    TwDraw();
 
    // Present frame buffer
    glutSwapBuffers();
}
 
// Callback function called by GLUT when window size changes
void OnReshape(int width, int height)
{
    // Set OpenGL viewport
    glViewport(0, 0, width, height);
 
    // Send the new window size to AntTweakBar
    TwWindowSize(width, height);
}
 
// Function called at exit
void OnTerminate(void)
{ 
    // terminate AntTweakBar
    //    TwTerminate();
}
 
// Event callbacks
void OnMouseButton(int glutButton, int glutState, int mouseX, int mouseY)
{
    // send event to AntTweakBar
    if (TwEventMouseButtonGLUT(glutButton, glutState, mouseX, mouseY))
        glutPostRedisplay(); // request redraw if event has been handled
}
 
void OnMouseMotion(int mouseX, int mouseY)
{
    // send event to AntTweakBar
    if (TwEventMouseMotionGLUT(mouseX, mouseY))
        glutPostRedisplay(); // request redraw if event has been handled
}
 
void OnKeyboard(unsigned char glutKey, int mouseX, int mouseY)
{
    // send event to AntTweakBar
    if (TwEventKeyboardGLUT(glutKey, mouseX, mouseY))
        glutPostRedisplay(); // request redraw if event has been handled
}
 
void OnSpecial(int glutKey, int mouseX, int mouseY)
{
    // send event to AntTweakBar
    if (TwEventSpecialGLUT(glutKey, mouseX, mouseY))
        glutPostRedisplay(); // request redraw if event has been handled
}
 
void __stdcall SetCB(const void *value, void *clientData)
{
 
    *((bool*)clientData) = *(bool *)value; // copy value to g_AutoRotate
    cout<<"SetCB"<< *((bool*)clientData)<<endl;
 
}
void __stdcall GetCB(void *value, void *clientData)
{
    (void)clientData; // unused
    //int i=0;
    *(bool*)value =  *((bool*)clientData); // copy g_AutoRotate to value
    cout<<"GetCB"<<*((bool*)clientData)<<endl;
}
void __stdcall BottumCB(void* clientData){
    *((bool*)clientData)=!*((bool*)clientData);
    cout<<"ボタンを押しました!"<<*((bool*)clientData)<<endl;
}
 
int main(int argc, char *argv[]) 
{
    // Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutCreateWindow("AntTweakBar string example");
    glutCreateMenu(NULL);
 
    // Set GLUT callbacks
    glutDisplayFunc(OnDisplay);
    glutReshapeFunc(OnReshape);
    atexit(OnTerminate);  // Called after glutMainLoop ends
 
    // Initialize AntTweakBar
    TwInit(TW_OPENGL, NULL);
 
    glutMouseFunc(OnMouseButton);
    glutMotionFunc(OnMouseMotion);
    glutPassiveMotionFunc(OnMouseMotion);
    glutKeyboardFunc(OnKeyboard);
    glutSpecialFunc(OnSpecial);
    TwGLUTModifiersFunc(glutGetModifiers);
 
    // Create a tweak bar
    TwBar *bar = TwNewBar("Main");
    TwDefine(" Main label='callback' fontSize=3 position='180 16' size='270 440' valuesWidth=100 ");
 
    bool button=false;
    TwAddVarRW(bar,"TwAddVarRW",TW_TYPE_BOOLCPP,&readwrite,NULL);
    TwAddButton(bar,"TwAddButton",BottumCB,&button,NULL);
    TwAddVarCB(bar, "TwAddVarCB", TW_TYPE_BOOLCPP, SetCB, GetCB, &val,NULL);
 
    // Call the GLUT main loop
    glutMainLoop();
 
    return 0;
}

On/Offで部品の表示・非表示を切り替える

TwDefine(" mybar/extra visible=false ");  // variable 'extra' is hidden
TwDefine(" mybar/extra visible=true ");  // variable 'extra' is displayed again
TwDefine(" mybar/speed      readonly=false ");  // variable 'speed' is made Read-Write
TwDefine(" mybar/framerate  readonly=true ");   // variable 'framerate' is made Read-Only
Bibliography
サポートサイト Wikidot.com