WindowAddHotSpot suggestion

Posted by Bast on Thu 15 Jul 2010 03:33 AM — 4 posts, 17,782 views.

#0
Instead of passing function names in WindowAddHotSpot, can we actually pass a true function? The whole keeping up with ids in a table and then having generic mouse functions that check this table seems complicated. Doubly complicated when you start trying to keep up with hotspots from multiple miniwindows in the same plugin.

In lua

function somefunction (flags, hotspotid)
  print("help")
end

-- New way
WindowAddHotspot(winid, winid .. ':' .. 'test',
                left, top, right, bottom,
                nil, -- mouseover
                nil, -- cancelmouseover
                nil, -- mousedown
                nil, -- cancelmousedown
                somefunction, -- mouseup
                hint,
                cursor, 0) 


I don't know how hard this would be, but somewhere MUSHclient has to look up the function from the string anyway, so can we cut out the middle man?


Thanks,
Bast
Australia Forum Administrator #1
It's all very well in Lua, but the miniwindows functions are supposed to be language-neutral. I'm not sure, for example, how I might store a Python function internally in the C++ data structures.

However what you can do is use a function-generating function (with upvalues) to pass a single function and have the upvalue remember who it belongs to.

For example, in movewindow.lua there is a function make_mousedown_handler that makes a function for such a purpose.
Australia Forum Administrator #2
Here's the whole function, you note that once it generates the mouse-down function, that function knows its miniwindow (you could save other stuff as well):


local function make_mousedown_handler (mwi)

  return function (flags, hotspot_id)

    local win = mwi.win
    
    -- find where mouse is so we can adjust window relative to mouse
    mwi.startx = WindowInfo (win, 14)
    mwi.starty = WindowInfo (win, 15)
    
    -- find where window is in case we drag it offscreen
    mwi.origx = WindowInfo (win, 10) 
    mwi.origy = WindowInfo (win, 11)
    
    -- find where the friends are relative to the window
    for i,v in ipairs(mwi.window_friends) do
        if (v ~= nil) then
            mwi.window_friend_deltas[i] = {WindowInfo(v, 10) - mwi.origx, WindowInfo(v, 11) - mwi.origy}
        end -- if
    end -- for
        
  end -- mousedown

end -- make_mousedown_handler


And here is how you add the hotspot:



  -- mouse handlers
  movewindow_info.mousedown   = make_mousedown_handler   (movewindow_info)  

...

  -- make a hotspot
  WindowAddHotspot(win, hotspot_id,  
                   left or 0, top or 0, right or 0, bottom or 0,   -- rectangle
                   "",   -- MouseOver
                   "",   -- CancelMouseOver
                   "mw_" .. win .. "_movewindow_info.mousedown",  -- MouseDown
                   "",   -- CancelMouseDown
                   "",   -- MouseUp
                   "Drag to move window",  -- tooltip text
                   cursor or 1, -- cursor
                   0)  -- flags
                   


Since handler functions can have dots in their names, you can store them all in some intermediate table, rather than polluting global namespace with them.
Amended on Thu 15 Jul 2010 05:26 AM by Nick Gammon
#3
Yeah, I figured it wouldn't be as easy as I wanted it to be and I already do something similar to your example in my miniwindows. I was curious if it was a limitation of the scripting languages.

Thanks again,
Bast