Changeset 71

Show
Ignore:
Timestamp:
05/28/08 23:08:25 (5 months ago)
Author:
JoeCoder
Message:

Moved parent child relationships of Node into yage.core.tree in anticipation of building the same type of tree with GUI surfaces.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/demo1/gameobj.d

    r70 r71  
    4747    void setMass(float mass) 
    4848    {   this.mass = mass; 
    49         rock.setScale(Vec3f(pow(mass, .33333)/2)); 
     49        rock.setSize(Vec3f(pow(mass, .33333)/2)); 
    5050        radius = pow(mass, .3333)*.75*4; 
    5151    } 
     
    6363        SpriteNode flare = new SpriteNode(this); 
    6464        flare.setMaterial("fx/flare1.xml"); 
    65         flare.setScale(Vec3f(2)); 
     65        flare.setSize(Vec3f(2)); 
    6666 
    6767        if (timer is null) 
  • trunk/src/demo1/main.d

    r70 r71  
    111111    SpriteNode star = new SpriteNode(l1); 
    112112    star.setMaterial("space/star.xml"); 
    113     star.setScale(Vec3f(2500)); 
     113    star.setSize(Vec3f(2500)); 
    114114 
    115115    // Planet 
    116116    auto planet = new ModelNode(scene); 
    117117    planet.setModel("space/planet.ms3d"); 
    118     planet.scale = Vec3f(60); 
     118    planet.setSize(Vec3f(60)); 
    119119    planet.setAngularVelocity(Vec3f(0, -0.01, 0)); 
    120120     
  • trunk/src/demo1/ship.d

    r70 r71  
    3535        ship = new ModelNode(pitch); 
    3636        ship.setModel("scifi/fighter.ms3d"); 
    37         ship.scale = Vec3f(.25); 
     37        ship.setSize(Vec3f(.25)); 
    3838 
    3939        spring = new Spring(ship); 
     
    7575            puff.setMaterial(Resource.material("fx/smoke.xml")); 
    7676            puff.setLifetime(5); 
    77             puff.setScale(Vec3f(.4)); 
     77            puff.setSize(Vec3f(.4)); 
    7878            //puff.setVelocity(getVelocity() - Vec3f(0, 0, -10).rotate(ship.getAbsoluteTransform())); 
    7979            puff.setPosition(ship.getAbsolutePosition()+Vec3f(.8, 0, 2.5).rotate(ship.getAbsoluteTransform())); 
     
    8383                node.setColor(Color(1, 1, 1, node.getLifetime()/5)); 
    8484                float scale = std.math.sqrt(5.0f)-std.math.sqrt(node.getLifetime()) + .4; 
    85                 node.setScale(scale); 
     85                node.setSize(scale); 
    8686            } 
    8787            puff.onUpdate(&fade); 
  • trunk/src/demo2/gameobj.d

    r70 r71  
    4747    void setMass(float mass) 
    4848    {   this.mass = mass; 
    49         rock.setScale(Vec3f(pow(mass, .33333)/2)); 
     49        rock.setSize(Vec3f(pow(mass, .33333)/2)); 
    5050        radius = pow(mass, .3333)*.75*4; 
    5151    } 
     
    6363        SpriteNode flare = new SpriteNode(this); 
    6464        flare.setMaterial("fx/flare1.xml"); 
    65         flare.setScale(Vec3f(2)); 
     65        flare.setSize(Vec3f(2)); 
    6666 
    6767        if (timer is null) 
  • trunk/src/demo2/main.d

    r70 r71  
    202202    SpriteNode star = new SpriteNode(l1); 
    203203    star.setMaterial("space/star.xml"); 
    204     star.setScale(Vec3f(2500)); 
     204    star.setSize(Vec3f(2500)); 
    205205 
    206206    // Planet 
    207207    auto planet = new ModelNode(scene); 
    208208    planet.setModel("space/planet.ms3d"); 
    209     planet.scale = Vec3f(600); 
     209    planet.setSize(Vec3f(600)); 
    210210    planet.setAngularVelocity(Vec3f(0, -0.01, 0)); 
    211211     
  • trunk/src/demo2/ship.d

    r70 r71  
    3434        ship = new ModelNode(pitch); 
    3535        ship.setModel("obj/tie2.obj"); 
    36         ship.scale = Vec3f(.25); 
     36        ship.setSize(Vec3f(.25)); 
    3737 
    3838        spring = new Spring(ship); 
  • trunk/src/yage/core/all.d

    r67 r71  
    2525    import yage.core.repeater; 
    2626    import yage.core.timer; 
     27    import yage.core.tree; 
    2728    import yage.core.types; 
    2829    import yage.core.vector; 
  • trunk/src/yage/core/xml.d

    r28 r71  
    77 * documents. 
    88 * 
    9  * It might eventually be good to rewrite this to throw Exceptions on any parse 
    10  * errors--it already catches some. 
     9 * This hasn't been profiled and probably isn't that efficient. 
    1110 * 
    1211 * History: 
  • trunk/src/yage/gui/surface.d

    r57 r71  
    2828 
    2929/**  
    30  * A surface will be similar to an HTML DOM element, including having text inside it,  
     30 * Surfaces are similar to HTML DOM elements, including having text inside it,  
    3131 * margin, padding, a border, and a background texture, including textures from a camera.  
    3232 * Surfaces will exist in a hierarchical structure, with each having a parent and an array of children.  
    33  * The children will be positioned relative to the borders of their parent. */ 
     33 * Surfacs are positioned relative to their parent.  
     34 * A style struct defines most of the styles associated with the Surface. */ 
    3435class Surface{ 
    3536    static final Style defaultStyle; 
     
    3738     
    3839    //Change from GPUTexture to Texture or Material 
    39     private GPUTexture texture; 
    40      
    41     Surface[] subs; 
     40    protected GPUTexture texture; 
     41     
     42    Surface parent; 
     43    Surface[] children; 
    4244    //Not sure if I should have a reference to Parent or not, but for now, I will. 
    43     Surface parent; 
     45     
    4446     
    4547    Vec2f topLeft;//rid self of these 
     
    5254     
    5355    //these are for calculating 
    54     private Vec2i locationAdd; 
     56    protected Vec2i locationAdd; 
    5557     
    5658    //used for the texture 
     
    139141        parent = p; 
    140142        if(parent is null){ 
    141             Device.subs ~= this; 
     143            Device.children ~= this; 
    142144            this.recalculate(); 
    143145        } 
    144146        else{ 
    145             parent.subs ~= this; 
     147            parent.children ~= this; 
    146148            this.recalculate(); 
    147149        } 
     
    171173     
    172174    private void recalculateSubs(){ 
    173         foreach(sub ;this.subs
     175        foreach(sub; this.children
    174176            sub.recalculate(position1, position2, size); 
    175177    } 
     
    480482            //  subs.radixSort((Surface s){return s.style.zIndex;} ); 
    481483             
    482             foreach(sub; subs
     484            foreach(sub; children
    483485                sub.draw(); 
    484486        } 
     
    491493    void raise(){ //Could be cleaner, whatever, I'll fix it later 
    492494        if(parent is null){ 
    493             uint index = findIndex(this, Device.subs); 
    494             for(; index < Device.subs.length - 1; index++) 
    495                 Device.subs[index] = Device.subs[index+1]; 
    496             Device.subs[$-1] = this; 
     495            uint index = findIndex(this, Device.children); 
     496            for(; index < Device.children.length - 1; index++) 
     497                Device.children[index] = Device.children[index+1]; 
     498            Device.children[$-1] = this; 
    497499        } 
    498500        else{ 
    499             uint index = findIndex(this, parent.subs); 
    500             for(; index < parent.subs.length - 1; index++) 
    501                 parent.subs[index] = parent.subs[index+1]; 
    502             parent.subs[$-1] = this; 
     501            uint index = findIndex(this, parent.children); 
     502            for(; index < parent.children.length - 1; index++) 
     503                parent.children[index] = parent.children[index+1]; 
     504            parent.children[$-1] = this; 
    503505        } 
    504506        if(onFocus) onFocus(this); 
     
    534536         
    535537    bool isSub(Surface surf){ 
    536         foreach(sub; subs){ 
     538        foreach(sub; children){ 
    537539            if (sub == surf) return true; 
    538540        } 
     
    544546//Could be better, a method perhaps... 
    545547Surface findSurface(int x, int y){ 
    546     foreach_reverse(sub; Device.subs){ 
     548    foreach_reverse(sub; Device.children){ 
    547549        if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ 
    548550            return findSurface(sub, x, y); 
     
    553555//Could be better, a method perhaps... 
    554556Surface findSurface(Surface surface,int x, int y){ 
    555     foreach_reverse(sub; surface.subs){ 
     557    foreach_reverse(sub; surface.children){ 
    556558        if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ 
    557559            return findSurface(sub, x, y); 
  • trunk/src/yage/node/graph.d

    r70 r71  
    101101 
    102102    /// Overridden to cache the radius if changed by the scale. 
    103     void scale(Vec3f scale) 
    104     {   this.scale = scale; 
    105         radius = model.getDimensions().scale(scale).length(); 
    106     } 
    107      
    108     Vec3f scale() 
    109     {   return super.scale;      
     103    void setSize(Vec3f size) 
     104    {   this.size = size; 
     105        radius = model.getDimensions().scale(size).length(); 
     106    }    
     107    Vec3f getSize() /// Ditto 
     108    {   return super.size;       
    110109    } 
    111110 
     
    229228        model.getMeshes()[0].setTriangles(triangles); 
    230229 
    231         radius = model.getDimensions().scale(scale).max(); 
     230        radius = model.getDimensions().scale(size).max(); 
    232231 
    233232        // Cache model in video memory 
  • trunk/src/yage/node/model.d

    r70 r71  
    4848    void setModel(Model model) 
    4949    {   this.model = model; 
    50         radius = model.getDimensions().scale(scale).length(); 
     50        radius = model.getDimensions().scale(size).length(); 
    5151    } 
    5252 
     
    6060 
    6161    /// Overridden to cache the radius if changed by the scale. 
    62     void scale(Vec3f s) 
    63     {   super.scale = Vec3f(s.x, s.y, s.z); 
     62    void setSize(Vec3f s) 
     63    {   super.size = Vec3f(s.x, s.y, s.z); 
    6464        if (model) 
    65             radius = model.getDimensions().scale(scale).max(); 
    66     } 
    67      
    68     Vec3f scale() 
    69     {   return super.scale;      
     65            radius = model.getDimensions().scale(size).max(); 
     66    }    
     67    Vec3f getSize() /// Ditto 
     68    {   return super.size;       
    7069    } 
    7170 
  • trunk/src/yage/node/node.d

    r70 r71  
    1111import std.bind; 
    1212import yage.core.all; 
     13import yage.core.tree; 
    1314import yage.node.visible; 
    1415import yage.node.scene; 
     
    4445 *                                   // to 0, 0, 0, instead of a. 
    4546 * --------------------------------*/ 
    46 abstract class Node 
     47abstract class Node : Tree!(Node) 
    4748{ 
    4849    // These are public for easy internal access. 
    4950    Scene   scene;          // The Scene that this node belongs to. 
    50     Node    parent; 
    51     Node[Node]  children; 
    5251    protected float lifetime = float.infinity;  // in seconds 
    5352    protected void delegate(Node self) on_update = null;    // called on update 
     
    8988 
    9089    /// Construct as a child of parent, a copy of original and recursivly copy all children. 
    91     this(Node parent, MovableNode original) 
     90    this(Node parent, Node original) 
    9291    {   this(parent); 
    9392 
     
    135134 
    136135    /** 
    137      * Add a child Node. 
    138      * Automatically detaches it from any other nodes. 
    139      * Returns: A self reference.*/ 
    140     Node addChild(VisibleNode child) 
    141     in {assert(child !is this);} 
    142     body 
    143     {   child.setParent(this); 
    144         return this; 
    145     } 
    146  
    147     /// Get an array of this Node's children 
    148     Node[Node] getChildren() 
    149     {   return children; 
    150     } 
    151  
    152     /** 
    153136     * Get / set the lifeime of a VisibleNode (in seconds). 
    154137     * The default value is float.infinity, but a lower number will cause the VisibleNode to be removed 
     
    162145     
    163146    /** 
    164      * Get / set the parent of this Node (what it's attached to). 
    165      * Setting a new parent removes it from its old parent's children and returns a self-reference. */ 
    166     Node getParent() 
    167     {   return parent; 
    168     }    
     147     * Overridden from Tree to set scene and mark transform dirty. */ 
    169148    Node setParent(Node _parent) /// Ditto 
    170     in { assert(_parent !is null); 
    171     }body 
    172     {            
    173         if (parent && this in parent.children) 
    174             parent.children.remove(this); 
    175          
    176         // Add to new parent 
    177         parent = _parent; 
    178         parent.children[this] = this; 
    179         scene = parent.scene; 
     149    {   scene = _parent.scene; 
    180150        setTransformDirty(); 
    181         return this
     151        return super.setParent(_parent)
    182152    } 
    183153 
     
    256226        return result; 
    257227    } 
    258      
    259     /// Remove this Node.  This function should be used instead of delete. 
    260     void remove() 
    261     {   // this needs to happen because some children (like lights) may need to do more in their remove() function. 
    262         foreach(Node c; children) 
    263             c.remove(); 
    264          
    265         if (parent && this in parent.children) 
    266             parent.children.remove(this); 
    267     } 
    268228 
    269229    /** 
  • trunk/src/yage/node/sprite.d

    r70 r71  
    4343    /// Return the distance to the furthest point of the SpriteNode. 
    4444    float getRadius() 
    45     {   return 1.414213562*scale.max(); 
     45    {   return 1.414213562*size.max(); 
    4646    } 
    4747 
  • trunk/src/yage/node/terrain.d

    r70 r71  
    3333 * -------------------------------- 
    3434 * TerrainNode a = new TerrainNode(scene);  // Child of scene 
    35  * a.setScale(1000, 100, 1000);             // Make it a decent size 
     35 * a.setSize(1000, 100, 1000);             // Make it a decent size 
    3636 * a.setMaterial("terrain/islands.xml"); 
    3737 * a.setHeightMap("terrain/islands-height.png"); 
     
    8484     * Generate the 3D landscape rendered for this Node, from a heighmap image. 
    8585     * The terrain generated always fits inside a 1x1x1 cube. 
    86      * Use setScale() to adjust to a comfortable size. 
     86     * Use setSize() to adjust to a comfortable size. 
    8787     * Params: 
    8888     * grayscale = An image to load.  It will be converted to a grayscale image 
     
    157157     
    158158    Vec3f scale() 
    159     {   return super.scale;        
     159    {   return super.size;         
    160160    } 
    161161     
  • trunk/src/yage/node/visible.d

    r70 r71  
    3333{    
    3434    protected bool  visible = true; 
    35     protected Vec3f scale; 
     35    protected Vec3f size; 
    3636    protected Color color;              // RGBA, used for glColor4f() 
    3737     
     
    4242    this(Node parent) 
    4343    {   visible = false; 
    44         scale = Vec3f(1); 
     44        size = Vec3f(1); 
    4545        color = Color("white"); 
    4646        super(parent); 
     
    5151    {   super(parent, original); 
    5252        visible = original.visible; 
    53         scale = original.scale; 
     53        size = original.size; 
    5454        color = original.color; 
    5555    } 
     
    8888    /// Get the radius of this VisibleNode's culling sphere. 
    8989    float getRadius() 
    90     {   return 1.732050807*scale.max();   // a value of zero would not be rendered since it's always smaller than the pixel threshold. 
     90    {   return 1.732050807*size.max();    // a value of zero would not be rendered since it's always smaller than the pixel threshold. 
    9191    }                                   // This is the radius of a 1x1x1 cube 
    9292 
     
    9494     * Get / set the scale of this VisibleNode in the x, y, and z directions. 
    9595     * The default is (1, 1, 1).  Unlike position and rotation, scale is not inherited. */   
    96     void setScale(float x, float y, float z)  
    97     {   scale.set(x, y, z);  
     96    void setSize(float x, float y, float z)  
     97    {   size.set(x, y, z);  
    9898    } 
    99     void setScale(Vec3f scale) /// ditto 
    100     {   setScale(scale.x, scale.y, scale.z);  
     99    void setSize(Vec3f size) /// ditto 
     100    {   setSize(size.x, size.y, size.z);  
    101101    } 
    102     void setScale(float scale) /// ditto 
    103     {   setScale(scale, scale, scale);  
     102    void setSize(float size) /// ditto 
     103    {   setSize(size, size, size);  
    104104    }  
    105     Vec3f getScale() /// ditto 
    106     {   return scale;  
     105    Vec3f getSize() /// ditto 
     106    {   return size;  
    107107    } 
    108108 
  • trunk/src/yage/system/device.d

    r56 r71  
    6363    public: 
    6464     
    65     static Surface[] subs
     65    static Surface[] children
    6666     
    6767    /// Unload SDL at exit. 
     
    338338        size.y = height; 
    339339         
    340         foreach(sub ;this.subs)   sub.recalculate(Vec2i(0, 0), size, size); 
     340        foreach(sub ;this.children)   sub.recalculate(Vec2i(0, 0), size, size); 
    341341         
    342342        // For some reason, SDL Linux requires a call to SDL_SetVideoMode for a screen resize that's 
    343         // larger than the current screen. (need to try this with latest version of SDL, alsy try SDL lock surface) 
     343        // larger than the current screen. (need to try this with latest version of SDL, also try SDL lock surface) 
    344344        // This same code would crash the engine on windows. 
    345345        // This code may now be un-needed and needs to be retested. 
  • trunk/src/yage/system/render.d

    r70 r71  
    8383            glPushMatrix(); 
    8484            glMultMatrixf(n.getAbsoluteTransform(true).v.ptr); 
    85             glScalef(n.getScale().x, n.getScale().y, n.getScale().z); 
     85            Vec3f size = n.getSize(); 
     86            glScalef(size.x, size.y, size.z); 
    8687            n.enableLights(); 
    8788 
     
    8990            {   case "yage.node.model.ModelNode": 
    9091                    model((cast(ModelNode)n).getModel(), n); 
    91                     //writefln((cast(ModelNode)n).getModel().getSource()); 
    9292                    break; 
    9393                case "yage.node.sprite.SpriteNode": 
     
    140140        /* 
    141141        for (int i=0; i<3; i++) 
    142         {   at.vertices[i] = abs_transform*v[tri.v[i]].scale(node.getScale()); 
     142        {   at.vertices[i] = abs_transform*v[tri.v[i]].scale(node.getSize()); 
    143143            at.texcoords[i] = &t[tri.v[i]]; 
    144144            at.normals[i] = &n[tri.v[i]]; 
     
    211211                        {   AlphaTriangle at; 
    212212                            for (int i=0; i<3; i++) 
    213                             {   at.vertices[i] = abs_transform*v[tri.v[i]].scale(node.getScale()); 
     213                            {   at.vertices[i] = abs_transform*v[tri.v[i]].scale(node.getSize()); 
    214214                                at.texcoords[i] = &t[tri.v[i]]; 
    215215                                at.normals[i] = &n[tri.v[i]]; 
  • trunk/src/yage/util/misc.d

    r70 r71  
    5555         
    5656        a.setModel(Resource.model("space/asteroid1.ms3d")); 
    57         a.scale = Vec3f(value5*value5*value5*value5*value5*value5*value5*1.1 + .2); 
     57        a.setSize(Vec3f(value5*value5*value5*value5*value5*value5*value5*1.1 + .2)); 
    5858        a.rotate(Vec3f(value4*12, value2*12, value3*12)); 
    59       a.setAngularVelocity(Vec3f(random(-.2, .2), random(-.2, .2), random(-.2, .2))); 
     59    //    a.setAngularVelocity(Vec3f(random(-.2, .2), random(-.2, .2), random(-.2, .2))); 
    6060    } 
    6161} 
     
    7474 
    7575        a.setModel(Resource.model("../media/planet/phobos.ms3d")); 
    76         a.scale = Vec3f(value5*value5*.4 + .2); 
     76        a.setSize(Vec3f(value5*value5*.4 + .2)); 
    7777        a.rotate(Vec3f(value4*12, value2*7, value3*11)); 
    7878    } 
     
    9191 
    9292        a.setModel(Resource.model("../media/planet/phobos.ms3d")); 
    93         a.scale = Vec3f(value5*value5*.4 + .2); 
     93        a.setSize(Vec3f(value5*value5*.4 + .2)); 
    9494        a.rotate(Vec3f(value4*12, value2*7, value*11)); 
    9595    } 
  • trunk/src/yage/util/spring.d

    r70 r71  
    5757        tail.setRotation(head.getAbsoluteRotation()); 
    5858 
    59         Vec3f dist = head.getAbsolutePosition() - tail.getAbsolutePosition() + distance.rotate(head.getAbsoluteTransform()); 
    60         Vec3f vel = dist.scale(min(stiffness, 1/delta)); // scale by 1/delta when framerate is low to prevent jerkiness. 
     59        Vec3f dist = head.getAbsolutePosition() + distance.rotate(head.getAbsoluteTransform()) - tail.getAbsolutePosition();        
     60        Vec3f vel = dist.scale(stiffness);  
    6161 
    6262        if (vel.length*delta > dist.length) 
    63             vel = dist; 
    64         tail.setVelocity(vel); 
     63            vel = dist;         
     64        tail.move(vel*delta); 
    6565    } 
    6666}