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