|
2D Graphics
With Step 2, you saw how to create a
graphic object, display it, and interact with it. The color used to display
the graphic object is the default color (black), and the manager has only
one view. In Step 3, you will create several rectangles with different
colors, place them in different layers, and create other views to display
the manager contents. The entire code is less than 60 lines long.
When running, this step appears like
this:
Changes to Step 2 :
IlvColor* color[4];
color[0] = display->getColor("Red");
color[1] = display->getColor("Green");
... |
This section creates colors using the
IlvColor
class. This class manages the color, one of the available graphic resources.
Creating a color is very easy, just ask the display to provide a
color by giving its name. A color can also be defined by specifying its
RGB or HSV values.
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);
} |
In this part of the code, you create
rectangles and add them in the manager. You create 50 rectangles
with random positions and sizes. Notice the specific types used for position
(IlvPos) and for dimension (IlvDim). For more information
about these data types, please refer to the user or reference manual.
Colors:
Foreground color is changed using
one of the color created previously. Is is also possible to use the IlvPalette
class which groups all the graphic resources.
Layers:
After the creation, each rectangle
is added to the manager. But each rectangle is added in a specific
layer of the manager. For example, layer 0 is the deepest, layer
1
is above. In this example, we have four different layers (one for
each color), so that a red rectangle will always be under a green rectangle,
which will always be under a blue one, and so on.
| manager->fitTransformerToContents(view); |
This manager method to make sure
that all the manager objects are visible in the view. This function
is also available as a default
accelerator (Shift-f key).
Creation of new views
You will now allow the user to create
new views associated with the same manager. They will be
new windows for the manager contents visualization. An accelerator
will trigger the creation of a new view on a double-click event.
| manager->addAccelerator(CreateView, IlvDoubleClick, IlvLeftButton); |
This accelerator is defined on
a double-click event, with the left mouse button. When the user double-clicks
with the left button, the CreateView function is called:
IlvPoint mousePosition;
event.getGLocation(mousePosition); |
First, you get the position of the mouse
when the double-click happened to create the new view at this position.
IlvView* newView = new IlvView(mgr->getDisplay(), "NewView",
"Additional View",
IlvRect(mousePosition.x(), mousePosition.y(), 300,200));
mgr->addView(newView); |
Now you can create the new view.
Its position is given by the mousePosition variable we have just
created, and its size is 300x200 pixels.
| newView->setDestroyCallback(KillView); |
Add this destroy callback to define
a specific behavior when the window is closed.
In this case, the KillWindow
function will destroy the window.
Conclusion
With this new step, you can see how
we can dynamically create graphic objects with less than 60 lines of code.
You may notice that the interactor that was instantiated in Step
2 is still valid, and that it applies to all the visible objects in the
view.
You can now create several views
attached to the same manager. Using the view creation accelerator,
you can create as many views as you want, and you can notice that
every operation (object selection, moving, resizing) processed in a view
is automtically dispatched in all the other views.
Download
source code.
|