Skip to content

Commit

Permalink
Fix selection background of formatted labels
Browse files Browse the repository at this point in the history
The metrics of the rectangles comprising the selection background
were calculated based on the selected range and the unformatted
text, so these rectangles would be off when the formatted text
was different (i.e. had any formatting).
  • Loading branch information
LBPHacker committed Jul 19, 2019
1 parent 34b4665 commit 8223e06
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ Controls
Command Line
---------------------------------------------------------------------------

| Command | Description | Example |
| ---------------------- | ------------------------------------------------ | ---------------------------------------------------- |
| `scale:SIZE` | Change window scale factor | `scale:2` |
| `kiosk` | Fullscreen mode | |
| `proxy:SERVER[:PORT]` | Proxy server to use | `proxy:wwwcache.lancs.ac.uk:8080` |
| `open FILE` | Opens the file as a stamp or game save | |
| `ddir DIRECTORY` | Directory used for saving stamps and preferences | |
| `ptsave:SAVEID[#NAME]` | Open online save, used by ptsave: URLs | `ptsave:2198#Destroyable_city_5_wth_metro~dima-gord` |
| Command | Description | Example |
| --------------------- | ------------------------------------------------ | --------------------------------- |
| `scale:SIZE` | Change window scale factor | `scale:2` |
| `kiosk` | Fullscreen mode | |
| `proxy:SERVER[:PORT]` | Proxy server to use | `proxy:wwwcache.lancs.ac.uk:8080` |
| `open FILE` | Opens the file as a stamp or game save | |
| `ddir DIRECTORY` | Directory used for saving stamps and preferences | |
| `ptsave:SAVEID` | Open online save, used by ptsave: URLs | `ptsave:2198` |
13 changes: 9 additions & 4 deletions src/gui/interface/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ void Label::updateSelection()
displayTextWithSelection = displayTextWrapper.WrappedText();
if (HasSelection())
{
displayTextWithSelection.Insert(displayTextWrapper.Clear2Index(selectionIndexL.clear_index).wrapped_index, "\x01");
displayTextWithSelection.Insert(displayTextWrapper.Clear2Index(selectionIndexH.clear_index).wrapped_index + 1, "\x01");
auto indexL = displayTextWrapper.Clear2Index(selectionIndexL.clear_index);
auto indexH = displayTextWrapper.Clear2Index(selectionIndexH.clear_index);
displayTextWithSelection.Insert(indexL.wrapped_index , "\x01");
displayTextWithSelection.Insert(indexH.wrapped_index + 1, "\x01");
}
}

Expand All @@ -229,13 +231,16 @@ void Label::Draw(const Point& screenPos)
}
Graphics *g = GetGraphics();

auto indexL = displayTextWrapper.Clear2Index(selectionIndexL.clear_index);
auto indexH = displayTextWrapper.Clear2Index(selectionIndexH.clear_index);

int selectionXL;
int selectionYL;
int selectionLineL = displayTextWrapper.Index2Point(selectionIndexL, selectionXL, selectionYL);
int selectionLineL = displayTextWrapper.Index2Point(indexL, selectionXL, selectionYL);

int selectionXH;
int selectionYH;
int selectionLineH = displayTextWrapper.Index2Point(selectionIndexH, selectionXH, selectionYH);
int selectionLineH = displayTextWrapper.Index2Point(indexH, selectionXH, selectionYH);

if (HasSelection())
{
Expand Down
42 changes: 19 additions & 23 deletions src/gui/interface/TextWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace ui
int raw_index;
int clear_index;
bool wraps;
bool may_eat_space;
};
int line_width = 0;
std::vector<wrap_record> records;
Expand All @@ -38,7 +39,8 @@ namespace ui
0, // width; fools the clickmap generator into not seeing this newline
0, // position; the clickmap generator is fooled, this can be anything
0,
true // signal the end of the line to the clickmap generator
true, // signal the end of the line to the clickmap generator
true // allow record to eat the following space
});
line_width = 0;
lines += 1;
Expand All @@ -62,15 +64,22 @@ namespace ui
{
// add more supported spaces here
case ' ':
if (!wrap_if_needed(line_width))
wrap_if_needed(line_width);
if (records.size() && records.back().may_eat_space)
{
// this is in the non-wrapping branch to make spaces immediately
// following newline characters inserted by the wrapper disappear
records.back().may_eat_space = false;
}
else
{
// this is pushed only if the previous record isn't a wrapping one
// to make spaces immediately following newline characters inserted
// by the wrapper disappear
records.push_back(wrap_record{
*it,
char_width,
(int)(it - text.begin()),
clear_count,
false,
false
});
line_width += char_width;
Expand All @@ -86,7 +95,8 @@ namespace ui
max_width - line_width, // width; make it span all the way to the end
(int)(it - text.begin()), // position; so the clickmap generator knows where *it is
clear_count,
true // signal the end of the line to the clickmap generator
true, // signal the end of the line to the clickmap generator
false
});
lines += 1;
line_width = 0;
Expand All @@ -109,7 +119,8 @@ namespace ui
0, // width; fools the clickmap generator into not seeing this sequence
0, // position; the clickmap generator is fooled, this can be anything
0,
false // signal nothing to the clickmap generator
false, // signal nothing to the clickmap generator
false
});
}
--it;
Expand Down Expand Up @@ -152,7 +163,8 @@ namespace ui
char_width, // width; make it span all the way to the end
(int)(it - text.begin()), // position; so the clickmap generator knows where *it is
clear_count,
false // signal nothing to the clickmap generator
false, // signal nothing to the clickmap generator
false
});
word_width += char_width;
line_width += char_width;
Expand Down Expand Up @@ -269,22 +281,6 @@ namespace ui
return regions[index.wrapped_index].pos_line;
}

TextWrapper::Index TextWrapper::Raw2Index(int raw_index) const
{
if (raw_index < 0)
{
return IndexBegin();
}
for (auto const &region : regions)
{
if (region.index.raw_index >= raw_index)
{
return region.index;
}
}
return IndexEnd();
}

TextWrapper::Index TextWrapper::Clear2Index(int clear_index) const
{
if (clear_index < 0)
Expand Down
1 change: 0 additions & 1 deletion src/gui/interface/TextWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace ui

public:
int Update(String const &text, bool do_wrapping, int max_width);
Index Raw2Index(int raw_index) const;
Index Clear2Index(int clear_index) const;
Index Point2Index(int x, int y) const;
int Index2Point(Index index, int &x, int &y) const;
Expand Down
10 changes: 5 additions & 5 deletions src/gui/interface/Textbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void Textbox::SetText(String newText)

if(cursor)
{
textWrapper.Index2Point(textWrapper.Raw2Index(cursor), cursorPositionX, cursorPositionY);
textWrapper.Index2Point(textWrapper.Clear2Index(cursor), cursorPositionX, cursorPositionY);
}
else
{
Expand Down Expand Up @@ -128,7 +128,7 @@ void Textbox::OnContextMenuAction(int item)

void Textbox::resetCursorPosition()
{
textWrapper.Index2Point(textWrapper.Raw2Index(cursor), cursorPositionX, cursorPositionY);
textWrapper.Index2Point(textWrapper.Clear2Index(cursor), cursorPositionX, cursorPositionY);
}

void Textbox::TabFocus()
Expand Down Expand Up @@ -175,7 +175,7 @@ void Textbox::cutSelection()

if(cursor)
{
textWrapper.Index2Point(textWrapper.Raw2Index(cursor), cursorPositionX, cursorPositionY);
textWrapper.Index2Point(textWrapper.Clear2Index(cursor), cursorPositionX, cursorPositionY);
}
else
{
Expand Down Expand Up @@ -245,7 +245,7 @@ void Textbox::pasteIntoSelection()

if(cursor)
{
textWrapper.Index2Point(textWrapper.Raw2Index(cursor), cursorPositionX, cursorPositionY);
textWrapper.Index2Point(textWrapper.Clear2Index(cursor), cursorPositionX, cursorPositionY);
}
else
{
Expand Down Expand Up @@ -463,7 +463,7 @@ void Textbox::AfterTextChange(bool changed)

if(cursor)
{
textWrapper.Index2Point(textWrapper.Raw2Index(cursor), cursorPositionX, cursorPositionY);
textWrapper.Index2Point(textWrapper.Clear2Index(cursor), cursorPositionX, cursorPositionY);
}
else
{
Expand Down

0 comments on commit 8223e06

Please sign in to comment.