Explorar el Código

Color picker no longer jitters/resets when you change values, fixed offset on click in color picker SV area

Ivan Safrin hace 12 años
padre
commit
85bb585f6b

+ 4 - 1
Modules/Contents/UI/Include/PolyUIColorBox.h

@@ -42,7 +42,7 @@ namespace Polycode {
 			UIColorPicker();
 			UIColorPicker();
 			~UIColorPicker();
 			~UIColorPicker();
 			
 			
-			void updateSelectedColor(bool updateTextFields=true);
+			void updateSelectedColor(bool updateTextFields=true, bool updateHue = true, bool updateSV=true);
 			void setHue(Number hueNum);			
 			void setHue(Number hueNum);			
 			void handleEvent(Event *event);
 			void handleEvent(Event *event);
 			void setSaturationAndValue(Number S, Number V);
 			void setSaturationAndValue(Number S, Number V);
@@ -62,6 +62,8 @@ namespace Polycode {
 		
 		
 			Vector2 lastMainSelectorPosition;
 			Vector2 lastMainSelectorPosition;
 			Number lastHueSelectorPosition;
 			Number lastHueSelectorPosition;
+			
+			bool suppressTextChangeEvent;
 		
 		
 			Color selectedColor;
 			Color selectedColor;
 		
 		
@@ -102,6 +104,7 @@ namespace Polycode {
 				
 				
 		protected:
 		protected:
 		
 		
+		
 			bool listeningToPicker;
 			bool listeningToPicker;
 			UIColorPicker *colorPicker;
 			UIColorPicker *colorPicker;
 			
 			

+ 25 - 12
Modules/Contents/UI/Source/PolyUIColorBox.cpp

@@ -148,7 +148,7 @@ UIColorPicker::UIColorPicker() : UIWindow(L"", 300, 240) {
 	
 	
 	visible = false;
 	visible = false;
 	enabled = false;
 	enabled = false;
-	
+	suppressTextChangeEvent = false;
 }
 }
 
 
 void UIColorPicker::cancelColorListeners() {
 void UIColorPicker::cancelColorListeners() {
@@ -211,10 +211,11 @@ void UIColorPicker::setPickerColor(Color newColor) {
 void UIColorPicker::setSaturationAndValue(Number S, Number V) {
 void UIColorPicker::setSaturationAndValue(Number S, Number V) {
 	currentS = S;
 	currentS = S;
 	currentV = V;	
 	currentV = V;	
-	updateSelectedColor();
+	updateSelectedColor(true, false, false);
 }
 }
 
 
-void UIColorPicker::updateSelectedColor(bool updateTextFields) {
+void UIColorPicker::updateSelectedColor(bool updateTextFields, bool updateHue, bool updateSV) {
+	
 	selectedColor.setColorHSV(currentH, currentS, currentV);
 	selectedColor.setColorHSV(currentH, currentS, currentV);
 	selectedColor.a = colorAlpha;
 	selectedColor.a = colorAlpha;
 	
 	
@@ -226,9 +227,13 @@ void UIColorPicker::updateSelectedColor(bool updateTextFields) {
 	mainColorRect->color.a = colorAlpha;
 	mainColorRect->color.a = colorAlpha;
 	
 	
 	if(updateTextFields) {
 	if(updateTextFields) {
+		suppressTextChangeEvent = true;
 		rTextInput->setText(String::IntToString(newR));
 		rTextInput->setText(String::IntToString(newR));
+		suppressTextChangeEvent = true;		
 		gTextInput->setText(String::IntToString(newG));
 		gTextInput->setText(String::IntToString(newG));
+		suppressTextChangeEvent = true;		
 		bTextInput->setText(String::IntToString(newB));
 		bTextInput->setText(String::IntToString(newB));
+		suppressTextChangeEvent = true;		
 		aTextInput->setText(String::IntToString(newA));
 		aTextInput->setText(String::IntToString(newA));
 	}
 	}
 	
 	
@@ -243,11 +248,15 @@ void UIColorPicker::updateSelectedColor(bool updateTextFields) {
 	mainColorRect->getMesh()->arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;				
 	mainColorRect->getMesh()->arrayDirtyMap[RenderDataArray::COLOR_DATA_ARRAY] = true;				
 	
 	
 		
 		
-	hueSelector->setPositionY(hueFrame->getPosition().y + hueFrame->getHeight() - ((currentH/360.0) * hueFrame->getHeight()));
-	lastHueSelectorPosition = hueSelector->getPosition().y;
+	if(updateHue) {
+		hueSelector->setPositionY(hueFrame->getPosition().y + hueFrame->getHeight() - ((currentH/360.0) * hueFrame->getHeight()));
+		lastHueSelectorPosition = hueSelector->getPosition().y;
+	}
 	
 	
-	mainSelector->setPosition(mainColorRect->getPosition().x + (currentS * mainColorRect->getWidth()), mainColorRect->getPosition().y + mainColorRect->getHeight() - (currentV * mainColorRect->getHeight()));					
-	lastMainSelectorPosition = mainSelector->getPosition2D();
+	if(updateSV) {
+		mainSelector->setPosition(mainColorRect->getPosition().x + (currentS * mainColorRect->getWidth()), mainColorRect->getPosition().y + mainColorRect->getHeight() - (currentV * mainColorRect->getHeight()));					
+		lastMainSelectorPosition = mainSelector->getPosition2D();
+	}
 	
 	
 	alphaSlider->setSliderValue(colorAlpha);
 	alphaSlider->setSliderValue(colorAlpha);
 
 
@@ -264,7 +273,11 @@ void UIColorPicker::handleEvent(Event *event) {
 		switch(event->getEventCode()) {
 		switch(event->getEventCode()) {
 			case UIEvent::CHANGE_EVENT:
 			case UIEvent::CHANGE_EVENT:
 			{
 			{
-				rebuildFromTextInputs();
+				if(!suppressTextChangeEvent) {
+					rebuildFromTextInputs();
+				} else {
+					suppressTextChangeEvent = false;
+				}
 			}
 			}
 			break;
 			break;
 		}	
 		}	
@@ -309,8 +322,8 @@ void UIColorPicker::handleEvent(Event *event) {
 			case InputEvent::EVENT_MOUSEDOWN:
 			case InputEvent::EVENT_MOUSEDOWN:
 			{
 			{
 				InputEvent *inputEvent = (InputEvent*) event;
 				InputEvent *inputEvent = (InputEvent*) event;
-				mainSelector->setPosition(inputEvent->getMousePosition().x, inputEvent->getMousePosition().y);
-				mainSelector->startDrag(inputEvent->mousePosition.x-mainSelector->getPosition().x,inputEvent->mousePosition.y-mainSelector->getPosition().y);
+				mainSelector->setPosition(inputEvent->getMousePosition().x+mainColorRect->position.x, inputEvent->getMousePosition().y+mainColorRect->position.y);
+				mainSelector->startDrag(inputEvent->mousePosition.x-mainSelector->getPosition().x+mainColorRect->position.x,inputEvent->mousePosition.y-mainSelector->getPosition().y+mainColorRect->position.y);
 				
 				
 				Number newV = 1.0 - inputEvent->getMousePosition().y / mainColorRect->getHeight();
 				Number newV = 1.0 - inputEvent->getMousePosition().y / mainColorRect->getHeight();
 				Number newS = inputEvent->getMousePosition().x / mainColorRect->getWidth();
 				Number newS = inputEvent->getMousePosition().x / mainColorRect->getWidth();
@@ -337,7 +350,7 @@ void UIColorPicker::setHue(Number hueNum) {
 		hueNum = 359.9999;		
 		hueNum = 359.9999;		
 		
 		
 	currentH = hueNum;
 	currentH = hueNum;
-	updateSelectedColor();
+	updateSelectedColor(true, false, false);
 }
 }
 
 
 UIColorPicker::~UIColorPicker() {
 UIColorPicker::~UIColorPicker() {
@@ -369,7 +382,7 @@ void UIColorPicker::Update() {
 
 
 		Number newS = (newPosX - mainColorRect->getPosition().x) / mainColorRect->getWidth();
 		Number newS = (newPosX - mainColorRect->getPosition().x) / mainColorRect->getWidth();
 					
 					
-		setSaturationAndValue(newS, newV);		
+		setSaturationAndValue(newS, newV);	
 	}
 	}
 	
 	
 	if(hueSelector->getPosition().y != lastHueSelectorPosition) {
 	if(hueSelector->getPosition().y != lastHueSelectorPosition) {