mirror of
https://github.com/aseprite/aseprite.git
synced 2025-04-18 20:42:31 +00:00
Add basic scripting support to eval expression in --shell and DevConsole
This commit is contained in:
parent
699d60b03f
commit
5b17f44526
@ -158,9 +158,6 @@ else()
|
|||||||
include_directories(${LIBPNG_DIR})
|
include_directories(${LIBPNG_DIR})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Include duktape directory
|
|
||||||
include_directories(${DUKTAPE_DIR})
|
|
||||||
|
|
||||||
# Do not use MMX optimizations in PNG code
|
# Do not use MMX optimizations in PNG code
|
||||||
add_definitions(-DPNG_NO_MMX_CODE)
|
add_definitions(-DPNG_NO_MMX_CODE)
|
||||||
|
|
||||||
|
@ -67,6 +67,7 @@
|
|||||||
#include "doc/sprite.h"
|
#include "doc/sprite.h"
|
||||||
#include "render/render.h"
|
#include "render/render.h"
|
||||||
#include "scripting/engine.h"
|
#include "scripting/engine.h"
|
||||||
|
#include "scripting/engine_delegate.h"
|
||||||
#include "she/display.h"
|
#include "she/display.h"
|
||||||
#include "she/error.h"
|
#include "she/error.h"
|
||||||
#include "she/system.h"
|
#include "she/system.h"
|
||||||
@ -102,7 +103,6 @@ public:
|
|||||||
InputChain m_inputChain;
|
InputChain m_inputChain;
|
||||||
// This is a raw pointer because we want to delete this explicitly.
|
// This is a raw pointer because we want to delete this explicitly.
|
||||||
app::crash::DataRecovery* m_recovery;
|
app::crash::DataRecovery* m_recovery;
|
||||||
scripting::Engine m_scriptingEngine;
|
|
||||||
|
|
||||||
Modules(bool verbose)
|
Modules(bool verbose)
|
||||||
: m_loggerModule(verbose)
|
: m_loggerModule(verbose)
|
||||||
@ -131,6 +131,13 @@ public:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class StdoutEngineDelegate : public scripting::EngineDelegate {
|
||||||
|
public:
|
||||||
|
void onConsolePrint(const char* text) override {
|
||||||
|
printf("%s\n", text);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
App* App::m_instance = NULL;
|
App* App::m_instance = NULL;
|
||||||
|
|
||||||
App::App()
|
App::App()
|
||||||
@ -559,13 +566,10 @@ void App::run()
|
|||||||
|
|
||||||
// Start shell to execute scripts.
|
// Start shell to execute scripts.
|
||||||
if (m_isShell) {
|
if (m_isShell) {
|
||||||
if (m_modules->m_scriptingEngine.supportEval()) {
|
StdoutEngineDelegate delegate;
|
||||||
|
scripting::Engine engine(&delegate);
|
||||||
Shell shell;
|
Shell shell;
|
||||||
shell.run(m_modules->m_scriptingEngine);
|
shell.run(engine);
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::cerr << "Your version of " PACKAGE " wasn't compiled with shell support.\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy all documents in the UIContext.
|
// Destroy all documents in the UIContext.
|
||||||
|
@ -61,9 +61,10 @@ protected:
|
|||||||
|
|
||||||
DevConsoleView::DevConsoleView()
|
DevConsoleView::DevConsoleView()
|
||||||
: Box(JI_VERTICAL)
|
: Box(JI_VERTICAL)
|
||||||
, m_textBox("Welcome Aseprite Console\n(experimental)", JI_LEFT)
|
, m_textBox("Welcome to Aseprite JavaScript Console\n(Experimental)", JI_LEFT)
|
||||||
, m_label(">")
|
, m_label(">")
|
||||||
, m_entry(new CommmandEntry)
|
, m_entry(new CommmandEntry)
|
||||||
|
, m_engine(this)
|
||||||
{
|
{
|
||||||
SkinTheme* theme = static_cast<SkinTheme*>(getTheme());
|
SkinTheme* theme = static_cast<SkinTheme*>(getTheme());
|
||||||
|
|
||||||
@ -130,7 +131,13 @@ bool DevConsoleView::onProcessMessage(Message* msg)
|
|||||||
|
|
||||||
void DevConsoleView::onExecuteCommand(const std::string& cmd)
|
void DevConsoleView::onExecuteCommand(const std::string& cmd)
|
||||||
{
|
{
|
||||||
m_textBox.setText(m_textBox.getText() + "\n" + cmd);
|
m_engine.eval(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DevConsoleView::onConsolePrint(const char* text)
|
||||||
|
{
|
||||||
|
if (text)
|
||||||
|
m_textBox.setText(m_textBox.getText() + "\n" + text);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace app
|
} // namespace app
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
#include "app/ui/tabs.h"
|
#include "app/ui/tabs.h"
|
||||||
#include "app/ui/workspace_view.h"
|
#include "app/ui/workspace_view.h"
|
||||||
|
#include "scripting/engine.h"
|
||||||
|
#include "scripting/engine_delegate.h"
|
||||||
#include "ui/box.h"
|
#include "ui/box.h"
|
||||||
#include "ui/label.h"
|
#include "ui/label.h"
|
||||||
#include "ui/textbox.h"
|
#include "ui/textbox.h"
|
||||||
@ -19,7 +21,8 @@
|
|||||||
namespace app {
|
namespace app {
|
||||||
class DevConsoleView : public ui::Box
|
class DevConsoleView : public ui::Box
|
||||||
, public TabView
|
, public TabView
|
||||||
, public WorkspaceView {
|
, public WorkspaceView
|
||||||
|
, public scripting::EngineDelegate {
|
||||||
public:
|
public:
|
||||||
DevConsoleView();
|
DevConsoleView();
|
||||||
~DevConsoleView();
|
~DevConsoleView();
|
||||||
@ -36,6 +39,9 @@ namespace app {
|
|||||||
bool onCloseView(Workspace* workspace) override;
|
bool onCloseView(Workspace* workspace) override;
|
||||||
void onTabPopup(Workspace* workspace) override;
|
void onTabPopup(Workspace* workspace) override;
|
||||||
|
|
||||||
|
// EngineDelegate impl
|
||||||
|
virtual void onConsolePrint(const char* text) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool onProcessMessage(ui::Message* msg) override;
|
bool onProcessMessage(ui::Message* msg) override;
|
||||||
void onExecuteCommand(const std::string& cmd);
|
void onExecuteCommand(const std::string& cmd);
|
||||||
@ -48,6 +54,7 @@ namespace app {
|
|||||||
ui::HBox m_bottomBox;
|
ui::HBox m_bottomBox;
|
||||||
ui::Label m_label;
|
ui::Label m_label;
|
||||||
CommmandEntry* m_entry;
|
CommmandEntry* m_entry;
|
||||||
|
scripting::Engine m_engine;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace app
|
} // namespace app
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# ASEPRITE
|
# Aseprite Scripting Library
|
||||||
# Copyright (C) 2001-2013, 2015 David Capello
|
# Copyright (C) 2015 David Capello
|
||||||
|
|
||||||
|
include_directories(${DUKTAPE_DIR})
|
||||||
|
|
||||||
add_library(scripting-lib
|
add_library(scripting-lib
|
||||||
engine.cpp)
|
engine.cpp)
|
||||||
|
|
||||||
target_link_libraries(scripting-lib duktape)
|
|
||||||
|
20
src/scripting/LICENSE.txt
Normal file
20
src/scripting/LICENSE.txt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
Copyright (c) 2015 David Capello
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
4
src/scripting/README.md
Normal file
4
src/scripting/README.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# Aseprite Scripting Library
|
||||||
|
*Copyright (C) 2015 David Capello*
|
||||||
|
|
||||||
|
> Distributed under [MIT license](LICENSE.txt)
|
@ -1,9 +1,8 @@
|
|||||||
// Aseprite
|
// Aseprite Scripting Library
|
||||||
// Copyright (C) 2001-2015 David Capello
|
// Copyright (c) 2015 David Capello
|
||||||
//
|
//
|
||||||
// This program is free software; you can redistribute it and/or modify
|
// This file is released under the terms of the MIT license.
|
||||||
// it under the terms of the GNU General Public License version 2 as
|
// Read LICENSE.txt for more information.
|
||||||
// published by the Free Software Foundation.
|
|
||||||
|
|
||||||
#include "scripting/engine.h"
|
#include "scripting/engine.h"
|
||||||
|
|
||||||
@ -11,17 +10,14 @@
|
|||||||
|
|
||||||
namespace scripting {
|
namespace scripting {
|
||||||
|
|
||||||
Engine::Engine() : m_impl(new EngineImpl) {
|
Engine::Engine(EngineDelegate* delegate)
|
||||||
|
: m_impl(new EngineImpl(delegate)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() {
|
Engine::~Engine() {
|
||||||
delete m_impl;
|
delete m_impl;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::supportEval() const {
|
|
||||||
return m_impl->supportEval();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::eval(const std::string& script) {
|
void Engine::eval(const std::string& script) {
|
||||||
m_impl->eval(script);
|
m_impl->eval(script);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
// Aseprite
|
// Aseprite Scripting Library
|
||||||
// Copyright (C) 2001-2015 David Capello
|
// Copyright (c) 2015 David Capello
|
||||||
//
|
//
|
||||||
// This program is free software; you can redistribute it and/or modify
|
// This file is released under the terms of the MIT license.
|
||||||
// it under the terms of the GNU General Public License version 2 as
|
// Read LICENSE.txt for more information.
|
||||||
// published by the Free Software Foundation.
|
|
||||||
|
|
||||||
#ifndef SCRIPTING_ENGINE_H_INCLUDED
|
#ifndef SCRIPTING_ENGINE_H_INCLUDED
|
||||||
#define SCRIPTING_ENGINE_H_INCLUDED
|
#define SCRIPTING_ENGINE_H_INCLUDED
|
||||||
@ -12,13 +11,13 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace scripting {
|
namespace scripting {
|
||||||
|
class EngineDelegate;
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
public:
|
public:
|
||||||
Engine();
|
Engine(EngineDelegate* delegate);
|
||||||
~Engine();
|
~Engine();
|
||||||
|
|
||||||
bool supportEval() const;
|
|
||||||
void eval(const std::string& script);
|
void eval(const std::string& script);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
21
src/scripting/engine_delegate.h
Normal file
21
src/scripting/engine_delegate.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// Aseprite Scripting Library
|
||||||
|
// Copyright (c) 2015 David Capello
|
||||||
|
//
|
||||||
|
// This file is released under the terms of the MIT license.
|
||||||
|
// Read LICENSE.txt for more information.
|
||||||
|
|
||||||
|
#ifndef SCRIPTING_ENGINE_DELEGATE_H_INCLUDED
|
||||||
|
#define SCRIPTING_ENGINE_DELEGATE_H_INCLUDED
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace scripting {
|
||||||
|
|
||||||
|
class EngineDelegate {
|
||||||
|
public:
|
||||||
|
virtual ~EngineDelegate() { }
|
||||||
|
virtual void onConsolePrint(const char* text) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,29 +1,62 @@
|
|||||||
// Aseprite
|
// Aseprite Scripting Library
|
||||||
// Copyright (C) 2001-2015 David Capello
|
// Copyright (c) 2015 David Capello
|
||||||
//
|
//
|
||||||
// This program is free software; you can redistribute it and/or modify
|
// This file is released under the terms of the MIT license.
|
||||||
// it under the terms of the GNU General Public License version 2 as
|
// Read LICENSE.txt for more information.
|
||||||
// published by the Free Software Foundation.
|
|
||||||
|
|
||||||
#include "scripting/engine.h"
|
#include "scripting/engine.h"
|
||||||
|
|
||||||
|
#include "base/convert_to.h"
|
||||||
|
#include "base/exception.h"
|
||||||
|
#include "base/memory.h"
|
||||||
|
#include "scripting/engine_delegate.h"
|
||||||
|
|
||||||
|
#define DUK_OPT_PANIC_HANDLER scripting_engine_panic_handler
|
||||||
#include <duktape.h>
|
#include <duktape.h>
|
||||||
|
|
||||||
class scripting::Engine::EngineImpl
|
class ScriptingEngineException : public base::Exception {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
EngineImpl() { }
|
ScriptingEngineException(duk_errcode_t code, const char* msg)
|
||||||
|
: Exception(std::string(msg) + " (code " + base::convert_to<std::string>(code) + ")") {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
bool supportEval() const {
|
static void scripting_engine_panic_handler(duk_errcode_t code, const char* msg)
|
||||||
return true;
|
{
|
||||||
|
throw ScriptingEngineException(code, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <duktape.c>
|
||||||
|
}
|
||||||
|
|
||||||
|
class scripting::Engine::EngineImpl {
|
||||||
|
private:
|
||||||
|
|
||||||
|
public:
|
||||||
|
EngineImpl(EngineDelegate* delegate)
|
||||||
|
: m_delegate(delegate) {
|
||||||
|
m_ctx = duk_create_heap_default();
|
||||||
|
}
|
||||||
|
|
||||||
|
~EngineImpl() {
|
||||||
|
duk_destroy_heap(m_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void eval(const std::string& scriptString) {
|
void eval(const std::string& scriptString) {
|
||||||
duk_context *ctx = duk_create_heap_default();
|
try {
|
||||||
duk_eval_string(ctx, scriptString.c_str());
|
duk_eval_string(m_ctx, scriptString.c_str());
|
||||||
duk_pop(ctx);
|
m_delegate->onConsolePrint(duk_safe_to_string(m_ctx, -1));
|
||||||
printf("%s\n", duk_get_string(ctx, -1));
|
duk_pop(m_ctx);
|
||||||
duk_destroy_heap(ctx);
|
}
|
||||||
|
catch (const std::exception& ex) {
|
||||||
|
std::string err = "Error: ";
|
||||||
|
err += ex.what();
|
||||||
|
m_delegate->onConsolePrint(err.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
duk_context* m_ctx;
|
||||||
|
EngineDelegate* m_delegate;
|
||||||
};
|
};
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
// Aseprite
|
|
||||||
// Copyright (C) 2001-2015 David Capello
|
|
||||||
//
|
|
||||||
// This program is free software; you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License version 2 as
|
|
||||||
// published by the Free Software Foundation.
|
|
||||||
|
|
||||||
#include "scripting/engine.h"
|
|
||||||
|
|
||||||
class scripting::Engine::EngineImpl
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
EngineImpl() { }
|
|
||||||
|
|
||||||
bool supportEval() const {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void eval(const std::string& scriptString) {
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
3
third_party/CMakeLists.txt
vendored
3
third_party/CMakeLists.txt
vendored
@ -55,6 +55,3 @@ if(NOT USE_SHARED_PIXMAN)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_subdirectory(simpleini)
|
add_subdirectory(simpleini)
|
||||||
|
|
||||||
# Duktape
|
|
||||||
add_library(duktape duktape/duktape.c)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user