Skip to content

Commit

Permalink
[Engine] FloatInput must be able to set values using embedded editor #…
Browse files Browse the repository at this point in the history
  • Loading branch information
eprikazchikov committed Nov 27, 2022
1 parent d255326 commit 73991ab
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 36 deletions.
7 changes: 5 additions & 2 deletions engine/includes/components/gui/floatinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class ENGINE_EXPORT FloatInput : public Widget {

A_METHODS(
A_SLOT(FloatInput::onIncrease),
A_SLOT(FloatInput::onDecrease)
A_SLOT(FloatInput::onDecrease),
A_SLOT(FloatInput::onEditingFinished)
)

public:
Expand All @@ -32,10 +33,12 @@ class ENGINE_EXPORT FloatInput : public Widget {
Vector4 corners() const;
void setCorners(Vector4 corners);

protected:
protected: // slots
void onIncrease();
void onDecrease();

void onEditingFinished();

void composeComponent() override;

private:
Expand Down
17 changes: 14 additions & 3 deletions engine/includes/components/gui/textinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class Label;
class ENGINE_EXPORT TextInput : public Widget {
A_REGISTER(TextInput, Widget, Components/UI)

A_METHODS(
A_SIGNAL(TextInput::focusIn),
A_SIGNAL(TextInput::focusOut),
A_SIGNAL(TextInput::editingFinished)
)

public:
TextInput();

Expand All @@ -24,15 +30,19 @@ class ENGINE_EXPORT TextInput : public Widget {
Vector4 textColor() const;
void setTextColor(Vector4 color);

public: // signals
void focusIn();
void focusOut();

void editingFinished();

protected:
void update() override;

void composeComponent() override;

void onReferenceDestroyed() override;

virtual void onClicked();

void recalcCursor();

private:
Expand All @@ -46,14 +56,15 @@ class ENGINE_EXPORT TextInput : public Widget {
Label *m_cursor;
Label *m_label;

size_t m_cursorPosition;
int32_t m_cursorPosition;

float m_fadeDuration;
float m_currentFade;
float m_cursorBlinkRate;
float m_cursorBlinkCurrent;

bool m_hovered;
bool m_focused;

};

Expand Down
2 changes: 2 additions & 0 deletions engine/includes/resources/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ENGINE_EXPORT Font : public Sprite {

float lineHeight() const;

float cursorWidth() const;

void loadUserData(const VariantMap &data) override;
private:
void clear();
Expand Down
14 changes: 13 additions & 1 deletion engine/src/components/gui/floatinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ FloatInput::FloatInput() :
m_value(0.0f),
m_singleStep(1.0f),
m_minimum(0.0f),
m_maximum(9.99f) {
m_maximum(99.99f) {
}

float FloatInput::value() const {
Expand Down Expand Up @@ -98,6 +98,15 @@ void FloatInput::onDecrease() {
setValue(m_value - m_singleStep);
}

void FloatInput::onEditingFinished() {
string text = m_input->text();
if(!text.empty()) {
setValue(stof(text));
} else {
setValue(value());
}
}

void FloatInput::composeComponent() {
Widget::composeComponent();

Expand All @@ -106,6 +115,9 @@ void FloatInput::composeComponent() {
Actor *text = Engine::composeActor(gTextInput, gTextInput, actor());
m_input = static_cast<TextInput *>(text->component(gTextInput));
if(m_input) {
connect(m_input, _SIGNAL(focusOut()), this, _SLOT(onEditingFinished()));
connect(m_input, _SIGNAL(editingFinished()), this, _SLOT(onEditingFinished()));

Frame *frame = m_input->background();
if(frame) {
frame->setCorners(Vector4());
Expand Down
36 changes: 21 additions & 15 deletions engine/src/components/gui/textinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ TextInput::TextInput() :
m_currentFade(1.0f),
m_cursorBlinkRate(0.85f),
m_cursorBlinkCurrent(0.0f),
m_hovered(false) {
m_hovered(false),
m_focused(false) {
}

Frame *TextInput::background() const {
Expand Down Expand Up @@ -76,6 +77,9 @@ string TextInput::text() const {
void TextInput::setText(const string text) {
if(m_label) {
m_label->setText(text);
u32string u32 = Utils::utf8ToUtf32(text);
m_cursorPosition = u32.size();
recalcCursor();
}
}

Expand Down Expand Up @@ -112,7 +116,7 @@ void TextInput::update() {
u32string u32 = Utils::utf8ToUtf32(text());
bool isBackspace = Input::isKeyDown(Input::KEY_BACKSPACE);
if(isBackspace || Input::isKeyDown(Input::KEY_DELETE)) {
if(isBackspace && m_cursorPosition > 0) {
if(isBackspace && m_cursorPosition >= 0) {
m_cursorPosition--;
}
if(m_cursorPosition >= 0) {
Expand All @@ -128,6 +132,8 @@ void TextInput::update() {
} else if(Input::isKeyDown(Input::KEY_RIGHT) && m_cursorPosition < u32.size()) {
m_cursorPosition++;
recalcCursor();
} else if(Input::isKeyDown(Input::KEY_ENTER) || Input::isKeyDown(Input::KEY_KP_ENTER)) {
emitSignal(_SIGNAL(editingFinished()));
} else {
string sub = Input::inputString();
sub.erase(remove_if(sub.begin(), sub.end(), [](unsigned char c) { return (c >= 0 && c < 32);}), sub.end());
Expand All @@ -143,6 +149,11 @@ void TextInput::update() {
if(m_cursor) {
m_cursor->setEnabled(false);
}

if(m_focused) {
emitSignal(_SIGNAL(focusOut()));
m_focused = false;
}
}

bool hover = rectTransform()->isHovered(pos.x, pos.y);
Expand All @@ -155,7 +166,14 @@ void TextInput::update() {
color = m_highlightedColor;
if(Input::isMouseButtonDown(0) || (Input::touchCount() > 0 && Input::touchState(0) == Input::TOUCH_BEGAN)) {
m_currentFade = 0.0f;
onClicked();

Widget::setFocusWidget(this);

if(m_cursor) {
m_cursor->setEnabled(true);
}
emitSignal(_SIGNAL(focusIn()));
m_focused = true;
}

if(Input::isMouseButtonUp(0)) {
Expand Down Expand Up @@ -226,18 +244,6 @@ void TextInput::onReferenceDestroyed() {
return;
}
}
/*!
\internal
*/
void TextInput::onClicked() {
setFocusWidget(this);

if(m_cursor) {
m_cursor->setEnabled(true);
}

emitSignal(_SIGNAL(clicked()));
}

void TextInput::recalcCursor() {
if(m_label && m_cursor) {
Expand Down
10 changes: 7 additions & 3 deletions engine/src/components/textrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,15 +477,17 @@ Vector2 TextRender::cursorPosition(Font *font, int size, const string &text, int
if(font) {
float spaceWidth = font->spaceWidth() * size;
float spaceLine = font->lineHeight() * size;
float cursorMid = font->cursorWidth() * 0.5f * size;

string data = Engine::translate(text);
font->requestCharacters(data);

Vector2 pos(0.0, boundaries.y - size);

uint32_t length = font->length(data);
if(length) {
u32string u32 = Utils::utf8ToUtf32(data);

Vector2 pos(0.0, boundaries.y - size);
uint32_t previous = 0;
uint32_t it = 0;

Expand Down Expand Up @@ -521,9 +523,11 @@ Vector2 TextRender::cursorPosition(Font *font, int size, const string &text, int
}
previous = ch;
}

return pos;
}

pos.x -= cursorMid;

return pos;
}
return Vector2();
}
Expand Down
4 changes: 2 additions & 2 deletions engine/src/editor/editorplatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Input::KeyCode mapToInput(int32_t key) {
//result = Input::KEY_WORLD_1
//result = Input::KEY_WORLD_2
{ Qt::Key_Escape, Input::KEY_ESCAPE },
{ Qt::Key_Enter, Input::KEY_ENTER },
{ Qt::Key_Return, Input::KEY_ENTER },
{ Qt::Key_Tab, Input::KEY_TAB },
{ Qt::Key_Backspace, Input::KEY_BACKSPACE },
{ Qt::Key_Insert, Input::KEY_INSERT },
Expand Down Expand Up @@ -122,7 +122,7 @@ Input::KeyCode mapToInput(int32_t key) {
//result = Input::KEY_KP_MULTIPLY
//result = Input::KEY_KP_SUBTRACT
//result = Input::KEY_KP_ADD
//result = Input::KEY_KP_ENTER
{Qt::Key_Enter, Input::KEY_KP_ENTER },
//result = Input::KEY_KP_EQUAL
{ Qt::Key_Shift, Input::KEY_LEFT_SHIFT },
{ Qt::Key_Control, Input::KEY_LEFT_CONTROL },
Expand Down
44 changes: 34 additions & 10 deletions engine/src/resources/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class FontPrivate {
FontPrivate() :
m_face(nullptr),
m_scale(DF_GLYPH_SIZE),
m_spaceWidth(0.0f),
m_lineHeight(0.0f),
m_cursorWidth(0.0f),
m_useKerning(false) {
}

Expand All @@ -35,6 +38,10 @@ class FontPrivate {

int32_t m_scale;

float m_spaceWidth;
float m_lineHeight;
float m_cursorWidth;

bool m_useKerning;
};

Expand Down Expand Up @@ -232,23 +239,23 @@ int Font::length(const string &characters) const {
float Font::spaceWidth() const {
PROFILE_FUNCTION();

FT_Error error = FT_Load_Glyph( p_ptr->m_face, FT_Get_Char_Index( p_ptr->m_face, ' ' ), FT_LOAD_DEFAULT );
if(!error) {
return static_cast<float>(p_ptr->m_face->glyph->advance.x) / p_ptr->m_scale / 64.0f;
}
return 0;
return p_ptr->m_spaceWidth;
}
/*!
Returns visual height for the font in world units.
*/
float Font::lineHeight() const {
PROFILE_FUNCTION();

FT_Error error = FT_Load_Glyph( p_ptr->m_face, FT_Get_Char_Index( p_ptr->m_face, '\n' ), FT_LOAD_DEFAULT );
if(!error) {
return static_cast<float>(p_ptr->m_face->glyph->metrics.height) / p_ptr->m_scale / 32.0f;
}
return 0;
return p_ptr->m_lineHeight;
}
/*!
Returns visual width of the cursor for the font in world units.
*/
float Font::cursorWidth() const {
PROFILE_FUNCTION();

return p_ptr->m_cursorWidth;
}
/*!
\internal
Expand All @@ -273,6 +280,23 @@ void Font::loadUserData(const VariantMap &data) {
return;
}
p_ptr->m_useKerning = FT_HAS_KERNING( p_ptr->m_face );

error = FT_Load_Glyph( p_ptr->m_face, FT_Get_Char_Index( p_ptr->m_face, ' ' ), FT_LOAD_DEFAULT );
if(!error) {
p_ptr->m_spaceWidth = static_cast<float>(p_ptr->m_face->glyph->advance.x) / p_ptr->m_scale / 64.0f;
}

error = FT_Load_Glyph( p_ptr->m_face, FT_Get_Char_Index( p_ptr->m_face, '\n' ), FT_LOAD_DEFAULT );
if(!error) {
p_ptr->m_lineHeight = static_cast<float>(p_ptr->m_face->glyph->metrics.height) / p_ptr->m_scale / 32.0f;
}

error = FT_Load_Glyph( p_ptr->m_face, FT_Get_Char_Index( p_ptr->m_face, '|' ), FT_LOAD_DEFAULT );
if(!error) {
p_ptr->m_cursorWidth = static_cast<float>(p_ptr->m_face->glyph->advance.x) / p_ptr->m_scale / 64.0f;
}


}
}
}
Expand Down

0 comments on commit 73991ab

Please sign in to comment.