Back to Home

ESO Lua File v100031

pregame/charactercreate/zo_charactercreatetriangle_base.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
ZO_CharacterCreateTriangle_Base = ZO_Object:Subclass()
function ZO_CharacterCreateTriangle_Base:New(...)
    local triangle = ZO_Object.New(self)
    triangle:Initialize(...)
    return triangle
end
function ZO_CharacterCreateTriangle_Base:Initialize(triangleControl, setterFn, updaterFn, triangleStringId, topStringId, leftStringId, rightStringId)
    GetControl(triangleControl, "LabelTop"):SetText(GetString(topStringId))
    GetControl(triangleControl, "LabelLeft"):SetText(GetString(leftStringId))
    GetControl(triangleControl, "LabelRight"):SetText(GetString(rightStringId))
    local pickerControl = GetControl(triangleControl, "Picker")
    self.width, self.height = pickerControl:GetDimensions()
    local picker = ZO_TrianglePicker:New(self.width, self.height, pickerControl:GetParent(), pickerControl)
    local thumb = GetControl(pickerControl, "Thumb")
    thumb:SetDrawLevel(1)
    picker:SetThumb(thumb)
    picker:SetThumbPosition(pickerControl:GetWidth() * 0.5, pickerControl:GetHeight() * 0.67) -- put the thumb in the "center" of the triangle for now.
    picker:SetUpdateCallback(function(picker, x, y) self:SetValue(x, y) end)
    triangleControl.sliderObject = self
    self.control = triangleControl
    self.padlock = GetControl(triangleControl, "Padlock")
    self.thumb = thumb
    self.picker = picker
    self.setterFn = setterFn
    self.updaterFn = updaterFn
    self.lockState = TOGGLE_BUTTON_OPEN
    -- NOTE: This button data may change to be defined by whatever is using the triangle object...for all triangles use the same icon layouts.
    -- NOTE: The coordinates are normalized!! You can make the window any size and the subtriangles will still be correct.
    -- Liberties have been taken with the normalized values to account for image data (make the buttons appear at the correct junction points)
    self.triangleButtonData =
    {
        { x = 0.5, y = 0, },          -- point 0 (top)
        { x = 0.7455, y = 0.5034, },  -- proceeding clockwise, point 1, etc...
        { x = 1, y = 1, },
        { x = 0.5, y = 1, },
        { x = 0, y = 1, },
        { x = 0.2544, y = 0.5034, },
    }
    -- The picker control is split into 4 sub triangles, the picker defines points in CCW order, but the sub triangles
    -- used by the game are in CW order, so adjust accordingly
    self.subTrianglePoints =
    {
        { 6, 2, 1 },
        { 4, 3, 2 },
        { 6, 2, 4, isMirrored = true }, -- the upside down triangle
        { 5, 4, 6},
    }
    -- Create control points
    local baseName = triangleControl:GetName().."Point"
    local width = pickerControl:GetWidth()
    local height = pickerControl:GetHeight()
    self.subTriangles = {}
    local points = {}
    for i = 1, #self.subTrianglePoints do
        local pointIndices = self.subTrianglePoints[i]
        local isMirrored = pointIndices.isMirrored
        ZO_TrianglePoints_SetPoint(points, 1, self.triangleButtonData[pointIndices[1]], isMirrored)
        ZO_TrianglePoints_SetPoint(points, 2, self.triangleButtonData[pointIndices[2]], isMirrored)
        ZO_TrianglePoints_SetPoint(points, 3, self.triangleButtonData[pointIndices[3]], isMirrored)
        self.subTriangles[i] = ZO_Triangle:New(points, isMirrored)
    end
end
function ZO_CharacterCreateTriangle_Base:ToggleLocked()
    self.lockState = not self.lockState
    ZO_ToggleButton_SetState(self.padlock, self.lockState)
end
function ZO_CharacterCreateTriangle_Base:SetLocked(isLocked)
    if self:IsLocked() ~= isLocked then
        self:ToggleLocked()
    end
end
function ZO_CharacterCreateTriangle_Base:CanLock()
    return true
end
function ZO_CharacterCreateTriangle_Base:IsLocked()
    return self.lockState ~= TOGGLE_BUTTON_OPEN
end
function ZO_CharacterCreateTriangle_Base:Randomize(randomizeType)
    if self.lockState == TOGGLE_BUTTON_OPEN then
        local triangle = zo_random(1, #self.subTrianglePoints)
        local a = zo_random() * .5
        local b = zo_random() * .5
        self.setterFn(triangle, a, b)
        if self.onValueChangedCallback then
            self.onValueChangedCallback()
        end
        self:Update()
    end
end
function ZO_CharacterCreateTriangle_Base:SetOnValueChangedCallback(onValueChangedCallback)
end
local function LengthSquared(x1, y1, x2, y2)
    local x = x2 - x1
    local y = y2 - y1
    return (x * x) + (y * y)
end
-- NOTE: x and y are normalized
function ZO_CharacterCreateTriangle_Base:GetSubTriangle(x, y)
    local closestX
    local closestY
    local closestTri
    for triIndex, triangle in ipairs(self.subTriangles) do
        local cX, cY, isInside = triangle:ContainsPoint(x, y)
        if isInside then
            return triIndex, cX, cY
        end
        if not closestX or (LengthSquared(cX, cY, x, y) < LengthSquared(closestX, closestY, x, y)) then
            closestX = cX
            closestY = cY
            closestTri = triIndex
        end
    end
    return closestTri, closestX, closestY
end
-- NOTE: x and y are normalized
function ZO_CharacterCreateTriangle_Base:GetSubTriangleParams(triIndex, x, y)
    return self.subTriangles[triIndex]:GetTriangleParams(x, y)
end
function ZO_CharacterCreateTriangle_Base:SetValue(x, y)
    x = x / self.width
    y = y / self.height
    local triangleIndex, subPosX, subPosY = self:GetSubTriangle(x, y)
    local setterParamA, setterParamB = self:GetSubTriangleParams(triangleIndex, subPosX, subPosY)
    self.setterFn(triangleIndex, setterParamA, setterParamB)
    if self.onValueChangedCallback then
        self.onValueChangedCallback()
    end
end
function ZO_CharacterCreateTriangle_Base:Update()
    local triIndex, a, b = self.updaterFn()
    local triangle = self.subTriangles[triIndex]
    if triangle then
        local x, y = triangle:PointFromParams(a, b)
        self.picker:SetThumbPosition(self.width * x, self.height * y)
    end
end
function ZO_CharacterCreateTriangle_Base:UpdateLockState()
    -- optional override
end