summaryrefslogtreecommitdiff
path: root/commands.c
blob: d93e3ad7d6cbf7bf11981bfe6653a7c1e1a34d9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "commands.h"
#include "lzr.h"
#include "cfg.h"
#include <SDL2/SDL_log.h>

int mouse_x, mouse_y, hovered = -1, editing = -1, hovered_instr = -1;

static const unsigned int commands[COM_LEN] = {
	COM_RIGHT,
	COM_RIGHT,
	COM_UP,
	COM_DOWN,
	COM_LEFT,
	COM_NOOP,
	COM_LEFT,
	COM_DOWN,
	COM_UP
};

static const unsigned int instructions[_COM_COUNT][2] = {
	{COM_LEFT, COM_NOOP},
	{COM_RIGHT, COM_NOOP},
	{COM_UP, COM_NOOP},
	{COM_DOWN, COM_NOOP},
	{COM_NOOP, COM_NOOP},
};

void commands_update(void)
{
	LZR_MousePosition(&mouse_x, &mouse_y);
	hovered = -1;
	hovered_instr = -1;
	const int y = (mouse_y - CFG_TSIZE / 2) / (int)(CFG_TSIZE * 1.5);
	if (mouse_x > CFG_TSIZE / 2 && mouse_x <= CFG_TSIZE * 2.5) {
		if (y < 0 || y >= COM_LEN)
			return;
		hovered = y;
		if (LZR_BUTTON(MOUSE_L))
			editing = commands[y];
	}
	if (mouse_x > CFG_DWIDTH - CFG_TSIZE * 2.5) {
		if (y < 0 || y > 1)
			return;
		hovered_instr = y;
	}
}

void commands_draw(void)
{
	for (int i = 0; i < COM_LEN; i++) {
		const int dx = CFG_TSIZE / 2;
		const int dy = CFG_TSIZE / 2 + i * (int)(CFG_TSIZE * 1.5);
		const int cmd = commands[i];
		const int blink = hovered == i || (LZR_GetTick() & 1);
		if (!blink)
			;
		else if (cmd == editing) {
			LZR_DrawSetColor(rand() & 1, rand() & 1, rand() & 1, 1);
			LZR_DrawImage(cmd, dx, dy);
		} else {
			LZR_DrawSetColor(1, 1, 1, 1);
			LZR_DrawImage(cmd, dx, dy);
		}
	}
	if (editing != -1) {
		const int dx = CFG_DWIDTH - CFG_TSIZE * 2.5;
		for (int i = 0; i < 2; i++) {
			const int dy = CFG_TSIZE / 2 + i * (int)(CFG_TSIZE * 1.5);
			const int instr = instructions[editing][i];
			const int blink = hovered_instr == i || (LZR_GetTick() & 1);
			if (!blink)
				continue;
			LZR_DrawSetColor(1, 1, 1, 1);
			LZR_DrawImage(instr, dx, dy);
		}
	}
}