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