1 /* Copyright 2005 Nicholas Bishop
2  *
3  * This file is part of SharpConstruct.
4  *
5  * SharpConstruct is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * SharpConstruct is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with SharpConstruct; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
18 
19 #include "Deformations.hh"
20 #include <gtkmm/checkbutton.h>
21 #include <gtkmm/colorbutton.h>
22 
23 using SharpConstruct::GInterface::Deformations;
24 
Deformations()25 Deformations::Deformations()
26 : flood_( 0, 100 )
27 {}
28 
Initialize(Glib::RefPtr<Gnome::Glade::Xml> glade)29 void Deformations::Initialize( Glib::RefPtr< Gnome::Glade::Xml > glade )
30 {
31 	using sigc::mem_fun;
32 
33 	glade_ = glade;
34 
35 	// Connect immediates
36 	glade_->connect_clicked( "DefSubdivide", mem_fun( *this, &Deformations::Subdivide ) );
37 	glade_->connect_clicked( "DefMirror", mem_fun( *this, &Deformations::on_mirror_ ) );
38 	glade_->connect_clicked( "DefFlipNormals", mem_fun( *this, &Deformations::FlipNormals ) );
39 	glade_->connect_clicked( "DefUnify", mem_fun( *this, &Deformations::Unify ) );
40 	glade_->connect_clicked( "DefToTriangles", mem_fun( *this, &Deformations::ToTriangles ) );
41 
42 	Gtk::Table* pt;
43 	glade_->get_widget( "PreviewableTable", pt );
44 
45 	pt->attach( move_,     1, 2, 0, 1 );
46 	pt->attach( scale_,    1, 2, 1, 2 );
47 	pt->attach( deflate_,  1, 2, 2, 3 );
48 	pt->attach( spherize_, 1, 2, 3, 4 );
49 	pt->attach( smooth_,   1, 2, 4, 5 );
50 	pt->attach( flood_,    1, 2, 5, 6 );
51 
52 	std::list< Slider* > sliders;
53 	sliders.push_back( &move_ );
54 	sliders.push_back( &scale_ );
55 	sliders.push_back( &deflate_ );
56 	sliders.push_back( &spherize_ );
57 	sliders.push_back( &smooth_ );
58 	sliders.push_back( &flood_ );
59 
60 	move_.SignalContinue().connect( mem_fun( *this, &Deformations::on_move_ ) );
61 	move_.SignalEnd().connect( mem_fun( *this, &Deformations::on_move_ ) );
62 	scale_.SignalContinue().connect( mem_fun( *this, &Deformations::on_scale_ ) );
63 	scale_.SignalEnd().connect( mem_fun( *this, &Deformations::on_scale_ ) );
64 	deflate_.SignalContinue().connect( mem_fun( *this, &Deformations::on_deflate_ ) );
65 	deflate_.SignalEnd().connect( mem_fun( *this, &Deformations::on_deflate_ ) );
66 	spherize_.SignalContinue().connect( mem_fun( *this, &Deformations::on_spherize_ ) );
67 	spherize_.SignalEnd().connect( mem_fun( *this, &Deformations::on_spherize_ ) );
68 	smooth_.SignalContinue().connect( mem_fun( *this, &Deformations::on_smooth_ ) );
69 	smooth_.SignalEnd().connect( mem_fun( *this, &Deformations::on_smooth_ ) );
70 	flood_.SignalContinue().connect( mem_fun( *this, &Deformations::on_flood_ ) );
71 	flood_.SignalEnd().connect( mem_fun( *this, &Deformations::on_flood_ ) );
72 
73 	for( std::list< Slider* >::iterator i = sliders.begin();
74 	     i != sliders.end(); ++i )
75 	{
76 		( *i )->SignalStart().connect( mem_fun( *this, &Deformations::before_deformation_ ) );
77 		( *i )->SignalEnd().connect( mem_fun( *this, &Deformations::after_deformation_ ) );
78 	}
79 }
80 
x_() const81 bool Deformations::x_() const
82 {
83 	Gtk::ToggleButton* tb;
84 	glade_->get_widget( "DefX", tb );
85 	return tb->get_active();
86 }
87 
y_() const88 bool Deformations::y_() const
89 {
90 	Gtk::ToggleButton* tb;
91 	glade_->get_widget( "DefY", tb );
92 	return tb->get_active();
93 }
94 
z_() const95 bool Deformations::z_() const
96 {
97 	Gtk::ToggleButton* tb;
98 	glade_->get_widget( "DefZ", tb );
99 	return tb->get_active();
100 }
101 
preview_() const102 bool Deformations::preview_() const
103 {
104 	Gtk::CheckButton* cb;
105 	glade_->get_widget( "PreviewDeformations", cb );
106 	return cb->get_active();
107 }
108 
Subdivide()109 void Deformations::Subdivide()
110 {
111 	Gtk::CheckButton* cb;
112 	glade_->get_widget( "DefSubdivideSmooth", cb );
113 	MeshHistory::Instance().AddMesh( true, true, true );
114 	MeshHistory::Instance().GetCurrentMesh().Subdivide( cb->get_active() );
115 	MeshHistory::Instance().GetCurrentMesh().RecalculateAllNormals();
116 }
on_mirror_()117 void Deformations::on_mirror_()
118 {
119 	MeshHistory::Instance().AddMesh( true, false, true );
120 	MeshHistory::Instance().GetCurrentMesh().Mirror( x_(), y_(), z_() );
121 }
FlipNormals()122 void Deformations::FlipNormals()
123 {
124 	MeshHistory::Instance().AddMesh( false, false, true );
125 	MeshHistory::Instance().GetCurrentMesh().FlipNormals();
126 }
Unify()127 void Deformations::Unify()
128 {
129 	MeshHistory::Instance().AddMesh( true, true, true );
130 	MeshHistory::Instance().GetCurrentMesh().Unify();
131 }
ToTriangles()132 void Deformations::ToTriangles()
133 {
134 	MeshHistory::Instance().AddMesh( true, true, true );
135 	MeshHistory::Instance().GetCurrentMesh().ToTriangles();
136 }
137 
before_deformation_(const double)138 void Deformations::before_deformation_( const double )
139 {
140 	if( preview_() )
141 	{
142 		mesh_copy_.SetVertices( new MeshHistory::PartialMesh::PartialVertexData );
143 		mesh_copy_.SetColors( new MeshHistory::PartialMesh::PartialColorData );
144 		*mesh_copy_.Vertices() = *MeshHistory::Instance().CurrentComposite().Vertices();
145 		*mesh_copy_.Colors() = *MeshHistory::Instance().CurrentComposite().Colors();
146 	}
147 }
148 
revert_to_copy_()149 void Deformations::revert_to_copy_()
150 {
151 	*MeshHistory::Instance().CurrentComposite().Vertices() =
152 		*mesh_copy_.Vertices();
153 	*MeshHistory::Instance().CurrentComposite().Colors() =
154 		*mesh_copy_.Colors();
155 }
156 
on_move_(const double v)157 void Deformations::on_move_( const double v )
158 {
159 	if( preview_() || !move_.InSlide() )
160 	{
161 		if( preview_() )
162 			revert_to_copy_();
163 		else
164 			MeshHistory::Instance().AddMesh( true, false, false );
165 
166 		float scaled_value = v / 100;
167 		scaled_value *= MeshHistory::Instance().GetCurrentMesh().Diameter();
168 		MeshHistory::Instance().GetCurrentMesh().Move
169 			( scaled_value * x_(), scaled_value * y_(), scaled_value * z_() );
170 	}
171 }
172 
on_scale_(const double v)173 void Deformations::on_scale_( const double v )
174 {
175 	if( preview_() || !scale_.InSlide() )
176 	{
177 		if( preview_() )
178 			revert_to_copy_();
179 		else
180 			MeshHistory::Instance().AddMesh( true, false, false );
181 
182 		float clipped_value = v / 50;
183 		if( clipped_value > 0 )
184 			clipped_value++;
185 		else
186 			clipped_value--;
187 		MeshHistory::Instance().GetCurrentMesh().Resize( clipped_value * x_(),
188 								 clipped_value * y_(),
189 								 clipped_value * z_() );
190 	}
191 }
192 
on_deflate_(const double v)193 void Deformations::on_deflate_( const double v )
194 {
195 	if( preview_() || !deflate_.InSlide() )
196 	{
197 		if( preview_() )
198 			revert_to_copy_();
199 		else
200 			MeshHistory::Instance().AddMesh( true, false, false );
201 
202 		MeshHistory::Instance().GetCurrentMesh().Deflate( v / 200 );
203 	}
204 }
205 
on_spherize_(const double v)206 void Deformations::on_spherize_( const double v )
207 {
208 	if( preview_() || !spherize_.InSlide() )
209 	{
210 		if( preview_() )
211 			revert_to_copy_();
212 		else
213 			MeshHistory::Instance().AddMesh( true, false, false );
214 
215 		MeshHistory::Instance().GetCurrentMesh().Spherize( v / 100 );
216 	}
217 }
218 
on_smooth_(const double v)219 void Deformations::on_smooth_( const double v )
220 {
221 	if( preview_() || !smooth_.InSlide() )
222 	{
223 		if( preview_() )
224 			revert_to_copy_();
225 		else
226 			MeshHistory::Instance().AddMesh( true, false, false );
227 
228 		MeshHistory::Instance().GetCurrentMesh().Smooth( v / 100 );
229 	}
230 }
231 
on_flood_(const double v)232 void Deformations::on_flood_( const double v )
233 {
234 	if( preview_() || !flood_.InSlide() )
235 	{
236 		if( preview_() )
237 			revert_to_copy_();
238 		else
239 			MeshHistory::Instance().AddMesh( false, true, false );
240 
241 		Gtk::ColorButton* cb;
242 		glade_->get_widget( "DefColor", cb );
243 		const Gdk::Color gcol( cb->get_color() );
244 		Color color( gcol.get_red_p(), gcol.get_green_p(), gcol.get_blue_p() );;
245 		MeshHistory::Instance().GetCurrentMesh().Flood( color, v / 100 );
246 	}
247 }
248 
249 
after_deformation_(const double)250 void Deformations::after_deformation_( const double )
251 {
252 	if( preview_() )
253 	{
254 		MeshHistory::PartialMesh::PartialVertexData* pvd =
255 			MeshHistory::Instance().CurrentComposite().Vertices();
256 		MeshHistory::PartialMesh::PartialColorData* pcd =
257 			MeshHistory::Instance().CurrentComposite().Colors();
258 		MeshHistory::Instance().AddMesh( true, true, false );
259 		*pvd = *mesh_copy_.Vertices();
260 		*pcd = *mesh_copy_.Colors();
261 	}
262 }
263