Template Code

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
dkwroot
Posts: 680
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Template Code

Post by dkwroot »

==============================================
INTRODUCTION AND UPDATES
==============================================
I created this thread to showcase template scripts to help people new to the Anime Studio API. Before you go dive into the templates, I'd suggest using these websites to help ease your transition into Anime Studio and LUA.

ANIME STUDIO API: http://www.animestudioscripting.com/

ONLINE LUA IDE(use this to practice LUA): http://repl.it/languages

A LUA TUTORIAL SERIES: https://www.youtube.com/playlist?list=P ... ctmMOJ70t_

If anyone wants to donate template code, send it to me and I'll toss it up and credit you. :D

==============================================
TEMPLATE ONE:
TYPE: TOOL
AS VERSION: 10
DESC: BASIC BUTTON MENU WITH NESTED BOX BUTTON
==============================================

Code: Select all

-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "dr_testtool"

-- **************************************************
-- General information about this script
-- **************************************************

dr_testtool = {} --Make certain that this table name is the SAME as the ScriptName Above!!!

dr_testtool.BASE_STR = 5285 --This value is used for language localization. If that means nothing to you, then ignore it.

function dr_testtool:Name() --This will display the name of your script
	return "testval"
end

function dr_testtool:Version()
	return "0.1"
end

function dr_testtool:Description() -- This sets the description that shows up below the header bar
	return MOHO.Localize("/Scripts/Tool/testval/Description= REPLACE THIS SENTENCE WITH WHAT YOU WANT TO SAY!")
end

function dr_testtool:Creator()
	return "YOUR NAME GOES HERE"
end

function dr_testtool:UILabel() -- This information is displayed when the user hovers over the tool
	return(MOHO.Localize("/Scripts/Tool/testval/testval=Label for Test"))
end

-- **************************************************
-- BODY OF THE SCRIPT STARTS HERE
-- **************************************************

--	**************************************************
--	PLACE TABLE TEMPLATES ('CLASS TEMPLATES' for people from other languages) HERE.
--	THESE TABLES ARE BASICALLY JUST CLASSES. THEY WILL GET ASSIGNED TO AN OBJECT IN
--	THE ACTIVE TABLE SIMILAR TO A CLASS.
--	**************************************************

local MenuTemplate = {} --CREATE A LOCAL TABLE AS A TEMPLATE FOR THE MENU BUTTON
function MenuTemplate:new() --IN THIS TABLE, WE DESIGN THE MENU.
	local menu = LM.GUI.SimpleDialog(MOHO.Localize("/Scripts/Tool/testval/testval=Test Val"), MenuTemplate)
	local lay = menu:GetLayout()
	
	lay:PushV(LM.GUI.ALIGN_LEFT, 0) --# This gets popped at the end of the function
	
	menu.testbox = LM.GUI.CheckBox(MOHO.Localize("/Scripts/Tool/testval/testbox=TEST BOX:"), dr_testtool.MenuObject_1_CHANGE) -- This buttons talks on channel "MenuObject_1_CHANGE"
	lay:AddChild(menu.testbox, LM.GUI.ALIGN_LEFT)
	
	lay:PushH(LM.GUI.ALIGN_RIGHT, 0)
		lay:AddChild(LM.GUI.Button(MOHO.Localize("/Scripts/Tool/testval/Close=Close"), LM.GUI.MSG_CANCEL))
	lay:Pop()
	
	lay:Pop() --# This Pops the first PushV
	
	return menu
end

function MenuTemplate:UpdateWidgets() --THIS IS JUST AN UPDATE TEMPLATE, IT MUST BE UPDATED BY THE 'REAL' UPDATER -> dr_testtool:UpdateWidgets()
	print("UpdateWidget FUNCTION ACTIVATED")
end

function MenuTemplate:OnOK() --THIS BUTTON WILL EXECUTE EVERYTIME YOU CLOSE THE BUTTON
	print("OnOK FUNCTION ACTIVATED")
end

function MenuTemplate:HandleMessage(msg) --THE HANDLEMESSAGE FUNCTION WATCHES FOR MESSAGES SENT BY BUTTONS. IF A BUTTON MATCHES ONE OF ITS CRITERIA, IT DOES STUFF.
	print("HANDLEMESSAGE FUNCTION ACTIVATED.")
	if (msg == dr_testtool.MenuObject_1_CHANGE) then -- MAKE SURE TO USE THE BASE TABLE WHEN CHECKING THE MESSAGE VALUE!!!!!
		if (self.testbox:Value() == true) then --THIS CHECKS THE TRUE/FALSE VALUE OF THE CHECK BOX
			print("BOXCHECK = " .. tostring(self.testbox:Value() ))
		else
			print("BOXCHECK = " .. tostring(self.testbox:Value() ))
		end
	end
end

-- **************************************************
--	THIS IS WHERE THE ACTIVE "MAIN" CODE EXISTS. THIS IS THE CODE THAT IS UPDATED AND DOES WORK.
-- **************************************************

function dr_testtool:DoLayout(moho, layout) --IN THIS TABLE, WE DRAW THE MENU TO THE SCREEN

	--Here we create an object of the button template we defined above.
	self.MenuObject_1 = MenuTemplate:new() 
	self.popup = LM.GUI.PopupDialog(MOHO.Localize("/Scripts/Tool/testval/testboxx=TEST BOX"), true, self.MenuObject_1_BEGIN) --Notice this buttons talks on channel "MenuObject_1_BEGIN"
	self.popup:SetDialog(self.MenuObject_1)
	layout:AddChild(self.popup) --This draws the button to the screen.

end

-- HERE WE DESIGNATE CHANNELS THAT THE BUTTONS WILL TALK ON. WE DON'T WANT BUTTONS TALKING ON ONE CHANNEL AT THE SAME TIME,
-- SO WE MAKE PLENTY OF CHANNELS AND WE MAKE THEM UNIQUE.
dr_testtool.CHANGE = MOHO.MSG_BASE --MOHO.MSG_BASE is a base channel number. The others will use this as a start and just offset by a number.
dr_testtool.MenuObject_1_BEGIN = MOHO.MSG_BASE + 1
dr_testtool.MenuObject_1_CHANGE = MOHO.MSG_BASE + 2

function dr_testtool:UpdateWidgets(moho) --THIS IS THE "MAIN" updatewidget. Anime Studio will update THIS widget.
	self.popup:Enable(true) -- THIS ENABLES OUR MENU BUTTON
	self.MenuObject_1:UpdateWidgets() -- Notice that the real widget updates the widget in MenuObject
end

function dr_testtool:HandleMessage(moho, view, msg) --THIS IS THE HANDLEMESSAGE FUNCTION FOR NON-NESTED BUTTONS.
end
Last edited by dkwroot on Thu Jul 03, 2014 2:57 pm, edited 4 times in total.
User avatar
hayasidist
Posts: 3849
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Template Code

Post by hayasidist »

Hi. Sorry that I don't have time to give you more help right now,

a couple of answers:

"dr_tool.BASE_STR = 5285 -- WHAT DOES THIS DO??????" -- BASE_STR is for language localisation. For custom scripts there's really no tidy way to add localisation; so this plus all calls to MOHO.Localize are not very useful.


your function "update Widgets" seems to say "If X then enable box1 else enable box1" (i.e. just enable box1)
and all the self.xxxx are all undefined?

I have a test harness that I use and this goes some way to providing the "skeleton" that you want, but you'll need to do some heavy meat-chopping-off if you really only want the bare bones. I had uploaded it to a file share site but I can't find it right now. I'll upload a new version -- but that will be in a week or two.

In the meantime take a look at http://gcharb2d.blogspot.co.uk/2011/09/ ... cript.html

Hope that helps a bit ..
dkwroot
Posts: 680
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Template Code

Post by dkwroot »

EDITED
Last edited by dkwroot on Thu Jul 03, 2014 2:41 pm, edited 1 time in total.
dkwroot
Posts: 680
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Template Code

Post by dkwroot »

EDITED
Last edited by dkwroot on Thu Jul 03, 2014 2:41 pm, edited 1 time in total.
dkwroot
Posts: 680
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Template Code

Post by dkwroot »

EDITED
Last edited by dkwroot on Thu Jul 03, 2014 2:41 pm, edited 1 time in total.
User avatar
hayasidist
Posts: 3849
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Template Code

Post by hayasidist »

dkwroot wrote: -- The BASE_STR sets the base number that buttons will use as a message channel. No two tools should have the same BASE_STR value so try to stagger them by at least 50.
dr_tool.BASE_STR = 5285

-- This is where the BASE_STR is important. Each of the values below has its own 'channel' that it talks on. You don't want them to talk on the same channel as another
-- button in this tool or ANY other tools. So, we use MOHO.MSG_BASE which is the value that we established when we set BASE_STR at the start of the script.
dr_tool.CHANGE = MOHO.MSG_BASE
dr_tool.DLOG_BEGIN = MOHO.MSG_BASE + 1
dr_tool.DLOG_CHANGE = MOHO.MSG_BASE + 2
Nope. that's not right.

BASE_STR is ONLY for localisation.
MOHO.MSG_BASE - The starting message code for use by Moho scripts.


If you haven't alreday done this, it would be worth a look at the scripting documentation. It is out-of-date (by which I mean there are new features that are not documented yet) but it will answer a lot of the things you're trying to do.

path looks something like: C:\Program Files\Smith Micro\Anime Studio Pro 10\Resources\Support\Extra Files\Lua Interfaces.zip
Post Reply