| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "ui/ui_main.h" |
| |
|
| | #include <math.h> |
| | #include <stdio.h> |
| | #include <string.h> |
| |
|
| | #include <mujoco/mjmacro.h> |
| | #include <mujoco/mujoco.h> |
| | #include "engine/engine_array_safety.h" |
| | #include "render/glad/glad.h" |
| |
|
| | |
| |
|
| | |
| | static const mjuiThemeSpacing themeSpacing0 = { |
| | 270, |
| | 15, |
| | 120, |
| | 8, |
| | 6, |
| | 6, |
| | 4, |
| | 4, |
| | 4, |
| | 8, |
| | 4, |
| | 30, |
| | 4 |
| | }; |
| |
|
| |
|
| | |
| | static const mjuiThemeSpacing themeSpacing1 = { |
| | 310, |
| | 15, |
| | 120, |
| | 10, |
| | 10, |
| | 10, |
| | 7, |
| | 7, |
| | 7, |
| | 10, |
| | 5, |
| | 30, |
| | 4 |
| | }; |
| |
|
| |
|
| | |
| | static const mjuiThemeColor themeColor0 = { |
| | {0.25, 0.25, 0.25}, |
| | {0.12, 0.12, 0.12}, |
| | {0.6, 0.2, 0.2}, |
| | {0.1, 0.1, 0.1}, |
| | {0.45, 0.17, 0.17}, |
| | {0.45, 0.17, 0.17}, |
| | {0.45, 0.17, 0.17}, |
| | {0.45, 0.17, 0.17}, |
| | {1.0, 1.0, 1.0}, |
| | {0.7, 0.7, 0.7}, |
| | {0.1, 0.1, 0.1}, |
| | {0.25, 0.25, 0.25}, |
| | {0.1, 0.1, 0.1}, |
| | {0.0, 0.0, 1.0}, |
| | {1.0, 1.0, 1.0}, |
| | {0.5, 0.5, 0.5}, |
| | {0.3, 0.3, 0.3}, |
| | {0.4, 0.4, 0.4}, |
| | {0.6, 0.4, 0.4}, |
| | {0.4, 0.4, 0.7}, |
| | {0.4, 0.6, 0.4}, |
| | {0.4, 0.6, 0.6}, |
| | {0.2, 0.3, 0.3}, |
| | {0.3, 0.2, 0.3}, |
| | {0.6, 0.4, 0.6}, |
| | {0.6, 0.6, 0.4}, |
| | {0.7, 0.0, 0.0}, |
| | {0.9, 0.9, 0.9} |
| | }; |
| |
|
| |
|
| | |
| | static const mjuiThemeColor themeColor1 = { |
| | {0.2, 0.2, 0.2}, |
| | {0.12, 0.12, 0.12}, |
| | {0.3, 0.3, 0.3}, |
| | {0.15, 0.15, 0.15}, |
| | {0.25, 0.25, 0.25}, |
| | {0.25, 0.25, 0.25}, |
| | {0.25, 0.25, 0.25}, |
| | {0.25, 0.25, 0.25}, |
| | {0.8, 0.8, 0.8}, |
| | {0.7, 0.7, 0.7}, |
| | {0.15, 0.15, 0.15}, |
| | {0.2, 0.2, 0.2}, |
| | {0.15, 0.15, 0.15}, |
| | {0.0, 0.0, 1.0}, |
| | {0.9, 0.9, 0.9}, |
| | {0.5, 0.5, 0.5}, |
| | {0.2, 0.2, 0.2}, |
| | {0.25, 0.25, 0.25}, |
| | {0.6, 0.4, 0.2}, |
| | {0.6, 0.4, 0.2}, |
| | {0.6, 0.4, 0.2}, |
| | {0.6, 0.4, 0.2}, |
| | {0.3, 0.2, 0.1}, |
| | {0.2, 0.2, 0.2}, |
| | {0.6, 0.4, 0.2}, |
| | {0.6, 0.4, 0.2}, |
| | {0.7, 0.0, 0.0}, |
| | {0.9, 0.9, 0.9} |
| | }; |
| |
|
| |
|
| | |
| | static const mjuiThemeColor themeColor2 = { |
| | {0.9, 0.9, 0.9}, |
| | {0.7, 0.7, 0.7}, |
| | {0.8, 0.8, 0.8}, |
| | {1.0, 1.0, 1.0}, |
| | {0.95, 0.95, 0.95}, |
| | {0.95, 0.95, 0.95}, |
| | {0.95, 0.95, 0.95}, |
| | {0.95, 0.95, 0.95}, |
| | {0.0, 0.0, 0.8}, |
| | {0.0, 0.0, 0.8}, |
| | {1.0, 1.0, 1.0}, |
| | {0.9, 0.9, 0.9}, |
| | {1.0, 1.0, 1.0}, |
| | {0.0, 1.0, 1.0}, |
| | {0.0, 0.0, 0.0}, |
| | {0.7, 0.7, 0.7}, |
| | {0.95, 0.95, 0.95}, |
| | {0.9, 0.9, 0.9}, |
| | {0.8, 0.8, 0.8}, |
| | {0.8, 0.8, 0.8}, |
| | {0.8, 0.8, 0.8}, |
| | {0.8, 0.8, 0.8}, |
| | {0.9, 0.9, 0.9}, |
| | {0.95, 0.95, 0.95}, |
| | {0.8, 0.8, 0.8}, |
| | {0.8, 0.8, 0.8}, |
| | {1.0, 0.3, 0.3}, |
| | {0.2, 0.2, 0.2} |
| | }; |
| |
|
| |
|
| | |
| | static const mjuiThemeColor themeColor3 = { |
| | {0.15, 0.15, 0.15}, |
| | {0.3, 0.3, 0.3}, |
| | {0.25, 0.25, 0.25}, |
| | {0.0, 0.0, 0.0}, |
| | {0.2, 0.2, 0.2}, |
| | {0.2, 0.2, 0.2}, |
| | {0.2, 0.2, 0.2}, |
| | {0.2, 0.2, 0.2}, |
| | {1.0, 0.3, 0.3}, |
| | {1.0, 0.3, 0.3}, |
| | {0.0, 0.0, 0.0}, |
| | {0.15, 0.15, 0.15}, |
| | {0.0, 0.0, 0.0}, |
| | {0.0, 0.0, 1.0}, |
| | {1.0, 1.0, 1.0}, |
| | {0.4, 0.4, 0.4}, |
| | {0.1, 0.1, 0.1}, |
| | {0.15, 0.15, 0.15}, |
| | {0.3, 0.3, 0.3}, |
| | {0.3, 0.3, 0.3}, |
| | {0.3, 0.3, 0.3}, |
| | {0.3, 0.3, 0.3}, |
| | {0.15, 0.15, 0.15}, |
| | {0.15, 0.15, 0.15}, |
| | {0.3, 0.3, 0.3}, |
| | {0.3, 0.3, 0.3}, |
| | {0.8, 0.2, 0.2}, |
| | {0.8, 0.8, 0.8} |
| | }; |
| |
|
| |
|
| |
|
| | |
| |
|
| | |
| | static int SCL(int sz, const mjrContext* con) { |
| | return mjMAX(0, mju_round(sz * 0.01 * con->fontScale)); |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void initOpenGL(const mjrRect* r, const mjrContext* con) { |
| | |
| | glDisable(GL_NORMALIZE); |
| | glDisable(GL_DEPTH_TEST); |
| | glDisable(GL_CULL_FACE); |
| | glDisable(GL_LIGHTING); |
| | glDisable(GL_COLOR_MATERIAL); |
| | glDisable(GL_BLEND); |
| | glShadeModel(GL_SMOOTH); |
| | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| |
|
| | |
| | glMatrixMode(GL_PROJECTION); |
| | glLoadIdentity(); |
| | glOrtho(0, r->width, 0, r->height, -1, 1); |
| | glMatrixMode(GL_MODELVIEW); |
| | glLoadIdentity(); |
| |
|
| | |
| | glViewport(r->left, r->bottom, r->width, r->height); |
| | } |
| |
|
| |
|
| |
|
| | |
| | static int textwidth(const char* text, const mjrContext* con, int limit) { |
| | int i = 0, width = 0; |
| |
|
| | |
| | if (limit == 0) { |
| | return 0; |
| | } |
| |
|
| | |
| | while (text[i] && (limit < 1 || i < limit)) { |
| | width += con->charWidth[(unsigned char)text[i++]]; |
| | } |
| |
|
| | return width; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void drawtext(const char* txt, int x, int y, int maxwidth, |
| | const float* rgb, const mjrContext* con) { |
| | |
| | int len = 0, width = 0; |
| | while (txt[len]) { |
| | width += con->charWidth[(unsigned char)txt[len]]; |
| | if (width >= maxwidth) { |
| | break; |
| | } else { |
| | len++; |
| | } |
| | } |
| |
|
| | |
| | glListBase(con->baseFontNormal); |
| | glColor3fv(rgb); |
| | glRasterPos3i(x, y, 0); |
| | glCallLists(len, GL_UNSIGNED_BYTE, txt); |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void drawtextrect(mjrRect rect, const char* txt, |
| | const float* rgb, const mjrContext* con) { |
| | int dy = (rect.height - con->charHeight)/2; |
| | int dx = (rect.width - textwidth(txt, con, -1))/2; |
| | dx = mjMAX(0, dx); |
| |
|
| | drawtext(txt, rect.left+dx, rect.bottom+dy, rect.width-dx, rgb, con); |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void drawrectangle(mjrRect rect, const float* rgb, const float* rgbback, |
| | const mjrContext* con) { |
| | |
| | glColor3fv(rgb); |
| | glBegin(GL_QUADS); |
| | glVertex2i(rect.left, rect.bottom); |
| | glVertex2i(rect.left+rect.width, rect.bottom); |
| | glVertex2i(rect.left+rect.width, rect.bottom+rect.height); |
| | glVertex2i(rect.left, rect.bottom+rect.height); |
| | glEnd(); |
| |
|
| | |
| | if (rgbback) { |
| | int margin = SCL(2, con); |
| | glColor3fv(rgbback); |
| | glBegin(GL_QUADS); |
| | glVertex2i(rect.left+margin, rect.bottom+margin); |
| | glVertex2i(rect.left+rect.width-margin, rect.bottom+margin); |
| | glVertex2i(rect.left+rect.width-margin, rect.bottom+rect.height-margin); |
| | glVertex2i(rect.left+margin, rect.bottom+rect.height-margin); |
| | glEnd(); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void roundcorner(mjrRect rect, int flg_skipbottom, int flg_separator, |
| | const mjUI* ui, const mjrContext* con) { |
| | |
| | int cornerspec = flg_separator ? ui->spacing.cornersep : ui->spacing.cornersect; |
| | if (cornerspec == 0) { |
| | return; |
| | } |
| |
|
| | |
| | int ndivide = 10; |
| | double radius = cornerspec * 0.01 * con->fontScale; |
| |
|
| | |
| | for (int ic = (flg_skipbottom ? 2 : 0); ic < 4; ++ic) { |
| | |
| | double corner[2]; |
| | switch (ic) { |
| | case 0: |
| | corner[0] = rect.left; |
| | corner[1] = rect.bottom; |
| | break; |
| |
|
| | case 1: |
| | corner[0] = rect.left + rect.width; |
| | corner[1] = rect.bottom; |
| | break; |
| |
|
| | case 2: |
| | corner[0] = rect.left + rect.width; |
| | corner[1] = rect.bottom + rect.height; |
| | break; |
| |
|
| | default: |
| | corner[0] = rect.left; |
| | corner[1] = rect.bottom + rect.height; |
| | } |
| |
|
| | |
| | double angle = ic * 0.5 * mjPI; |
| |
|
| | |
| | double center[2]; |
| | center[0] = corner[0] + mju_sqrt(2.0) * radius * cos(angle + 0.25 * mjPI); |
| | center[1] = corner[1] + mju_sqrt(2.0) * radius * sin(angle + 0.25 * mjPI); |
| |
|
| | |
| | glColor3fv(flg_separator ? ui->color.sectpane : ui->color.master); |
| | glBegin(GL_TRIANGLE_FAN); |
| | glVertex2d(corner[0], corner[1]); |
| |
|
| | |
| | for (int i = 0; i <= ndivide; i++) { |
| | double a = angle + mjPI + 0.5 * mjPI * (double)i / (double)ndivide; |
| | glVertex2d(center[0] + radius * cos(a), center[1] + radius * sin(a)); |
| | } |
| | glEnd(); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void drawoval(mjrRect rect, const float* rgb, const float* rgbback, |
| | const mjrContext* con) { |
| | const int ndivide = 15; |
| |
|
| | |
| | if (rect.height > rect.width) { |
| | return; |
| | } |
| |
|
| | |
| | double radius = 0.5*rect.height; |
| | double lcenter[2] = {rect.left + radius, rect.bottom + radius}; |
| | double rcenter[2] = {rect.left + rect.width - radius, rect.bottom + radius}; |
| |
|
| | |
| | glColor3fv(rgb); |
| | glBegin(GL_POLYGON); |
| |
|
| | |
| | for (int i=0; i <= ndivide; i++) { |
| | double angle = mjPI * (0.5 + (double)i / (double)ndivide); |
| | glVertex2d(lcenter[0] + radius*cos(angle), lcenter[1] + radius*sin(angle)); |
| | } |
| |
|
| | |
| | for (int i=0; i <= ndivide; i++) { |
| | double angle = mjPI * (1.5 + (double)i / (double)ndivide); |
| | glVertex2d(rcenter[0] + radius*cos(angle), rcenter[1] + radius*sin(angle)); |
| | } |
| | glEnd(); |
| |
|
| | |
| | if (rgbback) { |
| | int margin = SCL(2, con); |
| | radius -= margin; |
| |
|
| | glColor3fv(rgbback); |
| | glBegin(GL_POLYGON); |
| |
|
| | |
| | for (int i=0; i <= ndivide; i++) { |
| | double angle = mjPI * (0.5 + (double)i / (double)ndivide); |
| | glVertex2d(lcenter[0] + radius*cos(angle), lcenter[1] + radius*sin(angle)); |
| | } |
| |
|
| | |
| | for (int i=0; i <= ndivide; i++) { |
| | double angle = mjPI * (1.5 + (double)i / (double)ndivide); |
| | glVertex2d(rcenter[0] + radius*cos(angle), rcenter[1] + radius*sin(angle)); |
| | } |
| | glEnd(); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | static void drawsymbol(mjrRect rect, int flg_open, int type, |
| | const mjUI* ui, const mjrContext* con) { |
| | |
| | int texthor = SCL(ui->spacing.texthor, con); |
| | int cx = rect.left + rect.width - texthor; |
| | int cy = rect.bottom + rect.height/2; |
| | int d = mju_round(con->charHeight*0.33); |
| |
|
| | |
| | if (type == 3) { |
| | d = mju_round(con->charHeight*0.28); |
| | } |
| |
|
| | |
| | if (flg_open) { |
| | glColor3fv(ui->color.sectsymbol); |
| | glBegin(GL_TRIANGLES); |
| | glVertex2i(cx, cy+d); |
| | glVertex2i(cx-2*d, cy+d); |
| | glVertex2i(cx-d, cy-d); |
| | glEnd(); |
| | } |
| |
|
| | |
| | else { |
| | |
| | glColor3fv(ui->color.sectsymbol); |
| | glBegin(GL_TRIANGLES); |
| | glVertex2i(cx, cy-d); |
| | glVertex2i(cx, cy+d); |
| | glVertex2i(cx-2*d, cy); |
| | glEnd(); |
| |
|
| | |
| | switch (type) { |
| | case 0: |
| | glColor3f( |
| | (ui->color.secttitle[0] + ui->color.secttitle2[0]) * 0.5, |
| | (ui->color.secttitle[1] + ui->color.secttitle2[1]) * 0.5, |
| | (ui->color.secttitle[2] + ui->color.secttitle2[2]) * 0.5 |
| | ); |
| | break; |
| |
|
| | case 1: |
| | glColor3f( |
| | (ui->color.secttitleuncheck[0] + ui->color.secttitleuncheck2[0]) * 0.5, |
| | (ui->color.secttitleuncheck[1] + ui->color.secttitleuncheck2[1]) * 0.5, |
| | (ui->color.secttitleuncheck[2] + ui->color.secttitleuncheck2[2]) * 0.5 |
| | ); |
| | break; |
| |
|
| | case 2: |
| | glColor3f( |
| | (ui->color.secttitlecheck[0] + ui->color.secttitlecheck2[0]) * 0.5, |
| | (ui->color.secttitlecheck[1] + ui->color.secttitlecheck2[1]) * 0.5, |
| | (ui->color.secttitlecheck[2] + ui->color.secttitlecheck2[2]) * 0.5 |
| | ); |
| | break; |
| |
|
| | case 3: |
| | glColor3f( |
| | (ui->color.separator[0] + ui->color.separator2[0]) * 0.5, |
| | (ui->color.separator[1] + ui->color.separator2[1]) * 0.5, |
| | (ui->color.separator[2] + ui->color.separator2[2]) * 0.5 |
| | ); |
| | } |
| |
|
| | |
| | double margin = con->fontScale * 0.015; |
| | double u = 0.5 * sqrt(5.0) * margin; |
| | double y = d - u - 0.5 * margin; |
| | glBegin(GL_TRIANGLES); |
| | glVertex2d(cx-margin, cy-y); |
| | glVertex2d(cx-margin, cy+y); |
| | glVertex2d(cx-2*d+2*u, cy); |
| | glEnd(); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static mjrRect radioelement(const mjuiItem* it, int n, |
| | const mjUI* ui, const mjrContext* con) { |
| | |
| | int g_itemmid = SCL(ui->spacing.itemmid, con); |
| | int g_textver = SCL(ui->spacing.textver, con); |
| | int ncol = ui->radiocol ? ui->radiocol : 2; |
| | int nrow = (it->multi.nelem-1)/ncol + 1; |
| |
|
| | |
| | int cellwidth = (it->rect.width-(ncol-1)*g_itemmid)/ncol; |
| | int cellheight = con->charHeight+2*g_textver; |
| | int row = n/ncol; |
| | int col = n%ncol; |
| |
|
| | |
| | mjrRect r = { |
| | it->rect.left + col*(cellwidth+g_itemmid), |
| | it->rect.bottom+(nrow-1-row)*cellheight, |
| | cellwidth, |
| | cellheight |
| | }; |
| |
|
| | |
| | if (col == ncol-1) { |
| | r.width = it->rect.width - r.left + it->rect.left; |
| | } |
| |
|
| | return r; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void mouseinui(const mjUI* ui, const mjuiState* ins, int* x, int* y) { |
| | |
| | *x = (int)ins->x; |
| | *y = (int)ins->y; |
| | mjrRect rect = ins->rect[ui->rectid]; |
| |
|
| | |
| | if (ui->height > rect.height) { |
| | *y -= ui->scroll; |
| | } |
| |
|
| | |
| | *x -= rect.left; |
| | *y -= (rect.height - ui->height) + rect.bottom; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void mouseinrect(mjrRect rect, const mjUI* ui, const mjuiState* ins, |
| | double* rx, double* ry) { |
| | |
| | int x, y; |
| | mouseinui(ui, ins, &x, &y); |
| |
|
| | |
| | *rx = (x-rect.left) / (double)rect.width; |
| | *ry = (y-rect.bottom) / (double)rect.height; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static int findradio(const mjuiItem* it, const mjUI* ui, |
| | const mjuiState* ins, const mjrContext* con) { |
| | |
| | int ncol = ui->radiocol ? ui->radiocol : 2; |
| | int nrow = (it->multi.nelem-1)/ncol + 1; |
| |
|
| | |
| | double rx, ry; |
| | mouseinrect(it->rect, ui, ins, &rx, &ry); |
| |
|
| | |
| | ry = 1-ry; |
| |
|
| | |
| | int row = (int)floor(ry*nrow); |
| | int col = (int)floor(rx*ncol); |
| | row = mjMAX(0, mjMIN(row, nrow-1)); |
| | col = mjMAX(0, mjMIN(col, ncol-1)); |
| |
|
| | |
| | int ind = row*ncol + col; |
| | if (ind < it->multi.nelem) { |
| | return ind; |
| | } else { |
| | return -1; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void makeradioline(const mjuiItem* it, const mjrContext* con, int* sep) { |
| | int nelem = it->multi.nelem, totwid = 0; |
| | int elwid[mjMAXUIMULTI]; |
| |
|
| | |
| | if (!nelem) { |
| | return; |
| | } |
| |
|
| | |
| | for (int i=0; i < nelem; i++) { |
| | elwid[i] = textwidth(it->multi.name[i], con, -1); |
| | totwid += elwid[i]; |
| | } |
| |
|
| | |
| | double extra = ((double)(it->rect.width - totwid)) / ((double)nelem); |
| |
|
| | |
| | sep[0] = 0; |
| | for (int i=0; i < nelem; i++) { |
| | sep[i+1] = sep[i] + elwid[i] + mju_round((i+1)*extra) -mju_round(i*extra); |
| | } |
| | sep[nelem] = it->rect.width; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static int findradioline(const mjuiItem* it, const mjUI* ui, |
| | const mjuiState* ins, const mjrContext* con) { |
| | |
| | int sep[mjMAXUIMULTI+1]; |
| | makeradioline(it, con, sep); |
| |
|
| | |
| | double rx, ry; |
| | mouseinrect(it->rect, ui, ins, &rx, &ry); |
| |
|
| | |
| | int x = mju_round(rx*it->rect.width); |
| | for (int i=0; i < it->multi.nelem; i++) { |
| | if (x >= sep[i] && x < sep[i+1]) { |
| | return i; |
| | } |
| | } |
| |
|
| | |
| | return -1; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static int findselect(const mjuiItem* it, const mjUI* ui, |
| | const mjuiState* ins, const mjrContext* con) { |
| | |
| | if (!(ui->mousesect > 0 && ui->mouseitem >= 0 && it && it->type == mjITEM_SELECT)) { |
| | return -1; |
| | } |
| |
|
| | |
| | int g_textver = SCL(ui->spacing.textver, con); |
| | int cellheight = con->charHeight + 2*g_textver; |
| | mjrRect r = it->rect; |
| | r.height = it->multi.nelem * cellheight; |
| | r.bottom -= r.height; |
| |
|
| | |
| | double rx, ry; |
| | mouseinrect(r, ui, ins, &rx, &ry); |
| | if (ry > 0 && ry < 1 && rx > 0 && rx < 1) { |
| | int k = (int)floor(ry*it->multi.nelem); |
| | k = mjMAX(0, mjMIN(it->multi.nelem-1, k)); |
| | return it->multi.nelem-1-k; |
| | } else { |
| | return -1; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | void scrollrect(mjrRect rect, const mjUI* ui, const mjrContext* con, |
| | mjrRect* bar, mjrRect* thumb) { |
| | int w_scroll = SCL(ui->spacing.scroll, con); |
| |
|
| | |
| | *bar = rect; |
| | bar->left = rect.left+rect.width-w_scroll; |
| | bar->width = w_scroll; |
| |
|
| | |
| | double tstart = (double)ui->scroll / (double)ui->height; |
| | double tend = (double)(ui->scroll+rect.height) / (double)ui->height; |
| | *thumb = *bar; |
| | thumb->bottom = rect.bottom + mju_round(rect.height*(1-tend)); |
| | thumb->height = mju_round(rect.height*(tend-tstart)); |
| | } |
| |
|
| |
|
| |
|
| | |
| | static int inside(int x, int y, mjrRect r) { |
| | return (x >= r.left && x <= r.left+r.width && y >= r.bottom && y <= r.bottom+r.height); |
| | } |
| |
|
| |
|
| |
|
| | |
| | static int insideoval(int x, int y, mjrRect r) { |
| | |
| | if (!inside(x, y, r)) { |
| | return 0; |
| | } |
| |
|
| | |
| | int radius = r.height/2; |
| | if (x >= r.left+radius && x <= r.left+r.width-radius) { |
| | return 1; |
| | } |
| |
|
| | |
| | int dx = x - (r.left+radius); |
| | int dy = y - (r.bottom+radius); |
| | if (dx < 0 && (dx*dx + dy*dy < radius*radius)) { |
| | return 1; |
| | } |
| |
|
| | |
| | dx = x - (r.left+r.width-radius); |
| | if (dx > 0 && (dx*dx + dy*dy < radius*radius)) { |
| | return 1; |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | |
| | |
| | static void findmouse(const mjUI* ui, const mjuiState* ins, const mjrContext* con, |
| | int* sect, int* item) { |
| | |
| | *sect = 0; |
| | *item = 0; |
| |
|
| | |
| | int x = (int)ins->x; |
| | int y = (int)ins->y; |
| | mjrRect rect = ins->rect[ui->rectid]; |
| |
|
| | |
| | if (ui->height > rect.height) { |
| | |
| | mjrRect bar; |
| | mjrRect thumb; |
| | scrollrect(rect, ui, con, &bar, &thumb); |
| |
|
| | |
| | if (inside(x, y, bar)) { |
| | |
| | if (inside(x, y, thumb)) { |
| | *sect = -1; |
| | } |
| |
|
| | |
| | else if (y < thumb.bottom) { |
| | *sect = -2; |
| | } |
| |
|
| | |
| | else { |
| | *sect = -3; |
| | } |
| |
|
| | |
| | *item = -1; |
| | return; |
| | } |
| | } |
| |
|
| | |
| | mouseinui(ui, ins, &x, &y); |
| |
|
| | |
| | for (int n=0; n < ui->nsect; n++) { |
| | |
| | const mjuiSection* s = ui->sect + n; |
| |
|
| | |
| | if (s->state < 2 && inside(x, y, s->rtitle)) { |
| | *sect = n+1; |
| | *item = -1; |
| |
|
| | |
| | if (s->checkbox > 0) { |
| | mjrRect rcheck = s->rtitle; |
| | rcheck.width = mjMIN(rcheck.height, rcheck.width); |
| | if (inside(x, y, rcheck)) { |
| | *item = -2; |
| | } |
| | } |
| |
|
| | return; |
| | } |
| |
|
| | |
| | if (s->state && inside(x, y, s->rcontent)) { |
| | for (int i=0; i < s->nitem; i++) { |
| | if (s->item[i].type == mjITEM_BUTTON ? |
| | insideoval(x, y, s->item[i].rect) : |
| | inside(x, y, s->item[i].rect)) { |
| | *sect = n+1; |
| | *item = i; |
| | return; |
| | } |
| | } |
| | } |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void setslider(mjuiItem* it, mjUI* ui, |
| | const mjuiState* ins, const mjrContext* con) { |
| | |
| | double rx, ry; |
| | mouseinrect(it->rect, ui, ins, &rx, &ry); |
| |
|
| | |
| | rx = mjMAX(0, mjMIN(1, rx)); |
| | rx = mju_round(rx * it->slider.divisions) / mjMAX(1, it->slider.divisions); |
| | rx = mjMAX(0, mjMIN(1, rx)); |
| |
|
| | |
| | mjtNum val = (mjtNum)(it->slider.range[0]*(1-rx) + it->slider.range[1]*rx); |
| |
|
| | |
| | if (it->type == mjITEM_SLIDERINT) { |
| | *(int*)it->pdata = mju_round(val); |
| | } else { |
| | *(mjtNum*)it->pdata = val; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | static int checkedit(const char* text, const mjuiItem* it) { |
| | |
| | if (it->type == mjITEM_EDITTXT) { |
| | return 0; |
| | } |
| |
|
| | |
| | if (it->type != mjITEM_EDITINT && |
| | it->type != mjITEM_EDITNUM && |
| | it->type != mjITEM_EDITFLOAT) { |
| | mju_error("Internal error: expected edit control"); |
| | } |
| |
|
| | |
| | double val[mjMAXUIEDIT]; |
| | int n = sscanf(text, "%lf %lf %lf %lf %lf %lf %lf", |
| | val, val+1, val+2, val+3, val+4, val+5, val+6); |
| |
|
| | |
| | if (n != it->edit.nelem) { |
| | return 1; |
| | } |
| |
|
| | |
| | for (int i=0; i < n; i++) |
| | if (it->edit.range[i][0] < it->edit.range[i][1] && |
| | (val[i] < it->edit.range[i][0] || val[i] > it->edit.range[i][1])) { |
| | return 2; |
| | } |
| |
|
| | |
| | if (it->type == mjITEM_EDITINT) { |
| | for (int i=0; i < n; i++) { |
| | if (val[i] != (double)((int)val[i])) { |
| | return 3; |
| | } |
| | } |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| |
|
| |
|
| | |
| | |
| | static int text2array(const char* text, const mjuiItem* it) { |
| | |
| | if (it->type == mjITEM_EDITTXT) { |
| | |
| | char* pdata = (char*)it->pdata; |
| | strncpy(pdata, text, mjMAXUINAME); |
| | pdata[mjMAXUINAME-1] = 0; |
| | return 0; |
| | } |
| |
|
| | |
| | if (it->type != mjITEM_EDITINT && it->type != mjITEM_EDITNUM && it->type != mjITEM_EDITFLOAT) { |
| | mju_error("Internal error: expected edit control"); |
| | } |
| |
|
| | |
| | double val[mjMAXUIEDIT]; |
| | int n = sscanf(text, "%lf %lf %lf %lf %lf %lf %lf", |
| | val, val+1, val+2, val+3, val+4, val+5, val+6); |
| |
|
| | |
| | if (n != it->edit.nelem) { |
| | return 1; |
| | } |
| |
|
| | |
| | for (int i=0; i < n; i++) { |
| | if (it->edit.range[i][0] < it->edit.range[i][1] && |
| | (val[i] < it->edit.range[i][0] || val[i] > it->edit.range[i][1])) { |
| | return 2; |
| | } |
| | } |
| |
|
| | |
| | if (it->type == mjITEM_EDITINT) { |
| | for (int i=0; i < n; i++) { |
| | if (val[i] != (double)((int)val[i])) { |
| | return 3; |
| | } |
| | } |
| | } |
| |
|
| | |
| | if (it->type == mjITEM_EDITINT) { |
| | int* pdata = (int*)it->pdata; |
| | for (int i=0; i < n; i++) { |
| | pdata[i] = (int)val[i]; |
| | } |
| | } else { |
| | if (it->type == mjITEM_EDITNUM) { |
| | mjtNum* pdata = (mjtNum*)it->pdata; |
| | for (int i=0; i < n; i++) { |
| | pdata[i] = (mjtNum)val[i]; |
| | } |
| | } else { |
| | float* pdata = (float*)it->pdata; |
| | for (int i=0; i < n; i++) { |
| | pdata[i] = (float)val[i]; |
| | } |
| | } |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void array2text(char* text, const mjuiItem* it) { |
| | |
| | if (it->type == mjITEM_EDITTXT) { |
| | |
| | strncpy(text, (const char*)it->pdata, mjMAXUINAME); |
| | text[mjMAXUINAME-1] = 0; |
| | return; |
| | } |
| |
|
| | |
| | if (it->type != mjITEM_EDITINT && it->type != mjITEM_EDITNUM && it->type != mjITEM_EDITFLOAT) { |
| | mju_error("Internal error: expected edit control"); |
| | } |
| |
|
| | |
| | int n = it->edit.nelem; |
| | char buf[50]; |
| | text[0] = 0; |
| | for (int i=0; i < n; i++) { |
| | if (it->type == mjITEM_EDITINT) { |
| | mjSNPRINTF(buf, "%d", ((int*)it->pdata)[i]); |
| | } else if (it->type == mjITEM_EDITNUM) { |
| | mjSNPRINTF(buf, "%.4g", ((mjtNum*)it->pdata)[i]); |
| | } else { |
| | mjSNPRINTF(buf, "%.4g", ((float*)it->pdata)[i]); |
| | } |
| | strncat(text, buf, mjMAXUITEXT - strlen(text) - 1); |
| | if (i < n-1) { |
| | strncat(text, " ", mjMAXUITEXT - strlen(text) - 1); |
| | } |
| | } |
| | } |
| |
|
| |
|
| | |
| | static int validkey(int key, int sz, int type, const mjuiState* state) { |
| | |
| | if (type == mjITEM_EDITTXT) { |
| | if (sz < mjMAXUINAME-1 && key >= 32 && key <= 127) { |
| | |
| | if (key >= 'A' && key <= 'Z' && !state->shift) { |
| | key = key + 'a' - 'A'; |
| | } |
| |
|
| | |
| | else if (state->shift) { |
| | if (key == '`') { |
| | key = '~'; |
| | } else if (key == '-') { |
| | key = '_'; |
| | } else if (key == '=') { |
| | key = '+'; |
| | } else if (key == '[') { |
| | key = '{'; |
| | } else if (key == ']') { |
| | key = '}'; |
| | } else if (key == '\\') { |
| | key = '|'; |
| | } else if (key == ';') { |
| | key = ':'; |
| | } else if (key == '\'') { |
| | key = '"'; |
| | } else if (key == ',') { |
| | key = '<'; |
| | } else if (key == '.') { |
| | key = '>'; |
| | } else if (key == '/') { |
| | key = '?'; |
| | } |
| | } |
| |
|
| | return key; |
| | } else { |
| | return 0; |
| | } |
| | } |
| |
|
| | |
| | else if (type == mjITEM_EDITINT || type == mjITEM_EDITNUM || type == mjITEM_EDITFLOAT) { |
| | if (sz < (mjMAXUITEXT-1) && |
| | (key == ' ' || key == '+' || key == '=' || key == '-' || (key >= '0' && key <= '9') || |
| | (key >= mjKEY_NUMPAD_0 && key <= mjKEY_NUMPAD_9) || |
| | ((key == 'e' || key == 'E' || key == '.') && |
| | (type == mjITEM_EDITNUM || type == mjITEM_EDITFLOAT)))) { |
| |
|
| | |
| | if (key == '=') { |
| | key = '+'; |
| | } |
| |
|
| | |
| | if (key == 'E') { |
| | key = 'e'; |
| | } |
| |
|
| | |
| | if (key >= mjKEY_NUMPAD_0 && key <= mjKEY_NUMPAD_9) { |
| | key = key - mjKEY_NUMPAD_0 + '0'; |
| | } |
| |
|
| | return key; |
| |
|
| | } else { |
| | return 0; |
| | } |
| | } |
| |
|
| | |
| | else { |
| | return 0; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void revealcursor(mjrRect r, mjUI* ui, const mjrContext* con) { |
| | |
| | if (ui->editcursor <= ui->editscroll) { |
| | ui->editscroll = ui->editcursor; |
| | return; |
| | } |
| |
|
| | |
| | int width = r.width - 2*SCL(ui->spacing.texthor, con); |
| |
|
| | |
| | int i = ui->editcursor; |
| | while (width >= 0 && i >= ui->editscroll && i > 0) { |
| | i--; |
| | width -= con->charWidth[(unsigned char)ui->edittext[i]]; |
| | } |
| |
|
| | |
| | if (width < 0) { |
| | ui->editscroll = i+1; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void setcursor(mjrRect r, mjUI* ui, const mjuiState* ins, const mjrContext* con) { |
| | |
| | int g_texthor = SCL(ui->spacing.texthor, con); |
| | mjrRect r1 = r; |
| | r1.left += g_texthor; |
| | r1.width -= 2*g_texthor; |
| |
|
| | |
| | double rx, ry; |
| | mouseinrect(r1, ui, ins, &rx, &ry); |
| |
|
| | |
| | if (rx < 0) { |
| | ui->editcursor = 0; |
| | ui->editscroll = 0; |
| | } else if (rx > 1) { |
| | ui->editcursor = strlen(ui->edittext); |
| | revealcursor(r, ui, con); |
| | } |
| |
|
| | |
| | else { |
| | int besti = ui->editscroll, cumsum = 0; |
| | int R = mju_round(r1.width*rx), bestdif = R; |
| |
|
| | |
| | for (int i=ui->editscroll; i < strlen(ui->edittext); i++) { |
| | |
| | cumsum += con->charWidth[(unsigned char)ui->edittext[i]]; |
| |
|
| | |
| | if (bestdif > abs(cumsum-R)) { |
| | bestdif = abs(cumsum-R); |
| | besti = i+1; |
| | } |
| | } |
| |
|
| | |
| | ui->editcursor = besti; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void parseshortcut(const char* text, int* mod, int* key) { |
| | |
| | *mod = 0; |
| | *key = 0; |
| |
|
| | |
| | if (!text[0]) { |
| | return; |
| | } |
| |
|
| | |
| | if (strlen(text) < 2 || strlen(text) > 5) { |
| | mju_error("mjui_add: invalid shortcut specification"); |
| | } |
| |
|
| | |
| | switch (text[0]) { |
| | case ' ': |
| | *mod = 0; |
| | break; |
| |
|
| | case 'C': |
| | *mod = 1; |
| | break; |
| |
|
| | case 'S': |
| | *mod = 2; |
| | break; |
| |
|
| | case 'A': |
| | *mod = 4; |
| | break; |
| |
|
| | default: |
| | mju_error("mjui_add: invalid shortcut modifier"); |
| | } |
| |
|
| | |
| | if (text[1] == '#') { |
| | if (sscanf(text+2, "%d", key) != 1) { |
| | mju_error("mjui_add: invalid shortcut numeric code"); |
| | } |
| | } else { |
| | if (text[2] != 0) { |
| | mju_error("mjui_add: invalid shortcut"); |
| | } |
| | *key = (int)text[1]; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static int matchshortcut(const mjuiState* ins, int mod, int key) { |
| | |
| | if (!key || ins->key != key) { |
| | return 0; |
| | } |
| |
|
| | |
| | if ((ins->control != 0) + 2*(ins->shift != 0) + 4*(ins->alt != 0) != mod) { |
| | return 0; |
| | } |
| |
|
| | return 1; |
| | } |
| |
|
| |
|
| |
|
| | |
| |
|
| | |
| | mjuiThemeSpacing mjui_themeSpacing(int ind) { |
| | if (ind == 0) { |
| | return themeSpacing0; |
| | } else { |
| | return themeSpacing1; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | mjuiThemeColor mjui_themeColor(int ind) { |
| | if (ind == 0) { |
| | return themeColor0; |
| | } else if (ind == 1) { |
| | return themeColor1; |
| | } else if (ind == 2) { |
| | return themeColor2; |
| | } else { |
| | return themeColor3; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | void mjui_add(mjUI* ui, const mjuiDef* def) { |
| | int n = 0, i, start, num; |
| | double x[1+2*mjMAXUIEDIT]; |
| |
|
| | |
| | while (def[n].type != mjITEM_END) { |
| | |
| | if (def[n].type == mjITEM_SECTION) { |
| | |
| | if (ui->nsect >= mjMAXUISECT) { |
| | mju_error("mjui_add: too many sections"); |
| | } |
| |
|
| | |
| | if (strlen(def[n].name) >= mjMAXUINAME-1) { |
| | mju_error("mjui_add: section name too long"); |
| | } |
| | if (def[n].state != mjSECT_CLOSED && def[n].state != mjSECT_OPEN && |
| | def[n].state != mjSECT_FIXED && def[n].state != mjPRESERVE) { |
| | mju_error("mjui_add: invalid section state"); |
| | } |
| |
|
| | |
| | ui->nsect++; |
| | mjuiSection* se = ui->sect + (ui->nsect-1); |
| | int oldstate = se->state; |
| |
|
| | |
| | int itemstate[mjMAXUIITEM]; |
| | for (int i = 0; i < mjMAXUIITEM; ++i) { |
| | itemstate[i] = se->item[i].state; |
| | } |
| | memset(se, 0, sizeof(mjuiSection)); |
| | for (int i = 0; i < mjMAXUIITEM; ++i) { |
| | se->item[i].state = itemstate[i]; |
| | } |
| |
|
| | |
| | se->state = (def[n].state == mjPRESERVE ? oldstate : def[n].state); |
| |
|
| | |
| | mjSTRNCPY(se->name, def[n].name); |
| | se->checkbox = def[n].otherint; |
| | parseshortcut(def[n].other, &(se->modifier), &(se->shortcut)); |
| | } |
| |
|
| | |
| | else if (def[n].type >= 0 && def[n].type < mjNITEM) { |
| | |
| | if (ui->nsect <= 0) { |
| | mju_error("mjui_add: item defined outside section"); |
| | } |
| | mjuiSection* se = ui->sect + (ui->nsect-1); |
| |
|
| | |
| | if (se->nitem >= mjMAXUIITEM) { |
| | mju_error("mjui_add: too many items in section"); |
| | } |
| |
|
| | |
| | if (def[n].type < 0 || def[n].type >= mjNITEM) { |
| | mju_error("mjui_add: invalid item type"); |
| | } |
| | if (strlen(def[n].name) >= mjMAXUINAME) { |
| | mju_error("mjui_add: item name too long"); |
| | } |
| | if (def[n].state < 0) { |
| | mju_error("mjui_add: invalid item state"); |
| | } |
| |
|
| | |
| | se->nitem++; |
| | mjuiItem* it = se->item + (se->nitem-1); |
| | int oldstate = it->state; |
| | memset(it, 0, sizeof(mjuiItem)); |
| |
|
| | |
| | if (def[n].type == mjITEM_SEPARATOR && def[n].state == mjPRESERVE) { |
| | |
| | it->state = (oldstate < mjSEPCLOSED ? mjSEPCLOSED : oldstate); |
| | } |
| | else { |
| | it->state = def[n].state; |
| | } |
| |
|
| | |
| | it->type = def[n].type; |
| | it->pdata = def[n].pdata; |
| | mjSTRNCPY(it->name, def[n].name); |
| | it->sectionid = ui->nsect - 1; |
| | it->itemid = se->nitem - 1; |
| | it->userid = def[n].otherint; |
| |
|
| | |
| | if (it->type > mjITEM_BUTTON && it->pdata == 0) { |
| | mju_error("mjui_add: no data pointer for item with data"); |
| | } |
| | if (it->type <= mjITEM_BUTTON && it->pdata) { |
| | mju_error("mjui_add: data pointer for item without data"); |
| | } |
| |
|
| | |
| | if (it->type == mjITEM_BUTTON || |
| | it->type == mjITEM_CHECKINT || |
| | it->type == mjITEM_CHECKBYTE) { |
| | parseshortcut(def[n].other, &(it->single.modifier), &(it->single.shortcut)); |
| | } |
| |
|
| | |
| | else if (it->type == mjITEM_STATIC || |
| | it->type == mjITEM_RADIO || |
| | it->type == mjITEM_RADIOLINE || |
| | it->type == mjITEM_SELECT) { |
| | |
| | it->multi.nelem = 0; |
| | num = strlen(def[n].other); |
| | i = 0; |
| |
|
| | |
| | while (i < num) { |
| | |
| | if (it->multi.nelem >= mjMAXUIMULTI) { |
| | mju_error("mjui_add: too many multi elements"); |
| | } |
| |
|
| | |
| | start = i; |
| | while (def[n].other[i] != '\n' && def[n].other[i] != 0) { |
| | i++; |
| | } |
| |
|
| | |
| | if (i == start || i-start >= mjMAXUINAME-1) { |
| | mju_error("mjui_add: invalid multi element name"); |
| | } |
| |
|
| | |
| | int ind = it->multi.nelem; |
| | (it->multi.nelem)++; |
| | strncpy(it->multi.name[ind], def[n].other+start, i-start); |
| | it->multi.name[ind][i-start] = 0; |
| |
|
| | |
| | i++; |
| | } |
| | } |
| |
|
| | |
| | else if (it->type == mjITEM_SLIDERINT || it->type == mjITEM_SLIDERNUM) { |
| | |
| | num = sscanf(def[n].other, "%lf %lf %lf", x, x+1, x+2); |
| | if (num != 2 && num != 3) { |
| | mju_error("mjui_add: slider expects 'min max [div]'"); |
| | } |
| |
|
| | |
| | it->slider.range[0] = x[0]; |
| | it->slider.range[1] = x[1]; |
| |
|
| | |
| | if (num == 3) { |
| | it->slider.divisions = mjMAX(1, x[2]); |
| | } else { |
| | if (it->type == mjITEM_SLIDERINT) { |
| | it->slider.divisions = mjMAX(1, x[1]-x[0]); |
| | } else { |
| | it->slider.divisions = 200; |
| | } |
| | } |
| | } |
| |
|
| | |
| | else if (it->type == mjITEM_EDITINT || it->type == mjITEM_EDITNUM || it->type == mjITEM_EDITFLOAT) { |
| | |
| | if (mjMAXUIEDIT > 7) { |
| | mju_error("internal error: mjMAXUIEDIT bigger than 7"); |
| | } |
| |
|
| | |
| | num = sscanf(def[n].other, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", |
| | x, x+1, x+2, x+3, x+4, x+5, x+6, x+7, x+8, x+9, x+10, x+11, x+12, x+13, x+14); |
| |
|
| | |
| | if (num < 1) { |
| | mju_error("mjui_add: edit length missing"); |
| | } |
| | it->edit.nelem = mju_round((mjtNum)x[0]); |
| | if (it->edit.nelem < 1 || it->edit.nelem > mjMAXUIEDIT) { |
| | mju_error("mjui_add: invalid edit length"); |
| | } |
| | if (it->edit.nelem*2 != (num-1) && num > 1) { |
| | mju_error("mjui_add: incorrent number of edit ranges"); |
| | } |
| |
|
| | |
| | if (num > 1) { |
| | for (int i=0; i < it->edit.nelem; i++) { |
| | it->edit.range[i][0] = x[1+2*i]; |
| | it->edit.range[i][1] = x[2+2*i]; |
| | } |
| | } |
| | } |
| |
|
| | |
| | else if (it->type == mjITEM_EDITTXT) { |
| | |
| | it->edit.nelem = 0; |
| | } |
| | } |
| |
|
| | |
| | else { |
| | mju_error("mjui_add: invalid item type"); |
| | } |
| |
|
| | |
| | n++; |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | void mjui_addToSection(mjUI* ui, int sect, const mjuiDef* def) { |
| | int nsect = ui->nsect; |
| | ui->nsect = sect + 1; |
| | mjui_add(ui, def); |
| | ui->nsect = nsect; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void setitemskip(mjuiSection* s, int pass) { |
| | int skip = 0; |
| |
|
| | |
| | for (int i = 0; i < s->nitem; ++i) { |
| | mjuiItem* it = s->item + i; |
| |
|
| | |
| | if (pass == 0) { |
| | it->skip = 0; |
| | continue; |
| | } |
| |
|
| | |
| | if (it->type == mjITEM_SEPARATOR) { |
| | skip = (it->state == mjSEPCLOSED); |
| | } |
| |
|
| | |
| | else { |
| | it->skip = skip; |
| | } |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void tryresize(mjUI* ui, const mjrContext* con) { |
| | |
| | int w_master = SCL(ui->spacing.total, con); |
| | int w_scroll = SCL(ui->spacing.scroll, con); |
| | int g_section = SCL(ui->spacing.section, con); |
| | int g_itemside = SCL(ui->spacing.itemside, con); |
| | int g_itemmid = SCL(ui->spacing.itemmid, con); |
| | int g_itemver = SCL(ui->spacing.itemver, con); |
| | int g_textver = SCL(ui->spacing.textver, con); |
| | int g_label = SCL(ui->spacing.label, con); |
| |
|
| | |
| | int textheight = con->charHeight + 2*g_textver; |
| |
|
| | |
| | int colwidth = (w_master - w_scroll - 2*g_section - 2*g_itemside - g_itemmid)/2; |
| |
|
| | |
| | int Height, MaxHeight; |
| | for (int pass = 0; pass < 2; ++pass) { |
| | |
| | int height = 0; |
| | int maxheight = 0; |
| |
|
| | |
| | for (int n = 0; n < ui->nsect; n++) { |
| | |
| | height += g_section; |
| | maxheight += g_section; |
| |
|
| | |
| | mjuiSection* s = ui->sect + n; |
| |
|
| | |
| | setitemskip(s, pass); |
| |
|
| | |
| | s->rtitle.left = g_section; |
| | s->rtitle.width = w_master - w_scroll - 2 * g_section; |
| | if (s->state == mjSECT_FIXED) { |
| | s->rtitle.bottom = height; |
| | s->rtitle.height = 0; |
| | } |
| | else { |
| | s->rtitle.bottom = height + textheight; |
| | s->rtitle.height = textheight; |
| | } |
| |
|
| | |
| | height += s->rtitle.height; |
| | maxheight += s->rtitle.height; |
| |
|
| | |
| | s->rcontent.left = s->rtitle.left; |
| | s->rcontent.width = s->rtitle.width; |
| | s->rcontent.height = 0; |
| | s->rcontent.bottom = 0; |
| |
|
| | |
| | for (int i = 0; i < s->nitem; i++) { |
| | |
| | mjuiItem* it = s->item + i; |
| | memset(&it->rect, 0, sizeof(mjrRect)); |
| |
|
| | |
| | if (it->skip) { |
| | continue; |
| | } |
| |
|
| | |
| | s->rcontent.height += it->type == mjITEM_SEPARATOR ? g_section : g_itemver; |
| |
|
| | |
| | if (i < s->nitem - 1 && s->item[i + 1].type == it->type && |
| | (it->type == mjITEM_BUTTON || |
| | it->type == mjITEM_CHECKINT || |
| | it->type == mjITEM_CHECKBYTE)) { |
| | |
| | mjuiItem* it1 = s->item + (i + 1); |
| |
|
| | |
| | it->rect.left = s->rcontent.left + g_itemside; |
| | it->rect.width = colwidth; |
| | it->rect.height = textheight; |
| |
|
| | |
| | it1->rect.left = s->rcontent.left + g_itemside + colwidth + g_itemmid; |
| | it1->rect.width = colwidth; |
| | it1->rect.height = textheight; |
| | it1->rect.bottom = height + s->rcontent.height + it->rect.height; |
| |
|
| | |
| | i++; |
| | } |
| |
|
| | |
| | else { |
| | |
| | it->rect.left = s->rcontent.left + g_itemside; |
| |
|
| | |
| | if (it->type == mjITEM_STATIC) { |
| | it->rect.width = s->rcontent.width - 2 * g_itemside; |
| | it->rect.height = (con->charHeight + g_textver) * it->multi.nelem; |
| | } |
| |
|
| | |
| | else if (it->type == mjITEM_BUTTON || |
| | it->type == mjITEM_CHECKINT || |
| | it->type == mjITEM_CHECKBYTE) { |
| | it->rect.width = colwidth; |
| | it->rect.height = textheight; |
| | } |
| |
|
| | |
| | else if (it->type == mjITEM_RADIO) { |
| | int ncol = ui->radiocol ? ui->radiocol : 2; |
| | int nrow = (it->multi.nelem - 1) / ncol + 1; |
| | it->rect.width = s->rcontent.width - 2 * g_itemside; |
| | it->rect.height = textheight * nrow; |
| | } |
| |
|
| | |
| | else { |
| | it->rect.width = s->rcontent.width - 2 * g_itemside; |
| | it->rect.height = textheight; |
| | } |
| |
|
| | |
| | if (it->name[0] && |
| | (it->type >= mjITEM_RADIO || |
| | it->type >= mjITEM_RADIOLINE || |
| | it->type == mjITEM_STATIC)) { |
| | it->rect.left = s->rcontent.left + g_itemside + g_label; |
| | it->rect.width = s->rcontent.width - (2 * g_itemside + g_label); |
| | } |
| | } |
| |
|
| | |
| | it->rect.bottom = height + s->rcontent.height + it->rect.height; |
| | s->rcontent.height += it->rect.height; |
| | } |
| |
|
| | |
| | s->rcontent.height += g_itemver; |
| | s->rcontent.bottom = height + s->rcontent.height; |
| |
|
| | |
| | if (s->state != mjSECT_CLOSED) { |
| | height += s->rcontent.height; |
| | } |
| | maxheight += s->rcontent.height; |
| | } |
| |
|
| | |
| | height += g_section; |
| | maxheight += g_section; |
| |
|
| | |
| | if (pass == 0) { |
| | MaxHeight = maxheight; |
| | } |
| | else { |
| | Height = height; |
| | } |
| | } |
| |
|
| | |
| | for (int n=0; n < ui->nsect; n++) { |
| | |
| | mjuiSection* s = ui->sect + n; |
| | s->rtitle.bottom = Height - s->rtitle.bottom; |
| | s->rcontent.bottom = Height - s->rcontent.bottom; |
| |
|
| | |
| | for (int i=0; i < s->nitem; i++) { |
| | s->item[i].rect.bottom = Height - s->item[i].rect.bottom; |
| | } |
| | } |
| |
|
| | |
| | ui->width = w_master; |
| | ui->height = Height; |
| | ui->maxheight = MaxHeight; |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void insertionsortgroup(int* list, int num, int stride) { |
| | |
| | if (stride > 10) { |
| | mju_error("insertionsortgroup cannot handle stride greater than 10"); |
| | } |
| | int x[10]; |
| |
|
| | for (int i = 1; i < num; i++) { |
| | memcpy(x, list + i * stride, sizeof(int) * stride); |
| |
|
| | int j = i - 1; |
| | while (j >= 0 && list[j * stride] > x[0]) { |
| | memcpy(list + (j + 1) * stride, list + j * stride, sizeof(int) * stride); |
| | j--; |
| | } |
| |
|
| | memcpy(list + (j + 1) * stride, x, sizeof(int) * stride); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | void mjui_resize(mjUI* ui, const mjrContext* con) { |
| | |
| | int maxBufferSize = 0; |
| | glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &maxBufferSize); |
| |
|
| | |
| | |
| |
|
| | |
| | tryresize(ui, con); |
| | ui->maxheight = mjMIN(ui->maxheight, maxBufferSize); |
| |
|
| | |
| | if (ui->height > ui->maxheight) { |
| | |
| | int hnew = (ui->nsect + 1) * SCL(ui->spacing.section, con); |
| |
|
| | |
| | for (int n = 0; n < ui->nsect; ++n) { |
| | if (ui->sect[n].state == mjSECT_FIXED) { |
| | hnew += ui->sect[n].rcontent.height; |
| | } |
| | else { |
| | hnew += ui->sect[n].rtitle.height; |
| | } |
| | } |
| |
|
| | |
| | if (hnew > ui->maxheight) { |
| | mju_error("fixed section height already too big, closing sections cannot help"); |
| | } |
| |
|
| | |
| | int nopen = 0; |
| | int sortbuf[2 * mjMAXUISECT] = { 0 }; |
| | for (int n = 0; n < ui->nsect; ++n) { |
| | if (ui->sect[n].state == mjSECT_OPEN) { |
| | sortbuf[2 * nopen] = ui->sect[n].lastclick; |
| | sortbuf[2 * nopen + 1] = n; |
| | ++nopen; |
| | } |
| | } |
| | insertionsortgroup(sortbuf, nopen, 2); |
| |
|
| | |
| | if (nopen == 0) { |
| | mju_error("internal error: expected some sections to be open"); |
| | } |
| |
|
| | |
| | for (int i = nopen - 1; i >= 0; --i) { |
| | |
| | if (hnew + ui->sect[sortbuf[2 * i + 1]].rcontent.height <= ui->maxheight) { |
| | hnew += ui->sect[sortbuf[2 * i + 1]].rcontent.height; |
| | } |
| |
|
| | |
| | else { |
| | sortbuf[2 * i] = -1; |
| | } |
| | } |
| |
|
| | |
| | for (int i = 0; i < nopen; ++i) { |
| | if (sortbuf[2 * i] == -1) { |
| | ui->sect[sortbuf[2 * i + 1]].state = mjSECT_CLOSED; |
| | } |
| | } |
| |
|
| | |
| | tryresize(ui, con); |
| | ui->maxheight = mjMIN(ui->maxheight, maxBufferSize); |
| |
|
| | |
| | if (ui->height != hnew) { |
| | mju_error("internal error: tryresize produced unexpeced ui height"); |
| | } |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static int evalpredicate(int state, mjfItemEnable predicate, void* userdata) { |
| | if (state <= 0) { |
| | return 0; |
| | } else if (state == 1 || predicate == NULL) { |
| | return 1; |
| | } else { |
| | return predicate(state, userdata); |
| | } |
| | } |
| |
|
| |
|
| |
|
| | |
| | static void shortcuthelp(mjrRect r, int modifier, int shortcut, |
| | const mjUI* ui, const mjrContext* con) { |
| | |
| | #define NMAP 27 |
| | const struct { |
| | int key; |
| | const char* value; |
| | } keymap[NMAP] = { |
| | {32 , "Space"}, |
| | {mjKEY_ESCAPE , "Esc"}, |
| | {mjKEY_ENTER , "Enter"}, |
| | {mjKEY_TAB , "Tab"}, |
| | {mjKEY_BACKSPACE , "BackSpace"}, |
| | {mjKEY_INSERT , "Ins"}, |
| | {mjKEY_DELETE , "Del"}, |
| | {mjKEY_RIGHT , "Right"}, |
| | {mjKEY_LEFT , "Left"}, |
| | {mjKEY_DOWN , "Down"}, |
| | {mjKEY_UP , "Up"}, |
| | {mjKEY_PAGE_UP , "PgUp"}, |
| | {mjKEY_PAGE_DOWN , "PgDn"}, |
| | {mjKEY_HOME , "Home"}, |
| | {mjKEY_END , "End"}, |
| | {mjKEY_F1 , "F1"}, |
| | {mjKEY_F2 , "F2"}, |
| | {mjKEY_F3 , "F3"}, |
| | {mjKEY_F4 , "F4"}, |
| | {mjKEY_F5 , "F5"}, |
| | {mjKEY_F6 , "F6"}, |
| | {mjKEY_F7 , "F7"}, |
| | {mjKEY_F8 , "F8"}, |
| | {mjKEY_F9 , "F9"}, |
| | {mjKEY_F10 , "F10"}, |
| | {mjKEY_F11 , "F11"}, |
| | {mjKEY_F12 , "F12"} |
| | }; |
| |
|
| | |
| | char key[10] = ""; |
| | if (shortcut > 32 && shortcut <= 126) { |
| | key[0] = (char)shortcut; |
| | key[1] = 0; |
| | } else { |
| | for (int i=0; i < NMAP; i++) { |
| | if (keymap[i].key == shortcut) { |
| | mjSTRNCPY(key, keymap[i].value); |
| | break; |
| | } |
| | } |
| | } |
| |
|
| | |
| | char text[50] = ""; |
| | if (modifier == 1) { |
| | mjSTRNCPY(text, "Ctrl "); |
| | } else if (modifier == 2) { |
| | mjSTRNCPY(text, "Shift "); |
| | } else if (modifier == 4) { |
| | mjSTRNCPY(text, "Alt "); |
| | } |
| |
|
| | |
| | strcat(text, key); |
| |
|
| | |
| | int g_textver = SCL(ui->spacing.textver, con); |
| | int width = textwidth(text, con, -1) + 2*g_textver; |
| | r.left += (r.width-width); |
| | r.width = width; |
| | r.bottom += g_textver; |
| | r.height -= 2*g_textver; |
| |
|
| | |
| | drawrectangle(r, ui->color.shortcut, NULL, con); |
| | drawtext(text, r.left+g_textver, r.bottom, |
| | r.width, ui->color.fontactive, con); |
| | } |
| |
|
| |
|
| |
|
| | |
| | void mjui_update(int section, int item, const mjUI* ui, |
| | const mjuiState* state, const mjrContext* con) { |
| | int g_texthor = SCL(ui->spacing.texthor, con); |
| | int g_textver = SCL(ui->spacing.textver, con); |
| | int g_label = SCL(ui->spacing.label, con); |
| | int g_itemside = SCL(ui->spacing.itemside, con); |
| | int g_itemmid = SCL(ui->spacing.itemmid, con); |
| | int cellheight = con->charHeight + 2*g_textver; |
| | const float* rgbpane = ui->color.sectpane; |
| |
|
| | |
| | int msect, mitem; |
| | findmouse(ui, state, con, &msect, &mitem); |
| |
|
| | |
| | mjr_setAux(ui->auxid, con); |
| | mjrRect rgl = { 0, 0, ui->width, ui->height }; |
| | initOpenGL(&rgl, con); |
| |
|
| | |
| | if (section < 0) { |
| | glClearColor(ui->color.master[0], ui->color.master[1], |
| | ui->color.master[2], 1); |
| | glClear(GL_COLOR_BUFFER_BIT); |
| | } |
| |
|
| | |
| | int start_section = 0; |
| | int num_section = ui->nsect; |
| | if (section >= 0) { |
| | start_section = section; |
| | num_section = 1; |
| | } |
| |
|
| | |
| | mjtNum sel; |
| | mjrRect r; |
| | char text[mjMAXUITEXT]; |
| | int sep[mjMAXUIMULTI+1]; |
| | for (int n=start_section; n < start_section+num_section; n++) { |
| | |
| | const mjuiSection* s = ui->sect + n; |
| |
|
| | |
| | int maxwidth = (s->rtitle.width - 2*g_itemside - g_itemmid)/2 - 2*g_texthor; |
| |
|
| | |
| | if (section < 0 || item < 0) { |
| | r = s->rtitle; |
| |
|
| | |
| | if (s->state != mjSECT_FIXED) { |
| | |
| | if (s->checkbox == 0) { |
| | |
| | glBegin(GL_QUADS); |
| | glColor3fv(ui->color.secttitle2); |
| | glVertex2i(r.left, r.bottom); |
| | glVertex2i(r.left + r.width, r.bottom); |
| | glColor3fv(ui->color.secttitle); |
| | glVertex2i(r.left + r.width, r.bottom + r.height); |
| | glVertex2i(r.left, r.bottom + r.height); |
| | glEnd(); |
| |
|
| | |
| | drawsymbol(r, s->state, 0, ui, con); |
| | drawtext(s->name, r.left + g_texthor, |
| | r.bottom + g_textver, 2 * maxwidth, |
| | ui->color.sectfont, con); |
| | } |
| |
|
| | |
| | else { |
| | |
| | const float* rgb = (s->checkbox == 1 ? ui->color.secttitleuncheck |
| | : ui->color.secttitlecheck); |
| | const float* rgb2 = (s->checkbox == 1 ? ui->color.secttitleuncheck2 |
| | : ui->color.secttitlecheck2); |
| |
|
| | |
| | glBegin(GL_QUADS); |
| | glColor3fv(rgb2); |
| | glVertex2i(r.left, r.bottom); |
| | glVertex2i(r.left + r.width, r.bottom); |
| | glColor3fv(rgb); |
| | glVertex2i(r.left + r.width, r.bottom + r.height); |
| | glVertex2i(r.left, r.bottom + r.height); |
| | glEnd(); |
| |
|
| | |
| | drawsymbol(r, s->state, s->checkbox, ui, con); |
| | drawtext(s->name, r.left + r.height, |
| | r.bottom + g_textver, 2 * maxwidth - r.height, |
| | ui->color.sectfont, con); |
| |
|
| | |
| | int cgap = r.height / 4; |
| | mjrRect cr = {r.left + cgap, |
| | r.bottom + cgap, |
| | r.height - 2 * cgap, |
| | r.height - 2 * cgap}; |
| | float rgbmean[3] = { |
| | 0.5f * (rgb[0] + rgb2[0]), |
| | 0.5f * (rgb[1] + rgb2[1]), |
| | 0.5f * (rgb[2] + rgb2[2]), |
| | }; |
| | drawrectangle(cr, ui->color.sectsymbol, |
| | s->checkbox == 1 ? rgbmean : NULL, con); |
| | } |
| |
|
| | |
| | if (ui->mousehelp && s->shortcut) { |
| | shortcuthelp(r, s->modifier, s->shortcut, ui, con); |
| | } |
| | } |
| |
|
| | |
| | if (s->state != mjSECT_CLOSED) { |
| | drawrectangle(s->rcontent, ui->color.sectpane, NULL, con); |
| | } |
| |
|
| | |
| | mjrRect rround = s->rtitle; |
| | if (s->state == mjSECT_FIXED) { |
| | rround = s->rcontent; |
| | } |
| | else if (s->state == mjSECT_OPEN) { |
| | rround.bottom = s->rcontent.bottom; |
| | rround.height = s->rtitle.height + s->rcontent.height; |
| | } |
| | roundcorner(rround, 0, 0, ui, con); |
| | } |
| |
|
| | |
| | if (!s->state) { |
| | continue; |
| | } |
| |
|
| | |
| | int start_item = 0; |
| | int num_item = s->nitem; |
| | if (section >= 0 && item >= 0) { |
| | start_item = item; |
| | num_item = 1; |
| | } |
| |
|
| | |
| | for (int i=start_item; i < start_item+num_item; i++) { |
| | |
| | const mjuiItem* it = s->item + i; |
| |
|
| | |
| | if (it->rect.height == 0) { |
| | continue; |
| | } |
| |
|
| | |
| | const float* rgbfont = ui->color.fontinactive; |
| | const float* rgbdecor = ui->color.decorinactive; |
| | const float* rgbdecor2 = ui->color.decorinactive2; |
| |
|
| | |
| | int state = evalpredicate(it->state, ui->predicate, ui->userdata); |
| | if (state) { |
| | rgbfont = ui->color.fontactive; |
| | } |
| |
|
| | |
| | switch (it->type) { |
| | case mjITEM_SEPARATOR: |
| | |
| | r = it->rect; |
| | glBegin(GL_QUADS); |
| | glColor3fv(ui->color.separator2); |
| | glVertex2i(r.left, r.bottom); |
| | glVertex2i(r.left+r.width, r.bottom); |
| | glColor3fv(ui->color.separator); |
| | glVertex2i(r.left+r.width, r.bottom+r.height); |
| | glVertex2i(r.left, r.bottom+r.height); |
| | glEnd(); |
| |
|
| | |
| | drawtext(it->name, |
| | it->rect.left+g_texthor, |
| | it->rect.bottom+g_textver, |
| | it->rect.width-2*g_texthor, ui->color.sectfont, con); |
| |
|
| | |
| | if (it->state >= mjSEPCLOSED) { |
| | int flg_open = (it->state == mjSEPCLOSED + 1); |
| | drawsymbol(it->rect, flg_open, 3, ui, con); |
| | roundcorner(it->rect, flg_open, 1, ui, con); |
| | } |
| | break; |
| |
|
| | case mjITEM_STATIC: |
| | r = it->rect; |
| |
|
| | |
| | drawtext(it->name, |
| | s->rcontent.left+g_itemside+g_texthor, |
| | r.bottom+g_textver+(it->multi.nelem-1)*(con->charHeight+g_textver), |
| | g_label-2*g_texthor, rgbfont, con); |
| |
|
| | |
| | drawrectangle(r, rgbpane, rgbpane, con); |
| |
|
| | |
| | for (int k=0; k < it->multi.nelem; k++) { |
| | drawtext(it->multi.name[k], |
| | r.left+g_texthor, |
| | r.bottom+g_textver+(it->multi.nelem-k-1)*(con->charHeight+g_textver), |
| | r.width-2*g_texthor, rgbfont, con); |
| | } |
| | break; |
| |
|
| | case mjITEM_BUTTON: |
| | if (state) { |
| | rgbdecor = ui->color.button; |
| | } |
| |
|
| | |
| | if (ui->mousesect == n+1 && ui->mouseitem == i && msect == n+1 && mitem == i) { |
| | drawoval(it->rect, rgbdecor, NULL, con); |
| | } else { |
| | drawoval(it->rect, rgbdecor, rgbpane, con); |
| | } |
| |
|
| | |
| | drawtext(it->name, |
| | it->rect.left+g_texthor, |
| | it->rect.bottom+g_textver, |
| | maxwidth, rgbfont, con); |
| |
|
| | |
| | if (ui->mousehelp && it->single.shortcut) { |
| | shortcuthelp(it->rect, it->single.modifier, it->single.shortcut, ui, con); |
| | } |
| | break; |
| |
|
| | case mjITEM_CHECKINT: |
| | case mjITEM_CHECKBYTE: |
| | { |
| | if (state) { |
| | rgbdecor = ui->color.check; |
| | } |
| |
|
| | |
| | int k; |
| | if (it->type == mjITEM_CHECKINT) { |
| | k = *(int*)it->pdata; |
| | } else { |
| | k = *(mjtByte*)it->pdata; |
| | } |
| |
|
| | |
| | if (k) { |
| | drawrectangle(it->rect, rgbdecor, NULL, con); |
| | } else { |
| | drawrectangle(it->rect, rgbdecor, rgbpane, con); |
| | } |
| |
|
| | |
| | drawtext(it->name, |
| | it->rect.left+g_texthor, |
| | it->rect.bottom+g_textver, |
| | maxwidth, rgbfont, con); |
| |
|
| | |
| | if (ui->mousehelp && it->single.shortcut) |
| | shortcuthelp(it->rect, it->single.modifier, it->single.shortcut, |
| | ui, con); |
| | } |
| | break; |
| |
|
| | case mjITEM_RADIO: |
| | if (state) { |
| | rgbdecor = ui->color.radio; |
| | } |
| |
|
| | |
| | drawtext(it->name, |
| | s->rcontent.left+g_itemside+g_texthor, |
| | it->rect.bottom+it->rect.height-cellheight+g_textver, |
| | g_label-2*g_texthor, rgbfont, con); |
| |
|
| | |
| | drawrectangle(it->rect, rgbdecor, rgbpane, con); |
| |
|
| | |
| | r = radioelement(it, *(int*)it->pdata, ui, con); |
| | drawrectangle(r, rgbdecor, NULL, con); |
| |
|
| | |
| | for (int k=0; k < it->multi.nelem; k++) { |
| | r = radioelement(it, k, ui, con); |
| | drawtext(it->multi.name[k], |
| | r.left+g_texthor, |
| | r.bottom+g_textver, |
| | r.width-2*g_texthor, rgbfont, con); |
| | } |
| | break; |
| |
|
| | case mjITEM_RADIOLINE: |
| | { |
| | if (state) { |
| | rgbdecor = ui->color.radio; |
| | } |
| |
|
| | |
| | drawtext(it->name, |
| | s->rcontent.left+g_itemside+g_texthor, |
| | it->rect.bottom+g_textver, |
| | g_label-2*g_texthor, rgbfont, con); |
| |
|
| | |
| | drawrectangle(it->rect, rgbdecor, rgbpane, con); |
| |
|
| | |
| | makeradioline(it, con, sep); |
| |
|
| | |
| | int k = *(int*)it->pdata; |
| | r = it->rect; |
| | r.left += sep[k]; |
| | r.width = sep[k+1] - sep[k]; |
| | drawrectangle(r, rgbdecor, NULL, con); |
| |
|
| | |
| | for (int k=0; k < it->multi.nelem; k++) { |
| | |
| | r = it->rect; |
| | r.left += sep[k]; |
| | r.width = sep[k+1] - sep[k]; |
| |
|
| | |
| | drawtextrect(r, it->multi.name[k], rgbfont, con); |
| | } |
| | } |
| | break; |
| |
|
| | case mjITEM_SELECT: |
| | if (state) { |
| | rgbdecor = ui->color.select; |
| | } |
| |
|
| | |
| | drawtext(it->name, |
| | s->rcontent.left+g_itemside+g_texthor, |
| | it->rect.bottom+g_textver, |
| | g_label-2*g_texthor, rgbfont, con); |
| |
|
| | |
| | drawrectangle(it->rect, rgbdecor, NULL, con); |
| |
|
| | |
| | if (it->multi.nelem > 0) { |
| | drawtext(it->multi.name[*(int*)it->pdata], |
| | it->rect.left+g_texthor, |
| | it->rect.bottom+g_textver, |
| | it->rect.width-2*g_texthor, rgbfont, con); |
| | } |
| |
|
| | |
| | break; |
| |
|
| | case mjITEM_SLIDERINT: |
| | case mjITEM_SLIDERNUM: |
| | if (state) { |
| | rgbdecor = ui->color.slider; |
| | rgbdecor2 = ui->color.slider2; |
| | } |
| |
|
| | |
| | if (it->type == mjITEM_SLIDERINT) { |
| | sel = (mjtNum)(*(int*)it->pdata); |
| | } else { |
| | sel = *(mjtNum*)it->pdata; |
| | } |
| | sel = (sel - it->slider.range[0]) / |
| | mjMAX(1e-10, (it->slider.range[1] - it->slider.range[0])); |
| | sel = mjMIN(1, mjMAX(0, sel)); |
| |
|
| | |
| | r = it->rect; |
| | drawrectangle(r, rgbdecor, NULL, con); |
| | r.width = mju_round(r.width*sel); |
| | drawrectangle(r, rgbdecor2, NULL, con); |
| |
|
| | |
| | if (it->slider.divisions <= 20) { |
| | |
| | r.width = SCL(2, con); |
| | r.height = g_textver; |
| |
|
| | |
| | for (int k=1; k < (int)it->slider.divisions; k++) { |
| | r.left = it->rect.left - r.width/2 + |
| | it->rect.width*k/it->slider.divisions; |
| | drawrectangle(r, rgbpane, NULL, con); |
| | } |
| | } |
| |
|
| | |
| | drawtext(it->name, |
| | s->rcontent.left+g_itemside+g_texthor, |
| | it->rect.bottom+g_textver, |
| | g_label-2*g_texthor, rgbfont, con); |
| |
|
| | |
| | if (it->type == mjITEM_SLIDERINT) { |
| | mjSNPRINTF(text, "%d", *(int*)it->pdata); |
| | } else { |
| | mjSNPRINTF(text, "%.3g", *(mjtNum*)it->pdata); |
| | } |
| | drawtext(text, |
| | it->rect.left+g_texthor, |
| | it->rect.bottom+g_textver, |
| | it->rect.width-2*g_texthor, rgbfont, con); |
| | break; |
| |
|
| | case mjITEM_EDITINT: |
| | case mjITEM_EDITNUM: |
| | case mjITEM_EDITFLOAT: |
| | case mjITEM_EDITTXT: |
| | if (state) { |
| | rgbdecor = ui->color.edit; |
| | } |
| |
|
| | |
| | drawtext(it->name, |
| | s->rcontent.left+g_itemside+g_texthor, |
| | it->rect.bottom+g_textver, |
| | g_label-2*g_texthor, rgbfont, con); |
| |
|
| | |
| | if (ui->editsect > 0 && ui->editsect == n+1 && ui->edititem == i) { |
| | |
| | if (checkedit(ui->edittext, it) == 0) { |
| | drawrectangle(it->rect, rgbdecor, NULL, con); |
| | } else { |
| | drawrectangle(it->rect, ui->color.edit2, NULL, con); |
| | } |
| |
|
| | |
| | int k = textwidth(ui->edittext + ui->editscroll, con, |
| | ui->editcursor - ui->editscroll); |
| | r.left = it->rect.left + g_texthor + k - SCL(1, con); |
| | r.width = 2*SCL(1, con); |
| | r.bottom = it->rect.bottom + g_textver/2; |
| | r.height = it->rect.height - 2*(g_textver/2); |
| | drawrectangle(r, ui->color.cursor, NULL, con); |
| |
|
| | |
| | drawtext(ui->edittext + ui->editscroll, |
| | it->rect.left+g_texthor, |
| | it->rect.bottom+g_textver, |
| | it->rect.width-2*g_texthor, rgbfont, con); |
| | } |
| |
|
| | |
| | else { |
| | |
| | drawrectangle(it->rect, rgbdecor, rgbpane, con); |
| |
|
| | |
| | array2text(text, it); |
| | drawtext(text, |
| | it->rect.left+g_texthor, |
| | it->rect.bottom+g_textver, |
| | it->rect.width-2*g_texthor, rgbfont, con); |
| | } |
| | break; |
| |
|
| | default: |
| | mju_error("mjui_update: internal error: unexpected item type"); |
| | } |
| | } |
| | } |
| |
|
| | |
| | mjr_restoreBuffer(con); |
| | } |
| |
|
| |
|
| |
|
| | |
| | mjuiItem* mjui_event(mjUI* ui, mjuiState* state, const mjrContext* con) { |
| | int i, key, change = 0; |
| | mjuiItem* it; |
| | ui->editchanged = NULL; |
| |
|
| | |
| | if (state->type == mjEVENT_PRESS) { |
| | ++ui->mouseclicks; |
| | } |
| |
|
| | |
| | if ((state->type == mjEVENT_PRESS || state->type == mjEVENT_MOVE || |
| | state->type == mjEVENT_RELEASE) && state->button != mjBUTTON_LEFT) { |
| | if (state->button == mjBUTTON_RIGHT) { |
| | if (state->type == mjEVENT_PRESS) { |
| | ui->mousehelp = 1; |
| | mjui_update(-1, -1, ui, state, con); |
| | } else if (state->type == mjEVENT_RELEASE) { |
| | ui->mousehelp = 0; |
| | mjui_update(-1, -1, ui, state, con); |
| | } |
| | } |
| |
|
| | return NULL; |
| | } |
| |
|
| | |
| | int sect_cur = -1; |
| | int item_cur = -1; |
| | mjuiItem* it_cur = NULL; |
| | findmouse(ui, state, con, §_cur, &item_cur); |
| | if (sect_cur > 0 && item_cur >= 0) { |
| | it_cur = ui->sect[sect_cur-1].item + item_cur; |
| | } |
| |
|
| | |
| | if (sect_cur > 0 && state->type == mjEVENT_PRESS) { |
| | ui->sect[sect_cur - 1].lastclick = ui->mouseclicks; |
| | } |
| |
|
| | |
| | int sect_rec = ui->mousesect; |
| | int item_rec = -1; |
| | mjuiItem* it_rec = NULL; |
| | if (sect_rec > 0) { |
| | item_rec = ui->mouseitem; |
| | it_rec = ui->sect[sect_rec-1].item + item_rec; |
| | } |
| |
|
| | |
| | int sect_edit = ui->editsect; |
| | int item_edit = -1; |
| | mjuiItem* it_edit = NULL; |
| | if (sect_edit > 0) { |
| | item_edit = ui->edititem; |
| | it_edit = ui->sect[sect_edit-1].item + item_edit; |
| | } |
| |
|
| | |
| | switch (state->type) { |
| | case mjEVENT_MOVE: |
| | |
| | if (sect_rec == -1) { |
| | ui->scroll -= mju_round((state->dy * ui->height) / |
| | state->rect[ui->rectid].height); |
| | ui->scroll = mjMAX(0, mjMIN(ui->scroll, |
| | ui->height-state->rect[ui->rectid].height)); |
| | } |
| |
|
| | |
| | else if (it_rec) { |
| | |
| | if (it_rec->type == mjITEM_SLIDERINT || it_rec->type == mjITEM_SLIDERNUM) { |
| | setslider(it_rec, ui, state, con); |
| |
|
| | |
| | mjui_update(sect_rec-1, item_rec, ui, state, con); |
| | return it_rec; |
| | } |
| |
|
| | |
| | else if (it_rec->type == mjITEM_EDITINT || |
| | it_rec->type == mjITEM_EDITNUM || |
| | it_rec->type == mjITEM_EDITFLOAT || |
| | it_rec->type == mjITEM_EDITTXT) { |
| | setcursor(it_rec->rect, ui, state, con); |
| | } |
| |
|
| | |
| | mjui_update(sect_rec-1, item_rec, ui, state, con); |
| | } |
| | return NULL; |
| |
|
| | case mjEVENT_PRESS: |
| | |
| | if (sect_edit > 0) { |
| | |
| | if (sect_edit == sect_cur && item_edit == item_cur) { |
| | |
| | ui->mousesect = sect_cur; |
| | ui->mouseitem = item_cur; |
| |
|
| | |
| | setcursor(it_edit->rect, ui, state, con); |
| |
|
| | |
| | mjui_update(sect_edit-1, item_edit, ui, state, con); |
| | return NULL; |
| | } |
| |
|
| | |
| | if (text2array(ui->edittext, it_edit) == 0) { |
| | ui->editchanged = it_edit; |
| | } |
| |
|
| | |
| | ui->editsect = 0; |
| |
|
| | |
| | mjui_update(sect_edit-1, item_edit, ui, state, con); |
| | } |
| |
|
| | |
| | ui->mousesect = 0; |
| |
|
| | |
| | if (sect_cur == -1) { |
| | ui->mousesect = -1; |
| | ui->mouseitem = 0; |
| | } |
| |
|
| | |
| | else if (sect_cur == -2) { |
| | ui->scroll = ui->height - state->rect[ui->rectid].height; |
| | } |
| |
|
| | |
| | else if (sect_cur == -3) { |
| | ui->scroll = 0; |
| | } |
| |
|
| | |
| | else if (sect_cur > 0 && item_cur < 0) { |
| | mjuiSection* se = ui->sect + sect_cur - 1; |
| |
|
| | |
| | if (item_cur == -2 && se->checkbox > 0) { |
| | ui->mousesectcheck = sect_cur; |
| | return NULL; |
| | } |
| | else { |
| | ui->mousesectcheck = 0; |
| | } |
| |
|
| | |
| | if (state->doubleclick) { |
| | for (int i=0; i < ui->nsect; i++) { |
| | if (ui->sect[i].state != mjSECT_FIXED && se->state != mjSECT_FIXED) { |
| | ui->sect[i].state = se->state; |
| | } |
| | } |
| | } |
| |
|
| | |
| | else { |
| | if (se->state == mjSECT_OPEN) { |
| | se->state = mjSECT_CLOSED; |
| | } |
| | else if (se->state == mjSECT_CLOSED) { |
| | se->state = mjSECT_OPEN; |
| | } |
| | } |
| |
|
| | |
| | mjui_resize(ui, con); |
| | mjui_update(-1, -1, ui, state, con); |
| | } |
| |
|
| | |
| | else if (sect_cur > 0 && item_cur >= 0) { |
| | |
| | if (!evalpredicate(it_cur->state, ui->predicate, ui->userdata)) { |
| | return ui->editchanged; |
| | } |
| |
|
| | |
| | switch (it_cur->type) { |
| | case mjITEM_SEPARATOR: |
| | |
| | if (it_cur->state == mjSEPCLOSED+1) { |
| | it_cur->state = mjSEPCLOSED; |
| | mjui_resize(ui, con); |
| | mjui_update(-1, -1, ui, state, con); |
| | } |
| |
|
| | |
| | else if (it_cur->state == mjSEPCLOSED) { |
| | it_cur->state = mjSEPCLOSED+1; |
| | mjui_resize(ui, con); |
| | mjui_update(-1, -1, ui, state, con); |
| | } |
| |
|
| | |
| | return ui->editchanged; |
| |
|
| | case mjITEM_BUTTON: |
| | |
| | ui->mousesect = sect_cur; |
| | ui->mouseitem = item_cur; |
| | change = 1; |
| | break; |
| |
|
| | case mjITEM_CHECKINT: |
| | |
| | *(int*)it_cur->pdata = 1 - *(int*)it_cur->pdata; |
| | change = 1; |
| | break; |
| |
|
| | case mjITEM_CHECKBYTE: |
| | |
| | *(mjtByte*)it_cur->pdata = 1 - *(mjtByte*)it_cur->pdata; |
| | change = 1; |
| | break; |
| |
|
| | case mjITEM_RADIO: |
| | |
| | i = findradio(it_cur, ui, state, con); |
| | if (i >= 0) { |
| | *(int*)it_cur->pdata = i; |
| | change = 1; |
| | } |
| | break; |
| |
|
| | case mjITEM_RADIOLINE: |
| | |
| | i = findradioline(it_cur, ui, state, con); |
| | if (i >= 0) { |
| | *(int*)it_cur->pdata = i; |
| | change = 1; |
| | } |
| | break; |
| |
|
| | case mjITEM_SELECT: |
| | |
| | ui->mousesect = sect_cur; |
| | ui->mouseitem = item_cur; |
| | break; |
| |
|
| | case mjITEM_SLIDERINT: |
| | case mjITEM_SLIDERNUM: |
| | |
| | setslider(it_cur, ui, state, con); |
| | ui->mousesect = sect_cur; |
| | ui->mouseitem = item_cur; |
| | change = 1; |
| | break; |
| |
|
| | case mjITEM_EDITINT: |
| | case mjITEM_EDITNUM: |
| | case mjITEM_EDITFLOAT: |
| | case mjITEM_EDITTXT: |
| | |
| | array2text(ui->edittext, it_cur); |
| | ui->editscroll = 0; |
| |
|
| | |
| | setcursor(it_cur->rect, ui, state, con); |
| |
|
| | |
| | ui->editsect = sect_cur; |
| | ui->edititem = item_cur; |
| | ui->mousesect = sect_cur; |
| | ui->mouseitem = item_cur; |
| | break; |
| | } |
| |
|
| | |
| | mjui_update(sect_cur-1, item_cur, ui, state, con); |
| |
|
| | |
| | if (change) { |
| | return it_cur; |
| | } |
| | } |
| |
|
| | |
| | return ui->editchanged; |
| |
|
| | case mjEVENT_RELEASE: |
| | |
| | if (it_rec && it_rec->type == mjITEM_SELECT) { |
| | |
| | i = findselect(it_rec, ui, state, con); |
| | if (i >= 0) { |
| | *(int*)it_rec->pdata = i; |
| | } |
| |
|
| | |
| | ui->mousesect = 0; |
| | mjui_update(-1, -1, ui, state, con); |
| |
|
| | |
| | if (i >= 0) { |
| | return it_rec; |
| | } |
| | } |
| |
|
| | |
| | else if (it_rec && it_rec->type == mjITEM_BUTTON) { |
| | |
| | ui->mousesect = 0; |
| | mjui_update(sect_rec-1, item_rec, ui, state, con); |
| | } |
| |
|
| | |
| | ui->mousesect = 0; |
| | return NULL; |
| |
|
| | case mjEVENT_SCROLL: |
| | |
| | ui->scroll -= mju_round(state->sy * SCL(ui->spacing.linescroll, con)); |
| |
|
| | |
| | ui->scroll = mjMAX(0, mjMIN(ui->scroll, |
| | ui->height-state->rect[ui->rectid].height)); |
| | return NULL; |
| |
|
| | case mjEVENT_KEY: |
| | |
| | if (sect_edit > 0) { |
| | |
| | key = state->key; |
| | state->key = 0; |
| |
|
| | |
| | switch (key) { |
| | case mjKEY_ESCAPE: |
| | ui->editsect = 0; |
| | break; |
| |
|
| | case mjKEY_ENTER: |
| | if (text2array(ui->edittext, it_edit) == 0) { |
| | ui->editsect = 0; |
| |
|
| | |
| | mjui_update(sect_edit-1, item_edit, ui, state, con); |
| |
|
| | |
| | ui->editchanged = it_edit; |
| | return it_edit; |
| | } |
| | break; |
| |
|
| | case mjKEY_LEFT: |
| | |
| | if (state->control) { |
| | ui->editcursor = 0; |
| | } else { |
| | ui->editcursor = mjMAX(0, ui->editcursor-1); |
| | } |
| | break; |
| |
|
| | case mjKEY_RIGHT: |
| | |
| | if (state->control) { |
| | ui->editcursor = strlen(ui->edittext); |
| | } else { |
| | ui->editcursor = mjMIN(strlen(ui->edittext), ui->editcursor+1); |
| | } |
| | break; |
| |
|
| | case mjKEY_HOME: |
| | ui->editcursor = 0; |
| | break; |
| |
|
| | case mjKEY_END: |
| | ui->editcursor = strlen(ui->edittext); |
| | break; |
| |
|
| | case mjKEY_BACKSPACE: |
| | if (ui->editcursor > 0) { |
| | |
| | for (int i=ui->editcursor; i <= strlen(ui->edittext); i++) { |
| | ui->edittext[i-1] = ui->edittext[i]; |
| | } |
| |
|
| | |
| | ui->editcursor = ui->editcursor - 1; |
| | } |
| | break; |
| |
|
| | case mjKEY_DELETE: |
| | if (ui->editcursor < strlen(ui->edittext)) { |
| | |
| | for (int i=ui->editcursor; i <= strlen(ui->edittext); i++) { |
| | ui->edittext[i] = ui->edittext[i+1]; |
| | } |
| | } |
| | break; |
| |
|
| | default: |
| | key = validkey(key, strlen(ui->edittext), it_edit->type, state); |
| | if (key) { |
| | |
| | for (int i=strlen(ui->edittext); i >= ui->editcursor; i--) { |
| | ui->edittext[i+1] = ui->edittext[i]; |
| | } |
| |
|
| | |
| | ui->edittext[ui->editcursor] = key; |
| |
|
| | |
| | ui->editcursor = ui->editcursor + 1; |
| | } |
| | } |
| |
|
| | |
| | if (ui->editsect > 0) { |
| | revealcursor(it_edit->rect, ui, con); |
| | } |
| |
|
| | |
| | mjui_update(sect_edit-1, item_edit, ui, state, con); |
| | } |
| |
|
| | |
| | else { |
| | |
| | for (int n=0; n < ui->nsect; n++) |
| | if (matchshortcut(state, ui->sect[n].modifier, ui->sect[n].shortcut)) { |
| | |
| | for (int i=0; i < ui->nsect; i++) { |
| | if (ui->sect[i].state < 2) { |
| | ui->sect[i].state = 0; |
| | } |
| | } |
| |
|
| | |
| | if (ui->sect[n].state < 2) { |
| | ui->sect[n].state = 1; |
| | } |
| |
|
| | |
| | mjui_resize(ui, con); |
| | mjui_update(-1, -1, ui, state, con); |
| |
|
| | |
| | state->key = 0; |
| | return NULL; |
| | } |
| |
|
| | |
| | for (int n=0; n < ui->nsect; n++) { |
| | for (int i=0; i < ui->sect[n].nitem; i++) { |
| | |
| | it = ui->sect[n].item + i;; |
| |
|
| | |
| | if ((it->type == mjITEM_BUTTON || it->type == mjITEM_CHECKINT || |
| | it->type == mjITEM_CHECKBYTE) && |
| | matchshortcut(state, it->single.modifier, it->single.shortcut)) { |
| | |
| | state->key = 0; |
| |
|
| | |
| | if (evalpredicate(it->state, ui->predicate, ui->userdata)) { |
| | |
| | if (it->type == mjITEM_CHECKINT) { |
| | *(int*)it->pdata = 1 - *(int*)it->pdata; |
| | mjui_update(n, i, ui, state, con); |
| | } else if (it->type == mjITEM_CHECKBYTE) { |
| | *(mjtByte*)it->pdata = 1 - *(mjtByte*)it->pdata; |
| | mjui_update(n, i, ui, state, con); |
| | } |
| |
|
| | |
| | return it; |
| | } |
| |
|
| | |
| | else { |
| | return NULL; |
| | } |
| | } |
| | } |
| | } |
| | } |
| |
|
| | return NULL; |
| |
|
| | case mjEVENT_RESIZE: |
| | |
| | ui->scroll = mjMAX(0, mjMIN(ui->scroll, ui->height - state->rect[ui->rectid].height)); |
| | return NULL; |
| | } |
| |
|
| | |
| | return NULL; |
| | } |
| |
|
| |
|
| |
|
| | |
| | void mjui_render(mjUI* ui, const mjuiState* state, const mjrContext* con) { |
| | |
| | mjrRect rect = state->rect[ui->rectid]; |
| |
|
| | |
| | mjr_rectangle(rect, ui->color.master[0], ui->color.master[1], |
| | ui->color.master[2], 1); |
| |
|
| | |
| | if (ui->scroll > 0 && ui->height-ui->scroll < rect.height) { |
| | ui->scroll = mjMAX(0, ui->height - rect.height); |
| | } |
| |
|
| | |
| | mjrRect raux = {0, mjMAX(0, ui->height - ui->scroll - rect.height), |
| | ui->width, mjMIN(rect.height, ui->height - ui->scroll)}; |
| | mjr_blitAux(ui->auxid, raux, rect.left, |
| | rect.bottom + mjMAX(0, rect.height - ui->height + ui->scroll), con); |
| |
|
| | |
| | if (ui->height > rect.height) { |
| | |
| | mjrRect bar; |
| | mjrRect thumb; |
| | scrollrect(rect, ui, con, &bar, &thumb); |
| |
|
| | |
| | mjr_rectangle(thumb, ui->color.thumb[0], ui->color.thumb[1], |
| | ui->color.thumb[2], 1); |
| | } |
| |
|
| | |
| | if (ui->mousesect > 0 && ui->mouseitem >= 0) { |
| | |
| | const mjuiItem* it = ui->sect[ui->mousesect-1].item + ui->mouseitem; |
| |
|
| | |
| | if (it->type == mjITEM_SELECT) { |
| | |
| | int g_texthor = SCL(ui->spacing.texthor, con); |
| | int g_textver = SCL(ui->spacing.textver, con); |
| | int g_itemside = SCL(ui->spacing.itemside, con); |
| | int cellheight = con->charHeight + 2 * g_textver; |
| | int offset = mjMAX(0, rect.height - ui->height + ui->scroll) - |
| | mjMAX(0, ui->height - ui->scroll - rect.height); |
| |
|
| | |
| | mjrRect r = it->rect; |
| | r.left -= g_itemside; |
| | r.width += 2*g_itemside; |
| | r.height = it->multi.nelem * cellheight + g_itemside; |
| | r.bottom -= r.height; |
| | r.bottom += offset; |
| | r.left += rect.left; |
| | mjr_rectangle(r, ui->color.sectpane[0], |
| | ui->color.sectpane[1], ui->color.sectpane[2], 1); |
| |
|
| | |
| | r = it->rect; |
| | r.height = it->multi.nelem * cellheight; |
| | r.bottom -= r.height; |
| | r.bottom += offset; |
| | r.left += rect.left; |
| | mjr_rectangle(r, ui->color.select2[0], |
| | ui->color.select2[1], ui->color.select2[2], 1); |
| |
|
| | |
| | int k = findselect(it, ui, state, con); |
| | if (k >= 0) { |
| | mjrRect r1 = r; |
| | r1.bottom = r.bottom + (it->multi.nelem-1-k)*cellheight; |
| | r1.height = cellheight; |
| | mjr_rectangle(r1, ui->color.select[0], |
| | ui->color.select[1], ui->color.select[2], 1); |
| | } |
| |
|
| | |
| | initOpenGL(&rect, con); |
| | for (int k=0; k < it->multi.nelem; k++) { |
| | drawtext(it->multi.name[k], |
| | r.left+g_texthor - rect.left, |
| | r.bottom+g_textver+(it->multi.nelem-1-k)*cellheight, |
| | r.width-2*g_texthor, ui->color.fontactive, con); |
| | } |
| | } |
| | } |
| | } |
| |
|