]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/src/cistern.cpp
se arregla el bug de la NOT
[z.facultad/75.42/plaqui.git] / Constructor / src / cistern.cpp
1 #include "cistern.h"
2 #include "cisternptywnd.h"
3 #include "not.h"
4
5 Cistern::Cistern(int orientacion)
6 {
7         in_x = x+5;
8         in_y = y+16;
9         out_x = x + 48;
10         out_y = y + 64;
11         capacidad = 100.0;
12         contenido_inicial = 0;
13         imageE = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/tanque_e.png");
14         imageO = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/tanque_o.png");
15         null = Gdk::Pixbuf::create_from_file(PACKAGE_DATA_DIR"/plaqui-constructor/pixmaps/tanque_null.png");
16         imgActual = orientacion;
17         switch (imgActual) {
18                 case 1:
19                         image = imageO;
20                         break;
21                 default: 
22                         imgActual = 0;
23                         image = imageE;                 
24         }
25         set_size_request(image->get_width(), image->get_height());
26
27         Glib::RefPtr<Gnome::Glade::Xml> ref = Gnome::Glade::Xml::create(PACKAGE_DATA_DIR"/plaqui-constructor/dialogs/constructor.glade", "cistern_pty_wnd");
28         ref->get_widget_derived("cistern_pty_wnd",cistern_pty_wnd);
29         cistern_pty_wnd->cistern = this;
30         cistern_pty_wnd->set_title("Propiedades del Tanque");   
31         name = "tanque";
32         
33         Connector temp;
34         temp.id_dest = -1;
35         temp.type = IN;
36         connect_vec.push_back(temp); // entrada arriba
37         temp.type = OUT;
38         connect_vec.push_back(temp); // entrada abajo
39 }
40
41 Cistern::~Cistern()
42 {
43 }
44
45 bool Cistern::on_button_press_event(GdkEventButton *event)
46 {
47         GdkEventExpose e;
48         t_line tmp_line;
49         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
50                 WorkPlace::pointed = ID;
51                 list_pointed->push_back(name);
52                 combo_entry->set_popdown_strings(*list_pointed);
53                 combo_entry->get_entry()->set_text (name);
54                 if (CItem::logic_connect && CItem::gate_id != -1) {
55                         if ( detect_click_position((int)event->x, (int)event->y) == IN && !workplace->get_logic_item(CItem::gate_id)->get_out_logic_connect()){
56                                 tmp_line.logic_id =  workplace->get_logic_item(CItem::gate_id)->get_id();
57                                 workplace->get_logic_item(CItem::gate_id)->set_out_connected(true);
58                                 tmp_line.store_id = ID;
59                                 workplace->lista_lineas_out.push_back(tmp_line);
60                                 workplace->queue_draw();
61                         } else if (detect_click_position((int)event->x, (int)event->y) == OUT )  { 
62                                 if (dynamic_cast<Not *>(workplace->get_logic_item(CItem::gate_id))) {
63                                         if ( !dynamic_cast<Not *>(workplace->get_logic_item(CItem::gate_id))->get_in_logic_connect()) {
64                                                 workplace->get_logic_item(CItem::gate_id)->set_in_connected(true);
65                                                 tmp_line.logic_id =  CItem::gate_id;
66                                                 tmp_line.store_id = ID;
67                                                 workplace->lista_lineas_out.push_back(tmp_line);
68                                                 workplace->queue_draw();
69                                         }
70                                 } else {
71                                         tmp_line.logic_id =  CItem::gate_id;
72                                         tmp_line.store_id = ID;
73                                         workplace->lista_lineas_out.push_back(tmp_line);
74                                         workplace->queue_draw();
75                                 }                                       
76                         }
77                         CItem::gate_id = -1;
78                 }
79                 char f[20], g[25];
80                 Glib::ustring text;
81                 sprintf(f,"  Capacidad: %.1f  ",capacidad);
82                 sprintf(g,"Contenido Inicial: %.1f",contenido_inicial);
83                 text = name+ f;
84                 text += g;
85                 status_bar->push ( text, 0);            
86         }
87         
88         if ((event->type == GDK_BUTTON_PRESS) && ( event->button ==2)){
89                 image = null; 
90                 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);
91                 imgActual++;
92                 switch (imgActual) {
93                         case 1:
94                                 image = imageO;
95                                 in_x = x + image->get_width()-5;
96                                 in_y = y + 16;
97                                 out_x = x+16;
98                                 out_y = y + image->get_height();
99                                 break;
100                         default: 
101                                 imgActual = 0;
102                                 image = imageE;                 
103                                 in_x = x+5;
104                                 in_y = y+16;
105                                 out_x = x + image->get_width() -16;
106                                 out_y = y + image->get_height();
107                 }
108                 set_size_request(image->get_width(),image->get_height());
109                 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);
110         }
111         
112         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
113                 menu_popup.popup(event->button, event->time);
114                  return true; //It has been handled.
115         }
116         
117         if ((event->type == GDK_2BUTTON_PRESS) && (event->button ==1)){
118                 cistern_pty_wnd->spin_capacidad->set_value( capacidad );
119                 cistern_pty_wnd->spin_inicial->set_value( contenido_inicial );
120                 cistern_pty_wnd->txt_cistern_name->set_text( name );
121                 cistern_pty_wnd->show();
122         }               
123         workplace->queue_draw();
124         return true;
125 }
126
127 void Cistern::on_menu_popup_rotar()
128 {
129         GdkEventButton event; 
130                 event.type = GDK_BUTTON_PRESS;
131                 event.button = 2;
132         Cistern::on_button_press_event(&event);
133 }
134
135 void Cistern::on_menu_popup_propiedades()
136 {
137         GdkEventButton event; 
138                 event.type = GDK_2BUTTON_PRESS;
139                 event.button = 1;
140         Cistern::on_button_press_event(&event);
141 }
142
143 void Cistern::set_capacidad(double _cap)
144 {
145         capacidad = _cap;
146 }
147
148 void Cistern::set_liquid_color(Gdk::Color _color)
149 {
150         liquid_color = _color;
151 }
152
153 void Cistern::set_contenido_inicial(double _ini)
154 {
155         contenido_inicial = _ini;
156 }
157
158 double Cistern::get_capacidad()
159 {
160         return capacidad;
161 }
162
163 double Cistern::get_contenido_inicial()
164 {
165         return contenido_inicial;
166 }
167
168 Gdk::Color Cistern::get_liquid_color()
169 {
170         return liquid_color;
171 }
172
173 void Cistern::save(FILE *archivo)
174 {
175         char c_id[50], c_cap[50], c_x[50], c_y[50], c_img[50], c_ini[50], c_red[50], c_green[50], c_blue[50];
176         Glib::ustring  con0, con1;
177         con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
178         con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
179         sprintf(c_red,"\t\t\t<rojo>%d</rojo>\n",liquid_color.get_red());
180         sprintf(c_green,"\t\t\t<verde>%d</verde>\n",liquid_color.get_green());
181         sprintf(c_blue,"\t\t\t<azul>%d</azul>\n",liquid_color.get_blue());
182         sprintf(c_x,"\t\t<x>%d</x>\n",x);
183         sprintf(c_y,"\t\t<y>%d</y>\n",y);
184         sprintf(c_id,"%d",ID);
185         sprintf(c_cap,"\t\t<capacidad>%.2f</capacidad>\n", capacidad);
186         sprintf(c_ini,"\t\t<inicial>%.2f</inicial>\n", contenido_inicial);
187         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
188         Glib::ustring dato;
189         dato = "\t<tanque nombre=\""+name+"\" id=\"";
190         dato += c_id;
191         dato += "\">\n";
192         dato += c_cap;
193         dato += c_ini;
194         dato += "\t\t<color>\n";
195         dato += c_red;
196         dato += c_green;
197         dato += c_blue;
198         dato += "\t\t</color>\n";
199         dato += "\t\t<conector>\n"+con0+con1+"\t\t</conector>\n";
200         dato += c_img;
201         dato += c_x; 
202         dato += c_y;
203         dato += "\t</tanque>\n";
204         fprintf(archivo,dato.c_str());  
205 }
206
207 bool Cistern::check_connection()
208 {
209         CItem * _item, *_item1;
210         ConnectorType temp0, temp1;
211         switch (get_img_actual()) {
212                 case 0:
213                         temp0 = is_other_connection_area( get_position_x() + 16, get_position_y() - 5, &_item);//arriba - entrada
214                         temp1 = is_other_connection_area( get_position_x()+get_image()->get_width() + 5 , get_position_y()+get_image()->get_height() -16, &_item1);//abajo - salida             
215                         break;
216                 case 1:
217                         temp0 = is_other_connection_area( get_position_x()+get_image()->get_width() - 16, get_position_y() - 5, &_item);//arriba - entrada
218                         temp1 = is_other_connection_area( get_position_x() -5, get_position_y()+get_image()->get_height()-16, &_item1); //abajo - salida
219         }
220         if (temp0 == OUT && temp1 == IN) { 
221                 connect_vec[0].id_dest = _item->get_id();
222                 connect_vec[1].id_dest = _item1->get_id();
223                 return (is_connected = true);
224         }
225         return is_connected;
226 }
227
228 ConnectorType Cistern::get_connector_type(int _a, int _b)
229 {
230         switch (imgActual) {
231                 case 0: if ( (_a <= x+22)&&(_a>=x+10)&&(_b<=y+10)&&(_b > y) )//arriba
232                                                 return connect_vec[0].type;
233                                         if ( (_a <= x+image->get_width()-1)&&(_a >=x+image->get_width()-10)&&(_b<=y+image->get_height()-10)&&(_b >=y+image->get_height()-22) )//abajo
234                                                 return connect_vec[1].type;
235                                         break;
236                 case 1: if ( (_a <= x+image->get_width()-10)&&(_a>=x+image->get_width()-22)&&(_b<=y+10)&&(_b > y) )//arriba
237                                                 return connect_vec[0].type;
238                                         if ( (_a <= x +10)&&(_a > x)&&(_b<=y+image->get_height()-10)&&(_b >=y+image->get_height()-22) )//abajo
239                                                 return connect_vec[1].type;
240         }
241         return UNDEF;
242 }
243
244 void Cistern::set_default_connector()
245 {
246         connect_vec[0].type = IN;
247         connect_vec[1].type = OUT;
248 }
249
250 void Cistern::get_in_logic_connect_position(int& _a, int& _b)
251 {
252         _a = in_x;
253         _b = in_y;
254 }
255
256 void Cistern::get_out_logic_connect_position(int& _a, int& _b)
257 {
258         _a = out_x;
259         _b = out_y;
260 }
261         
262 ConnectorType Cistern::detect_click_position(int _a, int _b)
263 {
264         switch(imgActual) {
265                 case 0:
266                         if ( (_a<=32) &&(_a>=0)&&(_b<=32)&&(_b>=0) )
267                                 return IN;
268                         if ( (_a<=image->get_width())&&(_a>=image->get_width()-32)&&(_b<=image->get_height())&&(_b>=image->get_height()-32) )
269                                 return OUT;
270                         break;
271                 case 1:
272                         if ( (_a<=image->get_width()) &&(_a>=image->get_width()-32)&&(_b<=32)&&(_b>=0) )
273                                 return IN;
274                         if ( (_a<=32)&&(_a>=0)&&(_b<=image->get_height())&&(_b>=image->get_height()-32) )
275                                 return OUT;
276                 }
277                 return UNDEF;
278 }
279                 
280 void Cistern::update_logic_position()
281 {
282         switch (imgActual) {
283                 case 1:
284                         in_x = x + image->get_width()-5;
285                         in_y = y + 16;
286                         out_x = x+16;
287                         out_y = y + image->get_height();
288                         break;
289                 case 0: 
290                         in_x = x+5;
291                         in_y = y+16;
292                         out_x = x + image->get_width() -16;
293                         out_y = y + image->get_height();
294         }
295 }
296
297 void Cistern::draw_connectors()
298 {
299         if ( in_x != -1 ) {
300                 Glib::RefPtr<Gdk::Window> window = get_window();
301                 gc->set_foreground(blue);
302                 window->draw_rectangle(gc, 1, in_x-5-x, in_y-y, 10, 10);
303                 window->draw_rectangle(gc, 1, out_x-5-x, out_y-y-10, 10, 10);
304                 queue_draw();
305         }
306 }