void SoundEffects::CreateUI() { UIElement* root = GetSubsystem<UI>()->GetRoot(); auto* cache = GetSubsystem<ResourceCache>(); auto* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml"); // Set style to the UI root so that elements will inherit it root->SetDefaultStyle(uiStyle); // Create buttons for playing back sounds for (unsigned i = 0; i < NUM_SOUNDS; ++i) { Button* button = CreateButton(i * 140 + 20, 20, 120, 40, soundNames[i]); // Store the sound effect resource name as a custom variable into the button button->SetVar(VAR_SOUNDRESOURCE, soundResourceNames[i]); SubscribeToEvent(button, E_PRESSED, URHO3D_HANDLER(SoundEffects, HandlePlaySound)); } // Create buttons for playing/stopping music Button* button = CreateButton(20, 80, 120, 40, "Play Music"); SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SoundEffects, HandlePlayMusic)); button = CreateButton(160, 80, 120, 40, "Stop Music"); SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SoundEffects, HandleStopMusic)); auto* audio = GetSubsystem<Audio>(); // Create sliders for controlling sound and music master volume Slider* slider = CreateSlider(20, 140, 200, 20, "Sound Volume"); slider->SetValue(audio->GetMasterGain(SOUND_EFFECT)); SubscribeToEvent(slider, E_SLIDERCHANGED, URHO3D_HANDLER(SoundEffects, HandleSoundVolume)); slider = CreateSlider(20, 200, 200, 20, "Music Volume"); slider->SetValue(audio->GetMasterGain(SOUND_MUSIC)); SubscribeToEvent(slider, E_SLIDERCHANGED, URHO3D_HANDLER(SoundEffects, HandleMusicVolume)); }
void LANDiscovery::CreateUI() { SetLogoVisible(true); // We need the full rendering window auto* graphics = GetSubsystem<Graphics>(); UIElement* root = GetSubsystem<UI>()->GetRoot(); auto* cache = GetSubsystem<ResourceCache>(); auto* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml"); // Set style to the UI root so that elements will inherit it root->SetDefaultStyle(uiStyle); int marginTop = 20; CreateLabel("1. Start server", IntVector2(20, marginTop-20)); startServer_ = CreateButton("Start server", 160, IntVector2(20, marginTop)); stopServer_ = CreateButton("Stop server", 160, IntVector2(20, marginTop)); stopServer_->SetVisible(false); // Create client connection related fields marginTop += 80; CreateLabel("2. Discover LAN servers", IntVector2(20, marginTop-20)); refreshServerList_ = CreateButton("Search...", 160, IntVector2(20, marginTop)); marginTop += 80; CreateLabel("Local servers:", IntVector2(20, marginTop - 20)); serverList_ = CreateLabel("", IntVector2(20, marginTop)); // No viewports or scene is defined. However, the default zone's fog color controls the fill color GetSubsystem<Renderer>()->GetDefaultZone()->SetFogColor(Color(0.0f, 0.0f, 0.1f)); }
void UIDrag::CreateGUI() { auto* cache = GetSubsystem<ResourceCache>(); auto* ui = GetSubsystem<UI>(); UIElement* root = ui->GetRoot(); // Load the style sheet from xml root->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml")); for (int i=0; i < 10; i++) { auto* b = new Button(context_); root->AddChild(b); // Reference a style from the style sheet loaded earlier: b->SetStyleAuto(); b->SetMinWidth(250); b->SetPosition(IntVector2(50*i, 50*i)); // Enable the bring-to-front flag and set the initial priority b->SetBringToFront(true); b->SetPriority(i); // Set the layout mode to make the child text elements aligned vertically b->SetLayout(LM_VERTICAL, 20, {40, 40, 40, 40}); auto dragInfos = {"Num Touch", "Text", "Event Touch"}; for (auto name: dragInfos) b->CreateChild<Text>(name)->SetStyleAuto(); if (i % 2 == 0) b->AddTag("SomeTag"); SubscribeToEvent(b, E_CLICK, URHO3D_HANDLER(UIDrag, HandleClick)); SubscribeToEvent(b, E_DRAGMOVE, URHO3D_HANDLER(UIDrag, HandleDragMove)); SubscribeToEvent(b, E_DRAGBEGIN, URHO3D_HANDLER(UIDrag, HandleDragBegin)); SubscribeToEvent(b, E_DRAGCANCEL, URHO3D_HANDLER(UIDrag, HandleDragCancel)); } for (int i = 0; i < 10; i++) { auto* t = new Text(context_); root->AddChild(t); t->SetStyleAuto(); t->SetName("Touch "+ String(i)); t->SetVisible(false); t->SetPriority(100); // Make sure it has higher priority than the buttons } }
void SceneReplication::CreateUI() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); UIElement* root = ui->GetRoot(); XMLFile* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml"); // Set style to the UI root so that elements will inherit it root->SetDefaultStyle(uiStyle); // Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will // control the camera, and when visible, it can interact with the login UI SharedPtr<Cursor> cursor(new Cursor(context_)); cursor->SetStyleAuto(uiStyle); ui->SetCursor(cursor); // Set starting position of the cursor at the rendering window center Graphics* graphics = GetSubsystem<Graphics>(); cursor->SetPosition(graphics->GetWidth() / 2, graphics->GetHeight() / 2); // Construct the instructions text element instructionsText_ = ui->GetRoot()->CreateChild<Text>(); instructionsText_->SetText( "Use WASD keys to move and RMB to rotate view" ); instructionsText_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15); // Position the text relative to the screen center instructionsText_->SetHorizontalAlignment(HA_CENTER); instructionsText_->SetVerticalAlignment(VA_CENTER); instructionsText_->SetPosition(0, graphics->GetHeight() / 4); // Hide until connected instructionsText_->SetVisible(false); buttonContainer_ = root->CreateChild<UIElement>(); buttonContainer_->SetFixedSize(500, 20); buttonContainer_->SetPosition(20, 20); buttonContainer_->SetLayoutMode(LM_HORIZONTAL); textEdit_ = buttonContainer_->CreateChild<LineEdit>(); textEdit_->SetStyleAuto(); connectButton_ = CreateButton("Connect", 90); disconnectButton_ = CreateButton("Disconnect", 100); startServerButton_ = CreateButton("Start Server", 110); UpdateButtons(); }
void Chat::CreateUI() { SetLogoVisible(false); // We need the full rendering window Graphics* graphics = GetSubsystem<Graphics>(); UIElement* root = GetSubsystem<UI>()->GetRoot(); ResourceCache* cache = GetSubsystem<ResourceCache>(); XMLFile* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml"); // Set style to the UI root so that elements will inherit it root->SetDefaultStyle(uiStyle); Font* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"); chatHistoryText_ = root->CreateChild<Text>(); chatHistoryText_->SetFont(font, 12); buttonContainer_ = root->CreateChild<UIElement>(); buttonContainer_->SetFixedSize(graphics->GetWidth(), 20); buttonContainer_->SetPosition(0, graphics->GetHeight() - 20); buttonContainer_->SetLayoutMode(LM_HORIZONTAL); textEdit_ = buttonContainer_->CreateChild<LineEdit>(); textEdit_->SetStyleAuto(); sendButton_ = CreateButton("Send", 70); connectButton_ = CreateButton("Connect", 90); disconnectButton_ = CreateButton("Disconnect", 100); startServerButton_ = CreateButton("Start Server", 110); UpdateButtons(); int rowHeight = chatHistoryText_->GetRowHeight(); // Row height would be zero if the font failed to load if (rowHeight) chatHistory_.Resize((graphics->GetHeight() - 20) / rowHeight); // No viewports or scene is defined. However, the default zone's fog color controls the fill color GetSubsystem<Renderer>()->GetDefaultZone()->SetFogColor(Color(0.0f, 0.0f, 0.1f)); }
void UIDrag::CreateGUI() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); UIElement* root = ui->GetRoot(); // Load the style sheet from xml root->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml")); for (int i=0; i < 10; i++) { Button* b = new Button(context_); root->AddChild(b); // Reference a style from the style sheet loaded earlier: b->SetStyle("Button"); b->SetSize(300, 100); b->SetPosition(IntVector2(50*i, 50*i)); if (i % 2 == 0) b->AddTag("SomeTag"); SubscribeToEvent(b, E_DRAGMOVE, URHO3D_HANDLER(UIDrag, HandleDragMove)); SubscribeToEvent(b, E_DRAGBEGIN, URHO3D_HANDLER(UIDrag, HandleDragBegin)); SubscribeToEvent(b, E_DRAGCANCEL, URHO3D_HANDLER(UIDrag, HandleDragCancel)); SubscribeToEvent(b, E_DRAGEND, URHO3D_HANDLER(UIDrag, HandleDragEnd)); { Text* t = new Text(context_); b->AddChild(t); t->SetStyle("Text"); t->SetHorizontalAlignment(HA_CENTER); t->SetVerticalAlignment(VA_CENTER); t->SetName("Text"); } { Text* t = new Text(context_); b->AddChild(t); t->SetStyle("Text"); t->SetName("Event Touch"); t->SetHorizontalAlignment(HA_CENTER); t->SetVerticalAlignment(VA_BOTTOM); } { Text* t = new Text(context_); b->AddChild(t); t->SetStyle("Text"); t->SetName("Num Touch"); t->SetHorizontalAlignment(HA_CENTER); t->SetVerticalAlignment(VA_TOP); } } for (int i = 0; i < 10; i++) { Text* t = new Text(context_); root->AddChild(t); t->SetStyle("Text"); t->SetName("Touch "+ String(i)); t->SetVisible(false); } }
bool Menu::LoadXML(const XMLElement& source, XMLFile* styleFile, bool setInstanceDefault) { // Get style override if defined String styleName = source.GetAttribute("style"); // Apply the style first, if the style file is available if (styleFile) { // If not defined, use type name if (styleName.Empty()) styleName = GetTypeName(); SetStyle(styleName, styleFile); } // The 'style' attribute value in the style file cannot be equals to original's applied style to prevent infinite loop else if (!styleName.Empty() && styleName != appliedStyle_) { // Attempt to use the default style file styleFile = GetDefaultStyle(); if (styleFile) { // Remember the original applied style String appliedStyle(appliedStyle_); SetStyle(styleName, styleFile); appliedStyle_ = appliedStyle; } } // Then load rest of the attributes from the source if (!Serializable::LoadXML(source, setInstanceDefault)) return false; unsigned nextInternalChild = 0; // Load child elements. Internal elements are not to be created as they already exist XMLElement childElem = source.GetChild("element"); while (childElem) { bool internalElem = childElem.GetBool("internal"); bool popupElem = childElem.GetBool("popup"); String typeName = childElem.GetAttribute("type"); if (typeName.Empty()) typeName = "UIElement"; unsigned index = childElem.HasAttribute("index") ? childElem.GetUInt("index") : M_MAX_UNSIGNED; UIElement* child = 0; if (!internalElem) { if (!popupElem) child = CreateChild(typeName, String::EMPTY, index); else { // Do not add the popup element as a child even temporarily, as that can break layouts SharedPtr<UIElement> popup = DynamicCast<UIElement>(context_->CreateObject(typeName)); if (!popup) URHO3D_LOGERROR("Could not create popup element type " + typeName); else { child = popup; SetPopup(popup); } } } else { // An internal popup element should already exist if (popupElem) child = popup_; else { for (unsigned i = nextInternalChild; i < children_.Size(); ++i) { if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName) { child = children_[i]; nextInternalChild = i + 1; break; } } if (!child) URHO3D_LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName()); } } if (child) { if (!styleFile) styleFile = GetDefaultStyle(); // As popup is not a child element in itself, the parental chain to acquire the default style file is broken for popup's child elements // To recover from this, popup needs to have the default style set in its own instance so the popup's child elements can find it later if (popupElem) child->SetDefaultStyle(styleFile); if (!child->LoadXML(childElem, styleFile, setInstanceDefault)) return false; } childElem = childElem.GetNext("element"); } ApplyAttributes(); return true; }
void L10n::CreateGUI() { // Get localization subsystem auto* l10n = GetSubsystem<Localization>(); auto* cache = GetSubsystem<ResourceCache>(); UIElement* root = GetSubsystem<UI>()->GetRoot(); root->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml")); auto* window = new Window(context_); root->AddChild(window); window->SetMinSize(384, 192); window->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6)); window->SetAlignment(HA_CENTER, VA_CENTER); window->SetStyleAuto(); auto* windowTitle = new Text(context_); windowTitle->SetName("WindowTitle"); windowTitle->SetStyleAuto(); window->AddChild(windowTitle); // In this place the current language is "en" because it was found first when loading the JSON files ea::string langName = l10n->GetLanguage(); // Languages are numbered in the loading order int langIndex = l10n->GetLanguageIndex(); // == 0 at the beginning // Get string with identifier "title" in the current language ea::string localizedString = l10n->Get("title"); // Localization::Get returns EMPTY_STRING if the id is empty. // Localization::Get returns the id if translation is not found and will be added a warning into the log. windowTitle->SetText(localizedString + " (" + ea::to_string(langIndex) + " " + langName + ")"); auto* b = new Button(context_); window->AddChild(b); b->SetStyle("Button"); b->SetMinHeight(24); auto* t = b->CreateChild<Text>("ButtonTextChangeLang"); // The showing text value will automatically change when language is changed t->SetAutoLocalizable(true); // The text value used as a string identifier in this mode. // Remember that a letter case of the id and of the lang name is important. t->SetText("Press this button"); t->SetAlignment(HA_CENTER, VA_CENTER); t->SetStyle("Text"); SubscribeToEvent(b, E_RELEASED, URHO3D_HANDLER(L10n, HandleChangeLangButtonPressed)); b = new Button(context_); window->AddChild(b); b->SetStyle("Button"); b->SetMinHeight(24); t = b->CreateChild<Text>("ButtonTextQuit"); t->SetAlignment(HA_CENTER, VA_CENTER); t->SetStyle("Text"); // Manually set text in the current language t->SetText(l10n->Get("quit")); SubscribeToEvent(b, E_RELEASED, URHO3D_HANDLER(L10n, HandleQuitButtonPressed)); }
void GameMain::CreateMenu() { ResourceCache* cache = GetSubsystem<ResourceCache>(); UI* ui = GetSubsystem<UI>(); UIElement* root = ui->GetRoot(); //GetSubsystem<Input>()->SetMouseVisible(true); root->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml")); // New Game menu.btnNewGame = new Button(context_); root->AddChild(menu.btnNewGame); menu.btnNewGame->SetHorizontalAlignment(HA_CENTER); menu.btnNewGame->SetVerticalAlignment(VA_CENTER); menu.btnNewGame->SetName("NewGame"); menu.btnNewGame->SetStyleAuto(); menu.btnNewGame->SetSize(200, 50); menu.btnNewGame->SetVar("MENU", Variant(1)); menu.txtNewGame = new Text(context_); menu.btnNewGame->AddChild(menu.txtNewGame); menu.txtNewGame->SetStyleAuto(); menu.txtNewGame->SetHorizontalAlignment(HA_CENTER); menu.txtNewGame->SetVerticalAlignment(VA_CENTER); menu.txtNewGame->SetVisible(true); menu.txtNewGame->SetText("New game"); menu.txtNewGame->SetVar("MENU", Variant(1)); // options menu.btnOptions = new Button(context_); root->AddChild(menu.btnOptions); menu.btnOptions->SetHorizontalAlignment(HA_CENTER); menu.btnOptions->SetVerticalAlignment(VA_CENTER); menu.btnOptions->SetName("Options"); menu.btnOptions->SetStyleAuto(); menu.btnOptions->SetSize(200, 50); menu.btnOptions->SetPosition(menu.btnNewGame->GetPosition() + IntVector2(0, 55)); menu.btnOptions->SetVar("MENU", Variant(1)); menu.txtOptions = new Text(context_); menu.btnOptions->AddChild(menu.txtOptions); menu.txtOptions->SetStyleAuto(); menu.txtOptions->SetHorizontalAlignment(HA_CENTER); menu.txtOptions->SetVerticalAlignment(VA_CENTER); menu.txtOptions->SetVisible(true); menu.txtOptions->SetText("Options"); menu.txtOptions->SetVar("MENU", Variant(1)); //tone menu.optTone = new CheckBox(context_); root->AddChild(menu.optTone); menu.optTone->SetStyleAuto(); menu.optTone->SetName("Tone"); menu.optTone->SetVisible(true); menu.optTone->SetHorizontalAlignment(HA_CENTER); menu.optTone->SetVerticalAlignment(VA_CENTER); menu.optTone->SetPosition(menu.btnOptions->GetPosition() + IntVector2(120, 0)); menu.optTone->SetVar("MENU", Variant(1)); menu.txtTone = new Text(context_); menu.optTone->AddChild(menu.txtTone); menu.txtTone->SetStyleAuto(); menu.txtTone->SetHorizontalAlignment(HA_LEFT); menu.txtTone->SetVerticalAlignment(VA_CENTER); menu.txtTone->SetPosition(IntVector2(20, 0)); menu.txtTone->SetVisible(true); menu.txtTone->SetText("Tonemapping"); menu.txtTone->SetVar("MENU", Variant(1)); // Exit menu.btnExit = new Button(context_); root->AddChild(menu.btnExit); menu.btnExit->SetHorizontalAlignment(HA_CENTER); menu.btnExit->SetVerticalAlignment(VA_CENTER); menu.btnExit->SetStyleAuto(); menu.btnExit->SetSize(200, 50); menu.btnExit->SetName("Exit"); menu.btnExit->SetPosition(menu.btnOptions->GetPosition() + IntVector2(0, 55)); menu.btnExit->SetVar("MENU", Variant(1)); menu.txtExit = new Text(context_); menu.btnExit->AddChild(menu.txtExit); menu.txtExit->SetStyleAuto(); menu.txtExit->SetHorizontalAlignment(HA_CENTER); menu.txtExit->SetVerticalAlignment(VA_CENTER); //exitBtnText->SetName("Exit"); menu.txtExit->SetVisible(true); menu.txtExit->SetText("Exit"); menu.txtExit->SetVar("MENU", Variant(1)); GetSubsystem<Input>()->SetMouseVisible(true); menu.isActive = true; }
void Typography::Start() { // Execute base class startup Sample::Start(); // Enable OS cursor GetSubsystem<Input>()->SetMouseVisible(true); // Load XML file containing default UI style sheet auto* cache = GetSubsystem<ResourceCache>(); auto* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml"); // Set the loaded style as default style auto* ui = GetSubsystem<UI>(); UIElement* root = ui->GetRoot(); root->SetDefaultStyle(style); // Create a UIElement to hold all our content // (Don't modify the root directly, as the base Sample class uses it) uielement_ = new UIElement(context_); uielement_->SetAlignment(HA_CENTER, VA_CENTER); uielement_->SetLayout(LM_VERTICAL, 10, IntRect(20, 40, 20, 40)); root->AddChild(uielement_); // Add some sample text. CreateText(); // Add a checkbox to toggle the background color. CreateCheckbox("White background", URHO3D_HANDLER(Typography, HandleWhiteBackground)) ->SetChecked(false); // Add a checkbox to toggle SRGB output conversion (if available). // This will give more correct text output for FreeType fonts, as the FreeType rasterizer // outputs linear coverage values rather than SRGB values. However, this feature isn't // available on all platforms. CreateCheckbox("Graphics::SetSRGB", URHO3D_HANDLER(Typography, HandleSRGB)) ->SetChecked(GetSubsystem<Graphics>()->GetSRGB()); // Add a checkbox for the global ForceAutoHint setting. This affects character spacing. CreateCheckbox("UI::SetForceAutoHint", URHO3D_HANDLER(Typography, HandleForceAutoHint)) ->SetChecked(ui->GetForceAutoHint()); // Add a drop-down menu to control the font hinting level. const char* levels[] = { "FONT_HINT_LEVEL_NONE", "FONT_HINT_LEVEL_LIGHT", "FONT_HINT_LEVEL_NORMAL", nullptr }; CreateMenu("UI::SetFontHintLevel", levels, URHO3D_HANDLER(Typography, HandleFontHintLevel)) ->SetSelection(ui->GetFontHintLevel()); // Add a drop-down menu to control the subpixel threshold. const char* thresholds[] = { "0", "3", "6", "9", "12", "15", "18", "21", nullptr }; CreateMenu("UI::SetFontSubpixelThreshold", thresholds, URHO3D_HANDLER(Typography, HandleFontSubpixel)) ->SetSelection(ui->GetFontSubpixelThreshold() / 3); // Add a drop-down menu to control oversampling. const char* limits[] = { "1", "2", "3", "4", "5", "6", "7", "8", nullptr }; CreateMenu("UI::SetFontOversampling", limits, URHO3D_HANDLER(Typography, HandleFontOversampling)) ->SetSelection(ui->GetFontOversampling() - 1); // Set the mouse mode to use in the sample Sample::InitMouseMode(MM_FREE); }