File: cellmarker.cpp

package info (click to toggle)
openmw 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,076 kB
  • sloc: cpp: 380,958; xml: 2,192; sh: 1,449; python: 911; makefile: 26; javascript: 5
file content (101 lines) | stat: -rw-r--r-- 3,069 bytes parent folder | download
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
#include "cellmarker.hpp"

#include <string>

#include <osg/AutoTransform>
#include <osg/GL>
#include <osg/Group>
#include <osg/Material>
#include <osg/StateAttribute>
#include <osg/StateSet>
#include <osg/Vec3f>
#include <osg/Vec4f>
#include <osgText/Text>
#include <osgText/TextBase>

#include <components/misc/constants.hpp>

#include <apps/opencs/model/world/cellcoordinates.hpp>
#include <apps/opencs/view/render/mask.hpp>
#include <apps/opencs/view/render/tagbase.hpp>

CSVRender::CellMarkerTag::CellMarkerTag(CellMarker* marker)
    : TagBase(Mask_CellMarker)
    , mMarker(marker)
{
}

CSVRender::CellMarker* CSVRender::CellMarkerTag::getCellMarker() const
{
    return mMarker;
}

void CSVRender::CellMarker::buildMarker()
{
    const int characterSize = 20;

    // Set up attributes of marker text.
    osg::ref_ptr<osgText::Text> markerText(new osgText::Text);
    markerText->setLayout(osgText::Text::LEFT_TO_RIGHT);
    markerText->setCharacterSize(characterSize);
    markerText->setAlignment(osgText::Text::CENTER_CENTER);
    markerText->setDrawMode(osgText::Text::TEXT | osgText::Text::FILLEDBOUNDINGBOX);

    // If cell exists then show black bounding box otherwise show red.
    if (mExists)
    {
        markerText->setBoundingBoxColor(osg::Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
    }
    else
    {
        markerText->setBoundingBoxColor(osg::Vec4f(1.0f, 0.0f, 0.0f, 1.0f));
    }

    // Add text containing cell's coordinates.
    std::string coordinatesText = std::to_string(mCoordinates.getX()) + "," + std::to_string(mCoordinates.getY());
    markerText->setText(coordinatesText);

    // Add text to marker node.
    mMarkerNode->addChild(markerText);
}

void CSVRender::CellMarker::positionMarker()
{
    const int cellSize = Constants::CellSizeInUnits;
    const int markerHeight = 0;

    // Move marker to center of cell.
    int x = (mCoordinates.getX() * cellSize) + (cellSize / 2);
    int y = (mCoordinates.getY() * cellSize) + (cellSize / 2);
    mMarkerNode->setPosition(osg::Vec3f(x, y, markerHeight));
}

CSVRender::CellMarker::CellMarker(
    osg::Group* cellNode, const CSMWorld::CellCoordinates& coordinates, const bool cellExists)
    : mCellNode(cellNode)
    , mCoordinates(coordinates)
    , mExists(cellExists)
{
    // Set up node for cell marker.
    mMarkerNode = new osg::AutoTransform();
    mMarkerNode->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
    mMarkerNode->setAutoScaleToScreen(true);
    mMarkerNode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
    mMarkerNode->getOrCreateStateSet()->setRenderBinDetails(11, "RenderBin");
    osg::ref_ptr<osg::Material> mat = new osg::Material;
    mat->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
    mMarkerNode->getOrCreateStateSet()->setAttribute(mat);

    mMarkerNode->setUserData(new CellMarkerTag(this));
    mMarkerNode->setNodeMask(Mask_CellMarker);

    mCellNode->addChild(mMarkerNode);

    buildMarker();
    positionMarker();
}

CSVRender::CellMarker::~CellMarker()
{
    mCellNode->removeChild(mMarkerNode);
}