-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#ifndef VOYAGE_INPUT_H_ | ||
#define VOYAGE_INPUT_H_ | ||
|
||
#include "colors.h" | ||
|
||
typedef struct InputOptions { | ||
Color bgColor; | ||
Color fgColor; | ||
Color border; | ||
Color accent; | ||
Font font; | ||
int fontSize; | ||
int maxChars; | ||
int textSpacing; | ||
int hOffset; | ||
int vOffset; | ||
} InputOptions; | ||
|
||
typedef struct Input { | ||
Vector2 pos; | ||
int width; | ||
char *text; | ||
InputOptions options; | ||
void (*onEnter)(struct Input); | ||
} Input; | ||
|
||
#define InputDefaultOptions ((InputOptions) \ | ||
{.bgColor=Voyage_LightGrey, .fgColor=Voyage_White, \ | ||
.border=Voyage_DarkGrey, .accent=Voyage_White, \ | ||
.font=GetFontDefault(), .fontSize=24, .maxChars=50, \ | ||
.textSpacing=3, .hOffset=10, .vOffset=10}) | ||
|
||
void InputDefaultOnEnter(Input input) { | ||
printf("Log: Input(%s) entered\n", input.text); | ||
} | ||
|
||
Input Input_Init(Vector2, int, char *, void (*)(Input)); | ||
void Input_SetOptions(Input *, InputOptions); | ||
void Input_Resize(Input *, int); | ||
void Input_ResizeReposition(Input *, Vector2, int); | ||
Vector2 Input_Size(Input); | ||
void Input_Draw(Input); | ||
|
||
Input Input_Init(Vector2 pos, int width, char *initialText, void (*onEnter)(Input)) { | ||
char *text = (char *)malloc(sizeof(char)*(InputDefaultOptions.maxChars+1)); | ||
memcpy(text, initialText, InputDefaultOptions.maxChars); | ||
text[InputDefaultOptions.maxChars] = 0; | ||
return (Input){.pos = pos, .width=width, .text=text, .options=InputDefaultOptions, | ||
.onEnter = onEnter? onEnter: &InputDefaultOnEnter}; | ||
} | ||
|
||
void Input_SetOptions(Input *input, InputOptions options) { | ||
input->options = options; | ||
} | ||
|
||
void Input_Resize(Input *input, int width) { | ||
input->width = width; | ||
} | ||
|
||
void Input_ResizeReposition(Input *input, Vector2 pos, int width) { | ||
input->pos = pos; | ||
input->width = width; | ||
} | ||
|
||
Vector2 Input_Size(Input input) { | ||
Vector2 textSize = MeasureTextEx(input.options.font, input.text, | ||
input.options.fontSize, input.options.textSpacing); | ||
return (Vector2){input.width, textSize.y+2*input.options.vOffset}; | ||
} | ||
|
||
void Input_Draw(Input input) { | ||
Vector2 textSize = MeasureTextEx(input.options.font, input.text, | ||
input.options.fontSize, input.options.textSpacing); | ||
/* Vector2 textPos = (Vector2){input.pos.x+input.options.hOffset, */ | ||
/* input.pos.y+input.options.vOffset}; */ | ||
Rectangle container = (Rectangle){input.pos.x, input.pos.y, | ||
input.width, textSize.y+2*input.options.vOffset}; | ||
Vector2 mousePoint = GetMousePosition(); | ||
int state = 0, action = 0; | ||
if (CheckCollisionPointRec(mousePoint, container)) { | ||
state = (IsMouseButtonDown(MOUSE_BUTTON_LEFT))? 2: 1; // PRESSED: HOVER | ||
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) action = 1; // CLICK | ||
} | ||
else state = 0; // NORMAL | ||
Color bgColor = input.options.bgColor, fgColor = input.options.fgColor; | ||
Color border = input.options.border; | ||
if (state) { | ||
bgColor = input.options.fgColor; | ||
fgColor = input.options.bgColor; | ||
border = input.options.bgColor; | ||
} | ||
DrawRectangleRec(container, bgColor); | ||
DrawRectangleLinesEx(container, 2, border); | ||
DrawTextEx(input.options.font, input.text, textPos, input.options.fontSize, | ||
input.options.textSpacing, fgColor); | ||
if (action) input.onEnter(input); | ||
} | ||
|
||
#endif // VOYAGE_INPUT_H_ |