can't understand EditBox::updateCursorPosition details

tcsjzdnlh

05-07-2013 08:59:37

i want understand mygui engine code,
then i can improve it,
make it stronger,
and share it just like the men who create it,

i am willing spend time doing this ,and already spend much time.....


now i got some question, i can't understand those code ,
can you explain what does those code means?
maybe you need draw a illustration to show me....
thank you so much , you help is important for me ....


void EditBox::updateCursorPosition()
{
if (mClientText == nullptr || mClient == nullptr)
return;

// TextView::mViewSize + cursorwidth
IntSize textSize = mClientText->getTextSize();

// текущее смещение контекста текста
IntPoint point = mClientText->getViewOffset();
// расчетное смещение
IntPoint offset = point;

// абсолютные координаты курсора
IntRect cursor = mClientText->getCursorRect(mCursorPosition);
cursor.right ++;

// абсолютные координаты вью
const IntRect& view = mClient->getAbsoluteRect();

// проверяем и показываем курсор
if (!view.inside(cursor))
{
// горизонтальное смещение
if (textSize.width > view.width())
{
if (cursor.left < view.left) //
{
offset.left = point.left - (view.left - cursor.left);
// добавляем смещение, только если курсор не перепрыгнет
if ((float(view.width()) - EDIT_OFFSET_HORZ_CURSOR) > EDIT_OFFSET_HORZ_CURSOR)
offset.left -= int(EDIT_OFFSET_HORZ_CURSOR);
}
else if (cursor.right > view.right) //
{
offset.left = point.left + (cursor.right - view.right);
// добавляем смещение, только если курсор не перепрыгнет
if ((float(view.width()) - EDIT_OFFSET_HORZ_CURSOR) > EDIT_OFFSET_HORZ_CURSOR)
offset.left += int(EDIT_OFFSET_HORZ_CURSOR);
}
}

// вертикальное смещение
if (textSize.height > view.height())
{
int delta = 0;
if (cursor.height() > view.height())
{
// if text is bigger than edit height then place it in center
delta = ((cursor.bottom - view.bottom) - (view.top - cursor.top)) / 2;
}
else if (cursor.top < view.top)
{
delta = - (view.top - cursor.top);
}
else if (cursor.bottom > view.bottom)
{
delta = (cursor.bottom - view.bottom);
}
offset.top = point.top + delta;
}

}

if (offset != point)
{
mClientText->setViewOffset(offset);
// обновить скролы
if (mVScroll != nullptr)
mVScroll->setScrollPosition(offset.top);
if (mHScroll != nullptr)
mHScroll->setScrollPosition(offset.left);
}
}