]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Constructor/cistern.cpp
8d5a78585a81c92066f59135f975297fc919ae73
[z.facultad/75.42/plaqui.git] / Constructor / cistern.cpp
1 #include "cistern.h"
2 #include "cisternptywnd.h"
3
4 Cistern::Cistern()
5 {
6         imageE = Gdk::Pixbuf::create_from_file("tanque_e.png");
7         imageO = Gdk::Pixbuf::create_from_file("tanque_o.png");
8         null = Gdk::Pixbuf::create_from_file("tanque_null.png");
9         imgActual = 0;
10         image = imageE;
11         set_size_request(image->get_width(), image->get_height());
12
13         Glib::RefPtr<Gnome::Glade::Xml> ref = Gnome::Glade::Xml::create("constructor.glade", "cistern_pty_wnd");
14         ref->get_widget_derived("cistern_pty_wnd",cistern_pty_wnd);
15         cistern_pty_wnd->cistern = this;
16         cistern_pty_wnd->set_title("Propiedades del Tanque");   
17         name = "tanque";
18         
19         Connector temp;
20         temp.id_dest = -1;
21         temp.type = IN;
22         connect_vec.push_back(temp); // entrada arriba
23         temp.type = OUT;
24         connect_vec.push_back(temp); // salida abajo
25 }
26
27 Cistern::~Cistern()
28 {
29 }
30
31 bool Cistern::on_button_press_event(GdkEventButton *event)
32 {
33         GdkEventExpose e;
34         t_line tmp_line;
35         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 1)) {
36                 combo_entry->set_text(name);
37                 if (CItem::logic_connect) {
38                         tmp_line.logic =  workplace->get_logic_item(CItem::quien);
39                         tmp_line.store = this;
40                         workplace->lista_lineas.push_back(tmp_line);
41                         workplace->on_expose_event(&e);
42                 }
43                 CItem::logic_connect = false;   
44         }
45         
46         if ((event->type == GDK_BUTTON_PRESS) && ( event->button ==2)){
47                 image = null; 
48                 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);
49                 imgActual++;
50                 switch (imgActual) {
51                         case 1:
52                                 image = imageO;
53                                 break;
54                         default: 
55                                 imgActual = 0;
56                                 image = imageE;                 
57                 }
58                 set_size_request(image->get_width(),image->get_height());
59                 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);
60         }
61         
62         if ((event->type == GDK_BUTTON_PRESS) && ( event->button == 3)){
63                 menu_popup.popup(event->button, event->time);
64                  return true; //It has been handled.
65         }
66         
67         if ((event->type == GDK_2BUTTON_PRESS) && (event->button ==1)){
68                 cistern_pty_wnd->spin_capacidad->set_value( capacidad );
69                 cistern_pty_wnd->spin_inicial->set_value( contenido_inicial );
70                 cistern_pty_wnd->txt_cistern_name->set_text( name );
71                 cistern_pty_wnd->show();
72         }               
73         return true;
74 }
75
76 void Cistern::on_menu_popup_rotar()
77 {
78         GdkEventButton event; 
79                 event.type = GDK_BUTTON_PRESS;
80                 event.button = 2;
81         Cistern::on_button_press_event(&event);
82 }
83
84 void Cistern::on_menu_popup_propiedades()
85 {
86         GdkEventButton event; 
87                 event.type = GDK_2BUTTON_PRESS;
88                 event.button = 1;
89         Cistern::on_button_press_event(&event);
90 }
91
92 void Cistern::set_capacidad(double _cap)
93 {
94         capacidad = _cap;
95 }
96
97 void Cistern::set_liquid_color(Gdk::Color _color)
98 {
99         liquid_color = _color;
100 }
101
102 void Cistern::set_contenido_inicial(double _ini)
103 {
104         contenido_inicial = _ini;
105 }
106
107 double Cistern::get_capacidad()
108 {
109         return capacidad;
110 }
111
112 double Cistern::get_contenido_inicial()
113 {
114         return contenido_inicial;
115 }
116
117 Gdk::Color Cistern::get_liquid_color()
118 {
119         return liquid_color;
120 }
121
122 void Cistern::save(FILE *archivo)
123 {
124         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];
125         Glib::ustring  con0, con1;
126         con0 = "\t\t\t<entrada>"+get_other_name(connect_vec[0].id_dest)+"</entrada>\n";
127         con1 = "\t\t\t<salida>"+get_other_name(connect_vec[1].id_dest)+"</salida>\n";
128         sprintf(c_red,"\t\t\t<rojo>%d</rojo>\n",liquid_color.get_red());
129         sprintf(c_green,"\t\t\t<verde>%d</verde>\n",liquid_color.get_green());
130         sprintf(c_blue,"\t\t\t<azul>%d</azul>\n",liquid_color.get_blue());
131         sprintf(c_x,"\t\t<x>%d</x>\n",x);
132         sprintf(c_y,"\t\t<y>%d</y>\n",y);
133         sprintf(c_id,"%d",ID);
134         sprintf(c_cap,"\t\t<capacidad>%.2f</capacidad>\n", capacidad);
135         sprintf(c_ini,"\t\t<inicial>%.2f</inicial>\n", contenido_inicial);
136         sprintf(c_img,"\t\t<orientacion>%d</orientacion>\n",imgActual);
137         Glib::ustring dato;
138         dato = "\t<tanque nombre=\""+name+"\" id=\"";
139         dato += c_id;
140         dato += "\">\n";
141         dato += c_cap;
142         dato += c_ini;
143         dato += "\t\t<color>\n";
144         dato += c_red;
145         dato += c_green;
146         dato += c_blue;
147         dato += "\t\t</color>\n";
148         dato += "\t\t<conector>\n"+con0+con1+"\t\t</conector>\n";
149         dato += c_img;
150         dato += c_x; 
151         dato += c_y;
152         dato += "\t</tanque>\n";
153         fprintf(archivo,dato.c_str());  
154 }
155
156 bool Cistern::check_connection()
157 {
158         CItem * _item, *_item1;
159         ConnectorType temp0, temp1;
160         switch (get_img_actual()) {
161                 case 0:
162                         temp0 = is_other_connection_area( get_position_x() + 16, get_position_y() - 5, &_item);//arriba - entrada
163                         temp1 = is_other_connection_area( get_position_x()+get_image()->get_width() + 5 , get_position_y()+get_image()->get_height() -16, &_item1);//abajo - salida             
164                         break;
165                 case 1:
166                         temp0 = is_other_connection_area( get_position_x()+get_image()->get_width() - 16, get_position_y() - 5, &_item);//arriba - entrada
167                         temp1 = is_other_connection_area( get_position_x() -5, get_position_y()+get_image()->get_height()-16, &_item1); //abajo - salida
168         }
169         if (temp0 == OUT && temp1 == IN) { 
170                 connect_vec[0].id_dest = _item->get_id();
171                 connect_vec[1].id_dest = _item1->get_id();
172                 return (is_connected = true);
173         }
174         return is_connected;
175 }
176
177 ConnectorType Cistern::get_connector_type(int _a, int _b)
178 {
179         switch (imgActual) {
180                 case 0: if ( (_a <= x+22)&&(_a>=x+10)&&(_b<=y+10)&&(_b > y) )//arriba
181                                                 return connect_vec[0].type;
182                                         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
183                                                 return connect_vec[1].type;
184                                         break;
185                 case 1: if ( (_a <= x+image->get_width()-10)&&(_a>=x+image->get_width()-22)&&(_b<=y+10)&&(_b > y) )//arriba
186                                                 return connect_vec[0].type;
187                                         if ( (_a <= x +10)&&(_a > x)&&(_b<=y+image->get_height()-10)&&(_b >=y+image->get_height()-22) )//abajo
188                                                 return connect_vec[1].type;
189         }
190         return UNDEF;
191 }
192
193 void Cistern::set_default_connector()
194 {
195         connect_vec[0].type = IN;
196         connect_vec[1].type = OUT;
197 }