Created
January 31, 2012 06:11
-
-
Save tatsuro-ueda/1709197 to your computer and use it in GitHub Desktop.
simple-OpenNI sample Slider2d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 手を上下左右に動かすことで格子状のパネルにタッチする | |
import SimpleOpenNI.*; | |
SimpleOpenNI context; | |
// NITEモジュール | |
XnVSessionManager sessionManager; | |
XnVSelectableSlider2D trackPad; | |
int gridX = 7; | |
int gridY = 5; | |
Trackpad trackPadViz; | |
void setup() | |
{ | |
context = new SimpleOpenNI(this,SimpleOpenNI.RUN_MODE_MULTI_THREADED); | |
// デフォルトでは鏡像を有効にする | |
context.setMirror(true); | |
// 深度カメラを有効にする | |
if(context.enableDepth() == false) | |
{ | |
println("Can't open the depthMap, maybe the camera is not connected!"); | |
exit(); | |
return; | |
} | |
// 手の検出とジェスチャー検出を有効にする | |
context.enableGesture(); | |
context.enableHands(); | |
// NITEを準備する | |
sessionManager = context.createSessionManager("Click,Wave", "RaiseHand"); | |
trackPad = new XnVSelectableSlider2D(gridX,gridY); | |
sessionManager.AddListener(trackPad); | |
trackPad.RegisterItemHover(this); | |
trackPad.RegisterValueChange(this); | |
trackPad.RegisterItemSelect(this); | |
trackPad.RegisterPrimaryPointCreate(this); | |
trackPad.RegisterPrimaryPointDestroy(this); | |
// 視覚GUIを準備する | |
trackPadViz = new Trackpad(new PVector(context.depthWidth()/2, context.depthHeight()/2,0), | |
gridX,gridY,50,50,15); | |
size(context.depthWidth(), context.depthHeight()); | |
smooth(); | |
// テキスト情報 | |
println("-------------------------------"); | |
println("1. Wave till the tiles get green"); | |
println("2. The relative hand movement will select the tiles"); | |
println("-------------------------------"); | |
} | |
void draw() | |
{ | |
// カメラ情報を更新する | |
context.update(); | |
// NITE情報を更新する | |
context.update(sessionManager); | |
// 深度マップを描画する | |
image(context.depthImage(),0,0); | |
trackPadViz.draw(); | |
} | |
void keyPressed() | |
{ | |
switch(key) | |
{ | |
case 'e': | |
// セッションを終了する | |
sessionManager.EndSession(); | |
println("end session"); | |
break; | |
} | |
} | |
///////////////////////////////////////////////////////////////////////////////////////////////////// | |
// セッションのコールバック | |
void onStartSession(PVector pos) | |
{ | |
println("onStartSession: " + pos); | |
} | |
void onEndSession() | |
{ | |
println("onEndSession: "); | |
} | |
void onFocusSession(String strFocus,PVector pos,float progress) | |
{ | |
println("onFocusSession: focus=" + strFocus + ",pos=" + pos + ",progress=" + progress); | |
} | |
///////////////////////////////////////////////////////////////////////////////////////////////////// | |
// XnVSelectableSlider2Dのコールバック | |
void onItemHover(int nXIndex,int nYIndex) | |
{ | |
println("onItemHover: nXIndex=" + nXIndex +" nYIndex=" + nYIndex); | |
trackPadViz.update(nXIndex,nYIndex); | |
} | |
void onValueChange(float fXValue,float fYValue) | |
{ | |
// println("onValueChange: fXValue=" + fXValue +" fYValue=" + fYValue); | |
} | |
void onItemSelect(int nXIndex,int nYIndex,int eDir) | |
{ | |
println("onItemSelect: nXIndex=" + nXIndex + " nYIndex=" + nYIndex + " eDir=" + eDir); | |
trackPadViz.push(nXIndex,nYIndex,eDir); | |
} | |
void onPrimaryPointCreate(XnVHandPointContext pContext,XnPoint3D ptFocus) | |
{ | |
println("onPrimaryPointCreate"); | |
trackPadViz.enable(); | |
} | |
void onPrimaryPointDestroy(int nID) | |
{ | |
println("onPrimaryPointDestroy"); | |
trackPadViz.disable(); | |
} | |
///////////////////////////////////////////////////////////////////////////////////////////////////// | |
// Trackpad | |
class Trackpad | |
{ | |
int xRes; | |
int yRes; | |
int width; | |
int height; | |
boolean active; | |
PVector center; | |
PVector offset; | |
int space; | |
int focusX; | |
int focusY; | |
int selX; | |
int selY; | |
int dir; | |
Trackpad(PVector center,int xRes,int yRes,int width,int height,int space) | |
{ | |
this.xRes = xRes; | |
this.yRes = yRes; | |
this.width = width; | |
this.height = height; | |
active = false; | |
this.center = center.get(); | |
offset = new PVector(); | |
offset.set(-(float)(xRes * width + (xRes -1) * space) * .5f, | |
-(float)(yRes * height + (yRes -1) * space) * .5f, | |
0.0f); | |
offset.add(this.center); | |
this.space = space; | |
} | |
void enable() | |
{ | |
active = true; | |
focusX = -1; | |
focusY = -1; | |
selX = -1; | |
selY = -1; | |
} | |
void update(int indexX,int indexY) | |
{ | |
focusX = indexX; | |
focusY = (yRes-1) - indexY; | |
} | |
void push(int indexX,int indexY,int dir) | |
{ | |
selX = indexX; | |
selY = (yRes-1) - indexY; | |
this.dir = dir; | |
} | |
void disable() | |
{ | |
active = false; | |
} | |
void draw() | |
{ | |
pushStyle(); | |
pushMatrix(); | |
translate(offset.x,offset.y); | |
for(int y=0;y < yRes;y++) | |
{ | |
for(int x=0;x < xRes;x++) | |
{ | |
if(active && (selX == x) && (selY == y)) | |
{ // selected object | |
fill(100,100,220,190); | |
strokeWeight(3); | |
stroke(100,200,100,220); | |
} | |
else if(active && (focusX == x) && (focusY == y)) | |
{ // focus object | |
fill(100,255,100,220); | |
strokeWeight(3); | |
stroke(100,200,100,220); | |
} | |
else if(active) | |
{ // normal | |
strokeWeight(3); | |
stroke(100,200,100,190); | |
noFill(); | |
} | |
else | |
{ | |
strokeWeight(2); | |
stroke(200,100,100,60); | |
noFill(); | |
} | |
rect(x * (width + space),y * (width + space),width,height); | |
} | |
} | |
popMatrix(); | |
popStyle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment