| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #include <Urho3D/Core/CoreEvents.h>
- #include <Urho3D/Core/ProcessUtils.h>
- #include <Urho3D/Graphics/RenderPath.h>
- #include <Urho3D/Graphics/Zone.h>
- #include <Urho3D/Input/Input.h>
- #include <Urho3D/UI/CheckBox.h>
- #include <Urho3D/UI/DropDownList.h>
- #include <Urho3D/UI/Font.h>
- #include <Urho3D/UI/Text.h>
- #include <Urho3D/UI/UI.h>
- #include <Urho3D/UI/UIEvents.h>
- #include "Typography.h"
- #include <Urho3D/DebugNew.h>
- // 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<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);
- }
- void Typography::CreateText()
- {
- SharedPtr<UIElement> container(new UIElement(context_));
- container->SetAlignment(HA_LEFT, VA_TOP);
- container->SetLayout(LM_VERTICAL);
- uielement_->AddChild(container);
- auto* cache = GetSubsystem<ResourceCache>();
- auto* font = cache->GetResource<Font>("Fonts/BlueHighway.ttf");
- for (auto size2x = 2; size2x <= 36; ++size2x)
- {
- auto size = size2x / 2.f;
- SharedPtr<Text> 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<CheckBox> Typography::CreateCheckbox(const String& label, EventHandler* handler)
- {
- SharedPtr<UIElement> container(new UIElement(context_));
- container->SetAlignment(HA_LEFT, VA_TOP);
- container->SetLayout(LM_HORIZONTAL, 8);
- uielement_->AddChild(container);
- SharedPtr<CheckBox> box(new CheckBox(context_));
- container->AddChild(box);
- box->SetStyleAuto();
- SharedPtr<Text> text(new Text(context_));
- container->AddChild(text);
- text->SetText(label);
- text->SetStyleAuto();
- text->AddTag(TEXT_TAG);
- SubscribeToEvent(box, E_TOGGLED, handler);
- return box;
- }
- SharedPtr<DropDownList> Typography::CreateMenu(const String& label, const char** items, EventHandler* handler)
- {
- SharedPtr<UIElement> container(new UIElement(context_));
- container->SetAlignment(HA_LEFT, VA_TOP);
- container->SetLayout(LM_HORIZONTAL, 8);
- uielement_->AddChild(container);
- SharedPtr<Text> text(new Text(context_));
- container->AddChild(text);
- text->SetText(label);
- text->SetStyleAuto();
- text->AddTag(TEXT_TAG);
- SharedPtr<DropDownList> list(new DropDownList(context_));
- container->AddChild(list);
- list->SetStyleAuto();
- for (int i = 0; items[i]; ++i)
- {
- SharedPtr<Text> 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<CheckBox*>(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<Renderer>();
- Zone* zone = renderer->GetDefaultZone();
- zone->SetFogColor(bg);
- Vector<UIElement*> 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<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr());
- bool checked = box->IsChecked();
- GetSubsystem<UI>()->SetForceAutoHint(checked);
- }
- void Typography::HandleSRGB(StringHash eventType, VariantMap& eventData)
- {
- auto* box = static_cast<CheckBox*>(eventData[Toggled::P_ELEMENT].GetPtr());
- bool checked = box->IsChecked();
- auto* graphics = GetSubsystem<Graphics>();
- 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<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
- unsigned i = list->GetSelection();
- GetSubsystem<UI>()->SetFontHintLevel((FontHintLevel)i);
- }
- void Typography::HandleFontSubpixel(StringHash eventType, VariantMap& eventData)
- {
- auto* list = static_cast<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
- unsigned i = list->GetSelection();
- GetSubsystem<UI>()->SetFontSubpixelThreshold(i * 3);
- }
- void Typography::HandleFontOversampling(StringHash eventType, VariantMap& eventData)
- {
- auto* list = static_cast<DropDownList*>(eventData[Toggled::P_ELEMENT].GetPtr());
- unsigned i = list->GetSelection();
- GetSubsystem<UI>()->SetFontOversampling(i + 1);
- }
|