+ // Use cached data
+ if(mCache[index]->GetBorder() == border)
+ {
+ if(mCache[index]->GetLoadingState() == NPatchData::LoadingState::LOADING)
+ {
+ mCache[index]->AddObserver(textureObserver);
+ }
+ return mCache[index]->GetId(); // valid indices are from 1 onwards
+ }
+ else
+ {
+ if(mCache[index]->GetLoadingState() == NPatchData::LoadingState::LOAD_COMPLETE)
+ {
+ // Same url but border is different - use the existing texture
+ NPatchData* newData = new NPatchData();
+ newData->SetId(GenerateUniqueNPatchDataId());
+ newData->SetHash(hash);
+ newData->SetUrl(url);
+ newData->SetCroppedWidth(mCache[index]->GetCroppedWidth());
+ newData->SetCroppedHeight(mCache[index]->GetCroppedHeight());
+
+ newData->SetTextures(mCache[index]->GetTextures());
+
+ NPatchUtility::StretchRanges stretchRangesX;
+ stretchRangesX.PushBack(Uint16Pair(border.left, ((newData->GetCroppedWidth() >= static_cast<unsigned int>(border.right)) ? newData->GetCroppedHeight() - border.right : 0)));
+
+ NPatchUtility::StretchRanges stretchRangesY;
+ stretchRangesY.PushBack(Uint16Pair(border.top, ((newData->GetCroppedWidth() >= static_cast<unsigned int>(border.bottom)) ? newData->GetCroppedHeight() - border.bottom : 0)));
+
+ newData->SetStretchPixelsX(stretchRangesX);
+ newData->SetStretchPixelsY(stretchRangesY);
+ newData->SetBorder(border);
+
+ newData->SetPreMultiplyOnLoad(mCache[index]->IsPreMultiplied());
+
+ newData->SetLoadingState(NPatchData::LoadingState::LOAD_COMPLETE);
+ newData->AddObserver(textureObserver);
+
+ mCache.PushBack(newData);
+ return newData->GetId(); // valid ids start from 1u
+ }
+ }