// STEP 4 // Creation of a manager, a view, and a graphic object // Add an exit callback and a selection interactor // Add many rectangles of different colors, add another view and an accelerator // Add a timer to animate an ellipse #include #include #include #include #include static void Quit(IlvView* view, IlvAny) { delete view->getDisplay();; IlvExit(0); } static void QuitAccel(IlvManager* mgr, IlvView* view, IlvEvent& event, IlvAny userArg) { IlvDisplay* display = mgr->getDisplay(); delete display; IlvExit(0); } static void KillView(IlvView* view, IlvAny userArg) { delete view; } static void CreateView(IlvManager* mgr, IlvView* view, IlvEvent& event, IlvAny userArg) { // Get the coordinates of the double click event IlvPoint mousePosition; event.getGLocation(mousePosition); IlvView* newView = new IlvView(mgr->getDisplay(), "NewView", "Additional View", IlvRect(mousePosition.x(), mousePosition.y(), 300,200)); mgr->addView(newView); newView->setDestroyCallback(KillView); } static void AnimateEllipse(IlvTimer* timer, IlvAny userArg) { static dx = 10; static dy = 10; IlvManager* manager = (IlvManager*)userArg; IlvFilledEllipse* mobile = (IlvFilledEllipse*)manager->getObject("MovingEllipse"); if (!mobile) return; IlvRect bbox; mobile->boundingBox(bbox); if ((bbox.x() < 0) || (bbox.x() > 1000)) dx = -dx; if ((bbox.y() < 0) || (bbox.y() > 1000)) dy = -dy; manager->translateObject(mobile, dx, dy); } static void stopTimer(IlvManager* mgr, IlvView* view, IlvEvent& event, IlvAny userArg) { IlvTimer* timer = (IlvTimer*)userArg; timer->suspend(); } static void runTimer(IlvManager* mgr, IlvView* view, IlvEvent& event, IlvAny userArg) { IlvTimer* timer = (IlvTimer*)userArg; timer->run(); } int main(int argc, char** argv) { // Creation of the Display IlvDisplay* display = new IlvDisplay("EvalKit1"); // Creation of the Manager IlvManager* manager = new IlvManager(display); // Creation of the first view IlvView* view = new IlvView(display, "View1", "Main View - Step 4", IlvRect(0,0,600,400)); // Attach the view to the manager manager->addView(view); // Add a selection interactor so that the rectangle can be selected, moved, resized // and destroyed // Creation of the interactor IlvSelectInteractor* selector = new IlvSelectInteractor(manager, view); // Set the interactor on the main view // Every object displayed in this view will be selectable, moveable, resizable manager->setInteractor(selector, view); // Add an user-defined accelerator to quit the application manager->addAccelerator(QuitAccel, IlvKeyDown, 'q'); // Add another accelerator to create a new view manager->addAccelerator(CreateView, IlvDoubleClick, IlvLeftButton); // Creation of the colors IlvColor* color[4]; color[0] = display->getColor("Red"); color[1] = display->getColor("Green"); color[2] = display->getColor("Blue"); color[3] = display->getColor("Yellow"); // Creation of 50 filled rectangles for (IlvUInt i=0; i < 50; i++) { IlvPos x = rand() % 1000; IlvPos y = rand() % 1000; IlvDim w = rand() % 100; IlvDim h = rand() % 100; IlvFilledRectangle* rectangle = new IlvFilledRectangle(display, IlvRect(x,y,w,h)); rectangle->setForeground(color[i%4]); int layer = i % 4; manager->addObject(rectangle, IlvFalse, layer); } // Creation of the moving ellipse on the top layer IlvPos x = rand() % 1000; IlvPos y = rand() % 1000; IlvFilledEllipse* mobile = new IlvFilledEllipse(display, IlvRect(x,y,30,30)); mobile->setForeground(display->getColor("White")); manager->addObject(mobile, IlvFalse, 4); // Give a name to the ellipse object manager->setObjectName(mobile, "MovingEllipse"); // Creation of the timer to animate it IlvTimer* timer = new IlvTimer(display, 0, 50, AnimateEllipse, manager); timer->run(); // Add 2 accelerators to manage the timer manager->addAccelerator(stopTimer, IlvKeyDown, 's', 0, timer); manager->addAccelerator(runTimer, IlvKeyDown, 'r', 0, timer); // Add an exit callback so that the exit button is valid view->setDestroyCallback(Quit); // Change the zooming factor of the view so that all the objects are visible manager->fitTransformerToContents(view); // Run the event loop IlvMainLoop(); return 0; }