// Copyright (c) 2008-2023 the Urho3D project // License: MIT #include #include #include #include #include #include #include #include #include #include #include #include "Typography.h" #include // Expands to this example's entry-point URHO3D_DEFINE_APPLICATION_MAIN(Typography) namespace { // Tag used to find all Text elements const char* TEXT_TAG = "Typography_text_tag"; } Typography::Typography(Context* context) : Sample(context) { } void Typography::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 auto* ui = GetSubsystem(); 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()->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); } void Typography::CreateText() { SharedPtr container(new UIElement(context_)); container->SetAlignment(HA_LEFT, VA_TOP); container->SetLayout(LM_VERTICAL); uielement_->AddChild(container); auto* cache = GetSubsystem(); auto* font = cache->GetResource("Fonts/BlueHighway.ttf"); for (auto size2x = 2; size2x <= 36; ++size2x) { auto size = size2x / 2.f; SharedPtr text(new Text(context_)); text->SetText(String("The quick brown fox jumps over the lazy dog (") + String(size) + String("pt)")); text->SetFont(font, size); text->AddTag(TEXT_TAG); container->AddChild(text); } } SharedPtr Typography::CreateCheckbox(const String& label, EventHandler* handler) { SharedPtr container(new UIElement(context_)); container->SetAlignment(HA_LEFT, VA_TOP); container->SetLayout(LM_HORIZONTAL, 8); uielement_->AddChild(container); SharedPtr box(new CheckBox(context_)); container->AddChild(box); box->SetStyleAuto(); SharedPtr text(new Text(context_)); container->AddChild(text); text->SetText(label); text->SetStyleAuto(); text->AddTag(TEXT_TAG); SubscribeToEvent(box, E_TOGGLED, handler); return box; } SharedPtr Typography::CreateMenu(const String& label, const char** items, EventHandler* handler) { SharedPtr container(new UIElement(context_)); container->SetAlignment(HA_LEFT, VA_TOP); container->SetLayout(LM_HORIZONTAL, 8); uielement_->AddChild(container); SharedPtr text(new Text(context_)); container->AddChild(text); text->SetText(label); text->SetStyleAuto(); text->AddTag(TEXT_TAG); SharedPtr list(new DropDownList(context_)); container->AddChild(list); list->SetStyleAuto(); for (int i = 0; items[i]; ++i) { SharedPtr item(new Text(context_)); list->AddItem(item); item->SetText(items[i]); item->SetStyleAuto(); item->SetMinWidth(item->GetRowWidth(0) + 10); item->AddTag(TEXT_TAG); } text->SetMaxWidth(text->GetRowWidth(0)); SubscribeToEvent(list, E_ITEMSELECTED, handler); return list; } void Typography::HandleWhiteBackground(StringHash eventType, VariantMap& eventData) { auto* box = static_cast(eventData[Toggled::P_ELEMENT].GetPtr()); bool checked = box->IsChecked(); Color fg = checked ? Color::BLACK : Color::WHITE; Color bg = checked ? Color::WHITE : Color::BLACK; auto* renderer = GetSubsystem(); Zone* zone = renderer->GetDefaultZone(); zone->SetFogColor(bg); Vector text = uielement_->GetChildrenWithTag(TEXT_TAG, true); for (int i = 0; i < text.Size(); i++) { text[i]->SetColor(fg); } } void Typography::HandleForceAutoHint(StringHash eventType, VariantMap& eventData) { auto* box = static_cast(eventData[Toggled::P_ELEMENT].GetPtr()); bool checked = box->IsChecked(); GetSubsystem()->SetForceAutoHint(checked); } void Typography::HandleSRGB(StringHash eventType, VariantMap& eventData) { auto* box = static_cast(eventData[Toggled::P_ELEMENT].GetPtr()); bool checked = box->IsChecked(); auto* graphics = GetSubsystem(); if (graphics->GetSRGBWriteSupport()) { graphics->SetSRGB(checked); } else { URHO3D_LOGWARNING("Graphics::GetSRGBWriteSupport returned false"); // Note: PostProcess/GammaCorrection.xml implements SRGB conversion. // However, post-processing filters don't affect the UI layer. } } void Typography::HandleFontHintLevel(StringHash eventType, VariantMap& eventData) { auto* list = static_cast(eventData[Toggled::P_ELEMENT].GetPtr()); unsigned i = list->GetSelection(); GetSubsystem()->SetFontHintLevel((FontHintLevel)i); } void Typography::HandleFontSubpixel(StringHash eventType, VariantMap& eventData) { auto* list = static_cast(eventData[Toggled::P_ELEMENT].GetPtr()); unsigned i = list->GetSelection(); GetSubsystem()->SetFontSubpixelThreshold(i * 3); } void Typography::HandleFontOversampling(StringHash eventType, VariantMap& eventData) { auto* list = static_cast(eventData[Toggled::P_ELEMENT].GetPtr()); unsigned i = list->GetSelection(); GetSubsystem()->SetFontOversampling(i + 1); }