[lua] Add app.editor.zoom / scroll properties (fix #4722)

This commit is contained in:
David Capello 2024-11-14 12:25:02 -03:00
parent 7ff124d108
commit 03361fdd07
3 changed files with 103 additions and 19 deletions

View File

@ -302,6 +302,71 @@ int Editor_get_mousePos(lua_State* L)
return 1;
}
int Editor_get_zoom(lua_State* L)
{
auto obj = get_obj<EditorObj>(L, 1);
lua_pushnumber(L, obj->editor()->zoom().scale());
return 1;
}
int Editor_get_scroll(lua_State* L)
{
auto obj = get_obj<EditorObj>(L, 1);
gfx::PointF scroll = obj->editor()->spritePointInCenter();
// TODO add PointF wrapper or Vec2/Vec3/Vec4 classes/metatables.
lua_newtable(L);
lua_pushnumber(L, scroll.x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, scroll.y);
lua_setfield(L, -2, "y");
return 1;
}
int Editor_set_zoom(lua_State* L)
{
auto obj = get_obj<EditorObj>(L, 1);
auto scale = lua_tonumber(L, 2);
obj->editor()->setZoomAndCenterInMouse(
render::Zoom::fromScale(scale),
gfx::Point(),
Editor::ZoomBehavior::CENTER);
return 0;
}
int Editor_set_scroll(lua_State* L)
{
auto obj = get_obj<EditorObj>(L, 1);
gfx::PointF pt;
// TODO add PointF wrapper or Vec2/Vec3/Vec4 classes/metatables.
const int index = 2;
if (lua_istable(L, index)) {
const int type = lua_getfield(L, index, "x");
if (VALID_LUATYPE(type)) {
lua_getfield(L, index, "y");
pt.x = lua_tonumber(L, -2);
pt.y = lua_tonumber(L, -1);
lua_pop(L, 2);
}
else {
lua_pop(L, 1);
lua_geti(L, index, 1);
pt.x = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_geti(L, index, 2);
pt.y = lua_tonumber(L, -1);
lua_pop(L, 1);
}
}
else {
pt = convert_args_into_point(L, index);
}
obj->editor()->centerInSpritePoint(pt);
return 0;
}
const luaL_Reg Editor_methods[] = {
{ "__gc", Editor_gc },
{ "__eq", Editor_eq },
@ -314,6 +379,8 @@ const Property Editor_properties[] = {
{ "sprite", Editor_get_sprite, nullptr },
{ "spritePos", Editor_get_spritePos, nullptr },
{ "mousePos", Editor_get_mousePos, nullptr },
{ "zoom", Editor_get_zoom, Editor_set_zoom },
{ "scroll", Editor_get_scroll, Editor_set_scroll },
{ nullptr, nullptr, nullptr }
};

View File

@ -1519,7 +1519,7 @@ bool Editor::isAutoSelectLayer()
return App::instance()->contextBar()->isAutoSelectLayer();
}
gfx::Point Editor::screenToEditor(const gfx::Point& pt)
gfx::Point Editor::screenToEditor(const gfx::Point& pt) const
{
View* view = View::getView(this);
Rect vp = view->viewportBounds();
@ -1529,7 +1529,7 @@ gfx::Point Editor::screenToEditor(const gfx::Point& pt)
m_proj.removeY(pt.y - vp.y + scroll.y - m_padding.y));
}
gfx::Point Editor::screenToEditorCeiling(const gfx::Point& pt)
gfx::Point Editor::screenToEditorCeiling(const gfx::Point& pt) const
{
View* view = View::getView(this);
Rect vp = view->viewportBounds();
@ -1540,7 +1540,7 @@ gfx::Point Editor::screenToEditorCeiling(const gfx::Point& pt)
}
gfx::PointF Editor::screenToEditorF(const gfx::Point& pt)
gfx::PointF Editor::screenToEditorF(const gfx::Point& pt) const
{
View* view = View::getView(this);
Rect vp = view->viewportBounds();
@ -1550,7 +1550,7 @@ gfx::PointF Editor::screenToEditorF(const gfx::Point& pt)
m_proj.removeY<double>(pt.y - vp.y + scroll.y - m_padding.y));
}
Point Editor::editorToScreen(const gfx::Point& pt)
Point Editor::editorToScreen(const gfx::Point& pt) const
{
View* view = View::getView(this);
Rect vp = view->viewportBounds();
@ -1560,7 +1560,7 @@ Point Editor::editorToScreen(const gfx::Point& pt)
(vp.y - scroll.y + m_padding.y + m_proj.applyY(pt.y)));
}
gfx::PointF Editor::editorToScreenF(const gfx::PointF& pt)
gfx::PointF Editor::editorToScreenF(const gfx::PointF& pt) const
{
View* view = View::getView(this);
Rect vp = view->viewportBounds();
@ -1570,21 +1570,21 @@ gfx::PointF Editor::editorToScreenF(const gfx::PointF& pt)
(vp.y - scroll.y + m_padding.y + m_proj.applyY<double>(pt.y)));
}
Rect Editor::screenToEditor(const Rect& rc)
Rect Editor::screenToEditor(const Rect& rc) const
{
return gfx::Rect(
screenToEditor(rc.origin()),
screenToEditorCeiling(rc.point2()));
}
Rect Editor::editorToScreen(const Rect& rc)
Rect Editor::editorToScreen(const Rect& rc) const
{
return gfx::Rect(
editorToScreen(rc.origin()),
editorToScreen(rc.point2()));
}
gfx::RectF Editor::editorToScreenF(const gfx::RectF& rc)
gfx::RectF Editor::editorToScreenF(const gfx::RectF& rc) const
{
return gfx::RectF(
editorToScreenF(rc.origin()),
@ -1635,21 +1635,36 @@ Rect Editor::getVisibleSpriteBounds()
}
// Changes the scroll to see the given point as the center of the editor.
void Editor::centerInSpritePoint(const gfx::Point& spritePos)
void Editor::centerInSpritePoint(const gfx::PointF& spritePos)
{
HideBrushPreview hide(m_brushPreview);
View* view = View::getView(this);
Rect vp = view->viewportBounds();
gfx::Point scroll(
m_padding.x - (vp.w/2) + m_proj.applyX(1)/2 + m_proj.applyX(spritePos.x),
m_padding.y - (vp.h/2) + m_proj.applyY(1)/2 + m_proj.applyY(spritePos.y));
m_padding.x - (vp.w/2) + m_proj.applyX(spritePos.x),
m_padding.y - (vp.h/2) + m_proj.applyY(spritePos.y));
updateEditor(false);
setEditorScroll(scroll);
invalidate();
}
void Editor::centerInSpritePoint(const gfx::Point& spritePos)
{
centerInSpritePoint(gfx::PointF(spritePos.x + 0.5,
spritePos.y + 0.5));
}
gfx::PointF Editor::spritePointInCenter() const
{
View* view = View::getView(this);
Rect vp = view->viewportBounds();
gfx::Point screenPos(vp.x + vp.w/2,
vp.y + vp.h/2);
return screenToEditorF(screenPos);
}
void Editor::updateStatusBar()
{
if (!hasMouse())

View File

@ -182,14 +182,14 @@ namespace app {
// editor/sprite coordinates (pixel in the canvas).
//
// TODO we should rename these functions to displayToEditor() and editorToDisplay()
gfx::Point screenToEditor(const gfx::Point& pt);
gfx::Point screenToEditorCeiling(const gfx::Point& pt);
gfx::PointF screenToEditorF(const gfx::Point& pt);
gfx::Point editorToScreen(const gfx::Point& pt);
gfx::PointF editorToScreenF(const gfx::PointF& pt);
gfx::Rect screenToEditor(const gfx::Rect& rc);
gfx::Rect editorToScreen(const gfx::Rect& rc);
gfx::RectF editorToScreenF(const gfx::RectF& rc);
gfx::Point screenToEditor(const gfx::Point& pt) const;
gfx::Point screenToEditorCeiling(const gfx::Point& pt) const;
gfx::PointF screenToEditorF(const gfx::Point& pt) const;
gfx::Point editorToScreen(const gfx::Point& pt) const;
gfx::PointF editorToScreenF(const gfx::PointF& pt) const;
gfx::Rect screenToEditor(const gfx::Rect& rc) const;
gfx::Rect editorToScreen(const gfx::Rect& rc) const;
gfx::RectF editorToScreenF(const gfx::RectF& rc) const;
void add_observer(EditorObserver* observer);
void remove_observer(EditorObserver* observer);
@ -213,7 +213,9 @@ namespace app {
void collapseRegionByTiledMode(gfx::Region& rgn) const;
// Changes the scroll to see the given point as the center of the editor.
void centerInSpritePoint(const gfx::PointF& spritePos);
void centerInSpritePoint(const gfx::Point& spritePos);
gfx::PointF spritePointInCenter() const;
void updateStatusBar();