View Single Post
Old 05-13-2016, 03:01 PM   #31
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

I often don't have a complete example to post, since I'm just throwing the lesson code into an existing script. Don't worry though - the next one will have a HUGE pile of code you can look at. In the meantime:

- Are you getting any error messages?

- Is your state variable declared in the right place? If you put "local state" inside the function you're deferring, it will reset on every loop. As I mentioned above it has to be either global or declared outside the deferred function.

- Are the mouse position and button state being detected correctly?

Here's a function from X-Raym's tutorials that I've found really, really handy; it's seriously the first thing I put in every script.

Code:
-- Print stuff to the Reaper console, for debugging purposes
function Msg(message)
	
	reaper.ShowConsoleMsg(tostring(message).."\n")
	
end
If your code isn't working and you can't figure out where the problem is, you can throw in a couple of calls to Msg() to tell the value of a variable, or simply let you know if a certain if/then/else structure is working correctly.

In your case, you could try putting it in your IsInside function to see if it's giving the right answer:
Code:
-- Are these coordinates inside the given area?
local function IsInside(x, y, w, h)
	
	local mouse_x, mouse_y = gfx.mouse_x, gfx.mouse_y
	
	local inside = 
		mouse_x >= x and mouse_x < (x + w) and 
		mouse_y >= y and mouse_y < (y + h)

        Msg(inside)
		
	return inside
	
end
or put a couple of calls inside the "if the mouse button is down" structure to tell you which branch is being run:

Code:
	-- If the left button is down
	if gfx.mouse_cap & 1 == 1 then

                Msg("Mouse down")
	
		-- If the cursor is inside the rectangle AND the button wasn't down before
		if IsInside(x, y, w, h) and not mouse_btn_down then
			
                        Msg("Clicked")

			mouse_btn_down = true
			state = not state
			
		end

	-- If the left button is up
	else

                Msg("Mouse up")

		mouse_btn_down = false
	
	end
Now, obviously these are going to be running every time the script loops, so the Reaper console will be a constant stream of messages, but if you're clicking away and you never see "Clicked" coming up then obviously the script isn't getting to that section.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote