A game engine for top-down 2D RPG games.
rpg
game-engine
raylib
c99
1#include "keraforge/graphics.h"
2#include <keraforge.h>
3#include <raylib.h>
4
5
6struct kf_uiconfig kf_uiconfig = {
7 .select = KF_INPUTBIND_NONE,
8 .cancel = KF_INPUTBIND_NONE,
9 .up = KF_INPUTBIND_NONE,
10 .down = KF_INPUTBIND_NONE,
11 .fmt = " %s ",
12 .selectfmt = "> %s <",
13 .fontsize = 20,
14 .bg = BLACK,
15 .fg = WHITE,
16 .panelheight = 200,
17 .xpadding = 8,
18 .ypadding = 16,
19};
20
21
22void kf_ui_init(void)
23{
24 kf_uiconfig.select = kf_inputbind_select;
25 kf_uiconfig.cancel = kf_inputbind_cancel;
26 kf_uiconfig.up = kf_inputbind_ui_up;
27 kf_uiconfig.down = kf_inputbind_ui_down;
28 kf_uiconfig.fontsize = kf_window.fontsize;
29}
30
31int kf_ui_panel(char *title)
32{
33 int y = GetScreenHeight() - kf_uiconfig.panelheight;
34 DrawRectangle(0, y, GetScreenWidth(), kf_uiconfig.panelheight, kf_uiconfig.bg);
35
36 if (title)
37 {
38 y += kf_uiconfig.ypadding;
39 kf_drawtextshadowed(kf_uiconfig.fg, kf_uiconfig.xpadding, y, kf_uiconfig.fontsize, title);
40 y += kf_uiconfig.fontsize;
41 }
42
43 return y;
44}
45
46int kf_ui_choice(char *title, char **choices, int nchoices, int *choice)
47{
48 int y = kf_ui_panel(title);
49
50 if (*choice >= nchoices)
51 goto skip_text;
52
53 for (int i = 0 ; i < nchoices ; i++)
54 {
55 char *c = choices[i];
56
57 kf_drawtextshadowed(
58 kf_uiconfig.fg,
59 kf_uiconfig.xpadding,
60 y,
61 kf_uiconfig.fontsize,
62 (char *)TextFormat(i == *choice ? kf_uiconfig.selectfmt : kf_uiconfig.fmt, c)
63 );
64
65 y += kf_uiconfig.fontsize;
66 if (y >= GetScreenHeight())
67 break;
68 }
69
70skip_text:
71
72 if (kf_checkinputpress(kf_uiconfig.select) || kf_checkinputpress(kf_uiconfig.cancel))
73 return 1;
74 else if (kf_checkinputpress(kf_uiconfig.down) && *choice < nchoices - 1)
75 (*choice)++;
76 else if (kf_checkinputpress(kf_uiconfig.up) && *choice > 0)
77 (*choice)--;
78
79 return 0;
80}
81
82int kf_ui_yesno(char *title, int *choice)
83{
84 static char *yesno[] = { "yes", "no" };
85 return !kf_ui_choice(title, yesno, 2, choice);
86}
87
88int kf_ui_textinput(char *title, char *text);
89