-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorldOfParkour_TomTomCallbacks.lua
199 lines (178 loc) · 8.05 KB
/
WorldOfParkour_TomTomCallbacks.lua
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0")
local _, addon = ...
local utils = addon.utils
local errors = addon.errors
--[[-------------------------------------------------------------------
-- Dropdown menu code
-------------------------------------------------------------------]] --
local dropdown = CreateFrame("Frame", "TomTomDropdown", nil, "UIDropDownMenuTemplate")
local function completePoint(uid)
local idx = utils.getCoursePointIndex(uid)
local activeCourse = WorldOfParkour.activeCourseStore.activecourse.course
activeCourse[idx].completed = true
-- Check if the user is at the last point in the course.
if idx ~= #activeCourse then
-- Set crazy arrow to the next point in the course.
local nextUid = activeCourse[idx + 1].uid
TomTom:SetCrazyArrow(nextUid, WorldOfParkour.arrivalDistance, nextUid.title)
else
-- This is the final waypoint.
WorldOfParkour.activeCourseStore.activecourse.metadata.isComplete = true
end
TomTom:RemoveWaypoint(uid)
-- Notify that a point as been completed
AceConfigRegistry:NotifyChange("WorldOfParkour")
end
local function getAllPreviousUncompletedPoints(uid)
local previousUncompletedPoints = {}
local activeCourse = WorldOfParkour.activeCourseStore.activecourse.course
for _, v in pairs(activeCourse) do
if v.completed == false then
table.insert(previousUncompletedPoints, v.uid)
-- Break the loop when we hit the current point.
if TomTom:GetKey(v.uid) == TomTom:GetKey(uid) then break end
end
end
return previousUncompletedPoints
end
local dropdown_info = {
-- Define level one elements here
[1] = {
{ -- Title
text = "WorldOfParkour Point Options",
isTitle = 1
}, {
-- set as crazy arrow
text = "Set as waypoint arrow",
func = function()
if WorldOfParkour:IsNotInEditMode() then errors.notInEditModeError() end
local uid = dropdown.uid
TomTom:SetCrazyArrow(uid, WorldOfParkour.arrivalDistance, uid.title or "TomTom waypoint")
end
},
{ -- Add previous point, can be used if the user needs to recomplete the previous point for any reason.
text = "Show previous point",
func = function()
-- Don't clear if we are in edit mode.
if WorldOfParkour:IsInEditMode() then errors.inEditModeError() end
local uid = dropdown.uid
local nextUncompletedPoint = WorldOfParkour:GetNextUncompletedPoint()
if TomTom:GetKey(uid) ~= TomTom:GetKey(nextUncompletedPoint) then
WorldOfParkour:Error("The previous point is already shown.")
end
local idx = utils.getCoursePointIndex(uid)
local lastIdx = idx - 1
if lastIdx == 0 then
WorldOfParkour:Error("You are already at the first point!")
end
local activeCourse = WorldOfParkour.activeCourseStore.activecourse.course
local lastUid = activeCourse[lastIdx].uid
local m, x, y, options = WorldOfParkour:CreateTomTomWaypointArgs(lastUid)
local newLastUid = TomTom:AddWaypoint(m, x, y, options)
-- Uncomplete the last point
WorldOfParkour.activeCourseStore.activecourse.course[lastIdx].completed = false
TomTom:SetCrazyArrow(newLastUid, WorldOfParkour.arrivalDistance, newLastUid.title)
AceConfigRegistry:NotifyChange("WorldOfParkour")
end
}, { -- Remove waypoint
text = "Remove waypoint",
func = function()
if WorldOfParkour:IsNotInEditMode() then errors.notInEditModeError() end
local uid = dropdown.uid
WorldOfParkour:RemoveWaypointAndReorder(uid)
AceConfigRegistry:NotifyChange("WorldOfParkour")
-- TomTom:Printf("Removing waypoint %0.2f, %0.2f in %s", data.x, data.y, data.zone)
end
},
{ -- Complete point, can be used if TomTom is being weird and not clearing the waypoint automatically.
text = "Complete point",
func = function()
-- Don't clear if we are in edit mode.
if WorldOfParkour:IsInEditMode() then errors.inEditModeError() end
local uid = dropdown.uid
-- Make sure user is within clear distance before clearing.
if TomTom:GetDistanceToWaypoint(uid) > WorldOfParkour.clearDistance then
WorldOfParkour:Error("You need to be closer to complete this point.")
end
-- If the user skipped some points, they can stand on whatever point is closest to them
-- And complete all previous points.
local allPreviousUncompletedPoints = getAllPreviousUncompletedPoints(uid)
for _, previousWaypoints in pairs(allPreviousUncompletedPoints) do
completePoint(previousWaypoints)
end
end
}, { -- Show hint
text = "Show hint",
func = function()
local uid = dropdown.uid
local title = uid.title
local idx = utils.getCoursePointIndex(uid)
local coursePoint = WorldOfParkour.activeCourseStore.activecourse.course[idx]
WorldOfParkour:Printf("Hint for %s: %s", title, coursePoint.hint)
end
}
}
}
local function init_dropdown(level)
-- Make sure level is set to 1, if not supplied
level = 1
-- Get the current level from the info table
local info = dropdown_info[level]
-- If a value has been set, try to find it at the current level
if level > 1 and UIDROPDOWNMENU_MENU_VALUE then
if info[UIDROPDOWNMENU_MENU_VALUE] then info = info[UIDROPDOWNMENU_MENU_VALUE] end
end
-- Add the buttons to the menu
for idx, entry in ipairs(info) do
if type(entry.checkeda) == "function" then
-- Make this button dynamic
local new = {}
for k, v in pairs(entry) do new[k] = v end
new.checked = new.checked()
entry = new
else
entry.checked = nil
end
UIDropDownMenu_AddButton(entry, level)
end
end
function InitializeDropdown(uid)
dropdown.uid = uid
UIDropDownMenu_Initialize(dropdown, init_dropdown)
end
--[[-------------------------------------------------------------------
-- Callbacks
-------------------------------------------------------------------]] --
local function _both_onclick(event, uid, self, button)
InitializeDropdown(uid)
_G.ToggleDropDownMenu(1, nil, dropdown, "cursor", 0, 0)
end
local function _both_clear_distance(event, uid, range, distance, lastdistance)
-- Don't clear if we are in edit mode.
if WorldOfParkour:IsInEditMode() then return end
-- Dont clear if it is not the next waypoint.
local nextUncompletedPoint = WorldOfParkour:GetNextUncompletedPoint()
if TomTom:GetKey(uid) ~= TomTom:GetKey(nextUncompletedPoint) then return end
-- Only clear the waypoint if we weren't inside it when it was set
if lastdistance and not UnitOnTaxi("player") then completePoint(uid) end
end
function WorldOfParkour:CreateTomTomCallbacks()
local defaultCallbacks = TomTom:DefaultCallbacks()
local callbacks = {
minimap = {
onclick = _both_onclick,
tooltip_show = defaultCallbacks.minimap.tooltip_show,
tooltip_update = defaultCallbacks.minimap.tooltip_update
},
world = {
onclick = _both_onclick,
tooltip_show = defaultCallbacks.world.tooltip_show,
tooltip_update = defaultCallbacks.world.tooltip_update
},
distance = {
-- This table is indexed by distance, so the Key is the distance to clear the waypoint.
[self.clearDistance] = function(...) _both_clear_distance(...); end
}
}
return callbacks
end