OpenTTD Source: NWidgetMatrix Class Reference

OpenTTD Source 15.3

Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes
NWidgetMatrix Class Reference
Hierarchical widgets

Matrix container with implicitly equal sized (virtual) sub-widgets. More...

`#include <widget_type.h>`

Inheritance diagram for NWidgetMatrix:

## Public Member Functions
| NWidgetMatrix (Colours colour, WidgetID index)
void | SetClicked (int clicked)
| Sets the clicked element in the matrix.
void | SetCount (int count)
| Set the number of elements in this matrix.
void | SetScrollbar (Scrollbar *sb)
| Assign a scrollbar to this matrix.
int | GetCurrentElement () const
| Get current element.
void | SetupSmallestSize (Window *w) override
| Compute smallest size needed by the widget.
void | AssignSizePosition (SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
| Assign size and position to the widget.
NWidgetCore * | GetWidgetFromPos (int x, int y) override
| Retrieve a widget by its position.
void | Draw (const Window *w) override
| Draw the widgets of the tree.
Public Member Functions inherited from NWidgetPIPContainer
| NWidgetPIPContainer (WidgetType tp, NWidContainerFlags flags={}, WidgetID index=INVALID_WIDGET)
void | AdjustPaddingForZoom () override
void | SetPIP (uint8_t pip_pre, uint8_t pip_inter, uint8_t pip_post)
| Set additional pre/inter/post space for the container.
void | SetPIPRatio (uint8_t pip_ratio_pre, uint8_t pip_ratio_inter, uint8_t pip_ratio_post)
| Set additional pre/inter/post space for the container.
Public Member Functions inherited from NWidgetContainer
| NWidgetContainer (WidgetType tp, WidgetID index=INVALID_WIDGET)
void | Add (std::unique_ptr< NWidgetBase > &&wid)
| Append widget wid to container.
void | FillWidgetLookup (WidgetLookup &widget_lookup) override
| Fill the Window::widget_lookup with pointers to nested widgets in the tree.
bool | IsEmpty ()
| Return whether the container is empty.
NWidgetBase * | GetWidgetOfType (WidgetType tp) override
| Retrieve a widget by its type.
void | UnfocusWidgets (Window *parent_window)
| Unfocuses the focused widget of the window, if the focused widget is contained inside the container.
void | Clear (Window *parent_window)
| Clears the container, deleting all widgets that were contained.
Public Member Functions inherited from NWidgetBase
| NWidgetBase (WidgetType tp, WidgetID index=INVALID_WIDGET)
void | ApplyAspectRatio ()
template<class NWID >
NWID * | GetParentWidget ()
| Get parent widget of type NWID.
template<class NWID >
const NWID * | GetParentWidget () const
| Get parent widget of type NWID.
WidgetID | GetIndex () const
virtual bool | IsHighlighted () const
virtual TextColour | GetHighlightColour () const
virtual void | SetHighlighted (TextColour highlight_colour)
void | SetPadding (uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
| Set additional space (padding) around the widget.
void | SetPadding (const RectPadding &padding)
| Set additional space (padding) around the widget.
uint | GetHorizontalStepSize (SizingType sizing) const
| Get the horizontal sizing step.
uint | GetVerticalStepSize (SizingType sizing) const
| Get the vertical sizing step.
virtual void | SetDirty (const Window *w) const
| Mark the widget as 'dirty' (in need of repaint).
Rect | GetCurrentRect () const

## Protected Attributes
Colours | colour {}
| Colour of this widget.
int | clicked = -1
| The currently clicked element.
int | count = 0
| Amount of valid elements.
int | current_element = 0
| The element currently being processed.
Scrollbar * | sb = nullptr
| The scrollbar we're associated with.
Protected Attributes inherited from NWidgetPIPContainer
NWidContainerFlags | flags {}
| Flags of the container.
uint8_t | pip_pre = 0
| Amount of space before first widget.
uint8_t | pip_inter = 0
| Amount of space between widgets.
uint8_t | pip_post = 0
| Amount of space after last widget.
uint8_t | pip_ratio_pre = 0
| Ratio of remaining space before first widget.
uint8_t | pip_ratio_inter = 0
| Ratio of remaining space between widgets.
uint8_t | pip_ratio_post = 0
| Ratio of remaining space after last widget.
uint8_t | uz_pip_pre = 0
| Unscaled space before first widget.
uint8_t | uz_pip_inter = 0
| Unscaled space between widgets.
uint8_t | uz_pip_post = 0
| Unscaled space after last widget.
uint8_t | gaps = 0
| Number of gaps between widgets.
Protected Attributes inherited from NWidgetContainer
std::vector< std::unique_ptr< NWidgetBase > > | children {}
| Child widgets in container.
Protected Attributes inherited from NWidgetBase
const WidgetID | index = INVALID_WIDGET
| Index of the nested widget (`INVALID_WIDGET` means 'not used').

## Private Member Functions
void | GetScrollOffsets (int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
| Get the different offsets that are influenced by scrolling.

## Private Attributes
int | widget_w = 0
| The width of the child widget including inter spacing.
int | widget_h = 0
| The height of the child widget including inter spacing.
int | widgets_x = 0
| The number of visible widgets in horizontal direction.
int | widgets_y = 0
| The number of visible widgets in vertical direction.

## Additional Inherited Members
Data Fields inherited from NWidgetBase
WidgetType | type {}
| Type of the widget / nested widget.
uint | fill_x = 0
| Horizontal fill stepsize (from initial size, `0` means not resizable).
uint | fill_y = 0
| Vertical fill stepsize (from initial size, `0` means not resizable).
uint | resize_x = 0
| Horizontal resize step (`0` means not resizable).
uint | resize_y = 0
| Vertical resize step (`0` means not resizable).
uint | smallest_x = 0
| Smallest horizontal size of the widget in a filled window.
uint | smallest_y = 0
| Smallest vertical size of the widget in a filled window.
uint | current_x = 0
| Current horizontal size (after resizing).
uint | current_y = 0
| Current vertical size (after resizing).
float | aspect_ratio = 0
| Desired aspect ratio of widget.
AspectFlags | aspect_flags = AspectFlag::ResizeX
| Which dimensions can be resized.
int | pos_x = 0
| Horizontal position of top-left corner of the widget in the window.
int | pos_y = 0
| Vertical position of top-left corner of the widget in the window.
RectPadding | padding {}
| Padding added to the widget. Managed by parent container widget. (parent container may swap left and right for RTL)
RectPadding | uz_padding {}
| Unscaled padding, for resize calculation.
NWidgetBase * | parent = nullptr
| Parent widget of this widget, automatically filled in when added to container.
Protected Member Functions inherited from NWidgetBase
void | StoreSizePosition (SizingType sizing, int x, int y, uint given_width, uint given_height)
| Store size and position.

## Detailed Description

Matrix container with implicitly equal sized (virtual) sub-widgets.

This widget must have exactly one sub-widget. After that this sub-widget is used to draw all of the data within the matrix piece by piece. DrawWidget and OnClick calls will be done to that sub-widget, where the 16 high bits are used to encode the index into the matrix.

Definition at line 617 of file widget_type.h.

## Constructor & Destructor Documentation

## ◆ NWidgetMatrix()

NWidgetMatrix::NWidgetMatrix | ( | Colours | colour,
|  | WidgetID | index
| ) |  |
| inline

Definition at line 619 of file widget_type.h.

## Member Function Documentation

## ◆ AssignSizePosition()

void NWidgetMatrix::AssignSizePosition | ( | SizingType | sizing,
|  | int | x,
|  | int | y,
|  | uint | given_width,
|  | uint | given_height,
|  | bool | rtl
| ) |  |
| overridevirtual

Assign size and position to the widget.

Parameters

  sizing | Type of resizing to perform.
  x | Horizontal offset of the widget relative to the left edge of the window.
  y | Vertical offset of the widget relative to the top edge of the window.
  given_width | Width allocated to the widget.
  given_height | Height allocated to the widget.
  rtl | Adapt for right-to-left languages (position contents of horizontal containers backwards).

Afterwards, pos_x and pos_y contain the top-left position of the widget, smallest_x and smallest_y contain the smallest size such that all widgets of the window are consistent, and current_x and current_y contain the current size.

Implements NWidgetBase.

Definition at line 2034 of file widget.cpp.

References CeilDiv(), NWidgetContainer::children, count, NWidgetBase::current_x, NWidgetBase::current_y, NWidgetPIPContainer::pip_inter, NWidgetPIPContainer::pip_post, NWidgetPIPContainer::pip_pre, NWidgetBase::pos_x, NWidgetBase::pos_y, SetCount(), NWidgetBase::smallest_x, NWidgetBase::smallest_y, widget_h, widget_w, widgets_x, and widgets_y.

## ◆ Draw()

void NWidgetMatrix::Draw | ( | const Window * | w | ) |
| overridevirtual

Draw the widgets of the tree.

The function calls Window::DrawWidget for each widget with a non-negative index, after the widget itself is painted.

Parameters

  w | Window that owns the tree.

Reimplemented from NWidgetContainer.

Definition at line 2088 of file widget.cpp.

References _current_text_dir, NWidgetResizeBase::AssignSizePosition(), NWidgetContainer::children, clicked, colour, count, current_element, NWidgetBase::current_x, NWidgetBase::current_y, NWidgetBase::Draw(), FillDrawPixelInfo(), GetColourGradient(), GetScrollOffsets(), GfxFillRect(), NWidgetPIPContainer::pip_post, NWidgetPIPContainer::pip_pre, NWidgetBase::pos_x, NWidgetBase::pos_y, NWidgetCore::SetLowered(), NWidgetBase::smallest_x, NWidgetBase::smallest_y, ST_RESIZE, TD_RTL, widget_h, widget_w, widgets_x, and widgets_y.

## ◆ GetCurrentElement()

int NWidgetMatrix::GetCurrentElement | ( |  | ) | const

Get current element.

Returns
  index of current element.

Definition at line 2007 of file widget.cpp.

References current_element.

Referenced by BuildObjectWindow::DrawWidget().

## ◆ GetScrollOffsets()

void NWidgetMatrix::GetScrollOffsets | ( | int & | start_x,
|  | int & | start_y,
|  | int & | base_offs_x,
|  | int & | base_offs_y
| ) |  |
| private

Get the different offsets that are influenced by scrolling.

Parameters

  [out] | start_x | The start position in columns (index of the left-most column, swapped in RTL).
  [out] | start_y | The start position in rows.
  [out] | base_offs_x | The base horizontal offset in pixels (X position of the column start_x).
  [out] | base_offs_y | The base vertical offset in pixels (Y position of the column start_y).

Definition at line 2143 of file widget.cpp.

References _current_text_dir, Scrollbar::GetPosition(), Scrollbar::IsVertical(), sb, TD_RTL, widget_h, widget_w, and widgets_x.

Referenced by Draw(), and GetWidgetFromPos().

## ◆ GetWidgetFromPos()

NWidgetCore * NWidgetMatrix::GetWidgetFromPos | ( | int | x,
|  | int | y
| ) |  |
| overridevirtual

Retrieve a widget by its position.

Parameters

  x | Horizontal position relative to the left edge of the window.
  y | Vertical position relative to the top edge of the window.

Returns
  Returns the deepest nested widget that covers the given position, or `nullptr` if no widget can be found.

Reimplemented from NWidgetContainer.

Definition at line 2058 of file widget.cpp.

References _current_text_dir, NWidgetResizeBase::AssignSizePosition(), NWidgetContainer::children, count, current_element, NWidgetBase::current_x, NWidgetBase::current_y, GetScrollOffsets(), NWidgetCore::GetWidgetFromPos(), IsInsideBS(), NWidgetPIPContainer::pip_inter, NWidgetPIPContainer::pip_post, NWidgetPIPContainer::pip_pre, NWidgetBase::pos_x, NWidgetBase::pos_y, NWidgetBase::smallest_x, NWidgetBase::smallest_y, ST_RESIZE, TD_RTL, widget_h, widget_w, and widgets_x.

## ◆ SetClicked()

void NWidgetMatrix::SetClicked | ( | int | clicked | ) |

Sets the clicked element in the matrix.

Parameters

  clicked | The clicked element.

Definition at line 1957 of file widget.cpp.

References clicked, Scrollbar::GetPosition(), NWidgetPIPContainer::pip_inter, sb, Scrollbar::ScrollTowards(), widget_h, and widgets_x.

## ◆ SetCount()

void NWidgetMatrix::SetCount | ( | int | count | ) |

Set the number of elements in this matrix.

Note
  Updates the number of elements/capacity of the real scrollbar.

Parameters

  count | The number of elements.

Definition at line 1974 of file widget.cpp.

References CeilDiv(), NWidgetContainer::children, count, NWidgetBase::current_x, NWidgetBase::current_y, Scrollbar::IsVertical(), NWidgetPIPContainer::pip_inter, NWidgetPIPContainer::pip_post, NWidgetPIPContainer::pip_pre, sb, Scrollbar::SetCapacity(), Scrollbar::SetCount(), Scrollbar::SetStepSize(), widget_h, widget_w, widgets_x, and widgets_y.

Referenced by AssignSizePosition().

## ◆ SetScrollbar()

void NWidgetMatrix::SetScrollbar | ( | Scrollbar * | sb | ) |

Assign a scrollbar to this matrix.

Parameters

  sb | The scrollbar to assign to us.

Definition at line 1998 of file widget.cpp.

References sb.

## ◆ SetupSmallestSize()

void NWidgetMatrix::SetupSmallestSize | ( | Window * | w | ) |
| overridevirtual

Compute smallest size needed by the widget.

The smallest size of a widget is the smallest size that a widget needs to display itself properly. In addition, filling and resizing of the widget are computed. The function calls Window::UpdateWidgetSize for each leaf widget and background widget without child with a non-negative index.

Parameters

  w | Window owning the widget.

Note
  After the computation, the results can be queried by accessing the smallest_x and smallest_y data members of the widget.

Implements NWidgetBase.

Definition at line 2012 of file widget.cpp.

References NWidgetContainer::children, NWidgetBase::fill_x, NWidgetBase::fill_y, NWidgetBase::index, NWidgetBase::padding, NWidgetPIPContainer::pip_inter, NWidgetPIPContainer::pip_post, NWidgetPIPContainer::pip_pre, NWidgetBase::resize_x, NWidgetBase::resize_y, NWidgetBase::smallest_x, NWidgetBase::smallest_y, and Window::UpdateWidgetSize().

## Field Documentation

## ◆ clicked

int NWidgetMatrix::clicked = -1
| protected

The currently clicked element.

Definition at line 633 of file widget_type.h.

Referenced by Draw(), and SetClicked().

## ◆ colour

Colours NWidgetMatrix::colour {}
| protected

Colour of this widget.

Definition at line 632 of file widget_type.h.

Referenced by Draw().

## ◆ count

int NWidgetMatrix::count = 0
| protected

Amount of valid elements.

Definition at line 634 of file widget_type.h.

Referenced by AssignSizePosition(), Draw(), GetWidgetFromPos(), and SetCount().

## ◆ current_element

int NWidgetMatrix::current_element = 0
| protected

The element currently being processed.

Definition at line 635 of file widget_type.h.

Referenced by Draw(), GetCurrentElement(), and GetWidgetFromPos().

## ◆ sb

Scrollbar* NWidgetMatrix::sb = nullptr
| protected

The scrollbar we're associated with.

Definition at line 636 of file widget_type.h.

Referenced by GetScrollOffsets(), SetClicked(), SetCount(), and SetScrollbar().

## ◆ widget_h

int NWidgetMatrix::widget_h = 0
| private

The height of the child widget including inter spacing.

Definition at line 639 of file widget_type.h.

Referenced by AssignSizePosition(), Draw(), GetScrollOffsets(), GetWidgetFromPos(), SetClicked(), and SetCount().

## ◆ widget_w

int NWidgetMatrix::widget_w = 0
| private

The width of the child widget including inter spacing.

Definition at line 638 of file widget_type.h.

Referenced by AssignSizePosition(), Draw(), GetScrollOffsets(), GetWidgetFromPos(), and SetCount().

## ◆ widgets_x

int NWidgetMatrix::widgets_x = 0
| private

The number of visible widgets in horizontal direction.

Definition at line 640 of file widget_type.h.

Referenced by AssignSizePosition(), Draw(), GetScrollOffsets(), GetWidgetFromPos(), SetClicked(), and SetCount().

## ◆ widgets_y

int NWidgetMatrix::widgets_y = 0
| private

The number of visible widgets in vertical direction.

Definition at line 641 of file widget_type.h.

Referenced by AssignSizePosition(), Draw(), and SetCount().

The documentation for this class was generated from the following files:

- src/widget_type.h
- src/widget.cpp