LaunchpadScreen.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "LaunchpadScreen.h"
  2. #include <string>
  3. namespace midi {
  4. void LaunchpadScreen::keyPressed(unsigned char x, unsigned char y)
  5. {
  6. }
  7. void LaunchpadScreen::keyReleased(unsigned char x, unsigned char y)
  8. {
  9. }
  10. const LaunchpadColor& LaunchpadScreen::getColor(unsigned char x, unsigned char y) const
  11. {
  12. return colors[x][y];
  13. }
  14. void LaunchpadScreen::setColor(unsigned char x, unsigned char y, const LaunchpadColor& color)
  15. {
  16. if(active && (!launchpad->issetColor(x, y) || launchpad->getColor(x, y) != color)) {
  17. launchpad->setColor(x, y, color);
  18. }
  19. colors[x][y] = color;
  20. }
  21. void LaunchpadScreen::setColorAll(const LaunchpadColor& color)
  22. {
  23. for(unsigned char x = 0; x < Launchpad::width; x++) {
  24. for(unsigned char y = 0; y < Launchpad::height; y++) {
  25. if(x != 8 || y != 8)
  26. {
  27. setColor(x, y, color);
  28. }
  29. }
  30. }
  31. }
  32. void LaunchpadScreen::sync()
  33. {
  34. for(unsigned char x = 0; x < Launchpad::width; x++) {
  35. for(unsigned char y = 0; y < Launchpad::height; y++) {
  36. if((x != 8 || y != 8)
  37. && (!launchpad->issetColor(x, y) || launchpad->getColor(x, y) != colors[x][y])) {
  38. launchpad->setColor(x, y, colors[x][y]);
  39. }
  40. }
  41. }
  42. }
  43. bool LaunchpadScreen::getKeyPressed(KeyCoordinate x, KeyCoordinate y)
  44. {
  45. return enabled() && launchpad->getKeyPressed(x, y);
  46. }
  47. LaunchpadScreen::LaunchpadScreen()
  48. : launchpad(0), active(false)
  49. {
  50. }
  51. void LaunchpadScreen::enable()
  52. {
  53. if(!launchpad) {
  54. throw "no launchpad set";
  55. }
  56. launchpad->keyEventCallbackData = (void*)this;
  57. launchpad->keyPressedCallback = keyPressedCallback;
  58. launchpad->keyReleasedCallback = keyReleasedCallback;
  59. sync();
  60. active = true;
  61. }
  62. void LaunchpadScreen::disable()
  63. {
  64. launchpad->keyPressedCallback = 0;
  65. launchpad->keyReleasedCallback = 0;
  66. launchpad->keyEventCallbackData = 0;
  67. active = false;
  68. }
  69. bool LaunchpadScreen::enabled() const
  70. {
  71. return active;
  72. }
  73. void LaunchpadScreen::setLaunchpad(Launchpad& l)
  74. {
  75. if(enabled()) {
  76. disable();
  77. launchpad = &l;
  78. enable();
  79. } else {
  80. launchpad = &l;
  81. }
  82. }
  83. void LaunchpadScreen::keyPressedCallback(unsigned char x, unsigned char y, void* screen)
  84. {
  85. ((LaunchpadScreen*)screen)->keyPressed(x, y);
  86. }
  87. void LaunchpadScreen::keyReleasedCallback(unsigned char x, unsigned char y, void* screen)
  88. {
  89. ((LaunchpadScreen*)screen)->keyReleased(x, y);
  90. }
  91. } // namespace