Vec2f Node2D::screenToParent( const Vec2f &pt ) const { Vec2f p = pt; Node2DRef node = getParent<Node2D>(); if(node) p = node->screenToObject(p); return p; }
Vec2f Node2D::parentToScreen( const Vec2f &pt ) const { Vec2f p = pt; Node2DRef node = getParent<Node2D>(); if(node) p = node->objectToScreen(p); return p; }
vec2 Node2D::screenToParent( const vec2 &pt ) const { vec2 p = pt; Node2DRef node = getParent<Node2D>(); if( node ) p = node->screenToObject( p ); return p; }
vec2 Node2D::parentToScreen( const vec2 &pt ) const { vec2 p = pt; Node2DRef node = getParent<Node2D>(); if( node ) p = node->objectToScreen( p ); return p; }
void SimpleSceneGraphApp::setup() { // create the root node mRoot = Node2DRef( new Node2D() ); // create a large rectangle first mParent = NodeRectangleRef( new NodeRectangle() ); // specify the position of the anchor point on our canvas mParent->setPosition(400, 300); // relative to parent node // we can easily set the anchor point to its center mParent->setAnchorPercentage(0.5f, 0.5f); // set the size of the node mParent->setSize(600, 450); // add it to the root of our scenegraph mRoot->addChild(mParent); // add smaller rectangles to the root node NodeRectangleRef child1( new NodeRectangle() ); child1->setPosition(200, 225); // relative to parent node child1->setAnchorPercentage(0.5f, 0.5f); child1->setSize(240, 200); mParent->addChild(child1); NodeRectangleRef child2( new NodeRectangle() ); child2->setPosition(400, 225); // relative to parent node child2->setAnchorPercentage(0.5f, 0.5f); child2->setSize(240, 200); mParent->addChild(child2); // add even smaller rectangles to the child rectangles NodeRectangleRef child( new NodeRectangle() ); child->setPosition(60, 100); // relative to parent node child->setAnchorPercentage(0.5f, 0.5f); child->setSize(100, 100); child1->addChild(child); child.reset( new NodeRectangle() ); child->setPosition(180, 100); // relative to parent node child->setAnchorPercentage(0.5f, 0.5f); child->setSize(100, 100); child1->addChild(child); child.reset( new NodeRectangle() ); child->setPosition(60, 100); // relative to parent node child->setAnchorPercentage(0.5f, 0.5f); child->setSize(100, 100); child2->addChild(child); child.reset( new NodeRectangle() ); child->setPosition(180, 100); // relative to parent node child->setAnchorPercentage(0.5f, 0.5f); child->setSize(100, 100); child2->addChild(child); // note that we only keep a reference to the root node. The children are // not deleted when this function goes out of scope, because they sit happily // in the list of children of their parent node. They are not deleted until // they are removed from their parent. }
void SimpleSceneGraphApp::mouseMove( MouseEvent event ) { // pass the mouseMove event to all nodes. Important: this can easily bring your // frame rate down if you have a lot of nodes and none of them does anything with // this event. Only use it if you have just a few nodes, like in this sample, // or catch it as soon as possible by returning TRUE in your mouseMove() method. mRoot->treeMouseMove(event); }
void SimpleSceneGraphApp::draw() { // clear the window gl::clear(); gl::setMatricesWindow( getWindowSize(), true ); // draw all nodes, starting with the root node mRoot->treeDraw(); // example of coordinate conversion: // convert big rectangle's origin to screen coordinates and draw a red circle there gl::color( Color(1, 0, 0) ); gl::drawSolidCircle( mParent->objectToScreen( Vec2f::zero() ), 5.0f ); }
void SimpleSceneGraphApp::keyDown( KeyEvent event ) { // let nodes handle keys first if(! mRoot->treeKeyDown(event) ) { switch( event.getCode() ) { case KeyEvent::KEY_ESCAPE: quit(); break; case KeyEvent::KEY_RETURN: if(event.isAltDown()) { setFullScreen( !isFullScreen() ); } break; } } }
void SimpleSceneGraphApp::resize() { mRoot->treeResize(); }
void SimpleSceneGraphApp::keyUp( KeyEvent event ) { mRoot->treeKeyUp(event); }
void SimpleSceneGraphApp::mouseUp( MouseEvent event ) { // pass the mouseUp event to all nodes. This is usually very quick. mRoot->treeMouseUp(event); }
void SimpleSceneGraphApp::mouseDown( MouseEvent event ) { // pass the mouseDown event to all nodes. This is usually very quick because // it starts at the top nodes and they often catch the event. mRoot->treeMouseDown(event); }
void SimpleSceneGraphApp::update() { // update all nodes mRoot->treeUpdate(); }