-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.rb
241 lines (216 loc) · 4.75 KB
/
components.rb
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
module TileDrawable
def draw
@tile ||= @game.tiles[@name.hash]
@tile_frame = @game.time / 300 % @tile.frames
offset_x = (@game.width / 2) - (@game.map.width * (@tile.width / 2))
offset_y = (@game.height / 2) - ((@tile.height-8) / 2)
screen_x = (@y * @tile.width / 2) + (@x * @tile.width / 2) + offset_x
screen_y = (@x * (@tile.height-8) / 2) - (@y * (@tile.height-8) / 2) + offset_y
@tile.draw(screen_x, screen_y, @version, @tile_frame)
end
end
module Clickable
def update
@hovering ||= false
@clicked ||= false
@left_was_down_inside ||= false
if @clicked
@clicked = false
end
if mouse_within?
@hovering = true
if not @left_was_down_inside
@left_was_down_inside = left_mouse_down?
end
if @left_was_down_inside && left_mouse_up?
@left_was_down_inside = false
@clicked = true
end
else
@hovering = false
@left_was_down_inside = false
end
end
end
module TextDrawable
def draw
@game.fonts[@font].draw(@text, @x, @y, 1)
end
end
# TODO: rather than limit characters by width of box,
# limit based on a max-characters attribute and
# only draw what can be drawn
#
# rate limit!
module TextEditable
KEY_DELAY = 10 # frames a key can be held down before counting as two
def text_width(text)
@game.fonts[@font].text_width(text)
end
def update
@key_delay ||= KEY_DELAY
@height = @game.fonts[@font].height
if left_mouse_down?
if mouse_within?
@focused = true
rel_x = (mouse_x - @x).to_i
@caret_pos = 0
if @text.length > 1
while text_width(@text[0..@caret_pos]) < rel_x
if @caret_pos > @text.length
@caret_pos = @text.length
break
end
@caret_pos += 1
end
end
else
@focused = false
end
end
if key_down? && @focused
key = key_down
char = char_entered
if char.nil?
case key
when Gosu::KbBackspace
if @caret_pos > 0
@text.slice! @caret_pos-1
@caret_pos -= 1
end
when Gosu::KbLeft
if @caret_pos > 0
@caret_pos -= 1
end
when Gosu::KbRight
if @caret_pos < @text.length
@caret_pos += 1
end
end
else
if text_width(@text) + text_width(char) <= @width
@text.insert(@caret_pos, char)
@caret_pos += 1
end
end
end
end
def draw
@game.fonts[@font].draw(@text, @x, @y, 1, 1, 1, @fgcolor)
caret_x = @x + text_width(@text[0,@caret_pos])
if @focused && @game.time % 1000 < 500
@caret.draw(caret_x, @y, 1)
end
end
end
module Collidable
end
module MouseListener
def mouse_x
@game.mouse_x
end
def mouse_y
@game.mouse_y
end
def left_mouse_down?
@game.button_down? Gosu::MsLeft
end
def left_mouse_up?
!@game.keys_down.include? Gosu::MsLeft
end
def right_mouse_down?
@game.button_down? Gosu::MsRight
end
def right_mouse_up?
!@game.keys_down.include? Gosu::MsRight
end
end
class String
def shift
str = ""
each_char do |char|
c = case char
when "1"
"!"
when "2"
"@"
when "3"
"#"
when "4"
"$"
when "5"
"%"
when "6"
"^"
when "7"
"&"
when "8"
"*"
when "9"
"("
when "0"
")"
when "`"
"~"
when "-"
"_"
when "="
"+"
when "["
"{"
when "]"
"}"
when "\\"
"|"
when ";"
":"
when "'"
"\""
when ","
"<"
when "."
">"
when "/"
"?"
else
char.upcase
end
str += c
end
str
end
end
# TODO: key combos (ctrl-c, shift-1)
module KeyListener
def key_down
@game.keys_down[0]
end
def char_entered
key = key_down
char = @game.button_id_to_char(key)
if char.nil?
if key == Gosu::KbLeftShift && @game.keys_down.length > 1
char = @game.button_id_to_char(@game.keys_down[1])
if char.nil?
return nil
end
return char.shift
end
return nil
end
char
end
def key_up?(key)
!@game.keys_down.include? key
end
def key_down?
key = key_down
if !key.nil?
down_time = @game.key_down_times[key]
if down_time % @key_delay <= 1 || down_time <= 1
return true
end
end
false
end
end