Merge branch 'fixes' into main/gingo-test
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_texts.lua
blob279072535076bd24b41c8b978d167f4842faf61d
1 local registerFeature = function()
2 local feature = {}
3 feature.Name = "TextManager"
5 feature.Description = "A little texts manager"
6 feature.Components=
9 Name="TextManager",
10 Prop=
12 {Name="InstanceId",Type="String"},
13 {Name="Texts",Type="Table"},
17 Name="TextManagerEntry",
18 Prop=
20 {Name="InstanceId", Type="String" },
21 {Name="Text",Type="String"},
22 {Name="Count",Type="Number"}
29 --returns nil if the text is still in the textManager
30 --else, return a new entry, not inserted in the TextManager
31 feature.checkText = function(textManager,text)
32 local texts = r2.Scenario.Texts.Texts
33 k,entry = next(texts,nil)
34 while k ~=nil do
35 local textEntry = entry.Text
36 if textEntry==text
37 then
38 return entry
39 end
40 k,entry = next(texts,k)
41 end
42 entry = r2.newComponent("TextManagerEntry")
43 entry.Text=text
44 entry.Count=0
45 return entry
46 end
48 feature.getText = function (textManager, entry)
50 debugInfo("feature.getText")
52 for i=0, textManager.Texts.Size-1 do
54 local text = textManager.Texts[i]
56 if text.InstanceId == entry
57 then
58 return text.Text
59 end
60 end
61 return nil
62 end
64 --add a text to the text manager.
65 --if the text exist, increment its counter
66 feature.addText = function(textManager,text)
67 local max = table.getn(textManager.Texts)
68 local entry
70 for i=1,max do
71 entry = textManager.Texts[i]
72 if entry.Text==text
73 then
74 entry.Count = entry.Count+1
75 return entry
76 end
77 end
79 entry = r2.newComponent("TextManagerEntry")
80 entry.Text=text
81 entry.Count=1
82 table.insert(textManager.Texts,entry)
83 return entry
84 end
86 --decrement the counter of a text.
87 --if this counter==0, remove the text from the TextManager
88 feature.removeText = function(textManager, text)
90 for i=0,textManager.Texts.Size-1 do
91 local entry = textManager.Texts[i]
92 if entry.Text==text then
94 local newCount = entry.Count-1
96 if newCount==0 then
97 r2.requestEraseNode(entry.InstanceId, "", -1)
98 else
99 r2.requestSetNode(entry.InstanceId, "Count", newCount)
103 end
107 feature.Translator = function(context)
108 local comp = context.Feature
109 local texts = context.Feature.Texts
110 local entry
111 local rtTextMgr = context.RtScenario.Texts
112 --for each entry
113 local k,v = next(texts,nil)
114 while v~=nil do
115 if k~="Keys"
116 then
117 --create and fill a RT entry
118 entry = r2.newComponent("RtEntryText")
119 entry.Text = v.Text
120 --insert it in the RT text manager
121 table.insert(rtTextMgr.Texts,entry)
122 local tmp = {}
123 table.insert(tmp,v.InstanceId)
124 table.insert(tmp,entry.Id)
125 table.insert(context.TextTranslateId,tmp)
127 k,v = next(texts,k)
129 if table.getn(context.TextTranslateId)==0
130 then
131 debugInfo("translator:: pas d'entrees dans la table!!!")
135 -- ?
136 feature.getRtId = function(context, instanceId)
137 local tab = context.TextTranslateId
138 assert(tab~=nil)
139 for k,v in pairs(tab)
141 if instanceId == v[1]
142 then
143 return v[2]
146 local max = table.getn(tab)
147 if max==0
148 then
149 debugInfo(colorTag(255, 255, 0).."WAR: The text table is empty.")
150 return
153 for i=1,max do
154 if instanceId == tab[i][1]
155 then
156 return tab[i][2]
161 return feature
164 r2.Features["TextManager"] = registerFeature()