Skip to content

Commit 511d625

Browse files
committed
feat(highlighting): More bug fixes
1 parent 44f249c commit 511d625

File tree

1 file changed

+43
-53
lines changed

1 file changed

+43
-53
lines changed

src/BetterTextInputNode.cpp

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,12 @@ void BetterTextInputNode::highlightFromToPos(int from, int to)
614614
bool hasHighlightedStart = false;
615615
bool hasHighlightedEnd = false;
616616

617+
const auto& textLabelInfo = getTextLabelInfoFromPos(
618+
m_fields->m_highlighted.isHighlighting()
619+
? m_fields->m_highlighted.getToPos<false>()
620+
: m_fields->m_pos
621+
);
622+
617623
for (std::size_t line = 0; auto* label : textAreaLabels)
618624
{
619625
const std::size_t labelStrLen = std::string_view{ label->getString() }.length() - 1;
@@ -624,73 +630,57 @@ void BetterTextInputNode::highlightFromToPos(int from, int to)
624630
if (hasHighlightedStart && hasHighlightedEnd)
625631
continue; // dont break in case other highlights are visible
626632

627-
if (!hasHighlightedStart)
633+
if (!hasHighlightedStart && targetFrom <= labelStrLen)
628634
{
629-
if (targetFrom <= labelStrLen)
635+
hasHighlightedStart = true;
636+
637+
CharNodeInfo fromCharInfo = getCharNodePosInfoAtLine(targetFrom, line, true);
638+
CharNodeInfo toCharInfo;
639+
640+
// in case we're highlighting from and to the next label/the same label
641+
if (targetTo > labelStrLen)
630642
{
631-
const auto& textLabelInfo = getTextLabelInfoFromPos(
632-
m_fields->m_highlighted.isHighlighting()
633-
? m_fields->m_highlighted.getToPos<false>()
634-
: m_fields->m_pos
635-
);
636-
hasHighlightedStart = true;
637-
638-
CharNodeInfo fromCharInfo = getCharNodePosInfoAtLine(targetFrom, line, true);
639-
CharNodeInfo toCharInfo;
640-
641-
// in case we're highlighting from and to the next label/the same label
642-
if (targetTo > labelStrLen)
643-
{
644-
toCharInfo = getCharNodePosInfoAtLine(labelStrLen, line, false);
645-
646-
if (targetTo == labelStrLen + 1)
647-
hasHighlightedEnd = true;
648-
}
649-
else
650-
{
651-
// if selection ends at the end of the current label,
652-
// use the right of last character. else set to left of target character
653-
if (textLabelInfo.numCharsFromLabelStart == std::string_view{ textLabelInfo.label->getString() }.length() - 1)
654-
toCharInfo = getCharNodePosInfoAtLine(
655-
textLabelInfo.numCharsFromLabelStart,
656-
textLabelInfo.line,
657-
false
658-
);
659-
else
660-
toCharInfo = getCharNodePosInfoAtLine(
661-
targetTo,
662-
line,
663-
true
664-
);
643+
toCharInfo = getCharNodePosInfoAtLine(labelStrLen, line, false);
665644

645+
if (targetTo == labelStrLen + 1)
666646
hasHighlightedEnd = true;
667-
}
668-
669-
float topY = fromCharInfo.position.y - 3.f
670-
+ fromCharInfo.sprite->getScaledContentHeight() / 2.f;
671-
float bottomY = toCharInfo.position.y - 3.f
672-
- toCharInfo.sprite->getScaledContentHeight() / 2.f;
673-
674-
CCRect rect = createRectFromPoints(
675-
{ fromCharInfo.position.x, bottomY },
676-
{ toCharInfo.position.x, bottomY },
677-
{ toCharInfo.position.x, topY },
678-
{ fromCharInfo.position.x, topY }
647+
}
648+
else
649+
{
650+
toCharInfo = getCharNodePosInfoAtLine(
651+
targetTo,
652+
line,
653+
true
679654
);
680-
// we can get away by just lying about the height
681-
rect.size.height = label->getScaledContentHeight();
682655

683-
highlight->drawRect(rect, highlightColor, .0f, highlightColor);
656+
hasHighlightedEnd = true;
684657
}
658+
659+
float topY = fromCharInfo.position.y - 3.f
660+
+ fromCharInfo.sprite->getScaledContentHeight() / 2.f;
661+
float bottomY = toCharInfo.position.y - 3.f
662+
- toCharInfo.sprite->getScaledContentHeight() / 2.f;
663+
664+
CCRect rect = createRectFromPoints(
665+
{ fromCharInfo.position.x, bottomY },
666+
{ toCharInfo.position.x, bottomY },
667+
{ toCharInfo.position.x, topY },
668+
{ fromCharInfo.position.x, topY }
669+
);
670+
// we can get away by just lying about the height
671+
rect.size.height = label->getScaledContentHeight();
672+
673+
highlight->drawRect(rect, highlightColor, .0f, highlightColor);
685674
}
686-
else if (!hasHighlightedEnd)
675+
676+
if (hasHighlightedStart && !hasHighlightedEnd)
687677
{
688678
CharNodeInfo fromCharInfo = getCharNodePosInfoAtLine(0, line, true);
689679
CharNodeInfo toCharInfo;
690680

691681
// again, check if we're highlighting to the current label or afterwards
692682
if (targetTo > labelStrLen)
693-
toCharInfo = getCharNodePosInfoAtLine(labelStrLen, line, to != -1);
683+
toCharInfo = getCharNodePosInfoAtLine(labelStrLen, line, false);
694684
else
695685
{
696686
toCharInfo = getCharNodePosInfoAtLine(targetTo, line, to != -1);

0 commit comments

Comments
 (0)