libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/world/patcher_dynamic.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   PSYCHOSYNTH                                                           *
00004  *   ===========                                                           *
00005  *                                                                         *
00006  *   Copyright (C) 2007 by Juan Pedro Bolivar Puente                       *
00007  *                                                                         *
00008  *   This program is free software: you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation, either version 3 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #ifndef PSYNTH_PATCHERDYNAMIC_H
00024 #define PSYNTH_PATCHERDYNAMIC_H
00025 
00026 #include <map>
00027 #include <set>
00028 
00029 #include <psynth/world/patcher.hpp>
00030 
00031 namespace psynth
00032 {
00033 
00034 class patcher_dynamic : public patcher
00035 {    
00036     struct link {
00037         graph::node* src;
00038         graph::node* dest;
00039         float dist;
00040         float dist_to_center;
00041         int sock_type;
00042         int out_sock;
00043         int in_sock;
00044         int actual_in_sock;
00045         
00046         link (graph::node* s, graph::node* d, float ds, float dc, int t, int os, int is) :
00047             src(s), dest(d), dist(ds), dist_to_center(dc),
00048             sock_type(t), out_sock(os), in_sock(is), actual_in_sock(-1)
00049             {}
00050         
00051         bool operator< (const link& l) const {
00052             return dist == l.dist ? dist_to_center < l.dist_to_center : dist < l.dist;
00053         }
00054     };
00055 
00056     struct pnode {
00057         graph::node* obj;
00058         graph::node* dest;
00059         bool out_used; /* We output to one object only */
00060         int actual_sock_type;
00061         int actual_in_sock; 
00062 
00063         pnode (graph::node* o = NULL) :
00064             obj(o),
00065             dest(NULL),
00066             out_used(false),
00067             actual_sock_type(-1),
00068             actual_in_sock(-1) {}
00069     };
00070 
00071     class link_ptr_cmp {
00072     public:
00073         bool operator () (const link* a, const link* b) {
00074           return *a < *b;
00075         }
00076     };
00077 
00078     bool m_changed;
00079     
00080     std::map<int, pnode> m_nodes;
00081     std::multiset<link*, link_ptr_cmp> m_links;
00082 
00083     inline void undo_link (link& l);
00084     inline void make_link (link& l);
00085     inline void find_in_sock (link& l);
00086     inline bool is_linked (link& l);
00087     
00088 public:
00089     patcher_dynamic ();
00090     ~patcher_dynamic ();
00091     
00092     bool add_node (graph::node* obj);
00093     bool delete_node (graph::node* obj);
00094     void set_param_node (graph::node* obj, int id);
00095     void update ();
00096     void clear ();
00097 };
00098 
00099 } /* namespace psynth */
00100 
00101 #endif /* PSYNTH_PATCHERDYNAMIC_H */