Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_features_counter.lua
blob30a7153e0e283acf547904ffe5ab5eb2be249b02
2 -- In Translation file
3 -- Category : uiR2EdCounter --
4 -- CreationFrom : uiR2EdCounterParameters
7 r2.Features.CounterFeature = {}
9 local feature = r2.Features.CounterFeature
11 feature.Name="Counter"
13 feature.Description="A Counter"
16 function feature.registerForms()
17 r2.Forms.CounterFeatureForm =
19 Caption = "uiR2EdCounterParameters",
20 Prop =
22 -- following field are tmp for property sheet building testing
23 {Name="TriggerValue", Type="Number", Category="uiR2EDRollout_CounterFeature", Min="0", Default="0"},
24 {Name="Value", Type="Number", Category="uiR2EDRollout_CounterFeature", Min="1", Default="1"}
28 end
30 feature.Components = {}
32 feature.Components.Counter =
34 BaseClass="LogicEntity",
35 Name="Counter",
36 Menu="ui:interface:r2ed_feature_menu",
38 DisplayerUI = "R2::CDisplayerLua",
39 DisplayerUIParams = "defaultUIDisplayer",
40 DisplayerVisual = "R2::CDisplayerVisualEntity",
41 -----------------------------------------------------------------------------------------------
42 Parameters = {},
44 ApplicableActions = { "Activate", "Deactivate", "Increment", "Decrement", "Trigger"},
46 Events = { "On Trigger", "On Activation", "On Deactivation"},
48 Conditions = { "is active"},
50 TextContexts = {},
52 TextParameters = {},
54 LiveParameters = {},
56 Prop =
58 {Name="InstanceId", Type="String", WidgetStyle="StaticText"},
59 {Name="Name", Type="String", MaxNumChar="32"},
60 {Name="Components", Type="Table"},
61 {Name="Value", Type="Number"},
62 {Name="TriggerValue", Type="Number", Min="0", Default="0"}
65 getParentTreeNode = function(this)
66 return this:getFeatureParentTreeNode()
67 end,
69 appendInstancesByType = function(this, destTable, kind)
70 assert(type(kind) == "string")
71 --this:delegate():appendInstancesByType(destTable, kind)
72 r2.Classes.LogicEntity.appendInstancesByType(this, destTable, kind)
73 for k, component in specPairs(this.Components) do
74 component:appendInstancesByType(destTable, kind)
75 end
76 end,
78 getSelectBarSons = function(this)
79 return Components
80 end,
82 canHaveSelectBarSons = function(this)
83 return false;
84 end,
86 onPostCreate = function(this)
87 --feature.createFeatureLocally(this)
88 end,
90 getAvailableCommands = function(this, dest)
91 r2.Classes.LogicEntity.getAvailableCommands(this, dest) -- fill by ancestor
92 this:getAvailableDisplayModeCommands(dest)
93 end,
95 translate = function(this, context)
96 local initValue = this.Value
97 local trigValue = this.TriggerValue
99 r2.Translator.translateAiGroup(this, context)
101 local rtNpcGrp = r2.Translator.getRtGroup(context, this.InstanceId)
103 local action1 = r2.Translator.createAction("counter_init", rtNpcGrp.Id, initValue, trigValue)
104 local action2 = r2.Translator.createAction("generic_event_trigger", rtNpcGrp.Id, 0)
105 local action = r2.Translator.createAction("multi_actions", {action1, action2})
107 r2.Translator.translateAiGroupInitialState(this, context, action)
108 end,
110 pretranslate = function(this, context)
111 r2.Translator.createAiGroup(this, context)
112 end,
115 local component = feature.Components.Counter
118 function component.getLogicActionTrigger(entity, context, action, rtNpcGrp)
119 local retAction = r2.Translator.createAction("generic_event_trigger", rtNpcGrp.Id, 3)
120 assert(retAction)
121 return retAction, retAction
124 component.getLogicAction = function(entity, context, action)
126 assert( action.Class == "ActionStep")
127 local component = r2:getInstanceFromId(action.Entity)
128 assert(component)
129 local rtNpcGrp = r2.Translator.getRtGroup(context, component.InstanceId)
130 assert(rtNpcGrp)
132 if (action.Action.Type == "Activate") then
133 local action1 = r2.Translator.createAction("counter_enable", rtNpcGrp.Id, 0)
134 local action2 = r2.Translator.createAction("generic_event_trigger", rtNpcGrp.Id, 0)
135 local retAction = r2.Translator.createAction("multi_actions", {action1, action2})
136 return retAction, retAction
137 elseif (action.Action.Type == "Deactivate") then
138 local action1 = r2.Translator.createAction("counter_disable", rtNpcGrp.Id, 0)
139 local action2 = r2.Translator.createAction("generic_event_trigger", rtNpcGrp.Id, 1)
140 local retAction = r2.Translator.createAction("multi_actions", {action1, action2})
141 return retAction, retAction
142 elseif (action.Action.Type == "Increment") then
143 local action1 = r2.Translator.createAction("counter_inc", rtNpcGrp.Id, 0)
144 return action1, action1
145 elseif (action.Action.Type == "Decrement") then
146 local action1 = r2.Translator.createAction("counter_dec", rtNpcGrp.Id, 0)
147 return action1, action1
148 elseif (action.Action.Type == "Trigger") then
149 local retAction = r2.Translator.createAction("generic_event_trigger", rtNpcGrp.Id, 3)
150 assert(retAction)
151 return retAction, retAction
154 local firstAction, lastAction = nil, nil
155 return firstAction, lastAction
158 component.getLogicCondition = function(this, context, condition)
160 assert( condition.Class == "ConditionStep")
161 local component = r2:getInstanceFromId(condition.Entity)
162 assert(component)
163 local rtNpcGrp = r2.Translator.getRtGroup(context, component.InstanceId)
164 assert(rtNpcGrp)
166 if condition.Condition.Type == "is active" then
167 local action1 = r2.Translator.createAction("counter_is_enable", rtNpcGrp.Id, 0);
168 return action1, action1
169 else
170 assert(nil)
172 return nil,nil
175 component.getLogicEvent = function(this, context, event)
176 assert( event.Class == "LogicEntityAction")
178 local component = this -- r2:getInstanceFromId(event.Entity)
179 assert(component)
180 local rtNpcGrp = r2.Translator.getRtGroup(context, component.InstanceId)
181 assert(rtNpcGrp)
183 local eventType = tostring(event.Event.Type)
185 local eventHandler, lastCondition = nil, nil
187 if eventType == "On Trigger" then
188 return r2.Translator.getComponentGenericEvent(rtNpcGrp, 3)
189 elseif eventType == "On Activation" then
190 return r2.Translator.getComponentGenericEvent(rtNpcGrp, 0)
191 elseif eventType == "On Deactivation" then
192 return r2.Translator.getComponentGenericEvent(rtNpcGrp, 1)
195 return eventHandler, firstCondition, lastCondition
199 -- feature part
201 component.createComponent = function(x, y, value, tvalue)
203 local comp = r2.newComponent("Counter")
204 assert(comp)
206 comp.Base = "palette.entities.botobjects.milestone"
207 comp.Name = r2:genInstanceName(i18n.get("uiR2EDRollout_CounterFeature")):toUtf8()
209 comp.Position.x = x
210 comp.Position.y = y
211 comp.Position.z = r2:snapZToGround(x, y)
212 comp.TriggerValue = tvalue
213 comp.Value = value
215 comp._Seed = os.time()
217 return comp
220 component.create = function()
221 r2.requestNewAction(i18n.get("uiR2EDCreateCounterFeatureAction"))
224 if not r2:checkAiQuota() then return end
227 local function paramsOk(resultTable)
229 local x = tonumber( resultTable["X"] )
230 local y = tonumber( resultTable["Y"] )
231 local tvalue = tonumber( resultTable["TriggerValue"] )
232 local value = tonumber( resultTable["Value"] )
234 if not x or not y or not tvalue or not value
235 then
236 debugInfo("Can't create Component")
237 return
239 local component = feature.Components.CounterFeature.createComponent( x, y, value, tvalue)
240 r2.requestInsertNode(r2:getCurrentAct().InstanceId, "Features", -1, "", component)
241 --component.TriggerValue = tvalue
244 local function paramsCancel()
245 debugInfo("Cancel form for 'CounterFeature' creation")
247 local function posOk(x, y, z)
248 debugInfo(string.format("Validate creation of 'CounterFeature' at pos (%d, %d, %d)", x, y, z))
249 r2:doForm("CounterFeatureForm", {X=x, Y=y}, paramsOk, paramsCancel)
251 local function posCancel()
252 debugInfo("Cancel choice 'CounterFeature' position")
253 end
254 r2:choosePos("object_milestone.creature", posOk, posCancel, "createFeatureBanditCamp")
257 function component:registerMenu(logicEntityMenu)
258 local name = i18n.get("uiR2EDRollout_CounterFeature")
259 logicEntityMenu:addLine(ucstring(name), "lua", "", "CounterFeature")
262 function component:getLogicTranslations()
263 local logicTranslations = {
264 ["ApplicableActions"] = {
265 ["Activate"] = { menu=i18n.get( "uiR2AA0Activate" ):toUtf8(),
266 text=i18n.get( "uiR2AA1Activate" ):toUtf8()},
267 ["Desactivate"] = { menu=i18n.get( "uiR2AA0Deactivate" ):toUtf8(),
268 text=i18n.get( "uiR2AA1Deactivate" ):toUtf8()},
269 ["Increment"] = { menu=i18n.get( "uiR2AA0CounterIncrement" ):toUtf8(),
270 text=i18n.get( "uiR2AA1CounterIncrement" ):toUtf8()},
271 ["Decrement"] = { menu=i18n.get( "uiR2AA0CounterDecrement" ):toUtf8(),
272 text=i18n.get( "uiR2AA1CounterDecrement" ):toUtf8()},
274 ["Events"] = {
275 ["activation"] = { menu=i18n.get( "uiR2Event0Activation" ):toUtf8(),
276 text=i18n.get( "uiR2Event1Activation" ):toUtf8()},
277 ["desactivation"] = { menu=i18n.get( "uiR2Event0Deactivation" ):toUtf8(),
278 text=i18n.get( "uiR2Event1Deactivation" ):toUtf8()},
279 ["trigger"] = { menu=i18n.get( "uiR2Event0Trigger" ):toUtf8(),
280 text=i18n.get( "uiR2Event1Trigger" ):toUtf8()},
282 ["Conditions"] = {
283 ["is active"] = { menu=i18n.get( "uiR2Test0Active" ):toUtf8(),
284 text=i18n.get( "uiR2Test1Active" ):toUtf8()}
287 return logicTranslations
290 r2.Features["CounterFeature"] = feature