void drawGraph(const GraphParams* p, const ValueHistory* graph, int idx, const char* label, const unsigned int col) { const float sx = (p->w - p->pad*2) / (float)graph->getSampleCount(); const float sy = (p->h - p->pad*2) / (p->vmax - p->vmin); const float ox = (float)p->x + (float)p->pad; const float oy = (float)p->y + (float)p->pad - p->vmin*sy; // Values float px=0, py=0; for (int i = 0; i < graph->getSampleCount()-1; ++i) { const float x = ox + i*sx; const float y = oy + graph->getSample(i)*sy; if (i > 0) imguiDrawLine(px,py, x,y, 2.0f, col); px = x; py = y; } // Label const int size = 15; const int spacing = 10; int ix = p->x + p->w + 5; int iy = p->y + p->h - (idx+1)*(size+spacing); imguiDrawRoundedRect((float)ix, (float)iy, (float)size, (float)size, 2.0f, col); char text[64]; snprintf(text, 64, "%.2f %s", graph->getAverage(), p->units); imguiDrawText(ix+size+5, iy+3, IMGUI_ALIGN_LEFT, label, imguiRGBA(255,255,255,192)); imguiDrawText(ix+size+150, iy+3, IMGUI_ALIGN_RIGHT, text, imguiRGBA(255,255,255,128)); }
void drawGraphBackground(const GraphParams* p) { // BG imguiDrawRoundedRect((float)p->x, (float)p->y, (float)p->w, (float)p->h, (float)p->pad, imguiRGBA(64,64,64,128)); const float sy = (p->h-p->pad*2) / (p->vmax-p->vmin); const float oy = p->y+p->pad-p->vmin*sy; char text[64]; // Divider Lines for (int i = 0; i <= p->ndiv; ++i) { const float u = (float)i/(float)p->ndiv; const float v = p->vmin + (p->vmax-p->vmin)*u; snprintf(text, 64, "%.2f %s", v, p->units); const float fy = oy + v*sy; imguiDrawText(p->x + p->w - p->pad, (int)fy-4, IMGUI_ALIGN_RIGHT, text, imguiRGBA(0,0,0,255)); 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)); } }
int main( int argc, char **argv ) { int width = 1024, height=768; // Initialise GLFW if( !glfwInit() ) { fprintf( stderr, "Failed to initialize GLFW\n" ); exit( EXIT_FAILURE ); } // Open a window and create its OpenGL context if( !glfwOpenWindow( width, height, 0,0,0,0, 24,0, GLFW_WINDOW ) ) { fprintf( stderr, "Failed to open GLFW window\n" ); glfwTerminate(); exit( EXIT_FAILURE ); } glfwSetWindowTitle( "imgui sample imguiRenderGL2" ); GLenum err = glewInit(); if (GLEW_OK != err) { /* Problem: glewInit failed, something is seriously wrong. */ fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); exit( EXIT_FAILURE ); } // Ensure we can capture the escape key being pressed below glfwEnable( GLFW_STICKY_KEYS ); // Enable vertical sync (on cards that support it) glfwSwapInterval( 1 ); // Init UI if (!imguiRenderGLInit("DroidSans.ttf")) { fprintf(stderr, "Could not init GUI renderer.\n"); exit(EXIT_FAILURE); } glClearColor(0.8f, 0.8f, 0.8f, 1.f); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); // imgui states bool checked1 = false; bool checked2 = false; bool checked3 = true; bool checked4 = false; float value1 = 50.f; float value2 = 30.f; int scrollarea1 = 0; int scrollarea2 = 0; // glfw scrolling int glfwscroll = 0; do { glfwGetWindowSize(&width, &height); glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Mouse states unsigned char mousebutton = 0; int currentglfwscroll = glfwGetMouseWheel(); int mscroll = 0; if (currentglfwscroll < glfwscroll) mscroll = 2; if (currentglfwscroll > glfwscroll) mscroll = -2; glfwscroll = currentglfwscroll; int mousex; int mousey; glfwGetMousePos(&mousex, &mousey); mousey = height - mousey; int leftButton = glfwGetMouseButton( GLFW_MOUSE_BUTTON_LEFT ); int rightButton = glfwGetMouseButton( GLFW_MOUSE_BUTTON_RIGHT ); int middleButton = glfwGetMouseButton( GLFW_MOUSE_BUTTON_MIDDLE ); int toggle = 0; if( leftButton == GLFW_PRESS ) mousebutton |= IMGUI_MBUT_LEFT; // Draw UI glMatrixMode(GL_PROJECTION); glLoadIdentity(); float projection[16] = { 2.f/width, 0.f, 0.f, 0.f, 0.f, 2.f/height, 0.f, 0.f, 0.f, 0.f, -2.f, 0.f, -1.f, -1.f, -1.f, 1.f }; glLoadMatrixf(projection); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glUseProgram(0); imguiBeginFrame(mousex, mousey, mousebutton, mscroll); imguiBeginScrollArea("Scroll area", 10, 10, width / 5, height - 20, &scrollarea1); imguiSeparatorLine(); imguiSeparator(); imguiButton("Button"); imguiButton("Disabled button", false); imguiItem("Item"); imguiItem("Disabled item", false); toggle = imguiCheck("Checkbox", checked1); if (toggle) checked1 = !checked1; toggle = imguiCheck("Disabled checkbox", checked2, false); if (toggle) checked2 = !checked2; toggle = imguiCollapse("Collapse", "subtext", checked3); if (checked3) { imguiIndent(); imguiLabel("Collapsible element"); imguiUnindent(); } if (toggle) checked3 = !checked3; toggle = imguiCollapse("Disabled collapse", "subtext", checked4, false); if (toggle) checked4 = !checked4; imguiLabel("Label"); imguiValue("Value"); imguiSlider("Slider", &value1, 0.f, 100.f, 1.f); imguiSlider("Disabled slider", &value2, 0.f, 100.f, 1.f, false); imguiIndent(); imguiLabel("Indented"); imguiUnindent(); imguiLabel("Unindented"); imguiEndScrollArea(); imguiBeginScrollArea("Scroll area", 20 + width / 5, 500, width / 5, height - 510, &scrollarea2); imguiSeparatorLine(); imguiSeparator(); for (int i = 0; i < 100; ++i) imguiLabel("A wall of text"); imguiEndScrollArea(); imguiEndFrame(); imguiDrawText(30 + width / 5 * 2, height - 20, IMGUI_ALIGN_LEFT, "Free text", imguiRGBA(32,192, 32,192)); imguiDrawText(30 + width / 5 * 2 + 100, height - 40, IMGUI_ALIGN_RIGHT, "Free text", imguiRGBA(32, 32, 192, 192)); imguiDrawText(30 + width / 5 * 2 + 50, height - 60, IMGUI_ALIGN_CENTER, "Free text", imguiRGBA(192, 32, 32,192)); imguiDrawLine(30 + width / 5 * 2, height - 80, 30 + width / 5 * 2 + 100, height - 60, 1.f, imguiRGBA(32,192, 32,192)); imguiDrawLine(30 + width / 5 * 2, height - 100, 30 + width / 5 * 2 + 100, height - 80, 2.f, imguiRGBA(32, 32, 192, 192)); imguiDrawLine(30 + width / 5 * 2, height - 120, 30 + width / 5 * 2 + 100, height - 100, 3.f, imguiRGBA(192, 32, 32,192)); imguiDrawRoundedRect(30 + width / 5 * 2, height - 240, 100, 100, 5.f, imguiRGBA(32,192, 32,192)); imguiDrawRoundedRect(30 + width / 5 * 2, height - 350, 100, 100, 10.f, imguiRGBA(32, 32, 192, 192)); imguiDrawRoundedRect(30 + width / 5 * 2, height - 470, 100, 100, 20.f, imguiRGBA(192, 32, 32,192)); imguiDrawRect(30 + width / 5 * 2, height - 590, 100, 100, imguiRGBA(32, 192, 32, 192)); imguiDrawRect(30 + width / 5 * 2, height - 710, 100, 100, imguiRGBA(32, 32, 192, 192)); imguiDrawRect(30 + width / 5 * 2, height - 830, 100, 100, imguiRGBA(192, 32, 32,192)); imguiRenderGLDraw(width, height); // Check for errors GLenum err = glGetError(); if(err != GL_NO_ERROR) { fprintf(stderr, "OpenGL Error : %s\n", gluErrorString(err)); } // Swap buffers glfwSwapBuffers(); } // Check if the ESC key was pressed or the window was closed while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS && glfwGetWindowParam( GLFW_OPENED ) ); // Clean UI imguiRenderGLDestroy(); // Close OpenGL window and terminate GLFW glfwTerminate(); exit( EXIT_SUCCESS ); }