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