~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to minorGems/graphics/openGL/gui/ShadowImageButtonGL.h

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Modification History
 
3
 *
 
4
 * 2007-March-17    Jason Rohrer
 
5
 * Created.
 
6
 */
 
7
 
 
8
 
 
9
#ifndef SHADOW_IMAGE_BUTTON_GL_INCLUDED
 
10
#define SHADOW_IMAGE_BUTTON_GL_INCLUDED 
 
11
 
 
12
#include "ButtonGL.h"
 
13
 
 
14
#include "minorGems/graphics/Image.h"
 
15
 
 
16
#include "minorGems/graphics/openGL/SingleTextureGL.h"
 
17
 
 
18
#include <GL/gl.h>
 
19
 
 
20
 
 
21
/**
 
22
 * A textured button for GL-based GUIs that draws one image with a shadow
 
23
 *   to show pressed/unpressed status.
 
24
 *
 
25
 * @author Jason Rohrer
 
26
 */
 
27
class ShadowImageButtonGL : public ButtonGL {
 
28
 
 
29
 
 
30
        public:
 
31
 
 
32
 
 
33
 
 
34
                /**
 
35
                 * Constructs a button.
 
36
                 *
 
37
                 * @param inAnchorX the x position of the upper left corner
 
38
                 *   of this component.
 
39
                 * @param inAnchorY the y position of the upper left corner
 
40
                 *   of this component.
 
41
                 * @param inWidth the width of this component.
 
42
                 * @param inHeight the height of this component.
 
43
                 * @param inUnpressedImage the image to display
 
44
                 *   when this button is unpressed.  Must have dimensions
 
45
                 *   that are powers of 2.
 
46
                 *   Will be destroyed when this class is destroyed.
 
47
                 */
 
48
                ShadowImageButtonGL(
 
49
                        double inAnchorX, double inAnchorY, double inWidth,
 
50
                        double inHeight, Image *inUnpressedImage );
 
51
 
 
52
 
 
53
                
 
54
                ~ShadowImageButtonGL();
 
55
 
 
56
 
 
57
        // implements the ButtonGL interface
 
58
        virtual void drawPressed();
 
59
        virtual void drawUnpressed();
 
60
 
 
61
        
 
62
                
 
63
        protected:
 
64
                Image *mUnpressedImage;
 
65
 
 
66
                SingleTextureGL *mUnpressedTexture;
 
67
 
 
68
        
 
69
        /**
 
70
         * Draw this button with an optional z offset above it's shadow.
 
71
         *
 
72
         * @param inOffset true to draw the button above its shadow.
 
73
         */
 
74
        void drawWithOffset( char inOffset );
 
75
        
 
76
        };
 
77
 
 
78
 
 
79
 
 
80
inline ShadowImageButtonGL::ShadowImageButtonGL(
 
81
        double inAnchorX, double inAnchorY, double inWidth,
 
82
        double inHeight, Image *inUnpressedImage )
 
83
        : ButtonGL( inAnchorX, inAnchorY, inWidth, inHeight ),
 
84
          mUnpressedImage( inUnpressedImage ) {
 
85
 
 
86
        mUnpressedTexture = new SingleTextureGL( mUnpressedImage );
 
87
        }
 
88
 
 
89
 
 
90
 
 
91
inline ShadowImageButtonGL::~ShadowImageButtonGL() {
 
92
        delete mUnpressedImage;
 
93
 
 
94
        delete mUnpressedTexture;
 
95
        }
 
96
 
 
97
 
 
98
 
 
99
inline void ShadowImageButtonGL::drawPressed() {
 
100
    drawWithOffset( false );
 
101
    }
 
102
 
 
103
 
 
104
 
 
105
inline void ShadowImageButtonGL::drawUnpressed() {
 
106
    drawWithOffset( true );
 
107
    }
 
108
 
 
109
 
 
110
 
 
111
inline void ShadowImageButtonGL::drawWithOffset( char inOffset ) {
 
112
 
 
113
    // calculate offset for shadow
 
114
    double xOffset = mWidth * 0.05;
 
115
    double yOffset = mHeight * 0.05;
 
116
 
 
117
    // use smaller
 
118
    double offset = xOffset;
 
119
    if( yOffset < xOffset ) {
 
120
        offset = yOffset;
 
121
        }
 
122
 
 
123
    
 
124
    // set our texture
 
125
        mUnpressedTexture->enable();
 
126
 
 
127
    // shadow first
 
128
        glColor4f( 0.0, 0.0, 0.0, 0.5 ); 
 
129
    
 
130
        // shadow in lower left corner 
 
131
        glBegin( GL_QUADS ); {
 
132
                
 
133
                glTexCoord2f( 0, 1.0f );
 
134
                glVertex2d( mAnchorX, mAnchorY );
 
135
 
 
136
                glTexCoord2f( 1.0f, 1.0f );
 
137
                glVertex2d( mAnchorX + mWidth - offset, mAnchorY ); 
 
138
 
 
139
                glTexCoord2f( 1.0f, 0 );
 
140
                glVertex2d( mAnchorX + mWidth - offset, mAnchorY + mHeight - offset );
 
141
                
 
142
                glTexCoord2f( 0, 0 );
 
143
                glVertex2d( mAnchorX, mAnchorY + mHeight - offset );
 
144
                }
 
145
        glEnd();
 
146
 
 
147
    // now button image
 
148
    glColor4f( 1.0, 1.0, 1.0, 10 );
 
149
 
 
150
    double anchorOffset = 0;
 
151
    if( inOffset ) {
 
152
        // put it in upper right corner
 
153
        anchorOffset = offset;
 
154
        offset = 0;
 
155
        }
 
156
    
 
157
    glBegin( GL_QUADS ); {
 
158
                
 
159
                glTexCoord2f( 0, 1.0f );
 
160
                glVertex2d( mAnchorX + anchorOffset,
 
161
                    mAnchorY + anchorOffset );
 
162
 
 
163
                glTexCoord2f( 1.0f, 1.0f );
 
164
                glVertex2d( mAnchorX + mWidth - offset,
 
165
                    mAnchorY + anchorOffset ); 
 
166
 
 
167
                glTexCoord2f( 1.0f, 0 );
 
168
                glVertex2d( mAnchorX + mWidth - offset,
 
169
                    mAnchorY + mHeight - offset );
 
170
                
 
171
                glTexCoord2f( 0, 0 );
 
172
                glVertex2d( mAnchorX + anchorOffset,
 
173
                    mAnchorY + mHeight - offset );
 
174
                }
 
175
        glEnd();
 
176
    
 
177
    
 
178
        mUnpressedTexture->disable();
 
179
    }
 
180
 
 
181
 
 
182
 
 
183
#endif
 
184
 
 
185
 
 
186