/*
* Copyright (C) 2024 Tristan Riehs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "config.h"
#include
#include
#include
#include
#include "disp.h"
#define DEFAULT_HEIGHT 200
#define DEFAULT_WIDTH 16*DEFAULT_HEIGHT/9
#define MAX_FONTSIZE 100
static int current_input = 0;
static int current_x;
static int current_y;
void
init(void)
{
#ifndef NDEBUG
SetTraceLogLevel(LOG_DEBUG);
#else
SetTraceLogLevel(LOG_ERROR);
#endif
InitWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, "calculer");
if (!IsWindowReady())
{
fprintf(stderr, "calculer: error initialiazing Raylib \
window\n");
exit(1);
}
SetTargetFPS(60);
SetWindowState(FLAG_WINDOW_RESIZABLE);
BeginDrawing();
ClearBackground(RAYWHITE);
EndDrawing();
TraceLog(LOG_INFO, "window successfully initialized");
}
static int
min(int x, int y)
{
if (x < y)
return x;
else
return y;
}
static void
draw_calc(void)
{
char txt[64] = {0};
int win_width, win_height;
int txt_width;
int font_size;
sprintf(txt, "%d + %d", current_x, current_y);
win_width = GetScreenWidth();
win_height = GetScreenHeight();
font_size = min(win_height/3, MAX_FONTSIZE);
txt_width = MeasureText(txt, font_size);
TraceLog(LOG_DEBUG, "win_width = %d", win_width);
TraceLog(LOG_DEBUG, "win_height = %d", win_height);
TraceLog(LOG_DEBUG, "txt_width = %d", txt_width);
TraceLog(LOG_DEBUG, "font_size = %d", font_size);
if (txt_width > win_width)
{
float scale = ((float) txt_width)/win_width;
font_size = font_size/scale;
txt_width = MeasureText(txt, font_size);
}
DrawText(txt, (win_width-txt_width)/2, win_height/6, font_size, GRAY);
TraceLog(LOG_INFO, "calulation %d + %d successfully drawn",
current_x, current_y);
}
void
display_calc(int x, int y)
{
current_x = x;
current_y = y;
BeginDrawing();
ClearBackground(RAYWHITE);
draw_calc();
EndDrawing();
}
static void
draw_input(void)
{
char txt[64] = {0};
int win_width, win_height;
int txt_width;
int font_size;
sprintf(txt, "%d", current_input);
win_width = GetScreenWidth();
win_height = GetScreenHeight();
font_size = min(win_height/4, 3*MAX_FONTSIZE/4);
txt_width = MeasureText(txt, font_size);
if (txt_width > win_width)
{
float scale = txt_width/win_width;
font_size = font_size/scale;
txt_width = MeasureText(txt, font_size);
}
DrawText(txt, (win_width-txt_width)/2, win_height*4/6, font_size, DARKGRAY);
TraceLog(LOG_INFO, "input successfully drawn");
}
static void
redraw(void)
{
BeginDrawing();
ClearBackground(RAYWHITE);
draw_calc();
draw_input();
EndDrawing();
}
static void
add_input_digit(int digit)
{
current_input *= 10;
current_input += digit;
redraw();
}
static void
check_digit(int digit)
{
if (IsKeyPressed(KEY_ZERO + digit))
{
add_input_digit(digit);
TraceLog(LOG_INFO, "digit %d added to input", digit);
TraceLog(LOG_INFO, "input is now %d", current_input);
}
}
static void
check_digits(void)
{
for (int i = 0; i < 10; i++)
check_digit(i);
}
int
read_input(void)
{
TraceLog(LOG_INFO, "waiting for user input");
current_input = 0;
while (!WindowShouldClose())
{
PollInputEvents();
if (IsWindowResized())
redraw();
if (IsKeyPressed(KEY_ENTER))
return current_input;
if (IsKeyPressed(KEY_Q) || IsKeyPressed(KEY_A))
return DISP_QUIT;
if (IsKeyPressed(KEY_R) || IsKeyPressed(KEY_G))
return DISP_RELOAD;
if (IsKeyPressed(KEY_BACKSPACE))
{
current_input = current_input / 10;
redraw();
}
check_digits();
}
return DISP_QUIT;
}
void
display_res(bool, int)
{
}
void
destroy(void)
{
CloseWindow();
}
#ifndef NDEBUG
void *
pre_reload(void)
{
TraceLog(LOG_INFO, "pre-reload actions done");
return NULL;
}
void
post_reload(void *state)
{
(void) state;
TraceLog(LOG_INFO, "post-reload actions done");
}
#endif