Skip to content

Commit

Permalink
RichText and ScrollView enhancements (#1696)
Browse files Browse the repository at this point in the history
* Add id tag to several RichText elements to allow locating the nodes in RichText
Add font related styling to paragraph tags

* Move functionality out of ListView and into ScrollView to allow scrolling to a specific child node within a ScrollView

* Add function to allow finding protected child node by name

* Example of anchor tags to local RichText content in a ScrollView

* Fix string storage type
  • Loading branch information
rh101 authored Feb 18, 2024
1 parent 8e2e577 commit 978d15d
Show file tree
Hide file tree
Showing 10 changed files with 528 additions and 163 deletions.
11 changes: 11 additions & 0 deletions core/2d/ProtectedNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ Node* ProtectedNode::getProtectedChildByTag(int tag)
return nullptr;
}

Node* ProtectedNode::getProtectedChildByName(std::string_view name)
{
// AXASSERT(!name.empty(), "Invalid name");
for (auto&& child : _protectedChildren)
{
if (child && child->getName() == name)
return child;
}
return nullptr;
}

/* "remove" logic MUST only be on this method
* If a class want's to extend the 'removeChild' behavior it only needs
* to override this method
Expand Down
9 changes: 9 additions & 0 deletions core/2d/ProtectedNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ class AX_DLL ProtectedNode : public Node
*/
virtual Node* getProtectedChildByTag(int tag);

/**
* Gets a child from the container with its name.
*
* @param name An identifier to find the child node.
*
* @return a Node object whose name equals to the input parameter.
*/
virtual Node* getProtectedChildByName(std::string_view name);

////// REMOVES //////

/**
Expand Down
44 changes: 6 additions & 38 deletions core/ui/UIListView.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
https://axmolengine.github.io/
Expand Down Expand Up @@ -28,8 +29,6 @@ THE SOFTWARE.

NS_AX_BEGIN

static const float DEFAULT_TIME_IN_SEC_FOR_SCROLL_TO_ITEM = 1.0f;

namespace ui
{

Expand All @@ -45,7 +44,6 @@ ListView::ListView()
, _topPadding(0.0f)
, _rightPadding(0.0f)
, _bottomPadding(0.0f)
, _scrollTime(DEFAULT_TIME_IN_SEC_FOR_SCROLL_TO_ITEM)
, _curSelectedIndex(-1)
, _innerContainerDoLayoutDirty(true)
, _eventCallback(nullptr)
Expand Down Expand Up @@ -523,17 +521,6 @@ float ListView::getBottomPadding() const
return _bottomPadding;
}

void ListView::setScrollDuration(float time)
{
if (time >= 0)
_scrollTime = time;
}

float ListView::getScrollDuration() const
{
return _scrollTime;
}

void ListView::setDirection(Direction dir)
{
switch (dir)
Expand Down Expand Up @@ -642,13 +629,6 @@ void ListView::interceptTouchEvent(TouchEventType event, Widget* sender, Touch*
}
}

static Vec2 calculateItemPositionWithAnchor(Widget* item, const Vec2& itemAnchorPoint)
{
Vec2 origin(item->getLeftBoundary(), item->getBottomBoundary());
Vec2 size = item->getContentSize();
return origin + Vec2(size.width * itemAnchorPoint.x, size.height * itemAnchorPoint.y);
}

static Widget* findClosestItem(const Vec2& targetPosition,
const Vector<Widget*>& items,
const Vec2& itemAnchorPoint,
Expand Down Expand Up @@ -676,7 +656,7 @@ static Widget* findClosestItem(const Vec2& targetPosition,

// Binary search
ssize_t midIndex = (firstIndex + lastIndex) / 2;
Vec2 itemPosition = calculateItemPositionWithAnchor(items.at(midIndex), itemAnchorPoint);
Vec2 itemPosition = ListView::calculateItemPositionWithAnchor(items.at(midIndex), itemAnchorPoint);
float distanceFromMid = (targetPosition - itemPosition).length();
if (distanceFromFirst <= distanceFromLast)
{
Expand Down Expand Up @@ -830,17 +810,6 @@ void ListView::jumpToPercentBothDirection(const Vec2& percent)
ScrollView::jumpToPercentBothDirection(percent);
}

Vec2 ListView::calculateItemDestination(const Vec2& positionRatioInView, Widget* item, const Vec2& itemAnchorPoint)
{
const Vec2& contentSize = getContentSize();
Vec2 positionInView;
positionInView.x += contentSize.width * positionRatioInView.x;
positionInView.y += contentSize.height * positionRatioInView.y;

Vec2 itemPosition = calculateItemPositionWithAnchor(item, itemAnchorPoint);
return -(itemPosition - positionInView);
}

void ListView::jumpToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint)
{
Widget* item = getItem(itemIndex);
Expand Down Expand Up @@ -875,8 +844,7 @@ void ListView::scrollToItem(ssize_t itemIndex,
{
return;
}
Vec2 destination = calculateItemDestination(positionRatioInView, item, itemAnchorPoint);
startAutoScrollToDestination(destination, timeInSec, true);
ScrollView::scrollToItem(item, positionRatioInView, itemAnchorPoint, timeInSec);
}

ssize_t ListView::getCurSelectedIndex() const
Expand Down Expand Up @@ -920,12 +888,12 @@ void ListView::copyClonedWidgetChildren(Widget* model)
}
}

void ListView::copySpecialProperties(Widget* widget)
void ListView::copySpecialProperties(Widget* model)
{
ListView* listViewEx = dynamic_cast<ListView*>(widget);
ListView* listViewEx = dynamic_cast<ListView*>(model);
if (listViewEx)
{
ScrollView::copySpecialProperties(widget);
ScrollView::copySpecialProperties(model);
setItemModel(listViewEx->_model);
setItemsMargin(listViewEx->_itemsMargin);
setGravity(listViewEx->_gravity);
Expand Down
92 changes: 40 additions & 52 deletions core/ui/UIListView.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
https://axmolengine.github.io/
Expand Down Expand Up @@ -106,7 +107,7 @@ class AX_GUI_DLL ListView : public ScrollView
* @js NA
* @lua NA
*/
virtual ~ListView();
~ListView() override;

/**
* Create an empty ListView.
Expand Down Expand Up @@ -288,31 +289,16 @@ class AX_GUI_DLL ListView : public ScrollView
*/
float getBottomPadding() const;

/**
* Set the time in seconds to scroll between items.
* Subsequent calls of function 'scrollToItem', will take 'time' seconds for scrolling.
* @param time The seconds needed to scroll between two items. 'time' must be >= 0
* @see scrollToItem(ssize_t, const Vec2&, const Vec2&)
*/
void setScrollDuration(float time);

/**
* Get the time in seconds to scroll between items.
* @return The time in seconds to scroll between items
* @see setScrollDuration(float)
*/
float getScrollDuration() const;

// override methods
virtual void doLayout() override;
virtual void requestDoLayout() override;
virtual void addChild(Node* child) override;
virtual void addChild(Node* child, int localZOrder) override;
virtual void addChild(Node* child, int zOrder, int tag) override;
virtual void addChild(Node* child, int zOrder, std::string_view name) override;
virtual void removeAllChildren() override;
virtual void removeAllChildrenWithCleanup(bool cleanup) override;
virtual void removeChild(Node* child, bool cleanup = true) override;
void doLayout() override;
void requestDoLayout() override;
void addChild(Node* child) override;
void addChild(Node* child, int localZOrder) override;
void addChild(Node* child, int zOrder, int tag) override;
void addChild(Node* child, int zOrder, std::string_view name) override;
void removeAllChildren() override;
void removeAllChildrenWithCleanup(bool cleanup) override;
void removeChild(Node* child, bool cleanup = true) override;

/**
* @brief Query the closest item to a specific position in inner container.
Expand Down Expand Up @@ -367,17 +353,17 @@ class AX_GUI_DLL ListView : public ScrollView
/**
* Override functions
*/
virtual void jumpToBottom() override;
virtual void jumpToTop() override;
virtual void jumpToLeft() override;
virtual void jumpToRight() override;
virtual void jumpToTopLeft() override;
virtual void jumpToTopRight() override;
virtual void jumpToBottomLeft() override;
virtual void jumpToBottomRight() override;
virtual void jumpToPercentVertical(float percent) override;
virtual void jumpToPercentHorizontal(float percent) override;
virtual void jumpToPercentBothDirection(const Vec2& percent) override;
void jumpToBottom() override;
void jumpToTop() override;
void jumpToLeft() override;
void jumpToRight() override;
void jumpToTopLeft() override;
void jumpToTopRight() override;
void jumpToBottomLeft() override;
void jumpToBottomRight() override;
void jumpToPercentVertical(float percent) override;
void jumpToPercentHorizontal(float percent) override;
void jumpToPercentBothDirection(const Vec2& percent) override;

/**
* @brief Jump to specific item
Expand All @@ -391,15 +377,20 @@ class AX_GUI_DLL ListView : public ScrollView
* @brief Scroll to specific item
* @param positionRatioInView Specifies the position with ratio in list view's content size.
* @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.
* @param timeInSec Scroll time
*/
void scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint);

/**
* @brief Scroll to specific item
* @param positionRatioInView Specifies the position with ratio in list view's content size.
* @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.
* @param timeInSec Scroll time
*/
void scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float timeInSec);

/**
* @brief Query current selected widget's index.
*
* @return An index of a selected item.
*/
ssize_t getCurSelectedIndex() const;
Expand All @@ -423,14 +414,14 @@ class AX_GUI_DLL ListView : public ScrollView
* Direction Direction::VERTICAL means vertical scroll, Direction::HORIZONTAL means horizontal scroll.
* @param dir Set the list view's scroll direction.
*/
virtual void setDirection(Direction dir) override;
void setDirection(Direction dir) override;

virtual std::string getDescription() const override;
std::string getDescription() const override;

virtual bool init() override;
bool init() override;

protected:
virtual void handleReleaseLogic(Touch* touch) override;
void handleReleaseLogic(Touch* touch) override;

virtual void onItemListChanged();

Expand All @@ -439,19 +430,18 @@ class AX_GUI_DLL ListView : public ScrollView
void remedyVerticalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex);
void remedyHorizontalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex);

virtual void onSizeChanged() override;
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
virtual void copyClonedWidgetChildren(Widget* model) override;
void onSizeChanged() override;
Widget* createCloneInstance() override;
void copySpecialProperties(Widget* model) override;
void copyClonedWidgetChildren(Widget* model) override;
void selectedItemEvent(TouchEventType event);
virtual void interceptTouchEvent(Widget::TouchEventType event, Widget* sender, Touch* touch) override;
void interceptTouchEvent(Widget::TouchEventType event, Widget* sender, Touch* touch) override;

virtual Vec2 getHowMuchOutOfBoundary(const Vec2& addition = Vec2::ZERO) override;
Vec2 getHowMuchOutOfBoundary(const Vec2& addition = Vec2::ZERO) override;

virtual void startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& initialVelocity) override;
void startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& initialVelocity) override;

void startMagneticScroll();
Vec2 calculateItemDestination(const Vec2& positionRatioInView, Widget* item, const Vec2& itemAnchorPoint);

protected:
Widget* _model;
Expand All @@ -470,8 +460,6 @@ class AX_GUI_DLL ListView : public ScrollView
float _rightPadding;
float _bottomPadding;

float _scrollTime;

ssize_t _curSelectedIndex;

bool _innerContainerDoLayoutDirty;
Expand Down
Loading

0 comments on commit 978d15d

Please sign in to comment.