summaryrefslogtreecommitdiff
path: root/src/player/FilledVectorNode.cpp
blob: a804958be32c05adc422cf324b9bba33afb7f612 (plain)
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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "FilledVectorNode.h"

#include "TypeDefinition.h"
#include "Image.h"
#include "DivNode.h"

#include "../base/ScopeTimer.h"
#include "../base/Logger.h"
#include "../base/Exception.h"

using namespace std;
using namespace boost;

namespace avg {

void FilledVectorNode::registerType()
{
    TypeDefinition def = TypeDefinition("filledvectornode", "vectornode")
        .addArg(Arg<UTF8String>("filltexhref", "", false, 
                offsetof(FilledVectorNode, m_FillTexHRef)))
        .addArg(Arg<float>("fillopacity", 0, false, 
                offsetof(FilledVectorNode, m_FillOpacity)))
        .addArg(Arg<UTF8String>("fillcolor", "FFFFFF", false, 
                offsetof(FilledVectorNode, m_sFillColorName)))
        .addArg(Arg<glm::vec2>("filltexcoord1", glm::vec2(0,0), false,
                offsetof(FilledVectorNode, m_FillTexCoord1)))
        .addArg(Arg<glm::vec2>("filltexcoord2", glm::vec2(1,1), false,
                offsetof(FilledVectorNode, m_FillTexCoord2)))
        ;
    TypeRegistry::get()->registerType(def);
}

FilledVectorNode::FilledVectorNode(const ArgList& args)
    : VectorNode(args),
      m_pFillShape(new Shape(MaterialInfo(GL_REPEAT, GL_REPEAT, false)))
{
    m_FillTexHRef = args.getArgVal<UTF8String>("filltexhref"); 
    setFillTexHRef(m_FillTexHRef);
    m_sFillColorName = args.getArgVal<UTF8String>("fillcolor");
    m_FillColor = colorStringToColor(m_sFillColorName);
}

FilledVectorNode::~FilledVectorNode()
{
}

void FilledVectorNode::connectDisplay()
{
    VectorNode::connectDisplay();
    m_FillColor = colorStringToColor(m_sFillColorName);
    m_pFillShape->moveToGPU();
    m_OldOpacity = -1;
}

void FilledVectorNode::disconnect(bool bKill)
{
    if (bKill) {
        m_pFillShape->discard();
    } else {
        m_pFillShape->moveToCPU();
    }
    VectorNode::disconnect(bKill);
}

void FilledVectorNode::checkReload()
{
    Node::checkReload(m_FillTexHRef, m_pFillShape->getImage());
    if (getState() == Node::NS_CANRENDER) {
        m_pFillShape->moveToGPU();
        setDrawNeeded();
    }
    VectorNode::checkReload();
}

const UTF8String& FilledVectorNode::getFillTexHRef() const
{
    return m_FillTexHRef;
}

void FilledVectorNode::setFillTexHRef(const UTF8String& href)
{
    m_FillTexHRef = href;
    checkReload();
    setDrawNeeded();
}

void FilledVectorNode::setFillBitmap(BitmapPtr pBmp)
{
    m_FillTexHRef = "";
    m_pFillShape->setBitmap(pBmp);
    setDrawNeeded();
}

const glm::vec2& FilledVectorNode::getFillTexCoord1() const
{
    return m_FillTexCoord1;
}

void FilledVectorNode::setFillTexCoord1(const glm::vec2& pt)
{
    m_FillTexCoord1 = pt;
    setDrawNeeded();
}

const glm::vec2& FilledVectorNode::getFillTexCoord2() const
{
    return m_FillTexCoord2;
}

void FilledVectorNode::setFillTexCoord2(const glm::vec2& pt)
{
    m_FillTexCoord2 = pt;
    setDrawNeeded();
}

float FilledVectorNode::getFillOpacity() const
{
    return m_FillOpacity;
}

void FilledVectorNode::setFillOpacity(float opacity)
{
    m_FillOpacity = opacity;
    setDrawNeeded();
}

void FilledVectorNode::preRender(const VertexArrayPtr& pVA, bool bIsParentActive, 
        float parentEffectiveOpacity)
{
    Node::preRender(pVA, bIsParentActive, parentEffectiveOpacity);
    float curOpacity = parentEffectiveOpacity*m_FillOpacity;

    VertexDataPtr pShapeVD = m_pFillShape->getVertexData();
    if (isDrawNeeded() || curOpacity != m_OldOpacity) {
        pShapeVD->reset();
        Pixel32 color = getFillColorVal();
        calcFillVertexes(pShapeVD, color);
        m_OldOpacity = curOpacity;
    }
    if (isVisible()) {
        m_pFillShape->setVertexArray(pVA);
    }
    VectorNode::preRender(pVA, bIsParentActive, parentEffectiveOpacity);
}

static ProfilingZoneID RenderProfilingZone("FilledVectorNode::render");

void FilledVectorNode::render()
{
    ScopeTimer Timer(RenderProfilingZone);
    float curOpacity = getParent()->getEffectiveOpacity()*m_FillOpacity;
    if (curOpacity > 0.01) {
        m_pFillShape->draw(getTransform(), curOpacity);
    }
    VectorNode::render();
}

void FilledVectorNode::setFillColor(const UTF8String& sColor)
{
    if (m_sFillColorName != sColor) {
        m_sFillColorName = sColor;
        m_FillColor = colorStringToColor(m_sFillColorName);
        setDrawNeeded();
    }
}

const UTF8String& FilledVectorNode::getFillColor() const
{
    return m_sFillColorName;
}

Pixel32 FilledVectorNode::getFillColorVal() const
{
    return m_FillColor;
}

glm::vec2 FilledVectorNode::calcFillTexCoord(const glm::vec2& pt, const glm::vec2& minPt, 
        const glm::vec2& maxPt)
{
    glm::vec2 texPt;
    texPt.x = (m_FillTexCoord2.x-m_FillTexCoord1.x)*(pt.x-minPt.x)/(maxPt.x-minPt.x)
            +m_FillTexCoord1.x;
    texPt.y = (m_FillTexCoord2.y-m_FillTexCoord1.y)*(pt.y-minPt.y)/(maxPt.y-minPt.y)
            +m_FillTexCoord1.y;
    return texPt;
}

bool FilledVectorNode::isVisible() const
{
    return getEffectiveActive() && (getEffectiveOpacity() > 0.01 || 
            getParent()->getEffectiveOpacity()*m_FillOpacity > 0.01);
}

}