summaryrefslogtreecommitdiff
path: root/src/player/RectNode.cpp
blob: 980a957afc44692eb04fb1cf5c06b10821e1677d (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
//
//  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 "RectNode.h"

#include "TypeDefinition.h"

#include "../graphics/VertexArray.h"
#include "../base/Exception.h"
#include "../base/MathHelper.h"

#include <iostream>
#include <sstream>

using namespace std;

namespace avg {

void RectNode::registerType()
{
    float texCoords[] = {0, 0.25f, 0.5f, 0.75f, 1};
    TypeDefinition def = TypeDefinition("rect", "filledvectornode",
            ExportedObject::buildObject<RectNode>)
        .addArg(Arg<glm::vec2>("pos", glm::vec2(0,0), false, 
                offsetof(RectNode, m_Rect.tl)))
        .addArg(Arg<glm::vec2>("size", glm::vec2(0,0)))
        .addArg(Arg<float>("angle", 0.0f, false, offsetof(RectNode, m_Angle)))
        .addArg(Arg<vector<float> >("texcoords", vectorFromCArray(5, texCoords), false,
                offsetof(RectNode, m_TexCoords)))
        ;
    TypeRegistry::get()->registerType(def);
}

RectNode::RectNode(const ArgList& args)
    : FilledVectorNode(args)
{
    args.setMembers(this);
    setSize(args.getArgVal<glm::vec2>("size"));
}

RectNode::~RectNode()
{
}

const glm::vec2& RectNode::getPos() const 
{
    return m_Rect.tl;
}

void RectNode::setPos(const glm::vec2& pt) 
{
    float w = m_Rect.width();
    float h = m_Rect.height();
    m_Rect.tl = pt;
    m_Rect.setWidth(w);
    m_Rect.setHeight(h);
    setDrawNeeded();
}

glm::vec2 RectNode::getSize() const 
{
    return m_Rect.size();
}

void RectNode::setSize(const glm::vec2& pt) 
{
    m_Rect.setWidth(pt.x);
    m_Rect.setHeight(pt.y);
    notifySubscribers("SIZE_CHANGED", m_Rect.size());
    setDrawNeeded();
}

const vector<float>& RectNode::getTexCoords() const
{
    return m_TexCoords;
}

void RectNode::setTexCoords(const vector<float>& coords)
{
    if (coords.size() != 5) {
        throw(Exception(AVG_ERR_OUT_OF_RANGE, 
                "Number of texture coordinates for a rectangle must be 5."));
    }
    m_TexCoords = coords;
    setDrawNeeded();
}

float RectNode::getAngle() const
{
    return m_Angle;
}

void RectNode::setAngle(float angle)
{
    m_Angle = fmod(angle, 2*PI);
    setDrawNeeded();
}

glm::vec2 RectNode::toLocal(const glm::vec2& globalPos) const
{
    glm::vec2 localPos = globalPos - m_Rect.tl;
    glm::vec2 pivot = m_Rect.size()/2.f;
    return getRotatedPivot(localPos, -m_Angle, pivot);
}

glm::vec2 RectNode::toGlobal(const glm::vec2& localPos) const
{
    glm::vec2 pivot = m_Rect.tl + m_Rect.size()/2.f;
    glm::vec2 globalPos = getRotatedPivot(localPos, m_Angle, pivot); 
    return globalPos + m_Rect.tl;
}

void RectNode::getElementsByPos(const glm::vec2& pos, vector<NodePtr>& pElements)
{
    if (pos.x >= 0 && pos.y >= 0 && pos.x < m_Rect.size().x && pos.y < m_Rect.size().y 
            && reactsToMouseEvents())
    {
        pElements.push_back(getSharedThis());
    }
}

void RectNode::calcVertexes(const VertexDataPtr& pVertexData, Pixel32 color)
{
    glm::vec2 pivot = m_Rect.tl+m_Rect.size()/2.f;

    glm::vec2 p1 = m_Rect.tl;
    glm::vec2 p2(m_Rect.tl.x, m_Rect.br.y);
    glm::vec2 p3 = m_Rect.br;
    glm::vec2 p4(m_Rect.br.x, m_Rect.tl.y);
    
    vector<glm::vec2> pts; 
    pts.push_back(getRotatedPivot(p1, m_Angle, pivot));
    pts.push_back(getRotatedPivot(p2, m_Angle, pivot));
    pts.push_back(getRotatedPivot(p3, m_Angle, pivot));
    pts.push_back(getRotatedPivot(p4, m_Angle, pivot));
    calcPolyLine(pts, m_TexCoords, true, LJ_MITER, pVertexData, color);
}

void RectNode::calcFillVertexes(const VertexDataPtr& pVertexData, Pixel32 color)
{
    glm::vec2 pivot = m_Rect.tl+m_Rect.size()/2.f;

    glm::vec2 p1 = m_Rect.tl;
    glm::vec2 p2(m_Rect.tl.x, m_Rect.br.y);
    glm::vec2 p3 = m_Rect.br;
    glm::vec2 p4(m_Rect.br.x, m_Rect.tl.y);
    glm::vec2 rp1 = getRotatedPivot(p1, m_Angle, pivot);
    glm::vec2 rp2 = getRotatedPivot(p2, m_Angle, pivot);
    glm::vec2 rp3 = getRotatedPivot(p3, m_Angle, pivot);
    glm::vec2 rp4 = getRotatedPivot(p4, m_Angle, pivot);
    pVertexData->appendPos(rp1, getFillTexCoord1(), color);
    glm::vec2 blTexCoord = glm::vec2(getFillTexCoord1().x, getFillTexCoord2().y);
    pVertexData->appendPos(rp2, blTexCoord, color);
    pVertexData->appendPos(rp3, getFillTexCoord2(), color);
    glm::vec2 trTexCoord = glm::vec2(getFillTexCoord2().x, getFillTexCoord1().y);
    pVertexData->appendPos(rp4, trTexCoord, color);
    pVertexData->appendQuadIndexes(1, 0, 2, 3);
}

}