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