Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/openHarmonyExample.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// add this line at the top of your scripts to load the library before the execution.
include("openHarmony.js")
const $ = require("openHarmony.js");


/**
Expand Down
64 changes: 45 additions & 19 deletions openHarmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
* @example
* // To access the functions, first call the $ object. It is made available after loading openHarmony like so:
*
* include ("openHarmony.js");
* const $ = require ("openHarmony.js");
*
* var doc = $.scn; // grabbing the scene document
* $.log("hello"); // prints out a message to the MessageLog.
Expand All @@ -86,18 +86,19 @@
*
*/
$ = {
debug_level : 0,

/**
* Enum to set the debug level of debug statements.
* @name $#DEBUG_LEVEL
* @enum
*/
DEBUG_LEVEL : {
'ERROR' : 0,
'WARNING' : 1,
'LOG' : 2
},
debug_level: 0,
/**
* Enum to set the debug level of debug statements.
* @name $#DEBUG_LEVEL
* @enum
*/
DEBUG_LEVEL: {
"ERROR": 0,
"WARNING": 1,
"LOG": 2,
"INFO": 2,
"DEBUG": 3
},
file : __file__,
directory : false,
pi : 3.14159265359
Expand All @@ -111,8 +112,7 @@ $ = {
*/
Object.defineProperty( $, "directory", {
get : function(){
var currentFile = __file__
return currentFile.split("\\").join("/").split( "/" ).slice(0, -1).join('/');
return $.file.split("\\").join("/").split( "/" ).slice(0, -1).join('/');
}
});

Expand Down Expand Up @@ -148,10 +148,28 @@ _dir.setFilter( QDir.Files);
var _files = _dir.entryList();

for (var i in _files){
include( _ohDirectory + "/" + _files[i]);
}

const _path = "openHarmony/" + _files[i];
try {
const _exported = require(_path);
} catch (e) {
MessageLog.trace("Error requiring " + _path + ": " + e);
}

for (var _key in _exported) {
if (_exported.hasOwnProperty(_key)) {
// MessageLog.trace("$." + _key + " = " + _files[i] + ":" + _key);

$[_key] = _exported[_key];

/* Tried this instead of $[_key] = _exported[_key] to transfer properties
with setters and getters defined on a script's exports object, but they
are skipped? It's ok because oH doesn't use anything like this
Object.defineProperty(
$, _key, Object.getOwnPropertyDescriptor(_exported, _key));
*/
}
}
}


/**
Expand Down Expand Up @@ -525,9 +543,17 @@ for( var classItem in $ ){

//Also extend it to the global object.
this[classItem] = $[classItem];

/*
This didn't do what I expected, but after require()ing oH in, all the functions
are properties of the top-level this.
*/
// this.__proto__[classItem] = $[classItem];
}
}


// Add global access to $ object
this.__proto__.$ = $
this.__proto__.$ = $

exports = $
54 changes: 26 additions & 28 deletions openHarmony/openHarmony_application.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////
//////////////////////////////////////
// //
Expand All @@ -55,7 +54,7 @@
* The $.oApp class provides access to the Harmony application and its widgets.
* @constructor
*/
$.oApp = function(){
exports.oApp = function(){
}


Expand All @@ -65,7 +64,7 @@ $.oApp = function(){
* @type {string}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'versionString', {
Object.defineProperty(exports.oApp.prototype, 'versionString', {
get : function(){
return about.getVersionInfoStr().split("version").pop().split("build")[0].replace(/\s/g, "");
}
Expand All @@ -78,7 +77,7 @@ $.oApp = function(){
* @type {int}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'version', {
Object.defineProperty(exports.oApp.prototype, 'version', {
get : function(){
return parseInt(this.versionString.split(".")[0], 10);
}
Expand All @@ -91,7 +90,7 @@ Object.defineProperty($.oApp.prototype, 'version', {
* @type {int}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'minorVersion', {
Object.defineProperty(exports.oApp.prototype, 'minorVersion', {
get : function(){
return parseInt(this.versionString.split(".")[1], 10);
}
Expand All @@ -103,7 +102,7 @@ Object.defineProperty($.oApp.prototype, 'minorVersion', {
* @type {int}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'patch', {
Object.defineProperty(exports.oApp.prototype, 'patch', {
get : function(){
return parseInt(this.versionString.split(".")[2], 10);
}
Expand All @@ -117,7 +116,7 @@ Object.defineProperty($.oApp.prototype, 'minorVersion', {
* @type {string}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'flavour', {
Object.defineProperty(exports.oApp.prototype, 'flavour', {
get : function(){
return about.getFlavorString();
}
Expand All @@ -130,7 +129,7 @@ Object.defineProperty($.oApp.prototype, 'flavour', {
* @type {QWidget}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'mainWindow', {
Object.defineProperty(exports.oApp.prototype, 'mainWindow', {
get : function(){
var windows = QApplication.topLevelWidgets();
for ( var i in windows) {
Expand All @@ -147,7 +146,7 @@ Object.defineProperty($.oApp.prototype, 'mainWindow', {
* @type {QToolbar}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'toolbars', {
Object.defineProperty(exports.oApp.prototype, 'toolbars', {
get : function(){
var widgets = QApplication.allWidgets();
var _toolbars = widgets.filter(function(x){return x instanceof QToolBar})
Expand All @@ -157,16 +156,15 @@ Object.defineProperty($.oApp.prototype, 'toolbars', {
});



/**
* The Position of the mouse cursor in the toonboom window coordinates.
* @name $.oApp#mousePosition
* @type {$.oPoint}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'mousePosition', {
Object.defineProperty(exports.oApp.prototype, 'mousePosition', {
get : function(){
var _position = this.$.app.mainWindow.mapFromGlobal(QCursor.pos());
var _position = this.mainWindow.mapFromGlobal(QCursor.pos());
return new this.$.oPoint(_position.x(), _position.y(), 0);
}
});
Expand All @@ -178,7 +176,7 @@ Object.defineProperty($.oApp.prototype, 'mousePosition', {
* @type {$.oPoint}
* @readonly
*/
Object.defineProperty($.oApp.prototype, 'globalMousePosition', {
Object.defineProperty(exports.oApp.prototype, 'globalMousePosition', {
get : function(){
var _position = QCursor.pos();
return new this.$.oPoint(_position.x(), _position.y(), 0);
Expand Down Expand Up @@ -211,7 +209,7 @@ Object.defineProperty($.oApp.prototype, 'globalMousePosition', {
*
* brushTool.activate() // by using the activate function of the oTool class
*/
Object.defineProperty($.oApp.prototype, 'tools', {
Object.defineProperty(exports.oApp.prototype, 'tools', {
get: function(){
if (typeof this._toolsObject === 'undefined'){
this._toolsObject = [];
Expand All @@ -236,7 +234,7 @@ Object.defineProperty($.oApp.prototype, 'tools', {
* @name $.oApp#currentTool
* @type {$.oTool}
*/
Object.defineProperty($.oApp.prototype, 'currentTool', {
Object.defineProperty(exports.oApp.prototype, 'currentTool', {
get : function(){
var _tool = Tools.getToolSettings().currentTool.id;
return _tool;
Expand Down Expand Up @@ -268,7 +266,7 @@ Object.defineProperty($.oApp.prototype, 'currentTool', {
* @param {string} [parentName] The name of the parent widget to look into, in case of duplicates.
* @return {QWidget} The widget if found, or null if it doesn't exist.
*/
$.oApp.prototype.getWidgetByName = function(name, parentName){
exports.oApp.prototype.getWidgetByName = function(name, parentName){
var widgets = QApplication.allWidgets();
for( var i in widgets){
if (widgets[i].objectName == name){
Expand Down Expand Up @@ -307,7 +305,7 @@ $.oApp.prototype.getWidgetByName = function(name, parentName){
* // the preference object also holds a categories array with the list of all categories
* log (prefs.categories)
*/
Object.defineProperty($.oApp.prototype, 'preferences', {
Object.defineProperty(exports.oApp.prototype, 'preferences', {
get: function(){
if (typeof this._prefsObject === 'undefined'){
var _prefsObject = {};
Expand All @@ -324,11 +322,11 @@ Object.defineProperty($.oApp.prototype, 'preferences', {
value:_details
})

var prefFile = (new oFile(specialFolders.resource+"/prefs.xml")).parseAsXml().children[0].children;
var prefFile = (new this.$.oFile(specialFolders.resource+"/prefs.xml")).parseAsXml().children[0].children;

var userPrefFile = new oFile(specialFolders.userConfig + "/Harmony Premium-pref.xml")
var userPrefFile = new this.$.oFile(specialFolders.userConfig + "/Harmony Premium-pref.xml")
// Harmony Pref file is called differently on the database userConfig
if (!userPrefFile.exists) userPrefFile = new oFile(specialFolders.userConfig + "/Harmony-pref.xml")
if (!userPrefFile.exists) userPrefFile = new this.$.oFile(specialFolders.userConfig + "/Harmony-pref.xml")

if (userPrefFile.exists){
var userPref = {objectName: "category", id: "user", children:userPrefFile.parseAsXml().children[0].children};
Expand Down Expand Up @@ -394,11 +392,11 @@ Object.defineProperty($.oApp.prototype, 'preferences', {
* }
* }
*/
Object.defineProperty($.oApp.prototype, 'stencils', {
Object.defineProperty(exports.oApp.prototype, 'stencils', {
get: function(){
if (typeof this._stencilsObject === 'undefined'){
// parse stencil xml file penstyles.xml to get stencils info
var stencilsFile = (new oFile(specialFolders.userConfig+"/penstyles.xml")).read();
var stencilsFile = (new this.$.oFile(specialFolders.userConfig+"/penstyles.xml")).read();
var penRegex = /<pen>([\S\s]*?)<\/pen>/igm
var stencils = [];
var stencilXml;
Expand All @@ -419,7 +417,7 @@ Object.defineProperty($.oApp.prototype, 'stencils', {
* @name $.oApp#currentStencil
* @type {$.oStencil}
*/
Object.defineProperty($.oApp.prototype, 'currentStencil', {
Object.defineProperty(exports.oApp.prototype, 'currentStencil', {
get: function(){
return this.stencils[PaletteManager.getCurrentPenstyleIndex()];
},
Expand All @@ -438,7 +436,7 @@ Object.defineProperty($.oApp.prototype, 'currentStencil', {
* get a tool by its name
* @return {$.oTool} a oTool object representing the tool, or null if not found.
*/
$.oApp.prototype.getToolByName = function(toolName){
exports.oApp.prototype.getToolByName = function(toolName){
var _tools = this.tools;
for (var i in _tools){
if (_tools[i].name.toLowerCase() == toolName.toLowerCase()) return _tools[i];
Expand All @@ -452,7 +450,7 @@ $.oApp.prototype.getToolByName = function(toolName){
* @param {$.oTool} tool the tool object we want valid stencils for
* @return {$.oStencil[]} the list of stencils compatible with the specified tool
*/
$.oApp.prototype.getValidStencils = function (tool){
exports.oApp.prototype.getValidStencils = function (tool){
if (typeof tool === 'undefined') var tool = this.currentTool;
return tool.stencils;
}
Expand All @@ -463,7 +461,7 @@ $.oApp.prototype.getValidStencils = function (tool){
* @param {string} menuName The name of the menu containing the action (must be a top level menu such as File, Edit etc)
* @param {string} menuString The menu entry to trigger.
*/
$.oApp.prototype.runMenuCommand = function(menuName, menuString){
exports.oApp.prototype.runMenuCommand = function(menuName, menuString){
var menubar = this.mainWindow.menuBar();
var menus = menubar.children();

Expand Down Expand Up @@ -507,7 +505,7 @@ $.oApp.prototype.runMenuCommand = function(menuName, menuString){
* @param {QWidget} [parent] The parent widget to add the toolbar to.
* @param {bool} [show] Whether to show the toolbar instantly after creation.
*/
$.oToolbar = function( name, widgets, parent, show ){
exports.oToolbar = function( name, widgets, parent, show ){
if (typeof parent === 'undefined') var parent = $.app.mainWindow;
if (typeof widgets === 'undefined') var widgets = [];
if (typeof show === 'undefined') var show = true;
Expand All @@ -524,7 +522,7 @@ $.oToolbar = function( name, widgets, parent, show ){
* Shows the oToolbar.
* @name $.oToolbar#show
*/
$.oToolbar.prototype.show = function(){
exports.oToolbar.prototype.show = function(){
if (this.$.batchMode) {
this.$.debug("$.oToolbar not supported in batch mode", this.$.DEBUG_LEVEL.ERROR)
return;
Expand Down
Loading