| 46 | /* ClipCache */ |
| 47 | |
| 48 | std::pair<Texture2D*, Rect> ClipCache::loadTexture(String clipStr) { |
| 49 | if (clipStr.toString().find('|') != std::string::npos) { |
| 50 | auto tokens = clipStr.split("|"); |
| 51 | if (tokens.size() != 2 || Path::getExt(tokens.front().toString()) != "clip"_slice) { |
| 52 | Error("invalid clip str: \"{}\".", clipStr.toString()); |
| 53 | return {}; |
| 54 | } |
| 55 | ClipDef* clipDef = ClipCache::load(tokens.front()); |
| 56 | if (!clipDef) { |
| 57 | Error("failed to load clip: \"{}\".", clipStr.toString()); |
| 58 | return {}; |
| 59 | } |
| 60 | Slice name = tokens.back(); |
| 61 | auto nameStr = name.toString(); |
| 62 | auto it = clipDef->rects.find(nameStr); |
| 63 | if (it != clipDef->rects.end()) { |
| 64 | Texture2D* texture = SharedTextureCache.load(clipDef->textureFile); |
| 65 | return {texture, *it->second}; |
| 66 | } else { |
| 67 | Error("no clip named \"{}\" in {}", nameStr, tokens.front().toString()); |
| 68 | Texture2D* tex = SharedTextureCache.load(clipDef->textureFile); |
| 69 | Rect rect(0.0f, 0.0f, s_cast<float>(tex->getWidth()), s_cast<float>(tex->getHeight())); |
| 70 | return {}; |
| 71 | } |
| 72 | } else if (Path::getExt(clipStr.toString()) == "clip"_slice) { |
| 73 | Texture2D* tex = nullptr; |
| 74 | ClipDef* clipDef = SharedClipCache.load(clipStr); |
| 75 | if (clipDef) tex = SharedTextureCache.load(clipDef->textureFile); |
| 76 | if (tex) { |
| 77 | Rect rect(0.0f, 0.0f, s_cast<float>(tex->getWidth()), s_cast<float>(tex->getHeight())); |
| 78 | return {tex, rect}; |
| 79 | } |
| 80 | Error("failed to get clip from clipStr \"{}\".", clipStr.toString()); |
| 81 | return {}; |
| 82 | } else { |
| 83 | Texture2D* tex = SharedTextureCache.load(clipStr); |
| 84 | if (tex) { |
| 85 | Rect rect(0.0f, 0.0f, s_cast<float>(tex->getWidth()), s_cast<float>(tex->getHeight())); |
| 86 | return {tex, rect}; |
| 87 | } |
| 88 | Error("failed to get texture from clipStr \"{}\".", clipStr.toString()); |
| 89 | return {}; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | Sprite* ClipCache::loadSprite(String clipStr) { |
| 94 | Texture2D* tex = nullptr; |
no test coverage detected