-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid.lua
More file actions
136 lines (111 loc) · 3.16 KB
/
Copy pathid.lua
File metadata and controls
136 lines (111 loc) · 3.16 KB
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
local currentPath = (...):match('(.-)[^%./]+$')
--- @class IUILib
local iui = require(currentPath .. "iui")
--- @type table<number, table<string, number>>
local cache = {}
local cacheMetatable = { __mode = "k" }
--- @type number[]
local stack = {}
--- @type IUISet<number>
local check = iui.set.new()
function iui.resetIDCheck()
check:removeAll()
end
--- @param name string
--- @param canFocus? boolean
--- @return number
function iui.beginID(name, canFocus)
if canFocus == nil then
canFocus = true
end
if iui.isDisabled() then
canFocus = false
end
-- Initialize the hash to either the current hash, or the default.
local stackSize = #stack
local hash = stackSize > 0 and stack[stackSize] or 2166136261
-- Find the cached ids for the current top id.
local ids = cache[hash]
if ids == nil then
ids = setmetatable({}, cacheMetatable)
cache[hash] = ids
end
-- If there's a cached hash, use it, otherwise calculate it.
local cached = ids[name]
if cached then
hash = cached
else
for idx = 1, #name do
hash = bit.bxor(hash, string.byte(name, idx))
hash = (hash * 16777619) % 0x100000000
end
ids[name] = hash
end
if check:has(hash) then
print("Warning: duplicate hash for " .. name)
else
check:put(hash)
end
-- Push the hash onto the stack.
table.insert(stack, hash)
-- Take focus if nothing else has it, and we're in the desktop idiom.
if canFocus and iui.idiom == "desktop" then
iui.layer.setFocusID(iui.layer.getFocusID() or hash)
end
if iui.layer.getFocusID() == hash and iui.input.keyboard.pressed["tab"] then
if iui.input.keyboard.down:has("lshift") then
iui.layer.setFocusID(iui.layer.getLastID())
else
iui.layer.setFocusID(nil)
end
iui.input.keyboard.pressed["tab"] = nil
end
if canFocus then
iui.layer.setLastID(hash)
end
return hash
end
--- @param advanceLayout? boolean
function iui.endID(advanceLayout)
table.remove(stack)
if advanceLayout == nil or advanceLayout then
iui.layout.advance()
end
end
--- Attempts to set the current ID as the hover ID.
---
--- An ID won't become the hover ID if another ID is the current active ID, or
--- if the control is disabled.
function iui.becomeHover()
if iui.isDisabled() then
return
end
local id = stack[#stack]
if iui.activeID == nil or iui.activeID == id then
iui.hoverID = id
end
end
--- @param id? number
function iui.becomeActive(id)
if not iui.isDisabled() then
if iui.activeID and iui.widgetDeactivated then
iui.widgetDeactivated()
end
id = id or stack[#stack]
iui.activeID = id
iui.hadActiveID = true
if iui.widgetActivated then
iui.widgetActivated()
end
end
end
function iui.becomeFocus()
if not iui.isDisabled() then
iui.layer.setFocusID(stack[#stack])
end
end
--- @param id number
--- @return boolean
function iui.isFocused(id)
return iui.layer.isTop() and iui.layer.getFocusID() == id
end