1 /*
2  *  tracker/PianoControl.h
3  *
4  *  Copyright 2009 Peter Barth
5  *
6  *  This file is part of Milkytracker.
7  *
8  *  Milkytracker is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  Milkytracker is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with Milkytracker.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef PIANOCONTROL__H
24 #define PIANOCONTROL__H
25 
26 #include "BasicTypes.h"
27 #include "Control.h"
28 #include "Event.h"
29 
30 class PianoControl : public PPControl, public EventListenerInterface
31 {
32 public:
33 	enum Modes
34 	{
35 		ModeEdit = 0,
36 		ModePlay = 1
37 	};
38 
39 private:
40 	pp_int32 XMAX();
41 	pp_int32 YMAX();
42 	pp_int32 KEYWIDTH();
43 
44 	const pp_uint8 NUMNOTES;
45 
46 	bool border;
47 	PPColor ourOwnBorderColor;
48 	const PPColor* borderColor;
49 
50 	class PPScrollbar* hScrollbar;
51 
52 	PPControl* caughtControl;
53 	bool controlCaughtByLMouseButton, controlCaughtByRMouseButton;
54 
55 	// extent
56 	pp_int32 xscale;
57 	pp_int32 yscale;
58 
59 	pp_int32 xMax;
60 	pp_int32 yMax;
61 
62 	pp_int32 startPos;
63 	pp_int32 visibleWidth;
64 	pp_int32 visibleHeight;
65 
66 	pp_int32 sampleIndex;
67 
68 	pp_uint8* nbu;
69 
70 	struct KeyState
71 	{
72 		bool pressed;
73 		bool muted;
74 
KeyStateKeyState75 		KeyState() :
76 			pressed(false),
77 			muted(false)
78 		{
79 		}
80 	};
81 
82 	KeyState* keyState;
83 
84 	Modes mode;
85 	pp_int32 currentSelectedNote;
86 
87 	class PianoBitmapBase* pianoBitmap;
88 
89 public:
90 	PianoControl(pp_int32 id,
91 				 PPScreen* parentScreen,
92 				 EventListenerInterface* eventListener,
93 				 const PPPoint& location,
94 				 const PPSize& size,
95 				 pp_uint8 numNotes,
96 				 bool border = true);
97 
98 	~PianoControl();
99 
setMode(Modes mode)100 	void setMode(Modes mode) { this->mode = mode; }
getMode()101 	Modes getMode() const { return mode; }
102 
setBorderColor(const PPColor & color)103 	void setBorderColor(const PPColor& color) { this->borderColor = &color; }
104 
setxMax(pp_int32 xMax)105 	void setxMax(pp_int32 xMax) { this->xMax = xMax; adjustScrollbars(); }
getxMax()106 	pp_int32 getxMax() const { return xMax; }
107 
setyMax(pp_int32 yMax)108 	void setyMax(pp_int32 yMax) { this->yMax = yMax; adjustScrollbars(); }
getyMax()109 	pp_int32 getyMax() const { return yMax; }
110 
111 	void setxScale(pp_int32 scale);
112 	void setyScale(pp_int32 scale);
113 
getxScale()114 	pp_int32 getxScale() const { return xscale; }
getyScale()115 	pp_int32 getyScale() const { return yscale; }
116 
117 	void setSampleTable(const pp_uint8* nbu);
118 
setSampleIndex(pp_int32 index)119 	void setSampleIndex(pp_int32 index) { sampleIndex = index; }
120 
121 	void pressNote(pp_int32 note, bool pressed, bool muted = false);
122 
123 	bool getNoteState(pp_int32 note) const;
124 
125 	void assureNoteVisible(pp_int32 note);
126 
127 	// from PPControl
128 	virtual void paint(PPGraphicsAbstract* graphics);
129 	virtual pp_int32 dispatchEvent(PPEvent* event);
130 	virtual pp_int32 handleEvent(PPObject* sender, PPEvent* event);
131 
132 	virtual void setLocation(const PPPoint& location);
133 	virtual void setSize(const PPSize& size);
134 
135 private:
136 	pp_int32 getMaxWidth();
137 
138 	void adjustScrollbars();
139 
140 	pp_int32 positionToNote(PPPoint cp);
141 };
142 
143 
144 #endif
145