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

#include "fsCNativeTextSprite.h"

#include <Magnum/Math/Color.h>
#include <Magnum/Shaders/DistanceFieldVectorGL.h>
using namespace Magnum::Math::Literals;

#include <fsAppCore/fsIDisplay.h>

#include <fsCore/fsAffineMtx2d.h>
#include <fsCore/fsStr.h>
#include <fsCore/fsSColour.h>
#include <fsCore/fsRect.h>
#include <fsCore/src/fsNMaths.h>

#include "fsCNativeSprite.h"
#include "../fsCFont.h"

namespace
{
	// Magnum has no rich-text rendering yet. fsStr::removeXmlTags() deliberately preserves
	// <b>/<i>/<font ...> tags now (axmol/cocos2d-x render them), so without this they'd show
	// up as literal control codes here. Strip them, keeping the enclosed text, so Magnum falls
	// back to plain readable text instead.
	fsStr richTextTagsStrip(const fsStr& pText)
	{
		fsStr result = pText;
		fsStr::sizeT start = result.find('<');
		while (start != fsStr::npos)
		{
			const fsStr::sizeT end = result.find('>', start);
			if (end == fsStr::npos)
			{
				break;
			}
			result.erase(start, end - start + 1);
			start = result.find('<');
		}
		return result;
	}
}

Magnum::Shaders::DistanceFieldVectorGL3D* fsCNativeTextSprite::mShader = nullptr;

void fsCNativeTextSprite::outlineSet(const fsSColour4B& pOutline, fsF32 pSize) const<--- The member function 'fsCNativeTextSprite::outlineSet' can be static.
{
    
}

void fsCNativeTextSprite::glowSet(const fsSColour4B& pGlow) const<--- The member function 'fsCNativeTextSprite::glowSet' can be static.
{
}

void fsCNativeTextSprite::shadowSet(const fsSColour4B& pShadow, fsVec2d pOffset, fsF32 pBlurRadius) const<--- The member function 'fsCNativeTextSprite::shadowSet' can be static.
{
}

void fsCNativeTextSprite::renderDo()
{
	if (mText.emptyGet())
	{
		return;
	}
	auto fnt = static_cast<fsCFont*>(mFont);

	mShader
		->bindVectorTexture(fnt->_cache.texture())
		.setTransformationProjectionMatrix( mRenderMatrix )
		.setColor(0xffffff_rgbf)
		.setOutlineColor(0x000000_rgbf)
		.setOutlineRange(0.55f, 0.4f)
		.setSmoothness(0.25f)
		.draw(_myText);
}

void fsCNativeTextSprite::colourSetDo(const fsSColour4B& pColour)
{
	mShader->setColor(Magnum::Color4(pColour.mR, pColour.mG, pColour.mB, pColour.mA));
}

void fsCNativeTextSprite::matrixApplyDo(const fsAffineMtx2d& pRenderMatrix)
{
	fsF32 m[16] = {0.0};
	m[0] = 1.0f;
	m[5] = 1.0f;
	m[10] = 1.0f;
	m[15] = 1.0f;

	m[0] = pRenderMatrix.get(0, 0);
	m[1] = pRenderMatrix.get(0, 1);
	m[4] = pRenderMatrix.get(1, 0);
	m[5] = pRenderMatrix.get(1, 1);

	m[12] = pRenderMatrix.get(0, 2);
	m[13] = -pRenderMatrix.get(1, 2);
	m[14] = -0.01;

	mRenderMatrix = Magnum::Math::Matrix4x4<fsF32>::from(m);
	mRenderMatrix = fsCNativeSprite::projectionMatrix * mRenderMatrix;
}

void fsCNativeTextSprite::alignmentSet(fsS32 pAlignH, fsS32 pAlignV)<--- The member function 'fsCNativeTextSprite::alignmentSet' can be static.
{
}

void fsCNativeTextSprite::initialise(const fsStr& pText, fsIFont* pFont)
{
	mFont = pFont;
	textSet(pText);
}

fsS32 fsCNativeTextSprite::stringWidthInPixelsGet() const<--- The member function 'fsCNativeTextSprite::stringWidthInPixelsGet' can be static.
{
	return 0;
}

void fsCNativeTextSprite::formattingRectSizeSet(const fsRect& pFormattingRect)
{
	mBoundingBox = pFormattingRect;
}

fsStr fsCNativeTextSprite::textGet() const<--- The member function 'fsCNativeTextSprite::textGet' can be static.
{
	return "Not set";
}

void fsCNativeTextSprite::softWrap()
{
	auto fnt = static_cast<fsCFont*>(mFont);
	fsS32 w = 0;
	fsStr newText = "";
	// Magnum text wrapping here assumes the existing English/ASCII content pipeline.
	for (fsS32 currCharacter = 0; currCharacter < mText.lengthGet(); ++currCharacter)
	{
		newText += mText[currCharacter];
		auto glyphId = fnt->_fontManager->glyphId(mText[currCharacter]);
		auto glyph = fnt->_cache[glyphId].second.right() -
			fnt->_cache[glyphId].second.left();
		auto g= fnt->_cache[glyphId];
		w += fnt->_cache[glyphId].second.max().x() - fnt->_cache[glyphId].second.min().x();
		if (w > mBoundingBox.widthGet())
		{
			newText += "\n";
			w = 0;
		}
		
	}
	mText = newText;
}

void fsCNativeTextSprite::textSet(const fsStr& pText)
{
	const fsStr strippedText = richTextTagsStrip(pText);
	mText = strippedText;
	softWrap();

	auto fnt = static_cast<fsCFont*>(mFont);

	std::tie(_myText, std::ignore) = Magnum::Text::Renderer2D::render(
		*fnt->_fontManager,
		fnt->_cache,
		24,
		strippedText.utf8Get(),
		mVertices,
		mIndices,
		Magnum::GL::BufferUsage::StaticDraw,
		Magnum::Text::Alignment::LineCenter);
}

fsCNativeTextSprite::fsCNativeTextSprite():
fsINative()
{
	mScreenWidth = fsIDisplay::instanceGet()->visibleCanvasWidthGet();
	mScreenHeight = fsIDisplay::instanceGet()->visibleCanvasHeightGet();
}

fsCNativeTextSprite::~fsCNativeTextSprite()
{
}