summaryrefslogtreecommitdiffstats
path: root/src/waveform/renderers/allshader/waveformrendermarkrange.cpp
blob: 2ed3b0d5a34e3b65239b34990403871b58f8a352 (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
#include "waveform/renderers/allshader/waveformrendermarkrange.h"

#include <QColor>
#include <QDomNode>
#include <QObject>
#include <QVector>

#include "preferences/usersettings.h"
#include "track/track.h"
#include "waveform/renderers/allshader/matrixforwidgetgeometry.h"
#include "waveform/widgets/allshader/waveformwidget.h"
#include "widget/wskincolor.h"
#include "widget/wwidget.h"

allshader::WaveformRenderMarkRange::WaveformRenderMarkRange(WaveformWidgetRenderer* waveformWidget)
        : WaveformRenderer(waveformWidget) {
}

allshader::WaveformRenderMarkRange::~WaveformRenderMarkRange() {
}

void allshader::WaveformRenderMarkRange::initializeGL() {
    WaveformRenderer::initializeGL();
    m_shader.init();
}

void allshader::WaveformRenderMarkRange::fillRect(
        const QRectF& rect, QColor color) {
    const float posx1 = static_cast<float>(rect.x());
    const float posx2 = static_cast<float>(rect.x() + rect.width());
    const float posy1 = static_cast<float>(rect.y());
    const float posy2 = static_cast<float>(rect.y() + rect.height());

    const float posarray[] = {posx1, posy1, posx2, posy1, posx1, posy2, posx2, posy2};

    int colorLocation = m_shader.uniformLocation("color");
    int positionLocation = m_shader.attributeLocation("position");

    m_shader.setUniformValue(colorLocation, color);

    m_shader.setAttributeArray(
            positionLocation, GL_FLOAT, posarray, 2);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

void allshader::WaveformRenderMarkRange::setup(const QDomNode& node, const SkinContext& context) {
    m_markRanges.clear();
    m_markRanges.reserve(1);

    QDomNode child = node.firstChild();
    while (!child.isNull()) {
        if (child.nodeName() == "MarkRange") {
            m_markRanges.push_back(
                    WaveformMarkRange(
                            m_waveformRenderer->getGroup(),
                            child,
                            context,
                            *m_waveformRenderer->getWaveformSignalColors()));
        }
        child = child.nextSibling();
    }
}

void allshader::WaveformRenderMarkRange::paintGL() {
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    const QMatrix4x4 matrix = matrixForWidgetGeometry(m_waveformRenderer, false);

    int positionLocation = m_shader.attributeLocation("position");

    m_shader.bind();
    m_shader.enableAttributeArray(positionLocation);

    int matrixLocation = m_shader.uniformLocation("matrix");
    m_shader.setUniformValue(matrixLocation, matrix);

    for (auto&& markRange : m_markRanges) {
        // If the mark range is not active we should not draw it.
        if (!markRange.active()) {
            continue;
        }

        // If the mark range is not visible we should not draw it.
        if (!markRange.visible()) {
            continue;
        }

        // Active mark ranges by definition have starts/ends that are not
        // disabled so no need to check.
        double startSample = markRange.start();
        double endSample = markRange.end();

        double startPosition =
                m_waveformRenderer->transformSamplePositionInRendererWorld(
                        startSample);
        double endPosition = m_waveformRenderer->transformSamplePositionInRendererWorld(endSample);

        startPosition = std::floor(startPosition);
        endPosition = std::floor(endPosition);

        const double span = std::max(endPosition - startPosition, 1.0);

        // range not in the current display
        if (startPosition > m_waveformRenderer->getLength() || endPosition < 0) {
            continue;
        }

        QColor color = markRange.enabled() ? markRange.m_activeColor : markRange.m_disabledColor;
        color.setAlphaF(0.3f);

        fillRect(QRectF(startPosition, 0, span, m_waveformRenderer->getBreadth()), color);
    }
    m_shader.disableAttributeArray(positionLocation);
    m_shader.release();
}