2018-06-17 15:48:42 +02:00
|
|
|
/*****************************************************************************
|
2020-04-22 17:06:29 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2018-06-17 15:48:42 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2018-03-24 20:30:10 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-04-24 16:58:01 +02:00
|
|
|
#ifdef ENABLE_SCRIPTING
|
2020-02-15 00:36:24 +01:00
|
|
|
|
2020-02-23 16:33:01 +01:00
|
|
|
# include "ScWidget.hpp"
|
|
|
|
|
|
|
|
# include <openrct2/common.h>
|
|
|
|
# include <openrct2/interface/Window.h>
|
|
|
|
# include <openrct2/interface/Window_internal.h>
|
2020-02-27 23:13:49 +01:00
|
|
|
# include <openrct2/localisation/Language.h>
|
2020-02-23 16:33:01 +01:00
|
|
|
# include <openrct2/scripting/Duktape.hpp>
|
2018-07-31 17:59:48 +02:00
|
|
|
|
2018-03-24 20:30:10 +01:00
|
|
|
namespace OpenRCT2::Scripting
|
|
|
|
{
|
2020-02-22 14:29:18 +01:00
|
|
|
using namespace OpenRCT2::Ui::Windows;
|
|
|
|
|
2018-03-24 20:30:10 +01:00
|
|
|
class ScWindow
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
rct_windowclass _class;
|
|
|
|
rct_windownumber _number;
|
|
|
|
|
|
|
|
public:
|
2018-07-31 17:59:48 +02:00
|
|
|
ScWindow(rct_window* w)
|
2018-05-24 14:49:11 +02:00
|
|
|
: ScWindow(w->classification, w->number)
|
2018-03-25 19:23:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-24 20:30:10 +01:00
|
|
|
ScWindow(rct_windowclass c, rct_windownumber n)
|
2018-07-31 17:59:48 +02:00
|
|
|
: _class(c)
|
|
|
|
, _number(n)
|
2018-03-24 20:30:10 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:18:58 +02:00
|
|
|
int32_t classification_get() const
|
2020-02-24 20:25:31 +01:00
|
|
|
{
|
|
|
|
return static_cast<int32_t>(_class);
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:18:58 +02:00
|
|
|
int32_t number_get() const
|
2020-02-24 20:25:31 +01:00
|
|
|
{
|
|
|
|
return static_cast<int32_t>(_number);
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:18:58 +02:00
|
|
|
int32_t x_get() const
|
2018-07-31 17:59:48 +02:00
|
|
|
{
|
2020-05-12 19:53:37 +02:00
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
return w->windowPos.x;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-07-31 17:59:48 +02:00
|
|
|
}
|
2018-07-31 16:25:50 +02:00
|
|
|
void x_set(int32_t value)
|
2018-03-24 20:30:10 +01:00
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
2020-05-12 19:53:37 +02:00
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_set_position(w, { value, w->windowPos.y });
|
|
|
|
}
|
2018-03-24 20:30:10 +01:00
|
|
|
}
|
2020-04-24 22:18:58 +02:00
|
|
|
int32_t y_get() const
|
2018-07-31 17:59:48 +02:00
|
|
|
{
|
2020-05-12 19:53:37 +02:00
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
return w->windowPos.y;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-07-31 17:59:48 +02:00
|
|
|
}
|
2018-07-31 16:25:50 +02:00
|
|
|
void y_set(int32_t value)
|
2018-03-24 20:30:10 +01:00
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
2020-05-12 19:53:37 +02:00
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_set_position(w, { w->windowPos.x, value });
|
|
|
|
}
|
2018-03-24 20:30:10 +01:00
|
|
|
}
|
2020-04-24 22:18:58 +02:00
|
|
|
int32_t width_get() const
|
2018-07-31 17:59:48 +02:00
|
|
|
{
|
2020-04-30 21:32:39 +02:00
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
return w->width;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void width_set(int32_t value)
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_resize(w, value - w->width, 0);
|
|
|
|
}
|
2018-07-31 17:59:48 +02:00
|
|
|
}
|
2020-04-24 22:18:58 +02:00
|
|
|
int32_t height_get() const
|
2018-07-31 17:59:48 +02:00
|
|
|
{
|
2020-04-30 21:32:39 +02:00
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
return w->height;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void height_set(int32_t value)
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_resize(w, 0, value - w->height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t minWidth_get() const
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
return w->min_width;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void minWidth_set(int32_t value)
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_set_resize(w, value, w->min_height, w->max_width, w->max_height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t maxWidth_get() const
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
return w->max_width;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void maxWidth_set(int32_t value)
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_set_resize(w, w->min_width, w->min_height, value, w->max_height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t minHeight_get() const
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
return w->min_height;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void minHeight_set(int32_t value)
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_set_resize(w, w->min_width, value, w->max_width, w->max_height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int32_t maxHeight_get() const
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
return w->max_height;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void maxHeight_set(int32_t value)
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_set_resize(w, w->min_width, w->min_height, w->max_width, value);
|
|
|
|
}
|
2018-07-31 17:59:48 +02:00
|
|
|
}
|
2020-04-24 22:18:58 +02:00
|
|
|
bool isSticky_get() const
|
2018-03-24 20:30:10 +01:00
|
|
|
{
|
|
|
|
auto flags = GetWindow()->flags;
|
|
|
|
return (flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)) != 0;
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:05:56 +02:00
|
|
|
std::vector<DukValue> widgets_get() const
|
2020-02-15 00:36:24 +01:00
|
|
|
{
|
2020-05-04 01:05:56 +02:00
|
|
|
auto ctx = GetContext()->GetScriptEngine().GetContext();
|
|
|
|
|
|
|
|
std::vector<DukValue> result;
|
2020-02-15 00:36:24 +01:00
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
rct_widgetindex widgetIndex = 0;
|
|
|
|
for (auto widget = w->widgets; widget->type != WWT_LAST; widget++)
|
|
|
|
{
|
2020-05-06 23:34:56 +02:00
|
|
|
result.push_back(ScWidget::ToDukValue(ctx, w, widgetIndex));
|
2020-02-15 00:36:24 +01:00
|
|
|
widgetIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:18:58 +02:00
|
|
|
std::vector<int32_t> colours_get() const
|
2020-02-27 20:59:46 +01:00
|
|
|
{
|
|
|
|
std::vector<int32_t> result;
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
result.reserve(std::size(w->colours));
|
|
|
|
for (auto c : w->colours)
|
|
|
|
{
|
|
|
|
result.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
void colours_set(std::vector<int32_t> colours)
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
2020-02-28 09:09:52 +01:00
|
|
|
for (size_t i = 0; i < std::size(w->colours); i++)
|
2020-02-27 20:59:46 +01:00
|
|
|
{
|
|
|
|
w->colours[i] = i < colours.size() ? std::clamp<int32_t>(colours[i], COLOUR_BLACK, COLOUR_COUNT - 1)
|
|
|
|
: COLOUR_BLACK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:18:58 +02:00
|
|
|
std::string title_get() const
|
2020-02-27 23:13:49 +01:00
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr && w->classification == WC_CUSTOM)
|
|
|
|
{
|
2020-02-27 23:25:26 +01:00
|
|
|
return language_convert_string_to_tokens(GetWindowTitle(w));
|
2020-02-27 23:13:49 +01:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
void title_set(std::string value)
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr && w->classification == WC_CUSTOM)
|
|
|
|
{
|
|
|
|
UpdateWindowTitle(w, language_convert_string(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 21:32:39 +02:00
|
|
|
int32_t tabIndex_get() const
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr && w->classification == WC_CUSTOM)
|
|
|
|
{
|
|
|
|
return w->page;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-22 14:29:18 +01:00
|
|
|
void close()
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_close(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:05:56 +02:00
|
|
|
DukValue findWidget(std::string name) const
|
2020-02-22 14:29:18 +01:00
|
|
|
{
|
2020-05-04 01:05:56 +02:00
|
|
|
auto ctx = GetContext()->GetScriptEngine().GetContext();
|
2020-02-22 14:29:18 +01:00
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
auto widgetIndex = FindWidgetIndexByName(w, name);
|
|
|
|
if (widgetIndex)
|
|
|
|
{
|
2020-05-06 23:34:56 +02:00
|
|
|
return ScWidget::ToDukValue(ctx, w, *widgetIndex);
|
2020-02-22 14:29:18 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 01:05:56 +02:00
|
|
|
return GetObjectAsDukValue<ScWidget>(ctx, nullptr);
|
2020-02-22 14:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void bringToFront()
|
|
|
|
{
|
|
|
|
auto w = GetWindow();
|
|
|
|
if (w != nullptr)
|
|
|
|
{
|
|
|
|
window_bring_to_front(w);
|
|
|
|
w->flags |= WF_WHITE_BORDER_MASK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 17:59:48 +02:00
|
|
|
static void Register(duk_context* ctx)
|
2018-03-24 20:30:10 +01:00
|
|
|
{
|
2020-02-24 20:25:31 +01:00
|
|
|
dukglue_register_property(ctx, &ScWindow::classification_get, nullptr, "classification");
|
|
|
|
dukglue_register_property(ctx, &ScWindow::number_get, nullptr, "number");
|
2018-03-24 20:30:10 +01:00
|
|
|
dukglue_register_property(ctx, &ScWindow::x_get, &ScWindow::x_set, "x");
|
|
|
|
dukglue_register_property(ctx, &ScWindow::y_get, &ScWindow::y_set, "y");
|
2020-04-30 21:32:39 +02:00
|
|
|
dukglue_register_property(ctx, &ScWindow::width_get, &ScWindow::width_set, "width");
|
|
|
|
dukglue_register_property(ctx, &ScWindow::height_get, &ScWindow::height_set, "height");
|
|
|
|
dukglue_register_property(ctx, &ScWindow::minWidth_get, &ScWindow::minWidth_set, "minWidth");
|
|
|
|
dukglue_register_property(ctx, &ScWindow::maxWidth_get, &ScWindow::maxWidth_set, "maxWidth");
|
|
|
|
dukglue_register_property(ctx, &ScWindow::minHeight_get, &ScWindow::minHeight_set, "minHeight");
|
|
|
|
dukglue_register_property(ctx, &ScWindow::maxHeight_get, &ScWindow::maxHeight_set, "maxHeight");
|
2018-03-24 20:30:10 +01:00
|
|
|
dukglue_register_property(ctx, &ScWindow::isSticky_get, nullptr, "isSticky");
|
2020-02-15 00:36:24 +01:00
|
|
|
dukglue_register_property(ctx, &ScWindow::widgets_get, nullptr, "widgets");
|
2020-02-27 20:59:46 +01:00
|
|
|
dukglue_register_property(ctx, &ScWindow::colours_get, &ScWindow::colours_set, "colours");
|
2020-02-27 23:13:49 +01:00
|
|
|
dukglue_register_property(ctx, &ScWindow::title_get, &ScWindow::title_set, "title");
|
2020-04-30 21:32:39 +02:00
|
|
|
dukglue_register_property(ctx, &ScWindow::tabIndex_get, nullptr, "tabIndex");
|
2020-02-22 14:29:18 +01:00
|
|
|
|
2020-02-22 19:04:27 +01:00
|
|
|
dukglue_register_method(ctx, &ScWindow::close, "close");
|
|
|
|
dukglue_register_method(ctx, &ScWindow::findWidget, "findWidget");
|
|
|
|
dukglue_register_method(ctx, &ScWindow::bringToFront, "bringToFront");
|
2018-03-24 20:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-07-31 17:59:48 +02:00
|
|
|
rct_window* GetWindow() const
|
2018-03-24 20:30:10 +01:00
|
|
|
{
|
|
|
|
return window_find_by_number(_class, _number);
|
|
|
|
}
|
|
|
|
};
|
2018-07-31 17:59:48 +02:00
|
|
|
} // namespace OpenRCT2::Scripting
|
2020-02-23 16:33:01 +01:00
|
|
|
|
|
|
|
#endif
|