Mana
Loading...
Searching...
No Matches
emotedb.cpp
Go to the documentation of this file.
1/*
2 * Emote database
3 * Copyright (C) 2009 Aethyra Development Team
4 * Copyright (C) 2009-2013 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "resources/emotedb.h"
23
24#include "log.h"
25
26#include "resources/imageset.h"
28
29#include <algorithm>
30#include <vector>
31
32namespace
33{
34 std::vector<Emote> mEmotes;
35 Emote mUnknown;
36 bool mLoaded = false;
37}
38
40{
41 if (mLoaded)
42 unload();
43
44 mUnknown.name = "unknown";
45 mUnknown.effectId = -1;
46 mUnknown.image = ResourceManager::getInstance()->getImage("graphics/sprites/error.png");
47}
48
49void EmoteDB::readEmoteNode(XML::Node node, const std::string &filename)
50{
51 const int id = node.getProperty("id", -1);
52 if (id == -1)
53 {
54 Log::info("Emote Database: Emote with missing ID in %s!", filename.c_str());
55 return;
56 }
57
58 Emote emote;
59
60 emote.id = id;
61 emote.name = node.getProperty("name", "unknown");
62 emote.effectId = node.getProperty("effectid", -1);
63
64 if (emote.effectId == -1)
65 {
66 Log::info("Emote Database: Warning: Emote %s has no attached effect in %s!",
67 emote.name.c_str(), filename.c_str());
68 return;
69 }
70
71 const std::string imageName = node.getProperty("image", "");
72 const int width = node.getProperty("width", 0);
73 const int height = node.getProperty("height", 0);
74
75 if (imageName.empty() || width <= 0 || height <= 0)
76 {
77 Log::info("Emote Database: Warning: Emote %s has bad imageset values in %s",
78 emote.name.c_str(), filename.c_str());
79 return;
80 }
81
82 emote.is = ResourceManager::getInstance()->getImageSet(imageName,
83 width,
84 height);
85
86 if (!emote.is || emote.is->size() == 0)
87 {
88 Log::info("Emote Database: Error loading imageset for emote %s in %s",
89 emote.name.c_str(), filename.c_str());
90 return;
91 }
92
93 // For now we just use the first image in the animation
94 emote.image = emote.is->get(0);
95
96 mEmotes.push_back(std::move(emote));
97}
98
100{
101 mLoaded = true;
102}
103
105{
106 // These images are owned by the ImageSet
107 for (auto &emote : mEmotes)
108 emote.image.release();
109
110 mEmotes.clear();
111 mUnknown.image = nullptr;
112 mLoaded = false;
113}
114
115const Emote &EmoteDB::get(int id)
116{
117 auto i = std::find_if(mEmotes.begin(), mEmotes.end(),
118 [id](const Emote &e) { return e.id == id; });
119
120 if (i == mEmotes.end())
121 {
122 Log::info("EmoteDB: Warning, unknown emote ID %d requested", id);
123 return mUnknown;
124 }
125
126 return *i;
127}
128
129const Emote &EmoteDB::getByIndex(int index)
130{
131 return mEmotes.at(index);
132}
133
135{
136 return mEmotes.size();
137}
Image * get(size_t i) const
Definition imageset.cpp:49
size_t size() const
Definition imageset.h:58
static ResourceManager * getInstance()
Returns an instance of the class, creating one if it does not already exist.
ResourceRef< Image > getImage(const std::string &idPath)
Loads the Image resource found at the given identifier path.
ResourceRef< ImageSet > getImageSet(const std::string &imagePath, int w, int h)
Loads a image set based on the image referenced by the given path and the supplied sprite sizes.
int getProperty(const char *name, int def) const
Definition xml.h:144
void checkStatus()
Definition emotedb.cpp:99
const Emote & getByIndex(int index)
Definition emotedb.cpp:129
const Emote & get(int id)
Definition emotedb.cpp:115
void readEmoteNode(XML::Node node, const std::string &filename)
Definition emotedb.cpp:49
void init()
Definition emotedb.cpp:39
int getEmoteCount()
Definition emotedb.cpp:134
void unload()
Definition emotedb.cpp:104
void info(const char *log_text,...) LOG_PRINTF_ATTR
int id
Definition emotedb.h:33
int effectId
Definition emotedb.h:34
std::string name
Definition emotedb.h:35
ResourceRef< ImageSet > is
Definition emotedb.h:36
ResourceRef< Image > image
Definition emotedb.h:37