Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichText and ScrollView enhancements #1696

Merged
merged 5 commits into from
Feb 18, 2024

Conversation

rh101
Copy link
Contributor

@rh101 rh101 commented Feb 18, 2024

Describe your changes

Add support for the "id" attribute to RichText XML anchor, paragraph, header and custom tags. The ID is saved as the name of the node (via setName()).

Add support for adjusting font/size/color of paragraph elements in RichText.

Move scroll to item support from ListView to the base class ScrollView, since it applies to ScrollView nodes as well.

Add support for finding a protected node in a ScrollView via the name.

All of the above changes can be combined together when a RichText is child of a ScrollView, allowing the user to click on a link that takes them directly to another section of the RichText (scrolling the ScrollView to that point).

For example, taking this input:

<h1>Quick Links</h1>
<a href="#fancy_header">Jump To Fancy Header</a><br/>
<a href="#paragraph_2">Jump To Second Paragraph</a><br/>
<a href="#some_link">Jump To Web Search</a><br/>
<br/>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Purus faucibus ornare suspendisse sed nisi. Viverra aliquet eget sit amet tellus cras adipiscing. Ut tellus elementum sagittis vitae. Risus feugiat in ante metus dictum at. Semper eget duis at tellus at. Iaculis eu non diam phasellus vestibulum lorem sed risus. Sed vulputate odio ut enim. Morbi tristique senectus et netus et malesuada fames.</p>
<h1 id="fancy_header">Fancy Header</h1>
<p id="paragraph_2">Cras sed felis eget velit aliquet sagittis id consectetur purus. Turpis nunc eget lorem dolor sed viverra ipsum nunc. Ultrices tincidunt arcu non sodales neque sodales ut etiam sit. Risus feugiat in ante metus dictum at tempor. Id neque aliquam vestibulum morbi blandit cursus risus.</p>
<a id="some_link" href="https://google.com">Google!</a>
auto* richText = ax::ui::RichText::createWithXML(text);
scrollView->addChild(richText);
// ... some code to update the inner container size of the ScrollView to match the RichText size
// now add the hyperlink click handler
richText->setOpenUrlHandler([richText, scrollView](std::string_view url) {
        // Check if the href starts with a "#" character
	if (url.starts_with('#'))
	{
		auto* node = richText->getProtectedChildByName(url.substr(1));
		if (node)
		{
                        // Scroll to the location of that node, and the reason it works is because
                        // the ScrollView inner container is the same height as the RichText
			scrollView->scrollToItem(node, Vec2::ANCHOR_MIDDLE_TOP, Vec2::ANCHOR_MIDDLE_TOP);
		}
	}
	else if (!url.empty())
	{
		Application::getInstance()->openURL(url);
	}
});

A test case showcasing this functionality has been added to the cpp-tests project (RichText test 22).

This functionality should not impact any existing usage of ScrollView, ListView or RichText.

Issue ticket number and link

Checklist before requesting a review

For each PR

  • Add Copyright if it missed:
    - "Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md)."

  • I have performed a self-review of my code.

    Optional:

    • I have checked readme and add important infos to this PR.
    • I have added/adapted some tests too.

For core/new feature PR

  • I have checked readme and add important infos to this PR.
  • I have added thorough tests.

core/ui/UIRichText.h Outdated Show resolved Hide resolved
@halx99 halx99 added this to the 2.1.2 milestone Feb 18, 2024
@halx99 halx99 added the enhancement New feature or request label Feb 18, 2024
@halx99 halx99 merged commit 978d15d into axmolengine:dev Feb 18, 2024
9 checks passed
@rh101 rh101 deleted the richtext-scroll-enhancements branch February 18, 2024 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants