Old 09-01-2019, 08:45 AM   #1
Anton9
Human being with feelings
 
Anton9's Avatar
 
Join Date: Jun 2009
Location: Earth
Posts: 1,340
Default lua gfx help

Hey Guys,

I have the following code which opens a graphic of a vertical MIDI keyboard.
What I'd like to do now is when the mouse is over specific keys other graphics should open/close automatically.

Each key is aprox. 30 x 120, black keys are a little shorter. Detection zone does'nt have to be exact, just a few pix in each direction should work.

So when mouse is over key 1, "mygraphic001.png" should auto-open aprox. 100 pix to the right of the cursor, when mouse is over key 2, "mygraphic001.png" should close and "mygraphic002.png" should open.., etc.

Any help whould be greatly appreciated.

Code:
function Init()
  --- Init window ---
  gfx.clear = 0xF0F0F0
  gui = {w = 364, h = 975 , dock = 0, x = 1920, y = 3}
  gfx.loadimg(0,"C:\\Pictures\\kybd.png")         
  gfx.init("Keyboard", gui.w, gui.h, gui.dock, gui.x, gui.y)
  gfx.blit(0,1.0,0.0)
end

function  loop()
  char = gfx.getchar()
  if char~=-1 then reaper.defer(loop) end 
  gfx.update()
end

Init()
loop()
Anton9 is offline   Reply With Quote
Old 09-01-2019, 01:20 PM   #2
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

What is the current behavior and what did you try already?
And do you mean hovering or clicking a midi-note?
Where shall the other graphics appear?
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 09-01-2019, 11:48 PM   #3
Anton9
Human being with feelings
 
Anton9's Avatar
 
Join Date: Jun 2009
Location: Earth
Posts: 1,340
Default

So basically what I'm using this for is a reference aid that shows me what scripts/actions I have mapped to a MIDI controller. When the script is executed it displays a MIDI kybd and on each key is written what is assigned. For example I have one that shows various playrates that I have mapped to MIDI notes, another shows grid divisions that are mapped.
This is meant to be just a visual aid, so I don't have to remember hundreds of assignments.

This one I would like to pop open various graphics when each key is hovered over. The graphics should open aprox. 100 pixels to the right of the kybd graphic. As I hover over each key a different graphic should pop open and the previous one should close.

I just don't know how to program the mouse hover and pop open/close code.

For anyone trying the code I posted.., two things to take note of;
1) Change this line, gfx.loadimg(0,"C:\\Pictures\\kybd.png") to point to whatever graphic you would like displayed.
2) This is being used on a dual monitor system so you may also need to change "x = 1920" to whatever is suitable for your setup.

Cheers

Last edited by Anton9; 09-01-2019 at 11:54 PM.
Anton9 is offline   Reply With Quote
Old 09-02-2019, 04:40 AM   #4
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Ok. I could attempt an example which would show you how to do hovering and displaying but could write that only at the end of the week.
Unless someone else can do it faster
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 09-02-2019, 06:44 AM   #5
Anton9
Human being with feelings
 
Anton9's Avatar
 
Join Date: Jun 2009
Location: Earth
Posts: 1,340
Default

That would be awesome.., thanks mespotine.
Anton9 is offline   Reply With Quote
Old 09-02-2019, 07:04 AM   #6
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

I think the quickest and least headache-inducing solution would be to use my GUI library. You can still draw the keyboard image yourself with the code you've got above, although you'd have to redraw it on each graphics update.

PM me if you're interested and I can help you get started.
__________________
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
Old 09-12-2019, 09:55 AM   #7
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

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
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 09-18-2019, 05:45 AM   #8
Anton9
Human being with feelings
 
Anton9's Avatar
 
Join Date: Jun 2009
Location: Earth
Posts: 1,340
Default

@mespotine,

Thank you very much, this should help me out quite a bit.

@Lokasenna,

Thank you as well, for the offer.
Anton9 is offline   Reply With Quote
Old 09-19-2019, 07:09 AM   #9
lb0
Human being with feelings
 
Join Date: Apr 2014
Posts: 4,171
Default

Probably complete overkill - but you could draw an additional graphic (using the same code) for the keyboard but colour the keys - 0-127 - as shades of red from 0 - 127.

Then when you hover over the drawn keyboard - simply use gfx.getpixel and give the coordinates of the mouse over the coloured keyboard image to detect exactly which key the mouse is over.

Obviously the image you display is the original keyboard image - just hold the key detection bitmap in a buffer.

Benefits of this method - very fast detection when hovering over a key (no need to use a loop or maths) - just detect what shade of red is under the mouse location (in the detection image buffer). Less CPU overhead if constantly monitoring whether mouse is hovering over it and Pixel perfect detection. Actually less code to write overall too!

Anyway - like I said - it's really probably not necessary from a performance point of view for your purpose - but a suggestion none-the-less.
__________________
Projects - Reascripts - Lua:
Smart Knobs 2 | LBX Stripper | LBX Floating FX Positioner
Donate via Paypal | LBX Tools Website
lb0 is offline   Reply With Quote
Old 09-20-2019, 09:17 PM   #10
Anton9
Human being with feelings
 
Anton9's Avatar
 
Join Date: Jun 2009
Location: Earth
Posts: 1,340
Default

@lb0,

This actually sounds like a great idea.
Any chance I could bother you to post some code?

It's a mini-keyboard so only 25 keys.
Also it would be nice to have the images that open upon the detection actually open in a separate window apart from the keyboard image window.
In mespotine's example they open in the same window.

Thank you
Anton9 is offline   Reply With Quote
Old 09-23-2019, 07:03 AM   #11
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Opening in up in a different window is not possible, or at least not that easy, as every script can open only one gfx-window.

You can only code a second script, which opens that second window and you run that script using reaper.Main_OnCommand

Depending on what exactly you want to achieve, this could become messy quite quickly.

I'm working on a concept for quite some time for opening multiple gfx-windows for my Ultraschall-API, which solves all this background-script-issue, but it's difficult to make it right.

If you only want to have the same window opened for a specific key, this is easy to do.
If you want to change the content of the window, things may become difficult.


Edit: FWIW, maybe I can provide a function in the next Ultraschall-API, which could help you with that...stay tuned for that(need a few more weeks for the next update).
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:05 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.