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