Changeset - r28822:57cd1bc354de
[Not reviewed]
master
0 1 0
Peter Nelson - 9 months ago 2024-02-26 17:19:12
peter1138@openttd.org
Codechange: Add `GetVisibleRangeIterators()` to `Scrollbar`.
1 file changed with 14 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/widget_type.h
Show inline comments
 
@@ -813,48 +813,62 @@ public:
 
		}
 
		return this->SetPosition(this->pos + difference);
 
	}
 

	
 
	/**
 
	 * Scroll towards the given position; if the item is visible nothing
 
	 * happens, otherwise it will be shown either at the bottom or top of
 
	 * the window depending on where in the list it was.
 
	 * @param position the position to scroll towards.
 
	 */
 
	void ScrollTowards(int position)
 
	{
 
		if (position < this->GetPosition()) {
 
			/* scroll up to the item */
 
			this->SetPosition(position);
 
		} else if (position >= this->GetPosition() + this->GetCapacity()) {
 
			/* scroll down so that the item is at the bottom */
 
			this->SetPosition(position - this->GetCapacity() + 1);
 
		}
 
	}
 

	
 
	int GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const;
 

	
 
	/**
 
	 * Get a pair of iterators for the range of visible elements in a container.
 
	 * @param container Container of elements represented by the scrollbar.
 
	 * @returns Pair of iterators of visible elements.
 
	 */
 
	template <typename Tcontainer>
 
	auto GetVisibleRangeIterators(Tcontainer &container) const
 
	{
 
		assert(this->GetCount() == container.size()); // Scrollbar and container size must match.
 
		auto first = std::next(std::begin(container), this->GetPosition());
 
		auto last = std::next(first, std::min<size_t>(this->GetCapacity(), this->GetCount() - this->GetPosition()));
 
		return std::make_pair(first, last);
 
	}
 

	
 
	/**
 
	 * Return an iterator pointing to the element of a scrolled widget that a user clicked in.
 
	 * @param container   Container of elements represented by the scrollbar.
 
	 * @param clickpos    Vertical position of the mouse click (without taking scrolling into account).
 
	 * @param w           The window the click was in.
 
	 * @param widget      Widget number of the widget clicked in.
 
	 * @param padding     Amount of empty space between the widget edge and the top of the first row. Default value is \c 0.
 
	 * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
 
	 * @return Iterator to the element clicked at. If clicked at a wrong position, returns as interator to the end of the container.
 
	 */
 
	template <typename Tcontainer>
 
	typename Tcontainer::iterator GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const
 
	{
 
		assert(this->GetCount() == container.size()); // Scrollbar and container size must match.
 
		int row = this->GetScrolledRowFromWidget(clickpos, w, widget, padding, line_height);
 
		if (row == INT_MAX) return std::end(container);
 

	
 
		typename Tcontainer::iterator it = std::begin(container);
 
		std::advance(it, row);
 
		return it;
 
	}
 

	
 
	EventState UpdateListPositionOnKeyPress(int &list_position, uint16_t keycode) const;
 
};
 

	
0 comments (0 inline, 0 general)