#include #include #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, h, 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(); } void PUi::OnMotion(int x, int y) { 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) { /* 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) { float value = this->active->value + (dy * .1f); 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) { printf("keyboard %d %X\n", press, key); } static void onSpecial(PuglView *view, bool press, PuglKey key) { printf("special %d %X\n", 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); }