mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-06-25 20:07:51 +02:00
Added new deemphasis mode + new image widget
This commit is contained in:
66
core/src/gui/widgets/image.cpp
Normal file
66
core/src/gui/widgets/image.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
#include <gui/widgets/image.h>
|
||||
|
||||
namespace ImGui {
|
||||
ImageDisplay::ImageDisplay(int width, int height, GLenum format) {
|
||||
_width = width;
|
||||
_height = height;
|
||||
_format = format;
|
||||
buffer = malloc(_width * _height * 4);
|
||||
activeBuffer = malloc(_width * _height * 4);
|
||||
memset(buffer, 0, _width * _height * 4);
|
||||
memset(activeBuffer, 0, _width * _height * 4);
|
||||
|
||||
glGenTextures(1, &textureId);
|
||||
}
|
||||
|
||||
ImageDisplay::~ImageDisplay() {
|
||||
free(buffer);
|
||||
free(activeBuffer);
|
||||
}
|
||||
|
||||
void ImageDisplay::draw(const ImVec2& size_arg) {
|
||||
std::lock_guard<std::mutex> lck(bufferMtx);
|
||||
|
||||
ImGuiWindow* window = GetCurrentWindow();
|
||||
ImGuiStyle& style = GetStyle();
|
||||
float pad = style.FramePadding.y;
|
||||
ImVec2 min = window->DC.CursorPos;
|
||||
|
||||
// Calculate scale
|
||||
float width = CalcItemWidth();
|
||||
float height = roundf((width / (float)_width) * (float)_height);
|
||||
|
||||
ImVec2 size = CalcItemSize(size_arg, CalcItemWidth(), height);
|
||||
ImRect bb(min, ImVec2(min.x+size.x, min.y+size.y));
|
||||
float lineHeight = size.y;
|
||||
|
||||
ItemSize(size, style.FramePadding.y);
|
||||
if (!ItemAdd(bb, 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (newData) {
|
||||
newData = false;
|
||||
updateTexture();
|
||||
}
|
||||
|
||||
window->DrawList->AddImage((void*)(intptr_t)textureId, min, ImVec2(min.x + width, min.y + height));
|
||||
}
|
||||
|
||||
void ImageDisplay::swap() {
|
||||
std::lock_guard<std::mutex> lck(bufferMtx);
|
||||
void* tmp = activeBuffer;
|
||||
activeBuffer = buffer;
|
||||
buffer = tmp;
|
||||
newData = true;
|
||||
memset(buffer, 0, _width * _height * 4);
|
||||
}
|
||||
|
||||
void ImageDisplay::updateTexture() {
|
||||
glBindTexture(GL_TEXTURE_2D, textureId);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, activeBuffer);
|
||||
}
|
||||
}
|
34
core/src/gui/widgets/image.h
Normal file
34
core/src/gui/widgets/image.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
#include <dsp/stream.h>
|
||||
#include <mutex>
|
||||
#include <GL/glew.h>
|
||||
|
||||
namespace ImGui {
|
||||
class ImageDisplay {
|
||||
public:
|
||||
ImageDisplay(int width, int height, GLenum format);
|
||||
~ImageDisplay();
|
||||
void draw(const ImVec2& size_arg = ImVec2(0, 0));
|
||||
void swap();
|
||||
|
||||
void* buffer;
|
||||
|
||||
private:
|
||||
void updateTexture();
|
||||
|
||||
std::mutex bufferMtx;
|
||||
void* activeBuffer;
|
||||
|
||||
int _width;
|
||||
int _height;
|
||||
GLenum _format;
|
||||
|
||||
GLuint textureId;
|
||||
|
||||
bool newData = false;
|
||||
|
||||
};
|
||||
}
|
119
core/src/utils/optionlist.h
Normal file
119
core/src/utils/optionlist.h
Normal file
@ -0,0 +1,119 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
|
||||
template <class K, class T>
|
||||
class OptionList {
|
||||
public:
|
||||
void define(K key, std::string name, T value) {
|
||||
if (keyExists(key)) { throw std::runtime_error("Key already exists"); }
|
||||
if (nameExists(name)) { throw std::runtime_error("Name already exists"); }
|
||||
if (valueExists(value)) { throw std::runtime_error("Value already exists"); }
|
||||
keys.push_back(key);
|
||||
names.push_back(name);
|
||||
values.push_back(value);
|
||||
updateText();
|
||||
}
|
||||
|
||||
void define(std::string name, T value) {
|
||||
define(name, name, value);
|
||||
}
|
||||
|
||||
void undefined(int id) {
|
||||
keys.erase(id);
|
||||
names.erase(id);
|
||||
values.erase(id);
|
||||
updateText();
|
||||
}
|
||||
|
||||
void undefineKey(K key) {
|
||||
undefined(keyId(key));
|
||||
}
|
||||
|
||||
void undefineName(std::string name) {
|
||||
undefined(nameId(name));
|
||||
}
|
||||
|
||||
void undefineValue(T value) {
|
||||
undefined(valueId(value));
|
||||
}
|
||||
|
||||
void clear() {
|
||||
keys.clear();
|
||||
names.clear();
|
||||
values.clear();
|
||||
updateText();
|
||||
}
|
||||
|
||||
int size() {
|
||||
return keys.size();
|
||||
}
|
||||
|
||||
bool keyExists(K key) {
|
||||
if (std::find(keys.begin(), keys.end(), key) != keys.end()) { return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
bool nameExists(std::string name) {
|
||||
if (std::find(names.begin(), names.end(), name) != names.end()) { return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
bool valueExists(T value) {
|
||||
if (std::find(values.begin(), values.end(), value) != values.end()) { return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
int keyId(K key) {
|
||||
auto it = std::find(keys.begin(), keys.end(), key);
|
||||
if (it == keys.end()) { throw std::runtime_error("Key doesn't exists"); }
|
||||
return std::distance(keys.begin(), it);
|
||||
}
|
||||
|
||||
int nameId(std::string name) {
|
||||
auto it = std::find(names.begin(), names.end(), name);
|
||||
if (it == names.end()) { throw std::runtime_error("Name doesn't exists"); }
|
||||
return std::distance(names.begin(), it);
|
||||
}
|
||||
|
||||
int valueId(T value) {
|
||||
auto it = std::find(values.begin(), values.end(), value);
|
||||
if (it == values.end()) { throw std::runtime_error("Value doesn't exists"); }
|
||||
return std::distance(values.begin(), it);
|
||||
}
|
||||
|
||||
K key(int id) {
|
||||
return keys[id];
|
||||
}
|
||||
|
||||
std::string name(int id) {
|
||||
return names[id];
|
||||
}
|
||||
|
||||
T value(int id) {
|
||||
return values[id];
|
||||
}
|
||||
|
||||
T operator [](int& id) {
|
||||
return value(id);
|
||||
}
|
||||
|
||||
const char* txt = NULL;
|
||||
|
||||
private:
|
||||
void updateText() {
|
||||
_txt.clear();
|
||||
for (auto& name : names) {
|
||||
_txt += name;
|
||||
_txt += '\0';
|
||||
}
|
||||
txt = _txt.c_str();
|
||||
}
|
||||
|
||||
std::vector<std::string> keys;
|
||||
std::vector<std::string> names;
|
||||
std::vector<T> values;
|
||||
std::string _txt;
|
||||
|
||||
};
|
Reference in New Issue
Block a user