(element)->GetValue().c_str());
tweening_parameters.duration = value;
if (auto el_duration = element->GetElementById("duration"))
el_duration->SetInnerRML(CreateString(20, "%2.2f", value));
}
else if (value == "rating")
{
auto el_rating = element->GetElementById("rating");
auto el_rating_emoji = element->GetElementById("rating_emoji");
if (el_rating && el_rating_emoji)
{
enum { Sad, Mediocre, Exciting, Celebrate, Champion, CountEmojis };
static const Rml::String emojis[CountEmojis] = {(const char*)u8"😢", (const char*)u8"😐", (const char*)u8"😮", (const char*)u8"😎",
(const char*)u8"🏆"};
int value = event.GetParameter("value", 50);
Rml::String emoji;
if (value <= 0)
emoji = emojis[Sad];
else if (value < 50)
emoji = emojis[Mediocre];
else if (value < 75)
emoji = emojis[Exciting];
else if (value < 100)
emoji = emojis[Celebrate];
else
emoji = emojis[Champion];
el_rating->SetInnerRML(Rml::CreateString(30, "%d%%", value));
el_rating_emoji->SetInnerRML(emoji);
}
}
else if (value == "submit_form")
{
const auto& p = event.GetParameters();
Rml::String output = "";
for (auto& entry : p)
{
auto value = Rml::StringUtilities::EncodeRml(entry.second.Get());
if (entry.first == "message")
value = "
" + value;
output += "" + entry.first + ": " + value + "
";
}
output += "
";
demo_window->SubmitForm(output);
}
else if (value == "set_sandbox_body")
{
if (auto source = rmlui_dynamic_cast(element->GetElementById("sandbox_rml_source")))
{
auto value = source->GetValue();
demo_window->SetSandboxBody(value);
}
}
else if (value == "set_sandbox_style")
{
if (auto source = rmlui_dynamic_cast(element->GetElementById("sandbox_rcss_source")))
{
auto value = source->GetValue();
demo_window->SetSandboxStylesheet(value);
}
}
}
void OnDetach(Rml::Element* /*element*/) override { delete this; }
private:
Rml::String value;
Rml::Element* element;
};
class DemoEventListenerInstancer : public Rml::EventListenerInstancer {
public:
Rml::EventListener* InstanceEventListener(const Rml::String& value, Rml::Element* element) override
{
return new DemoEventListener(value, element);
}
};
#if defined RMLUI_PLATFORM_WIN32
#include
int APIENTRY WinMain(HINSTANCE /*instance_handle*/, HINSTANCE /*previous_instance_handle*/, char* /*command_line*/, int /*command_show*/)
#else
int main(int /*argc*/, char** /*argv*/)
#endif
{
const int width = 1600;
const int height = 890;
// Initializes the shell which provides common functionality used by the included samples.
if (!Shell::Initialize())
return -1;
// Constructs the system and render interfaces, creates a window, and attaches the renderer.
if (!Backend::Initialize("Demo Sample", width, height, true))
{
Shell::Shutdown();
return -1;
}
// Install the custom interfaces constructed by the backend before initializing RmlUi.
Rml::SetSystemInterface(Backend::GetSystemInterface());
Rml::SetRenderInterface(Backend::GetRenderInterface());
// RmlUi initialisation.
Rml::Initialise();
// Create the main RmlUi context.
Rml::Context* context = Rml::CreateContext("main", Rml::Vector2i(width, height));
if (!context)
{
Rml::Shutdown();
Backend::Shutdown();
Shell::Shutdown();
return -1;
}
Rml::Debugger::Initialise(context);
DemoEventListenerInstancer event_listener_instancer;
Rml::Factory::RegisterEventListenerInstancer(&event_listener_instancer);
Shell::LoadFonts();
demo_window = Rml::MakeUnique("Demo sample", context);
demo_window->GetDocument()->AddEventListener(Rml::EventId::Keydown, demo_window.get());
demo_window->GetDocument()->AddEventListener(Rml::EventId::Keyup, demo_window.get());
demo_window->GetDocument()->AddEventListener(Rml::EventId::Animationend, demo_window.get());
bool running = true;
while (running)
{
demo_window->Update();
running = Backend::ProcessEvents(context, &Shell::ProcessKeyDownShortcuts, true);
context->Update();
Backend::BeginFrame();
context->Render();
Backend::PresentFrame();
}
demo_window->Shutdown();
// Shutdown RmlUi.
Rml::Shutdown();
Backend::Shutdown();
Shell::Shutdown();
demo_window.reset();
return 0;
}