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