View Single Post
Old 05-09-2016, 05:16 PM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,030
Default

1. Creating and maintaining a window

gfx.init("name"[,width,height,dockstate, x, y])
The parameters should be pretty obvious - "name" is what will appear in the window's title bar, then your dimensions, docking state, and the coordinates of the top-left corner. Note that this convention applies to almost all of ReaScript's graphics functions: X and Y are always the top-left corner of whatever you're drawing, unless you're told otherwise.

- A docking state of 0 will give you a window. Other values are related to which docker you want the window in, but I don't have a clue how that stuff works. Someone cooler than me can answer that question.

- The [square brackets] let us know that those parameters are all optional. You can provide just the name, or the name and width, or all of them, etc.

Theoretically, this is a perfectly acceptable script:
Code:
local function Main()
	gfx.init("My Window", 640, 480, 0, 200, 200)
end

Main()
However, if you run it, you'll immediately see a new problem. The window is created with the name, size, and position we specified, but then it immediately disappears. Why? Because we didn't give it anything to do, so it showed up for the party and went "dude, there isn't even any punch" before turning around and going home.

In ReaScript, getting our window to stick around requires three things:

1. A way to keep the script running.
reaper.defer(function name or anonymous function definition)
This is Reaper's equivalent of "hey, remind me to get groceries later" - the next time Reaper is processing scripts, defer() tells it to run your function again. If a function uses defer() to call itself again then, obviously, it will just keep running endlessly.

Code:
reaper.defer(Main)
2. A way to keep the window open.
gfx.update()
Tells the window we opened to quit falling asleep, making sure it stays visible as long as our defer() loop is running. If you simply made the above function into a defer() loop, it would keep opening new windows rather than maintaining one.

Code:
gfx.update()
3. A way to determine when the script should close.
gfx.getchar([char])
This function simply gets the current keyboard state, which doesn't help us. However, the Reaper devs were nice enough to include a small treat for us - if the window has been closed, i.e. by clicking the big [X] button in Windows, gfx.getchar() will return -1. This gives us an easy way to break the defer() loop we started.

(Just for fun, this example will also let you close the window by pressing Escape.)

Code:
 local char = gfx.getchar()

--	(Escape key)	(Window closed)
if char ~= 27 and char ~= -1 then
	reaper.defer(Main)
end
So, with a bit of rearranging, we can now open a window and keep it there long enough to do something useful. Note that the gfx.init() call has to be outside of whatever function our defer() loop is running; we want to initialize the window once, and then include gfx.update() in our loop to keep it open.

Code:
local function Main()

	local char = gfx.getchar()
	if char ~= 27 and char ~= -1 then
		reaper.defer(Main)
	end
	
	gfx.update()

end

gfx.init("My Window", 640, 480, 0, 200, 200)
Main()

Last edited by Lokasenna; 05-10-2016 at 08:31 PM.
Lokasenna is online now   Reply With Quote