Changeset 40

Show
Ignore:
Timestamp:
07/10/07 19:03:27 (1 year ago)
Author:
Deformative
Message:

Did some work on surfaces and input

Files:

Legend:

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

    r37 r40  
    5959 
    6060    Surface disp = new Surface(null); 
    61     disp.texture = camera.getTexture(); 
    6261    disp.topLeft = Vec2f(0,0); 
    63     disp.bottomRight = Vec2f(1,1); 
    64     disp.map(); 
     62    disp.bottomRight = Vec2f(1, 1); 
     63    disp.setVisibility(true); 
    6564     
     65    Surface first = new Surface(disp); 
     66    first.texture = camera.getTexture(); 
     67    first.topLeft = Vec2f(0,0); 
     68    first.bottomRight = Vec2f(.75, .75); 
     69    first.setVisibility(true); 
    6670     
     71    Surface second = new Surface(disp); 
     72    second.texture = camera.getTexture(); 
     73    second.topLeft = Vec2f(.25,.25); 
     74    second.bottomRight = Vec2f(1,1); 
     75    second.setVisibility(true); 
     76     
     77    void onMousedown(Surface self, byte buttons, Vec2i coordinates){ 
     78        self.raise(); 
     79        Input.button[1].up = false; 
     80        Input.setGrabMouse(!Input.getGrabMouse()); 
     81    } 
     82     
     83    void onResize(Surface self, Vec2i difference){ 
     84        camera.setResolution(self.size.x, self.size.y); 
     85    } 
     86     
     87    first.onMousedown = &onMousedown; 
     88    second.onMousedown = &onMousedown;   
     89    first.onResize = &onResize; 
    6790     
    6891    // Music 
     
    102125 
    103126        // Toggle mouse grab 
    104       if (Input.button[1].up) 
    105       {   Input.button[1].up = false; 
    106           Input.setGrabMouse(!Input.getGrabMouse()); 
    107       } 
     127//        if (Input.button[1].up) 
     128//        {   Input.button[1].up = false; 
     129//            Input.setGrabMouse(!Input.getGrabMouse()); 
     130//        } 
    108131        ship.getSpring().update(1/60.0f); 
    109132    } 
     
    111134 
    112135    Device.resizeWindow(800, 600); 
    113     disp.recalculateTexture(); 
     136    //disp.recalculateTexture(); 
    114137     
    115138    // Rendering / Input Loop 
     
    127150        Input.processInput(); 
    128151        camera.toTexture(); 
    129         Device.render(); 
    130  
     152        disp.render(); 
     153         
     154         
    131155        // Print framerate 
    132156        fps++; 
  • trunk/src/yage/gui/surface.d

    r38 r40  
    55import yage.system.device; 
    66import derelict.opengl.gl; 
    7  
     7import derelict.sdl.sdl; 
     8import yage.gui.style; 
    89import yage.system.constant; 
    910 
     
    1112 
    1213class Surface{ 
    13      
    14      
    15     GPUTexture texture; 
    16      
    17     Surface[] subs; 
    18      
    19     Vec2f portion; 
     14    //Style style;  //move style into a higher level clas, perhaps make a geometry struct isntead 
     15    GPUTexture texture;  //Change from GPUTexture to Texture or Material 
     16     
     17    //Linked list would be faster for raising a window. 
     18    Surface[] subs;//Perhaps this should be changed into a linked list... 
     19    //Not sure if I should have a reference to Parent or not, but for now, I will. 
     20    Surface parent; 
     21     
     22    Vec2f portion; //used for the texture only 
    2023     
    2124    Vec2f topLeft; 
    2225    Vec2f bottomRight; 
    2326     
    24     Vec2i position; 
     27    //these are used for rendering, not calculation 
     28    Vec2i position1; 
    2529    Vec2i position2; 
    2630     
     31     
    2732    Vec2i size; 
    2833     
    29     bool mapped
     34    bool visible
    3035 
    3136    //add root position and stuff 
    3237     
    33     void delegate() onResize; 
    34      
    35     this(Surface parent){ 
     38    //Not sure how to impelement gluUnProject 
     39     
     40    void delegate(typeof(this) self) onBlur; 
     41    void blur(){ if(onBlur)onBlur(this);} 
     42 
     43    void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onClick; 
     44 
     45    void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onDblclick; 
     46 
     47    void delegate(typeof(this) self) onFocus; 
     48 
     49    void delegate(typeof(this) self, byte key, byte modifiers) onKeydown; 
     50 
     51    void delegate(typeof(this) self, byte key, byte modifiers) onKeypress; 
     52 
     53    void delegate(typeof(this) self, byte key, byte modifiers) onKeyup; 
     54 
     55    void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMousedown; 
     56    void mousedown(byte buttons, Vec2i coordinates){ if(onMousedown)onMousedown(this, buttons, coordinates);} 
     57 
     58    void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMousemove; 
     59    void mousemove(byte buttons, Vec2i coordinates){ if(onMousemove)onMousemove(this, buttons, coordinates);} 
     60 
     61    void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseout; 
     62 
     63    void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseover; 
     64 
     65    void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseup; 
     66    void mouseup(byte buttons, Vec2i coordinates){ if(onMouseup)onMouseup(this, buttons, coordinates);} 
     67 
     68    void delegate(typeof(this) self, Vec2i difference) onResize; 
     69     
     70     
     71    this(Surface p){ 
     72        parent = p; 
    3673        if(parent is null){ 
    3774            Device.subs ~= this; 
     75            recalculate(Device.getHeight(), Device.getWidth()); 
    3876        } 
    3977        else{ 
    4078            parent.subs ~= this; 
     79            recalculate(parent.size.x, parent.size.y); 
    4180        } 
    4281    } 
     
    4887     
    4988    void recalculate(int width, int height){ //not done 
    50         position.x = cast(int)(topLeft.x * cast(float)width); 
    51         position.y = cast(int)(topLeft.y * cast(float)height); 
     89        position1.x = cast(int)(topLeft.x * cast(float)width); 
     90        position1.y = cast(int)(topLeft.y * cast(float)height); 
    5291         
    5392        position2.x = cast(int)(bottomRight.x * cast(float)width); 
    5493        position2.y = cast(int)(bottomRight.y * cast(float)height); 
    5594         
    56         size.x = position2.x - position.x; 
    57         size.y = position2.y - position.y; 
    58          
    59         foreach(sub ;this.subs) sub.recalculate(size.x, size.y); 
     95        Vec2i temp = size; 
     96         
     97        size.x = position2.x - position1.x; 
     98        size.y = position2.y - position1.y; 
     99         
     100        if(onResize)onResize(this, Vec2i(temp.x - size.x, temp.y - size.y)); 
     101         
     102        foreach(sub ;this.subs) 
     103            sub.recalculate(size.x, size.y); 
     104    } 
     105     
     106    void recalculate(){ 
     107        if(parent == null) 
     108            recalculate(Device.width, Device.height); 
     109        else 
     110            recalculate(parent.size.x, parent.size.y); 
    60111    } 
    61112     
     
    65116    } 
    66117     
     118    void render(){ 
     119        glPushAttrib(0xFFFFFFFF);   // all attribs 
     120         
     121        // Setup the viewport in orthogonal mode, 
     122        // with dimensions 0..width, 0..height 
     123        // with 0,0 being at the top left. 
     124        glViewport(0, 0, Device.width, Device.height); 
     125        glMatrixMode(GL_PROJECTION); 
     126        glLoadIdentity(); 
     127        glOrtho(0, Device.width, Device.height, 0, -1, 1); 
     128        glMatrixMode(GL_MODELVIEW); 
     129        glLoadIdentity(); 
     130         
     131        glDisable(GL_DEPTH_TEST); 
     132        glDisable(GL_LIGHTING); 
     133         
     134        glEnable(GL_TEXTURE_2D); 
     135         
     136        //This may need to be changed for when people wish to render surfaces individually so the already rendered are not cleared. 
     137        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     138         
     139        draw(); 
     140         
     141        SDL_GL_SwapBuffers(); 
     142         
     143        glPopAttrib(); 
     144    } 
     145     
    67146    void draw(){ 
    68         recalculateTexture(); 
    69         if(mapped){ 
     147        if(visible){ 
    70148            if (texture !is null){ 
     149                recalculateTexture(); 
    71150                // Draw a textured quad of our current material 
    72151                Texture(texture, true, TEXTURE_FILTER_BILINEAR).bind(); 
     
    75154 
    76155                glTexCoord2f(0, 0); 
    77                 glVertex2f(position.x, bottomRight.y); 
     156                glVertex2i(position1.x, position2.y); 
    78157                 
    79                 glTexCoord2f(portion.x, 0);  
    80                 glVertex2f(bottomRight.x, bottomRight.y); 
     158                glTexCoord2f(portion.x, 0); 
     159                glVertex2i(position2.x, position2.y); 
    81160                 
    82161                glTexCoord2f(portion.x, portion.y);  
    83                 glVertex2f(bottomRight.x, topLeft.y); 
     162                glVertex2i(position2.x, position1.y); 
    84163 
    85164                glTexCoord2f(0, portion.y); 
    86                 glVertex2f(topLeft.x, topLeft.y); 
     165                glVertex2i(position1.x, position1.y); 
    87166 
    88167                glEnd(); 
     
    92171    } 
    93172     
    94     void map(){ 
    95         mapped = true; 
    96     } 
    97     void unmap(){ 
    98         mapped = false; 
    99     } 
    100 
     173    void setVisibility(bool v){ 
     174        visible = v; 
     175    } 
     176     
     177    void raise(){ //Could be cleaner, whatever, I'll fix it later 
     178        if(parent is null){ 
     179            uint index = findIndex(this, Device.subs); 
     180            for(; index < Device.subs.length - 1; index++) 
     181                Device.subs[index] = Device.subs[index+1]; 
     182            Device.subs[$] = this; 
     183        } 
     184        else{ 
     185            uint index = findIndex(this, parent.subs); 
     186            for(; index < parent.subs.length - 1; index++) 
     187                parent.subs[index] = parent.subs[index+1]; 
     188            parent.subs[$-1] = this; 
     189        } 
     190    } 
     191
     192 
     193Surface findSurface(int x, int y){ 
     194    foreach_reverse(sub; Device.subs){ 
     195        if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ 
     196            return findSurface(sub, x, y); 
     197        } 
     198    } 
     199    return null; 
     200
     201 
     202Surface findSurface(Surface surface,int x, int y){ 
     203    foreach_reverse(sub; surface.subs){ 
     204        if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ 
     205            return findSurface(sub, x, y); 
     206        } 
     207    } 
     208    return surface; 
     209
     210 
     211uint findIndex(Surface surface, Surface[] array){ //perhaps put into a template 
     212    foreach(uint index, Surface current; array){ 
     213        if(current == surface) return  index; 
     214    } 
     215    return 1 << 8; 
     216
  • trunk/src/yage/system/device.d

    r38 r40  
    2727// Enable specular highlights with textures. 
    2828const int LIGHT_MODEL_COLOR_CONTROL_EXT = 0x81F8; 
    29 const int SINGLE_COLOR_EXT             = 0x81F9; 
     29const int SINGLE_COLOR_EXT = 0x81F9; 
    3030const int SEPARATE_SPECULAR_COLOR_EXT   = 0x81FA; 
    3131 
     
    190190        SDL_EnableUNICODE(true); 
    191191        SDL_EnableKeyRepeat(1, 100); 
    192         Input.setGrabMouse(true); 
     192        //Input.setGrabMouse(true); 
    193193 
    194194        // Initialize OpenAL 
     
    275275    } 
    276276 
    277     /// Draw the current material to the screen. 
    278     static void render(){ 
    279         glPushAttrib(0xFFFFFFFF);   // all attribs 
    280  
    281         // Setup the viewport in orthogonal mode, 
    282         // with dimensions 0..width, 0..height 
    283         // with 0,0 being at the top left. 
    284         glViewport(0, 0, width, height); 
    285         glMatrixMode(GL_PROJECTION); 
    286         glLoadIdentity(); 
    287         glOrtho(0, 1, 1, 0, -1, 1); 
    288         glMatrixMode(GL_MODELVIEW); 
    289         glLoadIdentity(); 
    290  
    291         glDisable(GL_DEPTH_TEST); 
    292         glDisable(GL_LIGHTING); 
    293          
    294         glEnable(GL_TEXTURE_2D); 
    295          
    296         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    297         foreach(sub; this.subs) 
    298             sub.draw(); 
    299  
    300         SDL_GL_SwapBuffers(); 
    301  
    302         //Texture(texture, true, TEXTURE_FILTER_BILINEAR).bind(); 
    303         glPopAttrib(); 
    304     } 
    305  
    306277    /// Return the aspect ratio (width/height) of the rendering window. 
    307278    static float getAspectRatio() 
  • trunk/src/yage/system/input.d

    r28 r40  
    1212import yage.core.vector; 
    1313import yage.system.device; 
     14import yage.gui.surface; 
    1415 
    1516 
     
    7677                    button[event.button.button].xdown = mousex; 
    7778                    button[event.button.button].ydown = mousey; 
     79 
     80                    auto surface = findSurface(mousex, mousey); 
     81                    if(!(surface is null)) surface.mousedown(event.button.button, Vec2i(mousex,mousey)); 
     82 
    7883                    break; 
    7984                case SDL_MOUSEBUTTONUP: 
     
    8287                    button[event.button.button].xup = mousex; 
    8388                    button[event.button.button].yup = mousey; 
     89 
     90                    auto surface = findSurface(mousex, mousey); 
     91                    if(!(surface is null)) surface.mouseup(event.button.button, Vec2i(mousex,mousey)); 
     92 
    8493                    break; 
    8594                case SDL_MOUSEMOTION: 
     
    92101                    mousex = event.motion.x; 
    93102                    mousey = event.motion.y; 
     103                     
     104                    //auto surface = findSurface(mousex, mousey); 
     105                    //if(!(surface is null)) surface.mousemove(event.button.button, Vec2i(mousex,mousey)); 
    94106                    break; 
    95107