Skip to content

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.

feat: Add text input support and expand key code mappings

esengine9d2e24938caf
diff --git a/src/esengine/editor/EditorApplication.cpp b/src/esengine/editor/EditorApplication.cpp
index 3699ed03b..278d2de8c 100644
--- a/src/esengine/editor/EditorApplication.cpp
+++ b/src/esengine/editor/EditorApplication.cpp
@@ -97,6 +97,13 @@ void EditorApplication::onInit() {
}
});
+ // Wire up text input events to UI system
+ getPlatform().setTextInputCallback([this](const std::string& text) {
+ if (uiContext_) {
+ uiContext_->processTextInput(text);
+ }
+ });
+
// Initialize asset database with thumbnail generation callback
assetDatabase_.setOnAssetAdded([this](const AssetMetadata& asset) {
thumbnailGenerator_.generateThumbnail(asset.guid, asset.path, asset.type);
diff --git a/src/esengine/platform/Platform.hpp b/src/esengine/platform/Platform.hpp
index d4ccb475b..4dc4b0d75 100644
--- a/src/esengine/platform/Platform.hpp
+++ b/src/esengine/platform/Platform.hpp
@@ -21,6 +21,7 @@
// Standard library
#include <functional>
+#include <string>
namespace esengine {
@@ -132,6 +133,12 @@ using ResizeCallback = std::function<void(u32 width, u32 height)>;
*/
using ScrollCallback = std::function<void(f32 deltaX, f32 deltaY, f32 x, f32 y)>;
+/**
+ * @brief Callback type for text input events
+ * @param text UTF-8 encoded text input
+ */
+using TextInputCallback = std::function<void(const std::string& text)>;
+
// =============================================================================
// Platform Class
// =============================================================================
@@ -289,6 +296,12 @@ public:
*/
virtual void setScrollCallback(ScrollCallback callback) = 0;
+ /**
+ * @brief Sets the text input callback
+ * @param callback Function to call on text input events
+ */
+ virtual void setTextInputCallback(TextInputCallback callback) = 0;
+
// =========================================================================
// Factory
// =========================================================================
diff --git a/src/esengine/platform/native/NativePlatform.cpp b/src/esengine/platform/native/NativePlatform.cpp
index 9c93e44ce..1334a2afb 100644
--- a/src/esengine/platform/native/NativePlatform.cpp
+++ b/src/esengine/platform/native/NativePlatform.cpp
@@ -132,6 +132,7 @@ bool NativePlatform::initialize(u32 width, u32 height) {
glfwSetMouseButtonCallback(window_, glfwMouseButtonCallback);
glfwSetCursorPosCallback(window_, glfwCursorPosCallback);
glfwSetScrollCallback(window_, glfwScrollCallback);
+ glfwSetCharCallback(window_, glfwCharCallback);
glfwSetFramebufferSizeCallback(window_, glfwFramebufferSizeCallback);
glfwSetWindowCloseCallback(window_, glfwWindowCloseCallback);
@@ -259,6 +260,10 @@ void NativePlatform::setScrollCallback(ScrollCallback callback) {
scrollCallback_ = std::move(callback);
}
+void NativePlatform::setTextInputCallback(TextInputCallback callback) {
+ textInputCallback_ = std::move(callback);
+}
+
// =============================================================================
// GLFW Callbacks
// =============================================================================
@@ -334,6 +339,31 @@ void NativePlatform::glfwScrollCallback(GLFWwindow* window, double xoffset, doub
}
}
+void NativePlatform::glfwCharCallback(GLFWwindow* window, unsigned int codepoint) {
+ auto* platform = static_cast<NativePlatform*>(glfwGetWindowUserPointer(window));
+ if (!platform || !platform->textInputCallback_) return;
+
+ // Convert UTF-32 codepoint to UTF-8 string
+ std::string utf8;
+ if (codepoint < 0x80) {
+ utf8 += static_cast<char>(codepoint);
+ } else if (codepoint < 0x800) {
+ utf8 += static_cast<char>(0xC0 | (codepoint >> 6));
+ utf8 += static_cast<char>(0x80 | (codepoint & 0x3F));
+ } else if (codepoint < 0x10000) {
+ utf8 += static_cast<char>(0xE0 | (codepoint >> 12));
+ utf8 += static_cast<char>(0x80 | ((codepoint >> 6) & 0x3F));
+ utf8 += static_cast<char>(0x80 | (codepoint & 0x3F));
+ } else {
+ utf8 += static_cast<char>(0xF0 | (codepoint >> 18));
+ utf8 += static_cast<char>(0x80 | ((codepoint >> 12) & 0x3F));
+ utf8 += static_cast<char>(0x80 | ((codepoint >> 6) & 0x3F));
+ utf8 += static_cast<char>(0x80 | (codepoint & 0x3F));
+ }
+
+ platform->textInputCallback_(utf8);
+}
+
void NativePlatform::glfwFramebufferSizeCallback(GLFWwindow* window, int width, int height) {
auto* platform = static_cast<NativePlatform*>(glfwGetWindowUserPointer(window));
if (!platform) return;
@@ -373,26 +403,34 @@ void NativePlatform::glfwWindowCloseCallback(GLFWwindow* window) {
KeyCode NativePlatform::convertKeyCode(int glfwKey) {
switch (glfwKey) {
- case GLFW_KEY_SPACE: return KeyCode::Space;
- case GLFW_KEY_ENTER: return KeyCode::Enter;
- case GLFW_KEY_ESCAPE: return KeyCode::Escape;
- case GLFW_KEY_LEFT: return KeyCode::Left;
- case GLFW_KEY_UP: return KeyCode::Up;
- case GLFW_KEY_RIGHT: return KeyCode::Right;
- case GLFW_KEY_DOWN: return KeyCode::Down;
- case GLFW_KEY_A: return KeyCode::A;
- case GLFW_KEY_D: return KeyCode::D;
- case GLFW_KEY_S: return KeyCode::S;
- case GLFW_KEY_W: return KeyCode::W;
- case GLFW_KEY_Y: return KeyCode::Y;
- case GLFW_KEY_Z: return KeyCode::Z;
- case GLFW_KEY_LEFT_SHIFT: return KeyCode::LeftShift;
- case GLFW_KEY_RIGHT_SHIFT: return KeyCode::RightShift;
+ case GLFW_KEY_SPACE: return KeyCode::Space;
+ case GLFW_KEY_ENTER: return KeyCode::Enter;
+ case GLFW_KEY_ESCAPE: return KeyCode::Escape;
+ case GLFW_KEY_BACKSPACE: return KeyCode::Backspace;
+ case GLFW_KEY_DELETE: return KeyCode::Delete;
+ case GLFW_KEY_TAB: return KeyCode::Tab;
+ case GLFW_KEY_HOME: return KeyCode::Home;
+ case GLFW_KEY_END: return KeyCode::End;
+ case GLFW_KEY_LEFT: return KeyCode::Left;
+ case GLFW_KEY_UP: return KeyCode::Up;
+ case GLFW_KEY_RIGHT: return KeyCode::Right;
+ case GLFW_KEY_DOWN: return KeyCode::Down;
+ case GLFW_KEY_A: return KeyCode::A;
+ case GLFW_KEY_C: return KeyCode::C;
+ case GLFW_KEY_D: return KeyCode::D;
+ case GLFW_KEY_S: return KeyCode::S;
+ case GLFW_KEY_V: return KeyCode::V;
+ case GLFW_KEY_W: return KeyCode::W;
+ case GLFW_KEY_X: return KeyCode::X;
+ case GLFW_KEY_Y: return KeyCode::Y;
+ case GLFW_KEY_Z: return KeyCode::Z;
+ case GLFW_KEY_LEFT_SHIFT: return KeyCode::LeftShift;
+ case GLFW_KEY_RIGHT_SHIFT: return KeyCode::RightShift;
case GLFW_KEY_LEFT_CONTROL: return KeyCode::LeftControl;
case GLFW_KEY_RIGHT_CONTROL: return KeyCode::RightControl;
- case GLFW_KEY_LEFT_ALT: return KeyCode::LeftAlt;
- case GLFW_KEY_RIGHT_ALT: return KeyCode::RightAlt;
- default: return KeyCode::Unknown;
+ case GLFW_KEY_LEFT_ALT: return KeyCode::LeftAlt;
+ case GLFW_KEY_RIGHT_ALT: return KeyCode::RightAlt;
+ default: return KeyCode::Unknown;
}
}
diff --git a/src/esengine/platform/native/NativePlatform.hpp b/src/esengine/platform/native/NativePlatform.hpp
index 64eac8d3b..241160175 100644
--- a/src/esengine/platform/native/NativePlatform.hpp
+++ b/src/esengine/platform/native/NativePlatform.hpp
@@ -169,6 +169,12 @@ public:
*/
void setScrollCallback(ScrollCallback callback) override;
+ /**
+ * @brief Sets text input callback
+ * @param callback Function to call on character input
+ */
+ void setTextInputCallback(TextInputCallback callback) override;
+
private:
// =========================================================================
// GLFW Callbacks
@@ -179,6 +185,7 @@ private:
static void glfwMouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
static void glfwCursorPosCallback(GLFWwindow* window, double xpos, double ypos);
static void glfwScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
+ static void glfwCharCallback(GLFWwindow* window, unsigned int codepoint);
static void glfwFramebufferSizeCallback(GLFWwindow* window, int width, int height);
static void glfwWindowCloseCallback(GLFWwindow* window);
@@ -215,6 +222,7 @@ private:
KeyCallback keyCallback_; ///< Keyboard event callback
ResizeCallback resizeCallback_; ///< Window resize callback
ScrollCallback scrollCallback_; ///< Scroll event callback
+ TextInputCallback textInputCallback_; ///< Text input callback
static NativePlatform* instance_; ///< Singleton instance for callbacks
};
diff --git a/src/esengine/platform/web/WebPlatform.cpp b/src/esengine/platform/web/WebPlatform.cpp
index b978e920e..7b8797cac 100644
--- a/src/esengine/platform/web/WebPlatform.cpp
+++ b/src/esengine/platform/web/WebPlatform.cpp
@@ -148,6 +148,10 @@ public:
scrollCallback_ = std::move(callback);
}
+ void setTextInputCallback(TextInputCallback callback) override {
+ textInputCallback_ = std::move(callback);
+ }
+
// Static instance for callbacks
static WebPlatform* instance_;
@@ -274,6 +278,7 @@ private:
KeyCallback keyCallback_;
ResizeCallback resizeCallback_;
ScrollCallback scrollCallback_;
+ TextInputCallback textInputCallback_;
};
WebPlatform* WebPlatform::instance_ = nullptr;