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