]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/or.cpp
se mejora la conexion entre compuertas, se salvan en el XML, hay un par de cosas...
[z.facultad/75.42/plaqui.git] / Constructor / src / or.cpp
1 #include "or.h"
2
3 Or::Or(int orientacion)
4 {
5         is_logic = true;
6         out_connected = false;
7         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_n.png");
8         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_s.png");
9         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_e.png");
10         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_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 = "or";
45         menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("Conectar", menu_image_linea,SigC::slot(*this, &CItem::on_menu_popup_conectar) ) ) ;
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                         dato += "\t\t<entrada>";
156                         dato += vec_connector[i].name_dest + "</entrada>\n";
157                 } else {
158                         dato += "\t\t<salida>";
159                         dato += vec_connector[i].name_dest + "</salida>\n";
160                 }       
161         }       
162         dato += "\t</or>\n";
163         fprintf(archivo, dato.c_str() );
164 }
165
166 bool Or::check_connection()
167 {
168         int cant_in = 0, cant_out =0;
169         t_logic_connector temp;
170         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
171         while ( i != workplace->lista_lineas_in.end() ) {
172                 if ( (*i).logic_id== ID ) {
173                         temp.type = OUT;
174                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
175                         vec_connector.push_back(temp);
176                         cant_out++;
177                 }
178                 i++;
179         }
180
181         i = workplace->lista_lineas_out.begin();
182         while ( i != workplace->lista_lineas_out.end() ) {
183                 if ( (*i).logic_id== ID ) {
184                         temp.type = IN;
185                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
186                         vec_connector.push_back(temp);
187                         cant_in++;
188                 }
189                 i++;
190         }
191
192         i = workplace->lista_lineas_logic.begin();
193         while ( i != workplace->lista_lineas_logic.end() ) {
194                 if ( (*i).logic_id == ID ) {
195                         temp.type = OUT;
196                         temp.name_dest = workplace->get_logic_item((*i).store_id)->get_name();
197                         vec_connector.push_back(temp);
198                         cant_in++;
199                 }
200                 i++;
201         }
202
203         i = workplace->lista_lineas_logic.begin();
204         while ( i != workplace->lista_lineas_logic.end() ) {
205                 if ( (*i).store_id == ID ) {
206                         temp.type = IN;
207                         temp.name_dest = workplace->get_logic_item((*i).logic_id)->get_name();
208                         vec_connector.push_back(temp);
209                         cant_out++;
210                 }
211                 i++;
212         }
213         return true;
214 }
215
216 ConnectorType Or::detect_click_position(int _a, int _b)
217 {
218         switch (imgActual) {
219                 case 0: 
220                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
221                                 return OUT;
222                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
223                                 return IN;
224                         break;  
225                 case 1: 
226                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
227                                 return OUT;
228                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
229                                 return IN;
230                         break;
231                 case 2: 
232                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
233                                 return OUT;
234                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
235                                 return IN;
236                         break;
237                 case 3: 
238                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
239                                 return OUT;
240                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
241                                 return IN;
242         }
243         return UNDEF;
244 }
245         
246 void Or::update_logic_position() 
247 {
248         switch (imgActual) {
249                 case 0: 
250                         in_x = x;
251                         in_y = y+16;
252                         out_x = x+32;
253                         out_y = y+16;
254                         break;
255                 case 1:
256                         in_x = x+16;
257                         in_y = y;
258                         out_x = x+16;
259                         out_y = y+32;
260                         break;
261                 case 2:
262                         in_x = x+32;
263                         in_y = y+16;
264                         out_x = x;
265                         out_y = y+16;
266                         break;
267                 case 3:
268                         in_x = x+16;
269                         in_y = y+32;
270                         out_x = x+16;
271                         out_y = y;
272         }
273 }
274
275 void Or::get_in_logic_connect_position(int& _a, int& _b)
276 {
277         _a = in_x;
278         _b = in_y;
279 }
280
281 void Or::get_out_logic_connect_position(int& _a, int& _b)
282 {
283         _a =out_x;
284         _b =out_y;
285 }
286
287 void Or::set_out_connected(bool _o)
288 {
289         out_connected = _o;
290 }
291
292 bool Or::get_out_logic_connect()
293 {
294         return out_connected;
295 }
296
297 void Or::draw_connectors()
298 {
299         Glib::RefPtr<Gdk::Window> window = get_window();
300         int a,b,c,d;
301         switch (imgActual) {
302                 case 0: 
303                         a = in_x-x; 
304                         b = in_y-y-5;
305                         c = out_x-x-10;
306                         d = out_y-y-5;
307                 break;
308                 case 1: 
309                         a = in_x-x-5;
310                         b = in_y-y;
311                         c = out_x-x-5;
312                         d = out_y-y-10;
313                 break;
314                 case 2:
315                         a = in_x-x-10;
316                         b = in_y-y-5;
317                         c = out_x-x;
318                         d = out_y-y-5;
319                 break;
320                 case 3:
321                         a = in_x-x-5;
322                         b = in_y-y-10;
323                         c = out_x-x-5;
324                         d = out_y-y;
325         }
326         gc->set_foreground(red);
327         gc->set_background(red);
328         window->draw_rectangle(gc, 1, a, b, 10, 10);
329         gc->set_foreground(blue);
330         gc->set_background(blue);
331         window->draw_rectangle(gc, 1, c, d, 10, 10);
332         queue_draw();
333 }