Remap calls to pad/mouse/keyboard to input

This commit is contained in:
Xavier Del Campo Romero 2022-09-20 13:43:18 +02:00
parent 684587a3c3
commit 4c5630b0d4
22 changed files with 195 additions and 94 deletions

View File

@ -1,3 +1,12 @@
add_library(camera "src/camera.c" "src/pad.c" "src/mouse.c" "src/touch.c")
target_include_directories(camera PUBLIC "inc" PRIVATE "privinc")
target_link_libraries(camera PUBLIC container mouse pad peripheral util PRIVATE gfx)
target_link_libraries(camera
PUBLIC
container
input
peripheral
util
PRIVATE
gfx
mouse
pad)

View File

@ -1,8 +1,7 @@
#ifndef CAMERA_H
#define CAMERA_H
#include <mouse.h>
#include <pad.h>
#include <input.h>
#include <peripheral.h>
#include <util.h>
#include <stdbool.h>
@ -50,7 +49,7 @@ struct camera
extern struct sprite cursor_sprite;
void camera_update(struct camera *cam, const union peripheral *p);
void camera_update(struct camera *cam, const union peripheral *p, const struct input *in);
bool camera_translate(const struct camera *cam, const struct util_rect *dim, short *x, short *y);
void cursor_init(struct cursor *c);
bool cursor_collision(const struct camera *cam, const struct util_rect *d);

View File

@ -2,6 +2,9 @@
#define CAMERA_PRIVATE_H
#include <camera.h>
#include <input.h>
#include <mouse.h>
#include <pad.h>
#ifdef __cplusplus
extern "C"
@ -15,9 +18,11 @@ enum
};
void camera_update_pos(struct camera *cam);
void camera_update_pad(struct camera *cam, const struct pad *p);
void camera_update_pad(struct camera *cam, const struct pad *p,
const struct input *in);
void camera_update_mouse(struct camera *cam, const struct mouse *m);
void camera_update_touch(struct camera *cam, const struct mouse *m);
void camera_update_touch(struct camera *cam, const struct mouse *m,
const struct input *in);
#ifdef __cplusplus
}

View File

@ -124,12 +124,13 @@ void cursor_set_pos_list(struct cursor *const c,
};
}
void camera_update(struct camera *const cam, const union peripheral *const p)
void camera_update(struct camera *const cam,
const union peripheral *const p, const struct input *const in)
{
switch (p->common.type)
{
case PERIPHERAL_TYPE_PAD:
camera_update_pad(cam, &p->pad.pad);
camera_update_pad(cam, &p->pad.pad, in);
break;
case PERIPHERAL_TYPE_KEYBOARD_MOUSE:
@ -137,7 +138,7 @@ void camera_update(struct camera *const cam, const union peripheral *const p)
break;
case PERIPHERAL_TYPE_TOUCH:
camera_update_touch(cam, &p->kbm.mouse);
camera_update_touch(cam, &p->kbm.mouse, in);
break;
}
}

View File

@ -1,5 +1,6 @@
#include <camera.h>
#include <camera_private.h>
#include <input.h>
#include <pad.h>
#include <peripheral.h>
#include <gfx.h>
@ -7,7 +8,8 @@
#include <stdbool.h>
#include <stdlib.h>
static void cursor_update(struct camera *const cam, const struct pad *const p)
static void cursor_update(struct camera *const cam,
const struct pad *const p, const struct input *const in)
{
struct cursor *const c = &cam->cursor;
enum {STEP = 4};
@ -16,29 +18,31 @@ static void cursor_update(struct camera *const cam, const struct pad *const p)
|| c->screen.last_h != screen_h)
cursor_init(c);
if (pad_pressed(p, PAD_KEY_LEFT)
if (input_pad_pressed(in, p, PAD_KEY_LEFT)
&& (c->x - STEP)
&& (!cam->x || c->x != c->x_init))
c->x -= STEP;
else if (pad_pressed(p, PAD_KEY_RIGHT)
else if (input_pad_pressed(in, p, PAD_KEY_RIGHT)
&& (c->x + STEP < screen_w)
&& (c->x != c->x_init || cam->x <= -cam->dim.w))
c->x += STEP;
if (pad_pressed(p, PAD_KEY_UP)
if (input_pad_pressed(in, p, PAD_KEY_UP)
&& (c->y - STEP)
&& (c->y != c->y_init || !cam->y))
c->y -= STEP;
else if (pad_pressed(p, PAD_KEY_DOWN)
else if (input_pad_pressed(in, p, PAD_KEY_DOWN)
&& (c->y + STEP < screen_h)
&& (c->y != c->y_init || cam->y <= -cam->dim.h))
c->y += STEP;
c->state = pad_pressed(p, PAD_KEY_A) || pad_pressed(p, PAD_KEY_B) ?
c->state = input_pad_pressed(in, p, PAD_KEY_A) ||
input_pad_pressed(in, p, PAD_KEY_B) ?
CURSOR_STATE_PRESSED: CURSOR_STATE_IDLE;
}
static void update_speed(struct camera *const cam, const struct pad *const p)
static void update_speed(struct camera *const cam,
const struct pad *const p, const struct input *const in)
{
enum
{
@ -52,14 +56,14 @@ static void update_speed(struct camera *const cam, const struct pad *const p)
if (c->x == c->x_init
&& (!cam->x_speed || ++cam->xt >= T_STEP))
{
if (pad_pressed(p, PAD_KEY_RIGHT))
if (input_pad_pressed(in, p, PAD_KEY_RIGHT))
{
if (cam->x_speed > 0)
cam->x_speed = -STEP;
else if (cam->x_speed - STEP >= -MAX_SPEED)
cam->x_speed -= STEP;
}
else if (pad_pressed(p, PAD_KEY_LEFT))
else if (input_pad_pressed(in, p, PAD_KEY_LEFT))
{
if (cam->x_speed < 0)
cam->x_speed = STEP;
@ -77,14 +81,14 @@ static void update_speed(struct camera *const cam, const struct pad *const p)
if (c->y == c->y_init
&& (!cam->y_speed || ++cam->yt >= T_STEP))
{
if (pad_pressed(p, PAD_KEY_DOWN))
if (input_pad_pressed(in, p, PAD_KEY_DOWN))
{
if (cam->y_speed > 0)
cam->y_speed = STEP;
else if (cam->y_speed - STEP >= -MAX_SPEED)
cam->y_speed -= STEP;
}
else if (pad_pressed(p, PAD_KEY_UP))
else if (input_pad_pressed(in, p, PAD_KEY_UP))
{
if (cam->y_speed < 0)
cam->y_speed = -STEP;
@ -119,7 +123,7 @@ static enum pad_key get_ref_key(struct cursor_pos_rt *const rt,
}
static void cursor_update_fixed(struct camera *const cam,
const struct pad *const p)
const struct pad *const p, const struct input *const in)
{
struct cursor *const c = &cam->cursor;
struct cursor_pos_rt *const rt = &c->rt;
@ -129,7 +133,7 @@ static void cursor_update_fixed(struct camera *const cam,
{
const enum pad_key key = get_ref_key(rt, next);
if (pad_justpressed(p, key))
if (input_pad_justpressed(in, p, key))
rt->i = next;
}
else if (rt->i)
@ -137,7 +141,7 @@ static void cursor_update_fixed(struct camera *const cam,
const size_t prev = rt->i - 1;
const enum pad_key key = get_ref_key(rt, prev);
if (pad_justpressed(p, key))
if (input_pad_justpressed(in, p, key))
rt->i = prev;
}
@ -147,14 +151,15 @@ static void cursor_update_fixed(struct camera *const cam,
c->y = cur->y;
}
void camera_update_pad(struct camera *const cam, const struct pad *const p)
void camera_update_pad(struct camera *const cam, const struct pad *const p,
const struct input *const in)
{
if (cam->cursor.rt.list)
cursor_update_fixed(cam, p);
cursor_update_fixed(cam, p, in);
else
{
cursor_update(cam, p);
update_speed(cam, p);
cursor_update(cam, p, in);
update_speed(cam, p, in);
camera_update_pos(cam);
}
}

View File

@ -13,11 +13,12 @@ static void cursor_update(struct cursor *const c, const struct mouse *const m)
c->y = m->y;
}
static void update_speed(struct camera *const cam, const struct mouse *const m)
static void update_speed(struct camera *const cam, const struct mouse *const m,
const struct input *const in)
{
int *const sx = &cam->x_speed, *const sy = &cam->y_speed;
if (mouse_pressed(m, MOUSE_BUTTON_LEFT))
if (input_mouse_pressed(in, m, MOUSE_BUTTON_LEFT))
{
*sx = m->dx;
*sy = m->dy;
@ -42,9 +43,10 @@ static void update_speed(struct camera *const cam, const struct mouse *const m)
}
}
void camera_update_touch(struct camera *const cam, const struct mouse *const m)
void camera_update_touch(struct camera *const cam, const struct mouse *const m,
const struct input *const in)
{
cursor_update(&cam->cursor, m);
update_speed(cam, m);
update_speed(cam, m, in);
camera_update_pos(cam);
}

View File

@ -9,4 +9,4 @@ add_library(gui
"src/rounded_rect.c"
)
target_include_directories(gui PUBLIC "inc" PRIVATE "privinc")
target_link_libraries(gui PUBLIC camera gfx peripheral font)
target_link_libraries(gui PUBLIC camera gfx input peripheral font)

View File

@ -3,6 +3,7 @@
#include <camera.h>
#include <gfx.h>
#include <input.h>
#include <peripheral.h>
#ifdef __cplusplus
@ -16,7 +17,7 @@ struct gui_common
{
void (*add_child)(struct gui_common *parent, struct gui_common *child);
int (*update)(struct gui_common *, const union peripheral *,
const struct camera *);
const struct camera *, struct input *);
int (*render)(const struct gui_common *);
void (*get_dim)(const struct gui_common *, short *w, short *h);
} *cb;
@ -29,7 +30,7 @@ struct gui_common
void gui_add_child(struct gui_common *parent, struct gui_common *child);
void gui_add_sibling(struct gui_common *g, struct gui_common *sibling);
int gui_update(struct gui_common *g, const union peripheral *p,
const struct camera *c);
const struct camera *c, struct input *in);
int gui_render(const struct gui_common *g);
#ifdef __cplusplus

View File

@ -2,6 +2,9 @@
#define GUI_PRIVATE_H
#include <gui.h>
#include <input.h>
#include <peripheral.h>
#include <camera.h>
#ifdef __cplusplus
extern "C"
@ -9,6 +12,10 @@ extern "C"
#endif
void gui_coords(const struct gui_common *g, short *x, short *y);
bool gui_pressed(const struct gui_common *g, const struct input *in,
const union peripheral *p, const struct camera *cam, short w, short h);
bool gui_released(const struct gui_common *g, const union peripheral *p,
const struct camera *cam, short w, short h);
#ifdef __cplusplus
}

View File

@ -4,6 +4,7 @@
#include <gui_private.h>
#include <camera.h>
#include <gfx.h>
#include <input.h>
#include <mouse.h>
#include <pad.h>
#include <peripheral.h>
@ -37,47 +38,22 @@ static void get_dim(const struct gui_common *const g,
static bool pressed(const struct gui_button *const b,
const union peripheral *const p,
const struct camera *const cam)
const struct camera *const cam,
const struct input *const in)
{
bool check = false;
short w, h;
switch (p->common.type)
{
case PERIPHERAL_TYPE_PAD:
check = pad_justpressed(&p->pad.pad, PAD_KEY_A);
break;
case PERIPHERAL_TYPE_KEYBOARD_MOUSE:
check = mouse_justreleased(&p->kbm.mouse, MOUSE_BUTTON_LEFT);
break;
case PERIPHERAL_TYPE_TOUCH:
check = mouse_justpressed(&p->kbm.mouse, MOUSE_BUTTON_LEFT);
break;
}
if (check)
{
short x, y;
struct util_rect d;
gui_coords(&b->common, &x, &y);
get_dim(&b->common, &d.w, &d.h);
d.x = x;
d.y = y;
return cursor_collision(cam, &d);
}
return false;
get_dim(&b->common, &w, &h);
return gui_pressed(&b->common, in, p, cam, w, h);
}
static int update(struct gui_common *const g,
const union peripheral *const p, const struct camera *const c)
const union peripheral *const p, const struct camera *const c,
struct input *const in)
{
struct gui_button *const b = (struct gui_button *)g;
if (pressed(b, p, c) && b->on_pressed)
if (pressed(b, p, c, in) && b->on_pressed)
b->on_pressed(b->arg);
return 0;

View File

@ -1,6 +1,7 @@
#include <gui/container.h>
#include <gui.h>
#include <camera.h>
#include <input.h>
#include <peripheral.h>
static void add_child(struct gui_common *const parent,
@ -52,7 +53,8 @@ static void get_dim(const struct gui_common *const g,
}
static int update(struct gui_common *const g,
const union peripheral *const p, const struct camera *const cam)
const union peripheral *const p, const struct camera *const cam,
struct input *const in)
{
struct gui_container *const c = (struct gui_container *)g;

View File

@ -1,4 +1,7 @@
#include <gui.h>
#include <camera.h>
#include <input.h>
#include <peripheral.h>
static void get_centered(const struct gui_common *const g,
short *const x, short *const y)
@ -51,6 +54,79 @@ void gui_coords(const struct gui_common *const g, short *const x,
get_centered(g, x, y);
}
static bool check_collision(const struct gui_common *const g,
const union peripheral *const p,
const struct camera *cam, const short w, const short h)
{
short x, y;
gui_coords(g, &x, &y);
const struct util_rect d =
{
.x = x,
.y = y,
.w = w,
.h = h
};
return cursor_collision(cam, &d);
}
bool gui_pressed(const struct gui_common *const g,
const struct input *const in,
const union peripheral *const p,
const struct camera *cam, const short w, const short h)
{
bool check = false;
switch (p->common.type)
{
case PERIPHERAL_TYPE_PAD:
check = input_pad_justpressed(in, &p->pad.pad, PAD_KEY_A);
break;
case PERIPHERAL_TYPE_KEYBOARD_MOUSE:
/* Fall through. */
case PERIPHERAL_TYPE_TOUCH:
check = input_mouse_justreleased(in, &p->kbm.mouse,
MOUSE_BUTTON_LEFT);
break;
}
if (check)
return check_collision(g, p, cam, w, h);
return false;
}
bool gui_released(const struct gui_common *const g,
const union peripheral *const p,
const struct camera *cam, const short w, const short h)
{
bool check = false;
switch (p->common.type)
{
case PERIPHERAL_TYPE_PAD:
check = pad_justpressed(&p->pad.pad, PAD_KEY_A);
break;
case PERIPHERAL_TYPE_KEYBOARD_MOUSE:
/* Fall through. */
case PERIPHERAL_TYPE_TOUCH:
check = mouse_justreleased(&p->kbm.mouse,
MOUSE_BUTTON_LEFT);
break;
}
if (check)
return !check_collision(g, p, cam, w, h);
return false;
}
void gui_add_sibling(struct gui_common *const g,
struct gui_common *const s)
{
@ -77,16 +153,16 @@ void gui_add_child(struct gui_common *const p,
}
int gui_update(struct gui_common *const g, const union peripheral *const p,
const struct camera *const c)
const struct camera *const c, struct input *const in)
{
if (g->child && gui_update(g->child, p, c))
if (g->child && gui_update(g->child, p, c, in))
return -1;
if (g->cb && g->cb->update && g->cb->update(g, p, c))
if (g->cb && g->cb->update && g->cb->update(g, p, c, in))
return -1;
for (struct gui_common *s = g->sibling; s; s = s->sibling)
if (gui_update(s, p, c))
if (gui_update(s, p, c, in))
return -1;
return 0;

View File

@ -5,4 +5,4 @@ add_library(menu
"src/main_menu.c"
)
target_include_directories(menu PUBLIC "inc" PRIVATE "privinc")
target_link_libraries(menu PRIVATE camera game gfx gui system)
target_link_libraries(menu PRIVATE camera game gfx gui input system)

View File

@ -2,6 +2,7 @@
#define MENU_PRIVATE_H
#include <camera.h>
#include <input.h>
#include <peripheral.h>
#include <stdbool.h>
@ -14,6 +15,7 @@ struct menu_common
{
struct camera cam;
union peripheral p;
struct input in;
};
int menu_update(struct menu_common *c,

View File

@ -23,8 +23,8 @@ static int update(struct menu_common *const c, void *const arg)
m->r.w = screen_w / 2;
m->r.h = screen_h / 2;
if (gui_update(&m->cnt.common, &c->p, &c->cam)
|| gui_update(&m->bcnt.common, &c->p, &c->cam))
if (gui_update(&m->cnt.common, &c->p, &c->cam, &c->in)
|| gui_update(&m->bcnt.common, &c->p, &c->cam, &c->in))
return -1;
return 0;

View File

@ -16,7 +16,7 @@ static int update(struct menu_common *const c, void *const arg)
{
struct menu_hostjoin *const m = arg;
if (gui_update(&m->cnt.common, &c->p, &c->cam))
if (gui_update(&m->cnt.common, &c->p, &c->cam, &c->in))
return -1;
return 0;

View File

@ -18,7 +18,7 @@ static int update(struct menu_common *const c, void *const arg)
{
struct main_menu *const m = arg;
if (gui_update(&m->play.common, &c->p, &c->cam))
if (gui_update(&m->play.common, &c->p, &c->cam, &c->in))
return -1;
return 0;

View File

@ -3,6 +3,7 @@
#include <camera.h>
#include <game.h>
#include <gfx.h>
#include <input.h>
#include <peripheral.h>
#include <system.h>
#include <stdbool.h>
@ -19,7 +20,8 @@ int menu_update(struct menu_common *const c,
{
system_loop();
peripheral_update(&c->p);
camera_update(&c->cam, &c->p);
input_update(&c->in, &c->p);
camera_update(&c->cam, &c->p, &c->in);
if (update && update(c, arg))
return -1;
@ -32,6 +34,8 @@ int menu_update(struct menu_common *const c,
if (render && render(c, arg))
return -1;
else if (input_render(&c->in, &c->p))
return -1;
switch (c->p.common.type)
{

View File

@ -56,6 +56,7 @@ UTIL_STATIC_ASSERT(!offsetof(struct peripheral_kbm, common),
"unexpected offsetof for struct peripheral_kbm");
void peripheral_init(const struct peripheral_cfg *cfg, union peripheral *p);
void peripheral_input_set(union peripheral *p, void (*cb)(char, void *));
void peripheral_update(union peripheral *p);
#ifdef __cplusplus

View File

@ -11,6 +11,7 @@ target_link_libraries(player
gfx
keyboard
instance
input
mouse
pad
resource

View File

@ -4,6 +4,7 @@
#include <camera.h>
#include <keyboard.h>
#include <instance.h>
#include <input.h>
#include <mouse.h>
#include <pad.h>
#include <peripheral.h>
@ -32,6 +33,7 @@ struct human_player
struct player pl;
struct camera cam;
union peripheral periph;
struct input in;
struct sel_instance
{

View File

@ -6,6 +6,7 @@
#include <gfx.h>
#include <gui.h>
#include <instance.h>
#include <input.h>
#include <keyboard.h>
#include <pad.h>
#include <resource.h>
@ -542,26 +543,28 @@ static void update_target(struct human_player *const h)
static void update_from_pad(struct human_player *const h,
struct player_others *const o)
{
struct pad *const p = &h->periph.pad.pad;
const struct pad *const p = &h->periph.pad.pad;
const struct input *const in = &h->in;
if (pad_justpressed(p, PAD_KEY_A))
if (input_pad_justpressed(in, p, PAD_KEY_A))
select_instances(h, o, false, false);
else if (pad_justpressed(p, PAD_KEY_B))
else if (input_pad_justpressed(in, p, PAD_KEY_B))
move_units(h, o);
else if (pad_justpressed(p, PAD_KEY_C))
else if (input_pad_justpressed(in, p, PAD_KEY_C))
deselect_instances(h);
else if (pad_justpressed(p, PAD_KEY_E))
else if (input_pad_justpressed(in, p, PAD_KEY_E))
h->top_gui ^= true;
}
static void update_from_touch(struct human_player *const h,
struct player_others *const o)
{
struct mouse *const m = &h->periph.kbm.mouse;
const struct mouse *const m = &h->periph.kbm.mouse;
const struct input *const in = &h->in;
struct peripheral_kbm *const kbm = &h->periph.kbm;
bool *const pan = &h->cam.pan;
if (mouse_pressed(m, MOUSE_BUTTON_LEFT) && !*pan)
if (input_mouse_pressed(in, m, MOUSE_BUTTON_LEFT) && !*pan)
{
enum {LONG_PRESS_THRESHOLD = 30};
@ -573,7 +576,7 @@ static void update_from_touch(struct human_player *const h,
deselect_instances(h);
}
}
else if (mouse_justreleased(m, MOUSE_BUTTON_LEFT))
else if (input_mouse_justreleased(in, m, MOUSE_BUTTON_LEFT))
{
if (!*pan && !select_instances(h, o, false, true))
move_units(h, o);
@ -587,19 +590,22 @@ static void update_from_touch(struct human_player *const h,
static void update_from_keyboard_mouse(struct human_player *const h,
struct player_others *const o)
{
struct mouse *const m = &h->periph.kbm.mouse;
struct keyboard *const k = &h->periph.kbm.keyboard;
const struct mouse *const m = &h->periph.kbm.mouse;
const struct keyboard *const k = &h->periph.kbm.keyboard;
const struct input *const in = &h->in;
if (mouse_justreleased(m, MOUSE_BUTTON_LEFT))
if (input_mouse_justreleased(in, m, MOUSE_BUTTON_LEFT))
{
const bool shift_pressed =
keyboard_pressed(k, &KEYBOARD_COMBO(KEYBOARD_KEY_LSHIFT))
|| keyboard_pressed(k, &KEYBOARD_COMBO(KEYBOARD_KEY_RSHIFT));
input_keyboard_pressed(in, k,
&KEYBOARD_COMBO(KEYBOARD_KEY_LSHIFT))
|| input_keyboard_pressed(in, k,
&KEYBOARD_COMBO(KEYBOARD_KEY_RSHIFT));
if (!select_instances(h, o, !shift_pressed, false))
deselect_instances(h);
}
else if (mouse_justreleased(m, MOUSE_BUTTON_RIGHT))
else if (input_mouse_justreleased(in, m, MOUSE_BUTTON_RIGHT))
move_units(h, o);
}
@ -614,6 +620,7 @@ void human_player_update(struct human_player *const h,
update_selected(h);
update_target(h);
peripheral_update(&h->periph);
input_update(&h->in, &h->periph);
switch (h->periph.common.type)
{
@ -630,7 +637,7 @@ void human_player_update(struct human_player *const h,
break;
}
camera_update(&h->cam, &h->periph);
camera_update(&h->cam, &h->periph, &h->in);
player_update(p);
}
}
@ -702,7 +709,8 @@ int human_player_render(const struct human_player *const h,
|| render_own_units(h)
|| render_own_buildings(h)
|| render_resources(h, o->res, o->n_res)
|| human_player_gui_render(h))
|| human_player_gui_render(h)
|| input_render(&h->in, &h->periph))
return -1;
switch (h->periph.common.type)