]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/constructor.cpp
e2fddbff09e18b607f3cf2ad745ce88dc4418279
[z.facultad/75.42/plaqui.git] / Constructor / constructor.cpp
1 //constructor.cpp
2 #include "constructor.h"
3
4 Constructor::Constructor(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade):Gtk::Window(cobject)
5 {
6         id = 0;
7         set_title("Constructor");
8         
9         
10         ico_canio = Gdk::Pixbuf::create_from_file("item_canio.png");
11         ico_y     = Gdk::Pixbuf::create_from_file("item_y.png");
12         ico_codo  = Gdk::Pixbuf::create_from_file("item_codo.png");
13         ico_tanque  = Gdk::Pixbuf::create_from_file("item_tanque.png");
14         
15         refGlade->get_widget("btn_canio", btn_canio);
16         refGlade->get_widget("btn_codo", btn_codo);
17         refGlade->get_widget("btn_y", btn_y);
18         refGlade->get_widget("btn_tanque",btn_tanque);
19         refGlade->get_widget("main_menu_quit",main_menu_quit);
20         refGlade->get_widget("edit_menu_del",edit_menu_del);
21         refGlade->get_widget("chk_btn_logica",chk_btn_logica);
22         
23         refGlade->get_widget_derived("workplace", workplace);  //fixed
24         
25         
26         
27         //Targets
28         listTargets.push_back( Gtk::TargetEntry("STRING") );
29         listTargets.push_back( Gtk::TargetEntry("text/plain") );
30         listTargets.push_back( Gtk::TargetEntry("POINTER") );
31         listTargets.push_back( Gtk::TargetEntry("application/pointer") );
32         
33         btn_canio->drag_source_set(listTargets);
34         btn_y->drag_source_set(listTargets);
35         btn_codo->drag_source_set(listTargets);
36         btn_tanque->drag_source_set(listTargets);
37         
38         btn_canio->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_canio_drag_get));
39         btn_y->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_y_drag_get));
40         btn_codo->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_codo_drag_get));
41         btn_tanque->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_btn_tanque_drag_get));
42         chk_btn_logica->signal_clicked().connect(SigC::slot(*this, &Constructor::on_chk_btn_clicked));
43         
44         main_menu_quit->signal_activate().connect(SigC::slot(*this, &Constructor::on_main_menu_quit));
45         edit_menu_del->signal_activate().connect(SigC::slot(*this,&Constructor::on_edit_menu_del));
46         
47         // Señales para cambiar el icono cuando empieza el drag.
48         btn_canio->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_canio_drag_begin));
49         btn_y->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_y_drag_begin));
50         btn_codo->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_codo_drag_begin));
51         btn_tanque->signal_drag_begin().connect( SigC::slot(*this, &Constructor::on_tanque_drag_begin));
52         
53         
54         workplace->drag_dest_set(listTargets);
55         workplace->signal_drag_data_received().connect( SigC::slot(*this, &Constructor::on_item_drop_drag_received) );  
56         workplace->listaItems = &listaItems;    
57         logica = false;
58 }
59
60 Constructor::~Constructor()
61 {
62         std::list<CItem *>::iterator i = listaItems.begin();
63         std::cout << "ok" << std::endl;
64         while ( i != listaItems.end() ){
65                 CItem *temp = *i;
66                 listaItems.erase(i);
67                 std::cout << "Elimnando ... " << std::endl;
68                 delete temp;
69                 i = listaItems.begin();
70         }
71 }
72
73 void Constructor::on_btn_canio_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
74 {
75         gtk_selection_data_set (selection_data, selection_data->target, 8, (const guchar*)"item_canio.png", 14);
76 }
77
78 void Constructor::on_btn_y_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
79 {
80         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"item_y.png",10);
81 }
82
83 void Constructor::on_btn_codo_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
84 {
85         gtk_selection_data_set(selection_data, selection_data->target, 8 ,(const guchar*)"item_codo.png",13);
86 }
87
88 void Constructor::on_btn_tanque_drag_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
89 {
90         gtk_selection_data_set(selection_data, selection_data->target, 8,(const guchar*)"item_tanque.png",15);
91 }
92
93 void Constructor::on_main_menu_quit()
94 {
95         Gtk::Main::quit();
96         //hide();
97 }
98
99 void Constructor::on_edit_menu_del()
100 {
101         // hay que meter algo aca.
102 }
103
104 void Constructor::on_chk_btn_clicked()
105 {
106         logica = !logica;
107 }
108
109 void Constructor::on_canio_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
110 {
111         context->set_icon(ico_canio, 5, 5); 
112 }
113
114 void Constructor::on_y_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
115 {
116         context->set_icon(ico_y, 5, 5); 
117 }
118
119 void Constructor::on_codo_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
120 {
121         context->set_icon(ico_codo, 5, 5); 
122 }
123
124 void Constructor::on_tanque_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context)
125 {
126         context->set_icon(ico_tanque, 5, 5); 
127 }
128
129 void Constructor::on_item_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context, CItem *item)
130 {
131         context->set_icon(item->get_image(), 5, 5);
132 }
133
134 void Constructor::on_item_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, GtkSelectionData* selection_data, guint info, guint time)
135 {       
136                 gtk_selection_data_set (selection_data, selection_data->target, 10, (const guchar*)"item_codo.png", 13);
137 }
138
139 bool Constructor::can_drop(CItem *item, int _x, int _y)
140 {
141         std::list<CItem*>::iterator  i = listaItems.begin();
142         while( i != listaItems.end() ) {
143                 CItem *temp = *i;
144                 if ( temp->get_id() != item->get_id()   ) {
145                         if (  (temp->is_occupied_area(_x, _y))  ||  
146                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y+item->get_image()->get_height()-1))  ||
147                                 ( temp->is_occupied_area(_x, _y+item->get_image()->get_height()-1)) ||
148                                 ( temp->is_occupied_area(_x+item->get_image()->get_width()-1, _y) ) ) 
149                                 return false;
150                         else i++;
151                 }
152                 else i++;
153         }
154         return true;
155 }
156
157 void Constructor::on_item_drop_drag_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, GtkSelectionData* selection_data, guint info, guint time)
158 {
159         if ( !logica ) {
160                 /* Ajusto coordenada x e y para que caigan en un lugar de una cuadricula de 32x32 */
161                 int i,j;
162                 // el +1 es para evitar un bug cuando se selecciona muy cerce de la
163                 // separacion de 2 cuadritos
164                 i = (x+1)/32;
165                 j = (y+1)/32;
166                 // El drag es de un item
167                 if (selection_data->format == 10) {
168                         if (can_drop(((CItem*)drag_get_source_widget(context)),i*32, j*32)){
169                                 ((CItem*)drag_get_source_widget(context))->set_position(i*32, j*32);
170                                 workplace->move(*drag_get_source_widget(context), i*32, j*32);
171                         }
172                 }
173
174                 // El Drag es desde la barra de tareas
175                 if ((selection_data->length >= 0) && (selection_data->format == 8))     {
176                         CItem *a;
177                         if (strcmp((const char *)selection_data->data, "item_codo.png")==0) 
178                                 a = new Splitter();//Gtk::manage( new Splitter() );
179                                         else    if (strcmp((const char *)selection_data->data, "item_canio.png")==0) 
180                                                 a = new Conduct();//Gtk::manage( new Conduct() );
181                                                         else if (strcmp((const char *)selection_data->data, "item_y.png")==0) 
182                                                                 a = new Union();//Gtk::manage( new Union() );
183                                                                         else if (strcmp((const char *)selection_data->data, "item_tanque.png")==0) 
184                                                                                 a = new Cistern();//Gtk::manage( new Cistern() );
185                                                                                         else
186                                                                                                 a = new CItem();//Gtk::manage( new CItem((const char *)selection_data->data) );
187                         //Seteo el ID del item
188                         a->set_id(++id);                
189                         if ( can_drop(a, i*32, j*32) ) {
190                                 workplace->put(*a, i*32, j*32);
191                                 //Apunto al workplace
192                                 a->workplace= workplace;
193                                 //Seteo la posicion del item
194                                 a->set_position(i*32,j*32);
195                                 // Seteo la lista de tipos de drags 
196                                 a->drag_source_set(listTargets);
197                                 // Conecto las señales
198                                 a->signal_drag_data_get().connect( SigC::slot(*this, &Constructor::on_item_drag_data_get));
199
200                                 // Utilizo el SigC::bind para que el callback on_drag_begin acepte un
201                                 // parametro extra, en este caso un CItem *. Esto se hace para
202                                 // que cuando el usuario quiera mover un item, saber que item es
203                                 // y pedirle su ícono para mostrar cono icono durante la operacion,
204                                 // Esto va a permitir, que si un widget tiene una imagen rotara o algo
205                                 // raro se vea el widget tal cual.
206                                 a->signal_drag_begin().connect(SigC::bind( SigC::slot(*this, &Constructor::on_item_drag_begin), a));
207                                 a->show();      
208                                 listaItems.push_back(a);
209                         } else {
210                                         id--;
211                                         delete a;
212                                 }
213                 }
214                 context->drag_finish(false, false, time);
215         }
216 }