2020-05-06 21:39:57 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
|
|
|
*
|
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
*
|
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifdef ENABLE_SCRIPTING
|
|
|
|
|
|
|
|
# include <cstdint>
|
|
|
|
# include <memory>
|
|
|
|
# include <openrct2/scripting/Duktape.hpp>
|
|
|
|
# include <openrct2/scripting/ScriptEngine.h>
|
|
|
|
# include <optional>
|
|
|
|
# include <string>
|
|
|
|
# include <vector>
|
|
|
|
|
|
|
|
namespace OpenRCT2::Ui::Windows
|
|
|
|
{
|
|
|
|
using namespace OpenRCT2::Scripting;
|
|
|
|
|
|
|
|
enum class ScrollbarType
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Horizontal,
|
|
|
|
Vertical,
|
|
|
|
Both
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ColumnSortOrder
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Ascending,
|
|
|
|
Descending,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ListViewColumn
|
|
|
|
{
|
|
|
|
bool CanSort{};
|
|
|
|
ColumnSortOrder SortOrder;
|
|
|
|
std::string Header;
|
|
|
|
std::string HeaderTooltip;
|
|
|
|
std::optional<int32_t> RatioWidth{};
|
|
|
|
std::optional<int32_t> MinWidth{};
|
|
|
|
std::optional<int32_t> MaxWidth{};
|
|
|
|
int32_t Width{};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ListViewItem
|
|
|
|
{
|
|
|
|
std::vector<std::string> Cells;
|
|
|
|
|
|
|
|
ListViewItem() = default;
|
|
|
|
explicit ListViewItem(const std::string_view& text)
|
|
|
|
{
|
|
|
|
Cells.emplace_back(text);
|
|
|
|
}
|
|
|
|
explicit ListViewItem(std::vector<std::string>&& cells)
|
|
|
|
: Cells(cells)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RowColumn
|
|
|
|
{
|
|
|
|
int32_t Row{};
|
|
|
|
int32_t Column{};
|
|
|
|
|
|
|
|
RowColumn() = default;
|
|
|
|
RowColumn(int32_t row, int32_t column)
|
|
|
|
: Row(row)
|
|
|
|
, Column(column)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const RowColumn& other) const
|
|
|
|
{
|
|
|
|
return Row == other.Row && Column == other.Column;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const RowColumn& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CustomListView
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static constexpr int32_t HEADER_ROW = -1;
|
2020-05-09 17:06:21 +02:00
|
|
|
|
|
|
|
rct_window* ParentWindow{};
|
|
|
|
size_t ScrollIndex{};
|
2020-05-07 01:02:28 +02:00
|
|
|
std::vector<ListViewColumn> Columns;
|
2020-05-06 21:39:57 +02:00
|
|
|
std::vector<ListViewItem> Items;
|
2020-05-09 17:06:21 +02:00
|
|
|
ScrollbarType Scrollbars = ScrollbarType::Vertical;
|
2020-05-06 21:39:57 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
std::shared_ptr<Plugin> Owner;
|
|
|
|
std::vector<size_t> SortedItems;
|
|
|
|
std::optional<RowColumn> HighlightedCell;
|
|
|
|
std::optional<RowColumn> LastHighlightedCell;
|
|
|
|
std::optional<RowColumn> SelectedCell;
|
2020-05-06 23:46:03 +02:00
|
|
|
std::optional<int32_t> ColumnHeaderPressed;
|
2020-05-06 21:39:57 +02:00
|
|
|
bool ColumnHeaderPressedCurrentState{};
|
|
|
|
bool ShowColumnHeaders{};
|
|
|
|
bool IsStriped{};
|
|
|
|
ScreenSize LastKnownSize;
|
|
|
|
ColumnSortOrder CurrentSortOrder{};
|
2020-05-07 00:09:31 +02:00
|
|
|
int32_t CurrentSortColumn{};
|
2020-05-06 21:39:57 +02:00
|
|
|
bool LastIsMouseDown{};
|
|
|
|
bool IsMouseDown{};
|
|
|
|
bool CanSelect{};
|
|
|
|
|
|
|
|
DukValue OnClick;
|
|
|
|
DukValue OnHighlight;
|
|
|
|
|
2020-05-09 17:06:21 +02:00
|
|
|
CustomListView(rct_window* parent, size_t scrollIndex);
|
|
|
|
ScrollbarType GetScrollbars() const;
|
2020-05-09 18:09:47 +02:00
|
|
|
void SetScrollbars(ScrollbarType value, bool initialising = false);
|
2020-05-07 01:02:28 +02:00
|
|
|
const std::vector<ListViewColumn>& GetColumns() const;
|
2020-05-09 18:09:47 +02:00
|
|
|
void SetColumns(const std::vector<ListViewColumn>& columns, bool initialising = false);
|
2020-05-07 01:02:28 +02:00
|
|
|
const std::vector<ListViewItem>& GetItems() const;
|
2020-05-09 18:09:47 +02:00
|
|
|
void SetItems(const std::vector<ListViewItem>& items, bool initialising = false);
|
|
|
|
void SetItems(std::vector<ListViewItem>&& items, bool initialising = false);
|
2020-05-07 00:09:31 +02:00
|
|
|
bool SortItem(size_t indexA, size_t indexB, int32_t column);
|
|
|
|
void SortItems(int32_t column);
|
|
|
|
void SortItems(int32_t column, ColumnSortOrder order);
|
2020-05-06 21:39:57 +02:00
|
|
|
void Resize(const ScreenSize& size);
|
|
|
|
ScreenSize GetSize();
|
|
|
|
void MouseOver(const ScreenCoordsXY& pos, bool isMouseDown);
|
|
|
|
void MouseDown(const ScreenCoordsXY& pos);
|
|
|
|
void MouseUp(const ScreenCoordsXY& pos);
|
|
|
|
void Paint(rct_window* w, rct_drawpixelinfo* dpi, const rct_scroll* scroll) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void PaintHeading(
|
|
|
|
rct_window* w, rct_drawpixelinfo* dpi, const ScreenCoordsXY& pos, const ScreenSize& size, const std::string& text,
|
|
|
|
ColumnSortOrder sortOrder, bool isPressed) const;
|
|
|
|
void PaintCell(
|
|
|
|
rct_drawpixelinfo* dpi, const ScreenCoordsXY& pos, const ScreenSize& size, const char* text,
|
|
|
|
bool isHighlighted) const;
|
|
|
|
std::optional<RowColumn> GetItemIndexAt(const ScreenCoordsXY& pos);
|
2020-05-12 22:03:21 +02:00
|
|
|
void Invalidate();
|
2020-05-06 21:39:57 +02:00
|
|
|
};
|
|
|
|
} // namespace OpenRCT2::Ui::Windows
|
|
|
|
|
|
|
|
class DukValue;
|
|
|
|
|
|
|
|
namespace OpenRCT2::Scripting
|
|
|
|
{
|
|
|
|
using namespace OpenRCT2::Ui::Windows;
|
|
|
|
|
|
|
|
template<> ColumnSortOrder FromDuk(const DukValue& d);
|
|
|
|
template<> std::optional<int32_t> FromDuk(const DukValue& d);
|
|
|
|
template<> ListViewColumn FromDuk(const DukValue& d);
|
|
|
|
template<> ListViewItem FromDuk(const DukValue& d);
|
2020-05-07 01:02:28 +02:00
|
|
|
template<> std::vector<ListViewColumn> FromDuk(const DukValue& d);
|
|
|
|
template<> std::vector<ListViewItem> FromDuk(const DukValue& d);
|
2020-05-06 23:34:56 +02:00
|
|
|
template<> std::optional<RowColumn> FromDuk(const DukValue& d);
|
|
|
|
template<> DukValue ToDuk(duk_context* ctx, const RowColumn& value);
|
2020-05-07 01:02:28 +02:00
|
|
|
template<> DukValue ToDuk(duk_context* ctx, const ListViewColumn& value);
|
2020-05-09 17:06:21 +02:00
|
|
|
template<> ScrollbarType FromDuk(const DukValue& d);
|
|
|
|
template<> DukValue ToDuk(duk_context* ctx, const ScrollbarType& value);
|
2020-05-06 21:39:57 +02:00
|
|
|
} // namespace OpenRCT2::Scripting
|
|
|
|
|
|
|
|
#endif
|