mirror of https://github.com/OpenTTD/OpenTTD
(svn r20458) -Codechange: Move Scrollbar from window.cpp to widget.cpp
parent
57c063250f
commit
55bd5de43d
|
@ -1632,6 +1632,38 @@ void NWidgetViewport::UpdateViewportCoordinates(Window *w)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute the row of a scrolled widget that a user clicked in.
|
||||||
|
* @param clickpos Vertical position of the mouse click (without taking scrolling into account).
|
||||||
|
* @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 Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.
|
||||||
|
*/
|
||||||
|
int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
|
||||||
|
{
|
||||||
|
uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
|
||||||
|
if (pos != INT_MAX) pos += this->GetPosition();
|
||||||
|
return (pos >= this->GetCount()) ? INT_MAX : pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set capacity of visible elements from the size and resize properties of a widget.
|
||||||
|
* @param w Window.
|
||||||
|
* @param widget Widget with size and resize properties.
|
||||||
|
* @param padding Padding to subtract from the size.
|
||||||
|
* @note Updates the position if needed.
|
||||||
|
*/
|
||||||
|
void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
|
||||||
|
{
|
||||||
|
NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
|
||||||
|
if (this->is_vertical) {
|
||||||
|
this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
|
||||||
|
} else {
|
||||||
|
this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scrollbar widget.
|
* Scrollbar widget.
|
||||||
* @param tp Scrollbar type. (horizontal/vertical)
|
* @param tp Scrollbar type. (horizontal/vertical)
|
||||||
|
|
|
@ -501,6 +501,132 @@ public:
|
||||||
void UpdateViewportCoordinates(Window *w);
|
void UpdateViewportCoordinates(Window *w);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scrollbar data structure
|
||||||
|
*/
|
||||||
|
class Scrollbar {
|
||||||
|
private:
|
||||||
|
const bool is_vertical; ///< Scrollbar has vertical orientation.
|
||||||
|
uint16 count; ///< Number of elements in the list.
|
||||||
|
uint16 cap; ///< Number of visible elements of the scroll bar.
|
||||||
|
uint16 pos; ///< Index of first visible item of the list.
|
||||||
|
|
||||||
|
public:
|
||||||
|
Scrollbar(bool is_vertical) : is_vertical(is_vertical)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number of elements in the list
|
||||||
|
* @return the number of elements
|
||||||
|
*/
|
||||||
|
FORCEINLINE uint16 GetCount() const
|
||||||
|
{
|
||||||
|
return this->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number of visible elements of the scrollbar
|
||||||
|
* @return the number of visible elements
|
||||||
|
*/
|
||||||
|
FORCEINLINE uint16 GetCapacity() const
|
||||||
|
{
|
||||||
|
return this->cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the position of the first visible element in the list
|
||||||
|
* @return the position of the element
|
||||||
|
*/
|
||||||
|
FORCEINLINE uint16 GetPosition() const
|
||||||
|
{
|
||||||
|
return this->pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether given current item is visible in the list
|
||||||
|
* @param item to check
|
||||||
|
* @return true iff the item is visible
|
||||||
|
*/
|
||||||
|
FORCEINLINE bool IsVisible(uint16 item) const
|
||||||
|
{
|
||||||
|
return IsInsideBS(item, this->GetPosition(), this->GetCapacity());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the number of elements in the list
|
||||||
|
* @param num the number of elements in the list
|
||||||
|
* @note updates the position if needed
|
||||||
|
*/
|
||||||
|
void SetCount(int num)
|
||||||
|
{
|
||||||
|
assert(num >= 0);
|
||||||
|
assert(num <= MAX_UVALUE(uint16));
|
||||||
|
|
||||||
|
this->count = num;
|
||||||
|
num -= this->cap;
|
||||||
|
if (num < 0) num = 0;
|
||||||
|
if (num < this->pos) this->pos = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the capacity of visible elements.
|
||||||
|
* @param capacity the new capacity
|
||||||
|
* @note updates the position if needed
|
||||||
|
*/
|
||||||
|
void SetCapacity(int capacity)
|
||||||
|
{
|
||||||
|
assert(capacity > 0);
|
||||||
|
assert(capacity <= MAX_UVALUE(uint16));
|
||||||
|
|
||||||
|
this->cap = capacity;
|
||||||
|
if (this->cap + this->pos > this->count) this->pos = max(0, this->count - this->cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetCapacityFromWidget(Window *w, int widget, int padding = 0);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the position of the first visible element
|
||||||
|
* @param position the position of the element
|
||||||
|
*/
|
||||||
|
void SetPosition(int position)
|
||||||
|
{
|
||||||
|
assert(position >= 0);
|
||||||
|
assert(this->count <= this->cap ? (position == 0) : (position + this->cap <= this->count));
|
||||||
|
this->pos = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the position of the first visible element by the given amount.
|
||||||
|
* If the position would be too low or high it will be clamped appropriately
|
||||||
|
* @param difference the amount of change requested
|
||||||
|
*/
|
||||||
|
void UpdatePosition(int difference)
|
||||||
|
{
|
||||||
|
if (difference == 0) return;
|
||||||
|
this->SetPosition(Clamp(this->pos + difference, 0, max(this->count - this->cap, 0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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, int widget, int padding = 0, int line_height = -1) const;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nested widget to display and control a scrollbar in a window.
|
* Nested widget to display and control a scrollbar in a window.
|
||||||
* Also assign the scrollbar to other widgets using #SetScrollbar() to make the mousewheel work.
|
* Also assign the scrollbar to other widgets using #SetScrollbar() to make the mousewheel work.
|
||||||
|
|
|
@ -97,38 +97,6 @@ int Window::GetRowFromWidget(int clickpos, int widget, int padding, int line_hei
|
||||||
return (clickpos - (int)wid->pos_y - padding) / line_height;
|
return (clickpos - (int)wid->pos_y - padding) / line_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Compute the row of a scrolled widget that a user clicked in.
|
|
||||||
* @param clickpos Vertical position of the mouse click (without taking scrolling into account).
|
|
||||||
* @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 Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.
|
|
||||||
*/
|
|
||||||
int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
|
|
||||||
{
|
|
||||||
uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
|
|
||||||
if (pos != INT_MAX) pos += this->GetPosition();
|
|
||||||
return (pos >= this->GetCount()) ? INT_MAX : pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set capacity of visible elements from the size and resize properties of a widget.
|
|
||||||
* @param w Window.
|
|
||||||
* @param widget Widget with size and resize properties.
|
|
||||||
* @param padding Padding to subtract from the size.
|
|
||||||
* @note Updates the position if needed.
|
|
||||||
*/
|
|
||||||
void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
|
|
||||||
{
|
|
||||||
NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
|
|
||||||
if (this->is_vertical) {
|
|
||||||
this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
|
|
||||||
} else {
|
|
||||||
this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the Scrollbar to a widget index.
|
* Return the Scrollbar to a widget index.
|
||||||
* @param widnum Scrollbar widget index
|
* @param widnum Scrollbar widget index
|
||||||
|
|
126
src/window_gui.h
126
src/window_gui.h
|
@ -183,132 +183,6 @@ enum WindowDefaultFlag {
|
||||||
WDF_NO_FOCUS = 1 << 3, ///< This window won't get focus/make any other window lose focus when click
|
WDF_NO_FOCUS = 1 << 3, ///< This window won't get focus/make any other window lose focus when click
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Scrollbar data structure
|
|
||||||
*/
|
|
||||||
class Scrollbar {
|
|
||||||
private:
|
|
||||||
const bool is_vertical; ///< Scrollbar has vertical orientation.
|
|
||||||
uint16 count; ///< Number of elements in the list.
|
|
||||||
uint16 cap; ///< Number of visible elements of the scroll bar.
|
|
||||||
uint16 pos; ///< Index of first visible item of the list.
|
|
||||||
|
|
||||||
public:
|
|
||||||
Scrollbar(bool is_vertical) : is_vertical(is_vertical)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number of elements in the list
|
|
||||||
* @return the number of elements
|
|
||||||
*/
|
|
||||||
FORCEINLINE uint16 GetCount() const
|
|
||||||
{
|
|
||||||
return this->count;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number of visible elements of the scrollbar
|
|
||||||
* @return the number of visible elements
|
|
||||||
*/
|
|
||||||
FORCEINLINE uint16 GetCapacity() const
|
|
||||||
{
|
|
||||||
return this->cap;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the position of the first visible element in the list
|
|
||||||
* @return the position of the element
|
|
||||||
*/
|
|
||||||
FORCEINLINE uint16 GetPosition() const
|
|
||||||
{
|
|
||||||
return this->pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks whether given current item is visible in the list
|
|
||||||
* @param item to check
|
|
||||||
* @return true iff the item is visible
|
|
||||||
*/
|
|
||||||
FORCEINLINE bool IsVisible(uint16 item) const
|
|
||||||
{
|
|
||||||
return IsInsideBS(item, this->GetPosition(), this->GetCapacity());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the number of elements in the list
|
|
||||||
* @param num the number of elements in the list
|
|
||||||
* @note updates the position if needed
|
|
||||||
*/
|
|
||||||
void SetCount(int num)
|
|
||||||
{
|
|
||||||
assert(num >= 0);
|
|
||||||
assert(num <= MAX_UVALUE(uint16));
|
|
||||||
|
|
||||||
this->count = num;
|
|
||||||
num -= this->cap;
|
|
||||||
if (num < 0) num = 0;
|
|
||||||
if (num < this->pos) this->pos = num;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the capacity of visible elements.
|
|
||||||
* @param capacity the new capacity
|
|
||||||
* @note updates the position if needed
|
|
||||||
*/
|
|
||||||
void SetCapacity(int capacity)
|
|
||||||
{
|
|
||||||
assert(capacity > 0);
|
|
||||||
assert(capacity <= MAX_UVALUE(uint16));
|
|
||||||
|
|
||||||
this->cap = capacity;
|
|
||||||
if (this->cap + this->pos > this->count) this->pos = max(0, this->count - this->cap);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetCapacityFromWidget(Window *w, int widget, int padding = 0);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the position of the first visible element
|
|
||||||
* @param position the position of the element
|
|
||||||
*/
|
|
||||||
void SetPosition(int position)
|
|
||||||
{
|
|
||||||
assert(position >= 0);
|
|
||||||
assert(this->count <= this->cap ? (position == 0) : (position + this->cap <= this->count));
|
|
||||||
this->pos = position;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the position of the first visible element by the given amount.
|
|
||||||
* If the position would be too low or high it will be clamped appropriately
|
|
||||||
* @param difference the amount of change requested
|
|
||||||
*/
|
|
||||||
void UpdatePosition(int difference)
|
|
||||||
{
|
|
||||||
if (difference == 0) return;
|
|
||||||
this->SetPosition(Clamp(this->pos + difference, 0, max(this->count - this->cap, 0)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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, int widget, int padding = 0, int line_height = -1) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Data structure for resizing a window
|
* Data structure for resizing a window
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue