Skip to content

Commit

Permalink
Added proper handling of nullptrs in numerical control fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Semphris authored and HybridDog committed Jan 14, 2021
1 parent d1cffe0 commit ce5bef4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/interface/control_textbox_float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@

ControlTextboxFloat::ControlTextboxFloat() :
m_validate_float(),
m_value()
m_value(nullptr)
{
static float value = 0.f;
m_value = &value;
revert_value();
}

Expand All @@ -39,6 +37,9 @@ ControlTextboxFloat::update(float dt_sec, const Controller& controller)
bool
ControlTextboxFloat::parse_value(bool call_on_change /* = true (see header */)
{
if (!m_value)
return false;

// Calling super will put the correct value in m_string.
if (!ControlTextbox::parse_value(false)) {
// If the parent has failed, abandon. Keeping parsing should still result
Expand Down Expand Up @@ -79,6 +80,9 @@ ControlTextboxFloat::parse_value(bool call_on_change /* = true (see header */)
void
ControlTextboxFloat::revert_value()
{
if (!m_value)
return;

m_internal_string_backup = std::to_string(*m_value);

// Remove the trailing zeroes at the end of the decimal point...
Expand Down
10 changes: 7 additions & 3 deletions src/interface/control_textbox_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@

ControlTextboxInt::ControlTextboxInt() :
m_validate_int(),
m_value()
m_value(nullptr)
{
static int value = 0;
m_value = &value;
revert_value();
}

Expand All @@ -39,6 +37,9 @@ ControlTextboxInt::update(float dt_sec, const Controller& controller)
bool
ControlTextboxInt::parse_value(bool call_on_change /* = true (see header */)
{
if (!m_value)
return false;

// Calling super will put the correct value in m_string.
if (!ControlTextbox::parse_value(false)) {
// If the parent has failed, abandon. Keeping parsing should still result
Expand Down Expand Up @@ -79,6 +80,9 @@ ControlTextboxInt::parse_value(bool call_on_change /* = true (see header */)
void
ControlTextboxInt::revert_value()
{
if (!m_value)
return;

m_internal_string_backup = std::to_string(*m_value);
ControlTextbox::revert_value();
}
Expand Down

0 comments on commit ce5bef4

Please sign in to comment.