Here's some code on how to make hovering working.
It's very basic, but you should get the idea...
Code:
-- Mouse hovering demo for gfx-windows in Reaper
--
-- Meo Mespotine, 9.9.2019
--
-- this draws some simplified MIDI-keyboard-keys into a window and shows additional graphics/images, if the mouse is
-- hovering above a key
-- prepare some variables
xoffs_keys=10 -- x-offset for the MIDI-keys
yoffs_keys=10 -- y-offset for the MIDI-keys
width_of_keys=100 -- width of the MIDI-keys
height_of_keys=30 -- height of the MIDI-keys; minimum is 4 in this demo
keys_image_buffer=1 -- the image-buffer for the MIDI-keys
add_image_buffer=2 -- the first image-buffer for the additional hover images(called add-image in this script)
-- must be bigger than keys_image_buffer in this demo!
add_image_x=200 -- the x-position of the add-image
add_image_y=500 -- the y-position of the add-image
add_loaded_images={}-- the image-indices of the loaded add-images; will be set by function LoadImages
function LoadImages()
-- this loads up to 100 images we will display, when the mouse is hovering above a MIDI-key
-- this loads them from the resource-path/Data/toolbar_icons, but you can choose anything else as well
-- the table add_loaded_images holds the imagebuffer-indexes of all loaded files for your own reference
imagebuffer=add_image_buffer
for i=1, 100 do
-- load only png-files
ImageFilename=reaper.EnumerateFiles(reaper.GetResourcePath().."/Data/toolbar_icons", i)
if ImageFilename==nil then break end
if ImageFilename:match("%.png")~=nil then
add_loaded_images[imagebuffer]=gfx.loadimg(imagebuffer, reaper.GetResourcePath().."/Data/toolbar_icons/"..ImageFilename)
imagebuffer=imagebuffer+1
end
end
end
function drawkeys()
-- this draws some simple MIDI-keys
-- not pretty, but enough for this demo
local buf=gfx.dest
gfx.dest=keys_image_buffer
for i=yoffs_keys, 700, height_of_keys do
gfx.rect(xoffs_keys, i, width_of_keys, height_of_keys-3)
end
gfx.dest=buf
end
function check_for_hover()
-- let's check, whether the mouse is above any MIDI-key and return its index
-- if not hovering above a key, return -1
local X,Y=gfx.mouse_x, gfx.mouse_y
local key=0
for i=yoffs_keys, 700, height_of_keys do
-- this iterates through all existing keys and checks, whether a key is currently hovered over or not
key=key+1
if Y>=i and Y<=i+(height_of_keys-3) -- check if mouse if hovering over a key -> Y-position
and X>=xoffs_keys and X<=xoffs_keys+width_of_keys -- check if mouse if hovering over a key -> X-position
then
-- draw a rectangle above the found key
gfx.set(1,0,0,1)
gfx.rect(xoffs_keys+7, i+6, width_of_keys-14, height_of_keys-14, 1)
-- return the MIDI-keyindex
return key
end
end
return -1
end
function add_blit_image(imagefileindex)
-- if the imagefileindex is greater/equal 1, this blits the accompanying image
-- if not, it displays it not at all
if imagefileindex<1 then return end
-- get current x and y-coordinates
local tempx,tempy=gfx.x, gfx.y
-- set drawing-coordinates for add-image
gfx.x=add_image_x
gfx.y=add_image_y
-- blit the add-image into framebuffer
gfx.blit(add_image_buffer+imagefileindex, 1, 0)
-- restore old drawing-coordinates
gfx.x=tempx
gfx.y=tempy
end
function main()
gfx.blit(keys_image_buffer,1,0) -- put the MIDI-keys into the framebuffer
keyindex=check_for_hover() -- get the currently hovered key
add_blit_image(keyindex) -- put the add-image associated with a specific key into the framebuffer
reaper.defer(main) -- repeat it all over again
end
-- now let's begin the show
gfx.init("MIDI", 500,800,0,1,1) -- open the window
gfx.setimgdim(keys_image_buffer, 2048, 2048) -- set dimensions for framebuffer for the MIDI-keys
LoadImages() -- load some default-images from Reaper we display, if the mouse is hovering above a MIDI-key
drawkeys() -- let's draw the MIDI-keys
main() -- start the program