summaryrefslogtreecommitdiff
path: root/gui.lua
blob: 9f73101a4f1581509d7dedb43944c0a0fb552a0a (plain)
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
--
-- Agario Checkers - Checkers-like game with inspiration from agar.io
-- Copyright (C) 2015-2018 Delwink, LLC
--
-- Redistributions, modified or unmodified, in whole or in part, must retain
-- applicable copyright or other legal privilege notices, these conditions, and
-- the following license terms and disclaimer.  Subject to these conditions,
-- the holder(s) of copyright or other legal privileges, author(s) or
-- assembler(s), and contributors of this work hereby grant to any person who
-- obtains a copy of this work in any form:
--
-- 1. Permission to reproduce, modify, distribute, publish, sell, sublicense,
-- use, and/or otherwise deal in the licensed material without restriction.
--
-- 2. A perpetual, worldwide, non-exclusive, royalty-free, irrevocable patent
-- license to reproduce, modify, distribute, publish, sell, use, and/or
-- otherwise deal in the licensed material without restriction, for any and all
-- patents:
--
--     a. Held by each such holder of copyright or other legal privilege,
--     author or assembler, or contributor, necessarily infringed by the
--     contributions alone or by combination with the work, of that privilege
--     holder, author or assembler, or contributor.
--
--     b. Necessarily infringed by the work at the time that holder of
--     copyright or other privilege, author or assembler, or contributor made
--     any contribution to the work.
--
-- NO WARRANTY OF ANY KIND IS IMPLIED BY, OR SHOULD BE INFERRED FROM, THIS
-- LICENSE OR THE ACT OF DISTRIBUTION UNDER THE TERMS OF THIS LICENSE,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
-- A PARTICULAR PURPOSE, AND NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS,
-- ASSEMBLERS, OR HOLDERS OF COPYRIGHT OR OTHER LEGAL PRIVILEGE BE LIABLE FOR
-- ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN ACTION OF CONTRACT, TORT,
-- OR OTHERWISE ARISING FROM, OUT OF, OR IN CONNECTION WITH THE WORK OR THE USE
-- OF OR OTHER DEALINGS IN THE WORK.
--

require 'class'

local defaultfont = love.graphics.getFont()

Gui = class()

function Gui:__init()
   self._components = {}
   self._active = nil
end

function Gui:addcomponent(component)
   table.insert(self._components, component)
   component.gui = self
end

function Gui:mousepressed(x, y, button)
   for _,component in ipairs(self._components) do
      if component:mousepressed(x, y, button) then
         return true
      end
   end

   return false
end

function Gui:mousereleased(x, y, button)
   local ret = false

   for _,component in ipairs(self._components) do
      if component:mousereleased(x, y, button) then
         ret = true
      end
   end

   return ret
end

function Gui:textinput(c)
   if self._active and self._active.usereditable then
      self._active:textinput(c)
      return true
   end

   return false
end

function Gui:keypressed(key, isrepeat)
   for _,component in ipairs(self._components) do
      if component:keypressed(key, isrepeat) then
         return true
      end
   end

   return false
end

function Gui:draw()
   for _,component in ipairs(self._components) do
      component:draw()
   end
end

local function normalizedim(d)
   if type(d) ~= 'function' then
      return function(self) return d end
   end

   return d
end

GuiComponent = class()

function GuiComponent:__init(x, y, w, h, bg, fg, state)
   self.x = normalizedim(x)
   self.y = normalizedim(y)
   self.w = normalizedim(w)
   self.h = normalizedim(h)

   self.bg = bg
   self.fg = fg
   self.state = state

   self.clicked = false
   self.clicklisteners = {}
   self.text = ''
   self.usereditable = false
   self.visible = false
end

function GuiComponent:draw()
   if not self.visible then
      return false
   end

   return true
end

function GuiComponent:addclicklistener(listener)
   table.insert(self.clicklisteners, listener)
end

function GuiComponent:_triggerclicklisteners()
   for _,listener in ipairs(self.clicklisteners) do
      listener()
   end
end

function GuiComponent:mousepressed()
   self.clicked = true
end

function GuiComponent:mousereleased(x, y)
   if self.clicked then
      self.clicked = false

      if self:contains(x, y) then
         self:_triggerclicklisteners()
      end
   end
end

function GuiComponent:keypressed(key, isrepeat)

end

function GuiComponent:contains(x, y)
   return (x >= self:x() and x <= (self:x() + self:w())
	      and y >= self:y() and y <= (self:y() + self:h()))
end

function GuiComponent:setvisible(b)
   self.visible = b
end

function GuiComponent:isvisible()
   return self.visible
end

function GuiComponent:settext(text)
   self.text = text
end

function GuiComponent:textinput(c)

end

function GuiComponent:_halfheight()
   return self:h() / 2
end

Button = class(GuiComponent)

function Button:draw()
   if not self._base.draw(self) then
      return false
   end

   local bg = {self.bg[1], self.bg[2], self.bg[3]}
   local mx, my = love.mouse.getPosition()
   if self.clicked and self:contains(mx, my) then
      for i,val in ipairs(bg) do
	 bg[i] = bg[i] - 50
      end
   end

   love.graphics.setColor(bg)
   love.graphics.rectangle('fill', self:x(), self:y(), self:w(), self:h())

   love.graphics.setColor(self.fg)
   love.graphics.rectangle('line', self:x(), self:y(), self:w(), self:h())

   if not self._lasth or self:h() ~= self._lasth then
      self._font = love.graphics.newFont(self:h() * 0.8)
      self._lasth = self:h()
   end

   love.graphics.setFont(self._font)
   love.graphics.printf(self.text, self:x(),
			(self:y() + (self:h() / 2)) - self:_halfheight(),
			self:w(), 'center')
   love.graphics.setFont(defaultfont)

   return true
end

TextField = class(GuiComponent)

function TextField:__init(x, y, w, h, bg, fg, state)
   self._base.__init(self, x, y, w, h, bg, fg, state)
   self.texteditable = true
   self._placeholdertext = ''

   self:addclicklistener(function()
         self.state._activecomponent = self
         love.keyboard.setTextInput(true)
   end)
end