2015-06-20 20:19:52 -04:00
|
|
|
|
|
|
|
#include "GLPanel.h"
|
2015-06-26 22:04:16 -04:00
|
|
|
#include "cubic_math.h"
|
2015-06-20 20:19:52 -04:00
|
|
|
|
2015-06-26 22:04:16 -04:00
|
|
|
GLPanel::GLPanel() : fillType(GLPANEL_FILL_SOLID), contentsVisible(true), transform(CubicVR::mat4::identity()) {
|
2015-06-20 20:19:52 -04:00
|
|
|
pos[0] = 0.0f;
|
|
|
|
pos[1] = 0.0f;
|
|
|
|
size[0] = 1.0f;
|
|
|
|
size[1] = 1.0f;
|
2015-06-21 11:58:14 -04:00
|
|
|
fill[0] = RGB(0.5,0.5,0.5);
|
|
|
|
fill[1] = RGB(0.1,0.1,0.1);
|
2015-06-22 22:07:46 -04:00
|
|
|
borderColor = RGB(0.8, 0.8, 0.8);
|
2015-06-26 22:04:16 -04:00
|
|
|
setCoordinateSystem(GLPANEL_Y_DOWN_ZERO_ONE);
|
2015-06-20 20:19:52 -04:00
|
|
|
}
|
|
|
|
|
2015-06-21 11:58:14 -04:00
|
|
|
void GLPanel::genArrays() {
|
|
|
|
if (fillType == GLPANEL_FILL_SOLID || fillType == GLPANEL_FILL_GRAD_X || fillType == GLPANEL_FILL_GRAD_Y) {
|
|
|
|
glPoints.reserve(2 * 4);
|
|
|
|
glPoints.resize(2 * 4);
|
|
|
|
glColors.reserve(3 * 4);
|
|
|
|
glColors.resize(3 * 4);
|
|
|
|
|
|
|
|
float pts[2 * 4] = {
|
2015-06-22 22:07:46 -04:00
|
|
|
min, min,
|
|
|
|
min, max,
|
|
|
|
max, max,
|
|
|
|
max, min
|
2015-06-21 11:58:14 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
RGB c[4];
|
|
|
|
|
|
|
|
if (fillType == GLPANEL_FILL_SOLID) {
|
|
|
|
c[0] = c[1] = c[2] = c[3] = fill[0];
|
|
|
|
} else if (fillType == GLPANEL_FILL_GRAD_X) {
|
|
|
|
c[0] = c[1] = fill[0];
|
|
|
|
c[2] = c[3] = fill[1];
|
|
|
|
} else if (fillType == GLPANEL_FILL_GRAD_Y) {
|
|
|
|
c[0] = c[3] = fill[0];
|
|
|
|
c[1] = c[2] = fill[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
float clr[3 * 4] = {
|
|
|
|
c[0].r, c[0].g, c[0].b,
|
|
|
|
c[1].r, c[1].g, c[1].b,
|
|
|
|
c[2].r, c[2].g, c[2].b,
|
|
|
|
c[3].r, c[3].g, c[3].b
|
|
|
|
};
|
|
|
|
|
|
|
|
glPoints.assign(pts, pts + (2 * 4));
|
|
|
|
glColors.assign(clr, clr + (3 * 4));
|
|
|
|
} else {
|
|
|
|
glPoints.reserve(2 * 8);
|
|
|
|
glPoints.resize(2 * 8);
|
|
|
|
glColors.reserve(3 * 8);
|
|
|
|
glColors.resize(3 * 8);
|
|
|
|
|
|
|
|
RGB c[8];
|
|
|
|
|
|
|
|
if (fillType == GLPANEL_FILL_GRAD_BAR_X) {
|
|
|
|
float pts[2 * 8] = {
|
2015-06-22 22:07:46 -04:00
|
|
|
min, min,
|
|
|
|
min, max,
|
|
|
|
mid, max,
|
|
|
|
mid, min,
|
2015-06-21 11:58:14 -04:00
|
|
|
|
2015-06-22 22:07:46 -04:00
|
|
|
mid, min,
|
|
|
|
mid, max,
|
|
|
|
max, max,
|
|
|
|
max, min
|
2015-06-21 11:58:14 -04:00
|
|
|
};
|
|
|
|
glPoints.assign(pts, pts + (2 * 8));
|
|
|
|
|
|
|
|
c[0] = c[1] = fill[0];
|
|
|
|
c[2] = c[3] = fill[1];
|
|
|
|
|
|
|
|
c[4] = c[5] = fill[1];
|
|
|
|
c[6] = c[7] = fill[0];
|
|
|
|
|
|
|
|
} else if (fillType == GLPANEL_FILL_GRAD_BAR_Y) {
|
|
|
|
float pts[2 * 8] = {
|
2015-06-22 22:07:46 -04:00
|
|
|
min, min,
|
|
|
|
min, mid,
|
|
|
|
max, mid,
|
|
|
|
max, min,
|
2015-06-21 11:58:14 -04:00
|
|
|
|
2015-06-22 22:07:46 -04:00
|
|
|
min, mid,
|
|
|
|
min, max,
|
|
|
|
max, max,
|
|
|
|
max, mid
|
2015-06-21 11:58:14 -04:00
|
|
|
};
|
|
|
|
glPoints.assign(pts, pts + (2 * 8));
|
|
|
|
|
|
|
|
c[0] = c[3] = fill[0];
|
|
|
|
c[1] = c[2] = fill[1];
|
|
|
|
|
|
|
|
c[4] = c[7] = fill[1];
|
|
|
|
c[5] = c[6] = fill[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
float clr[3 * 8] = {
|
|
|
|
c[0].r, c[0].g, c[0].b,
|
|
|
|
c[1].r, c[1].g, c[1].b,
|
|
|
|
c[2].r, c[2].g, c[2].b,
|
|
|
|
c[3].r, c[3].g, c[3].b,
|
|
|
|
c[4].r, c[4].g, c[4].b,
|
|
|
|
c[5].r, c[5].g, c[5].b,
|
|
|
|
c[6].r, c[6].g, c[6].b,
|
|
|
|
c[7].r, c[7].g, c[7].b
|
|
|
|
};
|
|
|
|
|
|
|
|
glColors.assign(clr, clr + (3 * 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 22:04:16 -04:00
|
|
|
|
2015-06-20 20:19:52 -04:00
|
|
|
void GLPanel::setViewport() {
|
|
|
|
GLint vp[4];
|
|
|
|
glGetIntegerv(GL_VIEWPORT, vp);
|
|
|
|
|
|
|
|
view[0] = vp[2];
|
|
|
|
view[1] = vp[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLPanel::setPosition(float x, float y) {
|
|
|
|
pos[0] = x;
|
|
|
|
pos[1] = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLPanel::setSize(float w, float h) {
|
|
|
|
size[0] = w;
|
|
|
|
size[1] = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
float GLPanel::getWidthPx() {
|
|
|
|
return size[0]*view[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
float GLPanel::getHeightPx() {
|
|
|
|
return size[1]*view[0];
|
|
|
|
}
|
|
|
|
|
2015-06-26 22:04:16 -04:00
|
|
|
|
|
|
|
void GLPanel::setCoordinateSystem(GLPanelCoordinateSystem coord_in) {
|
|
|
|
coord = coord_in;
|
|
|
|
|
|
|
|
if (coord == GLPANEL_Y_DOWN || coord == GLPANEL_Y_UP) {
|
|
|
|
min = -1;
|
|
|
|
mid = 0;
|
|
|
|
max = 1;
|
|
|
|
} else {
|
|
|
|
min = 0;
|
|
|
|
mid = 0.5;
|
|
|
|
max = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
genArrays();
|
|
|
|
}
|
|
|
|
|
2015-06-20 20:19:52 -04:00
|
|
|
void GLPanel::setFill(GLPanelFillType fill_mode) {
|
2015-06-21 11:58:14 -04:00
|
|
|
fillType = fill_mode;
|
|
|
|
genArrays();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLPanel::setFillColor(RGB color1) {
|
|
|
|
fill[0] = color1;
|
|
|
|
genArrays();
|
2015-06-20 20:19:52 -04:00
|
|
|
}
|
|
|
|
|
2015-06-21 11:58:14 -04:00
|
|
|
void GLPanel::setFillColor(RGB color1, RGB color2) {
|
|
|
|
fill[0] = color1;
|
|
|
|
fill[1] = color2;
|
|
|
|
genArrays();
|
|
|
|
}
|
|
|
|
|
2015-06-20 20:19:52 -04:00
|
|
|
void GLPanel::setMargin(float marg) {
|
|
|
|
margin.left = margin.right = margin.top = margin.bottom = marg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLPanel::setMargin(float margl, float margr, float margt, float margb) {
|
|
|
|
margin.left = margl;
|
|
|
|
margin.right = margr;
|
|
|
|
margin.top = margt;
|
|
|
|
margin.bottom = margb;
|
|
|
|
}
|
|
|
|
|
2015-06-22 22:07:46 -04:00
|
|
|
void GLPanel::setBorderColor(RGB clr) {
|
|
|
|
borderColor = clr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLPanel::setBorder(float bord) {
|
|
|
|
border.left = border.right = border.top = border.bottom = bord;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLPanel::setBorder(float bordl, float bordr, float bordt, float bordb) {
|
|
|
|
border.left = bordl;
|
|
|
|
border.right = bordr;
|
|
|
|
border.top = bordt;
|
|
|
|
border.bottom = bordb;
|
|
|
|
}
|
2015-06-20 20:19:52 -04:00
|
|
|
|
|
|
|
void GLPanel::addChild(GLPanel *childPanel) {
|
|
|
|
children.push_back(childPanel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLPanel::drawChildren() {
|
|
|
|
if (children.size()) {
|
|
|
|
std::vector<GLPanel *>::iterator panel_i;
|
|
|
|
|
|
|
|
for (panel_i = children.begin(); panel_i != children.end(); panel_i++) {
|
2015-06-26 22:04:16 -04:00
|
|
|
(*panel_i)->draw(transform, this);
|
2015-06-20 20:19:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLPanel::drawPanelContents() {
|
|
|
|
drawChildren();
|
|
|
|
}
|
|
|
|
|
2015-06-26 22:04:16 -04:00
|
|
|
void GLPanel::draw(CubicVR::mat4 transform_in, GLPanel *parent) {
|
|
|
|
using namespace CubicVR;
|
2015-06-22 22:07:46 -04:00
|
|
|
|
2015-06-26 22:04:16 -04:00
|
|
|
mat4 mCoord = mat4::identity();
|
2015-06-22 22:07:46 -04:00
|
|
|
|
2015-06-20 20:19:52 -04:00
|
|
|
if (!parent) {
|
|
|
|
if (coord == GLPANEL_Y_DOWN_ZERO_ONE) {
|
2015-06-26 22:04:16 -04:00
|
|
|
mCoord *= mat4::translate(-1.0f, 1.0f, 0.0f) * mat4::scale(2.0f, -2.0f, 2.0f);
|
2015-06-20 20:19:52 -04:00
|
|
|
}
|
|
|
|
if (coord == GLPANEL_Y_UP_ZERO_ONE) {
|
2015-06-26 22:04:16 -04:00
|
|
|
mCoord = mat4::translate(-1.0f, -1.0f, 0.0f) * mat4::scale(2.0f, 2.0f, 2.0f);
|
2015-06-20 20:19:52 -04:00
|
|
|
}
|
|
|
|
if (coord == GLPANEL_Y_DOWN) {
|
2015-06-26 22:04:16 -04:00
|
|
|
mCoord = mat4::scale(2.0f, 2.0f, 2.0f);
|
2015-06-20 20:19:52 -04:00
|
|
|
}
|
2015-06-26 22:04:16 -04:00
|
|
|
// if (coord == GLPANEL_Y_UP) {
|
|
|
|
// }
|
2015-06-20 20:19:52 -04:00
|
|
|
}
|
2015-06-26 22:04:16 -04:00
|
|
|
|
|
|
|
// compute local transform
|
|
|
|
localTransform = mCoord * mat4::translate(pos[0]+margin.left, pos[1]+margin.top, 0) *
|
|
|
|
mat4::scale(size[0]-(margin.left+margin.right), size[1]-(margin.top+margin.bottom), 0);
|
|
|
|
|
|
|
|
// compute global transform
|
|
|
|
transform = transform_in * localTransform;
|
|
|
|
|
|
|
|
glLoadMatrixf(transform);
|
|
|
|
|
|
|
|
// init view[]
|
|
|
|
setViewport();
|
|
|
|
|
|
|
|
// get min/max transform
|
|
|
|
vec4 vmin_t = mat4::vec4_multiply(vec4(min, min, 0, 1), transform);
|
|
|
|
vec4 vmax_t = mat4::vec4_multiply(vec4(max, max, 0, 1), transform);
|
|
|
|
|
|
|
|
// screen dimensions
|
2015-06-26 22:18:49 -04:00
|
|
|
vec2 vmin((vmin_t.x>vmax_t.x)?vmax_t.x:vmin_t.x, (vmin_t.y>vmax_t.y)?vmax_t.y:vmin_t.y);
|
|
|
|
vec2 vmax((vmin_t.x>vmax_t.x)?vmin_t.x:vmax_t.x, (vmin_t.y>vmax_t.y)?vmin_t.y:vmax_t.y);
|
2015-06-26 22:04:16 -04:00
|
|
|
|
|
|
|
// unit dimensions
|
|
|
|
vec2 umin = (vmin * 0.5) + vec2(1,1);
|
|
|
|
vec2 umax = (vmax * 0.5) + vec2(1,1);
|
|
|
|
|
|
|
|
// pixel dimensions
|
|
|
|
vec2 pdim((umax.x - umin.x) * view[0], (umax.y - umin.y) * view[1]);
|
|
|
|
|
|
|
|
std::cout << umin << " :: " << umax << " :: " << pdim << std::endl;
|
2015-06-26 22:18:49 -04:00
|
|
|
|
2015-06-21 11:58:14 -04:00
|
|
|
if (fillType != GLPANEL_FILL_NONE) {
|
2015-06-20 20:19:52 -04:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, &glPoints[0]);
|
|
|
|
glColorPointer(3, GL_FLOAT, 0, &glColors[0]);
|
|
|
|
|
|
|
|
glDrawArrays(GL_QUADS, 0, glPoints.size() / 2);
|
|
|
|
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glDisableClientState(GL_COLOR_ARRAY);
|
2015-06-22 22:07:46 -04:00
|
|
|
|
|
|
|
if (border.left || border.right || border.top || border.bottom) {
|
2015-06-26 22:18:49 -04:00
|
|
|
glLoadMatrixf(mat4::identity());
|
|
|
|
|
2015-06-22 22:07:46 -04:00
|
|
|
glEnable(GL_LINE_SMOOTH);
|
|
|
|
glColor3f(borderColor.r, borderColor.g, borderColor.b);
|
|
|
|
|
|
|
|
if (border.left) {
|
|
|
|
glLineWidth(border.left);
|
|
|
|
glBegin(GL_LINES);
|
2015-06-26 22:18:49 -04:00
|
|
|
glVertex2f(vmin.x, vmin.y);
|
|
|
|
glVertex2f(vmin.x, vmax.y);
|
2015-06-22 22:07:46 -04:00
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (border.right) {
|
|
|
|
glLineWidth(border.right);
|
|
|
|
glBegin(GL_LINES);
|
2015-06-26 22:18:49 -04:00
|
|
|
glVertex2f(vmax.x, vmin.y);
|
|
|
|
glVertex2f(vmax.x, vmax.y);
|
2015-06-22 22:07:46 -04:00
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (border.top) {
|
|
|
|
glLineWidth(border.top);
|
|
|
|
glBegin(GL_LINES);
|
2015-06-26 22:18:49 -04:00
|
|
|
glVertex2f(vmin.x, vmin.y);
|
|
|
|
glVertex2f(vmax.x, vmin.y);
|
2015-06-22 22:07:46 -04:00
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (border.bottom) {
|
|
|
|
glLineWidth(border.bottom);
|
|
|
|
glBegin(GL_LINES);
|
2015-06-26 22:18:49 -04:00
|
|
|
glVertex2f(vmin.x, vmax.y);
|
|
|
|
glVertex2f(vmax.x, vmax.y);
|
2015-06-22 22:07:46 -04:00
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
glDisable(GL_LINE_SMOOTH);
|
2015-06-26 22:18:49 -04:00
|
|
|
glLoadMatrixf(transform);
|
2015-06-22 22:07:46 -04:00
|
|
|
}
|
2015-06-20 20:19:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (contentsVisible) {
|
|
|
|
glPushMatrix();
|
|
|
|
drawPanelContents();
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
}
|
2015-06-21 11:58:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GLTestPanel::drawPanelContents() {
|
|
|
|
glColor3f(1.0,1.0,1.0);
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
glVertex2f(0, 0.5);
|
|
|
|
glVertex2f(1, 0.5);
|
|
|
|
glVertex2f(0.5, 0);
|
|
|
|
glVertex2f(0.5, 1);
|
|
|
|
|
|
|
|
glVertex2f(0.5, 1);
|
|
|
|
glVertex2f(0.48, 0.80);
|
|
|
|
glVertex2f(0.5, 1);
|
|
|
|
glVertex2f(0.52, 0.80);
|
|
|
|
|
|
|
|
glVertex2f(1, 0.5);
|
|
|
|
glVertex2f(0.90, 0.25);
|
|
|
|
glVertex2f(1, 0.5);
|
|
|
|
glVertex2f(0.90, 0.75);
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|