// Copyright (c) 2008-2023 the Urho3D project // License: MIT #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "WindowSettingsDemo.h" #include URHO3D_DEFINE_APPLICATION_MAIN(WindowSettingsDemo) WindowSettingsDemo::WindowSettingsDemo(Context* context) : Sample(context) , uiRoot_(GetSubsystem()->GetRoot()) { } void WindowSettingsDemo::Start() { // Execute base class startup Sample::Start(); // Enable OS cursor GetSubsystem()->SetMouseVisible(true); // Load XML file containing default UI style sheet auto* cache = GetSubsystem(); auto* style = cache->GetResource("UI/DefaultStyle.xml"); // Set the loaded style as default style uiRoot_->SetDefaultStyle(style); // Create window with settings. InitSettings(); SynchronizeSettings(); SubscribeToEvent(E_SCREENMODE, [this](StringHash /*eventType*/, const VariantMap& /*eventData*/) { SynchronizeSettings(); }); // Set the mouse mode to use in the sample Sample::InitMouseMode(MM_FREE); // Create scene CreateScene(); // Setup viewport auto* renderer = GetSubsystem(); SharedPtr viewport(new Viewport(context_, scene_, cameraNode_->GetComponent())); renderer->SetViewport(0, viewport); } void WindowSettingsDemo::CreateScene() { auto* cache = GetSubsystem(); scene_ = new Scene(context_); scene_->CreateComponent(); auto* zone = scene_->CreateComponent(); zone->SetAmbientColor(Color::WHITE); // Create 3D object Node* objectNode = scene_->CreateChild("Object"); objectNode->SetRotation(Quaternion(45.0f, 45.0f, 45.0f)); auto* objectModel = objectNode->CreateComponent(); objectModel->SetModel(cache->GetResource("Models/Box.mdl")); objectModel->SetMaterial(cache->GetResource("Materials/Stone.xml")); // Create camera cameraNode_ = scene_->CreateChild("Camera"); cameraNode_->CreateComponent(); cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -4.0f)); // Rotate object SubscribeToEvent(scene_, E_SCENEUPDATE, [objectNode](StringHash /*eventType*/, VariantMap& eventData) { const float timeStep = eventData[SceneUpdate::P_TIMESTEP].GetFloat(); objectNode->Rotate(Quaternion(0.0f, 20.0f * timeStep, 0.0f), TransformSpace::World); }); } void WindowSettingsDemo::InitSettings() { auto* graphics = GetSubsystem(); // Create the Window and add it to the UI's root node window_ = uiRoot_->CreateChild("Window"); // Set Window size and layout settings window_->SetPosition(128, 128); window_->SetMinWidth(300); window_->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6)); window_->SetMovable(true); window_->SetStyleAuto(); // Create the Window title Text auto* windowTitle = window_->CreateChild("WindowTitle"); windowTitle->SetText("Window Settings"); windowTitle->SetStyleAuto(); // Create monitor selector monitorControl_ = window_->CreateChild("Monitor"); monitorControl_->SetMinHeight(24); monitorControl_->SetStyleAuto(); for (int i = 0; i < graphics->GetMonitorCount(); ++i) { auto text = MakeShared(context_); text->SetText(ToString("Monitor %d", i)); text->SetMinWidth(CeilToInt(text->GetRowWidth(0) + 10)); monitorControl_->AddItem(text); text->SetStyleAuto(); } // Create resolution selector resolutionControl_ = window_->CreateChild("Resolution"); resolutionControl_->SetMinHeight(24); resolutionControl_->SetStyleAuto(); auto resolutionPlaceholder = MakeShared(context_); resolutionPlaceholder->SetText("[Cannot fill list of resolutions]"); resolutionPlaceholder->SetMinWidth(CeilToInt(resolutionPlaceholder->GetRowWidth(0) + 10)); resolutionControl_->AddItem(resolutionPlaceholder); resolutionPlaceholder->SetStyleAuto(); // Create fullscreen controller auto* fullscreenFrame = window_->CreateChild("Fullscreen Frame"); fullscreenFrame->SetMinHeight(24); fullscreenFrame->SetLayout(LM_HORIZONTAL, 6); fullscreenControl_ = fullscreenFrame->CreateChild("Fullscreen Control"); fullscreenControl_->SetStyleAuto(); auto* fullscreenText = fullscreenFrame->CreateChild("Fullscreen Label"); fullscreenText->SetText("Fullscreen"); fullscreenText->SetMinWidth(CeilToInt(fullscreenText->GetRowWidth(0) + 10)); fullscreenText->SetStyleAuto(); // Create borderless controller auto* borderlessFrame = window_->CreateChild("Borderless Frame"); borderlessFrame->SetMinHeight(24); borderlessFrame->SetLayout(LM_HORIZONTAL, 6); borderlessControl_ = borderlessFrame->CreateChild("Borderless Control"); borderlessControl_->SetStyleAuto(); auto* borderlessText = borderlessFrame->CreateChild("Borderless Label"); borderlessText->SetText("Borderless"); borderlessText->SetMinWidth(CeilToInt(borderlessText->GetRowWidth(0) + 10)); borderlessText->SetStyleAuto(); // Create resizable controller auto* resizableFrame = window_->CreateChild("Resizable Frame"); resizableFrame->SetMinHeight(24); resizableFrame->SetLayout(LM_HORIZONTAL, 6); resizableControl_ = resizableFrame->CreateChild("Resizable Control"); resizableControl_->SetStyleAuto(); auto* resizableText = resizableFrame->CreateChild("Resizable Label"); resizableText->SetText("Resizable"); resizableText->SetMinWidth(CeilToInt(resizableText->GetRowWidth(0) + 10)); resizableText->SetStyleAuto(); // Create resizable controller auto* vsyncFrame = window_->CreateChild("V-Sync Frame"); vsyncFrame->SetMinHeight(24); vsyncFrame->SetLayout(LM_HORIZONTAL, 6); vsyncControl_ = vsyncFrame->CreateChild("V-Sync Control"); vsyncControl_->SetStyleAuto(); auto* vsyncText = vsyncFrame->CreateChild("V-Sync Label"); vsyncText->SetText("V-Sync"); vsyncText->SetMinWidth(CeilToInt(vsyncText->GetRowWidth(0) + 10)); vsyncText->SetStyleAuto(); // Create multi-sample controller from 1 (= 2^0) to 16 (= 2^4) multiSampleControl_ = window_->CreateChild("Multi-Sample Control"); multiSampleControl_->SetMinHeight(24); multiSampleControl_->SetStyleAuto(); for (int i = 0; i <= 4; ++i) { auto text = MakeShared(context_); text->SetText(i == 0 ? "No MSAA" : ToString("MSAA x%d", 1 << i)); text->SetMinWidth(CeilToInt(text->GetRowWidth(0) + 10)); multiSampleControl_->AddItem(text); text->SetStyleAuto(); } // Create "Apply" button auto* applyButton = window_->CreateChild