LaunchpadScreen.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. LaunchpadScreen::LaunchpadScreen()
  44. : launchpad(0), active(false)
  45. {
  46. }
  47. void LaunchpadScreen::enable()
  48. {
  49. if(!launchpad) {
  50. throw "no launchpad set";
  51. }
  52. launchpad->keyEventCallbackData = (void*)this;
  53. launchpad->keyPressedCallback = keyPressedCallback;
  54. launchpad->keyReleasedCallback = keyReleasedCallback;
  55. sync();
  56. active = true;
  57. }
  58. void LaunchpadScreen::disable()
  59. {
  60. launchpad->keyPressedCallback = 0;
  61. launchpad->keyReleasedCallback = 0;
  62. launchpad->keyEventCallbackData = 0;
  63. active = false;
  64. }
  65. bool LaunchpadScreen::enabled() const
  66. {
  67. return active;
  68. }
  69. void LaunchpadScreen::setLaunchpad(Launchpad& l)
  70. {
  71. if(enabled()) {
  72. disable();
  73. launchpad = &l;
  74. enable();
  75. } else {
  76. launchpad = &l;
  77. }
  78. }
  79. void LaunchpadScreen::keyPressedCallback(unsigned char x, unsigned char y, void* screen)
  80. {
  81. ((LaunchpadScreen*)screen)->keyPressed(x, y);
  82. }
  83. void LaunchpadScreen::keyReleasedCallback(unsigned char x, unsigned char y, void* screen)
  84. {
  85. ((LaunchpadScreen*)screen)->keyReleased(x, y);
  86. }
  87. } // namespace