annotate Source/Display/KeyboardDisplay.h @ 27:eef567a60146

Changed how OpenGL updates are handled so the canvas is only re-rendered when something changes. This should reduce CPU usage in certain circumstances.
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Sun, 02 Mar 2014 19:25:50 +0000
parents 73d2ec21de9a
children 73576f49ad1c
rev   line source
andrewm@0 1 /*
andrewm@0 2 TouchKeys: multi-touch musical keyboard control software
andrewm@0 3 Copyright (c) 2013 Andrew McPherson
andrewm@0 4
andrewm@0 5 This program is free software: you can redistribute it and/or modify
andrewm@0 6 it under the terms of the GNU General Public License as published by
andrewm@0 7 the Free Software Foundation, either version 3 of the License, or
andrewm@0 8 (at your option) any later version.
andrewm@0 9
andrewm@0 10 This program is distributed in the hope that it will be useful,
andrewm@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
andrewm@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
andrewm@0 13 GNU General Public License for more details.
andrewm@0 14
andrewm@0 15 You should have received a copy of the GNU General Public License
andrewm@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
andrewm@0 17
andrewm@0 18 =====================================================================
andrewm@0 19
andrewm@0 20 KeyboardDisplay.h: displays the keyboard state, including active MIDI
andrewm@0 21 notes and current touch position and size.
andrewm@0 22 */
andrewm@0 23
andrewm@0 24 #ifndef KEYBOARD_DISPLAY_H
andrewm@0 25 #define KEYBOARD_DISPLAY_H
andrewm@0 26
andrewm@0 27 #include <iostream>
andrewm@0 28 #include <map>
andrewm@0 29 //#include <OpenGL/gl.h>
andrewm@0 30 #include "../JuceLibraryCode/JuceHeader.h"
andrewm@0 31 #include "../TouchKeys/KeyTouchFrame.h"
andrewm@0 32 #include "OpenGLDisplayBase.h"
andrewm@0 33
andrewm@0 34
andrewm@0 35 // This class uses OpenGL to implement the actual drawing of the piano keyboard graphics.
andrewm@0 36 // Graphics include the current state of each key and the touches on the surface.
andrewm@0 37
andrewm@0 38 class KeyboardDisplay : public OpenGLDisplayBase {
andrewm@0 39 // Internal data structures and constants
andrewm@17 40 protected:
andrewm@0 41 // Display dimensions, normalized to the width of one white key
andrewm@0 42
andrewm@0 43 static const float kWhiteKeyFrontWidth;
andrewm@0 44 static const float kBlackKeyWidth;
andrewm@0 45 static const float kWhiteKeyFrontLength;
andrewm@0 46 static const float kWhiteKeyBackLength;
andrewm@0 47 static const float kBlackKeyLength;
andrewm@0 48 static const float kInterKeySpacing;
andrewm@0 49 static const float kAnalogSliderVerticalSpacing;
andrewm@0 50 static const float kAnalogSliderLength;
andrewm@0 51 static const float kAnalogSliderWidth;
andrewm@0 52 static const float kAnalogSliderMinimumValue;
andrewm@0 53 static const float kAnalogSliderMaximumValue;
andrewm@0 54 static const float kAnalogSliderZeroLocation;
andrewm@0 55 static const float kAnalogSliderOneLocation;
andrewm@0 56
andrewm@0 57 // Individual geometry for C, D, E, F, G, A, B, c'
andrewm@0 58
andrewm@0 59 static const float kWhiteKeyBackOffsets[9];
andrewm@0 60 static const float kWhiteKeyBackWidths[9];
andrewm@0 61
andrewm@0 62 // Display margins
andrewm@0 63
andrewm@0 64 static const float kDisplaySideMargin;
andrewm@0 65 static const float kDisplayBottomMargin;
andrewm@0 66 static const float kDisplayTopMargin;
andrewm@0 67
andrewm@0 68 // Key shape constants
andrewm@0 69
andrewm@0 70 static const int kShapeForNote[12];
andrewm@0 71 static const int kWhiteToChromatic[7];
andrewm@0 72 static const float kWhiteKeyFrontBackCutoff;
andrewm@0 73
andrewm@0 74 // Touch constants
andrewm@0 75 static const float kDisplayMinTouchSize;
andrewm@0 76 static const float kDisplayTouchSizeScaler;
andrewm@0 77
andrewm@0 78
andrewm@0 79 typedef struct {
andrewm@0 80 bool active;
andrewm@0 81 float locH;
andrewm@0 82 float locV1;
andrewm@0 83 float locV2;
andrewm@0 84 float locV3;
andrewm@0 85 float size1;
andrewm@0 86 float size2;
andrewm@0 87 float size3;
andrewm@0 88 } TouchInfo;
andrewm@0 89
andrewm@0 90 typedef struct {
andrewm@0 91 float x;
andrewm@0 92 float y;
andrewm@0 93 } Point;
andrewm@0 94
andrewm@0 95 public:
andrewm@0 96 KeyboardDisplay();
andrewm@17 97 virtual ~KeyboardDisplay() {}
andrewm@27 98
andrewm@27 99 // Set canvas for triggering rendering;
andrewm@27 100 void setCanvas(OpenGLJuceCanvas *canvas) { canvas_ = canvas; }
andrewm@27 101 void tellCanvasToRepaint();
andrewm@0 102
andrewm@0 103 // Setup methods for display size and keyboard range
andrewm@0 104 void setKeyboardRange(int lowest, int highest);
andrewm@0 105 float keyboardAspectRatio() { return totalDisplayWidth_ / totalDisplayHeight_; }
andrewm@17 106 virtual void setDisplaySize(float width, float height);
andrewm@0 107
andrewm@0 108 // Drawing methods
andrewm@17 109 virtual void render();
andrewm@0 110
andrewm@0 111 // Interaction methods
andrewm@17 112 virtual void mouseDown(float x, float y);
andrewm@17 113 virtual void mouseDragged(float x, float y);
andrewm@17 114 virtual void mouseUp(float x, float y);
andrewm@17 115 virtual void rightMouseDown(float x, float y);
andrewm@17 116 virtual void rightMouseDragged(float x, float y);
andrewm@17 117 virtual void rightMouseUp(float x, float y);
andrewm@0 118
andrewm@0 119 // Take action associated with clicking a key. These are called within the mouse
andrewm@0 120 // methods but may also be called externally.
andrewm@17 121 virtual void keyClicked(int key);
andrewm@17 122 virtual void keyRightClicked(int key);
andrewm@0 123
andrewm@0 124 // State-change methods
andrewm@0 125 void setTouchForKey(int key, const KeyTouchFrame& touch);
andrewm@0 126 void clearTouchForKey(int key);
andrewm@0 127 void clearAllTouches();
andrewm@0 128 void setAnalogCalibrationStatusForKey(int key, bool isCalibrated);
andrewm@0 129 void setAnalogValueForKey(int key, float value);
andrewm@0 130 void clearAnalogData();
andrewm@0 131 void setMidiActive(int key, bool active);
andrewm@0 132 void clearMidiData();
andrewm@0 133
andrewm@0 134 void setAnalogSensorsPresent(bool present) { analogSensorsPresent_ = present; }
andrewm@0 135 void setTouchSensorPresentForKey(int key, bool present);
andrewm@0 136 void setTouchSensingEnabled(bool enabled);
andrewm@0 137
andrewm@17 138 protected:
andrewm@0 139 void drawWhiteKey(float x, float y, int shape, bool first, bool last, bool highlighted);
andrewm@0 140 void drawBlackKey(float x, float y, bool highlighted);
andrewm@0 141
andrewm@0 142 void drawWhiteTouch(float x, float y, int shape, float touchLocH, float touchLocV, float touchSize);
andrewm@0 143 void drawBlackTouch(float x, float y, float touchLocH, float touchLocV, float touchSize);
andrewm@0 144
andrewm@0 145 void drawAnalogSlider(float x, float y, bool calibrated, bool whiteKey, float value);
andrewm@0 146
andrewm@0 147 // Indicate the shape of the given MIDI note. 0-6 for white keys C-B, -1 for black keys.
andrewm@0 148 // We handle unusual shaped keys at the top or bottom of the keyboard separately.
andrewm@0 149
andrewm@0 150 int keyShape(int key) {
andrewm@0 151 if(key < 0) return -1;
andrewm@0 152 return kShapeForNote[key % 12];
andrewm@0 153 }
andrewm@0 154
andrewm@0 155 void refreshViewport();
andrewm@0 156
andrewm@0 157 // Conversion from internal coordinate space to external pixel values and back
andrewm@0 158 Point screenToInternal(Point& inPoint);
andrewm@0 159 Point internalToScreen(Point& inPoint);
andrewm@0 160
andrewm@0 161 // Figure out which key (if any) the current point corresponds to
andrewm@0 162 int keyForLocation(Point& internalPoint);
andrewm@0 163
andrewm@17 164 protected:
andrewm@27 165 OpenGLJuceCanvas *canvas_; // Reference to object which handles rendering
andrewm@27 166
andrewm@0 167 int lowestMidiNote_, highestMidiNote_; // Which keys should be displayed (use MIDI note numbers)
andrewm@0 168 float totalDisplayWidth_, totalDisplayHeight_; // Size of the internal view (centered around origin)
andrewm@0 169 float displayPixelWidth_, displayPixelHeight_; // Pixel resolution of the surrounding window
andrewm@0 170 int currentHighlightedKey_; // What key is being clicked on at the moment
andrewm@0 171 bool touchSensingEnabled_; // Whether touch-sensitive keys are being used
andrewm@0 172 bool touchSensingPresentOnKey_[128]; // Whether the key with this MIDI note has a touch sensor
andrewm@0 173
andrewm@0 174 bool analogSensorsPresent_; // Whether the given device has analog sensors at all
andrewm@0 175 bool analogValueIsCalibratedForKey_[128]; // Whether the analog sensor is calibrated on this key
andrewm@0 176 float analogValueForKey_[128]; // Latest analog sensor value for each key
andrewm@0 177 bool midiActiveForKey_[128]; // Whether the MIDI note is on for each key
andrewm@0 178
andrewm@0 179 TouchInfo currentTouches_[128]; // Touch data for each key
andrewm@0 180 TouchInfo currentTouchesMirror_[128]; // Mirror of the above, used for active display
andrewm@0 181 //std::map<int, TouchInfo> currentTouches_; // Collection of current touch data
andrewm@0 182 CriticalSection displayMutex_; // Synchronize access between data and display threads
andrewm@0 183 };
andrewm@0 184
andrewm@0 185 #endif /* KEYBOARD_DISPLAY_H */