diff options
author | Loïc Blot <nerzhul@users.noreply.github.com> | 2021-01-13 09:05:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-13 09:05:09 +0100 |
commit | 4b012828213e3086f11afb3b94ce7aab85a52f55 (patch) | |
tree | e23dc7d1f72feecab374d6854185965e5ff5c718 /src/gui | |
parent | 1946835ee87bdd20c586f6bb79c422da1cad4685 (diff) | |
download | minetest-4b012828213e3086f11afb3b94ce7aab85a52f55.tar.gz minetest-4b012828213e3086f11afb3b94ce7aab85a52f55.tar.bz2 minetest-4b012828213e3086f11afb3b94ce7aab85a52f55.zip |
Factorize more guiEditBoxes code (#10789)
* Factorize more guiEditBoxes code
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/guiEditBox.cpp | 91 | ||||
-rw-r--r-- | src/gui/guiEditBox.h | 10 | ||||
-rw-r--r-- | src/gui/guiEditBoxWithScrollbar.cpp | 77 | ||||
-rw-r--r-- | src/gui/guiEditBoxWithScrollbar.h | 2 | ||||
-rw-r--r-- | src/gui/intlGUIEditBox.cpp | 99 | ||||
-rw-r--r-- | src/gui/intlGUIEditBox.h | 9 |
6 files changed, 105 insertions, 183 deletions
diff --git a/src/gui/guiEditBox.cpp b/src/gui/guiEditBox.cpp index 11d080be9..1214125a8 100644 --- a/src/gui/guiEditBox.cpp +++ b/src/gui/guiEditBox.cpp @@ -689,6 +689,46 @@ bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end return true; } +void GUIEditBox::inputChar(wchar_t c) +{ + if (!isEnabled() || !m_writable) + return; + + if (c != 0) { + if (Text.size() < m_max || m_max == 0) { + core::stringw s; + + if (m_mark_begin != m_mark_end) { + // clang-format off + // replace marked text + s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end; + s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin; + + s = Text.subString(0, real_begin); + s.append(c); + s.append(Text.subString(real_end, Text.size() - real_end)); + Text = s; + m_cursor_pos = real_begin + 1; + // clang-format on + } else { + // add new character + s = Text.subString(0, m_cursor_pos); + s.append(c); + s.append(Text.subString(m_cursor_pos, + Text.size() - m_cursor_pos)); + Text = s; + ++m_cursor_pos; + } + + m_blink_start_time = porting::getTimeMs(); + setTextMarkers(0, 0); + } + } + breakText(); + sendGuiEvent(EGET_EDITBOX_CHANGED); + calculateScrollPos(); +} + bool GUIEditBox::processMouse(const SEvent &event) { switch (event.MouseInput.Event) { @@ -817,3 +857,54 @@ void GUIEditBox::updateVScrollBar() } } } + +void GUIEditBox::deserializeAttributes( + io::IAttributes *in, io::SAttributeReadWriteOptions *options = 0) +{ + IGUIEditBox::deserializeAttributes(in, options); + + setOverrideColor(in->getAttributeAsColor("OverrideColor")); + enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled")); + setMax(in->getAttributeAsInt("MaxChars")); + setWordWrap(in->getAttributeAsBool("WordWrap")); + setMultiLine(in->getAttributeAsBool("MultiLine")); + setAutoScroll(in->getAttributeAsBool("AutoScroll")); + core::stringw ch = in->getAttributeAsStringW("PasswordChar"); + + if (ch.empty()) + setPasswordBox(in->getAttributeAsBool("PasswordBox")); + else + setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]); + + setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration( + "HTextAlign", GUIAlignmentNames), + (EGUI_ALIGNMENT)in->getAttributeAsEnumeration( + "VTextAlign", GUIAlignmentNames)); + + setWritable(in->getAttributeAsBool("Writable")); + // setOverrideFont(in->getAttributeAsFont("OverrideFont")); +} + +//! Writes attributes of the element. +void GUIEditBox::serializeAttributes( + io::IAttributes *out, io::SAttributeReadWriteOptions *options = 0) const +{ + // IGUIEditBox::serializeAttributes(out,options); + + out->addBool("OverrideColorEnabled", m_override_color_enabled); + out->addColor("OverrideColor", m_override_color); + // out->addFont("OverrideFont",m_override_font); + out->addInt("MaxChars", m_max); + out->addBool("WordWrap", m_word_wrap); + out->addBool("MultiLine", m_multiline); + out->addBool("AutoScroll", m_autoscroll); + out->addBool("PasswordBox", m_passwordbox); + core::stringw ch = L" "; + ch[0] = m_passwordchar; + out->addString("PasswordChar", ch.c_str()); + out->addEnum("HTextAlign", m_halign, GUIAlignmentNames); + out->addEnum("VTextAlign", m_valign, GUIAlignmentNames); + out->addBool("Writable", m_writable); + + IGUIEditBox::serializeAttributes(out, options); +} diff --git a/src/gui/guiEditBox.h b/src/gui/guiEditBox.h index 3e41c7e51..a20eb61d7 100644 --- a/src/gui/guiEditBox.h +++ b/src/gui/guiEditBox.h @@ -129,6 +129,14 @@ public: //! called if an event happened. virtual bool OnEvent(const SEvent &event); + //! Writes attributes of the element. + virtual void serializeAttributes(io::IAttributes *out, + io::SAttributeReadWriteOptions *options) const; + + //! Reads attributes of the element + virtual void deserializeAttributes( + io::IAttributes *in, io::SAttributeReadWriteOptions *options); + protected: virtual void breakText() = 0; @@ -147,7 +155,7 @@ protected: virtual s32 getCursorPos(s32 x, s32 y) = 0; bool processKey(const SEvent &event); - virtual void inputChar(wchar_t c) = 0; + virtual void inputChar(wchar_t c); //! returns the line number that the cursor is on s32 getLineFromPos(s32 pos); diff --git a/src/gui/guiEditBoxWithScrollbar.cpp b/src/gui/guiEditBoxWithScrollbar.cpp index 707dbb7db..c72070787 100644 --- a/src/gui/guiEditBoxWithScrollbar.cpp +++ b/src/gui/guiEditBoxWithScrollbar.cpp @@ -481,44 +481,6 @@ void GUIEditBoxWithScrollBar::setTextRect(s32 line) m_current_text_rect += m_frame_rect.UpperLeftCorner; } - -void GUIEditBoxWithScrollBar::inputChar(wchar_t c) -{ - if (!isEnabled()) - return; - - if (c != 0) { - if (Text.size() < m_max || m_max == 0) { - core::stringw s; - - if (m_mark_begin != m_mark_end) { - // replace marked text - const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end; - const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin; - - s = Text.subString(0, realmbgn); - s.append(c); - s.append(Text.subString(realmend, Text.size() - realmend)); - Text = s; - m_cursor_pos = realmbgn + 1; - } else { - // add new character - s = Text.subString(0, m_cursor_pos); - s.append(c); - s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos)); - Text = s; - ++m_cursor_pos; - } - - m_blink_start_time = porting::getTimeMs(); - setTextMarkers(0, 0); - } - } - breakText(); - calculateScrollPos(); - sendGuiEvent(EGET_EDITBOX_CHANGED); -} - // calculate autoscroll void GUIEditBoxWithScrollBar::calculateScrollPos() { @@ -682,54 +644,21 @@ void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color) //! Writes attributes of the element. void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const { - // IGUIEditBox::serializeAttributes(out,options); - out->addBool("Border", m_border); out->addBool("Background", m_background); - out->addBool("OverrideColorEnabled", m_override_color_enabled); - out->addColor("OverrideColor", m_override_color); // out->addFont("OverrideFont", OverrideFont); - out->addInt("MaxChars", m_max); - out->addBool("WordWrap", m_word_wrap); - out->addBool("MultiLine", m_multiline); - out->addBool("AutoScroll", m_autoscroll); - out->addBool("PasswordBox", m_passwordbox); - core::stringw ch = L" "; - ch[0] = m_passwordchar; - out->addString("PasswordChar", ch.c_str()); - out->addEnum("HTextAlign", m_halign, GUIAlignmentNames); - out->addEnum("VTextAlign", m_valign, GUIAlignmentNames); - out->addBool("Writable", m_writable); - - IGUIEditBox::serializeAttributes(out, options); + + GUIEditBox::serializeAttributes(out, options); } //! Reads attributes of the element void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0) { - IGUIEditBox::deserializeAttributes(in, options); + GUIEditBox::deserializeAttributes(in, options); setDrawBorder(in->getAttributeAsBool("Border")); setDrawBackground(in->getAttributeAsBool("Background")); - setOverrideColor(in->getAttributeAsColor("OverrideColor")); - enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled")); - setMax(in->getAttributeAsInt("MaxChars")); - setWordWrap(in->getAttributeAsBool("WordWrap")); - setMultiLine(in->getAttributeAsBool("MultiLine")); - setAutoScroll(in->getAttributeAsBool("AutoScroll")); - core::stringw ch = in->getAttributeAsStringW("PasswordChar"); - - if (!ch.size()) - setPasswordBox(in->getAttributeAsBool("PasswordBox")); - else - setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]); - - setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames), - (EGUI_ALIGNMENT)in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames)); - - // setOverrideFont(in->getAttributeAsFont("OverrideFont")); - setWritable(in->getAttributeAsBool("Writable")); } bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; } diff --git a/src/gui/guiEditBoxWithScrollbar.h b/src/gui/guiEditBoxWithScrollbar.h index b863ee614..3f7450dcb 100644 --- a/src/gui/guiEditBoxWithScrollbar.h +++ b/src/gui/guiEditBoxWithScrollbar.h @@ -49,8 +49,6 @@ protected: virtual void breakText(); //! sets the area of the given line virtual void setTextRect(s32 line); - //! adds a letter to the edit box - virtual void inputChar(wchar_t c); //! calculates the current scroll position void calculateScrollPos(); //! calculated the FrameRect diff --git a/src/gui/intlGUIEditBox.cpp b/src/gui/intlGUIEditBox.cpp index a61619148..0f09ea746 100644 --- a/src/gui/intlGUIEditBox.cpp +++ b/src/gui/intlGUIEditBox.cpp @@ -318,10 +318,7 @@ void intlGUIEditBox::draw() s32 intlGUIEditBox::getCursorPos(s32 x, s32 y) { - IGUIFont* font = m_override_font; - IGUISkin* skin = Environment->getSkin(); - if (!m_override_font) - font = skin->getFont(); + IGUIFont* font = getActiveFont(); const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1; @@ -547,49 +544,6 @@ void intlGUIEditBox::setTextRect(s32 line) } -void intlGUIEditBox::inputChar(wchar_t c) -{ - if (!isEnabled() || !m_writable) - return; - - if (c != 0) - { - if (Text.size() < m_max || m_max == 0) - { - core::stringw s; - - if (m_mark_begin != m_mark_end) - { - // replace marked text - const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end; - const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin; - - s = Text.subString(0, realmbgn); - s.append(c); - s.append( Text.subString(realmend, Text.size()-realmend) ); - Text = s; - m_cursor_pos = realmbgn+1; - } - else - { - // add new character - s = Text.subString(0, m_cursor_pos); - s.append(c); - s.append( Text.subString(m_cursor_pos, Text.size()-m_cursor_pos) ); - Text = s; - ++m_cursor_pos; - } - - m_blink_start_time = porting::getTimeMs(); - setTextMarkers(0, 0); - } - } - breakText(); - sendGuiEvent(EGET_EDITBOX_CHANGED); - calculateScrollPos(); -} - - void intlGUIEditBox::calculateScrollPos() { if (!m_autoscroll) @@ -668,56 +622,5 @@ void intlGUIEditBox::createVScrollBar() m_vscrollbar->setLargeStep(10 * fontHeight); } - -//! Writes attributes of the element. -void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const -{ - // IGUIEditBox::serializeAttributes(out,options); - - out->addBool ("OverrideColorEnabled", m_override_color_enabled ); - out->addColor ("OverrideColor", m_override_color); - // out->addFont("OverrideFont",m_override_font); - out->addInt ("MaxChars", m_max); - out->addBool ("WordWrap", m_word_wrap); - out->addBool ("MultiLine", m_multiline); - out->addBool ("AutoScroll", m_autoscroll); - out->addBool ("PasswordBox", m_passwordbox); - core::stringw ch = L" "; - ch[0] = m_passwordchar; - out->addString("PasswordChar", ch.c_str()); - out->addEnum ("HTextAlign", m_halign, GUIAlignmentNames); - out->addEnum ("VTextAlign", m_valign, GUIAlignmentNames); - out->addBool ("Writable", m_writable); - - IGUIEditBox::serializeAttributes(out,options); -} - - -//! Reads attributes of the element -void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) -{ - IGUIEditBox::deserializeAttributes(in,options); - - setOverrideColor(in->getAttributeAsColor("OverrideColor")); - enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled")); - setMax(in->getAttributeAsInt("MaxChars")); - setWordWrap(in->getAttributeAsBool("WordWrap")); - setMultiLine(in->getAttributeAsBool("MultiLine")); - setAutoScroll(in->getAttributeAsBool("AutoScroll")); - core::stringw ch = in->getAttributeAsStringW("PasswordChar"); - - if (ch.empty()) - setPasswordBox(in->getAttributeAsBool("PasswordBox")); - else - setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]); - - setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames), - (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames)); - - setWritable(in->getAttributeAsBool("Writable")); - // setOverrideFont(in->getAttributeAsFont("OverrideFont")); -} - - } // end namespace gui } // end namespace irr diff --git a/src/gui/intlGUIEditBox.h b/src/gui/intlGUIEditBox.h index 2abc12d1a..007fe1c93 100644 --- a/src/gui/intlGUIEditBox.h +++ b/src/gui/intlGUIEditBox.h @@ -38,12 +38,6 @@ namespace gui //! Updates the absolute position, splits text if required virtual void updateAbsolutePosition(); - //! Writes attributes of the element. - virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const; - - //! Reads attributes of the element - virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options); - virtual void setCursorChar(const wchar_t cursorChar) {} virtual wchar_t getCursorChar() const { return L'|'; } @@ -57,8 +51,7 @@ namespace gui virtual void breakText(); //! sets the area of the given line virtual void setTextRect(s32 line); - //! adds a letter to the edit box - virtual void inputChar(wchar_t c); + //! calculates the current scroll position void calculateScrollPos(); |