]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/or.cpp
-Las compuertas pueden tener n entradas pero solo 1 salida
[z.facultad/75.42/plaqui.git] / Constructor / src / or.cpp
1 #include "or.h"
2
3 Or::Or()
4 {
5         out_connected = false;
6         in_x = x;
7         in_y = y+16;
8         out_x = x+32;
9         out_y = y+16;
10         imageN = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_n.png");
11         imageS = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_s.png");
12         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_e.png");
13         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/or_o.png");
14         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/null.png");
15         imgActual = 0;
16         image = imageE;
17         set_size_request(image->get_width(), image->get_height());
18         name = "or";
19         menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("Conectar", menu_image_linea,SigC::slot(*this, &CItem::on_menu_popup_conectar) ) ) ;
20 }
21
22 Or::~Or()
23 {
24 }
25
26 bool Or::on_button_press_event(GdkEventButton *event)
27 {
28         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
29                 if ( CItem::logic_connect ) {
30                         if ( detect_click_position((int)event->x, (int)event->y ) == OUT && !out_connected) {
31                                 CItem::gate_id = ID;
32                                 WorkPlace::pointed = ID;
33                         } 
34                         if ( detect_click_position((int)event->x, (int)event->y ) == IN ) {
35                                 CItem::gate_id = ID;
36                                 WorkPlace::pointed = ID;
37                         }
38                 }
39                 list_pointed->push_back(name);
40                 combo_entry->set_popdown_strings(*list_pointed);
41                 combo_entry->get_entry()->set_text (name);
42         }
43         
44         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 2)){
45                 image = null;   
46                 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);
47                 imgActual++;
48                 switch (imgActual) {
49                         case 1:
50                                 image = imageS;
51                                 in_x = x +16;
52                                 in_y = y;
53                                 out_x = x+16;
54                                 out_y = y+32;
55                                 break;
56                         case 2:
57                                 image = imageO;
58                                 in_x = x+32;
59                                 in_y = y+16;
60                                 out_x = x;
61                                 out_y = y+16;
62                                 break;
63                         case 3:
64                                 image = imageN;
65                                 in_x = x+16;
66                                 in_y = y+32;
67                                 out_x = x+16;
68                                 out_y = y;
69                                 break;
70                         default: 
71                                 imgActual = 0;
72                                 image = imageE;                 
73                                 in_x = x;
74                                 in_y = y+16;
75                                 out_x = x+32;
76                                 out_y = y+16;
77                 }
78                 set_size_request(image->get_width(),image->get_height());
79                 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);
80         }
81         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
82                 menu_popup.popup(event->button, event->time);
83                  return true; //It has been handled.
84         }
85         workplace->queue_draw();
86         return true;
87 }
88
89 void Or::on_menu_popup_rotar()
90 {
91         GdkEventButton event; 
92         event.type = GDK_BUTTON_PRESS;
93         event.button = 2;
94         Or::on_button_press_event(&event);
95 }
96
97 void Or::save(FILE *archivo)
98 {
99         vec_connector.clear();
100         check_connection();
101         Glib::ustring dato;
102         char c_img[50], c_x[50], c_y[50], c_id[50];
103         sprintf(c_id,"%d",ID);
104         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
105         sprintf(c_x,"\t\t<x>%d</x>\n",x);
106         sprintf(c_y,"\t\t<y>%d</y>\n",y);
107         dato = "\t<or nombre=\""+name+"\" id=\"";
108         dato += c_id;
109         dato += "\">\n";
110         dato += c_img;
111         dato += c_x;
112         dato += c_y;
113         for ( int i=0; i<=vec_connector.size()-1&&!vec_connector.empty(); i++) {
114                 if ( vec_connector[i].type == IN ) {
115                         dato += "\t\t<entrada>";
116                         dato += vec_connector[i].name_dest + "</entrada>\n";
117                 } else {
118                         dato += "\t\t<salida>";
119                         dato += vec_connector[i].name_dest + "</salida>\n";
120                 }       
121         }       
122         dato += "\t</or>\n";
123         fprintf(archivo, dato.c_str() );
124 }
125
126 bool Or::check_connection()
127 {
128         int cant_in = 0, cant_out =0;
129         t_logic_connector temp;
130         std::list<t_line>::iterator i = workplace->lista_lineas_in.begin();
131         while ( i != workplace->lista_lineas_in.end() ) {
132                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
133                         temp.type = OUT;
134                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
135                         vec_connector.push_back(temp);
136                         cant_out++;
137                 }
138                 i++;
139         }
140
141         i = workplace->lista_lineas_out.begin();
142         while ( i != workplace->lista_lineas_out.end() ) {
143                 if ( workplace->get_logic_item((*i).logic_id)->get_id() == ID ) {
144                         temp.type = IN;
145                         temp.name_dest = workplace->get_item((*i).store_id)->get_name();
146                         vec_connector.push_back(temp);
147                         cant_in++;
148                 }
149                 i++;
150         }
151
152         //FIXME   la or tiene n entradas y una salida!!!!!!!!!!!!!!!!!!
153         return true;// (cant_in == cant_out );
154 }
155
156 ConnectorType Or::detect_click_position(int _a, int _b)
157 {
158         switch (imgActual) {
159                 case 0: 
160                         if ( (_a<=32 )&&(_a>=22)&&(_b<=20)&&(_b>=10) )
161                                 return OUT;
162                         if ( (_a<=10)&&(_a>=0)&&(_b<=20)&&(_b>=10) )
163                                 return IN;
164                         break;  
165                 case 1: 
166                         if ( (_a<=20 )&&(_a>=10)&&(_b<=32)&&(_b>=22) )
167                                 return OUT;
168                         if ( (_a<=20)&&(_a>=10)&&(_b<=10)&&(_b>=0) )
169                                 return IN;
170                         break;
171                 case 2: 
172                         if ( (_a<=10 )&&(_a>=0)&&(_b<=20)&&(_b>=10) )
173                                 return OUT;
174                         if ( (_a<=32)&&(_a>=22)&&(_b<=20)&&(_b>=10) )
175                                 return IN;
176                         break;
177                 case 3: 
178                         if ( (_a<=20 )&&(_a>=10)&&(_b<=10)&&(_b>=0) )
179                                 return OUT;
180                         if ( (_a<=20)&&(_a>=10)&&(_b<=32)&&(_b>=22) )
181                                 return IN;
182         }
183         return UNDEF;
184 }
185         
186 void Or::update_logic_position() 
187 {
188         switch (imgActual) {
189                 case 0: 
190                         in_x = x;
191                         in_y = y+16;
192                         out_x = x+32;
193                         out_y = y+16;
194                         break;
195                 case 1:
196                         in_x = x+16;
197                         in_y = y;
198                         out_x = x+16;
199                         out_y = y+32;
200                         break;
201                 case 2:
202                         in_x = x+32;
203                         in_y = y+16;
204                         out_x = x;
205                         out_y = y+16;
206                         break;
207                 case 3:
208                         in_x = x+16;
209                         in_y = y+32;
210                         out_x = x+16;
211                         out_y = y;
212         }
213 }
214
215 void Or::get_in_logic_connect_position(int& _a, int& _b)
216 {
217         _a = in_x;
218         _b = in_y;
219 }
220
221 void Or::get_out_logic_connect_position(int& _a, int& _b)
222 {
223         _a =out_x;
224         _b =out_y;
225 }
226
227 void Or::set_out_connected(bool _o)
228 {
229         out_connected = _o;
230 }