码迷,mamicode.com
首页 > 其他好文 > 详细

Transformations

时间:2014-12-18 16:40:31      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   os   sp   for   on   

  1 //Transformations.cpp
  2 
  3 
  4 #include "vcf/ApplicationKit/ApplicationKit.h"
  5 #include "vcf/ApplicationKit/ControlsKit.h"
  6 #include "vcf/ApplicationKit/HorizontalLayoutContainer.h"
  7 
  8 
  9 
 10 
 11 
 12 /**
 13 This is a simple application that demonstrates how to modify the 
 14 transformation matrix of a GraphicsContext.  
 15 */
 16 
 17 using namespace VCF;
 18 
 19 /**
 20 A simple panel that holds a series of slider control‘s that enable the 
 21 user to manipulate the values of the translate, scale, shear, and rotation
 22 values for a transformation matrix.
 23 */
 24 class TransFormController : public Panel {
 25 public:
 26 
 27     enum {
 28         transXTag = 0,
 29         transYTag,
 30         shearXTag,
 31         shearYTag,
 32         rotateTag,
 33         scaleXTag,
 34         scaleYTag
 35     }; 
 36 
 37     TransFormController() :xfrmView_(NULL),useConcatFunctions_(false){
 38         HorizontalLayoutContainer* container = new HorizontalLayoutContainer();
 39         setContainer( container );        
 40         
 41         translateX = new Label();
 42         translateX->setCaption( "Translate X:" );
 43         add( translateX );
 44 
 45         SliderControl* slider = new SliderControl();
 46         slider->setName( "translateX" );
 47         add( slider );
 48         slider->setTag( transXTag );
 49         slider->PositionChanged += 
 50             new ClassProcedure1<Event*,TransFormController>( this, &TransFormController::onSliderValChanged, "TransFormController::onSliderValChanged" );
 51 
 52         slider->setMaxValue( 400 );
 53         slider->setMinValue( -400 );
 54         slider->setTickFrequency( 80 );
 55 
 56 
 57 
 58         translateY = new Label();
 59         translateY->setCaption( "Translate Y:" );
 60         add( translateY );
 61 
 62         slider = new SliderControl();
 63         slider->setName( "translateY" );
 64         add( slider );
 65         slider->setTag( transYTag );
 66         slider->PositionChanged += getCallback( "TransFormController::onSliderValChanged" );
 67         slider->setMaxValue( 400 );
 68         slider->setMinValue( -400 );
 69         slider->setTickFrequency( 80 );
 70 
 71         shearX = new Label();
 72         shearX->setCaption( "Shear X:" );
 73         add( shearX );
 74 
 75         slider = new SliderControl();
 76         slider->setName( "shearX" );
 77         add( slider );
 78         slider->setTag( shearXTag );
 79         slider->PositionChanged += getCallback( "TransFormController::onSliderValChanged" );
 80         slider->setMinValue( -360 );
 81         slider->setMaxValue( 360 );
 82         slider->setHasTickMarks( false );
 83         slider->setTickFrequency( 72 );
 84 
 85 
 86         shearY = new Label();
 87         shearY->setCaption( "Shear Y:" );
 88         add( shearY );
 89 
 90         slider = new SliderControl();
 91         slider->setName( "shearY" );
 92         add( slider );
 93         slider->setTag( shearYTag );
 94         slider->PositionChanged += getCallback( "TransFormController::onSliderValChanged" );
 95         slider->setMinValue( -360 );
 96         slider->setMaxValue( 360 );
 97         slider->setHasTickMarks( false );
 98         slider->setTickFrequency( 72 );
 99 
100 
101         scaleX = new Label();
102         scaleX->setCaption( "Scale X:" );
103         add( scaleX );
104 
105         slider = new SliderControl();
106         slider->setName( "scaleX" );
107         slider->setPosition( 100 );
108         add( slider );
109         slider->setTag( scaleXTag );
110         slider->PositionChanged += getCallback( "TransFormController::onSliderValChanged" );
111 
112         scaleY = new Label();
113         scaleY->setCaption( "Scale Y:" );
114         add( scaleY );
115 
116         slider = new SliderControl();
117         slider->setName( "scaleY" );
118         slider->setPosition( 100 );
119         add( slider );
120         slider->setTag( scaleYTag );
121         slider->PositionChanged += getCallback( "TransFormController::onSliderValChanged" );
122 
123         rotate = new Label();
124         rotate->setCaption( "Rotate:" );
125         add( rotate );
126 
127         slider = new SliderControl();
128         slider->setName( "rotate" );
129         add( slider );
130         slider->setTag( rotateTag );
131         slider->PositionChanged += getCallback( "TransFormController::onSliderValChanged" );
132         slider->setMaxValue( 360 );
133         slider->setHasTickMarks( false );
134         slider->setTickFrequency( 36 );
135 
136 
137         CommandButton* reset = new CommandButton();
138         reset->setCaption( "Reset" );
139         reset->addButtonClickHandler( new ClassProcedure1<Event*,TransFormController>( this, &TransFormController::onReset, "TransFormController::onReset" ) );
140         add( reset );
141 
142         CheckBoxControl* checkBox = new CheckBoxControl();
143         checkBox->setCaption( "Use GraphicsContext::concatXXX functions" );
144         checkBox->addButtonClickHandler( new ClassProcedure1<Event*,TransFormController>( this, &TransFormController::onUseConcatFunctions, "TransFormController::onUseConcatFunctions" ) );
145         add( checkBox );
146     }
147 
148     virtual double getPreferredHeight() {
149 
150         HorizontalLayoutContainer* container = (HorizontalLayoutContainer*)getContainer();
151         return container->getMinimumVisibleHeight();
152     }
153 
154     void onReset( Event* e ) {
155         SliderControl* slider = (SliderControl*) findComponent( "translateX" );
156         slider->setPosition( 0.0 );
157 
158         slider = (SliderControl*) findComponent( "translateY" );
159         slider->setPosition( 0.0 );
160 
161         slider = (SliderControl*) findComponent( "scaleX" );
162         slider->setPosition( 100.0 );
163 
164         slider = (SliderControl*) findComponent( "scaleY" );
165         slider->setPosition( 100.0 );
166 
167         slider = (SliderControl*) findComponent( "shearX" );
168         slider->setPosition( 0.0 );
169 
170         slider = (SliderControl*) findComponent( "shearY" );
171         slider->setPosition( 0.0 );
172 
173         slider = (SliderControl*) findComponent( "rotate" );
174         slider->setPosition( 0.0 );
175     }
176 
177     void onUseConcatFunctions( Event* e ) {
178         useConcatFunctions_ = !useConcatFunctions_;
179         if ( NULL != xfrmView_ ) {
180             xfrmView_->repaint();
181         }
182     }
183 
184     void onSliderValChanged( Event* e ) {
185         SliderControl* slider = (SliderControl*)e->getSource();
186         switch( slider->getTag() ) {
187             case transXTag : {
188                 translateX->setCaption( "Translate X: " + StringUtils::toString( slider->getPosition() ) );
189             }
190             break;
191 
192             case transYTag : {
193                 translateY->setCaption( "Translate Y: " + StringUtils::toString( slider->getPosition() ) );
194             }
195             break;
196 
197             case shearXTag : {
198                 shearX->setCaption( "Shear X: " + StringUtils::toString( slider->getPosition() ) );
199             }
200             break;
201 
202             case shearYTag : {
203                 shearY->setCaption( "Shear Y: " + StringUtils::toString( slider->getPosition() ) );
204             }
205             break;
206 
207             case scaleXTag : {
208                 scaleX->setCaption( "Scale X: " + StringUtils::toString( slider->getPosition() ) );
209             }
210             break;
211 
212             case scaleYTag : {
213                 scaleY->setCaption( "Scale Y: " + StringUtils::toString( slider->getPosition() ) );
214             }
215             break;
216 
217             case rotateTag : {
218                 rotate->setCaption( "Rotate: " + StringUtils::toString( slider->getPosition() ) );
219             }
220             break;
221         }
222 
223         if ( NULL != xfrmView_ ) {
224             xfrmView_->repaint();
225         }
226     }
227 
228     Label* translateX;
229     Label* translateY;
230     Label* shearX;
231     Label* shearY;
232     Label* scaleX;
233     Label* scaleY;
234     Label* rotate;
235 
236     Control* xfrmView_;
237 
238     bool useConcatFunctions_;
239 
240     double getTransX() {
241         
242         SliderControl* slider = (SliderControl*) findComponent( "translateX" );
243         return slider->getPosition();
244     }
245 
246     double getTransY() {
247         
248         SliderControl* slider = (SliderControl*) findComponent( "translateY" );
249         return slider->getPosition();
250     }
251 
252     double getShearX() {
253         
254         SliderControl* slider = (SliderControl*) findComponent( "shearX" );
255         return slider->getPosition();
256     }
257 
258     double getShearY() {
259         
260         SliderControl* slider = (SliderControl*) findComponent( "shearY" );
261         return slider->getPosition();
262     }
263 
264     double getScaleX() {
265         
266         SliderControl* slider = (SliderControl*) findComponent( "scaleX" );
267         return slider->getPosition() / 100.0;
268     }
269 
270     double getScaleY() {
271         
272         SliderControl* slider = (SliderControl*) findComponent( "scaleY" );
273         return slider->getPosition() / 100.0;
274     }
275 
276     double getRotate() {
277         
278         SliderControl* slider = (SliderControl*) findComponent( "rotate" );
279         return slider->getPosition();
280     }
281 
282 };
283 
284 
285 
286 bool operator<( const Rect& lhs, const Rect& rhs ) 
287 {
288     
289     return (lhs.getWidth() * lhs.getHeight()) < (rhs.getWidth() * rhs.getHeight());
290 }
291 
292 
293 /**
294 A simple custom control that demonstrates drawing with the a matrix and how to 
295 modify the matrix a whole unit.
296 */
297 class TransformView : public CustomControl {
298 public:
299     TransformView() {
300 
301     }
302 
303     virtual void paint( GraphicsContext* context ) {
304         CustomControl::paint( context );
305 
306         Rect r = getClientBounds();
307         r.inflate( -10, -10 );
308 
309         context->setColor( Color::getColor("white") );
310         context->rectangle( &r );
311         context->fillPath();
312 
313         if ( !(context->getViewableBounds() < r) ) {
314             context->setClippingRect( &r );
315         }
316 
317         int gcs = context->saveState();
318 
319         if ( controller_->useConcatFunctions_ ) {
320             context->concatRotation( controller_->getRotate() );
321             context->concatScale( controller_->getScaleX(), controller_->getScaleY() );
322             context->concatShear( controller_->getShearX(), controller_->getShearY() );
323             context->concatTranslation( controller_->getTransX(), controller_->getTransY() );
324         }
325         else {
326             context->setRotation( controller_->getRotate() );
327             context->setScale( controller_->getScaleX(), controller_->getScaleY() );
328             context->setShear( controller_->getShearX(), controller_->getShearY() );
329             context->setTranslation( controller_->getTransX(), controller_->getTransY() );
330         }
331 
332         context->setStrokeWidth( 1 );
333 
334         context->setColor( Color::getColor("black") );
335 
336 
337         Rect rect(30,40,100,230);
338         context->rectangle( rect );
339 
340         context->circle( 500, 200, 50 );
341 
342         context->strokePath();
343         
344         
345 
346         context->ellipse( 200, 300, 300, 450 );
347         context->setColor( Color::getColor("green") );
348         context->fillPath();
349 
350 
351         context->setStrokeWidth( 2.5 );
352         context->setColor( Color::getColor("red") );
353         context->strokePath();
354 
355 
356         context->setStrokeWidth( 4 );
357         context->setColor( Color::getColor("purple") );
358         context->roundRect( 350, 400, 500, 500, 20, 20 );
359         context->strokePath();
360 
361         context->getCurrentFont()->setName( "Arial" );
362         context->getCurrentFont()->setPointSize( 15 );
363 
364         context->textAt( 200, 200, "Hello There!!" );
365 
366 
367         //note - you may have to adjust the path to this accordingly
368         //feel free to try different images as well
369         Image* img = GraphicsToolkit::createImage( "test.bmp" );
370         if ( NULL != img ) {
371 
372             context->drawImage( 500, 270, img, false );
373 
374             context->drawImage( 200, 10, img, false );
375 
376             context->renderImages();
377 
378             //delete img;
379         }
380 
381         context->setStrokeWidth( 2 );
382         context->setColor( Color::getColor("yellow") );
383         context->rectangle( Rect(499,269,901,351) );
384         context->strokePath();
385 
386         context->restoreState( gcs );
387 
388     }
389 
390     TransFormController* controller_;
391 };
392 
393 
394 
395 class TransformationsWindow : public Window {
396 public:
397     TransformationsWindow() {
398         setCaption( "Transformations" );
399 
400         TransformView* xfrmView = new TransformView() ;
401 
402         add( xfrmView, AlignClient ) ;
403 
404         TransFormController* controller = new TransFormController();
405         controller->setHeight( controller->getPreferredHeight() );
406 
407         add( controller, AlignTop );
408 
409         xfrmView->controller_ = controller;
410         controller->xfrmView_ = xfrmView;
411     }
412 
413     virtual ~TransformationsWindow(){};
414 
415 
416     
417 };
418 
419 
420 
421 
422 class TransformationsApplication : public Application {
423 public:
424 
425     TransformationsApplication( int argc, char** argv ) : Application(argc, argv) {
426 
427     }
428 
429     virtual bool initRunningApplication(){
430         bool result = Application::initRunningApplication();
431         
432         Window* mainWindow = new TransformationsWindow();
433         setMainWindow(mainWindow);
434         mainWindow->setBounds( 100.0, 100.0, 550.0, 700.0 );
435         mainWindow->show();
436         
437         return result;
438     }
439 
440 };
441 
442 
443 int main(int argc, char *argv[])
444 {
445     Application* app = new TransformationsApplication( argc, argv );
446 
447     Application::main();
448     
449     return 0;
450 }

 

Transformations

标签:style   blog   ar   io   color   os   sp   for   on   

原文地址:http://www.cnblogs.com/elitiwin/p/4171839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!