mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-21 22:13:07 +01:00
Allow items and columns get / set
This commit is contained in:
@@ -35,6 +35,19 @@ namespace OpenRCT2::Scripting
|
||||
return ColumnSortOrder::None;
|
||||
}
|
||||
|
||||
template<> DukValue ToDuk(duk_context* ctx, const ColumnSortOrder& value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case ColumnSortOrder::Ascending:
|
||||
return ToDuk(ctx, "ascending");
|
||||
case ColumnSortOrder::Descending:
|
||||
return ToDuk(ctx, "descending");
|
||||
default:
|
||||
return ToDuk(ctx, "none");
|
||||
}
|
||||
}
|
||||
|
||||
template<> std::optional<int32_t> FromDuk(const DukValue& d)
|
||||
{
|
||||
if (d.type() == DukValue::Type::NUMBER)
|
||||
@@ -67,6 +80,20 @@ namespace OpenRCT2::Scripting
|
||||
return result;
|
||||
}
|
||||
|
||||
template<> DukValue ToDuk(duk_context* ctx, const ListViewColumn& value)
|
||||
{
|
||||
DukObject obj(ctx);
|
||||
obj.Set("canSort", value.CanSort);
|
||||
obj.Set("sortOrder", ToDuk(ctx, value.SortOrder));
|
||||
obj.Set("header", value.Header);
|
||||
obj.Set("headerTooltip", value.HeaderTooltip);
|
||||
obj.Set("minWidth", value.MinWidth);
|
||||
obj.Set("maxWidth", value.MaxWidth);
|
||||
obj.Set("ratioWidth", value.RatioWidth);
|
||||
obj.Set("width", value.Width);
|
||||
return obj.Take();
|
||||
}
|
||||
|
||||
template<> ListViewItem FromDuk(const DukValue& d)
|
||||
{
|
||||
ListViewItem result;
|
||||
@@ -86,6 +113,34 @@ namespace OpenRCT2::Scripting
|
||||
return result;
|
||||
}
|
||||
|
||||
template<> std::vector<ListViewColumn> FromDuk(const DukValue& d)
|
||||
{
|
||||
std::vector<ListViewColumn> result;
|
||||
if (d.is_array())
|
||||
{
|
||||
auto dukColumns = d.as_array();
|
||||
for (const auto& dukColumn : dukColumns)
|
||||
{
|
||||
result.push_back(FromDuk<ListViewColumn>(dukColumn));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<> std::vector<ListViewItem> FromDuk(const DukValue& d)
|
||||
{
|
||||
std::vector<ListViewItem> result;
|
||||
if (d.is_array())
|
||||
{
|
||||
auto dukItems = d.as_array();
|
||||
for (const auto& dukItem : dukItems)
|
||||
{
|
||||
result.push_back(FromDuk<ListViewItem>(dukItem));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<> std::optional<RowColumn> FromDuk(const DukValue& d)
|
||||
{
|
||||
if (d.type() == DukValue::Type::OBJECT)
|
||||
@@ -109,8 +164,27 @@ namespace OpenRCT2::Scripting
|
||||
}
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
const std::vector<ListViewColumn>& CustomListView::GetColumns() const
|
||||
{
|
||||
return Columns;
|
||||
}
|
||||
|
||||
void CustomListView::SetColumns(const std::vector<ListViewColumn>& columns)
|
||||
{
|
||||
SelectedCell = std::nullopt;
|
||||
Columns = columns;
|
||||
LastKnownSize = {};
|
||||
SortItems(0, ColumnSortOrder::None);
|
||||
}
|
||||
|
||||
const std::vector<ListViewItem>& CustomListView::CustomListView::GetItems() const
|
||||
{
|
||||
return Items;
|
||||
}
|
||||
|
||||
void CustomListView::SetItems(const std::vector<ListViewItem>& items)
|
||||
{
|
||||
SelectedCell = std::nullopt;
|
||||
Items = items;
|
||||
SortItems(0, ColumnSortOrder::None);
|
||||
}
|
||||
|
||||
@@ -92,11 +92,11 @@ namespace OpenRCT2::Ui::Windows
|
||||
{
|
||||
private:
|
||||
static constexpr int32_t HEADER_ROW = -1;
|
||||
std::vector<ListViewColumn> Columns;
|
||||
std::vector<ListViewItem> Items;
|
||||
|
||||
public:
|
||||
std::shared_ptr<Plugin> Owner;
|
||||
std::vector<ListViewColumn> Columns;
|
||||
std::vector<size_t> SortedItems;
|
||||
std::optional<RowColumn> HighlightedCell;
|
||||
std::optional<RowColumn> LastHighlightedCell;
|
||||
@@ -116,6 +116,9 @@ namespace OpenRCT2::Ui::Windows
|
||||
DukValue OnClick;
|
||||
DukValue OnHighlight;
|
||||
|
||||
const std::vector<ListViewColumn>& GetColumns() const;
|
||||
void SetColumns(const std::vector<ListViewColumn>& columns);
|
||||
const std::vector<ListViewItem>& GetItems() const;
|
||||
void SetItems(const std::vector<ListViewItem>& items);
|
||||
void SetItems(std::vector<ListViewItem>&& items);
|
||||
bool SortItem(size_t indexA, size_t indexB, int32_t column);
|
||||
@@ -149,8 +152,11 @@ namespace OpenRCT2::Scripting
|
||||
template<> std::optional<int32_t> FromDuk(const DukValue& d);
|
||||
template<> ListViewColumn FromDuk(const DukValue& d);
|
||||
template<> ListViewItem FromDuk(const DukValue& d);
|
||||
template<> std::vector<ListViewColumn> FromDuk(const DukValue& d);
|
||||
template<> std::vector<ListViewItem> FromDuk(const DukValue& d);
|
||||
template<> std::optional<RowColumn> FromDuk(const DukValue& d);
|
||||
template<> DukValue ToDuk(duk_context* ctx, const RowColumn& value);
|
||||
template<> DukValue ToDuk(duk_context* ctx, const ListViewColumn& value);
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
#endif
|
||||
|
||||
@@ -171,22 +171,8 @@ namespace OpenRCT2::Ui::Windows
|
||||
}
|
||||
else if (result.Type == "listview")
|
||||
{
|
||||
if (desc["columns"].is_array())
|
||||
{
|
||||
auto dukColumns = desc["columns"].as_array();
|
||||
for (const auto& dukColumn : dukColumns)
|
||||
{
|
||||
result.ListViewColumns.push_back(FromDuk<ListViewColumn>(dukColumn));
|
||||
}
|
||||
}
|
||||
if (desc["items"].is_array())
|
||||
{
|
||||
auto dukItems = desc["items"].as_array();
|
||||
for (const auto& dukItem : dukItems)
|
||||
{
|
||||
result.ListViewItems.push_back(FromDuk<ListViewItem>(dukItem));
|
||||
}
|
||||
}
|
||||
result.ListViewColumns = FromDuk<std::vector<ListViewColumn>>(desc["columns"]);
|
||||
result.ListViewItems = FromDuk<std::vector<ListViewItem>>(desc["items"]);
|
||||
result.ShowColumnHeaders = AsOrDefault(desc["showColumnHeaders"], false);
|
||||
result.IsStriped = AsOrDefault(desc["isStriped"], false);
|
||||
result.OnClick = desc["onClick"];
|
||||
@@ -986,7 +972,7 @@ namespace OpenRCT2::Ui::Windows
|
||||
if (widgetDesc.Type == "listview")
|
||||
{
|
||||
CustomListView listView;
|
||||
listView.Columns = widgetDesc.ListViewColumns;
|
||||
listView.SetColumns(widgetDesc.ListViewColumns);
|
||||
listView.SetItems(widgetDesc.ListViewItems);
|
||||
listView.ShowColumnHeaders = widgetDesc.ShowColumnHeaders;
|
||||
listView.IsStriped = widgetDesc.IsStriped;
|
||||
|
||||
@@ -373,6 +373,8 @@ namespace OpenRCT2::Scripting
|
||||
dukglue_register_property(ctx, &ScListViewWidget::highlightedCell_get, nullptr, "highlightedCell");
|
||||
dukglue_register_property(
|
||||
ctx, &ScListViewWidget::selectedCell_get, &ScListViewWidget::selectedCell_set, "selectedCell");
|
||||
dukglue_register_property(ctx, &ScListViewWidget::columns_get, &ScListViewWidget::columns_set, "columns");
|
||||
dukglue_register_property(ctx, &ScListViewWidget::items_get, &ScListViewWidget::items_set, "items");
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -464,6 +466,53 @@ namespace OpenRCT2::Scripting
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<std::string>> items_get()
|
||||
{
|
||||
std::vector<std::vector<std::string>> result;
|
||||
auto listView = GetListView();
|
||||
if (listView != nullptr)
|
||||
{
|
||||
for (const auto& item : listView->GetItems())
|
||||
{
|
||||
result.push_back(item.Cells);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void items_set(const DukValue& value)
|
||||
{
|
||||
auto listView = GetListView();
|
||||
if (listView != nullptr)
|
||||
{
|
||||
listView->SetItems(FromDuk<std::vector<ListViewItem>>(value));
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<DukValue> columns_get()
|
||||
{
|
||||
std::vector<DukValue> result;
|
||||
auto listView = GetListView();
|
||||
if (listView != nullptr)
|
||||
{
|
||||
auto ctx = GetContext()->GetScriptEngine().GetContext();
|
||||
for (const auto& column : listView->GetColumns())
|
||||
{
|
||||
result.push_back(ToDuk(ctx, column));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void columns_set(const DukValue& value)
|
||||
{
|
||||
auto listView = GetListView();
|
||||
if (listView != nullptr)
|
||||
{
|
||||
listView->SetColumns(FromDuk<std::vector<ListViewColumn>>(value));
|
||||
}
|
||||
}
|
||||
|
||||
CustomListView* GetListView() const
|
||||
{
|
||||
auto w = GetWindow();
|
||||
|
||||
@@ -113,6 +113,20 @@ namespace OpenRCT2::Scripting
|
||||
duk_put_prop_string(_ctx, _idx, name);
|
||||
}
|
||||
|
||||
template<typename T> void Set(const char* name, const std::optional<T>& value)
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
EnsureObjectPushed();
|
||||
duk_push_null(_ctx);
|
||||
duk_put_prop_string(_ctx, _idx, name);
|
||||
}
|
||||
else
|
||||
{
|
||||
Set(name, *value);
|
||||
}
|
||||
}
|
||||
|
||||
DukValue Take()
|
||||
{
|
||||
EnsureObjectPushed();
|
||||
@@ -199,6 +213,16 @@ namespace OpenRCT2::Scripting
|
||||
duk_push_null(ctx);
|
||||
return DukValue::take_from_stack(ctx);
|
||||
}
|
||||
template<> inline DukValue ToDuk(duk_context* ctx, const std::string_view& value)
|
||||
{
|
||||
duk_push_lstring(ctx, value.data(), value.size());
|
||||
return DukValue::take_from_stack(ctx);
|
||||
}
|
||||
template<size_t TLen> inline DukValue ToDuk(duk_context* ctx, const char (&value)[TLen])
|
||||
{
|
||||
duk_push_string(ctx, value);
|
||||
return DukValue::take_from_stack(ctx);
|
||||
}
|
||||
template<typename T> DukValue ToDuk(duk_context* ctx, const std::optional<T>& value)
|
||||
{
|
||||
return value ? ToDuk(ctx, *value) : ToDuk(ctx, nullptr);
|
||||
|
||||
Reference in New Issue
Block a user