Skip to main content

Line

Simple CanvasWidget capable of drawing a line from one point to another point. The end points can be moved to new locations and the line width can be set and changed. A 10 pixel long line along the top of the screen with a width on 1 pixel has endpoints in (0, 0.5) and (10, 0.5) and line width 1. The Line class calculates the corners of the shape, which in this case would be (0, 0), (10, 0), (10, 1) and (0, 1) and tells CanvasWidgetRenderer to moveTo() the first coordinate and then lineTo() the next coordinates in order. Finally it tells CWR to render the inside of the shape using the set Painter object.

The Line class caches the four corners of the shape to speed up redrawing. In general, drawing lines involve some extra mathematics for calculating the normal vector of the line and this computation would slow down re-draws if not cached.

Note: All coordinates are internally handled as CWRUtil::Q5 which means that floating point values are rounded down to a fixed number of binary digits, for example:

Line line;
line.setStart(1.1f, 1.1f); // Will use (35/32, 35/32) = (1.09375f, 1.09375f)
int x, y;
line.getStart(&x, &y); // Will return (1, 1)

.

Inherits from: CanvasWidget, Widget, Drawable

Public Types

enumLINE_ENDING_STYLE { BUTT_CAP_ENDING, ROUND_CAP_ENDING, SQUARE_CAP_ENDING }
Values that represent line ending styles.

Public Functions

virtual booldrawCanvasWidget(const Rect & invalidatedArea) const
Draw canvas widget for the given invalidated area.
template <typename T >
void
getEnd(T & x, T & y) const
Gets the endpoint coordinates for the line.
LINE_ENDING_STYLEgetLineEndingStyle() const
Gets line ending style.
template <typename T >
T
getLineWidth() const
Gets line width.
template <typename T >
void
getLineWidth(T & width) const
Gets line width.
virtual RectgetMinimalRect() const
Gets minimal rectangle containing the shape drawn by this widget.
template <typename T >
void
getStart(T & x, T & y) const
Gets the starting point of the line as either integers or floats.
virtual voidinvalidateContent() const
Tell the framework that the contents of the Drawable needs to be redrawn.
Line()
voidsetCapPrecision(int precision)
Sets a precision of the arc at the ends of the Line.
voidsetEnd(CWRUtil::Q5 xQ5, CWRUtil::Q5 yQ5)
Sets the endpoint coordinates of the line.
template <typename T >
void
setEnd(T x, T y)
Sets the endpoint coordinates of the line.
template <typename T >
void
setLine(T startX, T startY, T endX, T endY)
Sets the starting point and ending point of the line.
voidsetLineEndingStyle(LINE_ENDING_STYLE lineEnding)
Sets line ending style.
voidsetLineWidth(CWRUtil::Q5 widthQ5)
Sets the width for this Line.
template <typename T >
void
setLineWidth(T width)
Sets the width for this Line.
voidsetStart(CWRUtil::Q5 xQ5, CWRUtil::Q5 yQ5)
Sets the starting point of the line.
template <typename T >
void
setStart(T x, T y)
Sets the starting point of the line.
voidupdateEnd(CWRUtil::Q5 xQ5, CWRUtil::Q5 yQ5)
Update the endpoint for this Line.
template <typename T >
void
updateEnd(T x, T y)
Update the endpoint for this Line.
voidupdateLengthAndAngle(CWRUtil::Q5 length, CWRUtil::Q5 angle)
Update the end point for this Line given the new length and angle in degrees.
voidupdateLineWidth(CWRUtil::Q5 widthQ5)
Update the width for this Line.
template <typename T >
void
updateLineWidth(T width)
Update the width for this Line and invalidates the minimal rectangle surrounding the line on screen.
voidupdateStart(CWRUtil::Q5 xQ5, CWRUtil::Q5 yQ5)
Update the start point for this Line.
template <typename T >
void
updateStart(T x, T y)
Update the start point for this Line.

Additional inherited members

Public Functions inherited from CanvasWidget

CanvasWidget()
virtual voiddraw(const Rect & invalidatedArea) const
Draws the given invalidated area.
virtual uint8_tgetAlpha() const
Gets the current alpha value of the widget.
const AbstractPainter *getPainter() const
Gets the current painter for the CanvasWidget.
virtual RectgetSolidRect() const
Gets the largest solid (non-transparent) rectangle.
virtual voidinvalidate() const
Invalidates the area covered by this CanvasWidget.
virtual voidsetAlpha(uint8_t newAlpha)
Sets the opacity (alpha value).
voidsetPainter(const AbstractPainter & painter)
Sets a painter for the CanvasWidget.

Protected Attributes inherited from CanvasWidget

uint8_talpha
The Alpha for this CanvasWidget.

Public Functions inherited from Widget

virtual voidgetLastChild(int16_t , int16_t , Drawable ** last)
Since a Widget is only one Drawable, Widget::getLastChild simply yields itself as result, but only if the Widget isVisible and isTouchable.

Public Functions inherited from Drawable

voidcenter()
Centers the Drawable inside its parent.
voidcenterX()
Center the Drawable horizontally inside its parent.
voidcenterY()
Center the Drawable vertically inside its parent.
virtual voidchildGeometryChanged()
This function can be called on parent nodes to signal that the size or position of one or more of its children has changed.
virtual voiddraw(const Rect & invalidatedArea) const =0
Draw this drawable.
Drawable()
Initializes a new instance of the Drawable class.
voiddrawToDynamicBitmap(BitmapId id)
Render the Drawable object into a dynamic bitmap.
voidexpand(int margin =0)
Expands the Drawable to have the same size as its parent with a given margin around the edge.
RectgetAbsoluteRect() const
Helper function for obtaining the rectangle this Drawable covers, expressed in absolute coordinates.
virtual Drawable *getFirstChild()
Function for obtaining the first child of this drawable if any.
int16_tgetHeight() const
Gets the height of this Drawable.
virtual voidgetLastChild(int16_t x, int16_t y, Drawable ** last) =0
Function for obtaining the the last child of this drawable that intersects with the specified point.
Drawable *getNextSibling()
Gets the next sibling node.
Drawable *getParent() const
Returns the parent node.
const Rect &getRect() const
Gets the rectangle this Drawable covers, in coordinates relative to its parent.
virtual RectgetSolidRect() const =0
Get (the largest possible) rectangle that is guaranteed to be solid (opaque).
virtual RectgetSolidRectAbsolute()
Helper function for obtaining the largest solid rect (as implemented by getSolidRect()) expressed in absolute coordinates.
virtual voidgetVisibleRect(Rect & rect) const
Function for finding the visible part of this drawable.
int16_tgetWidth() const
Gets the width of this Drawable.
int16_tgetX() const
Gets the x coordinate of this Drawable, relative to its parent.
int16_tgetY() const
Gets the y coordinate of this Drawable, relative to its parent.
virtual voidhandleClickEvent(const ClickEvent & event)
Defines the event handler interface for ClickEvents.
virtual voidhandleDragEvent(const DragEvent & event)
Defines the event handler interface for DragEvents.
virtual voidhandleGestureEvent(const GestureEvent & event)
Defines the event handler interface for GestureEvents.
virtual voidhandleTickEvent()
Called periodically by the framework if the Drawable instance has subscribed to timer ticks.
virtual voidinvalidate() const
Tell the framework that this entire Drawable needs to be redrawn.
virtual voidinvalidateRect(Rect & invalidatedArea) const
Request that a region of this drawable is redrawn.
boolisTouchable() const
Gets whether this Drawable receives touch events or not.
boolisVisible() const
Gets whether this Drawable is visible.
virtual voidmoveRelative(int16_t x, int16_t y)
Moves the drawable.
virtual voidmoveTo(int16_t x, int16_t y)
Moves the drawable.
virtual voidsetHeight(int16_t height)
Sets the height of this drawable.
voidsetPosition(const Drawable & drawable)
Sets the position of the Drawable to the same as the given Drawable.
voidsetPosition(int16_t x, int16_t y, int16_t width, int16_t height)
Sets the size and position of this Drawable, relative to its parent.
voidsetTouchable(bool touch)
Controls whether this Drawable receives touch events or not.
voidsetVisible(bool vis)
Controls whether this Drawable should be visible.
virtual voidsetWidth(int16_t width)
Sets the width of this drawable.
voidsetWidthHeight(const Bitmap & bitmap)
Sets the dimensions (width and height) of the Drawable without changing the x and y coordinates).
voidsetWidthHeight(const Drawable & drawable)
Sets the dimensions (width and height) of the Drawable without changing the x and y coordinates).
voidsetWidthHeight(const Rect & other)
Sets the dimensions (width and height) of the Drawable without changing the x and y coordinates).
voidsetWidthHeight(int16_t width, int16_t height)
Sets the dimensions (width and height) of the Drawable without changing the x and y coordinates).
virtual voidsetX(int16_t x)
Sets the x coordinate of this Drawable, relative to its parent.
voidsetXY(const Drawable & drawable)
Sets the x and y coordinates of this Drawable.
voidsetXY(int16_t x, int16_t y)
Sets the x and y coordinates of this Drawable, relative to its parent.
virtual voidsetY(int16_t y)
Sets the y coordinate of this Drawable, relative to its parent.
virtual voidtranslateRectToAbsolute(Rect & r) const
Helper function for converting a region of this Drawable to absolute coordinates.
virtual ~Drawable()
Finalizes an instance of the Drawable class.

Protected Attributes inherited from Drawable

Drawable *nextSibling
Pointer to the next Drawable.
Drawable *parent
Pointer to this drawable's parent.
Rectrect
The coordinates of this Drawable, relative to its parent.
booltouchable
True if this drawable should receive touch events.
boolvisible
True if this drawable should be drawn.

Public Types Documentation

LINE_ENDING_STYLE

Values that represent line ending styles.

BUTT_CAP_ENDINGThe line ending is cut 90 degrees at the end of the line.
ROUND_CAP_ENDINGThe line ending is rounded as a circle with center at the end of the line.
SQUARE_CAP_ENDINGThe line ending is cut 90 degrees, but extends half the width of the line.

Public Functions Documentation

drawCanvasWidget

virtual bool drawCanvasWidget(const Rect &invalidatedArea)

Draw canvas widget for the given invalidated area.

Similar to draw(), but might be invoked several times with increasingly smaller areas to due to memory constraints from the underlying CanvasWidgetRenderer.

Parameters:
invalidatedAreaThe invalidated area.
Returns:

true if the widget was drawn properly, false if not.

See also:

Reimplements: touchgfx::CanvasWidget::drawCanvasWidget

getEnd

void getEnd(T &x ,const
T &yconst
)const

Gets the endpoint coordinates for the line.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
xThe x coordinate rounded down to the precision of T.
yThe y coordinate rounded down to the precision of T.
See also:

getLineEndingStyle

LINE_ENDING_STYLE getLineEndingStyle()const

Gets line ending style.

Returns:

The line ending style.

See also:

getLineWidth

T getLineWidth()const

Gets line width.

Template Parameters:
TGeneric type parameter, either int or float.
Returns:

The line width rounded down to the precision of T.

See also:

getLineWidth

void getLineWidth(T &width)

Gets line width.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
widthThe line width rounded down to the precision of T.
See also:

getMinimalRect

virtual Rect getMinimalRect()const

Gets minimal rectangle containing the shape drawn by this widget.

Default implementation returns the size of the entire widget, but this function should be overwritten in subclasses and return the minimal rectangle containing the shape. See classes such as Circle for example implementations.

Returns:

The minimal rectangle containing the shape drawn.

Reimplements: touchgfx::CanvasWidget::getMinimalRect

getStart

void getStart(T &x ,const
T &yconst
)const

Gets the starting point of the line as either integers or floats.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
xThe x coordinate rounded down to the precision of T.
yThe y coordinate rounded down to the precision of T.
See also:

invalidateContent

virtual void invalidateContent()const

Tell the framework that the contents of the Drawable needs to be redrawn.

If the Drawable is invisible, nothing happens. Subclasses of Drawable are encouraged to implement this function and invalidate as little as possible, i.e. the smallest rectangle covering the visual element(s) drawn by the widget.

Reimplements: touchgfx::Drawable::invalidateContent

Line

setCapPrecision

void setCapPrecision(intprecision)

Sets a precision of the arc at the ends of the Line.

This only works for ROUND_CAP_ENDING. The precision is given in degrees where 18 is the default which results in a nice half circle with 10 line segments. 90 will draw "an arrow head", 180 will look exactly like a BUTT_CAP_ENDING.

Parameters:
precisionThe new ROUND_CAP_ENDING precision.
Note

The line is not invalidated. This is only used if line ending is set to ROUND_CAP_ENDING.

setEnd

Sets the endpoint coordinates of the line.

Parameters:
xQ5The x coordinate of the end point in Q5 format.
yQ5The y coordinate of the end point in Q5 format.
Note

The area containing the Line is not invalidated.

See also:

setEnd

void setEnd(Tx ,
Ty
)

Sets the endpoint coordinates of the line.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
xThe x coordinate of the end point.
yThe y coordinate of the end point.
Note

The area containing the Line is not invalidated.

See also:

setLine

void setLine(TstartX ,
TstartY ,
TendX ,
TendY
)

Sets the starting point and ending point of the line.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
startXThe x coordinate of the start point.
startYThe y coordinate of the start point.
endXThe x coordinate of the end point.
endYThe y coordinate of the end point.
Note

The area containing the Line is not invalidated.

See also:

setLineEndingStyle

Sets line ending style.

The same style is applied to both ends of the line.

Parameters:
lineEndingThe line ending style.
Note

The area containing the Line is not invalidated.

See also:

setLineWidth

Sets the width for this Line.

Parameters:
widthQ5The width of the line measured in pixels in Q5 format.
Note

The area containing the Line is not invalidated.

See also:

setLineWidth

void setLineWidth(Twidth)

Sets the width for this Line.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
widthThe width of the line measured in pixels.
Note

The area containing the Line is not invalidated.

See also:

setStart

Sets the starting point of the line.

Parameters:
xQ5The x coordinate of the start point in Q5 format.
yQ5The y coordinate of the start point in Q5 format.
Note

The area containing the Line is not invalidated.

See also:

setStart

void setStart(Tx ,
Ty
)

Sets the starting point of the line.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
xThe x coordinate of the start point.
yThe y coordinate of the start point.
Note

The area containing the Line is not invalidated.

See also:

updateEnd

Update the endpoint for this Line.

The rectangle that surrounds the line before and after will be invalidated.

Parameters:
xQ5The x coordinate of the end point in Q5 format.
yQ5The y coordinate of the end point in Q5 format.
Note

The area containing the Line is invalidated before and after the change.

See also:

updateEnd

void updateEnd(Tx ,
Ty
)

Update the endpoint for this Line.

The rectangle that surrounds the line before and after will be invalidated.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
xThe x coordinate of the end point.
yThe y coordinate of the end point.
Note

The area containing the Line is invalidated before and after the change.

See also:

updateLengthAndAngle

Update the end point for this Line given the new length and angle in degrees.

The rectangle that surrounds the line before and after will be invalidated. The starting coordinates will be fixed but the ending point will be updated. This is simply a different way to update the ending point.

Parameters:
lengthThe new length of the line in Q5 format.
angleThe new angle of the line in Q5 format.
Note

The area containing the Line is invalidated before and after the change. Angles are given in degrees, so a full circle is 360.

See also:

updateLineWidth

Update the width for this Line.

Update the width for this Line and invalidates the minimal rectangle surrounding the line on screen.

Parameters:
widthQ5The width of the line measured in pixels in Q5 format.
Note

The area containing the Line is invalidated before and after the change.

See also:

updateLineWidth

void updateLineWidth(Twidth)

Update the width for this Line and invalidates the minimal rectangle surrounding the line on screen.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
widthThe width of the line measured in pixels.
Note

The area containing the Line is invalidated before and after the change.

See also:

updateStart

Update the start point for this Line.

The rectangle that surrounds the line before and after will be invalidated.

Parameters:
xQ5The x coordinate of the start point in CWRUtil::Q5 format.
yQ5The y coordinate of the start point in CWRUtil::Q5 format.
Note

The area containing the Line is invalidated before and after the change.

See also:

updateStart

void updateStart(Tx ,
Ty
)

Update the start point for this Line.

The rectangle that surrounds the line before and after will be invalidated.

Template Parameters:
TGeneric type parameter, either int or float.
Parameters:
xThe x coordinate of the start point.
yThe y coordinate of the start point.
Note

The area containing the Line is invalidated before and after the change.

See also: