View Single Post
  #17  
Unread 07-13-2011, 12:15 PM
Equendil Equendil is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Apr 2011
Posts: 52
In case it helps, I'm using the following in my plugins, so I can manage my windows generically for F12 and Escape (the actual version I use also handles CTRL+\ for my drag bars, but I stripped that here):

Code:
import "Turbine.UI";

-- WindowManager lets us handles HUD toggling ("F12") and 'Escape" actions in a global manner
-- Managed windows will be made visible/invisible/closed based on those events.

-- WindowManager is a global unique instance 

-- Actions not defined in Turbine.UI.Lotro.Action
local ActionToggleDisplay = 0x100000B3;

-- local class, we don't want anyone else to reify this
local Manager = class( Turbine.UI.Window );
function Manager:Constructor()
	Turbine.UI.Window.Constructor( self );

	-- weak table so we don't prevent windows from being garbage collected
	self.windows = setmetatable({}, {__mode="k"}); 
	self.closeableWindows = setmetatable({}, {__mode="k"});
	
	-- status
	self.visibleHUD = true;
	
	-- action handler
	self:SetWantsKeyEvents(true);
	self.KeyDown = function( sender, args )
		if args.Action == ActionToggleDisplay then -- toggle display
			-- if the display was visible, save the visibility status of managedwindows
			if self.visibleHUD then
				for window, visible in pairs( self.windows ) do 
					self.windows[window] = window:IsVisible()
				end
			end
			
			self.visibleHUD = not self.visibleHUD;
			
			-- update visibility of all managed windows
			for window, visibility in pairs( self.windows ) do
				window:SetVisible( self.visibleHUD and visibility );
			end
		elseif args.Action == Turbine.UI.Lotro.Action.Escape then
			for window, v in pairs( self.closeableWindows ) do
				window:Close();
			end
			return;
		end
	end
end

-- manage a window
function Manager:ManageWindow( window, closeOnEscape )
	self.windows[window] = window:IsVisible();
	window:SetVisible( self.visibleHUD and window:IsVisible() );
	
	if closeOnEscape == nil or closeOnEscape == true then -- default = close on escape
		self.closeableWindows[window] = true;
	end
end

function Manager:IsGUIVisible()
	return self.visibleHUD;
end

-- create a unique instance of the window manager
WindowManager = Manager();
I then only have to call WindowManager:ManageWindow( window, <closeOnEscape> ) in my plugins, to have windows be made invisible/visible on F12 and be closed on ESC if <closeOnEscape> is true (or nil, default behaviour).

Note: I use weak references to 'managed' windows so as to not prevent the garbage collector from getting rid of unused windows.
__________________
Author of LIP, Bootstrap and Baruk

Last edited by Equendil : 07-13-2011 at 12:19 PM.
Reply With Quote