pplugins/pui/pui.cpp

232 lines
5.3 KiB
C++

/*
Copyright 2013 Peter Nelson
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#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);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
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->active) {
this->WidgetGrabbed(this->active, this->mb);
}
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();
}
}
}
void PUi::OnClose()
{
this->close = true;
}
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);
}
static void onClose(PuglView *view)
{
PUi *pui = (PUi *)puglGetHandle(view);
pui->OnClose();
}
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);
puglSetCloseFunc(this->view, onClose);
}