thrive/input.c
2025-01-30 18:03:08 -05:00

84 lines
1.8 KiB
C

#include <raylib.h>
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENTER 257
#define BACKSPACE 259
void push_character(Input *input, char c) {
if (input->input_length < INPUT_BUFFER_MAX_LENGTH) {
input->input_buffer[input->input_length++] = c;
input->input_buffer[input->input_length] = 0;
}
}
void pop_character(Input *input) {
if (input->input_length > 0) {
input->input_buffer[input->input_length - 1] = 0;
input->input_length--;
}
}
void clear_input_buffer(Input *input) {
input->input_buffer[0] = 0;
input->input_length = 0;
}
void push_input_buffer_to_log(Input *input) {
push_line_to_log(input->log, input->input_buffer);
}
void push_command_to_log(Input *input) {
push_line_to_log(input->log, &(input->command));
}
void handleKeyPress(Input *input, int c) {
if (c == BACKSPACE) {
pop_character(input);
} else if (c == ENTER) {
push_command_to_log(input);
game_handle_command(input->g, input->input_buffer);
clear_input_buffer(input);
} else if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' ') {
push_character(input, (char) c);
}
}
void handle_pressed_keys(Input *input) {
int c;
while((c = GetKeyPressed())) {
handleKeyPress(input, c);
}
}
void draw_prompt(Input *input) {
DrawText("> ", input->position.x, input->position.y, 20, YELLOW);
}
void draw_text(Input *input) {
draw_prompt(input);
// int text_width = MeasureText(input->input_buffer, 20);
DrawText(
input->input_buffer,
input->position.x + 20,
input->position.y,
20,
RAYWHITE
);
}
Input *create_input(Vector2 position) {
Input *input = malloc(sizeof(Input));
input->position.x = position.x;
input->position.y = position.y;
input->input_buffer[0] = 0;
return input;
}
void free_input(Input* input) {
free(input);
}