Fix formspec focus (#12795)

This commit is contained in:
DS 2022-10-21 17:11:41 +02:00 committed by GitHub
parent 9f0d88407d
commit 7153cb8a0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 8 deletions

@ -3046,7 +3046,7 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
} }
} else { } else {
// Don't keep old focus value // Don't keep old focus value
m_focused_element.clear(); m_focused_element = nullopt;
} }
// Remove children // Remove children

@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "guiTable.h" #include "guiTable.h"
#include "network/networkprotocol.h" #include "network/networkprotocol.h"
#include "client/joystick_controller.h" #include "client/joystick_controller.h"
#include "util/Optional.h"
#include "util/string.h" #include "util/string.h"
#include "util/enriched_string.h" #include "util/enriched_string.h"
#include "StyleSpec.h" #include "StyleSpec.h"
@ -356,7 +357,7 @@ private:
TextDest *m_text_dst; TextDest *m_text_dst;
std::string m_last_formname; std::string m_last_formname;
u16 m_formspec_version = 1; u16 m_formspec_version = 1;
std::string m_focused_element = ""; Optional<std::string> m_focused_element = nullopt;
JoystickController *m_joystick; JoystickController *m_joystick;
bool m_show_debug = false; bool m_show_debug = false;

@ -103,3 +103,57 @@ public:
explicit operator bool() const { return m_has_value; } explicit operator bool() const { return m_has_value; }
}; };
template <typename T>
constexpr bool operator==(const Optional<T> &opt, nullopt_t)
{
return !opt.has_value();
}
template <typename T>
constexpr bool operator==(nullopt_t, const Optional<T> &opt)
{
return !opt.has_value();
}
template <typename T>
constexpr bool operator!=(const Optional<T> &opt, nullopt_t)
{
return opt.has_value();
}
template <typename T>
constexpr bool operator!=(nullopt_t, const Optional<T> &opt)
{
return opt.has_value();
}
template <typename T, typename U>
constexpr bool operator==(const Optional<T> &opt, const U &value)
{
return opt.has_value() && *opt == value;
}
template <typename T, typename U>
constexpr bool operator==(const T &value, const Optional<U> &opt)
{
return opt.has_value() && value == *opt;
}
template <typename T, typename U>
constexpr bool operator!=(const Optional<T> &opt, const U &value)
{
return !opt.has_value() || *opt != value;
}
template <typename T, typename U>
constexpr bool operator!=(const T &value, const Optional<U> &opt)
{
return !opt.has_value() || value != *opt;
}
template <typename T, typename U>
constexpr bool operator==(const Optional<T> &lhs, const Optional<U> &rhs)
{
return lhs.has_value() ? *lhs == rhs : nullopt == rhs;
}
template <typename T, typename U>
constexpr bool operator!=(const Optional<T> &lhs, const Optional<U> &rhs)
{
return lhs.has_value() ? *lhs != rhs : nullopt != rhs;
}