123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #include "PadSelection.h"
- #include "PadDefinition.h"
- #include "Button.h"
- #include "Controller.h"
- #include "Operations.h"
- #include "PadConfiguration.h"
- #include <algorithm>
- InputInit::~InputInit()
- {
- int s = actions.size();
- for(int i=0; i<s; i++) delete actions[i];
- }
- PadSelection::PadSelection(const std::string& name, Json::Value& v, PadConfiguration* p) : m_pad(p)
- {
- m_name=name;
- m_width=p->get_pad_definition().get_width();
- m_height=p->get_pad_definition().get_height();
- m_matrix.resize(m_width*m_height);
- for(int i=0; i<m_width*m_height; i++) m_matrix[i]=NULL;
- _update_matrix();
- Json::Value a = v["operations"];
- int s = a.size();
- for(int i=0; i<s; i++)
- {
- m_operations.push_back(InputOperation::from_json(a[i]));
- }
- m_init.add( v["init"]);
- }
- void PadSelection::send_init()
- {
- int s = m_list.size();
- for(int i=0; i<s; i++)
- {
- MidiMessageList* msg = m_init.get_messages();
- m_list[i]->control(msg);
- }
- }
- PadSelection::~PadSelection()
- {
- int s = m_operations.size();
- for(int i=0; i<s; i++)
- delete m_operations[i];
- }
- void PadSelection::_update_matrix()
- {
- m_list.clear();
- for(int j=m_height-1; j>=0; j--)
- {
- for(int i=0; i<m_width; i++)
- {
- AbsInput* b = m_matrix[j*m_width+i];
- if(b) m_list.push_back(b);
- }
- }
- }
- static bool compare(AbsInput* a, AbsInput* b){
- return a->index<b->index;
- }
- void PadSelection::add(int i, int j)
- {
- int x = i+j*m_width;
- AbsInput* p = m_pad->get_pad_definition().at(i,j);
- if(!m_matrix[x]){
- m_list.push_back(p);
- }
- m_matrix[x]=m_pad->get_pad_definition().at(i,j);
- m_matrix[x]->set_selection(this);
- std::sort(m_list.begin(), m_list.end(), compare);
- }
- AbsInput* PadSelection::at(int i, int j)
- {
- return m_matrix[i+j*m_width];
- }
- void PadSelection::add(AbsInput* b)
- {
- return add(b->x, b->y);
- }
- void PadSelection::remove(int i, int j)
- {
- int x = i+j*m_pad->get_pad_definition().get_width();
- m_matrix[x]=NULL;
- _update_matrix();
- m_matrix[x]->set_selection(this);
- }
- void PadSelection::remove(AbsInput* b)
- {
- return remove(b->x, b->y);
- }
- bool PadSelection::has(int x, int y) const
- {
- return m_matrix[x+y*m_width];
- }
- bool PadSelection::has(AbsInput* p) const
- {
- return has( p->x, p->y);
- }
- int PadSelection::count_x(int x) const{
- int s=m_list.size(), count=0;
- for(int i=0; i<s; i++)
- if(m_list[i]->x==x)
- count++;
- return count;
- }
- int PadSelection::count_y(int y) const{
- int s=m_list.size(), count=0;
- for(int i=0; i<s; i++)
- if(m_list[i]->y==y)
- count++;
- return count;
- }
- int PadSelection::index_at_x(int absindex) const
- {
- int x = absindex%m_width;
- int y = absindex/m_width;
- int s=m_list.size(), count=0;
- for(int i=0; i<s; i++)
- if(m_list[i]->y==y && m_list[i]->x<x)
- count++;
- return count;
- }
- int PadSelection::index_at_y(int absindex) const
- {
- int x = absindex%m_width;
- int y = absindex/m_width;
- int s=m_list.size(), count=0;
- for(int i=0; i<s; i++)
- if(m_list[i]->y==y && m_list[i]->x<x)
- count++;
- return count;
- }
- /*
- MidiMessage* PadSelection::execute(AbsInput* in, MidiMessage* m) const
- {
- int s = m_list.size();
- for(int i=0; i<s; i++){
- if(m_list[i]==in){
- int s = m_operations.size();
- for(int j=0; j<s; j++)
- {
- if(!m_operations[j]->execute((PadSelection*)this, m, i)) return NULL;
- }
- return m;
- }
- }
- return m;
- }*/
- MidiMessage* PadSelection::execute(AbsInput* in, MidiMessage* m) const
- {
- int s = m_list.size(), i, count=0;
- for(int y=m_height-1; y>=in->y; y--){
- for(int x=0; x<m_width; x++){
- i=x+y*m_width;
- if(m_matrix[i]){
- if(m_matrix[i]==in){
- int s = m_operations.size();
- for(int j=0; j<s; j++)
- {
- if(!m_operations[j]->execute((PadSelection*)this, m, count)) return NULL;
- }
- return m;
- }
- else{
- count++;
- }
- }
- }
- }
- return m;
- }
|