Update lua versions
[ryzomcore.git] / ryzom / client / data / gamedev / interfaces_v3 / misc.lua
blobac920e59124fc37b83801f10214496dc0783cb0e
1 -- misc
2 --
3 if misc == nil then
4 misc = {}
5 end
7 -- convert CRGBA to hexadecimal 6-digits
8 function misc:toHexRGBA(rgba)
9 return string.format("%.2x%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B, rgba.A)
10 end
12 -- convert hexadecimal to its short-hand version
13 function misc:toHexShort(hex)
14 local s = ''
15 for i = 1, #str, 2 do s = s..string.sub(hex:lower(), i, i) end
16 return s
17 end
19 -- ready to use colored chat string
20 function misc:encodeColorTag(hex, str)
21 return "@{"..self:toHexShort(hex).."}"..str
22 end
25 function misc:nilToEmpty(value)
26 if value == nil then
27 return ""
28 end
29 return value
30 end
33 -- inventories observer
34 function misc:initInvObserver(
35 inventory,
36 func_add,
37 func_del,
38 func_change,
39 item_filter
41 if self.invObserver == nil then
42 self.invObserver = {
43 maxSlots = 499, obs = {}
45 function self.invObserver:__remove(n)
46 if #self.obs > 0 then
47 self.obs[n] = nil
48 end
49 end
50 function self.invObserver:__update(n)
51 self.obs[n].cache = nil
52 self:__observ(n)
53 end
54 function self.invObserver:__observ(n)
55 local t = {}
56 local n = n or 1
57 if #self.obs <= 0 then
58 return
59 end
60 for i = 0, self.maxSlots do
61 local sheet = getDbProp(self.obs[n].inv..":"..i..":SHEET")
62 if sheet > 0 then
63 t[sheet] = getDbProp(self.obs[n].inv..":"..i..":QUANTITY")
64 if self.obs[n].filter then
65 local name = getSheetName(sheet)
66 if not name:match(self.obs[n].filter) then
67 t[sheet] = nil
68 end
69 end
70 end
71 end
72 if not self.obs[n].cache then
73 self.obs[n].cache = t
74 t = {}
75 return
76 end
77 local lenT = function(t)
78 local i = 0
79 for _ in pairs(t) do i = i + 1 end return i
80 end
81 local t_len, b_len = lenT(t), lenT(self.obs[n].cache)
82 if t_len ~= b_len then
83 if t_len > b_len then
84 self.obs[n].onAdd(n)
85 else
86 self.obs[n].onDel(n)
87 end
88 self:__update(n)
89 return
90 end
91 for k, v in pairs(self.obs[n].cache) do
92 local found = false
93 for k1, v1 in pairs(t) do
94 if k == k1 then found = true
95 break
96 end
97 end
98 if found then
99 for k1, v1 in pairs(t) do
100 if k == k1 then
101 if v == v1 then break end
102 self.obs[n].onChange(n)
103 self:__update(n)
106 else
107 self:__update(n)
111 function self.invObserver:add(uiWindow, n)
112 if uiWindow then
113 setOnDraw(uiWindow, formatUI("misc.invObserver:__observ(#1)", n))
116 function self.invObserver:remove(uiWindow, n)
117 if uiWindow then
118 setOnDraw(uiWindow, "")
119 self:__remove(n)
122 self.invObserver.__index = self.invObserver
124 if inventory then
125 self.invObserver.obs[#self.invObserver.obs+1] =
127 inv = inventory,
128 onAdd = func_add,
129 onDel = func_del,
130 onChange = func_change,
131 filter = item_filter,
132 cache = nil
135 return setmetatable(misc, self.invObserver)
138 -- VERSION --
139 RYZOM_MISC_VERSION = 10469