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