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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "gfICutterComponent.h"

#include <fsCore/fsIFile.h>
#include <fsCore/fsIResourceNamePathAdjuster.h>
#include <fsCore/src/fsCResourceName.h>
#include <fsCore/src/fsCVariableTable.h>
#include <fsCore/src/debug/fsLog.h>

#include <fsAppCore/src/fsILevelBasedAppCoreMain.h>
#include <fsAppCore/src/fsCEntity.h>
#include <fsAppCore/src/fsCEntityFactory.h>
#include <fsRenderer/src/fsCBrushManager.h>
#include <fsRenderer/src/components/fsCSpriteComponent.h>
#include <fsAppCore/src/components/fsCNameComponent.h>
#include <fsAppCore/src/components/fsISubSceneComponent.h>

#include "gfIStencil.h"

#include <gfComponents/components/subScene/gfCSubSceneComponent.h>

#include "gfIMinigameComponent.h"
#include "gfIMinigameLogic.h"
#include "gfComponents/gfILevelLoaderComponent.h"


const fsCUniqueId gfICutterComponent::mTypeId("gfICutterComponent");

const fsStr gfICutterComponent::mStencilMarker("stencil");
const fsStr gfICutterComponent::mStencilChunkMarker("stencilsDone");


void gfICutterComponent::cut(fsBool pFirstTime)
{
	if (mMinigameEnts.size() == mStencils.size())
	{
		auto minigameEnt = mMinigameEnts.begin();
		auto stencil = mStencils.begin();
		for (; minigameEnt != mMinigameEnts.end(); ++minigameEnt, ++stencil)
		{
			doCut(*minigameEnt, *stencil, pFirstTime);
		}
	}
	else
	{
		for (fsCEntity* ent : mMinigameEnts)
		{
			doCut(ent, mStencils.front(), pFirstTime);
		}
	}
}

void gfICutterComponent::serialiseDo()
{
	parentGet()->strSerialise(mStencilMarker);

	for (auto currStencil = mStencils.begin();
	     mStencils.end() != currStencil;
	     ++currStencil)
	{
		(*currStencil)->save(parentGet());
		if (currStencil != mStencils.end() - 1)
		{
			parentGet()->strSerialise(mStencilMarker);
		}
	}
	parentGet()->strSerialise(mStencilChunkMarker);
}

void gfICutterComponent::deserialiseDo()
{
	fsStr sectionMarker = parentGet()->strDeserialise();
	while (sectionMarker != mStencilChunkMarker)
	{
		mStencils.push_back(stencilRestore());
		sectionMarker = parentGet()->strDeserialise();
	}
}

void gfICutterComponent::minigameEntitiesCreate(const fsCVariableTable& pStyle, const fsStr& pLevelName,
                                                fsCEntity* const pParent, const fsStr& pAutoMiniGameSourceArt,
                                                gfIMinigameLogic* pMingameLogic)
{
	mSceneRoot = pParent->firstChildEntityWithComponentsOfFamilyType<fsISubSceneComponent>();
	auto masks = mSceneRoot->childEntitiesWithComponentsOfFamilyType<fsISpriteComponent>();

	setup(pStyle, pLevelName, pAutoMiniGameSourceArt);

	fsCEntity* const pieceParent = pMingameLogic ? pMingameLogic->parentGet() : parentGet();
	const fsPoint foldedOrigin = originLayerOffsetReconcile(pieceParent);

	mGridOrigin.x -= static_cast<fsS32>(mCols / 2.0f * mPieceWidth - mPieceWidth / 2);
	mGridOrigin.y -= static_cast<fsS32>(mRows / 2.0f * mPieceHeight - mPieceHeight / 2);

	for (fsS32 currRow = 0; currRow < mRows; ++currRow)
	{
		for (fsS32 currCol = 0; currCol < mCols; ++currCol)
		{
			fsCEntity* const ent = parentGet()->entityFactoryGet()->entityConstruct(pieceNameGet(currRow, currCol));
			ent->posSet(fsPoint(mGridOrigin.x + currCol * mPieceWidth, mGridOrigin.y + currRow * mPieceHeight));
			ent->initialiseFromVariableTable(pStyle);
			pieceParent->childAdd(ent);
			mMinigameEnts.push_back(ent);
		}
	}

	for (fsCEntity* const maskEnt : masks)
	{
		if (!maskEnt->firstComponentOfFamilyType<gfIMinigameComponent>())
		{
			if (fsCNameComponent* nameComp = maskEnt->componentGet<fsCNameComponent>())
			{
				if (fsStr::npos == nameComp->nameSansSuffixGet().find(gfCSubSceneComponent::mEntityPrefix))
				{
					// undo the fold for masks that share the parent origin was folded into
					if (parentGet() == pieceParent)
					{
						maskEnt->posSet(fsPoint(maskEnt->posGet()) - foldedOrigin);
					}
					parentGet()->childAdd(maskEnt);
				}
			}
		}
	}
}

fsStr gfICutterComponent::sourceImageFilenameGet(const fsStr& pLevelFolder, const fsStr& pLayerNameSansPrefix)
{
	fsStr levelFilenameStem{pLevelFolder};
	levelFilenameStem.erase(levelFilenameStem.rfind(fsIResourceNamePathAdjuster::mPathSeparator));	// strip off trailing path separator
	levelFilenameStem.erase(0, levelFilenameStem.rfind(fsIResourceNamePathAdjuster::mPathSeparator) + 1);	// bin everything up to last path separator

	const auto layersSubFolder = gfILevelLoaderComponent::layersSubFolderGet(pLevelFolder);
	const std::vector<fsStr> candidate_filenames = {
		pLevelFolder + layersSubFolder + pLayerNameSansPrefix,
		pLevelFolder + layersSubFolder + levelFilenameStem
	};

	const std::vector<fsStr>filename_extensions = {
		".jpg",
		".png",
		".webp"
	};

	auto findImageFilename = [&]()
	{
		for (auto ext : filename_extensions)
		{
			for (auto filename : candidate_filenames)
			{
			    // raw file search as image to be cut not texture paged - defered uploads and all sorts of other
			    // issues so any image that's worked on is generally raw loaded
				if (fsIFile::fileExistsGet(fsCResourceName(filename + ext, fsCResourceName::eRES_LOCATION_ASSETS)))
				{
					return filename + ext;
				}
			}
		}
		return fsStr("");
	};
	
	return findImageFilename();
}

void gfICutterComponent::setup(const fsCVariableTable& pStyle, const fsStr& pLevelName,
                               const fsStr& pAutoMiniGameSourceArt)
{
	const fsStr textureFileName = !pAutoMiniGameSourceArt.emptyGet()
		                              ? pAutoMiniGameSourceArt
		                              : sourceImageFilenameGet(pLevelName, fsCNameComponent::get(parentGet())->nameSansPrefixGet());

	mSourceImage = fsCBrushManager::instanceGet()->get(
		fsCResourceName(textureFileName, fsCResourceName::eRES_LOCATION_ASSETS), false, true);

	mGridOrigin.x = pStyle.s32Get("originX");
	mGridOrigin.y = pStyle.s32Get("originY");

	if (mGridOrigin.x != 0 || mGridOrigin.y != 0)
	{
		fsLogError("originX/Y is deprecated - position new minigames via the CUx- cutter layer "
		           "in coords, not origin. Cutter: '%s'.", parentGet()->nameGet().utf8Get())
	}

	//mGridOrigin = parentGet()->posGet();
	
	mPieceWidth = pStyle.s32GetWithDefault("tileWidth", 96);
	mPieceHeight = pStyle.s32GetWithDefault("tileHeight", 96);

	doSetup(pStyle, pLevelName);
}

fsPoint gfICutterComponent::originLayerOffsetReconcile(fsCEntity* const pPieceParent)
{
	const fsPoint layerPos(pPieceParent->posGet());
	const fsBool originUsed = mGridOrigin.x != 0 || mGridOrigin.y != 0;
	const fsBool layerOffsetUsed = layerPos.x != 0 || layerPos.y != 0;
	if (!(originUsed && layerOffsetUsed))
	{
		return fsPoint(0, 0);
	}

	fsLogError("cutter '%s' uses both originX/Y (%d,%d) and a layer offset (%d,%d); "<--- There is an unknown macro here somewhere. Configuration is required. If fsLogError is a macro then please configure it.
	           "folding origin into the layer position.",
	           parentGet()->nameGet().utf8Get(), mGridOrigin.x, mGridOrigin.y, layerPos.x, layerPos.y)

	pPieceParent->posSet(layerPos + mGridOrigin);
	const fsPoint folded(mGridOrigin);
	mGridOrigin = fsPoint(0, 0);
	return folded;
}

fsStr gfICutterComponent::pieceNameGet(fsS32 pRow, fsS32 pCol) const
{
	fsStr pieceName(minigamePieceNameGet() + "-" + parentGet()->nameGet() + "-tile" + fsStr(pRow * mCols + pCol));
	return pieceName;
}

gfICutterComponent::gfICutterComponent(fsCEntity* const pParent) :
	fsIComponent(pParent),
	mPieceWidth(0),
	mPieceHeight(0),
	mCols(0),
	mRows(0),
	mGridOrigin(fsPoint(0, 0)),
	mSceneRoot(nullptr),
	mMinigameLogic(nullptr)
{
}

gfICutterComponent::~gfICutterComponent()
{
}