]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/not.cpp
-Arregle un poco el tema de que cada compuerta puede tener una sola salida
[z.facultad/75.42/plaqui.git] / Constructor / src / not.cpp
1 #include "not.h"
2
3 Not::Not(int orientacion)
4 {
5         is_logic = true;
6         in_connected = out_connected = false;
7         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/not_n.png");
8         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/not_s.png");
9         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/not_e.png");
10         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/not_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 = "not";
45         menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("Conectar", menu_image_linea,SigC::slot(*this, &CItem::on_menu_popup_conectar) ) ) ;
46 }
47
48 Not::~Not()
49 {
50 }
51
52 bool Not::on_button_press_event(GdkEventButton *event)
53 {
54
55         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
56                 WorkPlace::pointed = ID;
57                 t_line tmp_line;
58                 if (CItem::logic_connect) {
59                         if (CItem::gate_id != -1) {
60                                 if ( detect_click_position((int)event->x, (int)event->y) == IN && !in_connected && !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                                         set_in_connected(true);
64                                         tmp_line.store_id = ID;
65                                         workplace->lista_lineas_logic.push_back(tmp_line);
66                                         workplace->queue_draw();
67                                         CItem::gate_id = -1;
68                                 }
69                         } else
70                                 CItem::gate_id = ID;
71                         if ( detect_click_position((int)event->x, (int)event->y ) == OUT && !out_connected )  {
72                                 CItem::gate_id = ID;
73                         }
74                         if ( detect_click_position((int)event->x, (int)event->y ) == IN && !in_connected) {
75                                 CItem::gate_id = ID;
76                         }
77                 }
78                 list_pointed->push_back(name);
79                 combo_entry->set_popdown_strings(*list_pointed);
80                 combo_entry->get_entry()->set_text (name);
81                 workplace->queue_draw();
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 void Not::on_menu_popup_rotar()
129 {
130         GdkEventButton event; 
131         event.type = GDK_BUTTON_PRESS;
132         event.button = 2;
133         Not::on_button_press_event(&event);
134 }
135
136 void Not::save(FILE *archivo)
137 {
138         vec_connector.clear();
139         check_connection();
140         Glib::ustring dato;
141         char c_img[50], c_x[50], c_y[50], c_id[50];
142         sprintf(c_id,"%d",ID);
143         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
144         sprintf(c_x,"\t\t<x>%d</x>\n",x);
145         sprintf(c_y,"\t\t<y>%d</y>\n",y);
146         dato = "\t<not nombre=\""+name+"\" id=\"";
147         dato += c_id;
148         dato += "\">\n";
149         dato += c_img;
150         dato += c_x;
151         dato += c_y;
152         for ( int i=0; i<=vec_connector.size()-1&&!vec_connector.empty(); i++) {
153                 if ( vec_connector[i].type == IN ) {
154                         dato += "\t\t<entrada>";
155                         dato += vec_connector[i].name_dest + "</entrada>\n";
156                 } else {
157                         dato += "\t\t<salida>";
158                         dato += vec_connector[i].name_dest + "</salida>\n";
159                 }       
160         }
161         dato += "\t</not>\n";
162         fprintf(archivo, dato.c_str() );
163 }
164
165 bool Not::check_connection()
166 {
167         int cant_in = 0, cant_out =0;
168         t_logic_connector temp;
169         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
170         while ( i != workplace->lista_lineas_in.end() ) {
171                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
172                         temp.type = OUT;
173                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
174                         vec_connector.push_back(temp);
175                         cant_out++;
176                 }
177                 i++;
178         }
179
180         i = workplace->lista_lineas_out.begin();
181         while ( i != workplace->lista_lineas_out.end() ) {
182                 if (workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
183                         temp.type = IN;
184                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
185                         vec_connector.push_back(temp);
186                         cant_in++;
187                 }
188                 i++;
189         }
190         return true;// (in_connected    == out_connected ) ==true;
191 }
192
193 ConnectorType Not::detect_click_position(int _a, int _b)
194 {
195         switch (imgActual) {
196                 case 0: 
197                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
198                                 return OUT;
199                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
200                                 return IN;
201                         break;  
202                 case 1: 
203                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
204                                 return OUT;
205                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
206                                 return IN;
207                         break;
208                 case 2: 
209                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
210                                 return OUT;
211                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
212                                 return IN;
213                         break;
214                 case 3: 
215                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
216                                 return OUT;
217                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
218                                 return IN;
219         }
220         return UNDEF;
221 }
222
223 void Not::update_logic_position() 
224 {
225         switch (imgActual) {
226                 case 0: 
227                         in_x = x;
228                         in_y = y+16;
229                         out_x = x+32;
230                         out_y = y+16;
231                         break;
232                 case 1:
233                         in_x = x+16;
234                         in_y = y;
235                         out_x = x+16;
236                         out_y = y+32;
237                         break;
238                 case 2:
239                         in_x = x+32;
240                         in_y = y+16;
241                         out_x = x;
242                         out_y = y+16;
243                         break;
244                 case 3:
245                         in_x = x+16;
246                         in_y = y+32;
247                         out_x = x+16;
248                         out_y = y;
249         }
250 }
251
252 void Not::get_in_logic_connect_position(int& _a, int& _b)
253 {
254         _a = in_x;
255         _b = in_y;
256 }
257
258 void Not::get_out_logic_connect_position(int& _a, int& _b)
259 {
260         _a =out_x;
261         _b =out_y;
262 }
263
264 void Not::set_out_connected(bool _o)
265 {
266         out_connected = _o;
267 }
268
269 void Not::set_in_connected(bool _o)
270 {
271         in_connected = _o;
272 }
273
274 bool Not::get_out_logic_connect()
275 {
276         return out_connected;
277 }
278
279 void Not::draw_connectors()
280 {
281         Glib::RefPtr<Gdk::Window> window = get_window();
282         int a,b,c,d;
283         switch (imgActual) {
284                 case 0: 
285                         a = in_x-x; 
286                         b = in_y-y-5;
287                         c = out_x-x-10;
288                         d = out_y-y-5;
289                 break;
290                 case 1: 
291                         a = in_x-x-5;
292                         b = in_y-y;
293                         c = out_x-x-5;
294                         d = out_y-y-10;
295                 break;
296                 case 2:
297                         a = in_x-x-10;
298                         b = in_y-y-5;
299                         c = out_x-x;
300                         d = out_y-y-5;
301                 break;
302                 case 3:
303                         a = in_x-x-5;
304                         b = in_y-y-10;
305                         c = out_x-x-5;
306                         d = out_y-y;
307         }
308         gc->set_foreground(red);
309         gc->set_background(red);
310         window->draw_rectangle(gc, 1, a, b, 10, 10);
311         gc->set_foreground(blue);
312         gc->set_background(blue);
313         window->draw_rectangle(gc, 1, c, d, 10, 10);
314         queue_draw();
315 }