Revision 10

ofxArgosUI/src/Controls/ofxArgosUI_XYPad.h (revision 10)
1
/***********************************************************************
2
 
3
 Copyright (c) 2009 Dimitri Diakopoulos, http://www.dimitridiakopoulos.com/
4
 === Google Summer of Code 2009 - NUI Group === 
5

6
 Portions Copyright (c) 2008, 2009 Memo Atkens, http://www.memo.tv/
7
 -> Based on ofxSimpleGuiToo
8
 
9
 Portions Copyright (c) 2008 Todd Vanderlin, http://toddvanderlin.com/
10
 -> Inspired by ofxSimpleGui API
11

12
	Redistribution and use in source and binary forms, with or without modification, 
13
	are permitted provided that the following conditions are met:
14

15
	1. Redistributions of source code must retain the above copyright notice, 
16
	this list of conditions and the following disclaimer.
17

18
	2. Redistributions in binary form must reproduce the above copyright notice, 
19
	this list of conditions and the following disclaimer in the documentation and/or 
20
	other materials provided with the distribution.
21

22
	3. The name of the author may not be used to endorse or promote products derived 
23
	from this software without specific prior written permission.
24

25
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
26
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
27
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
28
	ARE DISCLAIMED. IN NOEVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
29
	INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
30
	(INCLUDING, BUT NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
31
	LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
32
	AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33
	OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
34
	OF THE USE OF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35

36
	ToDo
37
	- Add visual indicators of last position on the side(almost like a slider), x and y
38

39
*************************************************************************/ 
40
#pragma once
41

42
#include "ofxArgosUI_Control.h"
43

44

45
class ofxArgosUI_Slider2d : public ofxArgosUI_Control {
46
public:
47
	ofPoint		*value;
48
	ofPoint		point, min, max;
49
	
50
	//--------------------------------------------------------------------- construct
51
	ofxArgosUI_Slider2d(string name, ofPoint* value, float xmin, float xmax, float ymin, float ymax) : ofxArgosUI_Control(name) {
52
		min.set(xmin, ymin);
53
		max.set(xmax, ymax);
54
		this->value = value;
55
		controlType = "Slider2D";
56
		setup();
57
	}
58
	
59
	void setup() {
60
		setSize(config->slider2DSize.x, config->slider2DSize.y + config->slider2DTextHeight);
61
		point.x = ofMap((*value).x, min.x, max.x, x, x+width); 
62
		point.y = ofMap((*value).y, min.y, max.y, y, y+height-config->slider2DTextHeight); 
63
	}
64

65
	void loadFromXML(ofxXmlSettings &XML) {
66
		value->set(XML.getValue("controls:" + controlType + "_" + key + ":valueX", 0.0f), XML.getValue("controls:" + controlType + "_" + key + ":valueY", 0.0f));
67
	}
68
	
69
	
70
	void saveToXML(ofxXmlSettings &XML) {
71
		XML.addTag(controlType + "_" + key);
72
		XML.pushTag(controlType + "_" + key);
73
		XML.addValue("name", name);
74
		XML.addValue("valueX", value->x);
75
		XML.addValue("valueY", value->y);
76
		XML.popTag();
77
	}
78
	
79
	
80
	//--------------------------------------------------------------------- set xy
81
	void set(float x, float y) {
82
		(*value).x = x;
83
		(*value).y = y;
84
	}
85
	void setMin(float x, float y) {
86
		min.x = x;
87
		min.y = y;
88
	}	
89
	void setMax(float x, float y) {
90
		max.x = x;
91
		max.y = y;
92
	}	
93

94
	//--------------------------------------------------------------------- mouse pressed
95
	void onPress(int x, int y, int button) {
96
		lock = true;
97
		point.set(x, y);
98
	}
99
	
100
	//--------------------------------------------------------------------- mouse dragged
101
	void onDragOver(int x, int y, int button) {
102
		if(lock) {
103
			point.set(x, y);
104
		}
105
	}
106
	
107
	void onDragOutside(int x, int y, int button) {
108
		if(lock) {
109
			point.set(x, y);
110
		}
111
	}	
112
	
113
	//--------------------------------------------------------------------- mouse released
114
	void onRelease() {
115
		lock = false;
116
	}
117
	
118
	//--------------------------------------------------------------------- update
119
	void update() {
120
		if(point.x > x + width)				point.x = x + width; 
121
		else if(point.x < x)				point.x = x; 
122
		
123
		if(point.y > y+height - config->slider2DTextHeight)			point.y = y + height - config->slider2DTextHeight;
124
		else if(point.y < y)				point.y = y;
125
		
126
		if(lock){
127
			(*value).x = ofMap(point.x, x, x+width, min.x, max.x); 
128
			(*value).y = ofMap(point.y, y, y+height-config->slider2DTextHeight, min.y, max.y); 
129
		}
130
	}
131
	
132
	//--------------------------------------------------------------------- draw
133
	void draw(float x, float y) {
134
		setPos(x, y);
135
		ofPoint	pointv;
136
		pointv.x = ofMap((*value).x, min.x, max.x, x, x+width);
137
		pointv.y = ofMap((*value).y, min.y, max.y, y, y+height-config->slider2DTextHeight);
138
		
139
		ofEnableAlphaBlending();
140
		glPushMatrix();
141
		glTranslatef(x, y, 0);		
142
		
143
		ofFill();
144
		setFullColor();
145
		ofRect(0, 0, width, height - config->slider2DTextHeight);
146
		
147
		ofFill();
148
		setTextBGColor();
149
		ofRect(0, height-config->slider2DTextHeight, width, config->slider2DTextHeight);
150

151
		setTextColor();
152
		ofDrawBitmapString(name+"\nx:"+ofToString(value->x, 2)+"\ny:"+ofToString(value->y, 2), 3, height+15-config->slider2DTextHeight);
153
		
154
		setTextColor();
155
		ofCircle(pointv.x-x, pointv.y-y, 2);
156

157
		setTextColor();
158
		ofLine(pointv.x-x, 0, pointv.x-x, height-config->slider2DTextHeight);
159
		ofLine(0, pointv.y-y,width, pointv.y-y);
160
		
161
		glPopMatrix();
162
		ofDisableAlphaBlending();
163
		
164
	}
165
	
166
};

Also available in: Unified diff