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