1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-30 22:15:32 +00:00

modified tab to space

This commit is contained in:
gus 2014-07-22 13:32:15 +02:00
parent 195767db7f
commit baf6cca051
2 changed files with 195 additions and 195 deletions

View file

@ -22,185 +22,185 @@
//all credits to http://www.ogre3d.org/tikiwiki/tiki-index.php?page=HowTo:+Write+text+on+texture //all credits to http://www.ogre3d.org/tikiwiki/tiki-index.php?page=HowTo:+Write+text+on+texture
void WriteToTexture(const Ogre::String &str, Ogre::TexturePtr destTexture, Ogre::Image::Box destRectangle, Ogre::Font* font, const Ogre::ColourValue &color, char justify = 'l', bool wordwrap = true) void WriteToTexture(const Ogre::String &str, Ogre::TexturePtr destTexture, Ogre::Image::Box destRectangle, Ogre::Font* font, const Ogre::ColourValue &color, char justify = 'l', bool wordwrap = true)
{ {
using namespace Ogre; using namespace Ogre;
if (destTexture->getHeight() < destRectangle.bottom) if (destTexture->getHeight() < destRectangle.bottom)
destRectangle.bottom = destTexture->getHeight(); destRectangle.bottom = destTexture->getHeight();
if (destTexture->getWidth() < destRectangle.right) if (destTexture->getWidth() < destRectangle.right)
destRectangle.right = destTexture->getWidth(); destRectangle.right = destTexture->getWidth();
if (!font->isLoaded()) if (!font->isLoaded())
font->load(); font->load();
TexturePtr fontTexture = (TexturePtr)TextureManager::getSingleton().getByName(font->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName()); TexturePtr fontTexture = (TexturePtr)TextureManager::getSingleton().getByName(font->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName());
HardwarePixelBufferSharedPtr fontBuffer = fontTexture->getBuffer(); HardwarePixelBufferSharedPtr fontBuffer = fontTexture->getBuffer();
HardwarePixelBufferSharedPtr destBuffer = destTexture->getBuffer(); HardwarePixelBufferSharedPtr destBuffer = destTexture->getBuffer();
PixelBox destPb = destBuffer->lock(destRectangle, HardwareBuffer::HBL_NORMAL); PixelBox destPb = destBuffer->lock(destRectangle, HardwareBuffer::HBL_NORMAL);
// The font texture buffer was created write only...so we cannot read it back :o). One solution is to copy the buffer instead of locking it. (Maybe there is a way to create a font texture which is not write_only ?) // The font texture buffer was created write only...so we cannot read it back :o). One solution is to copy the buffer instead of locking it. (Maybe there is a way to create a font texture which is not write_only ?)
// create a buffer // create a buffer
size_t nBuffSize = fontBuffer->getSizeInBytes(); size_t nBuffSize = fontBuffer->getSizeInBytes();
uint8* buffer = (uint8*)calloc(nBuffSize, sizeof(uint8)); uint8* buffer = (uint8*)calloc(nBuffSize, sizeof(uint8));
// create pixel box using the copy of the buffer // create pixel box using the copy of the buffer
PixelBox fontPb(fontBuffer->getWidth(), fontBuffer->getHeight(), fontBuffer->getDepth(), fontBuffer->getFormat(), buffer); PixelBox fontPb(fontBuffer->getWidth(), fontBuffer->getHeight(), fontBuffer->getDepth(), fontBuffer->getFormat(), buffer);
fontBuffer->blitToMemory(fontPb); fontBuffer->blitToMemory(fontPb);
uint8* fontData = static_cast<uint8*>(fontPb.data); uint8* fontData = static_cast<uint8*>(fontPb.data);
uint8* destData = static_cast<uint8*>(destPb.data); uint8* destData = static_cast<uint8*>(destPb.data);
const size_t fontPixelSize = PixelUtil::getNumElemBytes(fontPb.format); const size_t fontPixelSize = PixelUtil::getNumElemBytes(fontPb.format);
const size_t destPixelSize = PixelUtil::getNumElemBytes(destPb.format); const size_t destPixelSize = PixelUtil::getNumElemBytes(destPb.format);
const size_t fontRowPitchBytes = fontPb.rowPitch * fontPixelSize; const size_t fontRowPitchBytes = fontPb.rowPitch * fontPixelSize;
const size_t destRowPitchBytes = destPb.rowPitch * destPixelSize; const size_t destRowPitchBytes = destPb.rowPitch * destPixelSize;
Box *GlyphTexCoords; Box *GlyphTexCoords;
GlyphTexCoords = new Box[str.size()]; GlyphTexCoords = new Box[str.size()];
Font::UVRect glypheTexRect; Font::UVRect glypheTexRect;
size_t charheight = 0; size_t charheight = 0;
size_t charwidth = 0; size_t charwidth = 0;
for (unsigned int i = 0; i < str.size(); i++) for (unsigned int i = 0; i < str.size(); i++)
{ {
if ((str[i] != '\t') && (str[i] != '\n') && (str[i] != ' ')) if ((str[i] != '\t') && (str[i] != '\n') && (str[i] != ' '))
{ {
glypheTexRect = font->getGlyphTexCoords(str[i]); glypheTexRect = font->getGlyphTexCoords(str[i]);
GlyphTexCoords[i].left = glypheTexRect.left * fontTexture->getSrcWidth(); GlyphTexCoords[i].left = glypheTexRect.left * fontTexture->getSrcWidth();
GlyphTexCoords[i].top = glypheTexRect.top * fontTexture->getSrcHeight(); GlyphTexCoords[i].top = glypheTexRect.top * fontTexture->getSrcHeight();
GlyphTexCoords[i].right = glypheTexRect.right * fontTexture->getSrcWidth(); GlyphTexCoords[i].right = glypheTexRect.right * fontTexture->getSrcWidth();
GlyphTexCoords[i].bottom = glypheTexRect.bottom * fontTexture->getSrcHeight(); GlyphTexCoords[i].bottom = glypheTexRect.bottom * fontTexture->getSrcHeight();
if (GlyphTexCoords[i].getHeight() > charheight) if (GlyphTexCoords[i].getHeight() > charheight)
charheight = GlyphTexCoords[i].getHeight(); charheight = GlyphTexCoords[i].getHeight();
if (GlyphTexCoords[i].getWidth() > charwidth) if (GlyphTexCoords[i].getWidth() > charwidth)
charwidth = GlyphTexCoords[i].getWidth(); charwidth = GlyphTexCoords[i].getWidth();
} }
} }
size_t cursorX = 0; size_t cursorX = 0;
size_t cursorY = 0; size_t cursorY = 0;
size_t lineend = destRectangle.getWidth(); size_t lineend = destRectangle.getWidth();
bool carriagreturn = true; bool carriagreturn = true;
for (unsigned int strindex = 0; strindex < str.size(); strindex++) for (unsigned int strindex = 0; strindex < str.size(); strindex++)
{ {
switch (str[strindex]) switch (str[strindex])
{ {
case ' ': cursorX += charwidth; break; case ' ': cursorX += charwidth; break;
case '\t':cursorX += charwidth * 3; break; case '\t':cursorX += charwidth * 3; break;
case '\n':cursorY += charheight; carriagreturn = true; break; case '\n':cursorY += charheight; carriagreturn = true; break;
default: default:
{ {
//wrapping //wrapping
if ((cursorX + GlyphTexCoords[strindex].getWidth()> lineend) && !carriagreturn) if ((cursorX + GlyphTexCoords[strindex].getWidth()> lineend) && !carriagreturn)
{ {
cursorY += charheight; cursorY += charheight;
carriagreturn = true; carriagreturn = true;
} }
//justify //justify
if (carriagreturn) if (carriagreturn)
{ {
size_t l = strindex; size_t l = strindex;
size_t textwidth = 0; size_t textwidth = 0;
size_t wordwidth = 0; size_t wordwidth = 0;
while ((l < str.size()) && (str[l] != '\n)')) while ((l < str.size()) && (str[l] != '\n)'))
{ {
wordwidth = 0; wordwidth = 0;
switch (str[l]) switch (str[l])
{ {
case ' ': wordwidth = charwidth; ++l; break; case ' ': wordwidth = charwidth; ++l; break;
case '\t': wordwidth = charwidth * 3; ++l; break; case '\t': wordwidth = charwidth * 3; ++l; break;
case '\n': l = str.size(); case '\n': l = str.size();
} }
if (wordwrap) if (wordwrap)
while ((l < str.size()) && (str[l] != ' ') && (str[l] != '\t') && (str[l] != '\n')) while ((l < str.size()) && (str[l] != ' ') && (str[l] != '\t') && (str[l] != '\n'))
{ {
wordwidth += GlyphTexCoords[l].getWidth(); wordwidth += GlyphTexCoords[l].getWidth();
++l; ++l;
} }
else else
{ {
wordwidth += GlyphTexCoords[l].getWidth(); wordwidth += GlyphTexCoords[l].getWidth();
l++; l++;
} }
if ((textwidth + wordwidth) <= destRectangle.getWidth()) if ((textwidth + wordwidth) <= destRectangle.getWidth())
textwidth += (wordwidth); textwidth += (wordwidth);
else else
break; break;
} }
if ((textwidth == 0) && (wordwidth > destRectangle.getWidth())) if ((textwidth == 0) && (wordwidth > destRectangle.getWidth()))
textwidth = destRectangle.getWidth(); textwidth = destRectangle.getWidth();
switch (justify) switch (justify)
{ {
case 'c': cursorX = (destRectangle.getWidth() - textwidth) / 2; case 'c': cursorX = (destRectangle.getWidth() - textwidth) / 2;
lineend = destRectangle.getWidth() - cursorX; lineend = destRectangle.getWidth() - cursorX;
break; break;
case 'r': cursorX = (destRectangle.getWidth() - textwidth); case 'r': cursorX = (destRectangle.getWidth() - textwidth);
lineend = destRectangle.getWidth(); lineend = destRectangle.getWidth();
break; break;
default: cursorX = 0; default: cursorX = 0;
lineend = textwidth; lineend = textwidth;
break; break;
} }
carriagreturn = false; carriagreturn = false;
} }
//abort - net enough space to draw //abort - net enough space to draw
if ((cursorY + charheight) > destRectangle.getHeight()) if ((cursorY + charheight) > destRectangle.getHeight())
goto stop; goto stop;
//draw pixel by pixel //draw pixel by pixel
for (size_t i = 0; i < GlyphTexCoords[strindex].getHeight(); i++) for (size_t i = 0; i < GlyphTexCoords[strindex].getHeight(); i++)
for (size_t j = 0; j < GlyphTexCoords[strindex].getWidth(); j++) for (size_t j = 0; j < GlyphTexCoords[strindex].getWidth(); j++)
{ {
float alpha = color.a * (fontData[(i + GlyphTexCoords[strindex].top) * fontRowPitchBytes + (j + GlyphTexCoords[strindex].left) * fontPixelSize + 1] / 255.0); float alpha = color.a * (fontData[(i + GlyphTexCoords[strindex].top) * fontRowPitchBytes + (j + GlyphTexCoords[strindex].left) * fontPixelSize + 1] / 255.0);
float invalpha = 1.0 - alpha; float invalpha = 1.0 - alpha;
size_t offset = (i + cursorY) * destRowPitchBytes + (j + cursorX) * destPixelSize; size_t offset = (i + cursorY) * destRowPitchBytes + (j + cursorX) * destPixelSize;
ColourValue pix; ColourValue pix;
PixelUtil::unpackColour(&pix, destPb.format, &destData[offset]); PixelUtil::unpackColour(&pix, destPb.format, &destData[offset]);
pix = (pix * invalpha) + (color * alpha); pix = (pix * invalpha) + (color * alpha);
PixelUtil::packColour(pix, destPb.format, &destData[offset]); PixelUtil::packColour(pix, destPb.format, &destData[offset]);
} }
cursorX += GlyphTexCoords[strindex].getWidth(); cursorX += GlyphTexCoords[strindex].getWidth();
}//default }//default
}//switch }//switch
}//for }//for
stop: stop:
delete[] GlyphTexCoords; delete[] GlyphTexCoords;
destBuffer->unlock(); destBuffer->unlock();
// Free the memory allocated for the buffer // Free the memory allocated for the buffer
free(buffer); buffer = 0; free(buffer); buffer = 0;
} }
void CSVRender::PagedWorldspaceWidget::displayCellCoord(bool display) void CSVRender::PagedWorldspaceWidget::displayCellCoord(bool display)
{ {
mDisplayCellCoord = display; mDisplayCellCoord = display;
std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter(mCells.begin()); std::map<CSMWorld::CellCoordinates, Cell *>::iterator iter(mCells.begin());
while (iter != mCells.end()) while (iter != mCells.end())
{ {
getSceneManager()->getBillboardSet("CellBillboardSet" + iter->first.getId(mWorldspace))->setVisible(display); getSceneManager()->getBillboardSet("CellBillboardSet" + iter->first.getId(mWorldspace))->setVisible(display);
iter++; iter++;
} }
} }
bool CSVRender::PagedWorldspaceWidget::adjustCells() bool CSVRender::PagedWorldspaceWidget::adjustCells()
@ -224,10 +224,10 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells()
delete iter->second; delete iter->second;
mCells.erase (iter++); mCells.erase (iter++);
getSceneManager()->getSceneNode("CellBillboardNode" + iter->first.getId(mWorldspace))->detachAllObjects(); getSceneManager()->getSceneNode("CellBillboardNode" + iter->first.getId(mWorldspace))->detachAllObjects();
getSceneManager()->getBillboardSet("CellBillboardSet" + iter->first.getId(mWorldspace))->removeBillboard( getSceneManager()->getBillboardSet("CellBillboardSet" + iter->first.getId(mWorldspace))->removeBillboard(
getSceneManager()->getBillboardSet("CellBillboardSet" + iter->first.getId(mWorldspace))->getBillboard(0)); getSceneManager()->getBillboardSet("CellBillboardSet" + iter->first.getId(mWorldspace))->getBillboard(0));
getSceneManager()->destroyBillboardSet("CellBillboardSet" + iter->first.getId(mWorldspace)); getSceneManager()->destroyBillboardSet("CellBillboardSet" + iter->first.getId(mWorldspace));
modified = true; modified = true;
} }
@ -258,60 +258,60 @@ bool CSVRender::PagedWorldspaceWidget::adjustCells()
new Cell (mDocument.getData(), getSceneManager(), new Cell (mDocument.getData(), getSceneManager(),
iter->getId (mWorldspace)))); iter->getId (mWorldspace))));
//billboard which indicate the Cell coord //billboard which indicate the Cell coord
Ogre::SceneNode* billboardNode = getSceneManager()->getRootSceneNode()->createChildSceneNode("CellBillboardNode" + iter->getId(mWorldspace)); Ogre::SceneNode* billboardNode = getSceneManager()->getRootSceneNode()->createChildSceneNode("CellBillboardNode" + iter->getId(mWorldspace));
billboardNode->setPosition(8192 * iter->getX() + 4096, 8192 * iter->getY() + 4096, 0); billboardNode->setPosition(8192 * iter->getX() + 4096, 8192 * iter->getY() + 4096, 0);
Ogre::Font* font; Ogre::Font* font;
if (!Ogre::FontManager::getSingletonPtr()->resourceExists("CellBillboardFont" + iter->getId(mWorldspace))) if (!Ogre::FontManager::getSingletonPtr()->resourceExists("CellBillboardFont" + iter->getId(mWorldspace)))
{ {
font = Ogre::FontManager::getSingletonPtr()->create("CellBillboardFont" + iter->getId(mWorldspace), "Data00000001").getPointer(); font = Ogre::FontManager::getSingletonPtr()->create("CellBillboardFont" + iter->getId(mWorldspace), "Data00000001").getPointer();
font->setType(Ogre::FT_TRUETYPE); font->setType(Ogre::FT_TRUETYPE);
font->setSource("Comic.ttf"); font->setSource("Comic.ttf");
font->setTrueTypeSize(256); font->setTrueTypeSize(256);
font->load(); font->load();
} }
else else
{ {
font = Ogre::FontManager::getSingletonPtr()->getByName("CellBillboardFont" + iter->getId(mWorldspace)).getPointer(); font = Ogre::FontManager::getSingletonPtr()->getByName("CellBillboardFont" + iter->getId(mWorldspace)).getPointer();
} }
Ogre::TexturePtr texture; Ogre::TexturePtr texture;
if (Ogre::TextureManager::getSingleton().resourceExists("CellBillboardTexture" + iter->getId(mWorldspace))) if (Ogre::TextureManager::getSingleton().resourceExists("CellBillboardTexture" + iter->getId(mWorldspace)))
{ {
texture = Ogre::TextureManager::getSingleton().getByName("CellBillboardTexture" + iter->getId(mWorldspace)); texture = Ogre::TextureManager::getSingleton().getByName("CellBillboardTexture" + iter->getId(mWorldspace));
} }
else else
{ {
texture = Ogre::TextureManager::getSingleton().createManual("CellBillboardTexture" + iter->getId(mWorldspace), "Data00000001", Ogre::TEX_TYPE_2D, 1024, 512, Ogre::MIP_UNLIMITED, Ogre::PF_X8R8G8B8, Ogre::TU_STATIC | Ogre::TU_AUTOMIPMAP); texture = Ogre::TextureManager::getSingleton().createManual("CellBillboardTexture" + iter->getId(mWorldspace), "Data00000001", Ogre::TEX_TYPE_2D, 1024, 512, Ogre::MIP_UNLIMITED, Ogre::PF_X8R8G8B8, Ogre::TU_STATIC | Ogre::TU_AUTOMIPMAP);
WriteToTexture(std::to_string(iter->getX()) + ";" + std::to_string(iter->getY()), texture, Ogre::Image::Box(0, 100, 1024, 512), font, Ogre::ColourValue(1.0, 1.0, 1.0, 1.0), 'c'); WriteToTexture(std::to_string(iter->getX()) + ";" + std::to_string(iter->getY()), texture, Ogre::Image::Box(0, 100, 1024, 512), font, Ogre::ColourValue(1.0, 1.0, 1.0, 1.0), 'c');
} }
Ogre::MaterialPtr material; Ogre::MaterialPtr material;
if (Ogre::MaterialManager::getSingleton().resourceExists("CellBillboardMaterial" + iter->getId(mWorldspace))) if (Ogre::MaterialManager::getSingleton().resourceExists("CellBillboardMaterial" + iter->getId(mWorldspace)))
{ {
material = Ogre::MaterialManager::getSingleton().getByName("CellBillboardMaterial" + iter->getId(mWorldspace)); material = Ogre::MaterialManager::getSingleton().getByName("CellBillboardMaterial" + iter->getId(mWorldspace));
} }
else else
{ {
material = Ogre::MaterialManager::getSingleton().create( material = Ogre::MaterialManager::getSingleton().create(
"CellBillboardMaterial" + iter->getId(mWorldspace), // name "CellBillboardMaterial" + iter->getId(mWorldspace), // name
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
material->getTechnique(0)->getPass(0)->createTextureUnitState("CellBillboardTexture" + iter->getId(mWorldspace)); material->getTechnique(0)->getPass(0)->createTextureUnitState("CellBillboardTexture" + iter->getId(mWorldspace));
material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA); material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
material->setDepthCheckEnabled(false); material->setDepthCheckEnabled(false);
material->setDepthWriteEnabled(false); material->setDepthWriteEnabled(false);
} }
Ogre::BillboardSet* mySet = getSceneManager()->createBillboardSet("CellBillboardSet" + iter->getId(mWorldspace)); Ogre::BillboardSet* mySet = getSceneManager()->createBillboardSet("CellBillboardSet" + iter->getId(mWorldspace));
Ogre::Billboard* myBillboard = mySet->createBillboard(Ogre::Vector3(0, 0, 0)); Ogre::Billboard* myBillboard = mySet->createBillboard(Ogre::Vector3(0, 0, 0));
mySet->setMaterial(material); mySet->setMaterial(material);
myBillboard->setDimensions(4000, 2000); myBillboard->setDimensions(4000, 2000);
mySet->setRenderQueueGroup(mySet->getRenderQueueGroup() + 1); // render the bilboard on top mySet->setRenderQueueGroup(mySet->getRenderQueueGroup() + 1); // render the bilboard on top
billboardNode->attachObject(mySet); billboardNode->attachObject(mySet);
mySet->setVisible(mDisplayCellCoord); mySet->setVisible(mDisplayCellCoord);
modified = true; modified = true;
} }

View file

@ -18,7 +18,7 @@ namespace CSVRender
CSMWorld::CellSelection mSelection; CSMWorld::CellSelection mSelection;
std::map<CSMWorld::CellCoordinates, Cell *> mCells; std::map<CSMWorld::CellCoordinates, Cell *> mCells;
std::string mWorldspace; std::string mWorldspace;
bool mDisplayCellCoord; bool mDisplayCellCoord;
private: private:
@ -53,9 +53,9 @@ namespace CSVRender
void useViewHint (const std::string& hint); void useViewHint (const std::string& hint);
void setCellSelection (const CSMWorld::CellSelection& selection); void setCellSelection(const CSMWorld::CellSelection& selection);
void displayCellCoord(bool display); void displayCellCoord(bool display);
virtual void handleDrop(const std::vector<CSMWorld::UniversalId>& data); virtual void handleDrop(const std::vector<CSMWorld::UniversalId>& data);