Toggle Switches for Plugins

Posted by LupusFatalis on Wed 10 Jun 2009 08:07 PM — 21 posts, 90,955 views.

#0
I am currently working on a plugin which I will be toggling on and off very frequently. I am wondering whether there is a way to make a toggle style button on the main menu bar to enable/disable this plugin. Its in python, if that matters. Any assistance in this matter would be greatly appreciated.
USA #1
I use EnablePlugin() in an alias when I need to do that.
Australia Forum Administrator #2
Yes I would use that.

http://www.gammon.com.au/scripts/doc.php?function=EnablePlugin

If you really want to click an icon, see:

http://www.gammon.com.au/forum/?id=9280

(You would still need to use EnablePlugin in the script for a button).
#3
ok... here is my button
  -- button 1
 {
  icon = "Compass_32x32.png",
  tooltip = "Underdark Mapper",
  script = function ()   -- script to execute when clicked
				if world.GetPluginInfo("aba6daf3fb30699071cb5d05", 17) == 1 then world.EnablePlugin("aba6daf3fb30699071cb5d05", 0) end
				if world.GetPluginInfo("aba6daf3fb30699071cb5d05", 17) == 0 then world.EnablePlugin("aba6daf3fb30699071cb5d05", 1) end
			end,  -- script function
            
    cooldown = 0,  -- 10 minute cooldown
  }, -- end of button 1


doesn't work... i'm kind of just bumbling around in lua and hoping for the best. lol

I guess the other bit is to edit the cooldown function to handle if cooldown = 0... it should count it as toggleable
Amended on Sat 13 Jun 2009 06:42 AM by LupusFatalis
USA #4
Not sure if this is what you intended, but you basically wrote 'if plugin is enabled, disable; if plugin is disabled, disable'.
#5
fixed... but didn't fix the problem.
Netherlands #6
Scripts inside a plugin won't run while the plugin is disabled. So clicking a miniwindow used by the same plugin to enable it again won't work.

I usually get this kind of functionality working with an extra plugin that is used solely for enabling/disabling.
#7
Oh, this is a completely separate plugin. I'm just trying to edit nick's Icon_Bar plugin to work for me. I'm having trouble with it because its Lua. The plugin I'm enabling/disabling is a different one entirely, and written in python. Its likely that I'll put in these toggle buttons for quite a few plugins if I can get them working. Just trying to get the one working for now.
Australia Forum Administrator #8
I tried your test on an existing plugin:


/print (GetPluginInfo("391192793248409895090099", 17))  --> true


In Lua, true is not the same as 1, one is a boolean value, one is a number. Thus the tests "== 1" and "== 0" will always fail, as the function returns true and false.

The documentation for GetPluginInfo correctly states it returns a boolean:


17: Enabled? (boolean)

Amended on Sun 14 Jun 2009 09:24 PM by Nick Gammon
#9
Thanks guys, here is what worked...
  -- button 1
 {
  icon = "Compass_32x32.png",
  tooltip = "Underdark Mapper",
  script = function ()   -- script to execute when clicked
				if world.GetPluginInfo("aba6daf3fb30699071cb5d05", 17) == true then
					world.EnablePlugin("aba6daf3fb30699071cb5d05", false)
				elseif world.GetPluginInfo("aba6daf3fb30699071cb5d05", 17) == false then
					world.EnablePlugin("aba6daf3fb30699071cb5d05", true)
				end
			end,  -- script function
  }, -- end of button 1
Australia Forum Administrator #10
Now to make it shorter:


-- button 1
 {
  icon = "Compass_32x32.png",
  tooltip = "Underdark Mapper",
  script = function ()   -- script to execute when clicked
      local id = "aba6daf3fb30699071cb5d05"
      EnablePlugin(id, not GetPluginInfo (id, 17))
      end,  -- script function
  }, -- end of button 1


Doing a "not" on a boolean reverses the sense of it (ie. not true is false, not false is true).
Amended on Mon 15 Jun 2009 04:06 AM by Nick Gammon
#11
heh, that's a lot better

Is there any way to have it change icons to distinguish whether it is enabled or disabled? Or gray out the entire bit, like you do with the cooldown? I was thinking maybe altering the cooldown code to accept 0 and act as if it were infinite. But looking at it, its beyond the scope by my bumbling abilities in lua.
Australia Forum Administrator #12
I'm not sure exactly which plugin you borrowed from now, but to make it visually show it is disabled, you could probably lower the contrast of the icon (of course, then you have to put it back again later). See:

http://www.gammon.com.au/mushclient/mw_images.htm
#13
Oh, I'm working on the IconBar plugin.

And this seems to work, not exactly what I was intended (grays the area around the icon instead of the icon itself), but it does convey whether its enabled/disabled. At least after the first click--not ideal, but good enough I suppose.
  -- button 1
 {
  icon = "search_32.png",
  tooltip = "Underdark Mapper",
  script = function ()   -- script to execute when clicked
	local id = "aba6daf3fb30699071cb5d05"
	local id2 = GetPluginID()
	if GetPluginInfo (id, 17) then WindowFilter (id2, 2, 2, 41, 42, 8, 2)
	elseif not GetPluginInfo (id, 17) then WindowFilter (id2, 2, 2, 41, 42, 8, .5) end
	EnablePlugin(id, not GetPluginInfo (id, 17))
	end,  -- script function
  }, -- end of button 1
Australia Forum Administrator #14
Well you have got me going on this one now ...

I am still not sure which plugin you used, the page I mentioned had two on it, but I'll assume you used the lower one, which lets you have horizontal and vertical icons, and be dragged around.

The code below lets you grey out the whole icon, by reloading the image, and decreasing its contrast if the target plugin is not enabled:


 -- button 1
 {
  icon = "search_32.png",
  tooltip = "Underdark Mapper",
  target_plugin = "aba6daf3fb30699071cb5d05",
  script = function (n) 
            
            local id = buttons [n].target_plugin
            
            local f = frames [n]  -- where is image?
            local x1, y1, x2, y2 = f.x1, f.y1, f.x2, f.y2
            
            -- redraw the image
            WindowDrawImage(win, n, 
                    x1 + 1, y1 + 1,   -- left, top
                    x2 - 1, y2 - 1,  -- right, bottom
                    2)  -- mode - stretch or shrink
                    
            -- toggle enable flag for target plugin
            EnablePlugin(id, not GetPluginInfo (id, 17))
            
            -- grey out if disabled now
            if not GetPluginInfo (id, 17) then 
               WindowFilter (win, x1, y1, x2, y2, 8, .4)
            end  -- if
  
            end,  -- function

  },   -- end of button 3


I've fiddled around a bit to make it more readable. For one thing, you don't need to test if something elseif not something. For example:


if GetPluginInfo (id, 17) then
  -- do something
elseif not GetPluginInfo (id, 17) then
  -- do something else
end -- if


A simple else will do that, you don't need to test again, ie.


if GetPluginInfo (id, 17) then
  -- do something
else
  -- do something else
end -- if


Also it seems more logical to me to test if the plugin is enabled *after* you have toggled its enable flag.

My code above simply reloads the image (I wasn't sure if changing its contrast would be symmetrical, the image might have eventually degraded). Fortunately all the images are still in memory. Also the current plugin's window is stored in the variable "win" already.

The only problem I then found was if you reloaded the plugin (after testing) and the target plugin (the mapper) was already disabled, the icon would be drawn the wrong way around. So I added a couple more lines further down where the image is originally loaded. That is, after:


   -- where to draw the icon
    if horizontal then
      x1, y1 = (n - 1) * (ICON_SIZE + OFFSET) + OFFSET, OFFSET
      x2, y2 = n * (ICON_SIZE + OFFSET), ICON_SIZE + OFFSET
    else
      x1, y1 = OFFSET, (n - 1) * (ICON_SIZE + OFFSET) + OFFSET
      x2, y2 = ICON_SIZE + OFFSET, n * (ICON_SIZE + OFFSET)
    end -- if
        
    -- draw the image
    WindowDrawImage(win, n, 
                    x1, y1,   -- left, top
                    x2, y2,  -- right, bottom
                    2)  -- mode - stretch or shrink


Add:



    if v.target_plugin then                    
      -- grey out if disabled now
      if not GetPluginInfo (v.target_plugin, 17) then 
         WindowFilter (win, x1, y1, x2, y2, 8, .4)
      end  -- if
    end -- if


This is why the variable which contained the target plugin's ID had to be moved from a local variable to be inside the table.
Amended on Mon 15 Jun 2009 11:45 PM by Nick Gammon
#15
Hmm... I copied that, the part that checks to see if its disabled when it loads the bar works fine. And the button toggles it on and off, but it only toggles the contrast to normal. Copied exactly what you have.
buttons = {

 -- button 1
 {
  icon = "search_32.png",
  tooltip = "Underdark Mapper",
  target_plugin = "aba6daf3fb30699071cb5d05",
  script = function (n) 
            
            local id = buttons [n].target_plugin
            
            local f = frames [n]  -- where is image?
            local x1, y1, x2, y2 = f.x1, f.y1, f.x2, f.y2
            
            -- redraw the image
            WindowDrawImage(win, n, 
                    x1 + 1, y1 + 1,   -- left, top
                    x2 - 1, y2 - 1,  -- right, bottom
                    2)  -- mode - stretch or shrink
                    
            -- toggle enable flag for target plugin
            EnablePlugin(id, not GetPluginInfo (id, 17))
            
            -- grey out if disabled now
            if not GetPluginInfo (id, 17) then 
               WindowFilter (win, x1, y1, x2, y2, 8, .4)
            end  -- if
  
            end,  -- function

  },   -- end of button 1

   -- button 2
 {
  icon = "bubble_32.png",
  tooltip = "Spell Practice",
  target_plugin = "656eeab9f4fe9bd89d066e25",
  script = function (n) 
            
            local id = buttons [n].target_plugin
            
            local f = frames [n]  -- where is image?
            local x1, y1, x2, y2 = f.x1, f.y1, f.x2, f.y2
            
            -- redraw the image
            WindowDrawImage(win, n, 
                    x1 + 1, y1 + 1,   -- left, top
                    x2 - 1, y2 - 1,  -- right, bottom
                    2)  -- mode - stretch or shrink
                    
            -- toggle enable flag for target plugin
            EnablePlugin(id, not GetPluginInfo (id, 17))
            
            -- grey out if disabled now
            if not GetPluginInfo (id, 17) then 
               WindowFilter (win, x1, y1, x2, y2, 8, .4)
            end  -- if
  
            end,  -- function

  },   -- end of button 2

   -- button 3
 {
  icon = "block_32.png",
  tooltip = "Crafting Omit",
  target_plugin = "157dce098adaa8ceebcbfc88",
  script = function (n) 
            
            local id = buttons [n].target_plugin
            
            local f = frames [n]  -- where is image?
            local x1, y1, x2, y2 = f.x1, f.y1, f.x2, f.y2
            
            -- redraw the image
            WindowDrawImage(win, n, 
                    x1 + 1, y1 + 1,   -- left, top
                    x2 - 1, y2 - 1,  -- right, bottom
                    2)  -- mode - stretch or shrink
                    
            -- toggle enable flag for target plugin
            EnablePlugin(id, not GetPluginInfo (id, 17))
            
            -- grey out if disabled now
            if not GetPluginInfo (id, 17) then 
               WindowFilter (win, x1, y1, x2, y2, 8, .4)
            end  -- if
			
            end,  -- function

  },   -- end of button 3

  
 --> add more buttons here
 
  
} -- end of buttons table


Thanks again Nick!
#16
Oh, I found another spot it won't work, I think. When the plugins are enabled/disabled in other means. Not necessarily by the user, but by the plugin itself. I was thinking maybe putting a heartbeat function in, like you did in the script. But is there a way to reference the id from the buttons rather than manually checking them? etc...
Australia Forum Administrator #17
Did you use the plugin further down that page? The version 2.0 one?

As for the heartbeat, you probably need a timer that fires every second, and in that, loop through every button (like when the buttons are loaded) and draws the image appropriately.

#18
This isn't the latest and greatest: http://www.gammon.com.au/forum/?id=9359 ?
Australia Forum Administrator #19
When I asked which plugin you were working on and you replied:

Quote:

Oh, I'm working on the IconBar plugin.


I released 3 or 4 versions of that, which is why I asked.

In page 1 of this thread I mentioned http://www.gammon.com.au/forum/?id=9280 which has versions 1 and 2 of the plugin, but you are using http://www.gammon.com.au/forum/?id=9359 which is indeed later, but has version 4 of the plugin in it.

Since you are using the latest, greatest version, you also need to add a bit of code to the cooldowns section, which also draws the icon.

In the function SetCooldown, after the lines:


  if max > 0 then
    percent = (amount or 0) / max 
  else
    percent = 0  -- don't divide by zero!
  end -- if
  
  -- reload the image
  if WindowDrawImage(win, n, 
                  x1, y1,   -- left, top
                  x2, y2,  -- right, bottom
                  2)  -- mode - stretch or shrink
    ~= error_code.eOK then
    WindowRectOp (win, 2, x1, y1,   -- left, top
                  x2, y2,  -- right, bottom
                  BACKGROUND_COLOUR)
  end


Add:

           
  if buttons [n].target_plugin then                    
    -- grey out if disabled now
    if not GetPluginInfo (buttons [n].target_plugin, 17) then 
       WindowFilter (win, x1, y1, x2, y2, 8, .4)
    end  -- if
  end -- if


#20
Ok, got it working. And I duplicated some of your code and now have a working heartbeat, check it out!

In the timers section:
  <timer second="1.00" 
         enabled="y" 
         script="heartbeat"
         >
  </timer>

And later in the script:
function heartbeat (timername)
  for n, v in ipairs (buttons) do

    if v.icon then
      if WindowLoadImage (win, n, GetInfo (66) .. v.icon) ~= error_code.eOK then
          DoAfterSpecial (1, string.format ([[
              ColourNote ("white", "red", "Could not load image '%s'")]], 
                          string.gsub (GetInfo (66) .. v.icon, '\', '\\')),
                          sendto.script)
      end -- if
    end -- if icon specified
       
    local x1, y1, x2, y2

    -- where to draw the icon
    if horizontal then
      x1, y1 = (n - 1) * (ICON_SIZE + OFFSET) + OFFSET, OFFSET
      x2, y2 = n * (ICON_SIZE + OFFSET), ICON_SIZE + OFFSET
    else
      x1, y1 = OFFSET, (n - 1) * (ICON_SIZE + OFFSET) + OFFSET
      x2, y2 = ICON_SIZE + OFFSET, n * (ICON_SIZE + OFFSET)
    end -- if
        
    -- draw the image
    WindowDrawImage(win, n, 
                    x1, y1,   -- left, top
                    x2, y2,  -- right, bottom
                    2)  -- mode - stretch or shrink
					
	if v.target_plugin then                    
      -- grey out if disabled now
      if not GetPluginInfo (v.target_plugin, 17) then 
         WindowFilter (win, x1, y1, x2, y2, 8, .4)
      end  -- if
    end -- if
  end --  for each world
end -- heartbeat