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