]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/and.cpp
1a36cfd72ca310b217bbe55f97eb30dab540a185
[z.facultad/75.42/plaqui.git] / Constructor / src / and.cpp
1 #include "and.h"
2 #include "cistern.h"
3 And::And(int orientacion)
4 {
5         is_logic = true;
6         out_connected = false;
7         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_n.png");
8         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_s.png");
9         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_e.png");
10         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/and_o.png");
11         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/null.png");
12         imgActual = orientacion;
13         switch (imgActual) {
14                 case 1:
15                         image = imageS;
16                         in_x = x +16;
17                         in_y = y;
18                         out_x = x+16;
19                         out_y = y+32;
20                         break;
21                 case 2:
22                         image = imageO;
23                         in_x = x+32;
24                         in_y = y+16;
25                         out_x = x;
26                         out_y = y+16;
27                         break;
28                 case 3:
29                         image = imageN;
30                         in_x = x+16;
31                         in_y = y+32;
32                         out_x = x+16;
33                         out_y = y;
34                         break;
35                 default: 
36                         imgActual = 0;
37                         image = imageE;                 
38                         in_x = x;
39                         in_y = y+16;
40                         out_x = x+32;
41                         out_y = y+16;
42         }
43         set_size_request(image->get_width(), image->get_height());
44         name = "and";
45         menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("Conectar", menu_image_linea,SigC::slot(*this, &CItem::on_menu_popup_conectar) ) ) ;
46 }
47
48 And::~And()
49 {
50 }
51
52 bool And::on_button_press_event(GdkEventButton *event)
53 {
54         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
55                 WorkPlace::pointed = ID;
56                 t_line tmp_line;
57                 std::cout << CItem::gate_id << std::endl;
58                 if (CItem::logic_connect) {
59                         if (CItem::gate_id != -1 ) {
60                                 if ( detect_click_position((int)event->x, (int)event->y ) == IN && CItem::gate_id != ID && !workplace->get_logic_item(CItem::gate_id)->get_out_logic_connect()) {
61                                         tmp_line.logic_id =  CItem::gate_id;
62                                         workplace->get_logic_item(CItem::gate_id)->set_out_connected(true);
63                                         tmp_line.store_id = ID;
64                                         workplace->lista_lineas_logic.push_back(tmp_line);
65                                         workplace->queue_draw();
66                                         CItem::gate_id = -1;
67                                 }
68                         } else {
69                                 CItem::gate_id = ID;
70                         }
71                 }
72                 if ( CItem::logic_connect && detect_click_position((int)event->x, (int)event->y ) == OUT && !out_connected) {
73                         CItem::gate_id = ID;
74                 }
75                 list_pointed->push_back(name);
76                 combo_entry->set_popdown_strings(*list_pointed);
77                 combo_entry->get_entry()->set_text (name);
78                 workplace->queue_draw();
79                 char f[20];
80                 Glib::ustring text;
81                 sprintf(f,"  Conector OUT = %d",out_connected);
82                 text = name+f;
83                 status_bar->push ( text, 0);
84         }
85         
86         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 2)){
87                 image = null;   
88                 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);
89                 imgActual++;
90                 switch (imgActual) {
91                         case 1:
92                                 image = imageS;
93                                 in_x = x +16;
94                                 in_y = y;
95                                 out_x = x+16;
96                                 out_y = y+32;
97                                 break;
98                         case 2:
99                                 image = imageO;
100                                 in_x = x+32;
101                                 in_y = y+16;
102                                 out_x = x;
103                                 out_y = y+16;
104                                 break;
105                         case 3:
106                                 image = imageN;
107                                 in_x = x+16;
108                                 in_y = y+32;
109                                 out_x = x+16;
110                                 out_y = y;
111                                 break;
112                         default: 
113                                 imgActual = 0;
114                                 image = imageE;                 
115                                 in_x = x;
116                                 in_y = y+16;
117                                 out_x = x+32;
118                                 out_y = y+16;
119                 }
120                 set_size_request(image->get_width(),image->get_height());
121                 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);
122         }
123         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
124                 menu_popup.popup(event->button, event->time);
125                 return true; //It has been handled.
126         }
127         workplace->queue_draw();
128         return true;
129 }
130 void And::on_menu_popup_rotar()
131 {
132         GdkEventButton event; 
133         event.type = GDK_BUTTON_PRESS;
134         event.button = 2;
135         And::on_button_press_event(&event);
136 }
137
138 void And::save(FILE *archivo)
139 {
140         vec_connector.clear();
141         check_connection();
142         Glib::ustring dato;
143         char c_img[50], c_x[50], c_y[50], c_id[50];
144         sprintf(c_id,"%d",ID);
145         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
146         sprintf(c_x,"\t\t<x>%d</x>\n",x);
147         sprintf(c_y,"\t\t<y>%d</y>\n",y);
148         dato = "\t<and nombre=\""+name+"\" id=\"";
149         dato += c_id;
150         dato += "\">\n";
151         dato += c_img;
152         dato += c_x;
153         dato += c_y;
154         for ( int i=0; i<=vec_connector.size()-1&&!vec_connector.empty(); i++) {
155                 if ( vec_connector[i].type == IN ) {
156                         if ( vec_connector[i].cistern_connector == 1 ) {
157                                 dato += "\t\t<entrada id = \"superior\">";
158                                 dato += vec_connector[i].name_dest + "</entrada>\n";
159                         }
160                         if ( vec_connector[i].cistern_connector == 0 ) {
161                                 dato += "\t\t<entrada id = \"inferior\">";
162                                 dato += vec_connector[i].name_dest + "</entrada>\n";
163                         }
164                         if ( vec_connector[i].cistern_connector == -1 ) {
165                                 dato += "\t\t<entrada>";
166                                 dato += vec_connector[i].name_dest + "</entrada>\n";
167                         }
168                 } else {
169                         dato += "\t\t<salida>";
170                         dato += vec_connector[i].name_dest + "</salida>\n";
171                 }       
172         }       
173         dato += "\t</and>\n";
174         fprintf(archivo, dato.c_str() );
175 }
176
177 bool And::check_connection()
178 {
179         t_logic_connector temp;
180         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
181         while ( i != workplace->lista_lineas_in.end() ) {
182                 if ( (*i).logic_id== ID ) {
183                         temp.type = OUT;
184                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
185                         vec_connector.push_back(temp);
186                 }
187                 i++;
188         }
189
190         i = workplace->lista_lineas_out.begin();
191         while ( i != workplace->lista_lineas_out.end() ) {
192                 if ( (*i).logic_id== ID ) {
193                         temp.type = IN;
194                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
195                         if ( dynamic_cast<Cistern *>(workplace->get_item((*i).store_id)) )  {
196                                 if ( (*i).cistern_out1 )
197                                         temp.cistern_connector = 1; //arriba
198                                 else 
199                                         temp.cistern_connector = 0; //abajo
200                         } else 
201                                 temp.cistern_connector = -1; //si no es tanque.
202                         vec_connector.push_back(temp);
203                 }
204                 i++;
205         }
206
207         i = workplace->lista_lineas_logic.begin();
208         while ( i != workplace->lista_lineas_logic.end() ) {
209                 if ( (*i).logic_id == ID ) {
210                         temp.type = OUT;
211                         temp.name_dest = workplace->get_logic_item((*i).store_id)->get_name();
212                         vec_connector.push_back(temp);
213                 }
214                 i++;
215         }
216
217         i = workplace->lista_lineas_logic.begin();
218         while ( i != workplace->lista_lineas_logic.end() ) {
219                 if ( (*i).store_id == ID ) {
220                         temp.type = IN;
221                         temp.name_dest = workplace->get_logic_item((*i).logic_id)->get_name();
222                         vec_connector.push_back(temp);
223                 }
224                 i++;
225         }
226
227         return true; //out_connected;
228 }
229
230 ConnectorType And::detect_click_position(int _a, int _b)
231 {
232         switch (imgActual) {
233                 case 0: 
234                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
235                                 return OUT;
236                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
237                                 return IN;
238                         break;  
239                 case 1: 
240                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
241                                 return OUT;
242                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
243                                 return IN;
244                         break;
245                 case 2: 
246                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
247                                 return OUT;
248                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
249                                 return IN;
250                         break;
251                 case 3: 
252                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
253                                 return OUT;
254                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
255                                 return IN;
256         }
257         return UNDEF;
258 }
259
260 void And::update_logic_position() 
261 {
262         switch (imgActual) {
263                 case 0: 
264                         in_x = x;
265                         in_y = y+16;
266                         out_x = x+32;
267                         out_y = y+16;
268                         break;
269                 case 1:
270                         in_x = x+16;
271                         in_y = y;
272                         out_x = x+16;
273                         out_y = y+32;
274                         break;
275                 case 2:
276                         in_x = x+32;
277                         in_y = y+16;
278                         out_x = x;
279                         out_y = y+16;
280                         break;
281                 case 3:
282                         in_x = x+16;
283                         in_y = y+32;
284                         out_x = x+16;
285                         out_y = y;
286         }
287 }
288
289 void And::get_in_logic_connect_position(int& _a, int& _b)
290 {
291         _a = in_x;
292         _b = in_y;
293 }
294
295 void And::get_out_logic_connect_position(int& _a, int& _b)
296 {
297         _a =out_x;
298         _b =out_y;
299 }
300
301 void And::set_out_connected(bool _o)
302 {
303         out_connected = _o;
304 }
305
306 bool And::get_out_logic_connect()
307 {
308         return out_connected;
309 }
310
311 void And::draw_connectors()
312 {
313         Glib::RefPtr<Gdk::Window> window = get_window();
314         int a,b,c,d;
315         switch (imgActual) {
316                 case 0: 
317                         a = in_x-x; 
318                         b = in_y-y-5;
319                         c = out_x-x-10;
320                         d = out_y-y-5;
321                 break;
322                 case 1: 
323                         a = in_x-x-5;
324                         b = in_y-y;
325                         c = out_x-x-5;
326                         d = out_y-y-10;
327                 break;
328                 case 2:
329                         a = in_x-x-10;
330                         b = in_y-y-5;
331                         c = out_x-x;
332                         d = out_y-y-5;
333                 break;
334                 case 3:
335                         a = in_x-x-5;
336                         b = in_y-y-10;
337                         c = out_x-x-5;
338                         d = out_y-y;
339         }
340         gc->set_foreground(red);
341         gc->set_background(red);
342         window->draw_rectangle(gc, 1, a, b, 10, 10);
343         gc->set_foreground(blue);
344         gc->set_background(blue);
345         window->draw_rectangle(gc, 1, c, d, 10, 10);
346         queue_draw();
347 }