[lua] Add app.editor.spritePos/mousePos and app.editor:cancel()

Added properties needed to know where the mouse position is on the
editor canvas + a method to cancel app.editor:askPoint() from Lua.

Related to aseprite/Attachment-System#102
This commit is contained in:
David Capello 2023-04-25 09:03:20 -03:00
parent 2baf291b4a
commit f655d57069
4 changed files with 53 additions and 6 deletions

View File

@ -15,6 +15,7 @@
#include "app/script/registry.h"
#include "app/ui/editor/editor.h"
#include "app/ui/editor/select_box_state.h"
#include "ui/display.h"
#ifdef ENABLE_UI
@ -94,10 +95,10 @@ public:
RegistryRef&& onclick,
RegistryRef&& onchange,
RegistryRef&& oncancel) {
// Cancel previous askPoint()
if (m_askPoint) {
onQuickboxCancel(m_editor);
ASSERT(!m_askPoint);
// Don't call onQuickboxCancel() to avoid calling oncancel, the
// script should know that it called askPoint() previously.
m_editor->backToPreviousState();
}
m_askPoint = std::make_unique<AskPoint>(L);
@ -119,6 +120,13 @@ public:
m_editor->setState(state);
}
void cancel() {
if (m_askPoint) {
m_askPoint.reset();
m_editor->backToPreviousState();
}
}
// EditorObserver impl
void onDestroyEditor(Editor* editor) override {
ASSERT(editor == m_editor);
@ -250,6 +258,13 @@ int Editor_askPoint(lua_State* L)
return 0;
}
int Editor_cancel(lua_State* L)
{
auto obj = get_obj<EditorObj>(L, 1);
obj->cancel();
return 0;
}
int Editor_get_sprite(lua_State* L)
{
auto obj = get_obj<EditorObj>(L, 1);
@ -257,15 +272,33 @@ int Editor_get_sprite(lua_State* L)
return 1;
}
int Editor_get_spritePos(lua_State* L)
{
auto obj = get_obj<EditorObj>(L, 1);
push_obj(L, obj->editor()->screenToEditor(
obj->editor()->display()->lastMousePos()));
return 1;
}
int Editor_get_mousePos(lua_State* L)
{
auto obj = get_obj<EditorObj>(L, 1);
push_obj(L, obj->editor()->display()->lastMousePos());
return 1;
}
const luaL_Reg Editor_methods[] = {
{ "__gc", Editor_gc },
{ "__eq", Editor_eq },
{ "askPoint", Editor_askPoint },
{ "cancel", Editor_cancel },
{ nullptr, nullptr }
};
const Property Editor_properties[] = {
{ "sprite", Editor_get_sprite, nullptr },
{ "spritePos", Editor_get_spritePos, nullptr },
{ "mousePos", Editor_get_mousePos, nullptr },
{ nullptr, nullptr, nullptr }
};

View File

@ -450,6 +450,10 @@ namespace app {
DocView* m_docView;
// Last known mouse position received by this editor when the
// mouse button was pressed. Used for auto-scrolling. To get the
// current mouse position on the editor you can use
// ui::Display::lastMousePos().
gfx::Point m_oldPos;
EditorFlags m_flags;

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2019-2023 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -75,6 +75,9 @@ namespace ui {
gfx::Size workareaSizeUIScale();
const gfx::Point& lastMousePos() const { return m_lastMousePos; }
void updateLastMousePos(const gfx::Point& pos) { m_lastMousePos = pos; }
void _setParentDisplay(Display* parentDisplay) {
m_parentDisplay = parentDisplay;
}
@ -86,6 +89,7 @@ namespace ui {
std::vector<Window*> m_windows; // Sub-windows in this display
gfx::Region m_invalidRegion; // Invalid region (we didn't receive paint messages yet for this).
gfx::Region m_dirtyRegion; // Region to flip to the os::Display
gfx::Point m_lastMousePos;
};
} // namespace ui

View File

@ -784,8 +784,14 @@ void Manager::handleWindowZOrder()
void Manager::updateMouseWidgets(const gfx::Point& mousePos,
Display* display)
{
gfx::Point screenPos = (display ? display->nativeWindow()->pointToScreen(mousePos):
mousePos);
gfx::Point screenPos;
if (display) {
screenPos = display->nativeWindow()->pointToScreen(mousePos);
display->updateLastMousePos(mousePos);
}
else {
screenPos = mousePos;
}
// Get the list of widgets to send mouse messages.
mouse_widgets_list.clear();