]> git.lizzy.rs Git - minetest.git/blob - src/nodemetadata.h
Generic NodeMetadata text input
[minetest.git] / src / nodemetadata.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 #ifndef NODEMETADATA_HEADER
21 #define NODEMETADATA_HEADER
22
23 #include "common_irrlicht.h"
24 #include <string>
25 #include <iostream>
26
27 /*
28         NodeMetadata stores arbitary amounts of data for special blocks.
29         Used for furnaces, chests and signs.
30
31         There are two interaction methods: inventory menu and text input.
32         Only one can be used for a single metadata, thus only inventory OR
33         text input should exist in a metadata.
34 */
35
36 class Inventory;
37
38 class NodeMetadata
39 {
40 public:
41         typedef NodeMetadata* (*Factory)(std::istream&);
42
43         NodeMetadata();
44         virtual ~NodeMetadata();
45         
46         static NodeMetadata* deSerialize(std::istream &is);
47         void serialize(std::ostream &os);
48         
49         // This usually is the CONTENT_ value
50         virtual u16 typeId() const = 0;
51         virtual NodeMetadata* clone() = 0;
52         virtual void serializeBody(std::ostream &os) = 0;
53         virtual std::string infoText() {return "";}
54         virtual Inventory* getInventory() {return NULL;}
55         // This is called always after the inventory is modified, before
56         // the changes are copied elsewhere
57         virtual void inventoryModified(){}
58         // A step in time. Returns true if metadata changed.
59         virtual bool step(float dtime) {return false;}
60         virtual bool nodeRemovalDisabled(){return false;}
61         // Used to make custom inventory menus.
62         // See format in guiInventoryMenu.cpp.
63         virtual std::string getInventoryDrawSpecString(){return "";}
64         // primarily used for locking chests, but others can play too
65         virtual std::string getOwner(){ return std::string(""); }
66         virtual void setOwner(std::string t){}
67         virtual bool allowsTextInput(){ return false; }
68         virtual std::string getText(){ return ""; }
69         virtual void setText(const std::string &t){}
70 protected:
71         static void registerType(u16 id, Factory f);
72 private:
73         static core::map<u16, Factory> m_types;
74 };
75
76 /*
77         List of metadata of all the nodes of a block
78 */
79
80 class NodeMetadataList
81 {
82 public:
83         ~NodeMetadataList();
84
85         void serialize(std::ostream &os);
86         void deSerialize(std::istream &is);
87         
88         // Get pointer to data
89         NodeMetadata* get(v3s16 p);
90         // Deletes data
91         void remove(v3s16 p);
92         // Deletes old data and sets a new one
93         void set(v3s16 p, NodeMetadata *d);
94         
95         // A step in time. Returns true if something changed.
96         bool step(float dtime);
97
98 private:
99         core::map<v3s16, NodeMetadata*> m_data;
100 };
101
102 #endif
103