Skip to content

Commit

Permalink
feat(selection): More bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaghettDev committed May 27, 2024
1 parent 853abf8 commit 13c92cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Text selection bugs

## [1.1.0] - 2024-05-256

### Fixed

- Text selection and copy/paste shortcuts

## [1.0.1] - 2024-05-26
Expand Down
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"gd": {
"win": "2.204"
},
"version": "v1.1.0",
"version": "v1.2.0",
"id": "spaghettdev.betterinputs",
"name": "BetterInputs",
"developer": "SpaghettDev",
Expand Down
22 changes: 11 additions & 11 deletions src/HighlightedString/HighlightedString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@

struct HighlightedString
{
HighlightedString(std::string_view str, int from = -2, int to = -2)
constexpr HighlightedString(std::string_view str, int from = -2, int to = -2)
: m_from(from == -1 ? str.size() : from),
m_to(to == -1 ? str.size() : to),
m_og_str(str)
{
update_string_substr();
}

HighlightedString()
constexpr HighlightedString()
: m_from(-2), m_to(-2), m_og_str(""), str("")
{}

void reset()
constexpr void reset()
{
m_from = -2;
m_to = -2;
m_og_str = "";
str = "";
}

void updateStr(std::string_view s)
constexpr void updateStr(std::string_view s)
{
m_og_str = s;

if (m_from != -2 && m_to != -2)
update_string_substr();
}

void update(std::string_view str, std::pair<std::size_t, std::size_t> pos)
constexpr void update(std::string_view str, std::pair<std::size_t, std::size_t> pos)
{
m_from = pos.first == -1 ? str.size() : pos.first;
m_to = pos.second == -1 ? str.size() : pos.second;
Expand All @@ -45,11 +45,11 @@ struct HighlightedString
update_string_substr();
}

inline bool isHighlighting() const { return !str.empty(); }
inline constexpr bool isHighlighting() const { return !str.empty(); }

inline int getFromPos() const { return m_from == m_og_str.size() ? -1 : m_from; }
inline int getToPos() const { return m_to == m_og_str.size() ? -1 : m_to; }
inline int getLength() const { return str.size(); }
inline constexpr int getFromPos() const { return m_from == m_og_str.size() ? -1 : m_from; }
inline constexpr int getToPos() const { return m_to == m_og_str.size() ? -1 : m_to; }
inline constexpr std::size_t getLength() const { return str.size(); }

public:
std::string_view str;
Expand All @@ -59,8 +59,8 @@ struct HighlightedString
int m_from = 0;
int m_to = -1;

void update_string_substr()
constexpr void update_string_substr()
{
str = m_og_str.substr(m_from, m_to);
str = m_og_str.substr(m_from, m_to - m_from);
}
};
24 changes: 9 additions & 15 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,8 @@ struct BetterTextInputNode : Modify<BetterTextInputNode, CCTextInputNode>
const std::string& clipboardText = clipboard::read();
const int pos = m_fields->m_pos == -1 ? m_fields->m_string.size() : m_fields->m_pos;

log::debug("from: {} len: {} text: \"{}\"", m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), clipboardText);

if (m_fields->m_highlighted.isHighlighting())
insertStringAtPos(m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), clipboardText);
insertStrAtPos(m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), clipboardText);
else
{
setAndUpdateString(BI::utils::insertStrAtIndex(m_fields->m_string, m_fields->m_pos, clipboardText));
Expand All @@ -332,7 +330,7 @@ struct BetterTextInputNode : Modify<BetterTextInputNode, CCTextInputNode>

clipboard::write(m_fields->m_highlighted.str.data());

insertStringAtPos(
insertStrAtPos(
m_fields->m_highlighted.getFromPos(),
m_fields->m_highlighted.getLength(),
""
Expand Down Expand Up @@ -413,7 +411,7 @@ struct BetterTextInputNode : Modify<BetterTextInputNode, CCTextInputNode>
) return;

if (m_fields->m_highlighted.isHighlighting())
insertStringAtPos(
insertStrAtPos(
m_fields->m_highlighted.getFromPos(),
m_fields->m_highlighted.getLength(),
""
Expand All @@ -433,19 +431,15 @@ struct BetterTextInputNode : Modify<BetterTextInputNode, CCTextInputNode>
* @param len
* @param str
*/
void insertStringAtPos(int pos, std::size_t len, const std::string_view str)
void insertStrAtPos(int pos, std::size_t len, const std::string& str)
{
log::debug("before inputting str: {}", m_fields->m_string);
m_fields->m_string.replace(pos, len, str);
log::debug("after inputting str: {}", m_fields->m_string);
const std::size_t endPos = m_fields->m_highlighted.getFromPos() + str.length();

m_fields->m_string.replace(pos, len, str.data());

setAndUpdateString(m_fields->m_string);

updateBlinkLabelToCharForced(
(m_fields->m_highlighted.getFromPos() + str.length()) == m_fields->m_string.length()
? -1
: m_fields->m_highlighted.getFromPos() + str.length()
);
updateBlinkLabelToCharForced(endPos == m_fields->m_string.length() ? -1 : endPos);
}

/**
Expand All @@ -467,7 +461,7 @@ struct BetterTextInputNode : Modify<BetterTextInputNode, CCTextInputNode>

if (m_fields->m_highlighted.isHighlighting())
{
insertStringAtPos(
insertStrAtPos(
m_fields->m_highlighted.getFromPos(),
m_fields->m_highlighted.getLength(),
""
Expand Down

0 comments on commit 13c92cc

Please sign in to comment.