File: StickyButtonGL.h

package info (click to toggle)
transcend 0.3.dfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,768 kB
  • sloc: cpp: 26,891; ansic: 693; sh: 210; makefile: 96; perl: 67
file content (162 lines) | stat: -rw-r--r-- 3,487 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Modification History
 *
 * 2001-September-16		Jason Rohrer
 * Created.
 */
 
 
#ifndef STICKY_BUTTON_GL_INCLUDED
#define STICKY_BUTTON_GL_INCLUDED 

#include "ButtonGL.h"

#include "minorGems/graphics/Image.h"

#include "minorGems/graphics/openGL/SingleTextureGL.h"



/**
 * A button that can be toggled between a pressed
 * and unpressed state.
 *
 * @author Jason Rohrer
 */
class StickyButtonGL : public ButtonGL {


	public:



		/**
		 * Constructs a button.
		 *
		 * @param inAnchorX the x position of the upper left corner
		 *   of this component.
		 * @param inAnchorY the y position of the upper left corner
		 *   of this component.
		 * @param inWidth the width of this component.
		 * @param inHeight the height of this component.
		 * @param inUnpressedImage the image to display
		 *   when this button is unpressed.  Must have dimensions
		 *   that are powers of 2.
		 *   Will be destroyed when this class is destroyed.
		 * @param inPressedImage the image to display
		 *   when this button is pressed.  Must have dimensions
		 *   that are powers of 2.
		 *   Will be destroyed when this class is destroyed.
		 */
		StickyButtonGL(
			double inAnchorX, double inAnchorY, double inWidth,
			double inHeight, Image *inUnpressedImage,
			Image *inPressedImage );



		/**
		 * Gets whether this button is toggled to a pressed state.
		 *
		 * @return true iff this button is pressed.
		 */
		virtual char isPressed();



		/**
		 * Sets this button's state.
		 *
		 * Note that if this function call causes
		 * this button's state to change, then all
		 * registered ActionListeners are notified.
		 *
		 * @param inPressed true iff this button should be
		 *   pressed.
		 */
		virtual void setPressed( char inPressed );

		
		
		// override funxtions in GUIComponentGL
		virtual void mouseDragged( double inX, double inY );
		virtual void mousePressed( double inX, double inY );
		virtual void mouseReleased( double inX, double inY );

	};



inline StickyButtonGL::StickyButtonGL(
	double inAnchorX, double inAnchorY, double inWidth,
	double inHeight, Image *inUnpressedImage,
	Image *inPressedImage  )
	: ButtonGL( inAnchorX, inAnchorY, inWidth, inHeight,
	  inUnpressedImage, inPressedImage ) {

	}



inline char StickyButtonGL::isPressed() {
	if( mCurrentTexture == mPressedTexture ) {
		return true;
		}
	else {
		return false;
		}
	}



inline void StickyButtonGL::setPressed( char inPressed ) {
	SingleTextureGL *lastTexture = mCurrentTexture;

	if( inPressed ) {
		mCurrentTexture = mPressedTexture;
		}
	else {
		mCurrentTexture = mUnpressedTexture;
		}

	// check for state change
	if( lastTexture != mCurrentTexture ) {
		fireActionPerformed( this );
		}
	}



inline void StickyButtonGL::mouseDragged( double inX, double inY ) {
	// do nothing until the release
	}



inline void StickyButtonGL::mousePressed( double inX, double inY ) {
	// do nothing until the release
	}



inline void StickyButtonGL::mouseReleased( double inX, double inY ) {
	// Note the following situation:
	// If the mouse is pressed on another button and then
	// released on this button, this button will toggle...
	// This is non-standard behavior, but the situation occurs
	// rarely.  By ignoring this situation, we save a bit of coding,
	// but this may need to be fixed later.
	
	if( isInside( inX, inY ) ) {
		// toggle our state to the opposite of it's current setting
		setPressed( !isPressed() );
		// this will fire an action automatically
		}
	}



#endif