eco2d/code/foundation/src/gui/inventory.c

163 lines
5.5 KiB
C
Raw Normal View History

2022-08-09 14:46:23 +00:00
typedef struct {
uint8_t selected_item;
bool drop_item;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
bool item_is_held;
uint8_t held_item_idx;
2022-09-28 13:17:33 +00:00
Item held_item;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
bool is_inside;
bool storage_action;
bool swap;
uint8_t swap_from;
uint8_t swap_to;
} inv_keystate;
2021-08-30 15:50:05 +00:00
2022-08-09 14:46:23 +00:00
static inv_keystate player_inv = {0};
static inv_keystate storage_inv = {0};
2021-08-30 15:50:05 +00:00
2022-08-09 14:46:23 +00:00
bool inv_is_open = false;
2022-08-08 11:57:03 +00:00
bool inv_is_inside = false;
2022-08-09 14:46:23 +00:00
bool inv_is_storage_action = false;
bool inv_swap_storage = false;
2021-08-30 15:50:05 +00:00
2022-08-09 14:46:23 +00:00
void inventory_draw_panel(entity_view *e, bool is_player, float sx, float sy){
if (!e->has_items && is_player)
return;
if (!e->has_storage_items && !is_player)
2021-11-01 17:35:33 +00:00
return;
2022-09-29 20:49:40 +00:00
2021-08-30 15:50:05 +00:00
float x = sx;
float y = sy;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
const int32_t grid_size = (is_player) ? (64*3) : (64*4);
const int32_t inv_size = (is_player) ? ITEMS_INVENTORY_SIZE : ITEMS_CONTAINER_SIZE;
const int32_t inv_cols = (is_player) ? 3 : 4;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
inv_keystate *inv = (!is_player) ? &storage_inv : &player_inv;
inv_keystate *inv2 = (is_player) ? &storage_inv : &player_inv;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
inv->is_inside = check_mouse_area(sx, sy, (float)grid_size, (float)grid_size) != DAREA_OUTSIDE;
inv_is_inside |= inv->is_inside;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
for (int32_t i = 0; i < inv_size; i += 1) {
2021-08-30 15:50:05 +00:00
{
debug_area_status area = check_mouse_area(x, y, 64, 64);
Color color = RAYWHITE;
2022-09-28 13:17:33 +00:00
Item *item = (is_player) ? &e->items[i] : &e->storage_items[i];
2022-09-29 20:49:40 +00:00
2021-08-30 15:50:05 +00:00
if (area == DAREA_HOVER) {
color = YELLOW;
2022-08-09 14:46:23 +00:00
} else if (area == DAREA_PRESS && inv2->item_is_held){
2021-08-30 15:50:05 +00:00
color = VIOLET;
2022-08-09 14:46:23 +00:00
inv_swap_storage = true;
inv_is_storage_action = true;
inv->item_is_held = false;
inv2->item_is_held = false;
inv->selected_item = i;
inv->swap = true;
inv->swap_from = inv2->held_item_idx;
inv->swap_to = i;
2022-08-09 16:11:02 +00:00
} else if (area == DAREA_PRESS && !inv->item_is_held && !inv2->item_is_held) {
2021-08-30 15:50:05 +00:00
color = VIOLET;
2022-08-09 14:46:23 +00:00
inv_is_storage_action = true;
2022-08-09 16:11:02 +00:00
inv->selected_item = i;
2022-08-09 14:46:23 +00:00
} else if (area == DAREA_PRESS && inv->item_is_held) {
color = VIOLET;
2022-08-09 16:11:02 +00:00
inv_is_storage_action = true;
2022-08-09 14:46:23 +00:00
inv->selected_item = i;
inv->item_is_held = false;
inv->swap = true;
inv->swap_from = inv->held_item_idx;
inv->swap_to = i;
2022-08-09 16:11:02 +00:00
} else if (area == DAREA_HELD && item->quantity > 0 && !inv->item_is_held && !inv2->item_is_held) {
2022-08-09 14:46:23 +00:00
inv_is_storage_action = true;
inv->selected_item = i;
inv->item_is_held = true;
inv->held_item = *item;
inv->held_item_idx = i;
} else if (i == inv->selected_item) {
2021-08-30 15:50:05 +00:00
color = RED;
}
2022-09-29 20:49:40 +00:00
2021-08-30 15:50:05 +00:00
DrawRectangleLinesEco(x, y, 64, 64, color);
2022-09-29 20:49:40 +00:00
2021-08-30 15:50:05 +00:00
if (item->quantity > 0) {
2021-11-02 10:48:32 +00:00
DrawTexturePro(GetSpriteTexture2D(assets_find(item->kind)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, WHITE);
2022-09-29 20:49:40 +00:00
}
if (item->quantity > 1) {
DrawTextEco(zpl_bprintf("%d", item->quantity), x+5, y+5, 16, RAYWHITE, 0.0f);
}
if (item->quantity > 0 && item->durability < 1.0f) {
DrawRectangleEco(x, y+56, 64, 8, BLACK);
DrawRectangleEco(x, y+56, 64*item->durability, 8, BlendColor(RED, GREEN, item->durability));
2021-08-30 15:50:05 +00:00
}
}
x += 64;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
if ((i+1) % inv_cols == 0) {
2021-08-30 15:50:05 +00:00
x = sx;
y += 64;
}
}
2022-09-29 20:49:40 +00:00
2022-08-09 15:32:58 +00:00
// NOTE(zaklaus): switch it off if is_player
if (is_player)
inv_is_storage_action = false;
}
void inventory_render_held_item(bool is_player){
inv_keystate *inv = (!is_player) ? &storage_inv : &player_inv;
inv_keystate *inv2 = (is_player) ? &storage_inv : &player_inv;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
if (inv->item_is_held) {
2021-08-30 15:50:05 +00:00
Vector2 mpos = GetMousePosition();
mpos.x -= 32;
mpos.y -= 32;
2022-10-15 23:48:23 +00:00
Texture2D tex = GetSpriteTexture2D(assets_find(inv->held_item.kind));
float aspect = tex.width/(float)tex.height;
float size = WORLD_BLOCK_SIZE * aspect;
DrawTexturePro(tex, ASSET_SRC_RECT_TEX(tex.width, tex.height), ASSET_DST_RECT_TEX(mpos.x, mpos.y, size, WORLD_BLOCK_SIZE), (Vector2){0.5f,0.5f}, 0.0f, ColorAlpha(WHITE, 0.8f));
2022-09-29 20:49:40 +00:00
DrawTextEco(zpl_bprintf("%d", inv->held_item.quantity), mpos.x, mpos.y, 16, RAYWHITE, 0.0f);
2022-08-09 14:46:23 +00:00
if (!inv->is_inside && IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && !inv2->is_inside) {
inv->drop_item = true;
inv->item_is_held = false;
2022-09-29 20:49:40 +00:00
inv_is_storage_action = inv == &storage_inv;
2021-08-30 15:50:05 +00:00
}
}
2022-08-09 14:46:23 +00:00
}
void inventory_reset_states(inv_keystate *ik) {
ik->drop_item = false;
ik->swap = false;
}
void inventory_draw() {
inv_is_storage_action = false;
inv_is_inside = false;
inv_swap_storage = false;
inventory_reset_states(&player_inv);
inventory_reset_states(&storage_inv);
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
camera cam = camera_get();
entity_view *e = game_world_view_active_get_entity(cam.ent_id);
if (!e || !e->has_items) return;
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
if (IsKeyPressed(KEY_TAB)) {
inv_is_open = !inv_is_open;
}
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
if (!inv_is_open || build_is_in_draw_mode) {
return;
}
2022-09-29 20:49:40 +00:00
2022-08-09 14:46:23 +00:00
inventory_draw_panel(e, true, screenWidth/2.0f + 128, screenHeight/2.0f - 96);
inventory_draw_panel(e, false, screenWidth/2.0f - 384, screenHeight/2.0f - 128);
2022-09-29 20:49:40 +00:00
2022-08-09 15:32:58 +00:00
inventory_render_held_item(true);
inventory_render_held_item(false);
2021-11-01 18:20:07 +00:00
}