Skip to content

Commit 4b01282

Browse files
authoredJan 13, 2021
Factorize more guiEditBoxes code (#10789)
* Factorize more guiEditBoxes code
1 parent 1946835 commit 4b01282

6 files changed

+105
-183
lines changed
 

Diff for: ‎src/gui/guiEditBox.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,46 @@ bool GUIEditBox::onKeyDelete(const SEvent &event, s32 &mark_begin, s32 &mark_end
689689
return true;
690690
}
691691

692+
void GUIEditBox::inputChar(wchar_t c)
693+
{
694+
if (!isEnabled() || !m_writable)
695+
return;
696+
697+
if (c != 0) {
698+
if (Text.size() < m_max || m_max == 0) {
699+
core::stringw s;
700+
701+
if (m_mark_begin != m_mark_end) {
702+
// clang-format off
703+
// replace marked text
704+
s32 real_begin = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
705+
s32 real_end = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
706+
707+
s = Text.subString(0, real_begin);
708+
s.append(c);
709+
s.append(Text.subString(real_end, Text.size() - real_end));
710+
Text = s;
711+
m_cursor_pos = real_begin + 1;
712+
// clang-format on
713+
} else {
714+
// add new character
715+
s = Text.subString(0, m_cursor_pos);
716+
s.append(c);
717+
s.append(Text.subString(m_cursor_pos,
718+
Text.size() - m_cursor_pos));
719+
Text = s;
720+
++m_cursor_pos;
721+
}
722+
723+
m_blink_start_time = porting::getTimeMs();
724+
setTextMarkers(0, 0);
725+
}
726+
}
727+
breakText();
728+
sendGuiEvent(EGET_EDITBOX_CHANGED);
729+
calculateScrollPos();
730+
}
731+
692732
bool GUIEditBox::processMouse(const SEvent &event)
693733
{
694734
switch (event.MouseInput.Event) {
@@ -817,3 +857,54 @@ void GUIEditBox::updateVScrollBar()
817857
}
818858
}
819859
}
860+
861+
void GUIEditBox::deserializeAttributes(
862+
io::IAttributes *in, io::SAttributeReadWriteOptions *options = 0)
863+
{
864+
IGUIEditBox::deserializeAttributes(in, options);
865+
866+
setOverrideColor(in->getAttributeAsColor("OverrideColor"));
867+
enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
868+
setMax(in->getAttributeAsInt("MaxChars"));
869+
setWordWrap(in->getAttributeAsBool("WordWrap"));
870+
setMultiLine(in->getAttributeAsBool("MultiLine"));
871+
setAutoScroll(in->getAttributeAsBool("AutoScroll"));
872+
core::stringw ch = in->getAttributeAsStringW("PasswordChar");
873+
874+
if (ch.empty())
875+
setPasswordBox(in->getAttributeAsBool("PasswordBox"));
876+
else
877+
setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
878+
879+
setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
880+
"HTextAlign", GUIAlignmentNames),
881+
(EGUI_ALIGNMENT)in->getAttributeAsEnumeration(
882+
"VTextAlign", GUIAlignmentNames));
883+
884+
setWritable(in->getAttributeAsBool("Writable"));
885+
// setOverrideFont(in->getAttributeAsFont("OverrideFont"));
886+
}
887+
888+
//! Writes attributes of the element.
889+
void GUIEditBox::serializeAttributes(
890+
io::IAttributes *out, io::SAttributeReadWriteOptions *options = 0) const
891+
{
892+
// IGUIEditBox::serializeAttributes(out,options);
893+
894+
out->addBool("OverrideColorEnabled", m_override_color_enabled);
895+
out->addColor("OverrideColor", m_override_color);
896+
// out->addFont("OverrideFont",m_override_font);
897+
out->addInt("MaxChars", m_max);
898+
out->addBool("WordWrap", m_word_wrap);
899+
out->addBool("MultiLine", m_multiline);
900+
out->addBool("AutoScroll", m_autoscroll);
901+
out->addBool("PasswordBox", m_passwordbox);
902+
core::stringw ch = L" ";
903+
ch[0] = m_passwordchar;
904+
out->addString("PasswordChar", ch.c_str());
905+
out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
906+
out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
907+
out->addBool("Writable", m_writable);
908+
909+
IGUIEditBox::serializeAttributes(out, options);
910+
}

Diff for: ‎src/gui/guiEditBox.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ class GUIEditBox : public IGUIEditBox
129129
//! called if an event happened.
130130
virtual bool OnEvent(const SEvent &event);
131131

132+
//! Writes attributes of the element.
133+
virtual void serializeAttributes(io::IAttributes *out,
134+
io::SAttributeReadWriteOptions *options) const;
135+
136+
//! Reads attributes of the element
137+
virtual void deserializeAttributes(
138+
io::IAttributes *in, io::SAttributeReadWriteOptions *options);
139+
132140
protected:
133141
virtual void breakText() = 0;
134142

@@ -147,7 +155,7 @@ class GUIEditBox : public IGUIEditBox
147155
virtual s32 getCursorPos(s32 x, s32 y) = 0;
148156

149157
bool processKey(const SEvent &event);
150-
virtual void inputChar(wchar_t c) = 0;
158+
virtual void inputChar(wchar_t c);
151159

152160
//! returns the line number that the cursor is on
153161
s32 getLineFromPos(s32 pos);

Diff for: ‎src/gui/guiEditBoxWithScrollbar.cpp

+3-74
Original file line numberDiff line numberDiff line change
@@ -481,44 +481,6 @@ void GUIEditBoxWithScrollBar::setTextRect(s32 line)
481481
m_current_text_rect += m_frame_rect.UpperLeftCorner;
482482
}
483483

484-
485-
void GUIEditBoxWithScrollBar::inputChar(wchar_t c)
486-
{
487-
if (!isEnabled())
488-
return;
489-
490-
if (c != 0) {
491-
if (Text.size() < m_max || m_max == 0) {
492-
core::stringw s;
493-
494-
if (m_mark_begin != m_mark_end) {
495-
// replace marked text
496-
const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
497-
const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
498-
499-
s = Text.subString(0, realmbgn);
500-
s.append(c);
501-
s.append(Text.subString(realmend, Text.size() - realmend));
502-
Text = s;
503-
m_cursor_pos = realmbgn + 1;
504-
} else {
505-
// add new character
506-
s = Text.subString(0, m_cursor_pos);
507-
s.append(c);
508-
s.append(Text.subString(m_cursor_pos, Text.size() - m_cursor_pos));
509-
Text = s;
510-
++m_cursor_pos;
511-
}
512-
513-
m_blink_start_time = porting::getTimeMs();
514-
setTextMarkers(0, 0);
515-
}
516-
}
517-
breakText();
518-
calculateScrollPos();
519-
sendGuiEvent(EGET_EDITBOX_CHANGED);
520-
}
521-
522484
// calculate autoscroll
523485
void GUIEditBoxWithScrollBar::calculateScrollPos()
524486
{
@@ -682,54 +644,21 @@ void GUIEditBoxWithScrollBar::setBackgroundColor(const video::SColor &bg_color)
682644
//! Writes attributes of the element.
683645
void GUIEditBoxWithScrollBar::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options = 0) const
684646
{
685-
// IGUIEditBox::serializeAttributes(out,options);
686-
687647
out->addBool("Border", m_border);
688648
out->addBool("Background", m_background);
689-
out->addBool("OverrideColorEnabled", m_override_color_enabled);
690-
out->addColor("OverrideColor", m_override_color);
691649
// out->addFont("OverrideFont", OverrideFont);
692-
out->addInt("MaxChars", m_max);
693-
out->addBool("WordWrap", m_word_wrap);
694-
out->addBool("MultiLine", m_multiline);
695-
out->addBool("AutoScroll", m_autoscroll);
696-
out->addBool("PasswordBox", m_passwordbox);
697-
core::stringw ch = L" ";
698-
ch[0] = m_passwordchar;
699-
out->addString("PasswordChar", ch.c_str());
700-
out->addEnum("HTextAlign", m_halign, GUIAlignmentNames);
701-
out->addEnum("VTextAlign", m_valign, GUIAlignmentNames);
702-
out->addBool("Writable", m_writable);
703-
704-
IGUIEditBox::serializeAttributes(out, options);
650+
651+
GUIEditBox::serializeAttributes(out, options);
705652
}
706653

707654

708655
//! Reads attributes of the element
709656
void GUIEditBoxWithScrollBar::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options = 0)
710657
{
711-
IGUIEditBox::deserializeAttributes(in, options);
658+
GUIEditBox::deserializeAttributes(in, options);
712659

713660
setDrawBorder(in->getAttributeAsBool("Border"));
714661
setDrawBackground(in->getAttributeAsBool("Background"));
715-
setOverrideColor(in->getAttributeAsColor("OverrideColor"));
716-
enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
717-
setMax(in->getAttributeAsInt("MaxChars"));
718-
setWordWrap(in->getAttributeAsBool("WordWrap"));
719-
setMultiLine(in->getAttributeAsBool("MultiLine"));
720-
setAutoScroll(in->getAttributeAsBool("AutoScroll"));
721-
core::stringw ch = in->getAttributeAsStringW("PasswordChar");
722-
723-
if (!ch.size())
724-
setPasswordBox(in->getAttributeAsBool("PasswordBox"));
725-
else
726-
setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
727-
728-
setTextAlignment((EGUI_ALIGNMENT)in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
729-
(EGUI_ALIGNMENT)in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
730-
731-
// setOverrideFont(in->getAttributeAsFont("OverrideFont"));
732-
setWritable(in->getAttributeAsBool("Writable"));
733662
}
734663

735664
bool GUIEditBoxWithScrollBar::isDrawBackgroundEnabled() const { return false; }

Diff for: ‎src/gui/guiEditBoxWithScrollbar.h

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ class GUIEditBoxWithScrollBar : public GUIEditBox
4949
virtual void breakText();
5050
//! sets the area of the given line
5151
virtual void setTextRect(s32 line);
52-
//! adds a letter to the edit box
53-
virtual void inputChar(wchar_t c);
5452
//! calculates the current scroll position
5553
void calculateScrollPos();
5654
//! calculated the FrameRect

Diff for: ‎src/gui/intlGUIEditBox.cpp

+1-98
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,7 @@ void intlGUIEditBox::draw()
318318

319319
s32 intlGUIEditBox::getCursorPos(s32 x, s32 y)
320320
{
321-
IGUIFont* font = m_override_font;
322-
IGUISkin* skin = Environment->getSkin();
323-
if (!m_override_font)
324-
font = skin->getFont();
321+
IGUIFont* font = getActiveFont();
325322

326323
const u32 lineCount = (m_word_wrap || m_multiline) ? m_broken_text.size() : 1;
327324

@@ -547,49 +544,6 @@ void intlGUIEditBox::setTextRect(s32 line)
547544

548545
}
549546

550-
void intlGUIEditBox::inputChar(wchar_t c)
551-
{
552-
if (!isEnabled() || !m_writable)
553-
return;
554-
555-
if (c != 0)
556-
{
557-
if (Text.size() < m_max || m_max == 0)
558-
{
559-
core::stringw s;
560-
561-
if (m_mark_begin != m_mark_end)
562-
{
563-
// replace marked text
564-
const s32 realmbgn = m_mark_begin < m_mark_end ? m_mark_begin : m_mark_end;
565-
const s32 realmend = m_mark_begin < m_mark_end ? m_mark_end : m_mark_begin;
566-
567-
s = Text.subString(0, realmbgn);
568-
s.append(c);
569-
s.append( Text.subString(realmend, Text.size()-realmend) );
570-
Text = s;
571-
m_cursor_pos = realmbgn+1;
572-
}
573-
else
574-
{
575-
// add new character
576-
s = Text.subString(0, m_cursor_pos);
577-
s.append(c);
578-
s.append( Text.subString(m_cursor_pos, Text.size()-m_cursor_pos) );
579-
Text = s;
580-
++m_cursor_pos;
581-
}
582-
583-
m_blink_start_time = porting::getTimeMs();
584-
setTextMarkers(0, 0);
585-
}
586-
}
587-
breakText();
588-
sendGuiEvent(EGET_EDITBOX_CHANGED);
589-
calculateScrollPos();
590-
}
591-
592-
593547
void intlGUIEditBox::calculateScrollPos()
594548
{
595549
if (!m_autoscroll)
@@ -668,56 +622,5 @@ void intlGUIEditBox::createVScrollBar()
668622
m_vscrollbar->setLargeStep(10 * fontHeight);
669623
}
670624

671-
672-
//! Writes attributes of the element.
673-
void intlGUIEditBox::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
674-
{
675-
// IGUIEditBox::serializeAttributes(out,options);
676-
677-
out->addBool ("OverrideColorEnabled", m_override_color_enabled );
678-
out->addColor ("OverrideColor", m_override_color);
679-
// out->addFont("OverrideFont",m_override_font);
680-
out->addInt ("MaxChars", m_max);
681-
out->addBool ("WordWrap", m_word_wrap);
682-
out->addBool ("MultiLine", m_multiline);
683-
out->addBool ("AutoScroll", m_autoscroll);
684-
out->addBool ("PasswordBox", m_passwordbox);
685-
core::stringw ch = L" ";
686-
ch[0] = m_passwordchar;
687-
out->addString("PasswordChar", ch.c_str());
688-
out->addEnum ("HTextAlign", m_halign, GUIAlignmentNames);
689-
out->addEnum ("VTextAlign", m_valign, GUIAlignmentNames);
690-
out->addBool ("Writable", m_writable);
691-
692-
IGUIEditBox::serializeAttributes(out,options);
693-
}
694-
695-
696-
//! Reads attributes of the element
697-
void intlGUIEditBox::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
698-
{
699-
IGUIEditBox::deserializeAttributes(in,options);
700-
701-
setOverrideColor(in->getAttributeAsColor("OverrideColor"));
702-
enableOverrideColor(in->getAttributeAsBool("OverrideColorEnabled"));
703-
setMax(in->getAttributeAsInt("MaxChars"));
704-
setWordWrap(in->getAttributeAsBool("WordWrap"));
705-
setMultiLine(in->getAttributeAsBool("MultiLine"));
706-
setAutoScroll(in->getAttributeAsBool("AutoScroll"));
707-
core::stringw ch = in->getAttributeAsStringW("PasswordChar");
708-
709-
if (ch.empty())
710-
setPasswordBox(in->getAttributeAsBool("PasswordBox"));
711-
else
712-
setPasswordBox(in->getAttributeAsBool("PasswordBox"), ch[0]);
713-
714-
setTextAlignment( (EGUI_ALIGNMENT) in->getAttributeAsEnumeration("HTextAlign", GUIAlignmentNames),
715-
(EGUI_ALIGNMENT) in->getAttributeAsEnumeration("VTextAlign", GUIAlignmentNames));
716-
717-
setWritable(in->getAttributeAsBool("Writable"));
718-
// setOverrideFont(in->getAttributeAsFont("OverrideFont"));
719-
}
720-
721-
722625
} // end namespace gui
723626
} // end namespace irr

Diff for: ‎src/gui/intlGUIEditBox.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ namespace gui
3838
//! Updates the absolute position, splits text if required
3939
virtual void updateAbsolutePosition();
4040

41-
//! Writes attributes of the element.
42-
virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
43-
44-
//! Reads attributes of the element
45-
virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options);
46-
4741
virtual void setCursorChar(const wchar_t cursorChar) {}
4842

4943
virtual wchar_t getCursorChar() const { return L'|'; }
@@ -57,8 +51,7 @@ namespace gui
5751
virtual void breakText();
5852
//! sets the area of the given line
5953
virtual void setTextRect(s32 line);
60-
//! adds a letter to the edit box
61-
virtual void inputChar(wchar_t c);
54+
6255
//! calculates the current scroll position
6356
void calculateScrollPos();
6457

0 commit comments

Comments
 (0)
Please sign in to comment.