1-- This file is a part of Falling Block Game 2.
2--
3-- Copyright (C) 2010 Jared Krinke.
4--
5-- Falling Block Game 2 is free software; you can redistribute it and/or
6-- modify it under the terms of the GNU General Public License
7-- as published by the Free Software Foundation; either version 2
8-- of the License, or (at your option) any later version.
9--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13-- GNU General Public License for more details.
14--
15-- You should have received a copy of the GNU General Public License
16-- along with this program; if not, write to the Free Software
17-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19require("UI/UI.lua")
20require("UI/Input.lua") -- TODO: Should this go in UI.lua?
21require("UI/Control.lua") -- TODO: Should this go in UI.lua?
22require("Utility/ForEach.lua")
23require("Utility/SortedList.lua")
24require("Utility/List.lua")
25
26local logicalEvents = {
27    "up",
28    "down",
29    "left",
30    "right",
31    "rotateLeft",
32    "rotateRight",
33    "activate",
34    "delete",
35    "cancel",
36}
37
38local logicalEventNames = {
39    up = "Menu Up/Rotate",
40    down = "Menu Down/Drop",
41    left = "Move Left",
42    right = "Move Right",
43    rotateLeft = "Rotate Left",
44    rotateRight = "Rotate Right",
45    activate = "Activate",
46    delete = "Delete",
47    cancel = "Cancel",
48}
49
50Controls = { }
51
52function Controls.save()
53    File.createDirectory("Settings")
54
55    local file = File.openWrite("Settings/Controls.lua")
56
57    file:write("controlsData = {")
58
59    Table.forEach(Fbg2.input:getRawToLogical(), function(rawEvent, logicalEvent)
60        -- TODO: Add utility function like formatAsTableKey (converting key to key, " to ["\""], \ to ["\\"], etc.
61        if rawEvent ~= "\\" then
62            file:write("    [\"" .. rawEvent .. "\"] = \"" .. logicalEvent .. "\",")
63        else
64            file:write("    [\"\\\"] = \"" .. logicalEvent .. "\",")
65        end
66    end)
67
68    file:write("}")
69    file:close()
70end
71
72Fbg2.input:addListener(Controls.save)
73
74-- Controls menu
75ControlsMenu = { }
76
77function ControlsMenu.new()
78    local controlsForm = Form.newGrid(1, Fbg2.formX, Fbg2.formY, Fbg2.formWidth)
79
80    -- UI
81    controlsForm:add(Title.new("Controls"))
82    controlsForm:add(Separator.new())
83
84    -- Add controls
85    local i = 1
86
87    while not isNil(logicalEvents[i]) do
88        local logicalEvent = logicalEvents[i]
89
90        controlsForm:add(Control.new(Fbg2.input, logicalEvent, logicalEventNames[logicalEvent]))
91        i = i + 1
92    end
93
94    -- TODO: Focus-able area on the back button spans the entire grid and
95    -- there's no way to fix this without laying out components differently
96    -- in the same form
97    controlsForm:add(Separator.new())
98    controlsForm:add(Button.new("Back", function() Layers.pop() end))
99
100    return FormLayer.new(controlsForm)
101end
102
103