DisplayPainter

class mono::display::DisplayPainter

The DisplayPainter draws shapes on a display, using the DisplayController interface.

You should use this class to draw shapes on the screen, and use it from inside view only. The standard view class has a reference to to an instance of this class.

The coordinate system used by the painter is the same as used by the display interface. This means all shape coords are relative to the display origo at the top left corner for the screen, when screen is in portrait mode.

A painter keeps track of an active foreground and background color. You can set new colors at any time, and the succeeding draw calls will paint in that color. The standard draw color is the active foreground color, but some draw routines might use the background color also. An example is the font drawing routines.

Like colors, the painter object keeps a active line width and textsize. When drawing shapes based on lines drawLine, drawPolygon, drawRect and drawEllipse, the line width used is the currently active line width property of the painter object.

When painting text characters the character size is dependend on the textsize property. Text painting is not affected by the current line width.

Public Functions

DisplayPainter(IDisplayController *displayController)

Construct a new painter object that are attached to a display. A painter object is automatically initialized by the view/UI system and shared among the view classes.

In most cases you should not have to initialize your own display painter.

Parameters
  • displayController -

    A pointer to the display controller of the active display

template <typename Owner>
void mono::display::DisplayPainter::setRefreshCallback(Owner * obj, void(Owner::*)(void) memPtr)

Set/Overwrite the display tearing effect / refresh callback.

Set the Painters display refresh callback handler. The display refreshes the screen at a regular interval. To avoid graphical artifacts, you should restrict your paint calls to right after this callback gets triggered.

The default View painter already has a callback installed, that triggers the View’s re-paint queue. If you create you own painter object you can safely overwrite this callback.

Parameters
  • obj -

    The this pointer for the object who shoould have its member function called

  • memPtr -

    A pointer the the class’ member function.

void setForegroundColor(Color color)

MARK: Color Accessors.

Set the painters foreground pencil color

Parameters
  • color -

    The new foreground color

void setBackgroundColor(Color color)

Sets the painters background pencil color.

Parameters
  • color -

    the new background color

Color ForegroundColor() const

Gets the painters current foreground pencil color.

Return
The current foreground color

Color BackgroundColor() const

Gets the painters current background pencil color.

Return
The current foreground color

void useAntialiasedDrawing(bool enable = true)

Turn on/off anti-aliased line drawing.

MARK: Anti-aliasing Accessors.

You can enable or disable anti-aliased drawing if you need nicer graphics or faster rendering. Anti-aliasing smoothes lines edges, that can otherwise appear jagged.

Parameters
  • enable -

    Optional: Switch to turn on/off anti-aliasing. Deafult is enabled.

bool IsAntialiasedDrawing()

Returns true if anti-aliased drawing is enabled.

See
useAntialiasedDrawing
Return
true if enabled, false otherwise.

uint8_t LineWidth() const

MARK: Pencil Property Accessors.

uint16_t CanvasWidth() const

MARK: Painting Canvas Info Accessors.

Get the canvas width in pixels. This is the display display width as well.

Return
The canvas/display width in pixels

uint16_t CanvasHeight() const

Get the canvas height in pixels. This is the display display height as well.

Return
The canvas/display height in pixels

IDisplayController *DisplayController() const

Get a pointer to the painters current display controller You can use this method to obtain the display controller interface if you need to blit pixels directly to the display.

Return
A pointer to an object implementing the IDisplayController interface

void drawPixel(uint16_t x, uint16_t y, bool background = false)

MARK: Drawing methods.

Draw a single pixel on a specific position on the display.

The pixel will be the active foreground color, unless you set the third parameter to true.

Parameters
  • x -

    The X-coordinate

  • y -

    The Y coordinate

  • background -

    Optional: Set to true to paint with active background color.

void drawPixel(uint16_t x, uint16_t y, uint8_t intensity, bool background = false)

Draw a pixel and blend it with the background/foreground color.

Use this method to draw transparent pixels. You define an intensity of the pixel, that corrosponds to its Alpha value or opacity. 0 is totally transparent and 255 is fully opaque.

Only the foreground is influenced by the alpha value, the background is always treated as fully opaque.

Parameters
  • x -

    The pixels x coordinate

  • y -

    The puxels y coordinate

  • intensity -

    The alpha value, 0 to 255 where 0 is transparent.

  • Optionaluse -

    the background color as foreground and vice versa

void drawFillRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, bool background = false)

Draw a filled rectangle.

Paints a filled rectangle in the actuive foreground color. Coordinates a defining the point of the rectangles upper left corner and are given in screen coordinates. (Absolute coordinates)

Parameters
  • x -

    X coordinate of upper left corner, in screen coordinates.

  • y -

    Y coordinate of upper left corner, in screen coordinates.

  • width -

    The width of the rectangle

  • height -

    The height of the rectangle

  • background -

    Optional: Set to true to paint in active background color

void drawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, bool background = false)

Draw a straight line between two points.

MARK: Lines.

Draw a line on the display. The line is defined by its two end points. End point coordinates are in absolute screen coordinates.

The line is not anti-aliased.

Based on Bresenham’s algorithm. But If the line you intend to draw is horizontal of vertical, this method will usemore efficient routines specialized for these cases.

Parameters
  • x1 -

    The X coordinate of the lines first endpoint

  • y1 -

    The Y coordinate of the lines first endpoint

  • x2 -

    The X coordinate of the lines second endpoint

  • y2 -

    The Y coordinate of the lines second endpoint

  • background -

    Optional: Set this to true to paint in active background color

void drawRect(uint16_t x, uint16_t y, uint16_t width, uint16_t height, bool background = false)

Draw a outlined rectangle.

MARK: Rects.

Draw an outlined rectangle with the current line width and the active color.

Parameters
  • x -

    Top left corner X coordinate

  • x -

    Top left corner X coordinate

  • width -

    The rectangles width

  • height -

    The rectangles height

  • background -

    Optional: Set this to true to paint in active background color

void drawChar(uint16_t x, uint16_t y, char character)

Draw a single character on the display.

MARK: Simple Characters.

Paint a single ASCII character on the display. Characters are always painted in the foreground color, with the background color as background fill.

The character is painted in the currently selected text size. The text is a monospaced font, width a minimum size of (5,3) per character. The origo of a character is the upper left corner of the (5,3) rectangle.

If you want write text on the screen, you should use the TextLabel view, or the Console view.

Parameters
  • x -

    The X coordinate of the characters upper left corner

  • y -

    The Y coordinate of the characters upper left corner

  • character -

    The text character to draw

void drawVLine(uint16_t x, uint16_t y1, uint16_t y2, bool background = false)

Helper function to draw a vertical line very fast. This method uses much less communication with the display.

*This method is automatically called by drawLine*

Parameters
  • x -

    The lines X coordinate (same for both end points)

  • y1 -

    The first end points Y coordinate

  • y2 -

    The second end points Y coordinate

  • background -

    Optional: Set this to true to paint in active background color

void drawHLine(uint16_t x1, uint16_t x2, uint16_t y, bool background = false)

Helper function to draw a horizontal line very fast. This method uses much less communication with the display.

*This method is automatically called by drawLine*

Parameters
  • x1 -

    The first end points X coordinate

  • x2 -

    The second end points X coordinate

  • y -

    The lines Y coordinate (same for both end points)

  • background -

    Optional: Set this to true to paint in active background color

void drawCircle(uint16_t x0, uint16_t y0, uint16_t r, bool background = false)

Paint an outlined circle.

MARK: Circles.

Protected Functions

void swap(uint16_t &a, uint16_t &b)

MARK: Simple Helper Methods.

Inline swap of two numbers.

Protected Attributes

mbed::FunctionPointer displayRefreshHandler

Handler for the DisplayControllers action queue, that gets triggered when the display refreshes.

This handler is normally used by the first View that gets contructed, to enable the re-paint queue.