Certainly! Below is detailed C++ code using the Qt framework to create a crafting UI similar to the one in 7 Days to Die. I've included exact sizing and styling decisions to match the look and feel of the game's interface.
Prerequisites:
- Install Qt (version 5 or later). You can download it from Qt's official website.
- Set up your development environment to compile Qt applications.
Overview
The crafting UI consists of the following components:
- Inventory Grid: Displays the player's items.
- Crafting Grid: Where the player places items to craft new ones.
- Crafting Output Slot: Shows the result of the crafting process.
- Crafting Queue: Displays items queued for crafting.
- Player Toolbar: Quick access to selected items.
File Structure
main.cpp: Entry point of the application.
CraftingUI.h: Header file for the CraftingUI class.
CraftingUI.cpp: Implementation of the CraftingUI class.
Code
1. main.cpp
#include <QApplication>
#include "CraftingUI.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
CraftingUI window;
window.show();
return app.exec();
}
2. CraftingUI.h
#ifndef CRAFTINGUI_H
#define CRAFTINGUI_H
#include <QWidget>
class CraftingUI : public QWidget {
Q_OBJECT
public:
explicit CraftingUI(QWidget *parent = nullptr);
};
#endif // CRAFTINGUI_H
3. CraftingUI.cpp
#include "CraftingUI.h"
#include <QtWidgets>
// Custom widget for inventory and crafting slots
class ItemSlot : public QLabel {
public:
explicit ItemSlot(QWidget *parent = nullptr) : QLabel(parent) {
// Set slot size
setFixedSize(60, 60);
// Set slot appearance
setStyleSheet("QLabel { border: 1px solid #555; background-color: #2E2E2E; }");
setAlignment(Qt::AlignCenter);
// Placeholder text (empty slot)
setText("");
}
};
CraftingUI::CraftingUI(QWidget *parent) : QWidget(parent) {
// Set window properties
setWindowTitle("Crafting UI");
resize(1024, 768);
// Main layout
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setContentsMargins(10, 10, 10, 10);
// Left panel: Inventory
QWidget *inventoryPanel = new QWidget(this);
inventoryPanel->setFixedWidth(350);
QVBoxLayout *inventoryLayout = new QVBoxLayout(inventoryPanel);
inventoryLayout->setSpacing(10);
QLabel *inventoryLabel = new QLabel("Inventory", this);
inventoryLabel->setStyleSheet("QLabel { color: #FFFFFF; font-size: 24px; font-weight: bold; }");
inventoryLabel->setAlignment(Qt::AlignCenter);
inventoryLayout->addWidget(inventoryLabel);
// Inventory grid (6x9 slots)
QGridLayout *inventoryGrid = new QGridLayout();
inventoryGrid->setSpacing(5);
const int inventoryRows = 9;
const int inventoryColumns = 6;
for (int row = 0; row < inventoryRows; ++row) {
for (int col = 0; col < inventoryColumns; ++col) {
ItemSlot *slot = new ItemSlot(this);
inventoryGrid->addWidget(slot, row, col);
}
}
inventoryLayout->addLayout(inventoryGrid);
// Right panel: Crafting area
QWidget *craftingPanel = new QWidget(this);
QVBoxLayout *craftingLayout = new QVBoxLayout(craftingPanel);
craftingLayout->setSpacing(20);
QLabel *craftingLabel = new QLabel("Crafting", this);
craftingLabel->setStyleSheet("QLabel { color: #FFFFFF; font-size: 24px; font-weight: bold; }");
craftingLabel->setAlignment(Qt::AlignCenter);
craftingLayout->addWidget(craftingLabel);
// Crafting grid (3x3 slots)
QGridLayout *craftingGrid = new QGridLayout();
craftingGrid->setSpacing(5);
const int craftingRows = 3;
const int craftingColumns = 3;
for (int row = 0; row < craftingRows; ++row) {
for (int col = 0; col < craftingColumns; ++col) {
ItemSlot *slot = new ItemSlot(this);
craftingGrid->addWidget(slot, row, col);
}
}
craftingLayout->addLayout(craftingGrid);
// Crafting output slot
QLabel *outputLabel = new QLabel("Output", this);
outputLabel->setStyleSheet("QLabel { color: #FFFFFF; font-size: 18px; }");
outputLabel->setAlignment(Qt::AlignCenter);
ItemSlot *outputSlot = new ItemSlot(this);
outputSlot->setStyleSheet("QLabel { border: 2px solid #FFD700; background-color: #2E2E2E; }");
craftingLayout->addWidget(outputLabel);
craftingLayout->addWidget(outputSlot, 0, Qt::AlignCenter);
// Crafting queue
QLabel *queueLabel = new QLabel("Crafting Queue", this);
queueLabel->setStyleSheet("QLabel { color: #FFFFFF; font-size: 18px; }");
queueLabel->setAlignment(Qt::AlignCenter);
QListWidget *queueList = new QListWidget(this);
queueList->setFixedHeight(150);
queueList->setStyleSheet("QListWidget { background-color: #1E1E1E; color: #FFFFFF; }");
craftingLayout->addWidget(queueLabel);
craftingLayout->addWidget(queueList);
// Add panels to the main layout
mainLayout->addWidget(inventoryPanel);
mainLayout->addWidget(craftingPanel);
// Set the main window stylesheet
setStyleSheet("QWidget { background-color: #1E1E1E; }");
}
Styling and Sizing Details
- Window Size: Set to
1024x768 pixels for a standard resolution.
- Main Layout Margins:
10 pixels padding around the content.
- Inventory Panel:
- Width:
350 pixels to accommodate the inventory grid.
- Inventory Label: Font size
24px, white color, bold.
- Inventory Grid:
- Rows:
9, Columns: 6.
- Slot Spacing:
5 pixels between slots.
- Crafting Panel:
- Spacing:
20 pixels between elements.
- Crafting Label: Same styling as inventory label.
- Crafting Grid:
- Rows:
3, Columns: 3.
- Slot Spacing:
5 pixels between slots.
- Output Slot:
- Label: Font size
18px, white color.
- Slot Styling: Gold border (
#FFD700), to highlight it.
- Crafting Queue:
- Label: Same styling as output label.
- List Widget: Height
150 pixels, dark background (#1E1E1E), white text.
- Item Slots:
- Size:
60x60 pixels.
- Border:
1px solid dark gray (#555).
- Background: Dark gray (
#2E2E2E).
- Alignment: Centered content.
- Global Styling:
- Background Color: Very dark gray (
#1E1E1E), matching the game's UI.
- Text Color: White (
#FFFFFF) for contrast.
- Fonts: Default system font, adjusted sizes where specified.
Compilation Instructions
-
Create a Project File (CraftingUI.pro):
QT += widgets
SOURCES += \
main.cpp \
CraftingUI.cpp
HEADERS += \
CraftingUI.h
-
Compile the Application:
Open a terminal in the project directory and run:
qmake
make
-
Run the Application:
./CraftingUI
Or on Windows:
CraftingUI.exe
Explanation
The provided code creates a Qt application with a GUI resembling the crafting interface in 7 Days to Die.
-
Structure:
- The main window is split into two panels using a horizontal box layout (
QHBoxLayout).
- The Inventory Panel on the left contains a grid of item slots representing the player's inventory.
- The Crafting Panel on the right contains the crafting grid, output slot, and crafting queue.
-
Custom Widgets:
ItemSlot is a subclass of QLabel representing individual slots.
- Styling is applied directly within the class and when instances are created.
-
Styling:
- CSS-like stylesheets are used to set colors, borders, and fonts.
- Consistent styling ensures a cohesive look similar to the game's interface.
-
Sizing:
- Exact pixel sizes are used for slots (
60x60 pixels) and spacings to control the layout precisely.
Next Steps
To make the UI functional:
-
Item Management:
- Implement item classes to represent different inventory items.
- Load item icons and display them within
ItemSlot widgets.
-
Interactivity:
- Allow drag-and-drop between inventory slots and crafting grid slots.
- Handle clicks and other user interactions.
-
Crafting Logic:
- Define crafting recipes and implement logic to check if the placed items match any recipe.
- Update the output slot with the crafted item when a valid recipe is detected.
-
Crafting Queue:
- Add functionality to queue items for crafting.
- Implement timers or processes to simulate crafting time.
-
Error Handling:
- Provide feedback for invalid actions (e.g., placing incorrect items).
Conclusion
This code provides a solid foundation for a crafting UI in C++. By expanding upon this, you can create a fully functional crafting system similar to 7 Days to Die.
Feel free to ask if you need further assistance or additional features added!