| 1 | // tAttribute.h  |
| 2 | //  |
| 3 | // All scene objects may have attributes. Attributes are extra data (that has a type) that the content author  |
| 4 | // attributed to the scene object.  |
| 5 | //  |
| 6 | // Copyright (c) 2006, 2017 Tristan Grimmer.  |
| 7 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
| 8 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
| 9 | //  |
| 10 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
| 11 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
| 12 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
| 13 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
| 14 | // PERFORMANCE OF THIS SOFTWARE.  |
| 15 |   |
| 16 | #pragma once  |
| 17 | #include <System/tChunk.h>  |
| 18 | namespace tScene  |
| 19 | {  |
| 20 |   |
| 21 |   |
| 22 | class tAttribute  |
| 23 | {  |
| 24 | public:  |
| 25 | tAttribute() { Clear(); }  |
| 26 | tAttribute(const tChunk& chunk) { Clear(); Load(chunk); }  |
| 27 | virtual ~tAttribute() { }  |
| 28 |   |
| 29 | enum class tType  |
| 30 | {  |
| 31 | Invalid = -1,  |
| 32 | Bool,  |
| 33 | Int,  |
| 34 | Colour, // 4 byte colour.  |
| 35 | Float,  |
| 36 | NumTypes  |
| 37 | };  |
| 38 |   |
| 39 | void Clear() { Name.Clear(); Type = tType::Invalid; Value = 0; }  |
| 40 | bool IsValid() const { return (Type != tType::Invalid) ? true : false; }  |
| 41 | void Save(tChunkWriter&) const;  |
| 42 | void Load(const tChunk&);  |
| 43 |   |
| 44 | bool IsBool() const { return (Type == tType::Bool) ? true : false; }  |
| 45 | bool IsInt() const { return (Type == tType::Int) ? true : false; }  |
| 46 | bool IsColour() const { return (Type == tType::Colour) ? true : false; }  |
| 47 | bool IsFloat() const { return (Type == tType::Float) ? true : false; }  |
| 48 |   |
| 49 | bool GetAsBool() const { if (Type != tType::Bool) return false; return Value ? true : false; }  |
| 50 | int GetAsInt() const { if (Type != tType::Int) return 0; return int(Value); }  |
| 51 | tColouri GetAsColour() const { if (Type != tType::Colour) return tColour::black; return *((tColouri*)(&Value)); }  |
| 52 | float GetAsFloat() const { if (Type != tType::Float) return 0.0f; return *((float*)(&Value)); }  |
| 53 |   |
| 54 | operator bool() const { return GetAsBool(); }  |
| 55 | operator int() const { return GetAsInt(); }  |
| 56 | operator tColouri() const { return GetAsColour(); }  |
| 57 | operator float() const { return GetAsFloat(); }  |
| 58 | bool operator==(const tAttribute& a) const { if (Name != a.Name) return false; if (Type != a.Type) return false; if (Value != a.Value) return false; return true; }  |
| 59 | bool operator!=(const tAttribute& a) const { return !(*this == a); }  |
| 60 | tAttribute& operator=(const tAttribute& a) { Name = a.Name; Type = a.Type; Value = a.Value; return *this; }  |
| 61 |   |
| 62 | tString Name;  |
| 63 | tType Type; // The type determines how to interpret Value.  |
| 64 | uint32 Value;  |
| 65 | };  |
| 66 |   |
| 67 |   |
| 68 | class tAttributes  |
| 69 | {  |
| 70 | public:  |
| 71 | tAttributes() { }  |
| 72 | tAttributes(const tAttributes& src) { *this = src; }  |
| 73 | tAttributes(const tChunk& chunk) { Load(chunk); }  |
| 74 | virtual ~tAttributes() { }  |
| 75 |   |
| 76 | void Clear() { Attributes.Empty(); }  |
| 77 | void Save(tChunkWriter&) const;  |
| 78 | void Load(const tChunk&);  |
| 79 | bool Contains(const tAttribute&) const;  |
| 80 | int Count() const { return Attributes.Count(); }  |
| 81 |   |
| 82 | // tAttributes are equal if they contain the exact same attributes in any order.  |
| 83 | bool operator==(const tAttributes&) const;  |
| 84 | bool operator!=(const tAttributes& a) const { return !(*this == a); }  |
| 85 | tAttributes& operator=(const tAttributes&);  |
| 86 |   |
| 87 | mutable tItList<tAttribute> Attributes;  |
| 88 | };  |
| 89 |   |
| 90 |   |
| 91 | }  |
| 92 | |