Fix mouse events sent to wrong GUI elements when dragging
[oweals/minetest.git] / src / itemstackmetadata.cpp
1 /*
2 Minetest
3 Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20
21 #include "itemstackmetadata.h"
22 #include "util/serialize.h"
23 #include "util/strfnd.h"
24
25 #define DESERIALIZE_START '\x01'
26 #define DESERIALIZE_KV_DELIM '\x02'
27 #define DESERIALIZE_PAIR_DELIM '\x03'
28 #define DESERIALIZE_START_STR "\x01"
29 #define DESERIALIZE_KV_DELIM_STR "\x02"
30 #define DESERIALIZE_PAIR_DELIM_STR "\x03"
31
32 #define TOOLCAP_KEY "tool_capabilities"
33
34 void ItemStackMetadata::clear()
35 {
36         Metadata::clear();
37         updateToolCapabilities();
38 }
39
40 bool ItemStackMetadata::setString(const std::string &name, const std::string &var)
41 {
42         bool result = Metadata::setString(name, var);
43         if (name == TOOLCAP_KEY)
44                 updateToolCapabilities();
45         return result;
46 }
47
48 void ItemStackMetadata::serialize(std::ostream &os) const
49 {
50         std::ostringstream os2;
51         os2 << DESERIALIZE_START;
52         for (const auto &stringvar : m_stringvars) {
53                 if (!stringvar.first.empty() || !stringvar.second.empty())
54                         os2 << stringvar.first << DESERIALIZE_KV_DELIM
55                                 << stringvar.second << DESERIALIZE_PAIR_DELIM;
56         }
57         os << serializeJsonStringIfNeeded(os2.str());
58 }
59
60 void ItemStackMetadata::deSerialize(std::istream &is)
61 {
62         std::string in = deSerializeJsonStringIfNeeded(is);
63
64         m_stringvars.clear();
65
66         if (!in.empty()) {
67                 if (in[0] == DESERIALIZE_START) {
68                         Strfnd fnd(in);
69                         fnd.to(1);
70                         while (!fnd.at_end()) {
71                                 std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
72                                 std::string var  = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
73                                 m_stringvars[name] = var;
74                         }
75                 } else {
76                         // BACKWARDS COMPATIBILITY
77                         m_stringvars[""] = in;
78                 }
79         }
80         updateToolCapabilities();
81 }
82
83 void ItemStackMetadata::updateToolCapabilities()
84 {
85         if (contains(TOOLCAP_KEY)) {
86                 toolcaps_overridden = true;
87                 toolcaps_override = ToolCapabilities();
88                 std::istringstream is(getString(TOOLCAP_KEY));
89                 toolcaps_override.deserializeJson(is);
90         } else {
91                 toolcaps_overridden = false;
92         }
93 }
94
95 void ItemStackMetadata::setToolCapabilities(const ToolCapabilities &caps)
96 {
97         std::ostringstream os;
98         caps.serializeJson(os);
99         setString(TOOLCAP_KEY, os.str());
100 }
101
102 void ItemStackMetadata::clearToolCapabilities()
103 {
104         setString(TOOLCAP_KEY, "");
105 }