fysxasteroids/engine/IMGUIControls.cc

101 lines
2.2 KiB
C++

#include "IMGUIControls.h"
#include "Engine.h"
#include "ControllerBase.h"
#include "ViewBase.h"
#include <GL/gl.h>
#include "DrawingsGL.h"
#include "keytable.h"
Engine::ControllerBase *controller = NULL;
Engine::ViewBase *view = NULL;
namespace Engine {
namespace GUI {
/** \brief Checks whether the mouse is in the given rectangle */
bool regionhit (int x, int y, int w, int h) {
controller = EngineGetController();
assert (controller);
int mouse_pos[2];
controller->GetMouseScreenPosition(mouse_pos);
if (mouse_pos[0] < x ||
mouse_pos[1] < y ||
mouse_pos[0] >= x + w ||
mouse_pos[1] >= y + h)
return false;
return true;
}
void Label (int id, const char* caption, int x, int y) {
if (caption != NULL) {
float width, height;
view = EngineGetView ();
assert (view);
glColor3f (1., 1., 1.);
view->DrawGLStringMeasure(caption, &width, &height);
view->DrawGLString(x , y + height * 0.5, caption);
}
}
/** \brief Draws a button at the given position
*
* \returns true if it was clicked
*
*/
bool Button (int id, const char* caption, int x, int y, int w, int h) {
controller = EngineGetController();
assert (controller);
// Check for hotness
if (regionhit (x, y, w, h)) {
controller->uistate.hotitem = id;
if (controller->uistate.activeitem == 0
&& controller->GetButtonState(MouseButtonLeft))
controller->uistate.activeitem = id;
}
// Render
glColor3f (0.2, 0.2, 0.2);
DrawRect2D (x + 4, y + 4, w, h);
if (controller->uistate.hotitem == id) {
if (controller->uistate.activeitem == id) {
glColor3f (0.8, 0.8, 0.8);
DrawRect2D (x, y, w, h);
} else {
glColor3f (0.7, 0.7, 0.7);
DrawRect2D (x, y, w, h);
}
} else {
glColor3f (0.4, 0.4, 0.4);
DrawRect2D (x, y, w, h);
}
// Caption
if (caption != NULL) {
float width, height;
view = EngineGetView ();
assert (view);
glColor3f (1., 1., 1.);
view->DrawGLStringMeasure(caption, &width, &height);
view->DrawGLString(x + w * 0.5 - width * 0.5, y + h * 0.5 - height * 0.5, caption);
}
// Logic
if (controller->GetButtonState(MouseButtonLeft) == false
&& controller->uistate.hotitem == id
&& controller->uistate.activeitem == id)
return true;
return false;
}
};
};