PadSelection.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "PadSelection.h"
  2. #include "PadDefinition.h"
  3. #include "Button.h"
  4. #include "Controller.h"
  5. #include "Operations.h"
  6. #include "PadConfiguration.h"
  7. #include <algorithm>
  8. InputInit::~InputInit()
  9. {
  10. int s = actions.size();
  11. for(int i=0; i<s; i++) delete actions[i];
  12. }
  13. PadSelection::PadSelection(const std::string& name, Json::Value& v, PadConfiguration* p) : m_pad(p)
  14. {
  15. m_name=name;
  16. m_width=p->get_pad_definition().get_width();
  17. m_height=p->get_pad_definition().get_height();
  18. m_matrix.resize(m_width*m_height);
  19. for(int i=0; i<m_width*m_height; i++) m_matrix[i]=NULL;
  20. _update_matrix();
  21. Json::Value a = v["operations"];
  22. int s = a.size();
  23. for(int i=0; i<s; i++)
  24. {
  25. m_operations.push_back(InputOperation::from_json(a[i]));
  26. }
  27. m_init.add( v["init"]);
  28. }
  29. void PadSelection::send_init()
  30. {
  31. int s = m_list.size();
  32. for(int i=0; i<s; i++)
  33. {
  34. MidiMessageList* msg = m_init.get_messages();
  35. m_list[i]->control(msg);
  36. }
  37. }
  38. PadSelection::~PadSelection()
  39. {
  40. int s = m_operations.size();
  41. for(int i=0; i<s; i++)
  42. delete m_operations[i];
  43. }
  44. void PadSelection::_update_matrix()
  45. {
  46. m_list.clear();
  47. for(int j=m_height-1; j>=0; j--)
  48. {
  49. for(int i=0; i<m_width; i++)
  50. {
  51. AbsInput* b = m_matrix[j*m_width+i];
  52. if(b) m_list.push_back(b);
  53. }
  54. }
  55. }
  56. static bool compare(AbsInput* a, AbsInput* b){
  57. return a->index<b->index;
  58. }
  59. void PadSelection::add(int i, int j)
  60. {
  61. int x = i+j*m_width;
  62. AbsInput* p = m_pad->get_pad_definition().at(i,j);
  63. if(!m_matrix[x]){
  64. m_list.push_back(p);
  65. }
  66. m_matrix[x]=m_pad->get_pad_definition().at(i,j);
  67. m_matrix[x]->set_selection(this);
  68. std::sort(m_list.begin(), m_list.end(), compare);
  69. }
  70. AbsInput* PadSelection::at(int i, int j)
  71. {
  72. return m_matrix[i+j*m_width];
  73. }
  74. void PadSelection::add(AbsInput* b)
  75. {
  76. return add(b->x, b->y);
  77. }
  78. void PadSelection::remove(int i, int j)
  79. {
  80. int x = i+j*m_pad->get_pad_definition().get_width();
  81. m_matrix[x]=NULL;
  82. _update_matrix();
  83. m_matrix[x]->set_selection(this);
  84. }
  85. void PadSelection::remove(AbsInput* b)
  86. {
  87. return remove(b->x, b->y);
  88. }
  89. bool PadSelection::has(int x, int y) const
  90. {
  91. return m_matrix[x+y*m_width];
  92. }
  93. bool PadSelection::has(AbsInput* p) const
  94. {
  95. return has( p->x, p->y);
  96. }
  97. int PadSelection::count_x(int x) const{
  98. int s=m_list.size(), count=0;
  99. for(int i=0; i<s; i++)
  100. if(m_list[i]->x==x)
  101. count++;
  102. return count;
  103. }
  104. int PadSelection::count_y(int y) const{
  105. int s=m_list.size(), count=0;
  106. for(int i=0; i<s; i++)
  107. if(m_list[i]->y==y)
  108. count++;
  109. return count;
  110. }
  111. int PadSelection::index_at_x(int absindex) const
  112. {
  113. int x = absindex%m_width;
  114. int y = absindex/m_width;
  115. int s=m_list.size(), count=0;
  116. for(int i=0; i<s; i++)
  117. if(m_list[i]->y==y && m_list[i]->x<x)
  118. count++;
  119. return count;
  120. }
  121. int PadSelection::index_at_y(int absindex) const
  122. {
  123. int x = absindex%m_width;
  124. int y = absindex/m_width;
  125. int s=m_list.size(), count=0;
  126. for(int i=0; i<s; i++)
  127. if(m_list[i]->y==y && m_list[i]->x<x)
  128. count++;
  129. return count;
  130. }
  131. /*
  132. MidiMessage* PadSelection::execute(AbsInput* in, MidiMessage* m) const
  133. {
  134. int s = m_list.size();
  135. for(int i=0; i<s; i++){
  136. if(m_list[i]==in){
  137. int s = m_operations.size();
  138. for(int j=0; j<s; j++)
  139. {
  140. if(!m_operations[j]->execute((PadSelection*)this, m, i)) return NULL;
  141. }
  142. return m;
  143. }
  144. }
  145. return m;
  146. }*/
  147. MidiMessage* PadSelection::execute(AbsInput* in, MidiMessage* m) const
  148. {
  149. int s = m_list.size(), i, count=0;
  150. for(int y=m_height-1; y>=in->y; y--){
  151. for(int x=0; x<m_width; x++){
  152. i=x+y*m_width;
  153. if(m_matrix[i]){
  154. if(m_matrix[i]==in){
  155. int s = m_operations.size();
  156. for(int j=0; j<s; j++)
  157. {
  158. if(!m_operations[j]->execute((PadSelection*)this, m, count)) return NULL;
  159. }
  160. return m;
  161. }
  162. else{
  163. count++;
  164. }
  165. }
  166. }
  167. }
  168. return m;
  169. }