-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.lua
192 lines (159 loc) · 5.23 KB
/
interface.lua
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
-- interface.lua
-- Copyright (C) 2013 Mateusz Belicki
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to
-- deal in the Software without restriction, including without limitation the
-- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-- sell copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-- IN THE SOFTWARE.
require 'class'
-- Cursor implementation:
Cursor = class()
function Cursor:init()
-- TODO: instancing this class has this purely evil side effect of hiding
-- system cursor
love.mouse.setVisible(false)
self:rebuildQuads()
end
local cursorTypes = {arrow = 0, lips = 1, eye = 2}
function Cursor:rebuildQuads()
local x = 0
local y = 0
local w = 64
local h = 32
-- source texture dimmesions:
local aW = 64
local aH = 192
self.quads = {}
for name, i in pairs(cursorTypes) do
y = 32 * i
local quad = love.graphics.newQuad(x, y, w, h, aW, aH)
self.quads[name] = quad
end
self.currentQuad = self.quads['arrow']
end
function Cursor:change(newType)
if cursorTypes[newType] ~= nil then
self.currentQuad = self.quads[newType]
end
end
function Cursor:draw(x, y, assets)
local texture = assets:getImage('assets/ui.png')
love.graphics.setColor(255, 255, 255, 255)
love.graphics.drawq(texture, self.currentQuad, x, y)
end
-- SpeechPopup implementation:
SpeechPopup = class()
function SpeechPopup:init(text, portraitSprite)
self.text = text
self.portrait = portraitSprite
self.fadePeriod = 0.25
self.fadeTimer = self.fadePeriod
self.fadeMode = "in"
self.width = 420
self.height = 58
self.bottomMargin = 40
self.padding = 8
self.bgColor = {r = 44, g = 52, b = 64, a = 220}
self.fgColor = {r = 255, g = 255, b = 255, a = 240}
end
function SpeechPopup:update(dt)
if self.fadeTimer > 0 then
self.fadeTimer = self.fadeTimer - dt
if self.fadeTimer < 0 then
self.fadeTimer = 0
end
end
end
function SpeechPopup:isFading()
return self.fadeTimer ~= 0
end
function SpeechPopup:isInvisible()
return self:getCurrentAlpha() == 0
end
function SpeechPopup:fadeOut()
self.fadeMode = "out"
self.fadeTimer = self.fadePeriod
end
function SpeechPopup:getCurrentAlpha()
if self.fadeTimer == 0
and self.fadeMode ~= "out"
then return 1 end
local t = self.fadeTimer / self.fadePeriod
if self.fadeMode == "in" then
return 1 - t
elseif self.fadeMode == "out" then
return t
else
return 0
end
end
function SpeechPopup:draw(assets)
local alpha = self:getCurrentAlpha()
local x = (assets.screen.width - self.width) / 2
local y = assets.screen.height - self.height - self.bottomMargin
-- draw background
love.graphics.setColor( self.bgColor.r , self.bgColor.g
, self.bgColor.b , self.bgColor.a * alpha
)
love.graphics.rectangle("fill", x, y, self.width, self.height)
-- draw foreground
y = y + self.padding * 2
love.graphics.setColor( self.fgColor.r , self.fgColor.g
, self.fgColor.b , self.fgColor.a * alpha
)
love.graphics.printf(self.text, x, y, self.width, "center")
if self.portrait ~= nil then
love.graphics.setColor(255, 255, 255, 255 * alpha)
self.portrait:draw(x - 148, y - 64, assets)
end
end
-- Interface implementation:
Interface = class()
function Interface:init()
self.speechPopup = nil
self.cursor = Cursor()
end
function Interface:spawnSpeechPopup(text, portrait)
self.speechPopup = SpeechPopup(text, portrait)
end
function Interface:dismissSpeechPopup()
if self.speechPopup ~= nil then
if self.speechPopup:isInvisible() then
self.speechPopup = nil
elseif false == self.speechPopup:isFading() then
self.speechPopup:fadeOut()
end
end
end
function Interface:requiresInteraction()
return self.speechPopup ~= nil
end
function Interface:update(dt, mouseInfo)
if self.speechPopup ~= nil then
self.speechPopup:update(dt)
if self.speechPopup:isInvisible() then
self.speechPopup = nil
end
end
self.mouse = mouseInfo
self.cursor:change(mouseInfo.kind)
end
function Interface:draw(assets)
if self.speechPopup ~= nil then
self.speechPopup:draw(assets)
end
self.cursor:draw(self.mouse.x, self.mouse.y, assets)
end