forked from FranciscoBiaso/TileMapEditorRealmz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImgPack.cpp
92 lines (74 loc) · 2.2 KB
/
ImgPack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "ImgPack.h"
#include <json/json.h>
#include <fstream>
data::ImgPack::ImgPack()
{
textureAtlas = new TextureAtlas(TEXTURE_ATLAS_MAX_WIDTH, TEXTURE_ATLAS_MAX_WIDTH);
}
void data::ImgPack::addImgObj(std::string name,const GdkPixbuf* srcImg,const def::IMG_SIZE size)
{
_imgs.push_back(ImgObj(name,size, textureAtlas->addAddImgs(srcImg, size))); // add img to the end of the vector //
}
void data::ImgPack::addImgObjFromJson(ImgObj imgobj)
{
_imgs.push_back(imgobj);
}
std::list<data::ImgObj>& data::ImgPack::getImgVec()
{
return _imgs;
}
void data::ImgPack::delImgObj(std::string name)
{
// try to find ImgObj by name // O(n)
std::list<data::ImgObj>::iterator it = find(name);
// if founded //
if (it != _imgs.end())
{
// remove imgs from texture atlas O(n) //
textureAtlas->delImgObj(it, _imgs.end());
// erase the image O(1) //
_imgs.erase(it);
}
}
std::list<data::ImgObj>::iterator data::ImgPack::find(std::string imgName)
{
return std::find_if(_imgs.begin(), _imgs.end(), [&, imgName](data::ImgObj& img) {
if (img.getName() == imgName)
return true;
return false;
});
}
data::TextureAtlas* data::ImgPack::getTextureAtlas() const
{
return textureAtlas;
}
void data::ImgPack::saveImgPackAsJsonFile()
{
Json::Value jsonValue;
for (auto it = _imgs.begin(); it != _imgs.end(); it++)
{
jsonValue[it->getName()]["size"] = it->getSizeAsString();
for (int i = 0; i < it->getSizeAsInt(); i++)
{
jsonValue[it->getName()][std::to_string(i) + "_ref_x"] = it->getRef(i).getX();
jsonValue[it->getName()][std::to_string(i) + "_ref_y"] = it->getRef(i).getY();
}
}
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(
builder.newStreamWriter());
// Make a new JSON document for the configuration. Preserve original comments.
std::ofstream ofs("resources//img_pack.json", std::ofstream::out);// file to read //
writer->write(jsonValue, &ofs);
ofs.close();
}
bool compare_nocase(const data::ImgObj& first, const data::ImgObj& second)
{
return (std::atoi(first.getName().c_str()) < std::atoi(second.getName().c_str()));
}
void data::ImgPack::sort()
{
_imgs.sort(compare_nocase);
}