Add ScreenToPercent and PercentToScreen.

This commit is contained in:
hispidence 2021-08-29 20:03:51 +01:00
parent 75e6f14287
commit 501be9c594
2 changed files with 43 additions and 0 deletions

View file

@ -168,6 +168,26 @@ static void MakeSpecialTable(sol::state * state, std::string const & name, funcI
meta.set_function("__newindex", fni, objPtr);
}
static std::tuple<int, int> PercentToScreen(double x, double y)
{
auto fWidth = static_cast<double>(g_Configuration.Width);
auto fHeight = static_cast<double>(g_Configuration.Height);
int resX = std::round(fWidth / 100.0 * x);
int resY = std::round(fHeight / 100.0 * y);
//todo this still assumes a resolution of 800/600. account for this somehow
return std::make_tuple(resX, resY);
}
static std::tuple<double, double> ScreenToPercent(int x, int y)
{
auto fWidth = static_cast<double>(g_Configuration.Width);
auto fHeight = static_cast<double>(g_Configuration.Height);
double resX = x/fWidth * 100.0;
double resY = y/fHeight * 100.0;
return std::make_tuple(resX, resY);
}
GameScript::GameScript(sol::state* lua) : LuaHandler{ lua }
{
/*** Ambience and music
@ -315,6 +335,27 @@ with a call to @{ShowString}, or this function will have no effect.
*/
m_lua->set_function(ScriptReserved_HideString, [this](GameScriptDisplayString const& s) {ShowString(s, 0.0f); });
/***
Translate a pair of percentages to screen-space pixel coordinates.
To be used with @{DisplayString:SetPos} and @{DisplayString.new}.
@function PercentToScreen
@tparam float x percent value to translate to x-coordinate
@tparam float y percent value to translate to y-coordinate
@treturn int x x coordinate in pixels
@treturn int y y coordinate in pixels
*/
m_lua->set_function(ScriptReserved_PercentToScreen, &PercentToScreen);
/***
Translate a pair of coordinates to percentages of window dimensions.
To be used with @{DisplayString:GetPos}.
@function ScreenToPercent
@tparam int x pixel value to translate to a percentage of the window width
@tparam int y pixel value to translate to a percentage of the window height
@treturn float x coordinate as percentage
@treturn float y coordinate as percentage
*/
m_lua->set_function(ScriptReserved_ScreenToPercent, &ScreenToPercent);
MakeReadOnlyTable(ScriptReserved_ObjID, kObjIDs);
ResetLevelTables();