Skip to content

Commit

Permalink
feat(util): add return value for LCUIRect_ValidateArea()
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Mar 3, 2019
1 parent 23090eb commit 2fa6cf3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions include/LCUI/util/rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ LCUI_API void LCUIRect_GetCutArea(int box_w, int box_h,
&& Y < (rect)->y + (rect)->height)

/** 将矩形区域范围调整在容器有效范围内 */
LCUI_API void LCUIRect_ValidateArea(LCUI_Rect *rect, int box_w, int box_h);
LCUI_API LCUI_BOOL LCUIRect_ValidateArea(LCUI_Rect *rect, int box_w, int box_h);

LCUI_API void LCUIRectF_ValidateArea(LCUI_RectF *rect, float box_w, float box_h);
LCUI_API LCUI_BOOL LCUIRectF_ValidateArea(LCUI_RectF *rect, float box_w, float box_h);

LCUI_API void LCUIRect_ToRectF(const LCUI_Rect *rect,
LCUI_RectF *rectf, float scale);
Expand Down
18 changes: 16 additions & 2 deletions src/util/rect.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,58 +101,72 @@ void LCUIRect_GetCutArea(int box_w, int box_h,
}

/* FIXME: need new shorter name */
void LCUIRect_ValidateArea(LCUI_Rect *rect, int box_w, int box_h)
LCUI_BOOL LCUIRect_ValidateArea(LCUI_Rect *rect, int box_w, int box_h)
{
LCUI_BOOL overflow = FALSE;

if (rect->x < 0) {
overflow = TRUE;
rect->width += rect->x;
rect->x = 0;
}
if (rect->y < 0) {
overflow = TRUE;
rect->height += rect->y;
rect->y = 0;
}

if (rect->x + rect->width > box_w) {
overflow = TRUE;
if (rect->x < box_w) {
rect->width = box_w - rect->x;
} else {
rect->width = 0;
}
}
if (rect->y + rect->height > box_h) {
overflow = TRUE;
if (rect->y < box_h) {
rect->height = box_h - rect->y;
} else {
rect->height = 0;
}
}
return overflow;
}

void LCUIRectF_ValidateArea(LCUI_RectF *rect, float box_w, float box_h)
LCUI_BOOL LCUIRectF_ValidateArea(LCUI_RectF *rect, float box_w, float box_h)
{
LCUI_BOOL overflow = FALSE;

if (rect->x < 0) {
overflow = TRUE;
rect->width += rect->x;
rect->x = 0;
}
if (rect->y < 0) {
overflow = TRUE;
rect->height += rect->y;
rect->y = 0;
}

if (rect->x + rect->width - box_w > 0) {
overflow = TRUE;
if (rect->x - box_w < 0) {
rect->width = box_w - rect->x;
} else {
rect->width = 0;
}
}
if (rect->y + rect->height - box_h > 0) {
overflow = TRUE;
if (rect->y - box_h < 0) {
rect->height = box_h - rect->y;
} else {
rect->height = 0;
}
}
return overflow;
}

/* FIXME: need new shorter name */
Expand Down

0 comments on commit 2fa6cf3

Please sign in to comment.