File: actions.lua

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (302 lines) | stat: -rw-r--r-- 8,469 bytes parent folder | download | duplicates (7)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    actions.lua
--  brief:   action interface for text commands, and bound commands
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local actionHandler = {
  textActions       = {},
  keyPressActions   = {},
  keyRepeatActions  = {},
  keyReleaseActions = {},
  syncActions = {}
}

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local function ParseTypes(types, def)
  if (type(types) ~= "string") then
    types = def
  end
  local text       = (string.find(types, "t") ~= nil)
  local keyPress   = (string.find(types, "p") ~= nil)
  local keyRepeat  = (string.find(types, "R") ~= nil)
  local keyRelease = (string.find(types, "r") ~= nil)
  return text, keyPress, keyRepeat, keyRelease
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Insertions
--

local function InsertCallInfo(callInfoList, widget, func, data)
  local layer = widget.whInfo.layer
  local index = 1
  for i,ci in ipairs(callInfoList) do
    local w = ci[1]
    if (w == widget) then
      return false  --  already in the table
    end
    if (layer >= w.whInfo.layer) then
      index = i + 1
    end
  end
  table.insert(callInfoList, index, { widget, func, data })
  return true
end


function actionHandler:AddAction(widget, cmd, func, data, types)
  local function add(actionMap)
    local callInfoList = actionMap[cmd]
    if (callInfoList == nil) then
      callInfoList = {}
      actionMap[cmd] = callInfoList
    end
    return InsertCallInfo(callInfoList, widget, func, data)
  end

  -- make sure that this is a fully initialized widget
  if (not widget.whInfo) then
    error("LuaUI error adding action: please use widget:Initialize()")
  end

  -- default to text and keyPress  (not repeat or releases)
  local text, keyPress, keyRepeat, keyRelease = ParseTypes(types, "tpRr")
  
  local tSuccess, pSuccess, RSuccess, rSuccess = false, false, false, false

  if (text)       then tSuccess = add(self.textActions)       end
  if (keyPress)   then pSuccess = add(self.keyPressActions)   end
  if (keyRepeat)  then RSuccess = add(self.keyRepeatActions)  end
  if (keyRelease) then rSuccess = add(self.keyReleaseActions) end

  return tSuccess, pSuccess, RSuccess, rSuccess
end


local function AddMapAction(map, widget, cmd, func, data)
  local callInfoList = map[cmd]
  if (callInfoList == nil) then
    callInfoList = {}
    map[cmd] = callInfoList
  end
  return InsertCallInfo(callInfoList, widget, func, data)
end


function actionHandler:AddSyncAction(widget, cmd, func, data)
  return AddMapAction(self.syncActions, widget, cmd, func, data)
end



--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Removals
--

local function RemoveCallInfo(callInfoList, widget)
  local count = 0
  for i,callInfo in ipairs(callInfoList) do
    local w = callInfo[1]
    if (w == widget) then
      table.remove(callInfoList, i)
      count = count + 1
      -- break
    end
  end
  return count
end


function actionHandler:RemoveAction(widget, cmd, types)
  local function remove(actionMap)
    local callInfoList = actionMap[cmd]
    if (callInfoList == nil) then
      return false
    end
    local count = RemoveCallInfo(callInfoList, widget)
    if (#callInfoList <= 0) then
      actionMap[cmd] = nil
    end
    return (count > 0)
  end

  -- default to removing all
  local text, keyPress, keyRepeat, keyRelease = ParseTypes(types, "tpRr")
  
  local tSuccess, pSuccess, RSuccess, rSuccess = false, false, false, false

  if (text)       then tSuccess = remove(self.textActions)       end
  if (keyPress)   then pSuccess = remove(self.keyPressActions)   end
  if (keyRepeat)  then RSuccess = remove(self.keyRepeatActions)  end
  if (keyRelease) then rSuccess = remove(self.keyReleaseActions) end
  
  return tSuccess, pSuccess, RSuccess, rSuccess
end


local function RemoveMapAction(map, widget, cmd)
  local callInfoList = map[cmd]
  if (callInfoList == nil) then
    return false
  end
  local count = RemoveCallInfo(callInfoList, widget)
  if (#callInfoList <= 0) then
    map[cmd] = nil
  end
  return (count > 0)
end


function actionHandler:RemoveSyncAction(widget, cmd)
  return RemoveMapAction(self.syncActions, widget, cmd)
end


function actionHandler:RemoveWidgetActions(widget)
  local function clearActionList(actionMap)
    for cmd, callInfoList in pairs(actionMap) do
      RemoveCallInfo(callInfoList, widget)
    end
  end
  clearActionList(self.textActions)
  clearActionList(self.keyPressActions)
  clearActionList(self.keyRepeatActions)
  clearActionList(self.keyReleaseActions)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  Calls
--

local function MakeWords(line)
  local words = {}
  for w in string.gmatch(line, "[^%s]+") do
    table.insert(words, w)
  end
  return words
end


local function MakeKeySetString(key, mods)
  local keyset = ""
  if (mods.alt)   then keyset = keyset .. "A+" end
  if (mods.ctrl)  then keyset = keyset .. "C+" end
  if (mods.meta)  then keyset = keyset .. "M+" end
  if (mods.shift) then keyset = keyset .. "S+" end
  local userSym, defSym = Spring.GetKeySymbol(key)
  return (keyset .. defSym)
end


local function TryAction(actionMap, cmd, optLine, optWords, isRepeat, release)
  local callInfoList = actionMap[cmd]
  if (callInfoList == nil) then
    return false
  end
  for i,callInfo in ipairs(callInfoList) do
    --local widget = callInfo[1]
    local func   = callInfo[2]
    local data   = callInfo[3]
    if (func(cmd, optLine, optWords, data, isRepeat, release)) then
      return true
    end
  end
  return false
end


function actionHandler:KeyAction(press, key, mods, isRepeat)
  local keyset = MakeKeySetString(key, mods)
  local defBinds = Spring.GetKeyBindings(keyset)
  if (defBinds) then
    local actionSet
    if (press) then
      actionSet = isRepeat and self.keyRepeatActions or self.keyPressActions
    else
      actionSet = self.keyReleaseActions
    end
    for b,bAction in ipairs(defBinds) do
      local bCmd, bOpts = next(bAction, nil)
		  local words = MakeWords(bOpts)
      if (TryAction(actionSet, bCmd, bOpts, words, isRepeat, not press)) then
        return true
      end
    end
  end
  return false
end


function actionHandler:TextAction(line)
  local words = MakeWords(line)
  local cmd = words[1]
  if (cmd == nil) then
    return false
  end
  -- remove the command from the words list and the raw line
  table.remove(words, 1)
  local _,_,line = string.find(line, "[^%s]+[%s]+(.*)")
  if (line == nil) then
    line = ""  -- no args
  end
  return TryAction(self.textActions, cmd, line, words, false, nil)
end


function actionHandler:RecvFromSynced(...)
  local arg1, arg2 = ...
  if (type(arg1) == 'string') then
    -- a raw sync msg
    local callInfoList = self.syncActions[arg1]
    if (callInfoList == nil) then
      return false
    end
    
    for i,callInfo in ipairs(callInfoList) do
      -- local widget = callInfo[1]
      local func = callInfo[2]
      if (func(...)) then
        return true
      end
    end
    return false
  end

  if (type(arg1) == 'number') then
    -- a proxied chat msg
    if (type(arg2) == 'string') then
      return GotChatMsg(arg2, arg1)
    end
    return false
  end
  
  return false -- unknown type
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

actionHandler.HaveSyncAction = function() return (next(self.syncActions) ~= nil) end

return actionHandler

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------