mirror of https://github.com/OpenTTD/OpenTTD
(svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
parent
0e9dbe6bb6
commit
ae28432e62
|
@ -151,6 +151,59 @@ bool Textbuf::InsertClipboard()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if it is possible to move carret to the left
|
||||||
|
* @return true if the caret can be moved to the left, otherwise false.
|
||||||
|
*/
|
||||||
|
bool Textbuf::CanMoveCaretLeft()
|
||||||
|
{
|
||||||
|
return this->caretpos != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Moves the caret to the left.
|
||||||
|
* @pre Ensure that Textbuf::CanMoveCaretLeft returns true
|
||||||
|
* @return The character under the caret.
|
||||||
|
*/
|
||||||
|
WChar Textbuf::MoveCaretLeft()
|
||||||
|
{
|
||||||
|
assert(this->CanMoveCaretLeft());
|
||||||
|
|
||||||
|
WChar c;
|
||||||
|
const char *s = Utf8PrevChar(this->buf + this->caretpos);
|
||||||
|
Utf8Decode(&c, s);
|
||||||
|
this->caretpos = s - this->buf;
|
||||||
|
this->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if it is possible to move carret to the right
|
||||||
|
* @return true if the caret can be moved to the right, otherwise false.
|
||||||
|
*/
|
||||||
|
bool Textbuf::CanMoveCaretRight()
|
||||||
|
{
|
||||||
|
return this->caretpos < this->bytes - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Moves the caret to the right.
|
||||||
|
* @pre Ensure that Textbuf::CanMoveCaretRight returns true
|
||||||
|
* @return The character under the caret.
|
||||||
|
*/
|
||||||
|
WChar Textbuf::MoveCaretRight()
|
||||||
|
{
|
||||||
|
assert(this->CanMoveCaretRight());
|
||||||
|
|
||||||
|
WChar c;
|
||||||
|
this->caretpos += (uint16)Utf8Decode(&c, this->buf + this->caretpos);
|
||||||
|
this->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
|
||||||
|
|
||||||
|
Utf8Decode(&c, this->buf + this->caretpos);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle text navigation with arrow keys left/right.
|
* Handle text navigation with arrow keys left/right.
|
||||||
* This defines where the caret will blink and the next characer interaction will occur
|
* This defines where the caret will blink and the next characer interaction will occur
|
||||||
|
@ -161,24 +214,15 @@ bool Textbuf::MovePos(int navmode)
|
||||||
{
|
{
|
||||||
switch (navmode) {
|
switch (navmode) {
|
||||||
case WKC_LEFT:
|
case WKC_LEFT:
|
||||||
if (this->caretpos != 0) {
|
if (this->CanMoveCaretLeft()) {
|
||||||
WChar c;
|
this->MoveCaretLeft();
|
||||||
const char *s = Utf8PrevChar(this->buf + this->caretpos);
|
|
||||||
Utf8Decode(&c, s);
|
|
||||||
this->caretpos = s - this->buf; // -= (this->buf + this->caretpos - s)
|
|
||||||
this->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WKC_RIGHT:
|
case WKC_RIGHT:
|
||||||
if (this->caretpos < this->bytes - 1) {
|
if (this->CanMoveCaretRight()) {
|
||||||
WChar c;
|
this->MoveCaretRight();
|
||||||
|
|
||||||
this->caretpos += (uint16)Utf8Decode(&c, this->buf + this->caretpos);
|
|
||||||
this->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
#ifndef TEXTBUF_TYPE_H
|
#ifndef TEXTBUF_TYPE_H
|
||||||
#define TEXTBUF_TYPE_H
|
#define TEXTBUF_TYPE_H
|
||||||
|
|
||||||
|
#include "string_type.h"
|
||||||
|
|
||||||
/** Helper/buffer for input fields. */
|
/** Helper/buffer for input fields. */
|
||||||
struct Textbuf {
|
struct Textbuf {
|
||||||
char *buf; ///< buffer in which text is saved
|
char *buf; ///< buffer in which text is saved
|
||||||
|
@ -38,6 +40,11 @@ struct Textbuf {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void DelChar(bool backspace);
|
void DelChar(bool backspace);
|
||||||
|
bool CanMoveCaretLeft();
|
||||||
|
WChar MoveCaretLeft();
|
||||||
|
bool CanMoveCaretRight();
|
||||||
|
WChar MoveCaretRight();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* TEXTBUF_TYPE_H */
|
#endif /* TEXTBUF_TYPE_H */
|
||||||
|
|
Loading…
Reference in New Issue