WindowAddHotspot()

Posted by Baatti on Fri 20 Nov 2009 02:16 PM — 2 posts, 10,950 views.

USA #0
Hey Nick and everyone.

I would like to STRONGLY request that WindowAddHotspot() have a make-over. The parameters regarding Mouse actions can only be strings representing function names and can not take parameters. Although there are ways to work around that, it is a serious draw back.

Thank you for taking the time to read this and I hope that this issue will be fixed.


[edit]: to be more clear, I wish that there was a way to pass FUNCTIONS instead of STRINGS, I understand that passing parameters is not necessary, but to have the ability to pass a FUNCTION instead of a STRING representing a function would be nice. Although Twisol explained to me why this isn't likely to be possible as there are multiple scripting languages at question and they all handle things differently. Still, if its do-able, that'd be nice
Amended on Fri 20 Nov 2009 05:40 PM by Baatti
Australia Forum Administrator #1
There would be no language-neutral way of passing functions as a call to other functions.

However it can be achieved to pass other arguments to hotspot functions, particularly if you are using Lua, as recommended.

In the file movewindow.lua which ships with MUSHclient there are examples of this. Here is one:


-- make a mouse-down handler with the movement information as an upvalue

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)
    
  end -- mousedown

end -- make_mousedown_handler


What this does is make a hotspot handler (which normally only takes flags and hotspot ID), and adds another parameter, the mwi field (which is actually a table of other parameters such as where the window is).

By using a function-making function this extra parameter is stored as an upvalue to the function, making it available when the function is called, as you can see above.

Now all you have to do is give the returned function (the one with the upvalue) a name which doesn't clash with other functions, and each miniwindow can have its own hotspot handler with extra arguments. eg


   -- mouse handlers
    movewindow_info.mousedown   = make_mousedown_handler   (movewindow_info)  
    movewindow_info.dragmove    = make_dragmove_handler    (movewindow_info)
    movewindow_info.dragrelease = make_dragrelease_handler (movewindow_info)
 
  -- save table in global namespace
  _G ["mw_" .. win .. "_movewindow_info"] = movewindow_info
    

-- and later ...

 -- 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


You will note that function names can have dots in them, so the function you pass can actually be an element in a table.