77 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
		
		
			
		
	
	
			77 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| 
								 | 
							
								local Hud = {}
							 | 
						|||
| 
								 | 
							
								local ESlateVisibility = import("ESlateVisibility")
							 | 
						|||
| 
								 | 
							
								local WidgetBlueprintLibrary = import("WidgetBlueprintLibrary")
							 | 
						|||
| 
								 | 
							
								local GameplayStatics = import("GameplayStatics")
							 | 
						|||
| 
								 | 
							
								local BusyActorManagerSubSystem = import("BusyActorManagerSubSystem")
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								local function ProcessSuspendShowRequests(hud)
							 | 
						|||
| 
								 | 
							
								    for _, request in pairs(hud.suspend_show_requests) do
							 | 
						|||
| 
								 | 
							
								        hud:CreateAndShowWidget(request[1], request[2])
							 | 
						|||
| 
								 | 
							
								    end
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								function Hud:ctor()
							 | 
						|||
| 
								 | 
							
								    self.layer = nil
							 | 
						|||
| 
								 | 
							
								    self.suspend_show_requests = {}
							 | 
						|||
| 
								 | 
							
								    self.widget_pool = {}
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								function Hud:ReceiveBeginPlay()
							 | 
						|||
| 
								 | 
							
								    print("Hud:ReceiveBeginPlay")
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    if self.layer == nil then
							 | 
						|||
| 
								 | 
							
								        self.layer = self:GetOrCreateWidget("UILayer")
							 | 
						|||
| 
								 | 
							
								    end
							 | 
						|||
| 
								 | 
							
								    if self.layer ~= nil then
							 | 
						|||
| 
								 | 
							
								        self.layer:SetVisibility(ESlateVisibility.SelfHitTestInvisible)
							 | 
						|||
| 
								 | 
							
								        self.layer:AddToViewport(0)
							 | 
						|||
| 
								 | 
							
								        self:CreateAndShowWidget("MainUI", {})
							 | 
						|||
| 
								 | 
							
								        ProcessSuspendShowRequests(self)
							 | 
						|||
| 
								 | 
							
								    end
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								function Hud:ReceiveEndPlay()
							 | 
						|||
| 
								 | 
							
								    print("Hud:ReceiveEndPlay")
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								function Hud:GetOrCreateWidget(widget_name)
							 | 
						|||
| 
								 | 
							
								    local exist_widget = self.widget_pool[widget_name]
							 | 
						|||
| 
								 | 
							
								    if exist_widget ~= nil and exist_widget.bSingletonInstance then
							 | 
						|||
| 
								 | 
							
								        return exist_widget
							 | 
						|||
| 
								 | 
							
								    end
							 | 
						|||
| 
								 | 
							
								    local cls = self.UIClassMapping:Get(widget_name)
							 | 
						|||
| 
								 | 
							
								    local pc = GameplayStatics.GetPlayerController(self, 0)
							 | 
						|||
| 
								 | 
							
								    local widget = WidgetBlueprintLibrary.Create(self, cls, pc)
							 | 
						|||
| 
								 | 
							
								    self.widget_pool[widget_name] = widget
							 | 
						|||
| 
								 | 
							
								    return widget  -- TODO 这里如果非单例widget,会被覆盖
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								function Hud:GetFirstCachedWidget(widget_name)
							 | 
						|||
| 
								 | 
							
								    return self.widget_pool[widget_name]
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								function Hud:CreateAndShowWidget(widget_name, args)
							 | 
						|||
| 
								 | 
							
								    if not self.layer then
							 | 
						|||
| 
								 | 
							
								        table.insert(self.suspend_show_requests, {widget_name, args})
							 | 
						|||
| 
								 | 
							
								        return
							 | 
						|||
| 
								 | 
							
								    end
							 | 
						|||
| 
								 | 
							
								    local widget = self:GetOrCreateWidget(widget_name)
							 | 
						|||
| 
								 | 
							
								    if not widget then return end
							 | 
						|||
| 
								 | 
							
								    self.layer:ShowWidget(widget, args)
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								function Hud:CloseWidget(widget)
							 | 
						|||
| 
								 | 
							
								    self.layer:CloseWidget(widget)
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								function Hud:HideWidgetByName(widget_name)
							 | 
						|||
| 
								 | 
							
								    self.layer:HideWidget(self.widget_pool[widget_name])
							 | 
						|||
| 
								 | 
							
								end
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								return Class(nil, nil, Hud)
							 |