Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limited counter #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
518 changes: 518 additions & 0 deletions Makefile

Large diffs are not rendered by default.

42 changes: 25 additions & 17 deletions cvui.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void printf(cv::Mat& theWhere, int theX, int theY, const char *theFmt, ...);
\param theFormat how the value of the counter should be presented, as it was printed by `stdio's printf()`. E.g. `"%d"` means the value will be displayed as an integer, `"%0d"` integer with one leading zero, etc.
\return integer that corresponds to the current value of the counter.
*/
int counter(cv::Mat& theWhere, int theX, int theY, int *theValue, int theStep = 1, const char *theFormat = "%d");
int counter(cv::Mat& theWhere, int theX, int theY, int *theValue, int theMin = 0, int theMax = 100, int theStep = 1, const char *theFormat = "%d");

/**
Display a counter for float values that the user can increase/descrease
Expand All @@ -435,8 +435,7 @@ int counter(cv::Mat& theWhere, int theX, int theY, int *theValue, int theStep =
\param theFormat how the value of the counter should be presented, as it was printed by `stdio's printf()`. E.g. `"%f"` means the value will be displayed as a regular float, `"%.2f"` float with two digits after the point, etc.
\return a float that corresponds to the current value of the counter.
*/
double counter(cv::Mat& theWhere, int theX, int theY, double *theValue, double theStep = 0.5, const char *theFormat = "%.2f");

double counter(cv::Mat& theWhere, int theX, int theY, double *theValue, double theMin = 0.0, double theMax = 100.0, double theStep = 0.5, const char *theFormat = "%.2f");
/**
Display a trackbar for numeric values that the user can increase/decrease
by clicking and/or dragging the marker right or left. This component uses templates
Expand Down Expand Up @@ -912,7 +911,7 @@ void printf(const char *theFmt, ...);
\sa endRow()
\sa endColumn()
*/
int counter(int *theValue, int theStep = 1, const char *theFormat = "%d");
int counter(int *theValue, int theMin = 0, int theMax = 100, int theStep = 1, const char *theFormat = "%d");

/**
Display a counter for float values that the user can increase/descrease
Expand All @@ -931,7 +930,7 @@ int counter(int *theValue, int theStep = 1, const char *theFormat = "%d");
\sa endRow()
\sa endColumn()
*/
double counter(double *theValue, double theStep = 0.5, const char *theFormat = "%.2f");
double counter(double *theValue, double theMin = 0.0, double theMax = 100.0, double theStep = 0.5, const char *theFormat = "%.2f");

/**
Display a trackbar for numeric values that the user can increase/decrease
Expand Down Expand Up @@ -1227,8 +1226,8 @@ namespace internal
void image(cvui_block_t& theBlock, int theX, int theY, cv::Mat& theImage);
bool checkbox(cvui_block_t& theBlock, int theX, int theY, const cv::String& theLabel, bool *theState, unsigned int theColor);
void text(cvui_block_t& theBlock, int theX, int theY, const cv::String& theText, double theFontScale, unsigned int theColor, bool theUpdateLayout);
int counter(cvui_block_t& theBlock, int theX, int theY, int *theValue, int theStep, const char *theFormat);
double counter(cvui_block_t& theBlock, int theX, int theY, double *theValue, double theStep, const char *theFormat);
int counter(cvui_block_t& theBlock, int theX, int theY, int *theValue, int theMin, int theMax, int theStep, const char *theFormat);
double counter(cvui_block_t& theBlock, int theX, int theY, double *theValue, double theMin, double theMax, double theStep, const char *theFormat);
void window(cvui_block_t& theBlock, int theX, int theY, int theWidth, int theHeight, const cv::String& theTitle);
void rect(cvui_block_t& theBlock, int theX, int theY, int theWidth, int theHeight, unsigned int theBorderColor, unsigned int theFillingColor);
void sparkline(cvui_block_t& theBlock, std::vector<double>& theValues, int theX, int theY, int theWidth, int theHeight, unsigned int theColor);
Expand Down Expand Up @@ -1754,18 +1753,22 @@ namespace internal
}
}

int counter(cvui_block_t& theBlock, int theX, int theY, int *theValue, int theStep, const char *theFormat) {
int counter(cvui_block_t& theBlock, int theX, int theY, int *theValue, int theMin, int theMax, int theStep, const char *theFormat) {
cv::Rect aContentArea(theX + 22, theY, 48, 22);

if (internal::button(theBlock, theX, theY, 22, 22, "-", false)) {
if((*theValue - theStep) >= theMin){
*theValue -= theStep;
}
}

sprintf_s(internal::gBuffer, theFormat, *theValue);
render::counter(theBlock, aContentArea, internal::gBuffer);

if (internal::button(theBlock, aContentArea.x + aContentArea.width, theY, 22, 22, "+", false)) {
if((*theValue + theStep) <= theMax){
*theValue += theStep;
}
}

// Update the layout flow
Expand All @@ -1775,18 +1778,23 @@ namespace internal
return *theValue;
}

double counter(cvui_block_t& theBlock, int theX, int theY, double *theValue, double theStep, const char *theFormat) {
double counter(cvui_block_t& theBlock, int theX, int theY, double *theValue, double theMin, double theMax, double theStep, const char *theFormat) {

cv::Rect aContentArea(theX + 22, theY, 48, 22);

if (internal::button(theBlock, theX, theY, 22, 22, "-", false)) {
if((*theValue - theStep) >= theMin){
*theValue -= theStep;
}
}

sprintf_s(internal::gBuffer, theFormat, *theValue);
render::counter(theBlock, aContentArea, internal::gBuffer);

if (internal::button(theBlock, aContentArea.x + aContentArea.width, theY, 22, 22, "+", false)) {
if((*theValue + theStep) <= theMax){
*theValue += theStep;
}
}

// Update the layout flow
Expand Down Expand Up @@ -2292,14 +2300,14 @@ void printf(cv::Mat& theWhere, int theX, int theY, const char *theFmt, ...) {
internal::text(internal::gScreen, theX, theY, internal::gBuffer, 0.4, 0xCECECE, true);
}

int counter(cv::Mat& theWhere, int theX, int theY, int *theValue, int theStep, const char *theFormat) {
int counter(cv::Mat& theWhere, int theX, int theY, int *theValue, int theMin, int theMax, int theStep, const char *theFormat) {
internal::gScreen.where = theWhere;
return internal::counter(internal::gScreen, theX, theY, theValue, theStep, theFormat);
return internal::counter(internal::gScreen, theX, theY, theValue, theMin, theMax, theStep, theFormat);
}

double counter(cv::Mat& theWhere, int theX, int theY, double *theValue, double theStep, const char *theFormat) {
double counter(cv::Mat& theWhere, int theX, int theY, double *theValue, double theMin, double theMax, double theStep, const char *theFormat) {
internal::gScreen.where = theWhere;
return internal::counter(internal::gScreen, theX, theY, theValue, theStep, theFormat);
return internal::counter(internal::gScreen, theX, theY, theValue, theMin, theMax, theStep, theFormat);
}

void window(cv::Mat& theWhere, int theX, int theY, int theWidth, int theHeight, const cv::String& theTitle) {
Expand Down Expand Up @@ -2406,14 +2414,14 @@ void printf(const char *theFmt, ...) {
internal::text(aBlock, aBlock.anchor.x, aBlock.anchor.y, internal::gBuffer, 0.4, 0xCECECE, true);
}

int counter(int *theValue, int theStep, const char *theFormat) {
int counter(int *theValue, int theStep, int theMin, int theMax, const char *theFormat) {
cvui_block_t& aBlock = internal::topBlock();
return internal::counter(aBlock, aBlock.anchor.x, aBlock.anchor.y, theValue, theStep, theFormat);
return internal::counter(aBlock, aBlock.anchor.x, aBlock.anchor.y, theValue, theMin, theMax, theStep, theFormat);
}

double counter(double *theValue, double theStep, const char *theFormat) {
double counter(double *theValue, double theStep, double theMin, double theMax, const char *theFormat) {
cvui_block_t& aBlock = internal::topBlock();
return internal::counter(aBlock, aBlock.anchor.x, aBlock.anchor.y, theValue, theStep, theFormat);
return internal::counter(aBlock, aBlock.anchor.x, aBlock.anchor.y, theValue, theMin, theMax, theStep, theFormat);
}

void window(int theWidth, int theHeight, const cv::String& theTitle) {
Expand Down
28 changes: 17 additions & 11 deletions cvui.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,18 @@ def text(self, theBlock, theX, theY, theText, theFontScale, theColor, theUpdateL
aTextSize.height += 1
self.updateLayoutFlow(theBlock, aTextSize)

def counter(self, theBlock, theX, theY, theValue, theStep, theFormat):
def counter(self, theBlock, theX, theY, theValue, theMin, theMax, theStep, theFormat):
aContentArea = Rect(theX + 22, theY, 48, 22)

if self.buttonWH(theBlock, theX, theY, 22, 22, '-', False):
if((theValue[0] - theStep) >= theMin):
theValue[0] -= theStep

aText = theFormat % theValue[0]
self._render.counter(theBlock, aContentArea, aText)

if self.buttonWH(theBlock, aContentArea.x + aContentArea.width, theY, 22, 22, "+", False):
if((theValue[0] + theStep) <= theMax):
theValue[0] += theStep

# Update the layout flow
Expand Down Expand Up @@ -727,7 +729,7 @@ def text(self, theBlock, theText, thePos, theFontScale, theColor):
aPosition = (int(thePos.x), int(thePos.y))
cv2.putText(theBlock.where, theText, aPosition, cv2.FONT_HERSHEY_SIMPLEX, theFontScale, self._internal.hexToScalar(theColor), 1, cv2.LINE_AA)

def counter(self, theBlock, theShape, theValue):
def counter(self, theBlock, theShape, theValue, theMin, theMax):
self.rectangle(theBlock.where, theShape, (0x29, 0x29, 0x29), CVUI_FILLED) # fill
self.rectangle(theBlock.where, theShape, (0x45, 0x45, 0x45)) # border

Expand Down Expand Up @@ -1551,7 +1553,7 @@ def printf(theWhere, theX, theY, theFmt):
"""
print('This is wrapper function to help code autocompletion.')

def counter(theWhere, theX, theY, theValue, theStep = 1, theFormat = '%d'):
def counter(theWhere, theX, theY, theValue, theMin = 0, theMax = 100, theStep = 1, theFormat = '%d'):
"""
Display a counter for integer values that the user can increase/descrease
by clicking the up and down arrows.
Expand Down Expand Up @@ -2223,7 +2225,7 @@ def printf(theFmt):
"""
print('This is wrapper function to help code autocompletion.')

def counter(theValue, theStep = 1, theFormat = '%d'):
def counter(theValue, theMin = 0, theMax = 100, theStep = 1, theFormat = '%d'):
"""
Display a counter for integer values that the user can increase/descrease
by clicking the up and down arrows.
Expand Down Expand Up @@ -2503,30 +2505,34 @@ def printf(*theArgs):

def counter(*theArgs):
if isinstance(theArgs[0], np.ndarray):
# Signature: counter(theWhere, theX, theY, theValue, theStep = 1, theFormat = "")
# Signature: counter(theWhere, theX, theY, theValue, theMin = 0, theMax = 100, theStep = 1, theFormat = "")
aWhere = theArgs[0]
aX = theArgs[1]
aY = theArgs[2]
aValue = theArgs[3]
aStep = theArgs[4] if len(theArgs) >= 5 else 1
aFormat = theArgs[5] if len(theArgs) >= 6 else ''
aMin = the[4] if len(theArgs) >= 5 else 0
aMax = the[5] if len(theArgs) >= 6 else 100
aStep = theArgs[6] if len(theArgs) >= 7 else 1
aFormat = theArgs[7] if len(theArgs) >= 8 else ''

__internal.screen.where = aWhere
aBlock = __internal.screen
else:
# Signature: counter(theValue, theStep = 1, theFormat = "%d")
# Signature: counter(theValue, theMin, theMax, theStep = 1, theFormat = "%d")
aBlock = __internal.topBlock()
aX = aBlock.anchor.x
aY = aBlock.anchor.y
aValue = theArgs[0]
aStep = theArgs[1] if len(theArgs) >= 2 else 1
aFormat = theArgs[2] if len(theArgs) >= 3 else ''
aMin = theArgs[1] if len(theArgs) >= 2 else 0
aMax = theArgs[2] if len(theArgs) > 3 else 100
aStep = theArgs[3] if len(theArgs) >= 4 else 1
aFormat = theArgs[4] if len(theArgs) >= 5 else ''

if not aFormat:
aIsInt = isinstance(aValue[0], int) == True and isinstance(aStep, int)
aFormat = '%d' if aIsInt else '%.1f'

return __internal.counter(aBlock, aX, aY, aValue, aStep, aFormat)
return __internal.counter(aBlock, aX, aY, aValue, aMin, aMax, aStep, aFormat)

def checkbox(*theArgs):
if isinstance(theArgs[0], np.ndarray):
Expand Down
6 changes: 5 additions & 1 deletion docs/components/counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ int counter (
int theX,
int theY,
int *theValue,
int theMin,
int theMax,
int theStep = 1,
const char *theFormat = "%d"
)
Expand All @@ -24,6 +26,8 @@ double counter (
int theX,
int theY,
double *theValue,
double theMin,
double theMax,
double theStep = 0.5,
const char *theFormat = "%.2f"
)
Expand All @@ -37,7 +41,7 @@ Below is an example showing a counter. The result on the screen is shown in Figu

```cpp
int count = 2;
cvui::counter(frame, 90, 50, &count);
cvui::counter(frame, 90, 50, &count, 0, 10);
```

![Counter]({{ site.url }}/img/counter.png)
Expand Down
Loading