浏览代码

Allow defining active areas for sliders. Fixes settings window.

Ivan Savenko 2 年之前
父节点
当前提交
507d8bf7fd

+ 9 - 1
client/gui/InterfaceObjectConfigurable.cpp

@@ -372,7 +372,15 @@ std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode
 	auto itemsTotal = config["itemsTotal"].Integer();
 	auto value = config["selected"].Integer();
 	bool horizontal = config["orientation"].String() == "horizontal";
-	return std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
+	auto const & result = std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
+
+	if (!config["scrollBounds"].isNull())
+	{
+		Rect bounds = readRect(config["scrollBounds"]);
+		result->setScrollBounds(bounds);
+	}
+
+	return result;
 }
 
 std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const

+ 24 - 2
client/widgets/Buttons.cpp

@@ -575,7 +575,7 @@ void CSlider::mouseMoved (const Point & cursorPosition)
 	v += 0.5;
 	if(v!=value)
 	{
-		moveTo((int)v);
+		moveTo(static_cast<int>(v));
 	}
 }
 
@@ -584,6 +584,16 @@ void CSlider::setScrollStep(int to)
 	scrollStep = to;
 }
 
+void CSlider::setScrollBounds(const Rect & bounds )
+{
+	scrollBounds = bounds;
+}
+
+void CSlider::clearScrollBounds()
+{
+	scrollBounds = boost::none;
+}
+
 int CSlider::getAmount() const
 {
 	return amount;
@@ -775,7 +785,19 @@ void CSlider::showAll(SDL_Surface * to)
 
 void CSlider::wheelScrolled(bool down, bool in)
 {
-	moveTo(value + 3 * (down ? +scrollStep : -scrollStep));
+	if (scrollBounds)
+	{
+		Rect testTarget = *scrollBounds + pos.topLeft();
+
+		if (!testTarget.isInside(GH.getCursorPosition()))
+			return;
+	}
+
+	// vertical slider -> scrolling up move slider upwards
+	// horizontal slider -> scrolling up moves slider towards right
+	bool positive = (down != horizontal);
+
+	moveTo(value + 3 * (positive ? +scrollStep : -scrollStep));
 }
 
 void CSlider::keyPressed(const SDL_Keycode & key)

+ 6 - 0
client/widgets/Buttons.h

@@ -229,6 +229,8 @@ class CSlider : public CIntObject
 	std::shared_ptr<CButton> right;
 	std::shared_ptr<CButton> slider;
 
+	boost::optional<Rect> scrollBounds;
+
 	int capacity;//how many elements can be active at same time (e.g. hero list = 5)
 	int positions; //number of highest position (0 if there is only one)
 	bool horizontal;
@@ -252,6 +254,10 @@ public:
 	/// Controls how many items wil be scrolled via one click
 	void setScrollStep(int to);
 
+	/// If set, mouse scroll will only scroll slider when inside of this area
+	void setScrollBounds(const Rect & bounds );
+	void clearScrollBounds();
+
 	/// Value modifiers
 	void moveLeft();
 	void moveRight();

+ 1 - 1
client/windows/settings/GeneralOptionsTab.cpp

@@ -147,7 +147,7 @@ GeneralOptionsTab::GeneralOptionsTab()
 	musicVolumeLabel->setText(std::to_string(CCS->musich->getVolume()) + "%");
 
 	std::shared_ptr<CLabel> soundVolumeLabel = widget<CLabel>("soundValueLabel");
-	musicVolumeLabel->setText(std::to_string(CCS->soundh->getVolume()) + "%");
+	soundVolumeLabel->setText(std::to_string(CCS->soundh->getVolume()) + "%");
 
 }
 

+ 2 - 0
config/widgets/settings/generalOptionsTab.json

@@ -138,6 +138,7 @@
 			"name": "musicSlider",
 			"type": "slider",
 			"position": {"x": 385, "y": 115},
+			"scrollBounds" : { "x" : -4, "y" : -34, "w" : 208, "h" : 52 },
 			"size": 200,
 			"style": "brown",
 			"orientation": "horizontal",
@@ -165,6 +166,7 @@
 			"name": "soundVolumeSlider",
 			"type": "slider",
 			"position": {"x": 385, "y": 175},
+			"scrollBounds" : { "x" : -4, "y" : -34, "w" : 208, "h" : 52 },
 			"size": 200,
 			"style": "brown",
 			"orientation": "horizontal",