[Scummvm-git-logs] scummvm master -> ef3a0f075788f8a61e3d74568dcc4901392542ff

OMGPizzaGuy noreply at scummvm.org
Sat Dec 24 04:37:35 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
ef3a0f0757 ULTIMA8: Update sliding widget to control drag bounds and calculate values


Commit: ef3a0f075788f8a61e3d74568dcc4901392542ff
    https://github.com/scummvm/scummvm/commit/ef3a0f075788f8a61e3d74568dcc4901392542ff
Author: Matthew Jimenez (matthew.jimenez at outlook.com)
Date: 2022-12-23T22:36:51-06:00

Commit Message:
ULTIMA8: Update sliding widget to control drag bounds and calculate values

Changed paths:
    engines/ultima/ultima8/gumps/slider_gump.cpp
    engines/ultima/ultima8/gumps/slider_gump.h
    engines/ultima/ultima8/gumps/widgets/sliding_widget.cpp
    engines/ultima/ultima8/gumps/widgets/sliding_widget.h


diff --git a/engines/ultima/ultima8/gumps/slider_gump.cpp b/engines/ultima/ultima8/gumps/slider_gump.cpp
index 642697df4bb..51244709f5e 100644
--- a/engines/ultima/ultima8/gumps/slider_gump.cpp
+++ b/engines/ultima/ultima8/gumps/slider_gump.cpp
@@ -67,7 +67,7 @@ static const int slidershape = 45;
 static const int sliderframe = 0;
 static const int slidery = 17;
 static const int sliderminx = 55;
-static const int slidermaxx = 130;
+static const int slidermaxx = 140;
 static const int labelx = 18;
 static const int labely = 26;
 static const int labelfont = 0;
@@ -77,21 +77,10 @@ static const int LEFT_INDEX = 2;
 static const int RIGHT_INDEX = 3;
 static const int SLIDER_INDEX = 4;
 
-int SliderGump::getSliderPos() {
-	return sliderminx + (_value - _min) * (slidermaxx - sliderminx) / (_max - _min);
-}
-
-void SliderGump::setValueFromSlider(int sliderx) {
-	int val = (sliderx - sliderminx) * (_max - _min) / (slidermaxx - sliderminx) + _min;
-	if (val < _min) val = _min;
-	if (val > _max) val = _max;
-	_value = _min + _delta * (static_cast<int16>(val / _delta));
-}
-
 void SliderGump::setSliderPos() {
-	Gump *slider = Gump::FindGump<SlidingWidget>();
+	SlidingWidget *slider = dynamic_cast<SlidingWidget *>(Gump::FindGump<SlidingWidget>());
 	assert(slider);
-	slider->Move(getSliderPos(), slidery);
+	slider->setValueForRange(_value, _min, _max);
 }
 
 void SliderGump::drawText(RenderSurface *surf) {
@@ -125,14 +114,15 @@ void SliderGump::InitGump(Gump *newparent, bool take_focus) {
 
 	// Create the SlidingWidget
 	FrameID frame(GameData::GUMPS, slidershape, sliderframe);
-	Gump *widget = new SlidingWidget(getSliderPos(), slidery, frame);
-	widget->SetIndex(SLIDER_INDEX);
-	widget->InitGump(this);
+	SlidingWidget *slider = new SlidingWidget(sliderminx, slidery, frame, Rect(sliderminx, slidery, slidermaxx, slidery));
+	slider->SetIndex(SLIDER_INDEX);
+	slider->InitGump(this);
+	slider->setValueForRange(_value, _min, _max);
 
 	FrameID button_up(GameData::GUMPS, okshape, 0);
 	FrameID button_down(GameData::GUMPS, okshape, 1);
 
-	widget = new ButtonWidget(158, 17, button_up, button_down);
+	Gump *widget = new ButtonWidget(158, 17, button_up, button_down);
 	widget->SetIndex(OK_INDEX);
 	widget->InitGump(this);
 
@@ -176,10 +166,13 @@ void SliderGump::ChildNotify(Gump *child, uint32 message) {
 		break;
 	case SLIDER_INDEX:
 		if (message == SlidingWidget::DRAGGING) {
-			int32 x, y;
-			child->getLocation(x, y);
-			setValueFromSlider(x);
-			setSliderPos();
+			SlidingWidget *slider = dynamic_cast<SlidingWidget *>(child);
+			assert(slider);
+			_value = slider->getValueForRange(_min, _max);
+			_value = _min + _delta * (static_cast<int16>(_value / _delta));
+
+			// Set value to force slider to snap to increment
+			slider->setValueForRange(_value, _min, _max);
 		}
 		break;
 	}
diff --git a/engines/ultima/ultima8/gumps/slider_gump.h b/engines/ultima/ultima8/gumps/slider_gump.h
index 760af669ba1..9daf16daa0f 100644
--- a/engines/ultima/ultima8/gumps/slider_gump.h
+++ b/engines/ultima/ultima8/gumps/slider_gump.h
@@ -66,9 +66,7 @@ protected:
 	int16 _renderedValue;
 	RenderedText *_renderedText;
 
-	int getSliderPos();
 	void setSliderPos();
-	void setValueFromSlider(int sliderx);
 	void drawText(RenderSurface *surf);
 };
 
diff --git a/engines/ultima/ultima8/gumps/widgets/sliding_widget.cpp b/engines/ultima/ultima8/gumps/widgets/sliding_widget.cpp
index 11dc8171e7b..5fdc1245a30 100644
--- a/engines/ultima/ultima8/gumps/widgets/sliding_widget.cpp
+++ b/engines/ultima/ultima8/gumps/widgets/sliding_widget.cpp
@@ -27,17 +27,38 @@ namespace Ultima8 {
 DEFINE_RUNTIME_CLASSTYPE_CODE(SlidingWidget)
 
 SlidingWidget::SlidingWidget()
-	: Gump() {
+	: Gump(), _dragBounds() {
 }
 
-SlidingWidget::SlidingWidget(int x, int y, FrameID frame)
-	: Gump(x, y, 5, 5, 0, FLAG_DRAGGABLE) {
+SlidingWidget::SlidingWidget(int x, int y, FrameID frame, const Rect &dragBounds)
+	: Gump(x, y, 5, 5, 0, FLAG_DRAGGABLE), _dragBounds(dragBounds) {
 	SetShape(frame, true);
+	if (_dragBounds.width() < _dims.width())
+		_dragBounds.setWidth(_dims.width());
+	if (_dragBounds.height() < _dims.height())
+		_dragBounds.setHeight(_dims.height());
 }
 
 SlidingWidget::~SlidingWidget() {
 }
 
+int SlidingWidget::getValueForRange(int min, int max) {
+	int val = min;
+	if (_dragBounds.isValidRect()) {
+		val = min + (_x - _dragBounds.left) * (max - min) / (_dragBounds.width() - _dims.width());
+		if (val < min)
+			val = min;
+		if (val > max)
+			val = max;
+	}
+	return val;
+}
+
+void SlidingWidget::setValueForRange(int value, int min, int max) {
+	assert(_dragBounds.isValidRect());
+	_x = _dragBounds.left + (value - min) * (_dragBounds.width() - _dims.width()) / (max - min);
+}
+
 void SlidingWidget::InitGump(Gump *newparent, bool take_focus) {
 	Gump::InitGump(newparent, take_focus);
 
@@ -51,6 +72,22 @@ uint16 SlidingWidget::TraceObjId(int32 mx, int32 my) {
 		return 0;
 }
 
+void SlidingWidget::Move(int32 x, int32 y) {
+	if (_dragBounds.isValidRect()) {
+		if (x < _dragBounds.left)
+			x = _dragBounds.left;
+		if (x > _dragBounds.right - _dims.width())
+			x = _dragBounds.right - _dims.width();
+		if (y < _dragBounds.top)
+			y = _dragBounds.top;
+		if (y > _dragBounds.bottom - _dims.height())
+			y = _dragBounds.bottom - _dims.height();
+	}
+
+	_x = x;
+	_y = y;
+}
+
 void SlidingWidget::onDrag(int32 mx, int32 my) {
 	Gump::onDrag(mx, my);
 	_parent->ChildNotify(this, DRAGGING);
diff --git a/engines/ultima/ultima8/gumps/widgets/sliding_widget.h b/engines/ultima/ultima8/gumps/widgets/sliding_widget.h
index b8f5c326d33..42005f88dae 100644
--- a/engines/ultima/ultima8/gumps/widgets/sliding_widget.h
+++ b/engines/ultima/ultima8/gumps/widgets/sliding_widget.h
@@ -29,16 +29,23 @@ namespace Ultima {
 namespace Ultima8 {
 
 class SlidingWidget : public Gump {
+protected:
+	Rect _dragBounds;
+
 public:
 	ENABLE_RUNTIME_CLASSTYPE()
 
 	SlidingWidget();
-	SlidingWidget(int x, int y, FrameID frame);
+	SlidingWidget(int x, int y, FrameID frame, const Rect &dragBounds);
 	~SlidingWidget() override;
 
+	int getValueForRange(int min, int max);
+	void setValueForRange(int value, int min, int max);
+
 	void InitGump(Gump *newparent, bool take_focus = true) override;
 	uint16 TraceObjId(int32 mx, int32 my) override;
 
+	void Move(int32 x, int32 y) override;
 	void onDrag(int32 mx, int32 my) override;
 
 	bool loadData(Common::ReadStream *rs, uint32 version);




More information about the Scummvm-git-logs mailing list