]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/or.cpp
-Arregle un poco el tema de que cada compuerta puede tener una sola salida
[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         }
78         
79         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 2)){
80                 image = null;   
81                 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);
82                 imgActual++;
83                 switch (imgActual) {
84                         case 1:
85                                 image = imageS;
86                                 in_x = x +16;
87                                 in_y = y;
88                                 out_x = x+16;
89                                 out_y = y+32;
90                                 break;
91                         case 2:
92                                 image = imageO;
93                                 in_x = x+32;
94                                 in_y = y+16;
95                                 out_x = x;
96                                 out_y = y+16;
97                                 break;
98                         case 3:
99                                 image = imageN;
100                                 in_x = x+16;
101                                 in_y = y+32;
102                                 out_x = x+16;
103                                 out_y = y;
104                                 break;
105                         default: 
106                                 imgActual = 0;
107                                 image = imageE;                 
108                                 in_x = x;
109                                 in_y = y+16;
110                                 out_x = x+32;
111                                 out_y = y+16;
112                 }
113                 set_size_request(image->get_width(),image->get_height());
114                 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);
115         }
116         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
117                 menu_popup.popup(event->button, event->time);
118                  return true; //It has been handled.
119         }
120         workplace->queue_draw();
121         return true;
122 }
123
124 void Or::on_menu_popup_rotar()
125 {
126         GdkEventButton event; 
127         event.type = GDK_BUTTON_PRESS;
128         event.button = 2;
129         Or::on_button_press_event(&event);
130 }
131
132 void Or::save(FILE *archivo)
133 {
134         vec_connector.clear();
135         check_connection();
136         Glib::ustring dato;
137         char c_img[50], c_x[50], c_y[50], c_id[50];
138         sprintf(c_id,"%d",ID);
139         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
140         sprintf(c_x,"\t\t<x>%d</x>\n",x);
141         sprintf(c_y,"\t\t<y>%d</y>\n",y);
142         dato = "\t<or nombre=\""+name+"\" id=\"";
143         dato += c_id;
144         dato += "\">\n";
145         dato += c_img;
146         dato += c_x;
147         dato += c_y;
148         for ( int i=0; i<=vec_connector.size()-1&&!vec_connector.empty(); i++) {
149                 if ( vec_connector[i].type == IN ) {
150                         dato += "\t\t<entrada>";
151                         dato += vec_connector[i].name_dest + "</entrada>\n";
152                 } else {
153                         dato += "\t\t<salida>";
154                         dato += vec_connector[i].name_dest + "</salida>\n";
155                 }       
156         }       
157         dato += "\t</or>\n";
158         fprintf(archivo, dato.c_str() );
159 }
160
161 bool Or::check_connection()
162 {
163         int cant_in = 0, cant_out =0;
164         t_logic_connector temp;
165         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
166         while ( i != workplace->lista_lineas_in.end() ) {
167                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
168                         temp.type = OUT;
169                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
170                         vec_connector.push_back(temp);
171                         cant_out++;
172                 }
173                 i++;
174         }
175
176         i = workplace->lista_lineas_out.begin();
177         while ( i != workplace->lista_lineas_out.end() ) {
178                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
179                         temp.type = IN;
180                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
181                         vec_connector.push_back(temp);
182                         cant_in++;
183                 }
184                 i++;
185         }
186
187         
188         return true;// out_connected;
189 }
190
191 ConnectorType Or::detect_click_position(int _a, int _b)
192 {
193         switch (imgActual) {
194                 case 0: 
195                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
196                                 return OUT;
197                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
198                                 return IN;
199                         break;  
200                 case 1: 
201                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
202                                 return OUT;
203                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
204                                 return IN;
205                         break;
206                 case 2: 
207                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
208                                 return OUT;
209                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
210                                 return IN;
211                         break;
212                 case 3: 
213                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
214                                 return OUT;
215                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
216                                 return IN;
217         }
218         return UNDEF;
219 }
220         
221 void Or::update_logic_position() 
222 {
223         switch (imgActual) {
224                 case 0: 
225                         in_x = x;
226                         in_y = y+16;
227                         out_x = x+32;
228                         out_y = y+16;
229                         break;
230                 case 1:
231                         in_x = x+16;
232                         in_y = y;
233                         out_x = x+16;
234                         out_y = y+32;
235                         break;
236                 case 2:
237                         in_x = x+32;
238                         in_y = y+16;
239                         out_x = x;
240                         out_y = y+16;
241                         break;
242                 case 3:
243                         in_x = x+16;
244                         in_y = y+32;
245                         out_x = x+16;
246                         out_y = y;
247         }
248 }
249
250 void Or::get_in_logic_connect_position(int& _a, int& _b)
251 {
252         _a = in_x;
253         _b = in_y;
254 }
255
256 void Or::get_out_logic_connect_position(int& _a, int& _b)
257 {
258         _a =out_x;
259         _b =out_y;
260 }
261
262 void Or::set_out_connected(bool _o)
263 {
264         out_connected = _o;
265 }
266
267 bool Or::get_out_logic_connect()
268 {
269         return out_connected;
270 }
271
272 void Or::draw_connectors()
273 {
274         Glib::RefPtr<Gdk::Window> window = get_window();
275         int a,b,c,d;
276         switch (imgActual) {
277                 case 0: 
278                         a = in_x-x; 
279                         b = in_y-y-5;
280                         c = out_x-x-10;
281                         d = out_y-y-5;
282                 break;
283                 case 1: 
284                         a = in_x-x-5;
285                         b = in_y-y;
286                         c = out_x-x-5;
287                         d = out_y-y-10;
288                 break;
289                 case 2:
290                         a = in_x-x-10;
291                         b = in_y-y-5;
292                         c = out_x-x;
293                         d = out_y-y-5;
294                 break;
295                 case 3:
296                         a = in_x-x-5;
297                         b = in_y-y-10;
298                         c = out_x-x-5;
299                         d = out_y-y;
300         }
301         gc->set_foreground(red);
302         gc->set_background(red);
303         window->draw_rectangle(gc, 1, a, b, 10, 10);
304         gc->set_foreground(blue);
305         gc->set_background(blue);
306         window->draw_rectangle(gc, 1, c, d, 10, 10);
307         queue_draw();
308 }