Changeset 46

Show
Ignore:
Timestamp:
07/13/07 00:26:29 (1 year ago)
Author:
Deformative
Message:

Bug fixes and cleanups

Files:

Legend:

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

    r43 r46  
    6262    disp.bottomRight = Vec2f(1, 1); 
    6363    disp.setVisibility(true); 
    64     disp.texture = camera.getTexture(); 
     64    disp.setTexture(camera.getTexture()); 
    6565 
    6666     
  • trunk/src/demo2/main.d

    r45 r46  
    128128    void onMousedown2(Surface self, byte buttons, Vec2i coordinates){ 
    129129        self.raise(); 
    130         Input.setSurfaceLock(self); 
     130        self.startDrag(); 
    131131    } 
    132132    void onMouseup2(Surface self, byte buttons, Vec2i coordinates){ 
    133         Input.unlockSurface(); 
     133        self.endDrag(); 
    134134    } 
    135135    void onMousemove(Surface self, byte buttons, Vec2i coordinates){ 
    136         if(buttons == 1) self.moveAdd(coordinates); 
     136        if(buttons == 1) self.drag(coordinates); 
    137137    } 
    138138     
     
    227227        // Cap framerate 
    228228        //if (dtime < 1/60.0) 
    229           std.c.time.usleep(cast(uint)(1000)); 
     229        //    std.c.time.usleep(cast(uint)(1000)); 
    230230        scene.swapTransformRead(); 
    231231    } 
  • trunk/src/yage/gui/surface.d

    r45 r46  
    4747    Vec2f bottomRight; 
    4848     
    49     //these are used for rendering, not for user 
     49    //these are calculated, not for calculating 
    5050    Vec2i position1; 
    5151    Vec2i position2; 
    52      
    53     Vec2i location; 
     52    Vec2i size; 
     53     
     54    //these are for calculating 
     55    private Vec2i locationAdd; 
    5456     
    5557    //used for the texture 
     
    121123    } 
    122124     
    123     //MAYBE I SHOULD PUT RECALCUALTE INTO A DELEGATE SO THAT THE USER COULD PICK ANYTHING! 
    124     void recalculate(Vec2i parent1, Vec2i parent2){ //not done 
    125         Vec2i parentSize = Vec2i(parent2.x - parent1.x, parent2.y - parent1.y); 
    126          
    127         Vec2i tempPosition1; 
    128         Vec2i tempPosition2; 
    129          
    130         tempPosition1.x = cast(int)(topLeft.x * cast(float)parentSize.x) + parent1.x + location.x; 
    131         tempPosition1.y = cast(int)(topLeft.y * cast(float)parentSize.y) + parent1.y + location.y; 
    132         tempPosition2.x = parent2.x - cast(int)((1.0 - bottomRight.x) * cast(float)parentSize.x) + location.x; 
    133         tempPosition2.y = parent2.y - cast(int)((1.0 - bottomRight.y) * cast(float)parentSize.y) + location.y; 
     125    void recalculate(Vec2i parent1, Vec2i parent2, Vec2i parentSize, bool doSubs = true){ //not done 
     126         
     127        position1.x = cast(int)(topLeft.x * cast(float)parentSize.x) + parent1.x + locationAdd.x; 
     128        position1.y = cast(int)(topLeft.y * cast(float)parentSize.y) + parent1.y + locationAdd.y; 
     129         
     130        position2.x = parent2.x - cast(int)((1.0 - bottomRight.x) * cast(float)parentSize.x) + locationAdd.x; 
     131        position2.y = parent2.y - cast(int)((1.0 - bottomRight.y) * cast(float)parentSize.y) + locationAdd.y; 
     132         
     133        size.x = position2.x - position1.x; 
     134        size.y = position2.y - position1.y; 
     135         
     136        if(onResize)onResize(this); 
     137         
     138        if(doSubs) 
     139            recalculateSubs(); 
     140    } 
     141     
     142    private void recalculateSubs(){ 
     143        foreach(sub ;this.subs) 
     144            sub.recalculate(position1, position2, size); 
     145    } 
     146     
     147    void recalculate(bool doSubs = true){ 
     148        if(parent is null){ 
     149            recalculate(Vec2i(0,0), Device.size, Device.size, doSubs); 
     150        } 
     151        else 
     152            recalculate(parent.position1, parent.position2, parent.size, doSubs); 
     153    } 
     154     
     155    void startDrag(){ 
     156        Input.setSurfaceLock(this); 
     157    } 
     158     
     159    void drag(Vec2i add){ 
     160        locationAdd.x -= add.x; 
     161        locationAdd.y -= add.y; 
     162         
     163         
     164        recalculate(false); 
     165         
    134166         
    135167        //All of the below is for not going out of boundry 
    136         if(parent is null) goto setx; 
    137         bool xfailed; 
    138         if(tempPosition1.x < parent.position1.x){ 
    139             xfailed = true; 
    140             goto afterx; 
    141         } 
    142         if(tempPosition2.x > parent.position2.x){ 
    143             xfailed = true; 
    144             goto afterx; 
    145         } 
    146          
    147         setx: 
    148             position1.x = tempPosition1.x; 
    149             position2.x = tempPosition2.x; 
    150             if(parent is null) goto sety; 
    151              
    152         afterx: 
    153             if(tempPosition1.y < parent.position1.y){ 
    154                 if(xfailed) return; //subs are not calculated and the window has not been resized 
    155                 else goto aftery; 
    156             } 
    157             if(tempPosition2.y > parent.position2.y){ 
    158                 if(xfailed) return; //subs are not calculated and the window has not ben resized 
    159                 else goto aftery; 
    160             } 
    161          
    162         sety: 
    163             position1.y = tempPosition1.y; 
    164             position2.y = tempPosition2.y; 
    165          
    166         aftery: 
    167         if(onResize)onResize(this); 
    168          
    169         foreach(sub ;this.subs) 
    170             sub.recalculate(position1, position2); 
    171     } 
    172      
    173     void recalculate(){ 
    174         if(parent is null) 
    175             recalculate(Vec2i(0,0), Vec2i(Device.width, Device.height)); 
    176         else 
    177             recalculate(parent.position1, parent.position2); 
    178     } 
    179      
    180     void moveAdd(Vec2i add){ 
    181         location.x -= add.x; 
    182         location.y -= add.y; 
    183         recalculate(); 
     168        if(parent is null){ 
     169            recalculateSubs(); 
     170            return; 
     171        } 
     172         
     173        if(position1.x < parent.position1.x){ 
     174            position1.x = parent.position1.x; 
     175            position2.x = position1.x + size.x; 
     176        } 
     177        else if(position2.x > parent.position2.x){ 
     178            position2.x = parent.position2.x; 
     179            position1.x = position2.x - size.x; 
     180        } 
     181         
     182        if(position1.y < parent.position1.y){ 
     183            position1.y = parent.position1.y; 
     184            position2.y = position1.y + size.y; 
     185        } 
     186        else if(position2.y > parent.position2.y){ 
     187            position2.y = parent.position2.y; 
     188            position1.y = position2.y - size.y; 
     189        } 
     190         
     191        recalculateSubs(); 
     192    } 
     193     
     194    void endDrag(){ 
     195        recalculate(false); 
     196        if(parent is null) goto after; 
     197         
     198         
     199        if(position1.x < parent.position1.x) 
     200            locationAdd.x += parent.position1.x - position1.x; 
     201        else if(position2.x > parent.position2.x) 
     202            locationAdd.x -= position2.x - parent.position2.x; 
     203         
     204         
     205        if(position1.y < parent.position1.y) 
     206            locationAdd.y += parent.position1.y - position1.y; 
     207        else if(position2.y > parent.position2.y) 
     208            locationAdd.y -= position2.y - parent.position2.y; 
     209         
     210        recalculate(false); 
     211        after: 
     212        Input.unlockSurface(); 
    184213    } 
    185214     
     
    196225        // with dimensions 0..width, 0..height 
    197226        // with 0,0 being at the top left. 
    198         glViewport(0, 0, Device.width, Device.height); 
     227        glViewport(0, 0, Device.size.x, Device.size.y); 
    199228        glMatrixMode(GL_PROJECTION); 
    200229        glLoadIdentity(); 
    201         glOrtho(0, Device.width, Device.height, 0, -1, 1); 
     230        glOrtho(0, Device.size.x, Device.size.y, 0, -1, 1); 
    202231        glMatrixMode(GL_MODELVIEW); 
    203232        glLoadIdentity(); 
     
    450479//Could be better, a method perhaps... 
    451480Surface findSurface(int x, int y){ 
    452     if(Input.surfaceLock) return Input.surfaceLock; 
    453481    foreach_reverse(sub; Device.subs){ 
    454482        if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ 
  • trunk/src/yage/system/device.d

    r45 r46  
    3838    // Video 
    3939    static SDL_Surface* sdl_surface; // Holds a reference to the main (and only) SDL surface 
    40     static uint         width;          // The width of the window. 
    41     static uint        height;         // The heght of the window. 
     40 
     41    static Vec2i       size;   // The width/height of the window. 
    4242    static uint         viewport_width; // The width of the current viewport 
    4343    static uint         viewport_height;// The height of the current viewport 
     
    8787    body 
    8888    { 
    89         this.width = width; 
    90         this.height= height; 
     89        this.size.x = width; 
     90        this.size.y= height; 
    9191        this.depth = depth; 
    9292        this.fullscreen = fullscreen; 
     
    115115        uint flags = SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL | SDL_RESIZABLE | SDL_HWPALETTE | SDL_HWACCEL; 
    116116        if (fullscreen) flags |= SDL_FULLSCREEN; 
    117         sdl_surface = SDL_SetVideoMode(width, height, depth, flags); 
     117        sdl_surface = SDL_SetVideoMode(size.x, size.y, depth, flags); 
    118118        if(sdl_surface is null) 
    119             throw new Exception ("Unable to set " ~ .toString(width) ~ "x" ~ .toString(height) ~ 
     119            throw new Exception ("Unable to set " ~ .toString(size.x) ~ "x" ~ .toString(size.y) ~ 
    120120            " video mode: : " ~ .toString(SDL_GetError())); 
    121121        SDL_LockSurface(sdl_surface); 
     
    277277    /// Return the aspect ratio (width/height) of the rendering window. 
    278278    static float getAspectRatio() 
    279     {   if (height==0) height=1; 
    280         return width/cast(float)height
     279    {   if (size.y==0) size.y=1; 
     280        return size.x/cast(float)size.x
    281281    } 
    282282 
    283283    /// Return the current width of the window in pixels. 
    284284    static uint getWidth() 
    285     {   return width
     285    {   return size.x
    286286    } 
    287287    /// return the current height of the window in pixels. 
    288288    static uint getHeight() 
    289     {   return height
     289    {   return size.y
    290290    } 
    291291 
     
    297297 
    298298        // special values of 0 means stretch to window size 
    299         if (viewport_width ==0) viewport_width  = width
    300         if (viewport_height==0) viewport_height = height
     299        if (viewport_width ==0) viewport_width  = size.x
     300        if (viewport_height==0) viewport_height = size.y
    301301 
    302302        // Ensure our new resolution is less than the window size 
    303303        // This might no longer be an issue once framebufferobjects are used. 
    304         if (viewport_width  > width)  viewport_width  = width
    305         if (viewport_height > height) viewport_height = height
     304        if (viewport_width  > size.x)  viewport_width  = size.x
     305        if (viewport_height > size.y) viewport_height = size.y
    306306 
    307307        glViewport(0, 0, viewport_width, viewport_height); 
     
    318318    /** Stores the dimensions of the current window size. 
    319319     *  This is called by a resize event in Input.checkInput(). */ 
    320     static void resizeWindow(int _width, int _height) 
    321     {   width = _width; 
    322         height = _height; 
     320    static void resizeWindow(int width, int height) 
     321    {   size.x = width; 
     322        size.y = height; 
    323323         
    324         foreach(sub ;this.subs) sub.recalculate(Vec2i(0, 0), Vec2i(width, height)); 
     324        foreach(sub ;this.subs) sub.recalculate(Vec2i(0, 0), size, size); 
    325325         
    326326        // For some reason, SDL Linux requires a call to SDL_SetVideoMode for a screen resize that's 
  • trunk/src/yage/system/input.d

    r45 r46  
    8181                    button[event.button.button].ydown = mousey; 
    8282 
    83                     auto surface = findSurface(mousex, mousey); 
     83                    auto surface = currentSurface(); 
     84 
    8485                    if(surface !is null) surface.mousedown(event.button.button, Vec2i(mousex,mousey)); 
    8586 
     
    9192                    button[event.button.button].yup = mousey; 
    9293 
    93                     auto surface = findSurface(mousex, mousey); 
     94                    auto surface = currentSurface(); 
     95 
    9496                    if(surface !is null) surface.mouseup(event.button.button, Vec2i(mousex,mousey)); 
    9597 
     
    109111                    mousey = event.motion.y; 
    110112                     
    111                     auto surface = findSurface(mousex, mousey); 
     113                    auto surface = currentSurface(); 
     114 
    112115                    if(surface !is null) surface.mousemove(event.button.button, Vec2i(xdiff,ydiff)); 
    113116                    break; 
     
    161164        surfaceLock = null; 
    162165    } 
     166     
     167    static Surface currentSurface(){ 
     168        if(surfaceLock) return surfaceLock; 
     169        return findSurface(mousex, mousey); 
     170    } 
    163171}