Skip to content

Commit

Permalink
#8 started working on text input
Browse files Browse the repository at this point in the history
  • Loading branch information
hyouteki committed Jun 6, 2024
1 parent 5b22fac commit 1b528f3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 12 deletions.
4 changes: 2 additions & 2 deletions demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

char *lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

void buttonOnClick() {
printf("Menu button clicked\n");
void buttonOnClick(Button button) {
printf("%s clicked\n", button.text);
}

int main() {
Expand Down
1 change: 1 addition & 0 deletions export.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
#include "voyage/md.h"
#include "voyage/quote.h"
#include "voyage/space.h"
#include "voyage/input.h"

#endif // VOYAGE_EXPORT_H_
20 changes: 10 additions & 10 deletions voyage/button.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef VOYAGE_BUTTON_H_
#define VOYAGE_BUTTON_H_

#include <stdio.h>
#include "colors.h"

typedef struct ButtonOptions {
Expand All @@ -21,26 +20,30 @@ typedef struct Button {
int width;
char *text;
ButtonOptions options;
void (*onClick)();
void (*onClick)(struct Button);
} Button;

#define ButtonDefaultOptions ((ButtonOptions) \
{.centerText=1, .bgColor=Voyage_LightGrey, \
.fgColor=Voyage_White, .border=Voyage_DarkGrey, \
.font=GetFontDefault(), .fontSize=24, .textSpacing=3, \
.hOffset=10, .vOffset=10})
#define ButtonDefaultOnClick() (printf("Log: Button(%s) clicked\n", button.text))

Button Button_Init(Vector2, int, char *, void (*onClick)());
void ButtonDefaultOnClick(Button button) {
printf("Log: Button(%s) clicked\n", button.text);
}

Button Button_Init(Vector2, int, char *, void (*)());
void Button_SetOptions(Button *, ButtonOptions);
void Button_Resize(Button *, int);
void Button_ResizeReposition(Button *, Vector2, int);
Vector2 Button_Size(Button);
void Button_Draw(Button);

Button Button_Init(Vector2 pos, int width, char *text, void (*onClick)()) {
Button Button_Init(Vector2 pos, int width, char *text, void (*onClick)(Button)) {
return (Button){.pos = pos, .width=width, .text=text,
.options=ButtonDefaultOptions, .onClick = onClick};
.options=ButtonDefaultOptions,
.onClick = onClick? onClick: &ButtonDefaultOnClick};
}

void Button_SetOptions(Button *button, ButtonOptions options) {
Expand Down Expand Up @@ -90,10 +93,7 @@ void Button_Draw(Button button) {
DrawRectangleLinesEx(container, 2, border);
DrawTextEx(button.options.font, button.text, textPos, button.options.fontSize,
button.options.textSpacing, fgColor);
if (action) {
if (!button.onClick) ButtonDefaultOnClick();
else button.onClick();
}
if (action) button.onClick(button);
}

#endif // VOYAGE_BUTTON_H_
99 changes: 99 additions & 0 deletions voyage/input.h
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_

0 comments on commit 1b528f3

Please sign in to comment.