ESO Lua File v100010

libraries/zo_focus/zo_focus.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
ZO_FOCUS_BIDIRECTIONAL = true
local BASE_FOCUS_NAME = "basefocus"
ZO_Focus = ZO_Object:Subclass()
function ZO_Focus:New(...)
    local group = ZO_Object.New(self)
    group:Initialize(...)
    return group
end
function ZO_Focus:Initialize(name, cancelCallback)
    self.controls = {}
    self.name = name
    self.cancelCallback = cancelCallback -- this is an active thing, players choose to cancel. this is not called upon focus deactivation
end
function ZO_Focus:HandlesDigitalDirections()
    return true
end
function ZO_Focus:GetName()
    return self.name
end
function ZO_Focus:GetCurrentFocusObject()
    return self.activeFocus or self
end
function ZO_Focus:IsActive()
    return self.isActive
end
function ZO_Focus:GetCurrentFocusControl()
    local focus = self:GetCurrentFocusObject()
    return focus.focusedControl
end
function ZO_Focus:SetFocusedControl(control)
    local focus = self:GetCurrentFocusObject()
    focus.focusedControl = control
    WINDOW_MANAGER:SetMouseFocusByName(control:GetName())
end
function ZO_Focus:ClearFocusedControl()
    local focus = self:GetCurrentFocusObject()
    focus.focusedControl = nil
end
function ZO_Focus:AddControl(control)
    self.controls[control] = {}
end
function ZO_Focus:LinkControls(control, linkedDirection, linkedControl, bidirectional)
    if(control and linkedControl) then
        self.controls[control][linkedDirection] = linkedControl
        if(bidirectional == ZO_FOCUS_BIDIRECTIONAL) then
            self:LinkControls(linkedControl, ZO_OPPOSITE_DIRECTIONS[linkedDirection], control)
        end
    end
end
function ZO_Focus:ActivateFocus(focus)
    if(IsInGamepadPreferredMode()) then
        -- For now, focus is always a ZO_Focus object
        assert(type(focus) == "table")
        self.activeFocus = focus
        self.isActive = true
        DIRECTIONAL_INPUT:Activate(self)
        HideMouse(true)
    end
end
function ZO_Focus:DeactivateFocus(focus)
    -- hack: since we're trying to make gamepad and keyboard play nicely, this will always deactivate focus group, even if it was started
    -- with keyboard...but the activation only happens if the user initiates focus groups with the gamepad.
    -- gotta come up with the concept of having both a focused FOCUS object and a focused control and maintaining
    -- what gets focused when changing a direction when you're at the edge of a focus group (stack based? purely direction based?
    -- dynamically linking as groups are activated and deactivated?
    focus:ClearFocusedControl()
    if(focus == self.activeFocus) then        
        self.isActive = false
        self.activeFocus = nil
    end
    DIRECTIONAL_INPUT:Deactivate(self)
    ShowMouse(true)
end
function ZO_Focus:FireDirection(direction)
    local focus = self:GetCurrentFocusObject()
    local currentFocus = focus.focusedControl
    if(currentFocus) then
        local nextFocus = focus.controls[currentFocus][direction]
        -- if it's hidden, you're at the edge of this focus group, don't move anywhere
        if(nextFocus and not nextFocus:IsHidden()) then            
            self:SetFocusedControl(nextFocus)
        end
    end
end
function ZO_Focus:ClickActive()
    local focusControl = self:GetCurrentFocusControl()
    if(focusControl) then
        -- local handler = focusControl:GetHandler("OnClicked") or focusControl:GetHandler("OnMouseUp") -- using OnClicked takes different arguments; this should be made more robust.
        local handler = focusControl:GetHandler("OnMouseUp")
        if(handler) then
            handler(focusControl, 1, true)
        end
    end
end
function ZO_Focus:Cancel()
    local focus = self:GetCurrentFocusObject()
    
    if(focus.cancelCallback) then
        focus:cancelCallback()
    elseif(self:GetName() == BASE_FOCUS_NAME) then
        SCENE_MANAGER:ShowBaseScene()
    end
end
FOCUS_MANAGER = ZO_Focus:New(BASE_FOCUS_NAME)