claw-code/.guardrails/examples/cpp/unreal-ui/CommonUI.cpp

303 lines
8.6 KiB
C++

/*
* CommonUI.cpp - Unreal Engine 5.4+ CommonUI Example
*
* Production-ready example demonstrating:
* - UE5 CommonUI plugin patterns
* - DDA 3.0 integration
* - Accessibility patterns (WCAG 3.0+)
* - Spatial computing (XR, eye-tracking)
* - Ethical engagement mechanics
* - Core loop integration (Action/Reward/Upgrade)
*
* © 2026 - Agent Guardrails Template
*/
#include "CoreMinimal.h"
#include "CommonUI/CommonUserWidget.h"
#include "CommonUI/CommonUISubsystem.h"
#include "DDA/DDAManager.h"
#include "Accessibility/AccessibilityPreferences.h"
#include "Ethical/EthicalEngagement.h"
#include "Spatial/VolumetricRenderer.h"
namespace AgentGuardrails::UnrealUI
{
/**
* Main CommonUI Controller for UE5 5.4+
* Integrates core loop phases with CommonUI widgets
*/
class UCommonUIController : public UCommonUserWidget
{
GENERATED_BODY()
private:
DDAManager* ddaManager;
AccessibilityPreferences* accessibilityPrefs;
EthicalEngagement* ethicalEngagement;
VolumetricRenderer* volumetricRenderer;
UCommonUserWidget* combatHUDWidget;
UCommonUserWidget* lootPanelWidget;
UCommonUserWidget* skillTreeWidget;
UCommonUserWidget* restStateIndicatorWidget;
public:
virtual void Initialize() override
{
Super::Initialize();
// Initialize DDA 3.0 system
ddaManager = DDAManager::GetInstance();
// Initialize accessibility preferences
accessibilityPrefs = AccessibilityPreferences::GetInstance();
// Initialize ethical engagement system
ethicalEngagement = EthicalEngagement::GetInstance();
// Initialize volumetric renderer
volumetricRenderer = VolumetricRenderer::GetInstance();
// Build UI hierarchy
BuildUIHierarchy();
}
/**
* Builds UI hierarchy with CommonUI widgets
* Core loop: Action → Reward → Upgrade
*/
void BuildUIHierarchy()
{
auto* rootPanel = NewObject<UCommonUserWidget>();
rootPanel->SetFlexDirection(EFlexDirection::Vertical);
// Combat HUD - Action Phase
combatHUDWidget = CreateCombatHUD();
combatHUDWidget->SetFlexGrow(1.0f);
rootPanel->AddChild(combatHUDWidget);
// Loot Panel - Reward Phase
lootPanelWidget = CreateLootPanel();
lootPanelWidget->SetFlexGrow(1.0f);
rootPanel->AddChild(lootPanelWidget);
// Skill Tree - Upgrade Phase
skillTreeWidget = CreateSkillTree();
skillTreeWidget->SetFlexGrow(1.0f);
rootPanel->AddChild(skillTreeWidget);
// Rest State Indicator - Ethical Engagement
restStateIndicatorWidget = CreateRestStateIndicator();
restStateIndicatorWidget->SetFlexGrow(0.5f);
rootPanel->AddChild(restStateIndicatorWidget);
// Apply accessibility styles
ApplyAccessibilityStyles(rootPanel);
}
/**
* Creates Combat HUD with DDA 3.0 visual adaptation
* Reactive health bars with shape+color redundancy
*/
UCommonUserWidget* CreateCombatHUD()
{
auto* hudPanel = NewObject<UCommonUserWidget>();
hudPanel->SetFlexDirection(EFlexDirection::Horizontal);
// Player health bar - WCAG 3.0+ compliance
auto* playerHealthBar = CreateHealthBar("PlayerHealth");
playerHealthBar->SetHealthState(EHealthState::Normal);
playerHealthBar->SetShapeIndicator(true); // Colorblindness independence
playerHealthBar->SetOpacity(ddaManager->GetUIOpacityFactor());
// Eye-tracking dwell indicator
playerHealthBar->SetDwellThreshold(150.0f); // 150ms
hudPanel->AddChild(playerHealthBar);
// Enemy health bar
auto* enemyHealthBar = CreateHealthBar("EnemyHealth");
enemyHealthBar->SetDDATier(ddaManager->GetCurrentTier());
hudPanel->AddChild(enemyHealthBar);
return hudPanel;
}
/**
* Creates Loot Panel with transparent RNG display
* Ethical engagement: no obfuscated drop rates
*/
UCommonUserWidget* CreateLootPanel()
{
auto* lootPanel = NewObject<UCommonUserWidget>();
lootPanel->SetFlexDirection(EFlexDirection::Vertical);
// Transparent loot table display
auto* lootTableLabel = NewObject<UCommonUserTextBlock>();
lootTableLabel->SetText(FText::FromString("Loot Table Transparency"));
lootPanel->AddChild(lootTableLabel);
// Inline drop rate display for each item
auto* currentLootTable = LootTableManager::GetCurrentTable();
for (auto* item : currentLootTable->GetItems())
{
auto* itemRow = NewObject<UCommonUserWidget>();
itemRow->AddChild(NewObject<UCommonUserTextBlock>()->SetText(item->GetName()));
itemRow->AddChild(NewObject<UCommonUserTextBlock>()->SetText(
FText::FromString(FString::Printf(TEXT("Drop Rate: %.2f%%"), item->GetDropRate()))
));
lootPanel->AddChild(itemRow);
}
// Pity timer visualization
auto* pityTimerProgress = NewObject<UCommonUserProgressBar>();
pityTimerProgress->SetValue(currentLootTable->GetPityTimerCount());
pityTimerProgress->SetMaxValue(currentLootTable->GetPityTimerMax());
lootPanel->AddChild(pityTimerProgress);
// Volumetric UI for legendary items
if (currentLootTable->IsLegendary())
{
volumetricRenderer->SetDepth(0.5f); // Z-axis parallax
lootPanel->AddChild(volumetricRenderer->CreateVolumetricWidget());
}
return lootPanel;
}
/**
* Creates Skill Tree with Z-depth parallax
* Spatial computing: holographic skill nodes
*/
UCommonUserWidget* CreateSkillTree()
{
auto* skillTreePanel = NewObject<UCommonUserWidget>();
skillTreePanel->SetFlexDirection(EFlexDirection::Vertical);
// Z-depth parallax for skill nodes
skillTreePanel->SetParallaxFactor(0.3f);
// GPU instancing for skill node rendering
skillTreePanel->EnableGPUInstancing(true);
return skillTreePanel;
}
/**
* Creates Rest State Indicator for ethical engagement
* Mandatory calm state after intense sessions
*/
UCommonUserWidget* CreateRestStateIndicator()
{
auto* restStatePanel = NewObject<UCommonUserWidget>();
restStatePanel->SetBackgroundColor(FColor(45, 74, 45)); // Calm green
auto* restStateLabel = NewObject<UCommonUserTextBlock>();
restStateLabel->SetText(FText::FromString("Rest State Activated - Calm Mode"));
restStatePanel->AddChild(restStateLabel);
// Timer for rest state trigger
int32 restStateTimer = 0;
restStatePanel->RegisterOnUpdate([restStateTimer, this]() {
restStateTimer++;
if (GameManager::GetSessionDuration() > 45 && restStateTimer >= 5)
{
ethicalEngagement->EnterRestState();
}
});
return restStatePanel;
}
/**
* Applies WCAG 3.0+ accessibility styles
* Minimum contrast ratio: 4.5:1 (Level AA)
*/
void ApplyAccessibilityStyles(UCommonUserWidget* rootPanel)
{
// High contrast mode for colorblindness
if (accessibilityPrefs->GetColorblindnessMode() != EColorblindnessMode::None)
{
rootPanel->SetBackgroundColor(FColor::White);
rootPanel->SetBackgroundBrush("high-contrast-border");
}
// Shape + icon redundancy for all interactive elements
rootPanel->SetShapeRedundance(true);
// Eye-tracking support: dwell-based selection
rootPanel->SetDwellThreshold(150.0f);
}
/**
* Triggers haptic feedback for UI interactions
* Scales intensity with DDA 3.0 tier
*/
void TriggerHapticFeedback(EUIEvent event)
{
auto hapticProfile = HapticProfile::FromEvent(event);
HapticFeedback::ApplyProfile(hapticProfile);
}
};
// Component definitions
USTRUCT()
struct FUIElementComponent
{
GENERATED_BODY()
FString ID;
bool IsInteractive;
bool IsPooled;
};
USTRUCT()
struct FLayoutComponent
{
GENERATED_BODY()
FVector Position;
FVector2D Size;
float Depth;
};
USTRUCT()
struct FVolumetricUIComponent
{
GENERATED_BODY()
float Depth;
bool IsHolographic;
float ParallaxFactor;
};
// Enumeration definitions
UENUM()
enum class EHealthState
{
Normal,
Critical
};
UENUM()
enum class EColorblindnessMode
{
None,
Protanopia,
Deuteranopia,
Tritanopia
};
UENUM()
enum class EUIEvent
{
CombatTrigger,
CombatSuccess,
LootReveal,
SkillUnlock,
LevelUp
};
} // namespace AgentGuardrails::UnrealUI