]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/exclusa.cpp
-Arregle un poco el tema de que cada compuerta puede tener una sola salida
[z.facultad/75.42/plaqui.git] / Constructor / src / exclusa.cpp
1 #include "exclusa.h"
2 #include "exclusaptywnd.h"
3 #include "not.h"
4
5 Exclusa::Exclusa(int orientacion)
6 {
7         in_x = x + 48;
8         in_y = y;
9         out_x = in_x;
10         out_y = y + 32;
11         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_h.png");
12         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_s.png");
13         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/null.png");
14         imgActual = orientacion;
15         switch (imgActual) {
16                 case 1:
17                         image = imageS;
18                         break;
19                 default: 
20                         imgActual = 0;
21                         image = imageN;                 
22         }
23         set_size_request(image->get_width(), image->get_height());
24         
25         Glib::RefPtr<Gnome::Glade::Xml> ref = Gnome::Glade::Xml::create(PACKAGE_DATA_DIR"/plaqui-constructor/dialogs/constructor.glade", "exclusa_pty_wnd");
26         ref->get_widget_derived("exclusa_pty_wnd",exclusa_pty_wnd);
27         exclusa_pty_wnd->exclusa = this;
28         name = "exclusa";
29         
30         Connector temp;
31         temp.id_dest = -1;
32         temp.type = UNDEF;
33         connect_vec.push_back(temp);
34         connect_vec.push_back(temp);
35 }
36
37 Exclusa::~Exclusa()
38 {
39 }
40
41 bool Exclusa::on_button_press_event(GdkEventButton *event)
42 {
43         t_line tmp_line;
44         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
45                 WorkPlace::pointed = ID;
46                 list_pointed->push_back(name);
47                 combo_entry->set_popdown_strings(*list_pointed);
48                 combo_entry->get_entry()->set_text (name);
49                 if (CItem::logic_connect && CItem::gate_id != -1) {
50                         if ( detect_click_position((int)event->x, (int)event->y) == IN && !workplace->get_logic_item(CItem::gate_id)->get_out_logic_connect()) {
51                                 tmp_line.logic_id =  CItem::gate_id;
52                                 workplace->get_logic_item(CItem::gate_id)->set_out_connected(true);
53                                 tmp_line.store_id = ID;
54                                 workplace->lista_lineas_in.push_back(tmp_line);
55                                 workplace->queue_draw();        
56                         } else if (detect_click_position((int)event->x, (int)event->y) == OUT )  { 
57                                 if (dynamic_cast<Not *>(workplace->get_logic_item(CItem::gate_id))) {
58                                         if (!dynamic_cast<Not *>(workplace->get_logic_item(CItem::gate_id))->get_out_logic_connect()) {
59                                                 workplace->get_logic_item(CItem::gate_id)->set_in_connected(true);
60                                                 tmp_line.logic_id =  CItem::gate_id;
61                                                 tmp_line.store_id = ID;
62                                                 workplace->lista_lineas_out.push_back(tmp_line);
63                                                 workplace->queue_draw();
64                                         }
65                                 } else {
66                                         tmp_line.logic_id =  CItem::gate_id;
67                                         tmp_line.store_id = ID;
68                                         workplace->lista_lineas_out.push_back(tmp_line);
69                                         workplace->queue_draw();
70                                 }                                       
71                         }
72                         CItem::gate_id = -1;
73                 }
74                 Glib::ustring text;
75                 if(estado) text = name+"  Estado: ABIERTO";
76                 else text = name+"  Estado: CERRADO";
77                 status_bar->push ( text, 0);            
78         }
79         
80         if ((event->type == GDK_BUTTON_PRESS) && ( event->button ==2)){
81                 image = null; 
82                 image->render_to_drawable(get_window(),get_style()->get_black_gc(),0,0,0,0,image->get_width(),image->get_height(),Gdk::RGB_DITHER_NONE,0,0);
83                 imgActual++;
84                 switch (imgActual) {
85                         case 1:
86                                 image = imageS;
87                                 in_x = x + image->get_width()-5;
88                                 in_y = y + image->get_height()/2-5;
89                                 out_x = x+5;
90                                 out_y = in_y+10;
91                                 break;
92                         default: 
93                                 imgActual = 0;
94                                 image = imageN;                 
95                                 in_x = x + image->get_width()/2;
96                                 in_y = y;
97                                 out_x = in_x;
98                                 out_y = y +image->get_height();
99                 }
100                 set_size_request(image->get_width(),image->get_height());               
101                 image->render_to_drawable(get_window(),get_style()->get_black_gc(),0,0,0,0,image->get_width(),image->get_height(),Gdk::RGB_DITHER_NONE,0,0);
102         }
103         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
104                 menu_popup.popup(event->button, event->time);
105                  return true; //It has been handled.
106         }
107         
108         if ((event->type == GDK_2BUTTON_PRESS) && (event->button ==1)){
109                 exclusa_pty_wnd->txt_exclusa_name->set_text( name);
110                 if (estado) exclusa_pty_wnd->rd_btn_open->set_active(true);
111                 exclusa_pty_wnd->show();
112         }
113         workplace->queue_draw();
114         return true;
115 }
116
117 void Exclusa::set_estado(bool _state)
118 {
119         estado = _state;
120 }
121
122 bool Exclusa::get_estado()
123 {
124         return estado;
125 }
126
127 void Exclusa::on_menu_popup_rotar()
128 {
129         GdkEventButton event; 
130                 event.type = GDK_BUTTON_PRESS;
131                 event.button = 2;
132         Exclusa::on_button_press_event(&event);
133 }
134
135 void Exclusa::on_menu_popup_propiedades()
136 {
137         GdkEventButton event;
138         event.type = GDK_2BUTTON_PRESS;
139         event.button = 1;
140         Exclusa::on_button_press_event(&event);
141 }
142
143 void Exclusa::save(FILE *archivo)
144 {
145         char c_id[50], c_est[50], c_x[50], c_y[50], c_img[50];
146         int est;
147         Glib::ustring  con0, con1;
148         if (connect_vec[0].type == IN)
149                 con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
150         else
151                 con0 = "\t\t\t<salida>"+get_other_name(connect_vec[0].id_dest)+"</salida>\n";
152         if (connect_vec[1].type == IN) 
153                 con1 = "\t\t\t<entrada>"+get_other_name(connect_vec[1].id_dest)+"</entrada>\n";
154         else
155                 con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
156         sprintf(c_x,"\t\t<x>%d</x>\n",x);
157         sprintf(c_y,"\t\t<y>%d</y>\n",y);
158         sprintf(c_id,"%d",ID);
159         if (estado) est = 1;
160         else est = 0;
161         sprintf(c_est,"\t\t<estado>%d</estado>\n", est);
162         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
163         Glib::ustring dato;
164         dato = "\t<exclusa nombre=\""+name+"\" id=\"";
165         dato += c_id;
166         dato += "\">\n";
167         dato += "\t\t<conector>\n"+con0 + con1+"\t\t</conector>\n";
168         dato += c_img;
169         dato += c_est;
170         dato += c_x; 
171         dato += c_y;
172         dato += "\t</exclusa>\n";
173         fprintf(archivo,dato.c_str());  
174 }
175
176 bool Exclusa::check_connection()
177 {
178         ConnectorType aux, aux1, aux2;
179         CItem *_item0, *_item1;
180         switch (get_img_actual()) {
181                 case 0:
182                         aux1 = is_other_connection_area( get_position_x()-5, get_position_y()+16, &_item0); //izquierda
183                         aux2 = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+16,& _item1); //derecha
184                 break;          
185                 case 1:
186                         aux1 =  is_other_connection_area( get_position_x()+16, get_position_y()-5, &_item0); // arriba
187                         aux2 = is_other_connection_area( get_position_x()+16, get_position_y()+get_image()->get_height() +5, &_item1); //abajo
188         }
189         if ( aux1 != UNDEF && aux2 != UNDEF ) {
190                 if (!is_connected) {
191                         //aux = connect_vec[0].type; 
192                         // Aca van invertidos los tipos de entrada/salida
193                         connect_vec[0].type = aux2; //connect_vec[1].type;
194                         connect_vec[1].type = aux1;
195                 }
196                 connect_vec[0].id_dest = _item0->get_id();
197                 connect_vec[1].id_dest = _item1->get_id();
198                 return ( is_connected = (connect_vec[0].type != connect_vec[1].type) );
199         }
200         return is_connected = false;
201 }
202
203 ConnectorType Exclusa::get_connector_type(int _a, int _b)
204 {
205         CItem *_item;
206         switch (imgActual) {
207                 case 1:
208                         if ( (_a <= x+image->get_width()-10 )&&(_a >= x + 10) && (_b <= y+10) &&(_b > 0 ) ){  //arriba
209                                 if ( is_connected ) return connect_vec[0].type;                 
210                                 connect_vec[1].type = is_other_connection_area(get_position_x()+16, get_position_y()+get_image()->get_height() +5, &_item); //pregunto que hay abajo
211                                 switch ( connect_vec[1].type ) {
212                                         case IN: 
213                                         connect_vec[1].type = OUT;
214                                         connect_vec[0].type = IN;               
215                                         break;
216                                         case OUT:
217                                         connect_vec[1].type = IN;
218                                         connect_vec[0].type = OUT;              
219                                         break;
220                                         case UNDEF:
221                                         return UNDEF;
222                                 }
223                                 is_connected = true;
224                                 return connect_vec[0].type;
225                         }
226                         
227                         if ((_a <= x+22 )&&(_a >= x + 10) && (_b <= y+image->get_height()-1) &&(_b >= y+image->get_height()-10 )) {//abajo
228                                 if ( is_connected ) return connect_vec[1].type;                 
229                                 connect_vec[0].type = is_other_connection_area( get_position_x()+16, get_position_y()-5,& _item);//pregunto que hay arriba.
230                                 switch ( connect_vec[0].type ) {
231                                         case IN: 
232                                         connect_vec[0].type = OUT;
233                                         connect_vec[1].type = IN;               
234                                         break;
235                                         case OUT:
236                                         connect_vec[0].type = IN;
237                                         connect_vec[1].type = OUT;              
238                                         break;
239                                         case UNDEF:
240                                         return UNDEF;
241                                 }
242                                 is_connected = true;
243                                 return connect_vec[1].type;
244                         }
245                 break;
246                 case 0: 
247                         if ((_a <= x+10 )&&(_a > 0) && (_b <= y+image->get_height()-10) &&(_b >= y+10 )){ //izquierda
248                                 if ( is_connected ) return connect_vec[0].type;                 
249                                 connect_vec[1].type = is_other_connection_area(get_position_x()+get_image()->get_width()+5, get_position_y()+16, &_item);//pregunto por la derecha
250                                 switch ( connect_vec[1].type ) {
251                                         case IN: 
252                                         connect_vec[1].type = OUT;
253                                         connect_vec[0].type = IN;               
254                                         break;
255                                         case OUT:
256                                         connect_vec[1].type = IN;
257                                         connect_vec[0].type = OUT;              
258                                         break;
259                                         case UNDEF:
260                                         return UNDEF;
261                                 }
262                                 is_connected = true;
263                                 return connect_vec[0].type;
264                         }
265                   if ((_a <= x+image->get_width()-1)&&(_a >= x+image->get_width()-10) && (_b <= y+image->get_height()-10) &&(_b >= y +10 )) {//derecha
266                                 if ( is_connected ) return connect_vec[1].type;                 
267                                 connect_vec[0].type =   is_other_connection_area(get_position_x()-5, get_position_y()+16, &_item); //pregunto por la izquierda. 
268                                 switch ( connect_vec[0].type ) {
269                                         case IN: 
270                                         connect_vec[0].type = OUT;
271                                         connect_vec[1].type = IN;               
272                                         break;
273                                         case OUT:
274                                         connect_vec[0].type = IN;
275                                         connect_vec[1].type = OUT;              
276                                         break;
277                                         case UNDEF:
278                                         return UNDEF;
279                                 }
280                                 is_connected = true;
281                                 return connect_vec[1].type;
282                         }
283                 }
284         return UNDEF;
285 }
286         
287 void Exclusa::set_default_connector()
288 {
289         connect_vec[0].type = UNDEF;
290         connect_vec[1].type = UNDEF;
291 }
292
293 void Exclusa::get_in_logic_connect_position(int& _a, int& _b)
294 {
295         _a = in_x;
296         _b = in_y;
297 }
298
299 void Exclusa::get_out_logic_connect_position(int& _a, int& _b)
300 {
301         _a = out_x;
302         _b = out_y;
303 }
304         
305 ConnectorType Exclusa::detect_click_position(int _a, int _b)
306 {
307         switch (imgActual) {
308                 case 0:
309                         if ( (_a <= image->get_width()/2+10)&&(_a>=image->get_width()/2-10)&&(_b<=16)&&(_b >= 0) )
310                                 return IN;
311                         if ( (_a <= image->get_width()/2+10)&&(_a>=image->get_width()/2-10)&&(_b<=32)&&(_b > 16) )
312                                 return OUT;
313                         break;
314                 case 1:
315                         if ( (_a <=32)&&(_a> 16)&&(_b<=image->get_height()/2+10)&&(_b >= image->get_height()/2-10) )
316                                 return IN;
317                         if ( (_a <= 16)&&(_a>=0)&&(_b<=image->get_height()/2+10)&&(_b >= image->get_height()/2-10) )
318                                 return OUT;
319                 }
320                 return UNDEF;
321 }
322
323 void Exclusa::update_logic_position()
324 {
325         switch (imgActual) {
326                 case 1:
327                         in_x = x + image->get_width()-5;
328                         in_y = y + image->get_height()/2-5;
329                         out_x = x+5;
330                         out_y = in_y+10;
331                         break;
332                 case 0: 
333                         in_x = x + image->get_width()/2;
334                         in_y = y;
335                         out_x = in_x;
336                         out_y = y +image->get_height();
337         }
338 }