#ClientMenu

This commit is contained in:
Applevangelist
2023-07-17 16:26:06 +02:00
parent e8ad649770
commit e8432db26e
4 changed files with 431 additions and 409 deletions

View File

@@ -3,12 +3,14 @@
-- **Main Features:**
--
-- * For complex, non-static menu structures
-- * Separation of menu tree creation from pushing it to clients
-- * Lightweigt implementation as alternative to MENU
-- * Separation of menu tree creation from menu on the clients's side
-- * Works with a SET_CLIENT set of clients
-- * Allow manipulation of the shadow tree in various ways
-- * Push to all or only one client
-- * Change entries' menu text, even if they have a sub-structure
-- * Option to make an entry usable once
-- * Change entries' menu text
-- * Option to make an entry usable once only across all clients
-- * Auto appends GROUP and CLIENT objects to menu calls
--
-- ===
--
@@ -55,7 +57,7 @@
CLIENTMENU = {
ClassName = "CLIENTMENUE",
lid = "",
version = "0.0.2",
version = "0.1.0",
name = nil,
path = nil,
group = nil,
@@ -94,8 +96,12 @@ function CLIENTMENU:NewEntry(Client,Text,Parent,Function,...)
end
self.name = Text or "unknown entry"
if Parent then
self.parentpath = Parent:GetPath()
Parent:AddChild(self)
if Parent:IsInstanceOf("MENU_BASE") then
self.parentpath = Parent.MenuPath
else
self.parentpath = Parent:GetPath()
Parent:AddChild(self)
end
end
self.Parent = Parent
self.Function = Function
@@ -103,7 +109,7 @@ function CLIENTMENU:NewEntry(Client,Text,Parent,Function,...)
table.insert(self.Functionargs,self.group)
table.insert(self.Functionargs,self.client)
if self.Functionargs and self.debug then
self:I({"Functionargs",self.Functionargs})
self:T({"Functionargs",self.Functionargs})
end
if not self.Generic then
if Function ~= nil then
@@ -136,7 +142,7 @@ function CLIENTMENU:NewEntry(Client,Text,Parent,Function,...)
self.path[#self.path+1] = Text
end
self.UUID = table.concat(self.path,";")
self:I({self.UUID})
self:T({self.UUID})
self.Once = false
-- Log id.
self.lid=string.format("CLIENTMENU %s | %s | ", self.ID, self.name)
@@ -144,6 +150,21 @@ function CLIENTMENU:NewEntry(Client,Text,Parent,Function,...)
return self
end
--- Create a UUID
-- @param #CLIENTMENU self
-- @param #CLIENTMENU Parent The parent object if any
-- @param #string Text The menu entry text
-- @return #string UUID
function CLIENTMENU:CreateUUID(Parent,Text)
local path = {}
if Parent and Parent.path then
path = Parent.path
end
path[#path+1] = Text
local UUID = table.concat(path,";")
return UUID
end
--- Set the CLIENTMENUMANAGER for this entry.
-- @param #CLIENTMENU self
-- @param #CLIENTMENUMANAGER Controller The controlling object.
@@ -166,9 +187,9 @@ end
-- @param #CLIENTMENU self
-- @return #CLIENTMENU self
function CLIENTMENU:RemoveF10()
self:I(self.lid.."RemoveF10")
if not self.Generic then
self:I(self.lid.."Removing")
self:T(self.lid.."RemoveF10")
if self.GroupID then
--self:I(self.lid.."Removing "..table.concat(self.path,";"))
missionCommands.removeItemForGroup(self.GroupID , self.path )
end
return self
@@ -214,10 +235,10 @@ end
-- @param #CLIENTMENU self
-- @return #CLIENTMENU self
function CLIENTMENU:RemoveSubEntries()
self:I(self.lid.."RemoveSubEntries")
self:I({self.Children})
self:T(self.lid.."RemoveSubEntries")
self:T({self.Children})
for _id,_entry in pairs(self.Children) do
self:I("Removing ".._id)
self:T("Removing ".._id)
if _entry then
_entry:RemoveSubEntries()
_entry:RemoveF10()
@@ -328,7 +349,21 @@ end
-- ## Add a single entry
--
-- local baimenu = menumgr:NewEntry("BAI",mymenu_lv1b)
-- menumgr:AddEntry(baimenu)
--
-- menumgr:AddEntry(baimenu)
--
-- ## Add an entry with a function
--
-- local baimenu = menumgr:NewEntry("Task Action", mymenu_lv1b, TestFunction, Argument1, Argument1)
--
-- Now, the class will **automatically append the call with GROUP and CLIENT objects**, as this is can only be done when pushing the entry to the clients. So, the actual function implementation needs to look like this:
--
-- function TestFunction( Argument1, Argument2, Group, Client)
--
-- **Caveat is**, that you need to ensure your arguments are not **nil** or **false**, as LUA will optimize those away. You would end up having Group and Client in wrong places in the function call. Hence,
-- if you need/ want to send **nil** or **false**, send a place holder instead and ensure your function can handle this, e.g.
--
-- local baimenu = menumgr:NewEntry("Task Action", mymenu_lv1b, TestFunction, "nil", Argument1)
--
-- ## Change the text of a leaf entry in the menu tree
--
@@ -346,7 +381,7 @@ end
CLIENTMENUMANAGER = {
ClassName = "CLIENTMENUMANAGER",
lid = "",
version = "0.0.4",
version = "0.1.0",
name = nil,
clientset = nil,
menutree = {},
@@ -370,7 +405,7 @@ function CLIENTMENUMANAGER:New(ClientSet, Alias)
-- Log id.
self.lid=string.format("CLIENTMENUMANAGER %s | %s | ", self.version, self.name)
if self.debug then
self:I(self.lid.."Created")
self:T(self.lid.."Created")
end
return self
end
@@ -396,12 +431,21 @@ function CLIENTMENUMANAGER:NewEntry(Text,Parent,Function,...)
return entry
end
--- Check matching entry in the generic structure by UUID.
-- @param #CLIENTMENUMANAGER self
-- @param #string UUID UUID of the menu entry.
-- @return #boolean Exists
function CLIENTMENUMANAGER:EntryUUIDExists(UUID)
local exists = self.flattree[UUID] and true or false
return exists
end
--- Find matching entry in the generic structure by UUID.
-- @param #CLIENTMENUMANAGER self
-- @param #string UUID UUID of the menu entry.
-- @return #CLIENTMENU Entry The #CLIENTMENU object found or nil.
function CLIENTMENUMANAGER:FindEntryByUUID(UUID)
self:I(self.lid.."FindEntryByUUID "..UUID or "None")
self:T(self.lid.."FindEntryByUUID "..UUID or "None")
local entry = nil
for _gid,_entry in pairs(self.flattree) do
local Entry = _entry -- #CLIENTMENU
@@ -412,33 +456,82 @@ function CLIENTMENUMANAGER:FindEntryByUUID(UUID)
return entry
end
--- Find matching entry by text in the generic structure by UUID.
--- Find matching entries by text in the generic structure by UUID.
-- @param #CLIENTMENUMANAGER self
-- @param #string Text Text or partial text of the menu entry to find
-- @param #string Text Text or partial text of the menu entry to find.
-- @param #CLIENTMENU Parent (Optional) Only find entries under this parent entry.
-- @return #table Table of matching UUIDs of #CLIENTMENU objects
-- @return #table Table of matching #CLIENTMENU objects
function CLIENTMENUMANAGER:FindUUIDsByText(Text)
self:I(self.lid.."FindUUIDsByText "..Text or "None")
-- @return #number Number of matches
function CLIENTMENUMANAGER:FindUUIDsByText(Text,Parent)
self:T(self.lid.."FindUUIDsByText "..Text or "None")
local matches = {}
local entries = {}
local n = 0
for _uuid,_entry in pairs(self.flattree) do
local Entry = _entry -- #CLIENTMENU
if Entry and string.find(Entry.name,Text) then
table.insert(matches,_uuid)
table.insert(entries,Entry )
if Parent then
if Entry and string.find(Entry.name,Text) and string.find(Entry.UUID,Parent.UUID) then
table.insert(matches,_uuid)
table.insert(entries,Entry )
n=n+1
end
else
if Entry and string.find(Entry.name,Text) then
table.insert(matches,_uuid)
table.insert(entries,Entry )
n=n+1
end
end
end
return matches, entries
return matches, entries, n
end
--- Find matching entries in the generic structure by the menu text.
-- @param #CLIENTMENUMANAGER self
-- @param #string Text Text or partial text of the F10 menu entry.
-- @param #CLIENTMENU Parent (Optional) Only find entries under this parent entry.
-- @return #table Table of matching #CLIENTMENU objects.
function CLIENTMENUMANAGER:FindEntriesByText(Text)
self:I(self.lid.."FindEntriesByText "..Text or "None")
local matches, objects = self:FindUUIDsByText(Text)
return objects
-- @return #number Number of matches
function CLIENTMENUMANAGER:FindEntriesByText(Text,Parent)
self:T(self.lid.."FindEntriesByText "..Text or "None")
local matches, objects, number = self:FindUUIDsByText(Text, Parent)
return objects, number
end
--- Find matching entries under a parent in the generic structure by UUID.
-- @param #CLIENTMENUMANAGER self
-- @param #CLIENTMENU Parent Find entries under this parent entry.
-- @return #table Table of matching UUIDs of #CLIENTMENU objects
-- @return #table Table of matching #CLIENTMENU objects
-- @return #number Number of matches
function CLIENTMENUMANAGER:FindUUIDsByParent(Parent)
self:T(self.lid.."FindUUIDsByParent")
local matches = {}
local entries = {}
local n = 0
for _uuid,_entry in pairs(self.flattree) do
local Entry = _entry -- #CLIENTMENU
if Parent then
if Entry and string.find(Entry.UUID,Parent.UUID) then
table.insert(matches,_uuid)
table.insert(entries,Entry )
n=n+1
end
end
end
return matches, entries, n
end
--- Find matching entries in the generic structure under a parent.
-- @param #CLIENTMENUMANAGER self
-- @param #CLIENTMENU Parent Find entries under this parent entry.
-- @return #table Table of matching #CLIENTMENU objects.
-- @return #number Number of matches
function CLIENTMENUMANAGER:FindEntriesByParent(Parent)
self:T(self.lid.."FindEntriesByParent")
local matches, objects, number = self:FindUUIDsByParent(Parent)
return objects, number
end
--- Alter the text of a leaf entry in the generic structure and push to one specific client's F10 menu.
@@ -468,8 +561,8 @@ end
-- @param Wrapper.Client#CLIENT Client (optional) If given, propagate only for this client.
-- @return #CLIENTMENU Entry
function CLIENTMENUMANAGER:Propagate(Client)
self:I(self.lid.."Propagate")
self:I(Client)
self:T(self.lid.."Propagate")
self:T(Client)
local Set = self.clientset.Set
if Client then
Set = {Client}
@@ -483,12 +576,12 @@ function CLIENTMENUMANAGER:Propagate(Client)
self.playertree[playername] = {}
end
for level,branch in pairs (self.menutree) do
self:I("Building branch:" .. level)
self:T("Building branch:" .. level)
for _,leaf in pairs(branch) do
self:I("Building leaf:" .. leaf)
self:T("Building leaf:" .. leaf)
local entry = self:FindEntryByUUID(leaf)
if entry then
self:I("Found generic entry:" .. entry.UUID)
self:T("Found generic entry:" .. entry.UUID)
local parent = nil
if entry.Parent and entry.Parent.UUID then
parent = self.playertree[playername][entry.Parent.UUID] or self:FindEntryByUUID(entry.Parent.UUID)
@@ -496,7 +589,7 @@ function CLIENTMENUMANAGER:Propagate(Client)
self.playertree[playername][entry.UUID] = CLIENTMENU:NewEntry(client,entry.name,parent,entry.Function,unpack(entry.Functionargs))
self.playertree[playername][entry.UUID].Once = entry.Once
else
self:I("NO generic entry for:" .. leaf)
self:T("NO generic entry for:" .. leaf)
end
end
end
@@ -521,15 +614,18 @@ function CLIENTMENUMANAGER:AddEntry(Entry,Client)
if client and client:IsAlive() then
local playername = client:GetPlayerName()
if Entry then
self:I("Adding generic entry:" .. Entry.UUID)
self:T("Adding generic entry:" .. Entry.UUID)
local parent = nil
if not self.playertree[playername] then
self.playertree[playername] = {}
end
if Entry.Parent and Entry.Parent.UUID then
parent = self.playertree[playername][Entry.Parent.UUID] or self:FindEntryByUUID(Entry.Parent.UUID)
end
self.playertree[playername][Entry.UUID] = CLIENTMENU:NewEntry(client,Entry.name,parent,Entry.Function,unpack(Entry.Functionargs))
self.playertree[playername][Entry.UUID].Once = Entry.Once
else
self:I("NO generic entry given")
self:T("NO generic entry given")
end
end
end
@@ -577,7 +673,7 @@ end
-- @param Wrapper.Client#CLIENT Client (optional) If given, make this change only for this client. In this case the generic structure will not be touched.
-- @return #CLIENTMENUMANAGER self
function CLIENTMENUMANAGER:DeleteF10Entry(Entry,Client)
self:I(self.lid.."DeleteF10Entry")
self:T(self.lid.."DeleteF10Entry")
local Set = self.clientset.Set
if Client then
Set = {Client}
@@ -588,6 +684,7 @@ function CLIENTMENUMANAGER:DeleteF10Entry(Entry,Client)
if self.playertree[playername] then
local centry = self.playertree[playername][Entry.UUID] -- #CLIENTMENU
if centry then
--self:I("Match for "..Entry.UUID)
centry:Clear()
end
end
@@ -601,7 +698,7 @@ end
-- @param #CLIENTMENU Entry The entry to remove
-- @return #CLIENTMENUMANAGER self
function CLIENTMENUMANAGER:DeleteGenericEntry(Entry)
self:I(self.lid.."DeleteGenericEntry")
self:T(self.lid.."DeleteGenericEntry")
if Entry.Children and #Entry.Children > 0 then
self:RemoveGenericSubEntries(Entry)
@@ -614,11 +711,11 @@ function CLIENTMENUMANAGER:DeleteGenericEntry(Entry)
if tbl[depth] then
for i=depth,#tbl do
self:I("Level = "..i)
--self:I("Level = "..i)
for _id,_uuid in pairs(tbl[i]) do
self:I(_uuid)
if string.find(_uuid,uuid) then
self:I("Match for ".._uuid)
self:T(_uuid)
if string.find(_uuid,uuid) or _uuid == uuid then
--self:I("Match for ".._uuid)
self.menutree[i][_id] = nil
self.flattree[_uuid] = nil
end
@@ -634,7 +731,7 @@ end
-- @param #CLIENTMENU Entry The entry where to start. This entry stays.
-- @return #CLIENTMENUMANAGER self
function CLIENTMENUMANAGER:RemoveGenericSubEntries(Entry)
self:I(self.lid.."RemoveGenericSubEntries")
self:T(self.lid.."RemoveGenericSubEntries")
local depth = #Entry.path + 1
local uuid = Entry.UUID
@@ -643,11 +740,11 @@ function CLIENTMENUMANAGER:RemoveGenericSubEntries(Entry)
if tbl[depth] then
for i=depth,#tbl do
self:I("Level = "..i)
self:T("Level = "..i)
for _id,_uuid in pairs(tbl[i]) do
self:I(_uuid)
self:T(_uuid)
if string.find(_uuid,uuid) then
self:I("Match for ".._uuid)
self:T("Match for ".._uuid)
self.menutree[i][_id] = nil
self.flattree[_uuid] = nil
end
@@ -664,7 +761,7 @@ end
-- @param Wrapper.Client#CLIENT Client (optional) If given, make this change only for this client. In this case the generic structure will not be touched.
-- @return #CLIENTMENUMANAGER self
function CLIENTMENUMANAGER:RemoveF10SubEntries(Entry,Client)
self:I(self.lid.."RemoveSubEntries")
self:T(self.lid.."RemoveSubEntries")
local Set = self.clientset.Set
if Client then
Set = {Client}

View File

@@ -278,7 +278,9 @@ do -- MENU_BASE
end
end
do -- MENU_COMMAND_BASE
do
---
-- MENU_COMMAND_BASE
-- @type MENU_COMMAND_BASE
-- @field #function MenuCallHandler
-- @extends Core.Menu#MENU_BASE
@@ -344,7 +346,9 @@ do -- MENU_COMMAND_BASE
end
end
do -- MENU_MISSION
do
---
-- MENU_MISSION
-- @type MENU_MISSION
-- @extends Core.Menu#MENU_BASE
--- Manages the main menus for a complete mission.
@@ -432,8 +436,9 @@ do -- MENU_MISSION
end
end
do -- MENU_MISSION_COMMAND
do
--- MENU_MISSION_COMMAND
-- @type MENU_MISSION_COMMAND
-- @extends Core.Menu#MENU_COMMAND_BASE
@@ -510,7 +515,8 @@ do -- MENU_MISSION_COMMAND
return self
end
end
do -- MENU_COALITION
do
--- MENU_COALITION
-- @type MENU_COALITION
-- @extends Core.Menu#MENU_BASE
@@ -636,8 +642,9 @@ do -- MENU_COALITION
return self
end
end
do -- MENU_COALITION_COMMAND
do
--- MENU_COALITION_COMMAND
-- @type MENU_COALITION_COMMAND
-- @extends Core.Menu#MENU_COMMAND_BASE
@@ -726,7 +733,10 @@ do
-- So every menu for a client created must be tracked so that program logic accidentally does not create.
-- the same menus twice during initialization logic.
-- These menu classes are handling this logic with this variable.
local _MENUGROUPS = {}
---
-- @type MENU_GROUP
-- @extends Core.Menu#MENU_BASE
@@ -900,7 +910,7 @@ do
return self
end
---
-- @type MENU_GROUP_COMMAND
-- @extends Core.Menu#MENU_COMMAND_BASE
@@ -984,6 +994,7 @@ do
end
--- MENU_GROUP_DELAYED
do
---
-- @type MENU_GROUP_DELAYED
-- @extends Core.Menu#MENU_BASE
@@ -1107,7 +1118,7 @@ do
return self
end
---
-- @type MENU_GROUP_COMMAND_DELAYED
-- @extends Core.Menu#MENU_COMMAND_BASE

View File

@@ -3945,7 +3945,8 @@ do -- SET_STATIC
end
do -- SET_CLIENT
---
-- @type SET_CLIENT
-- @field Core.Timer#TIMER ZoneTimer
-- @field #number ZoneTimerInterval
@@ -4059,7 +4060,7 @@ do -- SET_CLIENT
--- Remove CLIENT(s) from SET_CLIENT.
-- @param Core.Set#SET_CLIENT self
-- @param Wrapper.Client#CLIENT RemoveClientNames A single name or an array of CLIENT names.
-- @param Wrapper.Client#CLIENT RemoveClientNames A single object or an array of CLIENT objects.
-- @return self
function SET_CLIENT:RemoveClientsByName( RemoveClientNames )