-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cpp
134 lines (103 loc) · 3.37 KB
/
settings.cpp
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
#include "settings.hpp"
#include "colormap/colormap.h"
#include "imgui.h"
#include "imgui_elements.h"
#include <algorithm>
#include <iterator>
#include <iostream>
PaletteSetting::PaletteSetting(std::string pname)
: Setting("Palette") {
auto l = colormap::ColormapList::getAll();
for (auto &m : l) {
names.push_back(m->getTitle());
maps[m->getTitle()] = m;
}
vc = VectorCombo("Colormap", names);
vc.set_index(pname);
current_cmap = maps.at(pname);
}
bool PaletteSetting::RenderGui() {
ImGui::Checkbox("invert", &invert);
ImGui::SameLine(0, 1);
bool ret = vc.RenderGui();
if (ret)
current_cmap = maps.at(vc.get_value());
ScrollableSliderUInt("Max colors", &color_max, 1, 1024*32, "%d", 128);
ImGui::Text("current color / max %d / %d", current_color, color_max);
ImGui::Text("current color %x", get_color(current_color));
return ret;
}
void PaletteSetting::rescale(uint32_t ncolours) {
color_max = ncolours;
}
uint32_t PaletteSetting::get_color(uint32_t color_n) {
if (color_n > color_max)
std::cerr << "color_n > color_max " << color_n << " > " << color_max << std::endl;
return get_colorf((float)(color_n%color_max)/color_max);
}
uint32_t PaletteSetting::get_colorf(float color_n) const {
if (invert)
color_n = 1 / color_n;
auto c = current_cmap->getColor(color_n);
return ImGui::ColorConvertFloat4ToU32({static_cast<float>(c.r),
static_cast<float>(c.g),
static_cast<float>(c.b),
static_cast<float>(c.a)});
}
float PaletteSetting::get_color_index(uint32_t color_n) const {
return (float)color_n / color_max;
}
float PaletteSetting::get_next_color_index() {
++current_color;
if (current_color > color_max)
current_color = 0;
return (float)current_color / color_max;
}
uint32_t PaletteSetting::get_next_color() {
return get_color(get_next_color_index());
}
const colormap::Colormap & PaletteSetting::get_cmap() {
return *current_cmap;
}
bool VectorCombo::RenderGui() {
bool ret = false;
auto pb = items.begin();
std::advance(pb, item_current_idx);
int size = items.size();
const char* combo_preview_value = pb->c_str();
if (ImGui::BeginCombo(name.c_str(), combo_preview_value, 0))
{
auto pb = items.begin();
for (int n = 0; n < size; n++)
{
const bool is_selected = (item_current_idx == n);
const char * name = pb->c_str();
if (ImGui::Selectable(name, is_selected))
{
item_current_idx = n;
value = *pb;
ret = true;
}
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
ImGui::SetItemDefaultFocus();
std::advance(pb, 1);
}
ImGui::EndCombo();
}
return ret;
}
void VectorCombo::set_index(std::string p) {
auto it = std::find(items.begin(), items.end(), p);
set_index(std::distance(items.begin(), it));
}
void VectorCombo::set_index(int i) {
item_current_idx = i;
value = items.at(item_current_idx);
}
int VectorCombo::get_index() const {
return item_current_idx;
}
std::string VectorCombo::get_value() const {
return value;
}