/src/RButton.cpp
#include <GLES2/gl2.h>
#include <SDL.h>
#include "RButton.h"
#include "ShaderDepot.h"
RButton::RButton(int x, int y, int w, int h) {
setRect(x, y, w, h);
this->setColor(0.0, 0.0, 1.0);
this->setFadeColor(1.0, 1.0, 1.0);
this->lastPress = 0;
this->shader = (RShader*) loadShader("RShader");
}
RButton::~RButton(void) {
}
void RButton::setRect(int x, int y, int w, int h) {
vertices[0] = x;
vertices[1] = y;
vertices[2] = x + w;
vertices[3] = y;
vertices[4] = x + w;
vertices[5] = y + h;
vertices[6] = x;
vertices[7] = y + h;
}
bool RButton::testHit(int x, int y) {
bool r = (x > vertices[0] && x < vertices[2] && y > vertices[1] && y < vertices[5]);
if (r)
this->press();
return r;
}
void RButton::press() {
this->lastPress = SDL_GetTicks();
}
void RButton::update(int t) {
}
void RButton::draw(int t) {
shader->useProgram();
shader->setPositions(4, vertices);
shader->setColor(&color);
shader->setFadeColor(&fadeColor);
int delta = t - lastPress;
if (delta < PRESS_FADE)
shader->setFade(1.0 - (GLfloat)delta / (GLfloat)PRESS_FADE);
else
shader->setFade(0.0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
void RButton::setColor(float r, float g, float b) {
this->color.r = r;
this->color.g = g;
this->color.b = b;
this->color.a = 1.0;
}
void RButton::setFadeColor(float r, float g, float b) {
this->fadeColor.r = r;
this->fadeColor.g = g;
this->fadeColor.b = b;
this->fadeColor.a = 1.0;
}