Changeset 17

Show
Ignore:
Timestamp:
11/26/06 23:48:52 (2 years ago)
Author:
baxissimo
Message:

Added enter/leave events, and got dxut doing timer-based transitions between states.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/luigi/base.d

    r12 r17  
    171171    void set(float r_, float g_, float b_, float a_=1.0) { 
    172172        r=lrint(r_*255), g=lrint(g_*255), b=lrint(b_*255), a=lrint(a_*255); 
     173    } 
     174 
     175    static Color lerp(Color c1, Color c2, float t) { 
     176        Color ret; 
     177        ret.r = lrint( (1-t)*c1.r + t*c2.r ); 
     178        ret.g = lrint( (1-t)*c1.g + t*c2.g ); 
     179        ret.b = lrint( (1-t)*c1.b + t*c2.b ); 
     180        ret.a = lrint( (1-t)*c1.a + t*c2.a ); 
     181        return ret; 
    173182    } 
    174183 
  • trunk/luigi/event.d

    r11 r17  
    279279} 
    280280 
    281 // how do I do this??? 
    282 /// extern luigi.gui.Widget; 
    283  
    284281struct _FocusEvent 
    285282{ 
    286     Object previous; 
    287  
    288     bool is_alive = false; 
    289     bool alive(bool tf) { return is_alive=tf; } 
    290     bool alive() { return is_alive; } 
     283    Object previous; // would rather have Widget here 
     284 
     285    bool is_alive = false; 
     286    bool alive(bool tf) { return is_alive=tf; } 
     287    bool alive() { return is_alive; } 
     288
     289 
     290struct _EnterEvent 
     291
     292    // is anything needed here? 
    291293} 
    292294 
     
    299301alias _WindowCloseEvent* WindowCloseEvent; 
    300302alias _FocusEvent* FocusEvent; 
     303alias _EnterEvent* EnterEvent; 
     304alias _EnterEvent* LeaveEvent; 
    301305 
    302306alias void function(KeyEvent)         KeyEventFn; 
  • trunk/luigi/gui.d

    r15 r17  
    550550 
    551551 
     552    bool rollover() { return m_rollover; } 
     553 
    552554    //--------------------------------------------------- 
    553555    // Base widget event handling 
     
    578580        Button b = cast(Button)this; 
    579581    } 
     582    void on_enter(EnterEvent ev) { 
     583        m_rollover = true; 
     584    } 
     585 
     586    void on_leave(LeaveEvent ev) { 
     587        m_rollover = false; 
     588    } 
    580589 
    581590    void stretch(ubyte both)       { m_stretchx = m_stretchy = both; } 
     
    599608    bool m_enabled = true; 
    600609    bool m_focused = false; 
     610    bool m_rollover = false; 
    601611    Arranger m_arranger = null; 
    602612    ubyte m_stretchx = 0; // stretchyness for arrangement  
     
    10911101            // try to give it focus. 
    10921102            target.focus(); 
    1093             _event_to_widget(target, ev); 
     1103            _xform_event_to_widget(target, ev); 
    10941104            Luigi().theme.on_mouse_button(target, ev); 
    10951105        } 
    10961106    } 
    10971107    final void on_sys_mouse_move(MouseMoveEvent ev) 
     1108    { 
     1109        Widget target; 
     1110        target = find_item_at_point(Point(ev.winx,ev.winy)); 
     1111        if (target != m_hoverItem) { 
     1112            _EnterEvent eev; 
     1113            if (m_hoverItem)  
     1114                m_hoverItem.on_leave(&eev); 
     1115            if (target) 
     1116                target.on_enter(&eev); 
     1117            m_hoverItem = target; 
     1118        } 
     1119        if (m_grabItem) { 
     1120            target = m_grabItem; 
     1121        } 
     1122        if (target) { 
     1123            _xform_event_to_widget(target, ev); 
     1124            Luigi().theme.on_mouse_move(target, ev); 
     1125        } 
     1126    } 
     1127    final void on_sys_mouse_wheel(MouseWheelEvent ev) 
    10981128    { 
    10991129        Widget target; 
     
    11051135        } 
    11061136        if (target) { 
    1107             _event_to_widget(target, ev); 
    1108             Luigi().theme.on_mouse_move(target, ev); 
    1109         } 
    1110     } 
    1111     final void on_sys_mouse_wheel(MouseWheelEvent ev) 
    1112     { 
    1113         Widget target; 
    1114         if (m_grabItem) { 
    1115             target = m_grabItem; 
    1116         } 
    1117         else { 
    1118             target = find_item_at_point(Point(ev.winx,ev.winy)); 
    1119         } 
    1120         if (target) { 
    1121             _event_to_widget(target, ev); 
     1137            _xform_event_to_widget(target, ev); 
    11221138            Luigi().theme.on_mouse_wheel(target, ev); 
    11231139        } 
     
    11331149    } 
    11341150 
    1135     void _event_to_widget(EvT)(Widget w, inout EvT ev) { 
     1151    void _xform_event_to_widget(EvT)(Widget w, inout EvT ev) { 
    11361152        Point p = Point(ev.x,ev.y); 
    11371153        w.transform_window_to_widget(p); 
     
    11461162 
    11471163    Widget m_grabItem;  // item grabbing mouse. Could be any descendant 
    1148      
     1164    Widget m_hoverItem; // item containing mouse pointer currently 
    11491165} 
    11501166 
     
    12371253 
    12381254    bool depressed() { return m_depressed; } 
    1239     bool rollover() { return m_rollover; } 
    12401255 
    12411256    char[] label; 
     
    12471262protected: 
    12481263    bool m_depressed = false; 
    1249     bool m_rollover = false; 
    12501264} 
    12511265 
  • trunk/luigi/themes/dxut.d

    r15 r17  
    3535import luigi.gui; 
    3636 
     37import std.math : pow; 
     38import std.perf; 
     39 
    3740import derelict.opengl.extension.arb.texture_compression; 
    3841static const GLint GL_COMPRESSED_RGB_S3TC_DXT1_EXT  = 0x83F0; 
     
    4750} 
    4851 
    49  
    5052class DXUTTheme : ThemeBase 
    5153{ 
    5254    this() { 
     55        // Load texture first 
     56        try { 
     57            initialize_compressed_texture_extension(); 
     58            loadCompressedTexture("themes/dxutcontrols.dds", m_texid); 
     59            //loadCompressedTexture("themes/lena.dds", m_texid); 
     60            glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &m_texw); 
     61            glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &m_texh); 
     62        } 
     63        catch (Exception e) { 
     64            report_error(std.string.format("Couldn't load texture: ", e.toString)); 
     65        } 
     66 
     67        m_default_font = new ASCIIBitmapFont(); 
     68        m_timer = new PerformanceCounter; 
     69 
    5370        // Register all known size and draw functions. 
    5471        label_register(); 
     
    6178        radiogroup_register(); 
    6279        radiobutton_register(); 
    63  
    64         m_default_font = new ASCIIBitmapFont(); 
    65  
    66         // Load texture 
    67         try { 
    68             initialize_compressed_texture_extension(); 
    69             loadCompressedTexture("themes/dxutcontrols.dds", m_texid); 
    70             //loadCompressedTexture("themes/lena.dds", m_texid); 
    71             glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &m_texw); 
    72             glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &m_texh); 
    73         } 
    74         catch (Exception e) { 
    75             report_error(std.string.format("Couldn't load texture: ", e.toString)); 
    76         } 
    77  
    78         /* 
    79         bkg_color.set(212,208,200); 
    80         text_color.set(0,0,0); 
    81  
    82         sel_text_color.set(255,255,255); 
    83         sel_text_bkg_color.set(10,36,106); 
    84         sel_text_unfocus_bkg_color.set(176,176,176); 
    85  
    86         hilite_color[0].set(64,64,64); 
    87         hilite_color[1].set(128,128,128); 
    88         hilite_color[2].set(255,255,255); 
    89  
    90         m_bitmaps = new StdBitmapSet; 
    91  
    92         // Some default colors 
    93         set_background_color(Overlay.classinfo, Color(0,0,0,0)); 
    94         */ 
    9580    } 
    9681    ~this() { 
     
    9984    } 
    10085 
     86    //---------------------------------------------------------------------- 
     87    class ThemeData { 
     88        Element[] elements; 
     89    } 
     90    ThemeData get_theme_data(Widget w) { 
     91        Object td; 
     92        if (w && (td=w.theme_instance_data)!is null) { 
     93            return cast(ThemeData)td; 
     94        } 
     95        else { 
     96            ThemeData td2 = new ThemeData; 
     97            w.theme_instance_data = td2; 
     98            td2.elements = m_elements[w.classinfo].dup; 
     99            return td2; 
     100        } 
     101    } 
     102    Element[] get_widget_elements(Widget w) { 
     103        return get_theme_data(w).elements; 
     104    } 
     105    //---------------------------------------------------------------------- 
     106 
     107 
    101108    //============================================================================ 
    102109    // Built-in Drawing and size routines 
     
    115122    } 
    116123 
     124    /** Draw text centered in the rectangle */ 
     125    void draw_text_centered(Font font, char[] label, Rect rect) 
     126    { 
     127        Rect sr = font.string_rect(label); 
     128        float sx = rect.x+(rect.w-sr.w)/2; 
     129        float sy = rect.y+(rect.h-sr.h)/2; 
     130        Point trans = font.origin; trans.x+=sx; trans.y+=sy; 
     131        translate(trans); 
     132        font.draw_string(label); 
     133        untranslate(trans); 
     134    } 
    117135    /** Draw text so the upper left corner is at (x,y) */ 
    118     void draw_text(char[] label, float x, float y) 
    119     { 
    120         alias m_default_font font; 
     136    void draw_text(Font font, char[] label, float x, float y) 
     137    { 
    121138        Point trans = font.origin; trans.x+=x; trans.y+=y; 
    122139        translate(trans); 
     
    127144    //----GRPAHICS STATE------------------------------------------------------ 
    128145    void begin_drawing(Rect r) { 
     146        m_timer.stop(); 
     147        m_elapsedTime = m_timer.milliseconds / 1000.0; 
     148        m_timer.start(); 
     149         
    129150        gldraw.push_graphics_state(r); 
     151        glPushAttrib(GL_TEXTURE_BIT); 
     152 
     153        glEnable(GL_BLEND); 
     154        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
     155        glBindTexture(GL_TEXTURE_2D,m_texid); 
     156        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
     157 
     158        glMatrixMode(GL_TEXTURE); 
     159        glPushMatrix(); 
     160        glLoadIdentity(); 
     161        glScalef(1./m_texw, 1./m_texh,1); 
     162        glMatrixMode(GL_MODELVIEW); 
     163 
    130164    } 
    131165    void end_drawing() { 
     166        glPopAttrib(); // TEXTURE_BIT 
    132167        gldraw.pop_graphics_state(); 
     168        glMatrixMode(GL_TEXTURE); 
     169        glPopMatrix(); 
     170        glMatrixMode(GL_MODELVIEW); 
    133171    } 
    134172    //----LABEL--------------------------------------------------------------- 
     
    139177    void button_register() { 
    140178        addHandlers!(Button)(&button_draw, &button_best_size, &button_min_size); 
     179        Element[] elems = new Element[2]; 
     180        Element e; 
     181        // button layer 
     182        e.fontColor.states[ WidgetState.Disabled ] = Color(200,200,200,200); 
     183        e.set_texture( m_texid, [0,0,136,54] ); 
     184        e.set_font( m_default_font ); 
     185        e.texColor.states[ WidgetState.Normal ] = Color(255, 255, 255, 150); 
     186        e.texColor.states[ WidgetState.Depressed ] = Color(255, 255, 255, 200); 
     187        e.fontColor.states[ WidgetState.Rollover ] = Color(0, 0, 0, 255); 
     188        elems[0] = e; 
     189     
     190        // fill layer 
     191        e.set_texture( m_texid, [136, 0, 272, 54], Color(255, 255, 255, 0) ); 
     192        e.texColor.states[ WidgetState.Rollover ] = Color(255, 255, 255, 160); 
     193        e.texColor.states[ WidgetState.Depressed ] = Color(0, 0, 0, 60); 
     194        e.texColor.states[ WidgetState.Focus ] = Color(255, 255, 255, 30); 
     195        elems[1] = e; 
     196     
     197        m_elements[Button.classinfo] = elems; 
    141198    } 
    142199    Size button_min_size(Widget widget, Size bounds) { 
     
    150207        return Size(max(50.,minsz.w), max(22.,minsz.h)); 
    151208    } 
    152     int[4] button_tex_coord = [0,0,136,54]; 
    153     int[4] button_fill_tex_coord = [136,0,272,54]; 
    154209    void button_draw(Widget widget) { 
    155210        auto w = cast(Button)widget; assert(w); 
    156211        with (w) { 
    157             alias m_default_font f; 
    158             glEnable(GL_TEXTURE_2D); 
    159             glEnable(GL_BLEND); 
    160             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    161             glBindTexture(GL_TEXTURE_2D,m_texid); 
    162             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
    163  
    164             glMatrixMode(GL_TEXTURE); 
    165             glPushMatrix(); 
    166             glLoadIdentity(); 
    167             glScalef(1./m_texw, 1./m_texh,1); 
    168  
     212            int dx = 0; 
     213            int dy = 0; 
     214            WidgetState iState = WidgetState.Normal; 
     215            float blendRate = 0.8f; 
     216            if( !shown ) { 
     217                iState = WidgetState.Hidden;  
     218            } 
     219            else if( !enabled ) { 
     220                iState = WidgetState.Disabled;  
     221            } 
     222            else if( depressed ) 
    169223            { 
    170                 set_color(1); 
    171                 Rect r = rect; 
    172                 r.inset(1); 
    173                 if (depressed) { r.x += 1; r.y += 1; } 
    174                 else if (rollover)  { r.x -= 1; r.y -= 1; } 
    175                 fill_rect(r, button_tex_coord); 
     224                iState = WidgetState.Depressed; 
     225                dx = dy = 1; 
     226                blendRate = 0; 
    176227            } 
    177  
    178             glPopMatrix(); 
    179             glMatrixMode(GL_MODELVIEW); 
    180             glDisable(GL_TEXTURE_2D); 
    181  
     228            else if( rollover ) 
    182229            { 
    183                 set_color(1); 
    184                 Rect sr = f.string_rect(label); 
    185                 float x = w.rect.x+(w.rect.w-sr.w)/2; 
    186                 float y = w.rect.y+(w.rect.h-sr.h)/2; 
    187                 if (depressed) { x+=1; y+=1; } 
    188                 draw_text(label, x, y); 
     230                iState = WidgetState.Rollover; 
     231                dx = dy = -1; 
     232            } 
     233            else if( focused ) 
     234            { 
     235                iState = WidgetState.Focus; 
     236            } 
     237 
     238            //float elapsedTime = 1.0/60.; 
     239 
     240            Element[] elem = get_widget_elements(w); 
     241 
     242            Rect r = rect; 
     243            r.inset(1); 
     244            r.x += dx; 
     245            r.y += dy; 
     246 
     247            foreach(inout e; elem) { 
     248                e.texColor.blend( iState, m_elapsedTime, blendRate); 
     249                e.fontColor.blend( iState, m_elapsedTime, blendRate); 
     250                if (e.texColor.alpha != 0) { 
     251                    glEnable(GL_TEXTURE_2D); 
     252                    set_color(e.texColor.current); 
     253                    fill_rect(r, e.texRect); 
     254                } 
     255                if (e.fontColor.alpha != 0) { 
     256                    glDisable(GL_TEXTURE_2D); 
     257                    set_color(e.fontColor.current); 
     258                    draw_text_centered(e.font, label, r); 
     259                } 
    189260            } 
    190261        } 
     
    210281    int m_texw = 0; 
    211282    int m_texh = 0; 
     283    Element[][ClassInfo] m_elements; 
     284         
     285    PerformanceCounter m_timer; 
     286    float m_elapsedTime = 0; 
     287} 
     288 
     289 
     290//============================================================================ 
     291enum WidgetState 
     292{ 
     293    Normal = 0, 
     294    Disabled, 
     295    Hidden, 
     296    Focus, 
     297    Rollover, 
     298    Depressed 
     299} 
     300 
     301struct BlendColor 
     302{ 
     303    static BlendColor opCall( 
     304        Color defaultColor,  
     305        Color disabledColor = Color(128, 128, 128, 200),  
     306        Color hiddenColor = Color(0,0,0,0) )  
     307    { 
     308        BlendColor bc; bc.set(defaultColor,disabledColor,hiddenColor); 
     309        return bc; 
     310    } 
     311    void set( Color defaultColor,  
     312              Color disabledColor = Color(128, 128, 128, 200),  
     313              Color hiddenColor = Color(0,0,0,0) ) 
     314    { 
     315        for( int i=0; i <= WidgetState.max; i++ ) { 
     316            states[ i ] = defaultColor; 
     317        } 
     318        states[ WidgetState.Disabled ] = disabledColor; 
     319        states[ WidgetState.Hidden ] = hiddenColor; 
     320        current = hiddenColor; 
     321    } 
     322    void blend( WidgetState iState, float fElapsedTime, float fRate = 0.7f ) 
     323    { 
     324        current = Color.lerp(current, states[iState], 1.0f - pow( fRate, 30. * fElapsedTime ) ); 
     325    } 
     326 
     327    ubyte alpha() { return current.a; } 
     328     
     329    Color states[ WidgetState.max+1 ]; // Modulate colors for all possible control states 
     330    Color current; 
     331} 
     332 
     333struct Element 
     334{ 
     335    void set_texture(uint iTexID, int[4] textureRect,  
     336                     Color defaultTexColor = Color(255,255,255,255)) 
     337    { 
     338        iTexture = iTexID; 
     339        texRect[] = textureRect[]; 
     340     
     341        texColor.set( defaultTexColor ); 
     342    } 
     343    void set_font( Font fnt, Color defaultFontColor = Color(255, 255, 255, 255) )  
     344    { 
     345        font = fnt; 
     346        fontColor.set(defaultFontColor); 
     347    } 
     348 
     349    void refresh() { 
     350        texColor.current = texColor.states[ WidgetState.Hidden ]; 
     351        fontColor.current = fontColor.states[ WidgetState.Hidden ]; 
     352    } 
     353 
     354    uint iTexture;          // Index of the texture for this Element  
     355    Font font;              // Font object for this Element 
     356 
     357    int[4] texRect;       // Bounding rect of this element on the composite texture 
     358     
     359    BlendColor texColor; 
     360    BlendColor fontColor; 
    212361} 
    213362 
     
    497646 
    498647    fread( pDDSdata.pixels, 1, bufferSize, pFile ); 
     648    // need to do an endian swap on pixels for big-endian systems? 
    499649 
    500650    pDDSdata.width      = ddsinfo.width;