pplugins/pui/pui.cpp

197 lines
4.3 KiB
C++

#include <cstdio>
#include <pugl/pugl.h>
#include "pui.h"
PUi::~PUi()
{
delete this->widget;
}
void PUi::OnReshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
glOrtho(0, w * scale, h * scale, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
}
void PUi::OnDisplay()
{
if (!this->initialised) this->InitTextures();
glClear(GL_COLOR_BUFFER_BIT);
this->widget->OnPaint(this);
}
void PUi::OnKeyboard(bool press, uint32_t key)
{
printf("keyboard %d %X\n", press, key);
}
void PUi::OnSpecial(bool press, PuglKey key)
{
if (press) {
uint32_t port = (this->active ? this->active->port : 0);
uint32_t newport = port;
switch (key) {
case PUGL_KEY_LEFT: newport--; break;
case PUGL_KEY_RIGHT: newport++; break;
case PUGL_KEY_PAGE_UP: this->OnScroll(0, 2.5f); return;
case PUGL_KEY_PAGE_DOWN: this->OnScroll(0, -2.5f); return;
case PUGL_KEY_UP: this->OnScroll(0, 1); return;
case PUGL_KEY_DOWN: this->OnScroll(0, -1); return;
case PUGL_KEY_SHIFT: this->mm |= 1; break;
case PUGL_KEY_CTRL: this->mm |= 2; break;
default: break;
}
if (port != newport) {
if (this->active) this->active->active = false;
this->active = this->widget->GetWidget(newport);
if (this->active) this->active->active = true;
this->Repaint();
}
} else {
switch (key) {
case PUGL_KEY_SHIFT: this->mm &= ~1; break;
case PUGL_KEY_CTRL: this->mm &= ~2; break;
default: break;
}
}
printf("special %d %X\n", press, key);
}
void PUi::OnMotion(int x, int y)
{
x *= scale;
y *= scale;
if (!this->mb) {
/* No button */
Widget *w = this->widget->GetWidget(x, y);
if (this->active != w) {
if (this->active) this->active->active = false;
this->active = w;
if (this->active) this->active->active = true;
this->Repaint();
}
return;
}
if (this->active && this->mb == 2) {
/* Left button drag */
float value = this->active->GetNewValue(x, y);
if (this->active->SetValue(value)) {
this->ParameterChanged(this->active);
this->Repaint();
}
}
}
void PUi::OnMouse(int button, bool press, int x, int y)
{
x *= scale;
y *= scale;
/* Keep track of button state */
if (press) {
this->mb |= (1 << button);
} else {
this->mb &= ~(1 << button);
}
if (!this->mb) {
/* No button press, check for widget focus change */
Widget *w = this->widget->GetWidget(x, y);
if (this->active != w) {
if (this->active) this->active->active = false;
this->active = w;
if (this->active) this->active->active = true;
this->Repaint();
}
return;
}
if (this->active) {
/* RMB, zero value */
float value = (this->mb & 8) ? 0 : this->active->GetNewValue(x, y);
if (this->active->SetValue(value)) {
this->ParameterChanged(this->active);
this->Repaint();
}
}
}
void PUi::OnScroll(float dx, float dy)
{
if (this->active) {
dy *= .1f;
if (this->mm & 1) dy *= .1f;
if (this->mm & 2) dy *= .001f;
float value = this->active->value + dy;
if (this->active->SetValue(value)) {
this->ParameterChanged(this->active);
this->Repaint();
}
}
}
static void onReshape(PuglView *view, int width, int height)
{
PUi *pui = (PUi *)puglGetHandle(view);
pui->OnReshape(width, height);
}
static void onDisplay(PuglView *view)
{
PUi *pui = (PUi *)puglGetHandle(view);
pui->OnDisplay();
}
static void onKeyboard(PuglView *view, bool press, uint32_t key)
{
PUi *pui = (PUi *)puglGetHandle(view);
pui->OnKeyboard(press, key);
}
static void onSpecial(PuglView *view, bool press, PuglKey key)
{
PUi *pui = (PUi *)puglGetHandle(view);
pui->OnSpecial(press, key);
}
static void onMotion(PuglView *view, int x, int y)
{
PUi *pui = (PUi *)puglGetHandle(view);
pui->OnMotion(x, y);
}
static void onMouse(PuglView *view, int button, bool press, int x, int y)
{
PUi *pui = (PUi *)puglGetHandle(view);
pui->OnMouse(button, press, x, y);
}
static void onScroll(PuglView *view, float dx, float dy)
{
PUi *pui = (PUi *)puglGetHandle(view);
pui->OnScroll(dx, dy);
}
void PUi::SetFunc()
{
puglSetDisplayFunc(this->view, onDisplay);
puglSetReshapeFunc(this->view, onReshape);
puglSetKeyboardFunc(this->view, onKeyboard);
puglSetSpecialFunc(this->view, onSpecial);
puglSetMotionFunc(this->view, onMotion);
puglSetMouseFunc(this->view, onMouse);
puglSetScrollFunc(this->view, onScroll);
}