00001 #include "VSlider.h"
00002 #include <GL/glew.h>
00003 #include <GL/glut.h>
00004 #include "vmath.h"
00005 #include <math.h>
00006 #include <iostream>
00007
00008 using namespace std;
00009
00010 VSlider::VSlider(float initialvalue, int x1, int y1, int x2, int y2, float * alph)
00011 {
00012 this->value = initialvalue;
00013 this->begin = Vector2<float>(x1,y1);
00014 this->end = Vector2<float>(x2,y2);
00015 this->offset = 10;
00016 this->alpha = alph;
00017 this->color = Vector3<float>(1.0,0.0,0.0);
00018 }
00019
00020 VSlider::~VSlider(void){
00021 }
00022
00023 void VSlider::render() {
00024
00025 glLineWidth(2);
00026 glEnable(GL_LINE_SMOOTH);
00027 glBegin(GL_LINES);
00028 glColor4f(this->color.x,this->color.y,this->color.z,*this->alpha);
00029 glVertex2i(this->begin.x, this->begin.y);
00030 glVertex2i(this->end.x, this->end.y);
00031 glEnd();
00032
00033 Vector2<float> pos = this->begin.lerp(this->value,this->end);
00034 glLineWidth(7);
00035 glBegin(GL_LINES);
00036 glColor4f(this->color.x,this->color.y,this->color.z,*this->alpha);
00037 glVertex2i(pos.x-this->offset,pos.y);
00038 glVertex2i(pos.x+this->offset,pos.y);
00039 glEnd();
00040 glLineWidth(1);
00041 glDisable(GL_LINE_SMOOTH);
00042
00043 glColor4f(1.0,1.0,1.0,*this->alpha);
00044 char prozent[20];
00045 char * ch;
00046 _snprintf_s(prozent, 20, "%.2f", this->value);
00047 glRasterPos3f(pos.x-this->offset-27, pos.y+3, 0.0);
00048 for(ch = prozent; *ch; ch++) {
00049 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, (int)*ch);
00050 }
00051 }
00052
00053 bool VSlider::isInside(int xm,int ym) {
00054 Vector2<float> pos = this->begin.lerp(this->value,this->end);
00055 Vector2<float> mouse(xm,ym);
00056 return (pos-mouse).length() < this->offset;
00057 }
00058
00059 float * VSlider::getValue() {
00060 return &this->value;
00061 }
00062
00063 void VSlider::pressed(int xm,int ym) {
00064 this->fromPoint.x = xm;
00065 this->fromPoint.y = ym;
00066 }
00067
00068 void VSlider::drag(int xm,int ym) {
00069 Vector2<float> toPoint(xm,ym);
00070 Vector2<float> diffPoint = toPoint - fromPoint;
00071
00072 float maxlength = (this->begin-this->end).length();
00073 float newvalue = (diffPoint.length()/maxlength);
00074
00075 float sum = diffPoint[0] + diffPoint[1];
00076 if(sum >= 0) {
00077 if(this->value-newvalue >= 0.0)
00078 this->value -= newvalue;
00079 } else {
00080 if(this->value+newvalue <= 1.0)
00081 this->value += newvalue;
00082 }
00083
00084 fromPoint = toPoint;
00085 }
00086
00087 void VSlider::setValue(float val) {
00088 if(val >= 0.0 && val <= 1.0)
00089 this->value = val;
00090 }
00091
00092 void VSlider::released(int xm,int ym) {
00093 this->fromPoint.x = 0;
00094 this->fromPoint.y = 0;
00095 }