Compare commits
2 Commits
9106899c8a
...
c5937694e9
Author | SHA1 | Date |
---|---|---|
|
c5937694e9 | |
|
e2eb787a8b |
|
@ -1,52 +1,94 @@
|
|||
#include "menu_impl.h"
|
||||
#include "gui.h"
|
||||
#include "raymath.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static UIComp_t buttons[2] =
|
||||
{
|
||||
{
|
||||
.bbox = {25,255,125,30},
|
||||
.state = STATE_NORMAL,
|
||||
.alpha = 1.0
|
||||
},
|
||||
{
|
||||
.bbox = {25,300,125,30},
|
||||
.state = STATE_NORMAL,
|
||||
.alpha = 1.0
|
||||
}
|
||||
};
|
||||
|
||||
static void menu_scene_render_func(Scene_t *scene)
|
||||
{
|
||||
MenuSceneData_t *data = (MenuSceneData_t *)scene->scene_data;
|
||||
DrawText("This is a game", 25, 220, 12, BLACK);
|
||||
UI_button(buttons, "Start");
|
||||
UI_button(buttons + 1, "Exit");
|
||||
UI_button(data->buttons, "Start");
|
||||
UI_button(data->buttons + 1, "Continue");
|
||||
UI_button(data->buttons + 2, "Exit");
|
||||
}
|
||||
|
||||
static void menu_do_action(Scene_t *scene, ActionType_t action, bool pressed)
|
||||
{
|
||||
MenuSceneData_t *data = (MenuSceneData_t *)scene->scene_data;
|
||||
int new_selection = data->selected_comp;
|
||||
if (!pressed)
|
||||
{
|
||||
if (data->mode == MOUSE_MODE)
|
||||
{
|
||||
data->mode = KEYBOARD_MODE;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch(action)
|
||||
{
|
||||
case ACTION_UP:
|
||||
new_selection--;
|
||||
break;
|
||||
case ACTION_DOWN:
|
||||
new_selection++;
|
||||
break;
|
||||
case ACTION_LEFT:
|
||||
break;
|
||||
case ACTION_RIGHT:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
data->buttons[data->selected_comp].state = STATE_NORMAL;
|
||||
if (new_selection < 0) new_selection = data->max_comp - 1;
|
||||
if (new_selection >= data->max_comp) new_selection = 0;
|
||||
printf("new: %d, old %d\n", new_selection, data->selected_comp);
|
||||
|
||||
data->buttons[new_selection].state = STATE_FOCUSED;
|
||||
data->selected_comp = new_selection;
|
||||
}
|
||||
}
|
||||
|
||||
static void gui_loop(Scene_t* scene)
|
||||
{
|
||||
for (size_t i=0;i<2;i++)
|
||||
MenuSceneData_t *data = (MenuSceneData_t *)scene->scene_data;
|
||||
if (Vector2LengthSqr(GetMouseDelta()) > 1)
|
||||
{
|
||||
if ((buttons[i].state != STATE_DISABLED))
|
||||
data->mode = MOUSE_MODE;
|
||||
}
|
||||
if (data->mode == KEYBOARD_MODE)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
data->buttons[data->selected_comp].state = STATE_NORMAL;
|
||||
for (size_t i=0;i<data->max_comp;i++)
|
||||
{
|
||||
Vector2 mousePoint = GetMousePosition();
|
||||
|
||||
// Check button state
|
||||
if (CheckCollisionPointRec(mousePoint, buttons[i].bbox))
|
||||
if ((data->buttons[i].state != STATE_DISABLED))
|
||||
{
|
||||
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) buttons[i].state = STATE_PRESSED;
|
||||
else buttons[i].state = STATE_FOCUSED;
|
||||
Vector2 mousePoint = GetMousePosition();
|
||||
|
||||
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) buttons[i].pressed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
buttons[i].state = STATE_NORMAL;
|
||||
// Check button state
|
||||
if (CheckCollisionPointRec(mousePoint, data->buttons[i].bbox))
|
||||
{
|
||||
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
data->buttons[i].state = STATE_PRESSED;
|
||||
}
|
||||
else
|
||||
{
|
||||
data->buttons[i].state = STATE_FOCUSED;
|
||||
}
|
||||
if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
|
||||
{
|
||||
data->buttons[i].pressed = true;
|
||||
}
|
||||
data->selected_comp = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,6 +99,33 @@ void init_menu_scene(MenuScene_t *scene)
|
|||
|
||||
sc_array_add(&scene->scene.systems, &gui_loop);
|
||||
|
||||
scene->data.buttons[0] = (UIComp_t)
|
||||
{
|
||||
.bbox = {25,255,125,30},
|
||||
.state = STATE_NORMAL,
|
||||
.alpha = 1.0
|
||||
};
|
||||
|
||||
scene->data.buttons[1] = (UIComp_t)
|
||||
{
|
||||
.bbox = {25,300,125,30},
|
||||
.state = STATE_NORMAL,
|
||||
.alpha = 1.0
|
||||
};
|
||||
scene->data.buttons[2] = (UIComp_t)
|
||||
{
|
||||
.bbox = {25,345,125,30},
|
||||
.state = STATE_NORMAL,
|
||||
.alpha = 1.0
|
||||
};
|
||||
scene->data.max_comp = 3;
|
||||
scene->data.selected_comp = 0;
|
||||
scene->data.mode = KEYBOARD_MODE;
|
||||
|
||||
sc_map_put_64(&scene->scene.action_map, KEY_UP, ACTION_UP);
|
||||
sc_map_put_64(&scene->scene.action_map, KEY_DOWN, ACTION_DOWN);
|
||||
sc_map_put_64(&scene->scene.action_map, KEY_LEFT, ACTION_LEFT);
|
||||
sc_map_put_64(&scene->scene.action_map, KEY_RIGHT, ACTION_RIGHT);
|
||||
}
|
||||
|
||||
void free_menu_scene(MenuScene_t *scene)
|
||||
|
|
|
@ -1,11 +1,20 @@
|
|||
#ifndef __MENU_IMPL_H
|
||||
#define __MENU_IMPL_H
|
||||
#include "scene.h"
|
||||
#include "gui.h"
|
||||
|
||||
typedef enum GuiMode
|
||||
{
|
||||
KEYBOARD_MODE,
|
||||
MOUSE_MODE
|
||||
}GuiMode_t;
|
||||
|
||||
typedef struct MenuSceneData
|
||||
{
|
||||
Entity_t *menus[8];
|
||||
Entity_t *menu_opts[32];
|
||||
UIComp_t buttons[3];
|
||||
int selected_comp;
|
||||
int max_comp;
|
||||
GuiMode_t mode;
|
||||
}MenuSceneData_t;
|
||||
|
||||
typedef struct MenuScene
|
||||
|
|
Loading…
Reference in New Issue