bool onEvent(Event::t e, GLV& g){ //Plot::onEvent(e,g); // "inherit" mouse/keyboard controls float gx = pixToGrid(0, g.mouse().xRel()); // convert mouse to grid position float gy = pixToGrid(1, g.mouse().yRel()); // convert mouse to grid position int cx = (gx*0.5+0.5) * P.size(0); // convert grid to cell position int cy = (gy*0.5+0.5) * P.size(1); // convert grid to cell position switch(e){ case Event::MouseDown: case Event::MouseDrag: if(P.inBounds(cx, cy)){ P.elem<float>(cx,cy,iz) = g.mouse().button() ? -1:1; } break; case Event::KeyDown: switch(g.keyboard().key()){ case 'c': P.assignAll(0); break; } break; default:; } return true; }
bool DropDown::ItemList::onEvent(Event::t e, GLV& g){ const Keyboard& k = g.keyboard(); const Mouse& m = g.mouse(); switch(e){ case Event::KeyDown: switch(k.key()){ case Key::Enter: case Key::Return: dd.setValue(getValue()); dd.onEvent(e,g); break; default:; } break; case Event::FocusLost: disable(Visible); break; case Event::MouseUp: //printf("setValue\n"); //if(Rect(w,h).containsPoint(m.xRel(), m.yRel())){ if(containsPoint(m.x(), m.y())){ dd.setValue(getValue()); dd.notify(&dd, Update::Action); //dd.onEvent(e,g); } disable(Visible); g.setFocus(&dd); return false; default:; } return ListView::onEvent(e,g); }
bool SearchBox::ItemList::onEvent(Event::t e, GLV& g){ const Keyboard& k = g.keyboard(); const Mouse& m = g.mouse(); switch(e){ case Event::KeyDown: switch(k.key()){ case Key::Enter: case Key::Return: //sb.setValue(getValue()); sb.onEvent(e,g); break; default:; } break; case Event::FocusLost: disable(Visible); break; case Event::MouseUp: // ItemList is in absolute coordinates if(containsPoint(m.x(), m.y())){ sb.setValue(getValue()); sb.cursorEnd(); sb.notify(&sb, Update::Action); sb.onEvent(e,g); } disable(Visible); g.setFocus(&sb); return false; default:; } return ListView::onEvent(e,g); }
bool NumberDialers::onEvent(Event::t e, GLV& g){ if(!Widget::onEvent(e,g)) return false; const Keyboard& k = g.keyboard(); const Mouse& m = g.mouse(); switch(e){ case Event::MouseDown:{ mAcc = 0; int oldDig = dig(); selectFromMousePos(g); float dxDig = font().advance('M'); int d = (m.xRel() - (dx() * selectedX() + paddingX())) / dxDig; // printf("%2d (%2d, %2d)\n", d, selectedX(), selectedY()); dig(d); if(dig() == 0 && oldDig == 0 && mShowSign) flipSign(); return false; } case Event::MouseDrag: if(onNumber()){ mAcc += 0.25f * fabs(m.dy()); if(mAcc > 1){ int mul = (int)mAcc; int digit = dig(); /*if(m.left() && m.right() && digit < (numDigits()-1)){ ++digit; }*/ valAdd((m.dy() > 0.f ? -mag(digit) : mag(digit))*mul); mAcc -= mul; } } return false; case Event::KeyDown: if(k.ctrl() || k.alt() || k.meta()){} else if(k.isNumber() && onNumber()){ int vali = valInt(selected()); int v = vali < 0 ? -vali : vali; int p = pow(10., numDigits()-1-dig()); v += (k.keyAsNumber() - ((v / p) % 10)) * p; v = vali < 0 ? -v : v; setValue(v * pow(10., -mNF)); if(!mOverwriteMode) dig(dig()+1); return false; } else{ switch(k.key()){ case 'a': onNumber() ? valAdd( mag()) : flipSign(); return false; case 'z': onNumber() ? valAdd(-mag()) : flipSign(); return false; case '-': flipSign(); return false; case 'c': setValue(0); return false; case '=': //case Key::Return: if(!mTextEntryMode){ // bring up the text entry box mTextEntryMode=true; mTextEntry.filter(TextView::filterNumeric).paddingY(2); (*this) << mTextEntry; mTextEntry.setValue(toString(getValue())); mTextEntry.selectAll(); g.setFocus(&mTextEntry); } else{ // set value from text entry box, then remove it mTextEntryMode=false; g.setFocus(this); mTextEntry.remove(); if(!mTextEntry.getValue().empty()){ setValue(mTextEntry.data().at<double>(0)); } } return false; case '.': dig(numDigits()-mNF); return false; // go to first fraction digit (if any) case Key::Left: case 's': dig(dig()-1); return false; case Key::Right: case 'd': dig(dig()+1); return false; case 'o': mOverwriteMode^=true; return false; } } break; default:; } return true; }
bool TextView::onEvent(Event::t e, GLV& g){ const Keyboard& k = g.keyboard(); int key = k.key(); float mx = g.mouse().xRel(); switch(e){ case Event::KeyDown: if(k.ctrl()){ switch(key){ case 'a': selectAll(); return false; } } else if(k.alt() || k.meta()){} // bubble if control key down else if(k.isPrint()){ // preserve current text if something is selected std::string oldText; int oldSel = mSel; int oldPos = mPos; if(mSel){ oldText = mText; deleteSelected(); } // No filter assigned or filter returns true if(!mFilter || mFilter(getValue(), mPos, key)){ mText.insert(mPos, 1, k.key()); setValue(mText); cursorPos(mPos+1); return false; } // restore old text if the new character is invalid if(!oldText.empty()){ mText = oldText; mPos = oldPos; mSel = oldSel; } } else{ switch(key){ case Key::Backspace: if(textSelected()) deleteSelected(); else if(validPos()){ deleteText(mPos-1, 1); cursorPos(mPos-1); } return false; case Key::Delete: if(textSelected()) deleteSelected(); else if(mText.size()){ deleteText(mPos, 1); cursorPos(mPos); } return false; case Key::Left: if(k.shift()) select(mSel-1); else cursorPos(mPos-1); return false; case Key::Right: if(k.shift()) select(mSel+1); else cursorPos(mPos+1); return false; case Key::Down: cursorPos(mText.size()); return false; case Key::Up: cursorPos(0); return false; case Key::Enter: case Key::Return: notify(this, Update::Action); return false; } } break; case Event::MouseDown: cursorPos(xToPos(mx)); case Event::MouseUp: return false; case Event::MouseDrag: { int p = xToPos(mx); if(p >= mPos) select(p-mPos+1); else select(p-mPos); //printf("%d\n", mSel); } return false; default:; } return true; }