glui_node.cpp
上传用户:gb3593
上传日期:2022-01-07
资源大小:3028k
文件大小:7k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /****************************************************************************
  2.   
  3.   GLUI User Interface Toolkit
  4.   ---------------------------
  5.      glui_node.cpp - linked-list tree structure
  6.           --------------------------------------------------
  7.   Copyright (c) 1998 Paul Rademacher
  8.   WWW:    http://sourceforge.net/projects/glui/
  9.   Forums: http://sourceforge.net/forum/?group_id=92496
  10.   This library is free software; you can redistribute it and/or
  11.   modify it under the terms of the GNU Lesser General Public
  12.   License as published by the Free Software Foundation; either
  13.   version 2.1 of the License, or (at your option) any later version.
  14.   This library is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.   Lesser General Public License for more details.
  18.   You should have received a copy of the GNU Lesser General Public
  19.   License along with this library; if not, write to the Free Software
  20.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21. *****************************************************************************/
  22. #include "GL/glui.h"
  23. #include "glui_internal.h"
  24. /********************************************* GLUI_Node::GLUI_Node() *******/
  25. GLUI_Node::GLUI_Node()
  26.     parent_node(NULL),
  27.     child_head(NULL),
  28.     child_tail(NULL),
  29.     next_sibling(NULL),
  30.     prev_sibling(NULL)
  31. {
  32. }
  33. /********************************************* GLUI_Node::first() *******/
  34. /* Returns first sibling in 'this' node's sibling list                  */
  35. GLUI_Node   *GLUI_Node::first_sibling( void )
  36. {
  37.   if ( parent_node == NULL )  
  38.     return this;           /* root node has no siblings */
  39.   else
  40.     return parent_node->child_head;
  41. }
  42. /******************************************** GLUI_Node::next() ********/
  43. /* Returns next sibling in 'this' node's sibling list                  */
  44. GLUI_Node    *GLUI_Node::next( void )
  45. {
  46.   return next_sibling;
  47. }
  48. /******************************************** GLUI_Node::prev() ********/
  49. /* Returns prev sibling in 'this' node's sibling list                  */
  50. GLUI_Node    *GLUI_Node::prev( void )
  51. {
  52.   return prev_sibling;
  53. }
  54. /********************************************* GLUI_Node::last() *******/
  55. /* Returns last sibling in 'this' node's sibling list                  */
  56. GLUI_Node   *GLUI_Node::last_sibling( void )
  57. {
  58.   if ( parent_node == NULL )
  59.     return this;            /* root node has no siblings */
  60.   else
  61.     return parent_node->child_tail;
  62. }
  63. /*************************** GLUI_Node::link_this_to_parent_last() *******/
  64. /* Links as last child of parent                                         */
  65. void   GLUI_Node::link_this_to_parent_last( GLUI_Node *new_parent )
  66. {
  67.   if ( new_parent->child_tail == NULL ) {   /* parent has no children */
  68.     new_parent->child_head = this;
  69.     new_parent->child_tail = this;
  70.     this->parent_node      = new_parent;
  71.   }
  72.   else {                                 /* parent has children */
  73.     new_parent->child_tail->next_sibling = this;
  74.     this->prev_sibling                   = new_parent->child_tail;
  75.     new_parent->child_tail               = this;
  76.     this->parent_node                    = new_parent;
  77.   }
  78. }
  79. /*************************** GLUI_Node::link_this_to_parent_first() *******/
  80. /* Links as first child of parent                                         */
  81. void   GLUI_Node::link_this_to_parent_first( GLUI_Node *new_parent )
  82. {
  83.   if ( new_parent->child_head == NULL ) {   /* parent has no children */
  84.     new_parent->child_head               = this;
  85.     new_parent->child_tail               = this;
  86.     this->parent_node                    = new_parent;
  87.   }
  88.   else {                                 /* parent has children */
  89.     new_parent->child_head->prev_sibling = this;
  90.     this->next_sibling                   = new_parent->child_head;
  91.     new_parent->child_head               = this;
  92.     this->parent_node                    = new_parent;
  93.   }
  94. }
  95. /**************************** GLUI_Node::link_this_to_sibling_next() *****/
  96. void   GLUI_Node::link_this_to_sibling_next( GLUI_Node *sibling )
  97. {
  98.   if ( sibling->next_sibling == NULL ) {    /* node has no next sibling */
  99.     sibling->next_sibling  = this;
  100.     this->prev_sibling     = sibling;
  101.     /* This was the parent's last child, so update that as well */
  102.     if ( sibling->parent_node  != NULL ) {
  103.       sibling->parent_node->child_tail = this;
  104.     }
  105.   }
  106.   else {                            /* node already has a next sibling */
  107.     sibling->next_sibling->prev_sibling = this;
  108.     this->next_sibling                  = sibling->next_sibling;
  109.     sibling->next_sibling               = this;
  110.     this->prev_sibling                  = sibling;
  111.   }
  112.   this->parent_node = sibling->parent_node;
  113. }
  114. /**************************** GLUI_Node::link_this_to_sibling_prev() *****/
  115. void   GLUI_Node::link_this_to_sibling_prev( GLUI_Node *sibling )
  116. {
  117.   if ( sibling->prev_sibling == NULL ) {    /* node has no prev sibling */
  118.     sibling->prev_sibling  = this;
  119.     this->next_sibling     = sibling;
  120.     /* This was the parent's first child, so update that as well */
  121.     if ( sibling->parent_node  != NULL ) {
  122.       sibling->parent_node->child_head = this;
  123.     }
  124.   }
  125.   else {                            /* node already has a prev sibling */
  126.     sibling->prev_sibling->next_sibling = this;
  127.     this->prev_sibling                  = sibling->prev_sibling;
  128.     sibling->prev_sibling               = this;
  129.     this->next_sibling                  = sibling;
  130.   }
  131.   this->parent_node = sibling->parent_node;
  132. }
  133. /**************************************** GLUI_Node::unlink() **************/
  134. void   GLUI_Node::unlink( void )
  135. {
  136.   /* Unlink from prev sibling */
  137.   if ( this->prev_sibling != NULL ) {
  138.     this->prev_sibling->next_sibling = this->next_sibling;
  139.   }
  140.   else {                 /* No prev sibling: this was parent's first child */
  141.     this->parent_node->child_head = this->next_sibling;
  142.   }
  143.   /* Unlink from next sibling */
  144.   if ( this->next_sibling != NULL ) {
  145.     this->next_sibling->prev_sibling = this->prev_sibling;
  146.   }
  147.   else {                /* No next sibling: this was parent's last child */
  148.     this->parent_node->child_tail = this->prev_sibling;
  149.   }
  150.   this->parent_node  = NULL;
  151.   this->next_sibling = NULL;
  152.   this->prev_sibling = NULL;
  153.   this->child_head   = NULL;
  154.   this->child_tail   = NULL;
  155. }
  156. /**************************************** GLUI_Node::dump() **************/
  157. void GLUI_Node::dump( FILE *out, const char *name )
  158. {
  159.     fprintf( out, "GLUI_node: %sn", name );
  160.     fprintf( out, "   parent: %p     child_head: %p    child_tail: %pn",
  161.         (void *) parent_node,
  162.         (void *) child_head,
  163.         (void *) child_tail );
  164.     fprintf( out, "   next: %p       prev: %pn",
  165.         (void *) next_sibling,
  166.         (void *) prev_sibling );
  167. }