1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-14 01:19:59 +00:00

Merge branch 'kantoniak/lua-custom-markers' into 'master'

Draft: Lua API: Add read access to custom markers

See merge request OpenMW/openmw!4265
This commit is contained in:
Chris Antoniak 2025-03-10 02:43:37 -07:00
commit 56e35e6bee
4 changed files with 72 additions and 0 deletions

View File

@ -11,6 +11,7 @@
#include <MyGUI_KeyCode.h>
#include "../mwgui/mapwindow.hpp"
#include "../mwgui/mode.hpp"
#include <components/sdlutil/events.hpp>
@ -237,6 +238,7 @@ namespace MWBase
virtual void notifyInputActionBound() = 0;
virtual void addVisitedLocation(const std::string& name, int x, int y) = 0;
virtual MWGui::CustomMarkerCollection::RangeType getCustomMarkers() const = 0;
/// Hides dialog and schedules dialog to be deleted.
virtual void removeDialog(std::unique_ptr<MWGui::Layout>&& dialog) = 0;

View File

@ -1652,6 +1652,11 @@ namespace MWGui
mMap->addVisitedLocation(name, x, y);
}
MWGui::CustomMarkerCollection::RangeType WindowManager::getCustomMarkers() const
{
return std::make_pair(mCustomMarkers.begin(), mCustomMarkers.end());
}
const Translation::Storage& WindowManager::getTranslationDataStorage() const
{
return mTranslationDataStorage;

View File

@ -255,6 +255,7 @@ namespace MWGui
void notifyInputActionBound() override;
void addVisitedLocation(const std::string& name, int x, int y) override;
MWGui::CustomMarkerCollection::RangeType getCustomMarkers() const override;
/// Hides dialog and schedules dialog to be deleted.
void removeDialog(std::unique_ptr<Layout>&& dialog) override;

View File

@ -12,6 +12,7 @@
#include "../mwworld/cell.hpp"
#include "../mwworld/cellstore.hpp"
#include "../mwworld/scene.hpp"
#include "../mwworld/worldmodel.hpp"
#include "luamanagerimp.hpp"
#include "objectlists.hpp"
@ -41,8 +42,28 @@ namespace
}
}
namespace MWLua
{
struct CustomMarker
{
bool mMutable = false;
float mWorldX;
float mWorldY;
LCell mCell;
std::string mNote;
};
}
namespace sol
{
template <>
struct is_automagical<MWLua::CustomMarker> : std::false_type
{
};
template <>
struct is_automagical<MWPhysics::RayCastingResult> : std::false_type
{
@ -187,6 +208,49 @@ namespace MWLua
api["doors"] = LObjectList{ objectLists->getDoorsInScene() };
api["items"] = LObjectList{ objectLists->getItemsInScene() };
api["players"] = LObjectList{ objectLists->getPlayers() };
// FIXME: Make `customMarkers` a dynamic property rather than property of `function` type
api["customMarkers"] = sol::readonly_property([context]() {
// FIXME: Performance and list caching
auto worldModel = MWBase::Environment::get().getWorldModel();
auto esmCustomMarkers = MWBase::Environment::get().getWindowManager()->getCustomMarkers();
auto luaCustomMarkers = sol::table(context.mLua->sol(), sol::create);
size_t i = 0;
for (auto& it = esmCustomMarkers.first; it != esmCustomMarkers.second; it++, i++)
{
auto& esmCustomMarker = it->second;
LCell cell{ nullptr };
if (esmCustomMarker.mCell.is<ESM::ESM3ExteriorCellRefId>())
{
cell.mStore = &worldModel->getCell(esmCustomMarker.mCell);
}
else if (esmCustomMarker.mCell.is<ESM::StringRefId>())
{
cell.mStore = worldModel->findInterior(esmCustomMarker.mCell.getRefIdString());
}
else
{
// FIXME: Can it ever happen? What kind of error reporting to use?
}
luaCustomMarkers[i + 1] = CustomMarker{
.mWorldX = esmCustomMarker.mWorldX,
.mWorldY = esmCustomMarker.mWorldY,
.mCell = std::move(cell),
.mNote = esmCustomMarker.mNote,
};
}
return LuaUtil::makeReadOnly(luaCustomMarkers);
});
sol::usertype<CustomMarker> customMarkerType = context.mLua->sol().new_usertype<CustomMarker>("CustomMarker");
customMarkerType[sol::meta_function::to_string] = [](const CustomMarker& m) { return "CustomMarker[x=" + std::to_string(m.mWorldX) + ", y=" + std::to_string(m.mWorldY) + "]"; };
customMarkerType["worldX"] = sol::readonly_property([](const CustomMarker& m) { return m.mWorldX; });
customMarkerType["worldY"] = sol::readonly_property([](const CustomMarker& m) { return m.mWorldY; });
customMarkerType["cell"] = sol::readonly_property([](const CustomMarker& m) { return m.mCell; });
customMarkerType["note"] = sol::readonly_property([](const CustomMarker& m) { return m.mNote; });
api["NAVIGATOR_FLAGS"]
= LuaUtil::makeStrictReadOnly(LuaUtil::tableFromPairs<std::string_view, DetourNavigator::Flag>(lua,