A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat: Add AssetBrowserPanel to editor
esengine812f3a193a90
diff --git a/src/esengine/editor/CMakeLists.txt b/src/esengine/editor/CMakeLists.txtindex d33ec2249..3065f5728 100644--- a/src/esengine/editor/CMakeLists.txt+++ b/src/esengine/editor/CMakeLists.txt@@ -19,6 +19,8 @@ set(EDITOR_SOURCESpanels/HierarchyPanel.cpppanels/InspectorPanel.cpppanels/SceneViewPanel.cpp+ panels/AssetBrowserPanel.cpp+ panels/AssetGridItem.cpp)set(EDITOR_HEADERS@@ -50,6 +52,9 @@ set(EDITOR_HEADERSpanels/HierarchyPanel.hpppanels/InspectorPanel.hpppanels/SceneViewPanel.hpp+ panels/AssetBrowserPanel.hpp+ panels/AssetBrowserTypes.hpp+ panels/AssetGridItem.hpp)# Create editor executablediff --git a/src/esengine/editor/EditorApplication.cpp b/src/esengine/editor/EditorApplication.cppindex a35d82f51..9eb97210b 100644--- a/src/esengine/editor/EditorApplication.cpp+++ b/src/esengine/editor/EditorApplication.cpp@@ -26,6 +26,7 @@#include "panels/HierarchyPanel.hpp"#include "panels/InspectorPanel.hpp"#include "panels/SceneViewPanel.hpp"+#include "panels/AssetBrowserPanel.hpp"namespace esengine {namespace editor {@@ -309,11 +310,18 @@ void EditorApplication::setupEditorLayout() {ES_LOG_INFO("setupEditorLayout: Adding InspectorPanel to dock...");dockArea_->addPanel(std::move(inspectorPanel), ui::DockDropZone::Right, nullptr, 0.25f);+ ES_LOG_INFO("setupEditorLayout: Creating AssetBrowserPanel...");+ // Create Asset Browser panel (bottom)+ auto assetBrowserPanel = makeUnique<AssetBrowserPanel>();+ assetBrowserPanel->setMinSize(glm::vec2(300.0f, 200.0f));+ ES_LOG_INFO("setupEditorLayout: Adding AssetBrowserPanel to dock...");+ dockArea_->addPanel(std::move(assetBrowserPanel), ui::DockDropZone::Bottom, nullptr, 0.25f);+ES_LOG_INFO("setupEditorLayout: Setting root widget...");// Set dock area as the root widgetuiContext_->setRoot(std::move(dockArea));- ES_LOG_INFO("Editor layout initialized with Hierarchy, SceneView, and Inspector panels");+ ES_LOG_INFO("Editor layout initialized with Hierarchy, SceneView, Inspector, and AssetBrowser panels");}void EditorApplication::createDemoScene() {diff --git a/src/esengine/editor/panels/AssetBrowserPanel.cpp b/src/esengine/editor/panels/AssetBrowserPanel.cppnew file mode 100644index 000000000..326a62f2d--- /dev/null+++ b/src/esengine/editor/panels/AssetBrowserPanel.cpp@@ -0,0 +1,318 @@+#include "AssetBrowserPanel.hpp"+#include "AssetGridItem.hpp"+#include "../../ui/UIContext.hpp"+#include "../../ui/layout/StackLayout.hpp"+#include "../../ui/widgets/Label.hpp"+#include "../../platform/FileSystem.hpp"+#include "../../core/Log.hpp"+#include "../../events/Sink.hpp"++#include <algorithm>+#include <cctype>++namespace esengine::editor {++AssetBrowserPanel::AssetBrowserPanel()+ : DockPanel(ui::WidgetId("asset_browser_panel"), "Assets") {++ setPanelType("AssetBrowser");+ setClosable(true);+ setMinSize(glm::vec2(400.0f, 250.0f));++ rootPath_ = "assets";+ currentPath_ = rootPath_;++ buildUI();++#ifndef ES_PLATFORM_WEB+ rebuildFolderTree();+ refreshAssetList();+#endif+}++AssetBrowserPanel::~AssetBrowserPanel() = default;++void AssetBrowserPanel::refresh() {+ needsRebuild_ = true;+}++void AssetBrowserPanel::setRootPath(const std::string& path) {+ if (rootPath_ != path) {+ rootPath_ = path;+ currentPath_ = rootPath_;+ rebuildFolderTree();+ refreshAssetList();+ }+}++void AssetBrowserPanel::setViewMode(AssetViewMode mode) {+ if (viewMode_ != mode) {+ viewMode_ = mode;+ refreshAssetList();+ }+}++void AssetBrowserPanel::onActivated() {+ if (needsRebuild_) {+ rebuildFolderTree();+ refreshAssetList();+ needsRebuild_ = false;+ }+}++void AssetBrowserPanel::buildUI() {+ auto rootPanel = makeUnique<ui::Panel>(ui::WidgetId(getId().path + "_root"));+ rootPanel->setLayout(makeUnique<ui::StackLayout>(ui::StackDirection::Horizontal, 4.0f));+ rootPanel->setDrawBackground(false);++ auto leftPanel = makeUnique<ui::Panel>(ui::WidgetId(getId().path + "_left"));+ leftPanel->setWidth(ui::SizeValue::px(200.0f));+ leftPanel->setHeight(ui::SizeValue::flex(1.0f));+ leftPanel->setDrawBackground(true);+ leftPanel->setPadding(ui::Insets::all(4.0f));++ auto folderTree = makeUnique<ui::TreeView>(ui::WidgetId(getId().path + "_folder_tree"));+ folderTree->setMultiSelect(false);+ folderTree->setRowHeight(22.0f);+ folderTree->setIndentSize(16.0f);+ folderTree->setWidth(ui::SizeValue::flex(1.0f));+ folderTree->setHeight(ui::SizeValue::flex(1.0f));+ folderTree_ = folderTree.get();+ leftPanel->addChild(std::move(folderTree));++ leftPanel_ = leftPanel.get();+ rootPanel->addChild(std::move(leftPanel));++ auto rightPanel = makeUnique<ui::Panel>(ui::WidgetId(getId().path + "_right"));+ rightPanel->setWidth(ui::SizeValue::flex(1.0f));+ rightPanel->setHeight(ui::SizeValue::flex(1.0f));+ rightPanel->setLayout(makeUnique<ui::StackLayout>(ui::StackDirection::Vertical, 4.0f));+ rightPanel->setDrawBackground(false);++ auto toolbar = makeUnique<ui::Panel>(ui::WidgetId(getId().path + "_toolbar"));+ toolbar->setHeight(ui::SizeValue::px(32.0f));+ toolbar->setWidth(ui::SizeValue::flex(1.0f));+ toolbar->setPadding(ui::Insets::symmetric(4.0f, 4.0f));+ toolbar->setDrawBackground(true);++ auto searchField = makeUnique<ui::TextField>(ui::WidgetId(getId().path + "_search"));+ searchField->setPlaceholder("Search...");+ searchField->setWidth(ui::SizeValue::px(200.0f));+ searchField->setHeight(ui::SizeValue::px(24.0f));+ searchField_ = searchField.get();+ toolbar->addChild(std::move(searchField));++ rightPanel->addChild(std::move(toolbar));++ auto scrollView = makeUnique<ui::ScrollView>(ui::WidgetId(getId().path + "_scroll"));+ scrollView->setScrollDirection(ui::ScrollDirection::Vertical);+ scrollView->setWidth(ui::SizeValue::flex(1.0f));+ scrollView->setHeight(ui::SizeValue::flex(1.0f));++ auto gridPanel = makeUnique<ui::Panel>(ui::WidgetId(getId().path + "_grid"));+ gridPanel->setDrawBackground(false);+ gridPanel->setWidth(ui::SizeValue::flex(1.0f));+ gridPanel->setHeight(ui::SizeValue::autoSize());+ assetGridPanel_ = gridPanel.get();+ scrollView->setContent(std::move(gridPanel));++ assetScrollView_ = scrollView.get();+ rightPanel->addChild(std::move(scrollView));++ rightPanel_ = rightPanel.get();+ rootPanel->addChild(std::move(rightPanel));++ setContent(std::move(rootPanel));++ folderSelectedConnection_ = sink(folderTree_->onNodeSelected).connect(+ [this](ui::TreeNodeId nodeId) { onFolderSelected(nodeId); }+ );++ searchChangedConnection_ = sink(searchField_->onTextChanged).connect(+ [this](const std::string& text) { onSearchTextChanged(text); }+ );+}++void AssetBrowserPanel::rebuildFolderTree() {+#ifdef ES_PLATFORM_WEB+ auto label = makeUnique<ui::Label>(ui::WidgetId(getId().path + "_web_msg"), "N/A on Web");+ label->setFontSize(12.0f);+ leftPanel_->clearChildren();+ leftPanel_->addChild(std::move(label));+ return;+#endif++ folderTree_->clear();+ nodeToPath_.clear();+ pathToNode_.clear();++ if (!FileSystem::directoryExists(rootPath_)) {+ ES_LOG_WARN("Asset root path does not exist: {}", rootPath_);+ return;+ }++ ui::TreeNodeId rootNodeId = folderTree_->addNode(ui::INVALID_TREE_NODE, "assets");+ nodeToPath_[rootNodeId] = rootPath_;+ pathToNode_[rootPath_] = rootNodeId;++ addFolderNodes(rootPath_, rootNodeId);++ folderTree_->setNodeExpanded(rootNodeId, true);+ folderTree_->selectNode(rootNodeId);+}++void AssetBrowserPanel::addFolderNodes(const std::string& path, ui::TreeNodeId parentNode) {+#ifdef ES_PLATFORM_WEB+ return;+#endif++ std::vector<std::string> entries = FileSystem::listDirectory(path, false);++ for (const auto& entry : entries) {+ if (FileSystem::directoryExists(entry)) {+ std::string folderName = getFileName(entry);++ ui::TreeNodeId nodeId = folderTree_->addNode(parentNode, folderName);+ nodeToPath_[nodeId] = entry;+ pathToNode_[entry] = nodeId;++ addFolderNodes(entry, nodeId);+ }+ }+}++void AssetBrowserPanel::refreshAssetList() {+#ifdef ES_PLATFORM_WEB+ return;+#endif++ currentAssets_.clear();+ assetGridPanel_->clearChildren();++ if (!FileSystem::directoryExists(currentPath_)) {+ return;+ }++ std::vector<std::string> files = FileSystem::listDirectory(currentPath_, false);++ for (const auto& filePath : files) {+ AssetEntry entry;+ entry.path = filePath;+ entry.name = getFileName(filePath);+ entry.extension = getFileExtension(filePath);+ entry.isDirectory = FileSystem::directoryExists(filePath);++ if (entry.isDirectory) {+ entry.type = AssetType::Folder;+ } else {+ entry.type = getAssetTypeFromExtension(entry.extension);+ entry.fileSize = FileSystem::getFileSize(filePath);+ entry.modificationTime = FileSystem::getFileModificationTime(filePath);+ }++ if (!searchFilter_.empty()) {+ std::string lowerName = toLower(entry.name);+ std::string lowerFilter = toLower(searchFilter_);+ if (lowerName.find(lowerFilter) == std::string::npos) {+ continue;+ }+ }++ currentAssets_.push_back(entry);+ }++ std::sort(currentAssets_.begin(), currentAssets_.end(),+ [](const AssetEntry& a, const AssetEntry& b) {+ if (a.isDirectory != b.isDirectory) {+ return a.isDirectory;+ }+ return a.name < b.name;+ });++ for (usize i = 0; i < currentAssets_.size(); ++i) {+ const auto& entry = currentAssets_[i];++ auto item = makeUnique<AssetGridItem>(+ ui::WidgetId::indexed(assetGridPanel_->getId().path, "item", static_cast<u32>(i)),+ entry+ );++ (void)sink(item->onClick).connect([this](const std::string& path) {+ onAssetItemClicked(path);+ });++ (void)sink(item->onDoubleClick).connect([this](const std::string& path) {+ onAssetItemDoubleClicked(path);+ });++ assetGridPanel_->addChild(std::move(item));+ }+}++void AssetBrowserPanel::onFolderSelected(ui::TreeNodeId nodeId) {+ auto it = nodeToPath_.find(nodeId);+ if (it != nodeToPath_.end()) {+ if (currentPath_ != it->second) {+ currentPath_ = it->second;+ refreshAssetList();+ }+ }+}++void AssetBrowserPanel::onSearchTextChanged(const std::string& text) {+ searchFilter_ = text;+ refreshAssetList();+}++void AssetBrowserPanel::onAssetItemClicked(const std::string& path) {+ selectedAssetPath_ = path;++ for (auto& child : assetGridPanel_->getChildren()) {+ auto* item = dynamic_cast<AssetGridItem*>(child.get());+ if (item) {+ item->setSelected(item->getEntry().path == path);+ }+ }++ onAssetSelected.publish(path);+}++void AssetBrowserPanel::onAssetItemDoubleClicked(const std::string& path) {+ if (FileSystem::directoryExists(path)) {+ currentPath_ = path;+ refreshAssetList();++ auto it = pathToNode_.find(path);+ if (it != pathToNode_.end()) {+ folderTree_->selectNode(it->second);+ folderTree_->setNodeExpanded(it->second, true);+ }+ } else {+ onAssetDoubleClicked.publish(path);+ }+}++std::string AssetBrowserPanel::getFileName(const std::string& path) const {+ usize pos = path.find_last_of("/\\");+ if (pos != std::string::npos) {+ return path.substr(pos + 1);+ }+ return path;+}++std::string AssetBrowserPanel::getFileExtension(const std::string& path) const {+ usize pos = path.find_last_of('.');+ if (pos != std::string::npos) {+ return path.substr(pos);+ }+ return "";+}++std::string AssetBrowserPanel::toLower(const std::string& str) const {+ std::string result = str;+ std::transform(result.begin(), result.end(), result.begin(),+ [](unsigned char c) { return static_cast<char>(std::tolower(c)); });+ return result;+}++} // namespace esengine::editordiff --git a/src/esengine/editor/panels/AssetBrowserPanel.hpp b/src/esengine/editor/panels/AssetBrowserPanel.hppnew file mode 100644index 000000000..9f8b02581--- /dev/null+++ b/src/esengine/editor/panels/AssetBrowserPanel.hpp@@ -0,0 +1,72 @@+#pragma once++#include "../../ui/docking/DockPanel.hpp"+#include "../../ui/widgets/TreeView.hpp"+#include "../../ui/widgets/ScrollView.hpp"+#include "../../ui/widgets/TextField.hpp"+#include "../../ui/widgets/Panel.hpp"+#include "../../events/Connection.hpp"+#include "AssetBrowserTypes.hpp"++#include <unordered_map>+#include <vector>++namespace esengine::editor {++class AssetBrowserPanel : public ui::DockPanel {+public:+ AssetBrowserPanel();+ ~AssetBrowserPanel() override;++ void refresh();+ void setRootPath(const std::string& path);+ const std::string& getRootPath() const { return rootPath_; }++ void setViewMode(AssetViewMode mode);+ AssetViewMode getViewMode() const { return viewMode_; }++ Signal<void(const std::string&)> onAssetSelected;+ Signal<void(const std::string&)> onAssetDoubleClicked;++protected:+ void onActivated() override;++private:+ void buildUI();+ void rebuildFolderTree();+ void addFolderNodes(const std::string& path, ui::TreeNodeId parentNode);+ void refreshAssetList();++ void onFolderSelected(ui::TreeNodeId nodeId);+ void onSearchTextChanged(const std::string& text);+ void onAssetItemClicked(const std::string& path);+ void onAssetItemDoubleClicked(const std::string& path);++ std::string getFileName(const std::string& path) const;+ std::string getFileExtension(const std::string& path) const;+ std::string toLower(const std::string& str) const;++ std::string rootPath_;+ std::string currentPath_;+ AssetViewMode viewMode_ = AssetViewMode::Grid;++ ui::Panel* leftPanel_ = nullptr;+ ui::Panel* rightPanel_ = nullptr;+ ui::TreeView* folderTree_ = nullptr;+ ui::TextField* searchField_ = nullptr;+ ui::ScrollView* assetScrollView_ = nullptr;+ ui::Panel* assetGridPanel_ = nullptr;++ std::vector<AssetEntry> currentAssets_;+ std::unordered_map<ui::TreeNodeId, std::string> nodeToPath_;+ std::unordered_map<std::string, ui::TreeNodeId> pathToNode_;+ std::string searchFilter_;++ std::string selectedAssetPath_;+ bool needsRebuild_ = false;++ Connection folderSelectedConnection_;+ Connection searchChangedConnection_;+};++} // namespace esengine::editordiff --git a/src/esengine/editor/panels/AssetBrowserTypes.hpp b/src/esengine/editor/panels/AssetBrowserTypes.hppnew file mode 100644index 000000000..7c2034d56--- /dev/null+++ b/src/esengine/editor/panels/AssetBrowserTypes.hpp@@ -0,0 +1,75 @@+#pragma once++#include "../../core/Types.hpp"++#include <string>++namespace esengine::editor {++enum class AssetType : u8 {+ Unknown,+ Folder,+ Texture,+ Audio,+ Script,+ Shader,+ Scene,+ Prefab,+ Font+};++enum class AssetViewMode : u8 {+ Grid,+ List+};++struct AssetEntry {+ std::string name;+ std::string path;+ std::string extension;+ AssetType type = AssetType::Unknown;+ usize fileSize = 0;+ u64 modificationTime = 0;+ bool isDirectory = false;+};++inline AssetType getAssetTypeFromExtension(const std::string& ext) {+ if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".bmp" || ext == ".tga") {+ return AssetType::Texture;+ }+ if (ext == ".wav" || ext == ".mp3" || ext == ".ogg") {+ return AssetType::Audio;+ }+ if (ext == ".js" || ext == ".ts") {+ return AssetType::Script;+ }+ if (ext == ".glsl" || ext == ".vert" || ext == ".frag") {+ return AssetType::Shader;+ }+ if (ext == ".scene" || ext == ".scene.json") {+ return AssetType::Scene;+ }+ if (ext == ".prefab" || ext == ".prefab.json") {+ return AssetType::Prefab;+ }+ if (ext == ".ttf" || ext == ".otf") {+ return AssetType::Font;+ }+ return AssetType::Unknown;+}++inline glm::vec4 getAssetTypeColor(AssetType type) {+ switch (type) {+ case AssetType::Folder: return {0.9f, 0.8f, 0.3f, 1.0f};+ case AssetType::Texture: return {0.3f, 0.7f, 0.9f, 1.0f};+ case AssetType::Audio: return {0.9f, 0.5f, 0.3f, 1.0f};+ case AssetType::Script: return {0.5f, 0.9f, 0.5f, 1.0f};+ case AssetType::Shader: return {0.8f, 0.5f, 0.9f, 1.0f};+ case AssetType::Scene: return {0.3f, 0.9f, 0.9f, 1.0f};+ case AssetType::Prefab: return {0.9f, 0.3f, 0.6f, 1.0f};+ case AssetType::Font: return {0.7f, 0.7f, 0.7f, 1.0f};+ default: return {0.5f, 0.5f, 0.5f, 1.0f};+ }+}++} // namespace esengine::editordiff --git a/src/esengine/editor/panels/AssetGridItem.cpp b/src/esengine/editor/panels/AssetGridItem.cppnew file mode 100644index 000000000..c01a3c4e9--- /dev/null+++ b/src/esengine/editor/panels/AssetGridItem.cpp@@ -0,0 +1,106 @@+#include "AssetGridItem.hpp"+#include "../../ui/UIContext.hpp"+#include "../../ui/font/Font.hpp"+#include "../../ui/rendering/UIBatchRenderer.hpp"++#include <chrono>++namespace esengine::editor {++namespace {+ f64 getCurrentTimeSeconds() {+ auto now = std::chrono::steady_clock::now();+ auto duration = now.time_since_epoch();+ return std::chrono::duration<f64>(duration).count();+ }+} // namespace++AssetGridItem::AssetGridItem(const ui::WidgetId& id, const AssetEntry& entry)+ : Widget(id), entry_(entry) {+ setWidth(ui::SizeValue::px(ITEM_WIDTH));+ setHeight(ui::SizeValue::px(ITEM_HEIGHT));+}++void AssetGridItem::setSelected(bool selected) {+ selected_ = selected;+}++glm::vec2 AssetGridItem::measure(f32 availableWidth, f32 availableHeight) {+ (void)availableWidth;+ (void)availableHeight;+ return {ITEM_WIDTH, ITEM_HEIGHT};+}++void AssetGridItem::render(ui::UIBatchRenderer& renderer) {+ ui::UIContext* ctx = getContext();+ if (!ctx) return;++ ui::Rect bounds = getBounds();+ const auto& theme = ctx->getTheme();++ if (selected_) {+ renderer.drawRoundedRect(+ bounds,+ theme.colors.selection,+ ui::CornerRadii::all(4.0f)+ );+ } else if (isHovered()) {+ renderer.drawRoundedRect(+ bounds,+ theme.colors.buttonHover,+ ui::CornerRadii::all(4.0f)+ );+ }++ f32 iconX = bounds.x + (bounds.width - ICON_SIZE) * 0.5f;+ f32 iconY = bounds.y + ICON_PADDING;+ ui::Rect iconBounds{iconX, iconY, ICON_SIZE, ICON_SIZE};++ glm::vec4 iconColor = getAssetTypeColor(entry_.type);+ renderer.drawRoundedRect(iconBounds, iconColor, ui::CornerRadii::all(6.0f));++ ui::Font* font = ctx->getDefaultFont();+ if (font) {+ ui::Rect labelBounds{+ bounds.x + 4.0f,+ bounds.y + ICON_PADDING + ICON_SIZE + 4.0f,+ bounds.width - 8.0f,+ LABEL_HEIGHT+ };++ glm::vec4 textColor = theme.colors.textPrimary;+ renderer.drawTextInBounds(+ entry_.name,+ labelBounds,+ *font,+ 11.0f,+ textColor,+ ui::HAlign::Center,+ ui::VAlign::Top+ );+ }+}++bool AssetGridItem::onMouseDown(const ui::MouseButtonEvent& event) {+ if (event.button == ui::MouseButton::Left) {+ f64 currentTime = getCurrentTimeSeconds();+ constexpr f64 DOUBLE_CLICK_THRESHOLD = 0.3;++ if (currentTime - lastClickTime_ < DOUBLE_CLICK_THRESHOLD) {+ onDoubleClick.publish(entry_.path);+ lastClickTime_ = 0.0;+ } else {+ onClick.publish(entry_.path);+ lastClickTime_ = currentTime;+ }+ return true;+ }+ return false;+}++bool AssetGridItem::onMouseUp(const ui::MouseButtonEvent& event) {+ (void)event;+ return false;+}++} // namespace esengine::editordiff --git a/src/esengine/editor/panels/AssetGridItem.hpp b/src/esengine/editor/panels/AssetGridItem.hppnew file mode 100644index 000000000..748c5b125--- /dev/null+++ b/src/esengine/editor/panels/AssetGridItem.hpp@@ -0,0 +1,40 @@+#pragma once++#include "../../ui/widgets/Widget.hpp"+#include "../../events/Signal.hpp"+#include "AssetBrowserTypes.hpp"++#include <glm/glm.hpp>++namespace esengine::editor {++class AssetGridItem : public ui::Widget {+public:+ static constexpr f32 ITEM_WIDTH = 80.0f;+ static constexpr f32 ITEM_HEIGHT = 100.0f;+ static constexpr f32 ICON_SIZE = 48.0f;+ static constexpr f32 ICON_PADDING = 16.0f;+ static constexpr f32 LABEL_HEIGHT = 36.0f;++ AssetGridItem(const ui::WidgetId& id, const AssetEntry& entry);++ const AssetEntry& getEntry() const { return entry_; }++ void setSelected(bool selected);+ bool isSelected() const { return selected_; }++ Signal<void(const std::string&)> onClick;+ Signal<void(const std::string&)> onDoubleClick;++ glm::vec2 measure(f32 availableWidth, f32 availableHeight) override;+ void render(ui::UIBatchRenderer& renderer) override;+ bool onMouseDown(const ui::MouseButtonEvent& event) override;+ bool onMouseUp(const ui::MouseButtonEvent& event) override;++private:+ AssetEntry entry_;+ bool selected_ = false;+ f64 lastClickTime_ = 0.0;+};++} // namespace esengine::editor