]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/exclusa.cpp
-Los cables que van a las compuertas se posicionan en la entrada o salida y no en...
[z.facultad/75.42/plaqui.git] / Constructor / src / exclusa.cpp
1 #include "exclusa.h"
2 #include "exclusaptywnd.h"
3
4 Exclusa::Exclusa(int orientacion)
5 {
6         in_x = x + 48;
7         in_y = y;
8         out_x = in_x;
9         out_y = y + 32;
10         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_h.png");
11         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/exclusa_s.png");
12         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/null.png");
13         imgActual = orientacion;
14         switch (imgActual) {
15                 case 1:
16                         image = imageS;
17                         break;
18                 default: 
19                         imgActual = 0;
20                         image = imageN;                 
21         }
22         set_size_request(image->get_width(), image->get_height());
23         
24         Glib::RefPtr<Gnome::Glade::Xml> ref = Gnome::Glade::Xml::create(PACKAGE_DATA_DIR"/plaqui-constructor/dialogs/constructor.glade", "exclusa_pty_wnd");
25         ref->get_widget_derived("exclusa_pty_wnd",exclusa_pty_wnd);
26         exclusa_pty_wnd->exclusa = this;
27         name = "exclusa";
28         
29         Connector temp;
30         temp.id_dest = -1;
31         temp.type = UNDEF;
32         connect_vec.push_back(temp);
33         connect_vec.push_back(temp);
34 }
35
36 Exclusa::~Exclusa()
37 {
38 }
39
40 bool Exclusa::on_button_press_event(GdkEventButton *event)
41 {
42         t_line tmp_line;
43         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
44                 combo_entry->set_text(name);
45                 WorkPlace::pointed = ID;
46                 if (CItem::logic_connect) {
47                         if ( detect_click_position(event->x, event->y) == IN) {
48                                 tmp_line.logic =  workplace->get_logic_item(CItem::gate_id);
49                                 tmp_line.store = this;
50                                 workplace->lista_lineas_in.push_back(tmp_line);
51                                 workplace->queue_draw();        
52                         } else if (detect_click_position(event->x, event->y) == OUT) {  
53                                 tmp_line.logic =  workplace->get_logic_item(CItem::gate_id);
54                                 tmp_line.store = this;
55                                 workplace->lista_lineas_out.push_back(tmp_line);
56                                 workplace->queue_draw();
57                         }
58                 }
59         }
60         
61         if ((event->type == GDK_BUTTON_PRESS) && ( event->button ==2)){
62                 image = null; 
63                 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);
64                 imgActual++;
65                 switch (imgActual) {
66                         case 1:
67                                 image = imageS;
68                                 in_x = x + image->get_width();
69                                 in_y = y + image->get_height()/2;
70                                 out_x = x;
71                                 out_y = in_y;
72                                 break;
73                         default: 
74                                 imgActual = 0;
75                                 image = imageN;                 
76                                 in_x = x + image->get_width()/2;
77                                 in_y = y;
78                                 out_x = in_x;
79                                 out_y = y +image->get_height();
80                 }
81                 set_size_request(image->get_width(),image->get_height());               
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         }
84         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
85                 menu_popup.popup(event->button, event->time);
86                  return true; //It has been handled.
87         }
88         
89         if ((event->type == GDK_2BUTTON_PRESS) && (event->button ==1)){
90                 exclusa_pty_wnd->txt_exclusa_name->set_text( name);
91                 if (estado) exclusa_pty_wnd->rd_btn_open->set_active(true);
92                 exclusa_pty_wnd->show();
93         }
94         CItem::logic_connect =false;
95         workplace->queue_draw();
96         return true;
97 }
98
99 void Exclusa::set_estado(bool _state)
100 {
101         estado = _state;
102 }
103
104 bool Exclusa::get_estado()
105 {
106         return estado;
107 }
108
109 void Exclusa::on_menu_popup_rotar()
110 {
111         GdkEventButton event; 
112                 event.type = GDK_BUTTON_PRESS;
113                 event.button = 2;
114         Exclusa::on_button_press_event(&event);
115 }
116
117 void Exclusa::on_menu_popup_propiedades()
118 {
119         GdkEventButton event;
120         event.type = GDK_2BUTTON_PRESS;
121         event.button = 1;
122         Exclusa::on_button_press_event(&event);
123 }
124
125 void Exclusa::save(FILE *archivo)
126 {
127         char c_id[50], c_est[50], c_x[50], c_y[50], c_img[50];
128         int est;
129         Glib::ustring  con0, con1;
130         if (connect_vec[0].type == IN)
131                 con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
132         else
133                 con0 = "\t\t\t<salida>"+get_other_name(connect_vec[0].id_dest)+"</salida>\n";
134         if (connect_vec[1].type == IN) 
135                 con1 = "\t\t\t<entrada>"+get_other_name(connect_vec[1].id_dest)+"</entrada>\n";
136         else
137                 con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
138         sprintf(c_x,"\t\t<x>%d</x>\n",x);
139         sprintf(c_y,"\t\t<y>%d</y>\n",y);
140         sprintf(c_id,"%d",ID);
141         if (estado) est = 1;
142         else est = 0;
143         sprintf(c_est,"\t\t<estado>%d</estado>\n", est);
144         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
145         Glib::ustring dato;
146         dato = "\t<exclusa nombre=\""+name+"\" id=\"";
147         dato += c_id;
148         dato += "\">\n";
149         dato += "\t\t<conector>\n"+con0 + con1+"\t\t</conector>\n";
150         dato += c_img;
151         dato += c_est;
152         dato += c_x; 
153         dato += c_y;
154         dato += "\t</exclusa>\n";
155         fprintf(archivo,dato.c_str());  
156 }
157
158 bool Exclusa::check_connection()
159 {
160         ConnectorType aux;
161         CItem *_item0, *_item1;
162         switch (get_img_actual()) {
163                         case 0:
164                                 connect_vec[0].type = is_other_connection_area( get_position_x()-5, get_position_y()+16, &_item0); //izquierda
165                                 connect_vec[1].type = is_other_connection_area( get_position_x()+get_image()->get_width()+5, get_position_y()+16,& _item1); //derecha
166                         break;          
167                         case 1:
168                                 connect_vec[0].type =  is_other_connection_area( get_position_x()+16, get_position_y()-5, &_item0); // arriba
169                                 connect_vec[1].type = is_other_connection_area( get_position_x()+16, get_position_y()+get_image()->get_height() +5, &_item1); //abajo
170         }
171         if ( connect_vec[0].type != UNDEF && connect_vec[1].type != UNDEF ) {
172                 aux = connect_vec[0].type; 
173                 connect_vec[0].type = connect_vec[1].type;
174                 connect_vec[1].type = aux;
175                 connect_vec[0].id_dest = _item0->get_id();
176                 connect_vec[1].id_dest = _item1->get_id();
177                 return ( is_connected = (connect_vec[0].type != connect_vec[1].type) );
178         }
179         else return is_connected;
180 }
181
182 ConnectorType Exclusa::get_connector_type(int _a, int _b)
183 {
184         CItem * _item;
185         if ( ! is_connected ) {
186                 switch (imgActual) {
187                         case 0:
188                                 if ((_a <= x+10 )&&(_a > 0) && (_b <= y+image->get_height()-10) &&(_b >= y+10 )) //izquierda
189                                         return is_other_connection_area(get_position_x()+get_image()->get_width()+5, get_position_y()+16, &_item);//pregunto por la derecha
190                                 if ((_a <= x+image->get_width()-1)&&(_a >= x+image->get_width()-10) && (_b <= y+image->get_height()-10) &&(_b >= y +10 )) //derecha
191                                         return is_other_connection_area(get_position_x()-5, get_position_y()+16, &_item); //pregunto por la izquierda.
192                                 break;
193                         case 1: 
194                                 if ( (_a <= x+image->get_width()-10 )&&(_a >= x + 10) && (_b <= y+10) &&(_b > 0 ) )  //arriba
195                                         return is_other_connection_area(get_position_x()+16, get_position_y()+get_image()->get_height() +5, &_item); //pregunto que hay abajo
196                                 if ((_a <= x+22 )&&(_a >= x + 10) && (_b <= y+image->get_height()-1) &&(_b >= y+image->get_height()-10 ))//abajo
197                                         return is_other_connection_area( get_position_x()+16, get_position_y()-5, &_item);//pregunto que hay arriba.
198                 }
199         } else {
200                 switch (imgActual) {
201                         case 0: 
202                                 if ((_a <= x+10 )&&(_a > 0) && (_b <= y+image->get_height()-10) &&(_b >= y+10 )) //izquierda
203                                         return connect_vec[0].type;
204                                 if ((_a <= x+image->get_width()-1)&&(_a >= x+image->get_width()-10) && (_b <= y+image->get_height()-10) &&(_b >= y +10 )) //derecha
205                                         return connect_vec[1].type;
206                                 break;
207                         case 1:
208                                 if ( (_a <= x+image->get_width()-10 )&&(_a >= x + 10) && (_b <= y+10) &&(_b > 0 ) ) //arriba
209                                         return connect_vec[0].type;
210                                 if ((_a <= x+22 )&&(_a >= x + 10) && (_b <= y+image->get_height()-1) &&(_b >= y+image->get_height()-10 )) //abajo
211                                         return connect_vec[1].type;
212                 }
213         }
214         return UNDEF;
215 }
216         
217 void Exclusa::set_default_connector()
218 {
219         connect_vec[0].type = UNDEF;
220         connect_vec[1].type = UNDEF;
221 }
222
223 void Exclusa::get_in_logic_connect_position(int& _a, int& _b)
224 {
225         _a = in_x;
226         _b = in_y;
227 }
228
229 void Exclusa::get_out_logic_connect_position(int& _a, int& _b)
230 {
231         _a = out_x;
232         _b = out_y;
233 }
234         
235 ConnectorType Exclusa::detect_click_position(int _a, int _b)
236 {
237         switch (imgActual) {
238                 case 0:
239                         if ( (_a <= image->get_width()/2+10)&&(_a>=image->get_width()/2-10)&&(_b<=16)&&(_b >= 0) )
240                                 return IN;
241                         if ( (_a <= image->get_width()/2+10)&&(_a>=image->get_width()/2-10)&&(_b<=32)&&(_b > 16) )
242                                 return OUT;
243                         break;
244                 case 1:
245                         if ( (_a <=32)&&(_a> 16)&&(_b<=image->get_height()/2+10)&&(_b >= image->get_height()/2-10) )
246                                 return IN;
247                         if ( (_a <= 16)&&(_a>=0)&&(_b<=image->get_height()/2+10)&&(_b >= image->get_height()/2-10) )
248                                 return OUT;
249                 }
250                 return UNDEF;
251 }
252
253 void Exclusa::update_logic_position()
254 {
255         switch (imgActual) {
256                 case 1:
257                         in_x = x + image->get_width();
258                         in_y = y + image->get_height()/2;
259                         out_x = x;
260                         out_y = in_y;
261                         break;
262                 case 0: 
263                         in_x = x + image->get_width()/2;
264                         in_y = y;
265                         out_x = in_x;
266                         out_y = y +image->get_height();
267         }
268 }