| 1 | #include "ValueHistory.h" |
| 2 | #include "imgui.h" |
| 3 | #include <string.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #ifdef WIN32 |
| 7 | # define snprintf _snprintf |
| 8 | #endif |
| 9 | |
| 10 | ValueHistory::ValueHistory() : |
| 11 | m_hsamples(0) |
| 12 | { |
| 13 | for (int i = 0; i < MAX_HISTORY; ++i) |
| 14 | m_samples[i] = 0; |
| 15 | } |
| 16 | |
| 17 | float ValueHistory::getSampleMin() const |
| 18 | { |
| 19 | float val = m_samples[0]; |
| 20 | for (int i = 1; i < MAX_HISTORY; ++i) |
| 21 | if (m_samples[i] < val) |
| 22 | val = m_samples[i]; |
| 23 | return val; |
| 24 | } |
| 25 | |
| 26 | float ValueHistory::getSampleMax() const |
| 27 | { |
| 28 | float val = m_samples[0]; |
| 29 | for (int i = 1; i < MAX_HISTORY; ++i) |
| 30 | if (m_samples[i] > val) |
| 31 | val = m_samples[i]; |
| 32 | return val; |
| 33 | } |
| 34 | |
| 35 | float ValueHistory::getAverage() const |
| 36 | { |
| 37 | float val = 0; |
| 38 | for (int i = 0; i < MAX_HISTORY; ++i) |
| 39 | val += m_samples[i]; |
| 40 | return val/(float)MAX_HISTORY; |
| 41 | } |
| 42 | |
| 43 | void GraphParams::setRect(int ix, int iy, int iw, int ih, int ipad) |
| 44 | { |
| 45 | x = ix; |
| 46 | y = iy; |
| 47 | w = iw; |
| 48 | h = ih; |
| 49 | pad = ipad; |
| 50 | } |
| 51 | |
| 52 | void GraphParams::setValueRange(float ivmin, float ivmax, int indiv, const char* iunits) |
| 53 | { |
| 54 | vmin = ivmin; |
| 55 | vmax = ivmax; |
| 56 | ndiv = indiv; |
| 57 | strcpy(units, iunits); |
| 58 | } |
| 59 | |
| 60 | void drawGraphBackground(const GraphParams* p) |
| 61 | { |
| 62 | // BG |
| 63 | imguiDrawRoundedRect((float)p->x, (float)p->y, (float)p->w, (float)p->h, (float)p->pad, imguiRGBA(64,64,64,128)); |
| 64 | |
| 65 | const float sy = (p->h-p->pad*2) / (p->vmax-p->vmin); |
| 66 | const float oy = p->y+p->pad-p->vmin*sy; |
| 67 | |
| 68 | char text[64]; |
| 69 | |
| 70 | // Divider Lines |
| 71 | for (int i = 0; i <= p->ndiv; ++i) |
| 72 | { |
| 73 | const float u = (float)i/(float)p->ndiv; |
| 74 | const float v = p->vmin + (p->vmax-p->vmin)*u; |
| 75 | snprintf(text, 64, "%.2f %s", v, p->units); |
| 76 | const float fy = oy + v*sy; |
| 77 | imguiDrawText(p->x + p->w - p->pad, (int)fy-4, IMGUI_ALIGN_RIGHT, text, imguiRGBA(0,0,0,255)); |
| 78 | imguiDrawLine((float)p->x + (float)p->pad, fy, (float)p->x + (float)p->w - (float)p->pad - 50, fy, 1.0f, imguiRGBA(0,0,0,64)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void drawGraph(const GraphParams* p, const ValueHistory* graph, |
| 83 | int idx, const char* label, const unsigned int col) |
| 84 | { |
| 85 | const float sx = (p->w - p->pad*2) / (float)graph->getSampleCount(); |
| 86 | const float sy = (p->h - p->pad*2) / (p->vmax - p->vmin); |
| 87 | const float ox = (float)p->x + (float)p->pad; |
| 88 | const float oy = (float)p->y + (float)p->pad - p->vmin*sy; |
| 89 | |
| 90 | // Values |
| 91 | float px=0, py=0; |
| 92 | for (int i = 0; i < graph->getSampleCount()-1; ++i) |
| 93 | { |
| 94 | const float x = ox + i*sx; |
| 95 | const float y = oy + graph->getSample(i)*sy; |
| 96 | if (i > 0) |
| 97 | imguiDrawLine(px,py, x,y, 2.0f, col); |
| 98 | px = x; |
| 99 | py = y; |
| 100 | } |
| 101 | |
| 102 | // Label |
| 103 | const int size = 15; |
| 104 | const int spacing = 10; |
| 105 | int ix = p->x + p->w + 5; |
| 106 | int iy = p->y + p->h - (idx+1)*(size+spacing); |
| 107 | |
| 108 | imguiDrawRoundedRect((float)ix, (float)iy, (float)size, (float)size, 2.0f, col); |
| 109 | |
| 110 | char text[64]; |
| 111 | snprintf(text, 64, "%.2f %s", graph->getAverage(), p->units); |
| 112 | imguiDrawText(ix+size+5, iy+3, IMGUI_ALIGN_LEFT, label, imguiRGBA(255,255,255,192)); |
| 113 | imguiDrawText(ix+size+150, iy+3, IMGUI_ALIGN_RIGHT, text, imguiRGBA(255,255,255,128)); |
| 114 | } |
| 115 |