1 ////////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2004-2011 by The Allacrost Project
3 // Copyright (C) 2012-2018 by Bertram (Valyria Tear)
4 // All Rights Reserved
5 //
6 // This code is licensed under the GNU GPL version 2. It is free software
7 // and you may modify it and/or redistribute it under the terms of this license.
8 // See http://www.gnu.org/copyleft/gpl.html for details.
9 ////////////////////////////////////////////////////////////////////////////////
10
11 #include "emote_handler.h"
12
13 #include "script/script_read.h"
14
15 using namespace vt_video;
16
17 namespace vt_global
18 {
19
LoadEmotes(const std::string & emotes_filename)20 void EmoteHandler::LoadEmotes(const std::string& emotes_filename)
21 {
22 // First, clear the list in case of reloading
23 _emotes.clear();
24
25 vt_script::ReadScriptDescriptor emotes_script;
26 if(!emotes_script.OpenFile(emotes_filename))
27 return;
28
29 if(!emotes_script.DoesTableExist("emotes")) {
30 emotes_script.CloseFile();
31 return;
32 }
33
34 std::vector<std::string> emotes_id;
35 emotes_script.ReadTableKeys("emotes", emotes_id);
36
37 // Read all the values
38 emotes_script.OpenTable("emotes");
39 for(uint32_t i = 0; i < emotes_id.size(); ++i) {
40
41 if(!emotes_script.DoesTableExist(emotes_id[i]))
42 continue;
43 emotes_script.OpenTable(emotes_id[i]);
44
45 std::string animation_file = emotes_script.ReadString("animation");
46
47 AnimatedImage anim;
48 if(anim.LoadFromAnimationScript(animation_file)) {
49 // NOTE: The map mode should one day be fixed to use the same coords
50 // than everything else, thus making possible to remove this
51 vt_map::private_map::ScaleToMapZoomRatio(anim);
52
53 _emotes.insert(std::make_pair(emotes_id[i], anim));
54
55 // The vector containing the offsets
56 std::vector<std::pair<float, float> > emote_offsets;
57 emote_offsets.resize(vt_map::private_map::NUM_ANIM_DIRECTIONS);
58
59 // For each directions
60 for(uint32_t j = 0; j < vt_map::private_map::NUM_ANIM_DIRECTIONS; ++j) {
61 emotes_script.OpenTable(j);
62
63 std::pair<float, float> offsets;
64 offsets.first = emotes_script.ReadFloat("x");
65 offsets.second = emotes_script.ReadFloat("y");
66
67 emote_offsets[j] = offsets;
68
69 emotes_script.CloseTable(); // direction table.
70 }
71
72 _emotes_offsets.insert(std::make_pair(emotes_id[i], emote_offsets));
73 }
74
75 emotes_script.CloseTable(); // emote_id[i]
76 }
77 emotes_script.CloseAllTables();
78 emotes_script.CloseFile();
79 }
80
GetEmoteOffset(float & x,float & y,const std::string & emote_id,vt_map::private_map::ANIM_DIRECTIONS dir)81 void EmoteHandler::GetEmoteOffset(float& x, float& y,
82 const std::string& emote_id,
83 vt_map::private_map::ANIM_DIRECTIONS dir)
84 {
85 x = 0.0f;
86 y = 0.0f;
87
88 if(dir < vt_map::private_map::ANIM_SOUTH || dir >= vt_map::private_map::NUM_ANIM_DIRECTIONS)
89 return;
90
91 std::map<std::string, std::vector<std::pair<float, float> > >::const_iterator it =
92 _emotes_offsets.find(emote_id);
93
94 if(it == _emotes_offsets.end())
95 return;
96
97 x = it->second[dir].first;
98 y = it->second[dir].second;
99 }
100
DoesEmoteExist(const std::string & emote_id)101 bool EmoteHandler::DoesEmoteExist(const std::string& emote_id) {
102 return (_emotes.count(emote_id));
103 }
104
105 //! \brief Get a pointer reference to the given emote animation. Don't delete it!
GetEmoteAnimation(const std::string & emote_id)106 vt_video::AnimatedImage* EmoteHandler::GetEmoteAnimation(const std::string& emote_id) {
107 if(_emotes.find(emote_id) != _emotes.end()) return &_emotes.at(emote_id);
108 else return nullptr;
109 }
110
111 } // namespace vt_global
112