7 The figureitems Library

The figureitems library is implementing a relative simple vector graphics system, sufficient for many purposes. For more advanced graphics, the Mjølner System contains a full-fledged 2D graphics library, called Bifrost (see [MIA 91-13] and [MIA 91-19]).

The vector graphics patterns are all subpatterns of figureItem. FigureItem is a subpattern of windowItem, and inherits all the event handling etc., allowing figureItems to react to user manipulations.

[2kb 260x117 GIF]

FigureItem is the general superpattern for all vector graphics patterns, implementing the common attributes of all graphics patterns. All figureItems have the attribute pen. Pen is an object used for drawing the figureItem. Pen has attributes for defining the drawing pattern of pen, for defining the foreground and background color of pen, and for defining the size (rectangle) of pen.

Line is used for drawing straight lines. The start and end attributes are used for controlling the starting (resp. ending) points of the line.

Shape is the general superpattern for all figureItems that can be filled with some paint. It defines one additional attribute, fill. Fill is an object used for modeling the properties of the paint used for filling the figureItem. Fill has attributes for defining the drawing pattern being used for the paint, and for defining the foreground and background color of the paint.

Oval and Rect are very similar in not defining any additional attributes.

RoundRect is similar to rect but with round corners. The roundness of the corners are defined as ovals and controlled through the roundness attribute.

Wedge defines a `piece of cake' and defines startAngle and endAngle for controlling the wedge.

Polygon is a collection of points defining a connected set of straight lines. Polygon defines one additional attribute, points, used to control the points in the polygon.

7.1 Using the figureitems Library

Remember that in order to utilize this extension to Lidskjalv, the fragment figureitems must be included as follows:

ORIGIN '~beta/guienv/guienv';
INCLUDE '~beta/guienv/figureitems'
--- program: descriptor ---
guienv(# l: @line;
         ...
      do ...
         ps1 -> l.start;
         ps2 -> l.end;
         ...
      #)

7.2 Examples of Use of the figureitems Fragment

This demo program is a little more elaborate. It is a simple draw editor in which you interactively can draw lines and polygons.

Program 6: draw.bet

ORIGIN '~beta/guienv/guienv';
INCLUDE '~beta/guienv/figureitems';
(* This demo gives an example of how you can draw lines and polygons
 * with mouse using the figureitems "line" and "polygon".  Clicking
 * with the left mouse button defines a node in the polygon/line,
 * clicking with the right mouse button ends the definition of the
 * polygon/line and draws it on the screen.
 *)
--- program: descriptor ---
guienv
(# drawEditor: @window
     (# points: @
          (# ps: [16] ^point;
             top: @integer;
             init:
               (# 
               do 0 -> top;
                  (for i: ps.range repeat
                       &point[] -> ps[i][]
                  for)
               #);
             clear:
               (# do 0 -> top #);
             add:
               (# p: @point;
                  size: @integer
               enter p
               do top + 1 -> top;
                  (if top > ps.range then
                      ps.range -> size;
                      size -> ps.extend;
                      (for i: size repeat
                           &point[] -> ps[size + i][]
                      for)
                  if);
                  p -> ps[top]
               #);
          exit ps[1:top]
          #);
        polygonEditor: polygon
          (# open::
               (# do patterns.dkgray[] -> fill.tile #);
             eventHandler::
               (# onMouseDown::
                    (#
                    do drag;
                       this(polygonEditor)[] -> father.selection.set
                    #)
               #)
          #);
        lineEditor: line
          (# eventHandler::
               (# onMouseDown::
                    (#
                    do drag;
                       this(lineEditor)[] -> father.selection.set
                    #)
               #)
          #);
        definingPoly: @boolean;
        eventHandler::
          (# onAboutToClose:: (# do terminate #);
             onMouseDown::
               (# ps: [0] ^point
               do (if definingPoly then
                      (if buttonState
                       //1 then
                          localPosition -> points.add
                       //3 then
                          localPosition -> points.add;
                          points -> ps;
                          (if ps.range > 2 then
                              ps -> createPolygon
                           else
                              ps -> createLine
                          if);
                          false -> definingPoly
                      if);
                   else
                      (if buttonState=1 then
                          true -> definingPoly;
                          points.clear;
                          localPosition -> points.add
                      if)
                  if)
               #)
          #);
        createPolygon:
          (# ps: [0] ^point;
             poly: ^polygon
          enter ps
          do &polygonEditor[] -> poly[];
             poly.open;
             ps -> poly.points
          #);
        createLine:
          (# l: ^line;
             ps: [0] ^point
          enter ps
          do &lineEditor[] -> l[];
             l.open;
             ps[1] -> l.start;
             ps[2] -> l.end
          #);
        open::
          (# 
          do (400,500) -> size;
             contents -> target;
             points.init
          #)
     #)
do drawEditor.open
#)
[29kb 402x309 GIF]


Lidskjalv: User Interface Framework - Reference Manual
© 1994-2002 Mjølner Informatics
[Modified: Friday October 27th 2000 at 14:56]