CubicSDR/src/visual/GainCanvas.cpp

230 lines
5.7 KiB
C++
Raw Normal View History

#include "GainCanvas.h"
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#if !wxUSE_GLCANVAS
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
#endif
#include "CubicSDR.h"
#include "CubicSDRDefs.h"
#include "AppFrame.h"
#include <algorithm>
wxBEGIN_EVENT_TABLE(GainCanvas, wxGLCanvas) EVT_PAINT(GainCanvas::OnPaint)
EVT_IDLE(GainCanvas::OnIdle)
EVT_MOTION(GainCanvas::OnMouseMoved)
EVT_LEFT_DOWN(GainCanvas::OnMouseDown)
EVT_LEFT_UP(GainCanvas::OnMouseReleased)
EVT_LEAVE_WINDOW(GainCanvas::OnMouseLeftWindow)
EVT_ENTER_WINDOW(GainCanvas::OnMouseEnterWindow)
EVT_MOUSEWHEEL(GainCanvas::OnMouseWheelMoved)
wxEND_EVENT_TABLE()
GainCanvas::GainCanvas(wxWindow *parent, int *dispAttrs) :
InteractiveCanvas(parent, dispAttrs) {
glContext = new PrimaryGLContext(this, &wxGetApp().GetContext(this));
bgPanel.setCoordinateSystem(GLPanel::GLPANEL_Y_UP);
bgPanel.setFill(GLPanel::GLPANEL_FILL_GRAD_X);
numGains = 1;
spacing = 2.0/numGains;
barWidth = (1.0/numGains)*0.8;
startPos = spacing/2.0;
barHeight = 0.8f;
refreshCounter = 0;
}
GainCanvas::~GainCanvas() {
}
void GainCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
wxPaintDC dc(this);
const wxSize ClientSize = GetClientSize();
glContext->SetCurrent(*this);
initGLExtensions();
glViewport(0, 0, ClientSize.x, ClientSize.y);
2016-07-07 22:37:57 -04:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
bgPanel.draw();
SwapBuffers();
}
void GainCanvas::OnIdle(wxIdleEvent &event) {
if (mouseTracker.mouseInView()) {
Refresh();
} else {
event.Skip();
}
2016-07-07 22:37:57 -04:00
for (auto gi : gainPanels) {
if (gi->getChanged()) {
wxGetApp().setGain(gi->getName(), gi->getValue());
gi->setChanged(false);
}
}
}
void GainCanvas::SetLevel() {
2016-07-07 22:37:57 -04:00
CubicVR::vec2 mpos = mouseTracker.getGLXY();
for (auto gi : gainPanels) {
if (gi->isMeterHit(mpos)) {
float value = gi->getMeterHitValue(mpos, *gi);
gi->setValue(value);
gi->setChanged(true);
break;
}
}
}
void GainCanvas::OnMouseMoved(wxMouseEvent& event) {
InteractiveCanvas::OnMouseMoved(event);
2016-07-07 22:37:57 -04:00
CubicVR::vec2 mpos = mouseTracker.getGLXY();
2016-07-07 22:37:57 -04:00
for (auto gi : gainPanels) {
if (gi->isMeterHit(mpos)) {
float value = gi->getMeterHitValue(mpos, *gi);
gi->setHighlight(value);
gi->setHighlightVisible(true);
wxGetApp().setActiveGainEntry(gi->getName());
} else {
gi->setHighlightVisible(false);
}
}
if (mouseTracker.mouseDown()) {
SetLevel();
}
}
void GainCanvas::OnMouseDown(wxMouseEvent& event) {
InteractiveCanvas::OnMouseDown(event);
SetLevel();
}
void GainCanvas::OnMouseWheelMoved(wxMouseEvent& event) {
InteractiveCanvas::OnMouseWheelMoved(event);
CubicVR::vec2 hitResult;
2016-07-07 22:37:57 -04:00
// int panelHit = GetPanelHit(hitResult);
//
// if (panelHit >= 0) {
// float movement = 3.0 * (float)event.GetWheelRotation();
//
// GainInfo *gInfo;
//
// gInfo = gainInfo[panelHit];
//
// gInfo->current = gInfo->current + ((movement / 100.0) * ((gInfo->high - gInfo->low) / 100.0));
//
// //BEGIN Clamp to prevent the meter to escape
// if (gInfo->current > gInfo->high) {
// gInfo->current = gInfo->high;
// }
// if (gInfo->current < gInfo->low) {
// gInfo->current = gInfo->low;
// }
//
// gInfo->changed = true;
//
// float levelVal = float(gInfo->current-gInfo->low)/float(gInfo->high-gInfo->low);
// gInfo->levelPanel.setSize(1.0, levelVal);
// gInfo->levelPanel.setPosition(0.0, levelVal-1.0);
//
// gInfo->valuePanel.setText(std::to_string(int(gInfo->current)),GLFont::GLFONT_ALIGN_CENTER, GLFont::GLFONT_ALIGN_CENTER, true);
// }
}
void GainCanvas::OnMouseReleased(wxMouseEvent& event) {
InteractiveCanvas::OnMouseReleased(event);
}
void GainCanvas::OnMouseLeftWindow(wxMouseEvent& event) {
InteractiveCanvas::OnMouseLeftWindow(event);
SetCursor(wxCURSOR_CROSS);
2016-07-07 22:37:57 -04:00
for (auto gi : gainPanels) {
gi->setHighlightVisible(false);
}
2016-07-07 22:37:57 -04:00
Refresh();
}
void GainCanvas::OnMouseEnterWindow(wxMouseEvent& event) {
InteractiveCanvas::mouseTracker.OnMouseEnterWindow(event);
SetCursor(wxCURSOR_CROSS);
#ifdef _WIN32
if (wxGetApp().getAppFrame()->canFocus()) {
this->SetFocus();
}
#endif
}
void GainCanvas::setHelpTip(std::string tip) {
helpTip = tip;
}
void GainCanvas::updateGainUI() {
SDRDeviceInfo *devInfo = wxGetApp().getDevice();
DeviceConfig *devConfig = wxGetApp().getConfig()->getDevice(devInfo->getDeviceId());
gains = devInfo->getGains(SOAPY_SDR_RX, 0);
SDRRangeMap::iterator gi;
numGains = gains.size();
float i = 0;
if (!numGains) {
return;
}
spacing = 2.0/numGains;
barWidth = (1.0/numGains)*0.7;
startPos = spacing/2.0;
2016-07-07 22:37:57 -04:00
barHeight = 1.0f;
2016-07-07 22:37:57 -04:00
while (gainPanels.size()) {
MeterPanel *mDel = gainPanels.back();
gainPanels.pop_back();
bgPanel.removeChild(mDel);
delete mDel;
}
2016-07-07 22:37:57 -04:00
for (auto gi : gains) {
MeterPanel *mPanel = new MeterPanel(gi.first, gi.second.minimum(), gi.second.maximum(), devConfig->getGain(gi.first,wxGetApp().getGain(gi.first)));
2016-07-07 22:37:57 -04:00
float midPos = -1.0+startPos+spacing*i;
mPanel->setPosition(midPos, 0);
mPanel->setSize(barWidth, barHeight);
bgPanel.addChild(mPanel);
2016-07-07 22:37:57 -04:00
gainPanels.push_back(mPanel);
i++;
}
setThemeColors();
}
void GainCanvas::setThemeColors() {
Refresh();
}