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 Checkbox widget

esengine821f87032731
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 24fb79b07..be315f085 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -48,6 +48,7 @@ set(ESENGINE_SOURCES
src/esengine/ui/widgets/ScrollView.cpp
src/esengine/ui/widgets/TreeView.cpp
src/esengine/ui/widgets/TextField.cpp
+ src/esengine/ui/widgets/Checkbox.cpp
# Docking System
src/esengine/ui/docking/DockNode.cpp
src/esengine/ui/docking/DockPanel.cpp
@@ -113,6 +114,7 @@ set(ESENGINE_HEADERS
src/esengine/ui/widgets/ScrollView.hpp
src/esengine/ui/widgets/TreeView.hpp
src/esengine/ui/widgets/TextField.hpp
+ src/esengine/ui/widgets/Checkbox.hpp
# Docking System
src/esengine/ui/docking/DockTypes.hpp
src/esengine/ui/docking/DockNode.hpp
diff --git a/src/esengine/ui/widgets/Checkbox.cpp b/src/esengine/ui/widgets/Checkbox.cpp
new file mode 100644
index 000000000..7233d5989
--- /dev/null
+++ b/src/esengine/ui/widgets/Checkbox.cpp
@@ -0,0 +1,192 @@
+/**
+ * @file Checkbox.cpp
+ * @brief Checkbox widget implementation
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+
+#include "Checkbox.hpp"
+#include "../UIContext.hpp"
+#include "../rendering/UIBatchRenderer.hpp"
+#include "../../math/Math.hpp"
+
+namespace esengine::ui {
+
+// =============================================================================
+// Constructor
+// =============================================================================
+
+Checkbox::Checkbox(const WidgetId& id) : Widget(id) {}
+
+// =============================================================================
+// State Management
+// =============================================================================
+
+void Checkbox::setChecked(bool checked) {
+ if (checked_ == checked) {
+ return;
+ }
+
+ checked_ = checked;
+ onChanged.publish(checked_);
+}
+
+void Checkbox::toggle() {
+ setChecked(!checked_);
+}
+
+// =============================================================================
+// Label
+// =============================================================================
+
+void Checkbox::setLabel(const std::string& label) {
+ if (label_ == label) {
+ return;
+ }
+
+ label_ = label;
+ invalidateLayout();
+}
+
+// =============================================================================
+// Appearance
+// =============================================================================
+
+void Checkbox::setCheckboxSize(f32 size) {
+ if (checkboxSize_ == size) {
+ return;
+ }
+
+ checkboxSize_ = size;
+ invalidateLayout();
+}
+
+// =============================================================================
+// Widget Interface
+// =============================================================================
+
+glm::vec2 Checkbox::measure(f32 availableWidth, f32 availableHeight) {
+ (void)availableHeight;
+
+ f32 width = checkboxSize_;
+ f32 height = checkboxSize_;
+
+ if (!label_.empty() && getContext() && getContext()->getDefaultFont()) {
+ f32 fontSize = 14.0f;
+ if (getContext()) {
+ fontSize = getContext()->getTheme().typography.fontSizeNormal;
+ }
+
+ f32 labelWidth = static_cast<f32>(label_.length()) * fontSize * 0.6f;
+ width += LABEL_SPACING + labelWidth;
+ height = glm::max(height, fontSize);
+ }
+
+ const SizeConstraints& constraints = getConstraints();
+ width = glm::clamp(width, constraints.minWidth, constraints.maxWidth);
+ height = glm::clamp(height, constraints.minHeight, constraints.maxHeight);
+
+ return glm::vec2(width, height);
+}
+
+void Checkbox::render(UIBatchRenderer& renderer) {
+ const Rect& bounds = getBounds();
+ const Insets& padding = getPadding();
+
+ WidgetStyle style;
+ if (getContext()) {
+ style = getContext()->getTheme().getButtonStyle();
+ }
+
+ WidgetState state{
+ .hovered = isHovered(),
+ .pressed = isPressed(),
+ .focused = false,
+ .disabled = !isEnabled(),
+ .visible = true
+ };
+
+ f32 checkboxX = bounds.x + padding.left;
+ f32 checkboxY = bounds.y + (bounds.height - checkboxSize_) * 0.5f;
+
+ Rect checkboxBounds{checkboxX, checkboxY, checkboxSize_, checkboxSize_};
+
+ glm::vec4 bgColor = checked_ ? style.getBackgroundColor(state) : glm::vec4(0.12f, 0.12f, 0.12f, 1.0f);
+ glm::vec4 borderColor = state.hovered ? glm::vec4(0.4f, 0.4f, 0.4f, 1.0f) : glm::vec4(0.3f, 0.3f, 0.3f, 1.0f);
+
+ if (getContext()) {
+ if (checked_) {
+ bgColor = getContext()->getTheme().colors.accent;
+ } else {
+ bgColor = getContext()->getTheme().colors.input;
+ }
+ borderColor = state.hovered
+ ? getContext()->getTheme().colors.inputBorderFocused
+ : getContext()->getTheme().colors.inputBorder;
+ }
+
+ CornerRadii radii = CornerRadii::all(4.0f);
+ renderer.drawRoundedRect(checkboxBounds, bgColor, radii);
+ renderer.drawRoundedRectOutline(checkboxBounds, borderColor, radii, 1.0f);
+
+ if (checked_) {
+ glm::vec4 checkColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
+ if (getContext()) {
+ checkColor = getContext()->getTheme().colors.textPrimary;
+ }
+
+ f32 checkSize = checkboxSize_ * 0.6f;
+ f32 checkX = checkboxX + (checkboxSize_ - checkSize) * 0.5f;
+ f32 checkY = checkboxY + (checkboxSize_ - checkSize) * 0.5f;
+
+ f32 x1 = checkX + checkSize * 0.2f;
+ f32 y1 = checkY + checkSize * 0.5f;
+ f32 x2 = checkX + checkSize * 0.45f;
+ f32 y2 = checkY + checkSize * 0.8f;
+ f32 x3 = checkX + checkSize * 0.9f;
+ f32 y3 = checkY + checkSize * 0.2f;
+
+ renderer.drawLine(glm::vec2(x1, y1), glm::vec2(x2, y2), checkColor, 2.0f);
+ renderer.drawLine(glm::vec2(x2, y2), glm::vec2(x3, y3), checkColor, 2.0f);
+ }
+
+ if (!label_.empty()) {
+ f32 fontSize = 14.0f;
+ glm::vec4 textColor = glm::vec4(0.95f, 0.95f, 0.95f, 1.0f);
+
+ if (getContext()) {
+ fontSize = getContext()->getTheme().typography.fontSizeNormal;
+ textColor = state.disabled
+ ? getContext()->getTheme().colors.textDisabled
+ : getContext()->getTheme().colors.textPrimary;
+ }
+
+ f32 labelX = checkboxX + checkboxSize_ + LABEL_SPACING;
+ f32 labelY = bounds.y + (bounds.height - fontSize) * 0.5f;
+
+ if (getContext() && getContext()->getDefaultFont()) {
+ renderer.drawText(label_, glm::vec2(labelX, labelY),
+ *getContext()->getDefaultFont(), fontSize, textColor);
+ }
+ }
+}
+
+bool Checkbox::onMouseDown(const MouseButtonEvent& event) {
+ if (event.button != MouseButton::Left) {
+ return false;
+ }
+
+ const Rect& bounds = getBounds();
+ if (!bounds.contains(event.x, event.y)) {
+ return false;
+ }
+
+ toggle();
+ return true;
+}
+
+} // namespace esengine::ui
diff --git a/src/esengine/ui/widgets/Checkbox.hpp b/src/esengine/ui/widgets/Checkbox.hpp
new file mode 100644
index 000000000..58d3cdb88
--- /dev/null
+++ b/src/esengine/ui/widgets/Checkbox.hpp
@@ -0,0 +1,134 @@
+/**
+ * @file Checkbox.hpp
+ * @brief Checkbox widget for boolean input
+ * @details Provides a clickable checkbox with optional label text.
+ * Emits onChanged signal when the checked state toggles.
+ *
+ * @author ESEngine Team
+ * @date 2026
+ *
+ * @copyright Copyright (c) 2026 ESEngine Team
+ * Licensed under the MIT License.
+ */
+#pragma once
+
+// =============================================================================
+// Includes
+// =============================================================================
+
+#include "Widget.hpp"
+#include "../../events/Signal.hpp"
+
+#include <string>
+
+namespace esengine::ui {
+
+// =============================================================================
+// Checkbox Class
+// =============================================================================
+
+/**
+ * @brief Checkbox widget for boolean selection
+ *
+ * @details Renders a checkbox with optional label. Clicking toggles the
+ * checked state and emits the onChanged signal.
+ *
+ * @code
+ * auto checkbox = makeUnique<Checkbox>(WidgetId("show_grid"));
+ * checkbox->setLabel("Show Grid");
+ * checkbox->setChecked(true);
+ * checkbox->onChanged.connect([](bool checked) {
+ * ES_LOG_INFO("Grid visibility: {}", checked);
+ * });
+ * @endcode
+ */
+class Checkbox : public Widget {
+public:
+ /**
+ * @brief Constructs a checkbox widget
+ * @param id Unique widget identifier
+ */
+ explicit Checkbox(const WidgetId& id);
+
+ ~Checkbox() override = default;
+
+ // =========================================================================
+ // State Management
+ // =========================================================================
+
+ /**
+ * @brief Sets the checked state
+ * @param checked True to check, false to uncheck
+ */
+ void setChecked(bool checked);
+
+ /**
+ * @brief Gets the checked state
+ * @return True if checked
+ */
+ bool isChecked() const { return checked_; }
+
+ /**
+ * @brief Toggles the checked state
+ */
+ void toggle();
+
+ // =========================================================================
+ // Label
+ // =========================================================================
+
+ /**
+ * @brief Sets the label text
+ * @param label Text to display next to checkbox
+ */
+ void setLabel(const std::string& label);
+
+ /**
+ * @brief Gets the label text
+ * @return Current label text
+ */
+ const std::string& getLabel() const { return label_; }
+
+ // =========================================================================
+ // Appearance
+ // =========================================================================
+
+ /**
+ * @brief Sets the checkbox size
+ * @param size Size in pixels (default 16.0f)
+ */
+ void setCheckboxSize(f32 size);
+
+ /**
+ * @brief Gets the checkbox size
+ * @return Checkbox size in pixels
+ */
+ f32 getCheckboxSize() const { return checkboxSize_; }
+
+ // =========================================================================
+ // Widget Interface
+ // =========================================================================
+
+ glm::vec2 measure(f32 availableWidth, f32 availableHeight) override;
+ void render(class UIBatchRenderer& renderer) override;
+
+ bool onMouseDown(const MouseButtonEvent& event) override;
+
+ // =========================================================================
+ // Signals
+ // =========================================================================
+
+ /**
+ * @brief Emitted when the checked state changes
+ * @param checked New checked state
+ */
+ Signal<void(bool)> onChanged;
+
+private:
+ bool checked_ = false;
+ std::string label_;
+ f32 checkboxSize_ = 16.0f;
+ static constexpr f32 LABEL_SPACING = 8.0f;
+};
+
+} // namespace esengine::ui