finalized EditableWidget

enabled selection drawing on all derived widgets
This commit is contained in:
thrust26 2020-11-04 20:18:41 +01:00
parent ac47d855e1
commit 2ec1f463ad
8 changed files with 62 additions and 63 deletions

View File

@ -646,7 +646,7 @@ void DataGridWidget::drawWidget(bool hilite)
// Only draw the caret while editing, and if it's in the current viewport
if(_editMode)
drawCaret();
drawCaretSelection();
// Draw the scrollbar
if(_scrollBar)

View File

@ -547,7 +547,7 @@ void RomListWidget::drawWidget(bool hilite)
s.drawString(_font, editString(), _x + r.x(), ypos, r.w(), textColor,
TextAlign::Left, -_editScrollOffset, false);
drawCaret();
drawCaretSelection();
}
else
{

View File

@ -145,7 +145,7 @@ void CheckListWidget::drawWidget(bool hilite)
(!_useScrollbar ||
((_selectedItem >= _scrollBar->_currentPos) &&
(_selectedItem < _scrollBar->_currentPos + _rows))))
drawCaret();
drawCaretSelection();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -108,10 +108,8 @@ void EditTextWidget::drawWidget(bool hilite)
: onTop && isEnabled() ? _textcolor : kColor,
TextAlign::Left, scrollOffset(), !isEditable());
// Draw selected text
drawSelection();
// Draw the caret
drawCaret();
// Draw the caret and selection
drawCaretSelection();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -159,12 +159,12 @@ bool EditableWidget::handleControlKeys(StellaKey key, StellaMod mod)
if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
case KBDK_K: // TODO
case KBDK_K:
handled = killLine(+1);
if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
case KBDK_U: // TODO
case KBDK_U:
handled = killLine(-1);
if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
@ -284,13 +284,17 @@ bool EditableWidget::handleNormalKeys(StellaKey key)
break;
case KBDK_BACKSPACE:
handled = killChar(-1);
handled = killSelectedText();
if(!handled)
handled = killChar(-1);
if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
case KBDK_DELETE:
case KBDK_KP_PERIOD:
handled = killChar(+1);
handled = killSelectedText();
if(!handled)
handled = killChar(+1);
if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
@ -342,7 +346,7 @@ int EditableWidget::getCaretOffset() const
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::drawCaret()
void EditableWidget::drawCaretSelection()
{
// Only draw if item is visible
if (!_editable || !isVisible() || !_boss->isVisible() || !_hasFocus)
@ -360,44 +364,36 @@ void EditableWidget::drawCaret()
FBSurface& s = _boss->dialog().surface();
s.vLine(x, y + 2, y + editRect.h() - 2, kTextColorHi);
s.vLine(x-1, y + 2, y + editRect.h() - 2, kTextColorHi);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::drawSelection()
{
// Only draw if item is visible
if(!_editable || !isVisible() || !_boss->isVisible() || !_hasFocus
|| !_selectSize)
return;
FBSurface& s = _boss->dialog().surface();
string text = selectString();
const Common::Rect& editRect = getEditRect();
int x = editRect.x();
int y = editRect.y();
int w = editRect.w();
int h = editRect.h();
int wt = int(text.length()) * _font.getMaxCharWidth() + 1;
int dx = selectStartPos() * _font.getMaxCharWidth() - _editScrollOffset;
if(dx < 0)
if(_selectSize)
{
// selected text starts left of displayed rect
text = text.substr(-(dx - 1) / _font.getMaxCharWidth());
wt += dx;
dx = 0;
string text = selectString();
x = editRect.x();
y = editRect.y();
int w = editRect.w();
int h = editRect.h();
int wt = int(text.length()) * _font.getMaxCharWidth() + 1;
int dx = selectStartPos() * _font.getMaxCharWidth() - _editScrollOffset;
if(dx < 0)
{
// selected text starts left of displayed rect
text = text.substr(-(dx - 1) / _font.getMaxCharWidth());
wt += dx;
dx = 0;
}
else
x += dx;
// limit selection to the right of displayed rect
w = std::min(w - dx + 1, wt);
x += _x;
y += _y;
s.fillRect(x - 1, y + 1, w + 1, h - 3, kTextColorHi);
s.drawString(_font, text, x, y + 1, w, h,
kTextColorInv, TextAlign::Left, 0, false);
}
else
x += dx;
// limit selection to the right of displayed rect
w = std::min(w - dx + 1, wt);
x += _x;
y += _y;
s.fillRect(x - 1, y + 1, w + 1, h - 3, kTextColorHi);
s.drawString(_font, text, x, y + 1, w, h,
kTextColorInv, TextAlign::Left, 0, false);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -466,25 +462,22 @@ int EditableWidget::scrollOffset()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool EditableWidget::killChar(int direction)
{
bool handled = killSelectedText();
bool handled = false;
if(!handled)
if(direction == -1) // Delete previous character (backspace)
{
if(direction == -1) // Delete previous character (backspace)
{
if(_caretPos > 0)
{
_caretPos--;
_editString.erase(_caretPos, 1);
handled = true;
}
}
else if(direction == 1) // Delete next character (delete)
if(_caretPos > 0)
{
_caretPos--;
_editString.erase(_caretPos, 1);
handled = true;
}
}
else if(direction == 1) // Delete next character (delete)
{
_editString.erase(_caretPos, 1);
handled = true;
}
return handled;
}
@ -503,6 +496,9 @@ bool EditableWidget::killLine(int direction)
killChar(-1);
handled = true;
// remove selection for removed text
if(_selectSize < 0)
_selectSize = 0;
}
}
else if(direction == 1) // erase from current position to end of line
@ -514,6 +510,9 @@ bool EditableWidget::killLine(int direction)
killChar(+1);
handled = true;
// remove selection for removed text
if(_selectSize > 0)
_selectSize = 0;
}
}
@ -546,6 +545,9 @@ bool EditableWidget::killLastWord()
killChar(-1);
handled = true;
// remove selection for removed word
if(_selectSize < 0)
_selectSize = std::min(_selectSize + count, 0);
}
return handled;

View File

@ -72,7 +72,7 @@ class EditableWidget : public Widget, public CommandSender
virtual Common::Rect getEditRect() const = 0;
virtual int getCaretOffset() const;
void drawCaret();
void drawCaretSelection();
bool setCaretPos(int newPos);
bool moveCaretPos(int direction);
bool adjustOffset();
@ -82,7 +82,6 @@ class EditableWidget : public Widget, public CommandSender
string& editString() { return _editString; }
const string selectString() const;
void resetSelection() { _selectSize = 0; }
void drawSelection();
int scrollOffset();
private:

View File

@ -297,7 +297,7 @@ void PopUpWidget::drawWidget(bool hilite)
align, editable ? -_editScrollOffset : 0, !editable);
if(editable)
drawCaret();
drawCaretSelection();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -112,7 +112,7 @@ void StringListWidget::drawWidget(bool hilite)
(!_useScrollbar ||
((_selectedItem >= _scrollBar->_currentPos) &&
(_selectedItem < _scrollBar->_currentPos + _rows))))
drawCaret();
drawCaretSelection();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -