File: TuningContext.cpp

package info (click to toggle)
cubicsdr 0.2.5%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,512 kB
  • sloc: cpp: 32,617; sh: 10; makefile: 7
file content (199 lines) | stat: -rw-r--r-- 5,831 bytes parent folder | download | duplicates (2)
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
// Copyright (c) Charles J. Cliffe
// SPDX-License-Identifier: GPL-2.0+

#include "TuningContext.h"
#include "TuningCanvas.h"

#include "ColorTheme.h"

// http://stackoverflow.com/questions/7276826/c-format-number-with-commas
class comma_numpunct: public std::numpunct<char> {
protected:
    virtual char do_thousands_sep() const {
        return ',';
    }

    virtual std::string do_grouping() const {
        return "\03";
    }
};

TuningContext::TuningContext(TuningCanvas *canvas, wxGLContext *sharedContext) :
        PrimaryGLContext(canvas, sharedContext) {
    glDisable(GL_CULL_FACE);
    glDisable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    comma_locale = std::locale(std::locale(), new comma_numpunct());
    freqStrFormatted.imbue(comma_locale);
}

void TuningContext::DrawBegin() {
    glClearColor(ThemeMgr::mgr.currentTheme->generalBackground.r, ThemeMgr::mgr.currentTheme->generalBackground.g,
            ThemeMgr::mgr.currentTheme->generalBackground.b, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glDisable(GL_TEXTURE_2D);
}

void TuningContext::Draw(float r, float g, float b, float a, float p1, float p2) {
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);
    glBegin(GL_QUADS);
    glColor4f(r * 0.5, g * 0.5, b * 0.5, a);
    glVertex2f(-1.0 + p2 * 2.0, 1.0);
    glVertex2f(-1.0 + p1 * 2.0, 1.0);
    glColor4f(r, g, b, a);
    glVertex2f(-1.0 + p1 * 2.0, 0.0);
    glVertex2f(-1.0 + p2 * 2.0, 0.0);

    glVertex2f(-1.0 + p2 * 2.0, 0.0);
    glVertex2f(-1.0 + p1 * 2.0, 0.0);
    glColor4f(r * 0.5, g * 0.5, b * 0.5, a);
    glVertex2f(-1.0 + p1 * 2.0, -1.0);
    glVertex2f(-1.0 + p2 * 2.0, -1.0);
    glEnd();
    glDisable(GL_BLEND);
}

void TuningContext::DrawEnd() {
//    glFlush();

//    CheckGLError();
}

void TuningContext::DrawTuner(long long freq, int count, float displayPos, float displayWidth) {
    GLint vp[4];
    glGetIntegerv( GL_VIEWPORT, vp);

    float viewWidth = (float) vp[2];
    float viewHeight = (float) vp[3];

    freqStr.str("");
    freqStr << freq;
    std::string freqChars = freqStr.str();

    int fontSize = 32;

    if (viewWidth < 300) {
        fontSize = 18;
    } else if (viewWidth < 500) {
        fontSize = 24;
    }
    
    if (viewHeight < 18) {
        fontSize = 12;
    } else if (viewHeight < 24) {
        fontSize = 16;
    } else if (viewHeight < 28) {
        fontSize = 18;
    }

    glColor3f(ThemeMgr::mgr.currentTheme->text.r, ThemeMgr::mgr.currentTheme->text.g, ThemeMgr::mgr.currentTheme->text.b);
    int numChars = freqChars.length();
    int ofs = count - numChars;

    //do not zoom this one:
    GLFont::Drawer refDrawingFont = GLFont::getFont(fontSize);

    for (int i = ofs; i < count; i++) {
        float xpos = displayPos + (displayWidth / (float) count) * (float) i + ((displayWidth / 2.0) / (float) count);
        refDrawingFont.drawString(freqStr.str().substr(i - ofs, 1), xpos, 0, GLFont::GLFONT_ALIGN_CENTER, GLFont::GLFONT_ALIGN_CENTER);
    }

    glColor4f(0.65f, 0.65f, 0.65f, 0.25f);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBegin(GL_LINES);
    for (int i = count; i >= 0; i--) {
        float xpos = displayPos + (displayWidth / (float) count) * (float) i;
        glVertex2f(xpos, -1.0);
        glVertex2f(xpos, 1.0);
    }
    glEnd();
    glDisable(GL_BLEND);
}


void TuningContext::DrawTunerDigitBox(int index, int count, float displayPos, float displayWidth, RGBA4f /* c */) {
    GLint vp[4];
    glGetIntegerv( GL_VIEWPORT, vp);

    float viewHeight = (float) vp[3];
    float pixelHeight = 2.0/viewHeight;

    glColor4f(1.0, 0,0,1);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    float xpos = displayPos + (displayWidth / (float) count) * (float) (count-index);
    float xpos2 = displayPos + (displayWidth / (float) count) * (float) ((count-1)-index);
    glBegin(GL_LINE_STRIP);
    glVertex2f(xpos, 1.0-pixelHeight);
    glVertex2f(xpos, -1.0+pixelHeight);
    glVertex2f(xpos2, -1.0+pixelHeight);
    glVertex2f(xpos2, 1.0-pixelHeight);
    glVertex2f(xpos, 1.0-pixelHeight);
    glEnd();
    glDisable(GL_BLEND);
}




int TuningContext::GetTunerDigitIndex(float mPos, int count, float displayPos, float displayWidth) {
    mPos -= 0.5;
    mPos *= 2.0;

    float delta = mPos - displayPos;

    if (delta < 0 || delta > displayWidth) {
        return 0;
    }

    int index = floor((delta / displayWidth) * (count));

    return count - index;
}

void TuningContext::DrawTunerBarIndexed(int start, int end, int count, float displayPos, float displayWidth, RGBA4f color, float /* alpha */, bool top,
bool bottom) {
    float ofs = (displayWidth / (float) count);
    float p2 = displayPos + ofs * (float) (count - start + 1);
    float p1 = displayPos + ofs * (float) (count - end);

    float r = color.r, g = color.g, b = color.b, a = 0.6f;

    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE);
    glBegin(GL_QUADS);
    if (top) {
        glColor4f(r * 0.5, g * 0.5, b * 0.5, a);
        glVertex2f(p2, 1.0);
        glVertex2f(p1, 1.0);
        glColor4f(r, g, b, a);
        glVertex2f(p1, 0.0);
        glVertex2f(p2, 0.0);
    }
    if (bottom) {
        glColor4f(r, g, b, a);
        glVertex2f(p2, 0.0);
        glVertex2f(p1, 0.0);
        glColor4f(r * 0.5, g * 0.5, b * 0.5, a);
        glVertex2f(p1, -1.0);
        glVertex2f(p2, -1.0);
    }
    glEnd();
    glDisable(GL_BLEND);
}

void TuningContext::DrawDemodFreqBw(long long freq, unsigned int bw, long long center) {
    DrawTuner(freq, 11, -1.0, (1.0f / 3.0f) * 2.0f);
    DrawTuner(bw, 7, -1.0 + (2.25f / 3.0f), (1.0f / 4.0f) * 2.0f);
    DrawTuner(center, 11, -1.0 + (2.0f / 3.0f) * 2.0, (1.0f / 3.0f) * 2.0f);
}