-
Notifications
You must be signed in to change notification settings - Fork 1
/
button.cpp
110 lines (91 loc) · 3.05 KB
/
button.cpp
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
#include "button.h"
Button:: Button()
:Entity( BUTTON, "", UNDESTRUCTIBLE, UNDESTRUCTIBLE, Point( 0, 0 ), Point( 0, 0 ), EMPTY, RECTANGLE, true, false )
{
toggleOn = OFF;
frameTouched = 0;
window = Window::getInstance();
dir = UP;
setImages( "./data/images/btnUp.png", "./data/images/btnDown.png" );
resourceMgr->loadSound( "./data/audio/buttonSound.wav", "button" );
}
Button:: Button( string name, Point uL, Point lR , Direction d)
:Entity( BUTTON, name, UNDESTRUCTIBLE, UNDESTRUCTIBLE, uL, lR, EMPTY, RECTANGLE, true, false )
{
toggleOn = OFF;
frameTouched = 0;
window = Window::getInstance();
dir = d;
resourceMgr->loadSound( "./data/audio/buttonSound.wav", "button" );
setImages( "./data/images/btnUp.png", "./data/images/btnDown.png" );
}
void Button:: onTouch()
{
if ( window->getFrameNumber() - frameTouched > 2 )
{
//Play sound effect
resourceMgr->playSound( "button" );
toggleOn = !toggleOn;
if(toggleOn)
Entity::onTouch();
else
Entity::onUnTouch();
}
frameTouched = window->getFrameNumber();
}
void Button:: draw()
{
glColor3f( 0, 1, 1 );
if ( toggleOn )
{
if ( dir == UP )
resourceMgr->drawImage( imageDown, upperLeft.getX(), lowerRight.getX(),
lowerLeft.getY(), upperRight.getY() );
else if ( dir == DOWN )
resourceMgr->drawImage( imageDown, upperLeft.getX(), lowerRight.getX(),
upperLeft.getY(), lowerRight.getY() );
else if ( dir == RIGHT )
resourceMgr->drawImage( imageDown, upperRight, lowerRight,
upperLeft, lowerLeft) ;
else if ( dir == LEFT )
resourceMgr->drawImage( imageDown, upperLeft, lowerLeft,
upperRight, lowerRight) ;
}
else
{
if ( dir == UP )
resourceMgr->drawImage( imageUp, upperLeft.getX(), lowerRight.getX(),
lowerLeft.getY(), upperRight.getY() );
else if ( dir == DOWN )
resourceMgr->drawImage( imageUp, upperLeft.getX(), lowerRight.getX(),
upperLeft.getY(), lowerRight.getY() );
else if ( dir == RIGHT )
resourceMgr->drawImage( imageUp, upperRight, lowerRight,
upperLeft, lowerLeft) ;
else if ( dir == LEFT )
resourceMgr->drawImage( imageUp, upperLeft, lowerLeft,
upperRight, lowerRight) ;
}
}
void Button:: setToggle( bool status )
{
toggleOn = status;
}
void Button:: setImages( string u, string d )
{
imageUp = u;
imageDown = d;
resourceMgr->loadImage( imageUp, imageUp );
resourceMgr->loadImage( imageDown, imageDown );
}
Button:: ~Button()
{
window->release();
}
void Button:: onAlways()
{
//if ( toggleOn )
// Entity:: onTouch();
// else
// Entity:: onUnTouch();
}