diff options
Diffstat (limited to 'src/ui.c')
| -rw-r--r-- | src/ui.c | 98 |
1 files changed, 91 insertions, 7 deletions
@@ -83,10 +83,10 @@ void updateInteractionChat(InteractionChat* chat, Game* game) } Color background = DARKGRAY; - background.a = game->settings.interactionAlpha; + background.a = game->settings.uiAlpha; DrawRectangleRec(chat->rect, background); - float lineThickness = game->settings.interactionOutlineSize; + float lineThickness = game->settings.uiOutlineSize; float border = lineThickness + 1.0; int fontSize = game->settings.interactionFontSize; @@ -134,7 +134,7 @@ void resizeInteractionMenu(InteractionMenu* menu, const Settings* settings) settings->interactionFontSize; menu->rect.x = 0.0; menu->rect.y = GetRenderHeight() - settings->interactionChatHeight - - menu->rect.height - settings->interactionOutlineSize; + menu->rect.height - settings->uiOutlineSize; } void initInteractionMenu(InteractionMenu* menu, const Settings* settings) @@ -198,10 +198,10 @@ void updateInteractionMenu(InteractionMenu* menu, Game* game) // Draw background. Color background = DARKGRAY; - background.a = game->settings.interactionAlpha; + background.a = game->settings.uiAlpha; DrawRectangleRec(menu->rect, background); - float lineThickness = game->settings.interactionOutlineSize; + float lineThickness = game->settings.uiOutlineSize; float border = lineThickness + 1.0; int fontSize = game->settings.interactionFontSize; @@ -228,8 +228,18 @@ void updateInteractionMenu(InteractionMenu* menu, Game* game) } } -void initInventory(Inventory* inventory) +void resizeInventory(Inventory* inventory, const Settings* settings) { + float outlineThickness = settings->uiOutlineSize * 2.0; + float spriteSize = INVENTORY_SPRITE_SIZE * INVENTORY_SCALE; + inventory->rect.width = INVENTORY_COLUMNS * spriteSize + outlineThickness; + inventory->rect.height = INVENTORY_ROWS * spriteSize + outlineThickness; + inventory->rect.x = GetRenderWidth() / 2.0 - inventory->rect.width / 2.0; + inventory->rect.y = GetRenderHeight() / 2.0 - inventory->rect.height / 2.0; +} +void initInventory(Inventory* inventory, const Settings* settings) +{ + resizeInventory(inventory, settings); inventory->visable = false; inventory->itemCount = 0; } @@ -246,8 +256,82 @@ void hideInventory(Inventory* inventory) void addItemToInventory(Inventory* inventory, InventoryItem item) { + // If item is already in the inventory. + for (int index = 0; index < inventory->itemCount; ++index) + { + if (inventory->items[index].id == item.id) + { + inventory->items[index].count += item.count; + return; + } + } + + // Add item to inventory. + if (inventory->itemCount >= INVENTORY_MAX) + { + TraceLog(LOG_ERROR, "inventory->itemCount >= INVENTORY_MAX"); + } + else + { + inventory->items[inventory->itemCount] = item; + ++inventory->itemCount; + } } -void updateInventory(Inventory* inventory) +void updateInventory(Inventory* inventory, Game* game) { + if (IsWindowResized()) + { + resizeInventory(inventory, &game->settings); + } + + // Handle toggle key. + if (IsKeyPressed(game->settings.toggleInventoryKey)) + { + inventory->visable = !inventory->visable; + } + + if (!inventory->visable) + { + return; + } + + // Draw background. + float outlineThickness = game->settings.uiOutlineSize; + Color backgroundColor = PINK; + backgroundColor.a = game->settings.uiAlpha; + DrawRectangleRec(inventory->rect, backgroundColor); + DrawRectangleLinesEx(inventory->rect, outlineThickness, BLACK); + + // Draw items. + float spriteSize = INVENTORY_SPRITE_SIZE * INVENTORY_SCALE; + int startX = inventory->rect.x + outlineThickness; + int startY = inventory->rect.y + outlineThickness; + int maxX = inventory->rect.width + inventory->rect.x - spriteSize; + int maxY = inventory->rect.height + inventory->rect.y - spriteSize; + int x = startX; + int y = startY; + int fontSize = spriteSize / 3.0; + + for (int index = 0; index < inventory->itemCount; ++index) + { + InventoryItem* item = &inventory->items[index]; + + DrawTextureEx(game->assets.textures[item->textureId], (Vector2){x, y}, 0.0, + INVENTORY_SCALE, WHITE); + + // Draw count. + if (item->count > 1) + { + DrawText(TextFormat("%d", item->count), x, y, fontSize, BLACK); + } + + x += spriteSize; + + if (x >= maxX) + { + x = startX; + y += spriteSize; + } + } } |
