Jump to content

[Sourcecode] Window (Widget) Manager Script


Andy90
 Share

Recommended Posts

 

Hello, after some development, my game has been growing quite well. With the expansion of the game, the number of UI "windows" has also increased. To effectively manage all these windows or widgets, I've decided to create a simple script that will handle them. The script includes a table with the information about each "window" and several functions:

  • HideAll(): This function hides all managed windows.
  • AddWindow(widget, show_callback, hide_callback): This function adds a window to the table. The parameters are straightforward. widget refers to the created widget, show_callback is a function that is called whenever the manager opens this window, and hide_callback is called whenever the manager closes the window. It will also return the window ID for the added widget.
  • Show(id): This function displays the window with the specified window ID (which you received from the AddWindow function).
  • Hide(id): It hides the window with the given ID (which you received from the AddWindow function).
  • IsAnyWindowOpen(): This function checks if any window is open.
     

WindowManager.lua
 

WindowManager = {}

function WindowManager:AddWindow(window, show_callback, hide_callback)
	table.insert(self, {
		Widget = window,
		OnShow = show_callback,
		OnHide = hide_callback
	})
	return #self
end

function WindowManager:HideAll() 
	for _, item in ipairs(self) do
		if not item.Widget:Hidden() then
			item.Widget:Hide()
			item.OnHide()
		end
	end
end

function WindowManager:Show(index) 
	self:HideAll()
	self[index].Widget:Show()
	self[index].OnShow()
end

function WindowManager:Hide(index) 
	if self[index] ~= nil then
		if not self[index].Widget:Hidden() then
			self[index].Widget:Hide()
			self[index].OnHide()
		end
	end
end

function WindowManager:IsAnyWindowOpen() 
	for _, item in ipairs(self) do
		if not item.Widget:Hidden() then
			return true
		end
	end
	return false
end

Implementation the Script
Just include the script file within your main lua. For example

import("Scripts/Own/WindowManager.lua")

Adding a widget

	self.panel = Widget:Panel(10, 10, 600, 500, base)
	self.windowID =
		WindowManager:AddWindow(
		self.panel,
		function()
			self:ShowUI()
		end,
		function()
			self:HideUI()
		end
	)

I hope this can help someone :) maybe @Josh can create a new forum category for source codes which other users can use.

 

  • Like 3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...