From f655d5706915b634b6dabe34f40dde6101ff679e Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 25 Apr 2023 09:03:20 -0300 Subject: [PATCH] [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 --- src/app/script/editor_class.cpp | 39 ++++++++++++++++++++++++++++++--- src/app/ui/editor/editor.h | 4 ++++ src/ui/display.h | 6 ++++- src/ui/manager.cpp | 10 +++++++-- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/app/script/editor_class.cpp b/src/app/script/editor_class.cpp index 9c671e0b2..b0937c963 100644 --- a/src/app/script/editor_class.cpp +++ b/src/app/script/editor_class.cpp @@ -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(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(L, 1); + obj->cancel(); + return 0; +} + int Editor_get_sprite(lua_State* L) { auto obj = get_obj(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(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(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 } }; diff --git a/src/app/ui/editor/editor.h b/src/app/ui/editor/editor.h index ea34c4818..be23fb28e 100644 --- a/src/app/ui/editor/editor.h +++ b/src/app/ui/editor/editor.h @@ -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; diff --git a/src/ui/display.h b/src/ui/display.h index 662ac838a..7eaa3adc5 100644 --- a/src/ui/display.h +++ b/src/ui/display.h @@ -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 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 diff --git a/src/ui/manager.cpp b/src/ui/manager.cpp index fce4b4d32..b10a8eba5 100644 --- a/src/ui/manager.cpp +++ b/src/ui/manager.cpp @@ -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();