GroveEngine/modules/UIModule/Widgets/UITextInput.h
StillHammer 579cadeae8 feat: Complete UIModule Phase 7 - ScrollPanel & Tooltips
This commit implements Phase 7 of the UIModule, adding advanced features
that make the UI system production-ready.

## Phase 7.1 - UIScrollPanel

New scrollable container widget with:
- Vertical and horizontal scrolling (configurable)
- Mouse wheel support with smooth scrolling
- Drag-to-scroll functionality (drag content or scrollbar)
- Interactive scrollbar with proportional thumb
- Automatic content size calculation
- Visibility culling for performance
- Full styling support (colors, borders, scrollbar)

Files added:
- modules/UIModule/Widgets/UIScrollPanel.h
- modules/UIModule/Widgets/UIScrollPanel.cpp
- modules/UIModule/Core/UIContext.h (added mouseWheelDelta)
- modules/UIModule/UIModule.cpp (mouse wheel event routing)

## Phase 7.2 - Tooltips

Smart tooltip system with:
- Hover delay (500ms default)
- Automatic positioning with edge avoidance
- Semi-transparent background with border
- Per-widget tooltip text via JSON
- Tooltip property on all UIWidget types
- Renders on top of all UI elements

Files added:
- modules/UIModule/Core/UITooltip.h
- modules/UIModule/Core/UITooltip.cpp
- modules/UIModule/Core/UIWidget.h (added tooltip property)
- modules/UIModule/Core/UITree.cpp (tooltip parsing)

## Tests

Added comprehensive visual tests:
- test_28_ui_scroll.cpp - ScrollPanel with 35+ items
- test_29_ui_advanced.cpp - Tooltips on various widgets
- assets/ui/test_scroll.json - ScrollPanel layout
- assets/ui/test_tooltips.json - Tooltips layout

## Documentation

- docs/UI_MODULE_PHASE7_COMPLETE.md - Complete Phase 7 docs
- docs/PROMPT_UI_MODULE_PHASE6.md - Phase 6 & 7 prompt
- Updated CMakeLists.txt for new files and tests

## UIModule Status

UIModule is now feature-complete with:
 9 widget types (Panel, Label, Button, Image, Slider, Checkbox,
   ProgressBar, TextInput, ScrollPanel)
 Flexible layout system (vertical, horizontal, stack, absolute)
 Theme and style system
 Complete event system
 Tooltips with smart positioning
 Hot-reload support
 Comprehensive tests (Phases 1-7)

🚀 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 07:13:13 +08:00

189 lines
4.8 KiB
C++

#pragma once
#include "../Core/UIWidget.h"
#include <cstdint>
#include <string>
namespace grove {
/**
* @brief Text input filter types
*/
enum class TextInputFilter {
None, // No filtering
Alphanumeric, // Letters and numbers only
Numeric, // Numbers only (int)
Float, // Numbers with decimal point
NoSpaces // No whitespace characters
};
/**
* @brief Text input visual state
*/
enum class TextInputState {
Normal,
Focused,
Disabled
};
/**
* @brief Style properties for text input
*/
struct TextInputStyle {
uint32_t bgColor = 0x222222FF;
uint32_t textColor = 0xFFFFFFFF;
uint32_t placeholderColor = 0x888888FF;
uint32_t cursorColor = 0xFFFFFFFF;
uint32_t selectionColor = 0x4444AAAA;
uint32_t borderColor = 0x666666FF;
uint32_t focusBorderColor = 0x4488FFFF;
float borderWidth = 2.0f;
};
/**
* @brief Single-line text input widget
*
* Features:
* - Text editing with cursor
* - Text selection (future)
* - Input filtering (numbers only, max length, etc.)
* - Password mode (mask characters)
* - Horizontal scroll for long text
* - Placeholder text
* - Copy/paste (future)
*
* Events Published:
* - ui:text_changed → {widgetId, text}
* - ui:text_submit → {widgetId, text} (Enter pressed)
* - ui:focus_gained → {widgetId}
* - ui:focus_lost → {widgetId}
*/
class UITextInput : public UIWidget {
public:
UITextInput() = default;
~UITextInput() override = default;
void update(UIContext& ctx, float deltaTime) override;
void render(UIRenderer& renderer) override;
std::string getType() const override { return "textinput"; }
/**
* @brief Check if a point is inside this text input
*/
bool containsPoint(float px, float py) const;
/**
* @brief Handle mouse button event (for focus)
* @return true if event was consumed
*/
bool onMouseButton(int button, bool pressed, float x, float y);
/**
* @brief Handle keyboard input when focused
* @param keyCode Key code
* @param character Unicode character (if printable)
* @param ctrl Ctrl key modifier
* @return true if event was consumed
*/
bool onKeyInput(int keyCode, uint32_t character, bool ctrl);
/**
* @brief Gain focus (start receiving keyboard input)
*/
void gainFocus();
/**
* @brief Lose focus (stop receiving keyboard input)
*/
void loseFocus();
/**
* @brief Insert text at cursor position
*/
void insertText(const std::string& str);
/**
* @brief Delete character before cursor (backspace)
*/
void deleteCharBefore();
/**
* @brief Delete character after cursor (delete)
*/
void deleteCharAfter();
/**
* @brief Move cursor left/right
*/
void moveCursor(int offset);
/**
* @brief Set cursor to specific position
*/
void setCursorPosition(int pos);
/**
* @brief Get visible text with scroll offset applied
*/
std::string getVisibleText() const;
/**
* @brief Calculate pixel offset for cursor
*/
float getCursorPixelOffset() const;
// Text input properties
std::string text;
std::string placeholder = "Enter text...";
int maxLength = 256;
TextInputFilter filter = TextInputFilter::None;
bool passwordMode = false;
bool enabled = true;
float fontSize = 16.0f;
std::string onSubmit; // Action to publish on Enter
// State-specific styles
TextInputStyle normalStyle;
TextInputStyle focusedStyle;
TextInputStyle disabledStyle;
// Current state
TextInputState state = TextInputState::Normal;
bool isFocused = false;
int cursorPosition = 0; // Index in text string
float scrollOffset = 0.0f; // Horizontal scroll for long text
// Cursor blink animation
float cursorBlinkTimer = 0.0f;
bool cursorVisible = true;
static constexpr float CURSOR_BLINK_INTERVAL = 0.5f;
// Text measurement (approximate)
static constexpr float CHAR_WIDTH = 8.0f; // Average character width
static constexpr float CURSOR_WIDTH = 2.0f;
static constexpr float PADDING = 8.0f;
private:
/**
* @brief Get the appropriate style for current state
*/
const TextInputStyle& getCurrentStyle() const;
/**
* @brief Check if character passes filter
*/
bool passesFilter(uint32_t ch) const;
/**
* @brief Get display text (masked if password mode)
*/
std::string getDisplayText() const;
/**
* @brief Update scroll offset to keep cursor visible
*/
void updateScrollOffset();
};
} // namespace grove