Example #1
0
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 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;
	}
Example #3
0
int main(){

	// Create the Views
	GLV glv;
	View v(Rect(100,100, 600,400));
	View v1(Rect(10,10, 300,200)), v2(Rect(v1.right()+10,10, 100,200));
	View v11(Rect(20,20, 80,100)), v12(Rect(80,80,100,80));

	// Create the tree hierarchy 
	glv << v;
	v << v1 << v2;
	v1 << v11 << v12;
	
	// Set properties of Views	
	View* views[] = {&v, &v1, &v2, &v11, &v12};
	for(int i=0; i<5; ++i){
		views[i]->addHandler(Event::MouseDrag, Behavior::mouseMove);
		//views[i]->enable(KeepWithinParent);
	}
	
	// Disable some of the default View properties
	v.disable(DrawBack);
	v2.disable(DrawBorder);
	v12.disable(FocusHighlight);
	
	// Set color styles
	glv.cloneStyle().colors().set(StyleColor::WhiteOnBlack);
	v1.colors().set(Color(0.2,0.4,1,0.8), 0.7);
			
	Window win(800,600, "Views", &glv);

	Application::run();
}
int main (int argc, char ** argv){
	GLV top;
	top.colors().set(Color(HSV(0.6,0.5,0.5), 0.9), 0.4);
	top << scene;

	Window win(800, 600, "Space Curve", &top);
	Application::run();
}
	bool onEvent(Event::t e, GLV& g){
		if(e == Event::KeyDown){
			switch(g.keyboard().key()){
				case '`': toggle(Visible); return false;
			}
		}
		if (e == Event::MouseDrag){
			if (g.keyboard().ctrl()) printf("ctrl");
			if (g.keyboard().shift()) printf("shift");
		}
		return true;
	}
Example #6
0
bool ListView::onEvent(Event::t e, GLV& g){

	if(!Widget::onEvent(e,g)) return false;

	switch(e){
	case Event::MouseDown:
	case Event::MouseDrag:
		selectFromMousePos(g);
		return false;

//		case Event::MouseMove:
//			if(containsPoint(g.mouse().xRel(), g.mouse().yRel())){
//				selectFromMousePos(g);
//				return false;
//			}
//			break;
	
	case Event::KeyDown:
		switch(g.keyboard().key()){
			case Key::Enter:
			case Key::Return:
				notify(this, Update::Action);
//				printf("action on %d\n", selected());
				return false;
		default:;
		}
		break;
	
	default:;
	}
	

	return true;
}
Example #7
0
void ListView::onDraw(GLV& g){

	using namespace glv::draw;

	Indexer idx(data().size(0), data().size(1));
	float dx_ = dx(0);
	float dy_ = dy(1);
	
	while(idx()){
		int ix = idx[0];
		int iy = idx[1];
		
		float px = dx_ * ix;
		float py = dy_ * iy;

		if(selectedX() == ix && selectedY() == iy){
			color(colors().selection);
			draw::rectangle(px,py, px+dx_,py+dy_);
		}
		
		color(colors().text);
		lineWidth(1);
		
		//font().render(data().at<std::string>(ix,iy).c_str(), pixc(px+paddingX()), pixc(py+paddingY()));
		font().render(g.graphicsData(), data().at<std::string>(ix,iy).c_str(), px+paddingX(), py+paddingY());
	}
	
	Widget::onDraw(g);
}
Example #8
0
void Plot::onDraw(GLV& g){

	GraphicsData& gd = g.graphicsData();
	draw::color(colors().fore);

	{ pushGrid();
		for(unsigned i=0; i<plottables().size(); ++i){
			Plottable& p = *plottables()[i];
			if(mActive[i] && p.drawUnderGrid()){
				gd.reset();
				gd.colors()[0] = p.color();
				p.doPlot(gd, p.data().hasData() ? p.data() : data());
			}
		}

	popGrid(); }

	Grid::onDraw(g);

	draw::color(colors().fore);

	// push into grid space and call attached plottables
	{ pushGrid();
		for(unsigned i=0; i<plottables().size(); ++i){
			Plottable& p = *plottables()[i];
			if(mActive[i] && !p.drawUnderGrid()){
				gd.reset();
				gd.colors()[0] = p.color();
				p.doPlot(gd, p.data().hasData() ? p.data() : data());
			}
		}
	popGrid(); }
}
Example #9
0
int main(){

	// Create the Views
	GLV top;
	
	top.colors().set(StyleColor::WhiteOnBlack);
	//top.colors().set(StyleColor::SmokeyGray);

	//Grid v1(Rect(700,500));
	//v1.pos(Place::CC).anchor(0.5,0.5);
	
	Grid v1(Rect(0,0));
	v1.stretch(1,1);
	//v1.disable(CropSelf);
	top << v1;
	
	Window win(800,800, "Grid", &top);

	Application::run();
}
Example #10
0
void Label::onDraw(GLV& g){
	using namespace glv::draw;
	lineWidth(stroke());
	color(colors().text);
	if(mVertical){ translate(0,h); rotate(0,0,-90); }
	font().render(
		g.graphicsData(),
		data().toString().c_str(),
		paddingX(),
		paddingY()
	);
	//scale(mSize, mSize);
	//text(value().c_str());
}
Example #11
0
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);
}
Example #12
0
bool Plot::onEvent(Event::t e, GLV& g){
	if(Grid::onEvent(e,g)){
		
		const Keyboard& k = g.keyboard();
		
		if(e == Event::KeyDown){
			if(k.ctrl() && k.alt() && k.isNumber()){
				unsigned i = k.keyAsNumber();
				i = i>0 ? i-1 : 10; 
				if(i < plottables().size() && plottables()[i]){
					mActive[i] ^= 1;
				}
				return false;
			}
		}
		
		return true;
	}
	return false;
}
Example #13
0
	virtual void onDraw3D(GLV& g){
		using namespace glv::draw;
		translateZ(-3);
		rotateY(angle+=1);
		const int n=12;

		GraphicsData& buf = g.graphicsData();

		for(int i=0; i<n; ++i){
			float p = float(i)/n;
			float x = cos(p*6.283)*1.2;
			float z = sin(p*6.283)*1.2;
			
			buf.addVertex(x, -0.7, z);
			buf.addVertex(x, +0.7, z);
			buf.addColor(HSV(p,1,1));
			buf.addColor(0);
		}

		paint(Triangles, buf);
	}
Example #14
0
void TextView::onDraw(GLV& g){
	using namespace draw;

	float padX = paddingX();
	float padY = paddingY();
	float addY =-4;//was -2		// subtraction from top since some letters go above cap

	float tl = mPos * font().advance('M') + padX;
//	float tr = tl + font().advance('M');
	float tt = addY + padY;
	float tb = tt + fabs(font().descent()+font().cap()) - addY;
	float strokeWidth = 1;
	
	// draw selection
	if(textSelected()){
		float sl, sr;
		if(mSel>0){
			sl = tl;
			sr = sl + mSel*font().advance('M');
		}
		else{
			sr = tl;
			sl = sr + mSel*font().advance('M');
		}
		color(colors().selection);
		rectangle(sl, tt, sr, tb);
	}

	// draw cursor
	if(mBlink<0.5 && enabled(Focused)){
		stroke(1);
		color(colors().text);
		shape(Lines, pixc(tl), tt, pixc(tl), tb);
	}

	draw::lineWidth(strokeWidth);
	color(colors().text);
//	font().render(mText.c_str(), pixc(padX), pixc(padY-1));
	font().render(g.graphicsData(), mText.c_str(), padX, padY-1);
}
Example #15
0
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;
}
bool keyDownCB(View * v, GLV& glv){
	switch(glv.keyboard().key()){
		case '`': glv.toggle(Visible); break;
	}
	return false;
}
Example #17
0
void DropDown::hideList(GLV& g){
	mItemList.disable(Visible);
	g.setFocus(this);
}
Example #18
0
bool DropDown::onEvent(Event::t e, GLV& g){
//printf("DropDown::onEvent %s\n", toString(e));

/*
	From ListSelect:

		switch(e){
		case Event::KeyDown:
			if(!k.ctrl() && !k.alt() && isgraph(k.key())){
				char c = tolower(k.key());
				for(unsigned i=0; i<mItems.size(); ++i){
					if(mItems[i][0] == c){
						select(i); break;
					}
				}
				return false;
			}
			else{
				switch(k.key()){
				case Key::Up:	select(selected()-1); return false;
				case Key::Down:	select(selected()+1); return false;
				default:;
				}
			}
			break;
		case Event::MouseDown:
			return false;
		case Event::KeyUp:
		case Event::MouseUp: return false;

		case Event::MouseDrag:{
			int dy = m.y() - m.y(m.button());
			int inc = ((dy+800000) % 8 == 0) * (dy<0?-1:1);
			select(selected()+inc);
			return false;
		}
			
		default:;
		}
		return true;
	}

*/

	const Keyboard& k = g.keyboard();

	switch(e){
	case Event::KeyDown:
		// bubble if modifier key down
		if(k.ctrl() || k.alt() || k.meta()){}
		
		// printable keys search list and select first matching item
		else if(k.isPrint()){
			unsigned char lkey = tolower(k.key());
			for(unsigned i=0; i<items().size(); ++i){
				if(lkey == tolower(items()[i][0])){
					mItemList.select(i);
					showList();
					break;
				}
			}
			return false;
		}
		
		// switch on other keys...
		else{
			switch(k.key()){
			case Key::Down:
			case Key::Up:
				if(!mItemList.visible()){
					mItemList.selectValue(getValue());
				}
				else{ // pass on up/down arrows to item list...
					mItemList.onEvent(e,g);
				}
				showList();
				return false;
			case Key::Enter:
			case Key::Return:
				if(mItemList.visible()) setValue(mItemList.getValue());
				hideList(g);
				return TextView::onEvent(e,g); // let base class handle it from here
			default:;
			}
		}
		break;
	case Event::MouseUp:
		return false;
	case Event::MouseDown:
		if(mItemList.visible())	hideList(g);
		else{					showList(); //g.setFocus(&mItemList); // momentary
		}
		return false;
	case Event::FocusLost:
		if(!mItemList.enabled(Focused)) mItemList.disable(Visible);
		return false;
	default:;
	}

	return true;
}
Example #19
0
bool SearchBox::onEvent(Event::t e, GLV& g){

//	printf("SearchBox::onEvent %s\n", toString(e));

	bool showList = true;
	const Keyboard& k = g.keyboard();

	switch(e){
	case Event::KeyDown:
		switch(k.key()){
		case Key::Down:	// pass on up/down arrows to search list...
		case Key::Up:
			mItemList.onEvent(e,g);
			return false;
		case Key::Enter:
		case Key::Return:
			if(mItemList.visible()){
				setValue(mItemList.getValue());
				cursorEnd();
			}
			mItemList.disable(Visible);
			showList = false;
			break; // pass event on to TextView
			//return false;
		case Key::Tab:
			if(!empty()){
				const std::string& tstr = getValue();
				for(unsigned i=0; i<mItems.size(); ++i){
					const std::string& s = mItems[i];
					int r = strncmp(s.c_str(), tstr.c_str(), tstr.size());
					if(0 == r){
						setValue(s);
						cursorEnd();
						break;
					}
				}
			}
			return false;
		default:;
		}
		break;
	case Event::FocusLost:
		if(!mItemList.enabled(Focused)) mItemList.disable(Visible);
		return false;
	default:;
	}
	
	bool res = TextView::onEvent(e,g);

	if(Event::KeyDown == e || Event::MouseDown == e){

		mItemList.disable(Visible);
		if(showList && !empty()){
			std::vector<std::string> listItems;
			const std::string& tstr = getValue();
			for(unsigned i=0; i<mItems.size(); ++i){
				const std::string& s = mItems[i];
				//int r = strncasecmp(s.c_str(), tstr.c_str(), tstr.size());
				size_t res = s.find(tstr);
				if(res != std::string::npos){
//					printf("%s, %d\n", s.c_str(), res);
					listItems.push_back(s);
				}
			}

			if(listItems.size()){
				if(!((listItems.size() == 1) && (listItems[0].size() == tstr.size()))){
					space_t ax = 0, ay = height();
					const View& top = *toAbs(ax, ay);
					
					mItemList.data().resize(1, listItems.size());
					mItemList.data().assignFromArray(&listItems[0], listItems.size());
					mItemList.clipIndices();
					mItemList.fitExtent();

					if(ay > top.height()/2){
						ay -= mItemList.height() + height();
					}

					mItemList.pos(ax,ay);
					mItemList.enable(Visible);
					mItemList.bringToFront();
				}
			}
			if(mItemList.parent == 0) root() << mItemList;			
		}
	}

	return res;
}
Example #20
0
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;
}
Example #21
0
void NumberDialers::onDraw(GLV& g){ //printf("% g\n", value());
	using namespace glv::draw;

	fitExtent();

	float dxCell= dx();
	float dyCell= dy();
	float dxDig = font().advance('M');

//	View::enable(DrawSelectionBox);
//	View::enable(DrawGrid);

	// draw box at position (only if focused)
	if(enabled(Focused)){

		float x = dxCell*selectedX() + paddingX()/1 - 1;
		//float y = dyCell*selectedY() + paddingY()/2;
		float y = dyCell*(selectedY()+0.5);
		float ty= font().cap()/2. + 3;

//		color(colors().fore, colors().fore.a*0.4);
		color(colors().selection);
		//rectangle(bx + dig()*dxDig, by, bx + (dig()+1)*dxDig, by + dyCell-0.5f);
		rectangle(x + dig()*dxDig, y-ty, x + (dig()+1)*dxDig, y+ty);
	}

	drawSelectionBox();
	drawGrid(g.graphicsData());

	lineWidth(1);

	if(mTextEntryMode){
		mTextEntry.extent(dxCell, dyCell);
		mTextEntry.pos(dxCell*selectedX(), dyCell*selectedY());
	}

	for(int i=0; i<sizeX(); ++i){
		for(int j=0; j<sizeY(); ++j){

			float cx = dxCell*i;	// left edge of cell
			float cy = dyCell*j;	// top edge of cell

			// draw number
			int vali = valInt(i,j);
			int absVal = vali < 0 ? -vali : vali;
			int msd = mNF;	// position from right of most significant digit

			if(absVal > 0){
				msd = (int)log10((double)absVal);
				int p = numDigits() - (mShowSign ? 2:1);
				msd = msd < mNF ? mNF : (msd > p ? p : msd);
			}

			if(mNI == 0) msd-=1;

			// Determine digit string
			char str[32];
			int ic = numDigits();
			str[ic] = '\0';
			for(int i=0; i<numDigits(); ++i) str[i]=' ';

			int power = 1;
			bool drawChar = false; // don't draw until non-zero or past decimal point

			if(mShowSign && vali < 0) str[0] = '-';

			for(int i=0; i<=msd; ++i){
				char c = '0' + (absVal % (power*10))/power;
				power *= 10;
				if(c!='0' || i>=mNF) drawChar = true;
				--ic;
				if(drawChar) str[ic] = c;
			}

			// Draw the digit string
			float tx = int(cx + paddingX());
			float ty = int(cy + paddingY());

			if(vali || !dimZero()){
				color(colors().text);
			} else {
				color(colors().text.mix(colors().back, 0.8));
			}
		//	printf("%s\n", str);
//			font().render(g.graphicsData(), str, pixc(tx), pixc(ty));
//			if(mNF>0) font().render(g.graphicsData(), ".", pixc(dxDig*(mNI+numSignDigits()-0.5f) + tx), pixc(ty));
			font().render(g.graphicsData(), str, tx, ty);
			if(mNF>0) font().render(g.graphicsData(), ".", dxDig*(mNI+numSignDigits()-0.5f) + tx, ty);
		}
	}
}