REVIEWED: GuiValueBoxFloat() formatting...

This commit is contained in:
Ray 2024-05-07 12:33:21 +02:00
parent a717c69962
commit 680a50e83b

View File

@ -722,7 +722,7 @@ RAYGUIAPI int GuiComboBox(Rectangle bounds, const char *text, int *active);
RAYGUIAPI int GuiDropdownBox(Rectangle bounds, const char *text, int *active, bool editMode); // Dropdown Box control RAYGUIAPI int GuiDropdownBox(Rectangle bounds, const char *text, int *active, bool editMode); // Dropdown Box control
RAYGUIAPI int GuiSpinner(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode); // Spinner control RAYGUIAPI int GuiSpinner(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode); // Spinner control
RAYGUIAPI int GuiValueBox(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode); // Value Box control, updates input text with numbers RAYGUIAPI int GuiValueBox(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode); // Value Box control, updates input text with numbers
RAYGUIAPI int GuiValueBoxF(Rectangle bounds,const char* text, char *val_str, float *value, bool editMode); // Floating point Value box control, val_str is buffer where string representation is stored RAYGUIAPI int GuiValueBoxFloat(Rectangle bounds, const char* text, char *textValue, float *value, bool editMode); // Value box control for float values
RAYGUIAPI int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode); // Text Box control, updates input text RAYGUIAPI int GuiTextBox(Rectangle bounds, char *text, int textSize, bool editMode); // Text Box control, updates input text
RAYGUIAPI int GuiSlider(Rectangle bounds, const char *textLeft, const char *textRight, float *value, float minValue, float maxValue); // Slider control RAYGUIAPI int GuiSlider(Rectangle bounds, const char *textLeft, const char *textRight, float *value, float minValue, float maxValue); // Slider control
@ -1450,7 +1450,7 @@ static bool CheckCollisionPointRec(Vector2 point, Rectangle rec); // Check if
static const char *TextFormat(const char *text, ...); // Formatting of text with variables to 'embed' static const char *TextFormat(const char *text, ...); // Formatting of text with variables to 'embed'
static const char **TextSplit(const char *text, char delimiter, int *count); // Split text into multiple strings static const char **TextSplit(const char *text, char delimiter, int *count); // Split text into multiple strings
static int TextToInteger(const char *text); // Get integer value from text static int TextToInteger(const char *text); // Get integer value from text
static int TextToFloat(const char *text); // Get float value from text static float TextToFloat(const char *text); // Get float value from text
static int GetCodepointNext(const char *text, int *codepointSize); // Get next codepoint in a UTF-8 encoded text static int GetCodepointNext(const char *text, int *codepointSize); // Get next codepoint in a UTF-8 encoded text
static const char *CodepointToUTF8(int codepoint, int *byteSize); // Encode codepoint into UTF-8 text (char array size returned as parameter) static const char *CodepointToUTF8(int codepoint, int *byteSize); // Encode codepoint into UTF-8 text (char array size returned as parameter)
@ -2938,15 +2938,18 @@ int GuiValueBox(Rectangle bounds, const char *text, int *value, int minValue, in
// Floating point Value Box control, updates input val_str with numbers // Floating point Value Box control, updates input val_str with numbers
// NOTE: Requires static variables: frameCounter // NOTE: Requires static variables: frameCounter
int GuiValueBoxF(Rectangle bounds,const char* text, char *val_str, float *value, bool editMode) int GuiValueBoxFloat(Rectangle bounds, const char *text, char *textValue, float *value, bool editMode)
{ {
#if !defined(RAYGUI_VALUEBOX_MAX_CHARS) #if !defined(RAYGUI_VALUEBOX_MAX_CHARS)
#define RAYGUI_VALUEBOX_MAX_CHARS 32 #define RAYGUI_VALUEBOX_MAX_CHARS 32
#endif #endif
int result = 0; int result = 0;
GuiState state = guiState; GuiState state = guiState;
//char textValue[RAYGUI_VALUEBOX_MAX_CHARS + 1] = "\0";
//sprintf(textValue, "%2.2f", *value);
Rectangle textBounds = {0}; Rectangle textBounds = {0};
if (text != NULL) if (text != NULL)
{ {
@ -2954,8 +2957,7 @@ int GuiValueBoxF(Rectangle bounds,const char* text, char *val_str, float *value,
textBounds.height = (float)GuiGetStyle(DEFAULT, TEXT_SIZE); textBounds.height = (float)GuiGetStyle(DEFAULT, TEXT_SIZE);
textBounds.x = bounds.x + bounds.width + GuiGetStyle(VALUEBOX, TEXT_PADDING); textBounds.x = bounds.x + bounds.width + GuiGetStyle(VALUEBOX, TEXT_PADDING);
textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2; textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2;
if (GuiGetStyle(VALUEBOX, TEXT_ALIGNMENT) == TEXT_ALIGN_LEFT) if (GuiGetStyle(VALUEBOX, TEXT_ALIGNMENT) == TEXT_ALIGN_LEFT) textBounds.x = bounds.x - textBounds.width - GuiGetStyle(VALUEBOX, TEXT_PADDING);
textBounds.x = bounds.x - textBounds.width - GuiGetStyle(VALUEBOX, TEXT_PADDING);
} }
// Update control // Update control
@ -2970,23 +2972,22 @@ int GuiValueBoxF(Rectangle bounds,const char* text, char *val_str, float *value,
{ {
state = STATE_PRESSED; state = STATE_PRESSED;
int keyCount = (int)strlen(val_str); int keyCount = (int)strlen(textValue);
// Only allow keys in range [48..57] // Only allow keys in range [48..57]
if (keyCount < RAYGUI_VALUEBOX_MAX_CHARS) if (keyCount < RAYGUI_VALUEBOX_MAX_CHARS)
{ {
if (GetTextWidth(val_str) < bounds.width) if (GetTextWidth(textValue) < bounds.width)
{ {
int key = GetCharPressed(); int key = GetCharPressed();
if ( if (((key >= 48) && (key <= 57)) ||
((key >= 48) && (key <= 57)) (key == '.') ||
||key=='.' ((keyCount == 0) && (key == '+')) || // NOTE: Sign can only be in first position
||(!keyCount && key=='+') //sign can only be in first position ((keyCount == 0) && (key == '-')))
||(!keyCount && key=='-')
)
{ {
val_str[keyCount] = (char) key; textValue[keyCount] = (char)key;
keyCount++; keyCount++;
valueHasChanged = true; valueHasChanged = true;
} }
} }
@ -2998,24 +2999,21 @@ int GuiValueBoxF(Rectangle bounds,const char* text, char *val_str, float *value,
if (keyCount > 0) if (keyCount > 0)
{ {
keyCount--; keyCount--;
val_str[keyCount] = '\0'; textValue[keyCount] = '\0';
valueHasChanged = true; valueHasChanged = true;
} }
} }
if (valueHasChanged) if (valueHasChanged) *value = TextToFloat(textValue);
*value = TextToFloat(val_str);
if (IsKeyPressed(KEY_ENTER) || if (IsKeyPressed(KEY_ENTER) || (!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) result = 1;
(!CheckCollisionPointRec(mousePoint, bounds) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))) }
result = 1; else
} else
{ {
if (CheckCollisionPointRec(mousePoint, bounds)) if (CheckCollisionPointRec(mousePoint, bounds))
{ {
state = STATE_FOCUSED; state = STATE_FOCUSED;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) result = 1;
result = 1;
} }
} }
} }
@ -3027,25 +3025,23 @@ int GuiValueBoxF(Rectangle bounds,const char* text, char *val_str, float *value,
if (state == STATE_PRESSED) baseColor = GetColor(GuiGetStyle(VALUEBOX, BASE_COLOR_PRESSED)); if (state == STATE_PRESSED) baseColor = GetColor(GuiGetStyle(VALUEBOX, BASE_COLOR_PRESSED));
else if (state == STATE_DISABLED) baseColor = GetColor(GuiGetStyle(VALUEBOX, BASE_COLOR_DISABLED)); else if (state == STATE_DISABLED) baseColor = GetColor(GuiGetStyle(VALUEBOX, BASE_COLOR_DISABLED));
GuiDrawRectangle(bounds, GuiGetStyle(VALUEBOX, BORDER_WIDTH), GetColor(GuiGetStyle(VALUEBOX, BORDER + (state * 3))), GuiDrawRectangle(bounds, GuiGetStyle(VALUEBOX, BORDER_WIDTH), GetColor(GuiGetStyle(VALUEBOX, BORDER + (state*3))), baseColor);
baseColor); GuiDrawText(textValue, GetTextBounds(VALUEBOX, bounds), TEXT_ALIGN_CENTER, GetColor(GuiGetStyle(VALUEBOX, TEXT + (state*3))));
GuiDrawText(val_str, GetTextBounds(VALUEBOX, bounds), TEXT_ALIGN_CENTER,
GetColor(GuiGetStyle(VALUEBOX, TEXT + (state * 3))));
// Draw cursor // Draw cursor
if (editMode) if (editMode)
{ {
// NOTE: ValueBox internal text is always centered // NOTE: ValueBox internal text is always centered
Rectangle cursor = {bounds.x + GetTextWidth(val_str) / 2 + bounds.width / 2 + 1, Rectangle cursor = {bounds.x + GetTextWidth(textValue)/2 + bounds.width/2 + 1,
bounds.y + 2 * GuiGetStyle(VALUEBOX, BORDER_WIDTH), 4, bounds.y + 2*GuiGetStyle(VALUEBOX, BORDER_WIDTH), 4,
bounds.height - 4 * GuiGetStyle(VALUEBOX, BORDER_WIDTH)}; bounds.height - 4*GuiGetStyle(VALUEBOX, BORDER_WIDTH)};
GuiDrawRectangle(cursor, 0, BLANK, GetColor(GuiGetStyle(VALUEBOX, BORDER_COLOR_PRESSED))); GuiDrawRectangle(cursor, 0, BLANK, GetColor(GuiGetStyle(VALUEBOX, BORDER_COLOR_PRESSED)));
} }
// Draw text label if provided // Draw text label if provided
GuiDrawText(text, textBounds, GuiDrawText(text, textBounds,
(GuiGetStyle(VALUEBOX, TEXT_ALIGNMENT) == TEXT_ALIGN_RIGHT) ? TEXT_ALIGN_LEFT : TEXT_ALIGN_RIGHT, (GuiGetStyle(VALUEBOX, TEXT_ALIGNMENT) == TEXT_ALIGN_RIGHT)? TEXT_ALIGN_LEFT : TEXT_ALIGN_RIGHT,
GetColor(GuiGetStyle(LABEL, TEXT + (state * 3)))); GetColor(GuiGetStyle(LABEL, TEXT + (state*3))));
//-------------------------------------------------------------------- //--------------------------------------------------------------------
return result; return result;
@ -5572,26 +5568,35 @@ static int TextToInteger(const char *text)
return value*sign; return value*sign;
} }
// Get float value from text
// NOTE: This function replaces atof() [stdlib.h]
// WARNING: Only '.' character is understood as decimal point
static float TextToFloat(const char *text) static float TextToFloat(const char *text)
{ {
float value = 0.0f; float value = 0.0f;
float sign = 1.0f; float sign = 1.0f;
if ((text[0] == '+') || (text[0] == '-')) if ((text[0] == '+') || (text[0] == '-'))
{ {
if (text[0] == '-') sign = -1; if (text[0] == '-') sign = -1.0f;
text++; text++;
} }
int i = 0;
for (; ((text[i] >= '0') && (text[i] <= '9')); ++i) value = value*10.0f + (float)(text[i] - '0'); int i = 0;
if (text[i++] != '.') return value*sign; for (; ((text[i] >= '0') && (text[i] <= '9')); i++) value = value*10.0f + (float)(text[i] - '0');
float divisor = 10.0f;
for (; ((text[i] >= '0') && (text[i] <= '9')); ++i) if (text[i++] != '.') value *= sign;
{ else
value += ((float)(text[i] - '0'))/divisor; {
divisor = divisor*10.0f; float divisor = 10.0f;
} for (; ((text[i] >= '0') && (text[i] <= '9')); i++)
return value; {
value += ((float)(text[i] - '0'))/divisor;
divisor = divisor*10.0f;
}
}
return value;
} }
// Encode codepoint into UTF-8 text (char array size returned as parameter) // Encode codepoint into UTF-8 text (char array size returned as parameter)