1-- Copyright (C) 2012 Jared Krinke.
2--
3-- This is free software; you can redistribute it and/or
4-- modify it under the terms of the GNU General Public License
5-- as published by the Free Software Foundation; either version 2
6-- of the License, or (at your option) any later version.
7--
8-- This program is distributed in the hope that it will be useful,
9-- but WITHOUT ANY WARRANTY; without even the implied warranty of
10-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11-- GNU General Public License for more details.
12--
13-- You should have received a copy of the GNU General Public License
14-- along with this program; if not, write to the Free Software
15-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17require("UI/Form.lua")
18
19local cursorSize = 24
20
21Cursor = { }
22
23function Cursor.new()
24    local cursor = Entity.new(0, 0, 1, cursorSize, cursorSize)
25
26    cursor.elements:add(Element.Image.new("Images/Cursor.png", 0.5, -0.5, 0, 1, 1))
27
28    function cursor.setPosition(self, x, y)
29        self.x = x
30        self.y = y
31    end
32
33    return cursor
34end
35
36FormLayer = { }
37
38function FormLayer.new(form, period, showCursor)
39    -- Allow period to be overridden
40    local effectivePeriod = -1
41
42    if isNumber(period) then
43        effectivePeriod = period
44    end
45
46    local formLayer = Layer.new(effectivePeriod)
47
48    formLayer.form = form
49    formLayer.showCursor = true
50
51    if isBoolean(showCursor) then
52        formLayer.showCursor = showCursor
53    end
54
55    -- Create a cusor, if necessary
56    if formLayer.showCursor then
57        formLayer.cursor = formLayer:addChild(Cursor.new())
58    end
59
60    function formLayer.keyPressed(self, key, pressed, character)
61        return self.form:keyPressed(key, pressed, character)
62    end
63
64    function formLayer.mouseMoved(self, x, y)
65        if self.showCursor then
66            self.cursor:setPosition(x, y)
67        end
68
69        return self.form:mouseMoved(x, y)
70    end
71
72    function formLayer.inputReceived(self, event, pressed)
73        local handled = false
74
75        -- TODO: This shouldn't necessarily do this... instead propagate a Boolean value indicating if the event was used or not
76        if event == "cancel" and pressed then
77            Layers.pop()
78            handled = true
79
80            local silent = self.silent
81        else
82            handled = self.form:inputReceived(event, pressed)
83        end
84
85        return handled
86    end
87
88    -- TODO: Is this a good idea?
89    function formLayer.errorOccurred(self, message)
90        print(message)
91
92        if not isNil(console) then
93            console:open()
94        end
95    end
96
97    function formLayer.show(self)
98        Layers.push(self)
99    end
100
101    function formLayer.shown(self)
102        -- Adjust cursor, if necessary
103        if self.showCursor then
104            self.cursor:setPosition(Mouse.getPosition())
105
106            -- Make sure focus changes too
107            self:mouseMoved(Mouse.getPosition())
108        end
109
110        -- Notify that the form has been shown
111        if isFunction(self.formShown) then
112            self:formShown()
113        end
114    end
115
116    if not isNil(FormLayer.defaultInput) then
117        FormLayer.defaultInput:setupEventHandlers(formLayer)
118    end
119
120    form:setLayer(formLayer)
121    form:focused()
122
123    return formLayer
124end
125
126function FormLayer.setDefaultInput(input)
127    FormLayer.defaultInput = input
128end
129
130