Webcam Tracker  V1.0
Using openCV and an Arduino Uno
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members

A button that can be clicked. Member of GUI classes Graphic group. More...

#include <Button.h>

Classes

struct  STYLE
 Button style information. More...
 

Public Member Functions

 Button ()
 Default constructor.
 
 Button (cv::Point startPoint, int height, cv::String text)
 Constructor. More...
 
void draw (cv::Mat *img)
 Draws the button to the image matrix. More...
 
void setStartPoint (cv::Point startPoint)
 Sets the topleft corner of the button. More...
 
void checkClick (int x, int y)
 Checks if a mouse-click event happened inside the button. More...
 
int getWidth ()
 Gets the width of the button. More...
 
bool getClick ()
 Gets the button state. More...
 

Private Member Functions

void roundedRectangle (cv::Mat *img, cv::Point topLeft, cv::Point bottomRight, const cv::Scalar lineColor, const cv::Scalar fillColor, const int thickness, const int cornerRadius)
 creates a rectangle with rounded corners More...
 

Private Attributes

cv::Point startPoint
 X and Y coordinate of the topleft corner in pixels.
 
cv::Size size
 Height and Width of the button in pixels.
 
cv::String text
 Button Text.
 
bool clicked
 Button state.
 
cv::Mat * img
 Pointer to the img where the button should be drawn.
 
struct Button::STYLE style
 button style information
 

Detailed Description

A button that can be clicked. Member of GUI classes Graphic group.

Constructor & Destructor Documentation

◆ Button()

Button::Button ( cv::Point  startPoint,
int  height,
cv::String  text 
)

Constructor.

Parameters
startPointX and Y coordinate of the topleft corner in pixels
heightHeight of the button in pixels
textButton text
21  {
22  int baseline = 0;
23  this->startPoint = startPoint;
24  this->size = Size(getTextSize(text, style.fontFace, style.fontScale, 1, &baseline).width + 4, height);
25  this->text = text;
26  this->clicked = false;
27 };
struct Button::STYLE style
button style information
cv::String text
Button Text.
Definition: Button.h:23
bool clicked
Button state.
Definition: Button.h:24
cv::Size size
Height and Width of the button in pixels.
Definition: Button.h:22
cv::Point startPoint
X and Y coordinate of the topleft corner in pixels.
Definition: Button.h:21
int fontFace
Font type
Definition: Button.h:30
float fontScale
Font size.
Definition: Button.h:31

Member Function Documentation

◆ roundedRectangle()

void Button::roundedRectangle ( cv::Mat *  img,
cv::Point  topLeft,
cv::Point  bottomRight,
const cv::Scalar  lineColor,
const cv::Scalar  fillColor,
const int  thickness,
const int  cornerRadius 
)
private

creates a rectangle with rounded corners

First draws the four edges, than draws the corners and last fills the rectangle.

Parameters
imgPointer to the img where the rounded rectangle should be drawn
topLeftX and Y coordinate of the topleft corner in pixels
bottomRightX and Y coordinate of the bottomright corner in pixels
lineColorcolor of the button edge(line)
fillColorcolor of the button
thicknessline thickness
cornerRadiussize of the rounded corner radius in pixels
30 {
31  /* corners:
32  * p1 - p2
33  * | |
34  * p4 - p3
35  */
36  Point p1 = topLeft;
37  Point p2 = Point(bottomRight.x, topLeft.y);
38  Point p3 = bottomRight;
39  Point p4 = Point(topLeft.x, bottomRight.y);
40 
41  // draw straight lines
42  line(*img, Point(p1.x + cornerRadius, p1.y), Point(p2.x - cornerRadius, p2.y), lineColor, thickness);
43  line(*img, Point(p2.x, p2.y + cornerRadius), Point(p3.x, p3.y - cornerRadius), lineColor, thickness);
44  line(*img, Point(p4.x + cornerRadius, p4.y), Point(p3.x - cornerRadius, p3.y), lineColor, thickness);
45  line(*img, Point(p1.x, p1.y + cornerRadius), Point(p4.x, p4.y - cornerRadius), lineColor, thickness);
46 
47  // draw arcs
48  ellipse(*img, p1 + Point(cornerRadius, cornerRadius), Size(cornerRadius, cornerRadius), 180.0, 0, 90, lineColor, thickness);
49  ellipse(*img, p2 + Point(-cornerRadius, cornerRadius), Size(cornerRadius, cornerRadius), 270.0, 0, 90, lineColor, thickness);
50  ellipse(*img, p3 + Point(-cornerRadius, -cornerRadius), Size(cornerRadius, cornerRadius), 0.0, 0, 90, lineColor, thickness);
51  ellipse(*img, p4 + Point(cornerRadius, -cornerRadius), Size(cornerRadius, cornerRadius), 90.0, 0, 90, lineColor, thickness);
52 
53  //Fill the rectangle
54  Point fillFrom(topLeft.x + 10, topLeft.y + 10);
55  floodFill(*img, fillFrom, fillColor);
56 }
cv::Mat * img
Pointer to the img where the button should be drawn.
Definition: Button.h:25

◆ draw()

void Button::draw ( cv::Mat *  img)

Draws the button to the image matrix.

the color of the line is based on the button state. The text is always centered in the middle of the button.

Parameters
imgPointer to the image where the button should be drawn
58  {
59  int baseLine = 0;
60  Scalar lColor;
61  if (this->clicked)
62  lColor = style.lineColorClicked;
63  else
64  lColor = style.lineColor;
65 
66  Size textSize = getTextSize(text, style.fontFace, style.fontScale, 1, &baseLine);
67  roundedRectangle(img, startPoint, Point(startPoint.x + size.width, startPoint.y + size.height), lColor, style.fillColor, 1, 3);
68  Point textOrg((startPoint.x + size.width / 2 - textSize.width / 2), (startPoint.y + size.height / 2 + textSize.height / 2));
69  putText(*img, text, textOrg, style.fontFace, style.fontScale, style.fontColor, 1, 8);
70 
71 }
void roundedRectangle(cv::Mat *img, cv::Point topLeft, cv::Point bottomRight, const cv::Scalar lineColor, const cv::Scalar fillColor, const int thickness, const int cornerRadius)
creates a rectangle with rounded corners
Definition: Button.cpp:29
cv::Scalar lineColor
Line color (black)
Definition: Button.h:33
cv::Scalar fillColor
Fill color (grey)
Definition: Button.h:35
cv::Scalar fontColor
Font color (white)
Definition: Button.h:32
cv::Scalar lineColorClicked
Line color when clicked (green)
Definition: Button.h:34

◆ setStartPoint()

void Button::setStartPoint ( cv::Point  startPoint)

Sets the topleft corner of the button.

is used to shift the startpoints of the buttons when creating the GUI.

Parameters
startPointX and Y coordinate of the topleft corner in pixels
73  {
74  this->startPoint = startPoint;
75 }

◆ checkClick()

void Button::checkClick ( int  x,
int  y 
)

Checks if a mouse-click event happened inside the button.

Parameters
xX-part of the mouse event Coordinate
yY-part of the mouse event Coordinate
77  {
78  //Check if x and y lie within the button
79  if ((x >= startPoint.x) && (x <= startPoint.x + size.width) && (y >= startPoint.y) && (y <= startPoint.y + size.height)) {
80  clicked = !clicked;
81  }
82 }

◆ getWidth()

int Button::getWidth ( )

Gets the width of the button.

Returns
Width in pixels
84  {
85  return size.width;
86 }

◆ getClick()

bool Button::getClick ( )

Gets the button state.

Returns
button state(Boolean)
88  {
89  return clicked;
90 }