-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathform.rb
341 lines (293 loc) · 8.73 KB
/
form.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Copyright 2019 Victor David Santos
#
# This file is part of Super Bombinhas.
#
# Super Bombinhas is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Super Bombinhas is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Super Bombinhas. If not, see <https://www.gnu.org/licenses/>.
require_relative 'global'
include MiniGL
module FormElement
attr_reader :x, :y, :start_x, :start_y, :initialized
def init_movement
@start_x = @x
@start_y = @y
@initialized = true
end
def move_to(x, y)
@aim_x = x
@aim_y = y
end
def update_movement
if @aim_x
dist_x = @aim_x - @x
dist_y = @aim_y - @y
if dist_x.round == 0 and dist_y.round == 0
@x = @aim_x
@y = @aim_y
@aim_x = @aim_y = nil
else
set_position(@x + dist_x / 5.0, @y + dist_y / 5.0)
end
end
end
end
class MenuElement
include FormElement
def update; end
def set_position(x, y)
@x = x; @y = y
end
end
class MenuText < MenuElement
attr_reader :text_id
attr_writer :text
def initialize(text_or_id, x, y, width = 760, mode = :justified, scale = 2)
if text_or_id.is_a?(String)
@text = text_or_id
@text_id = nil
else
@text_id = text_or_id
@text = SB.text(text_or_id)
end
@x = x
@y = y
@width = width
@mode = mode
@scale = scale
end
def draw
SB.text_helper.write_breaking(@text, @x, @y, @width, @mode, 0, 255, 0, @scale, @scale)
end
end
class MenuNumber < MenuElement
attr_accessor :num
def initialize(num, x, y, mode, color = 0)
@num = num
@x = x
@y = y
@mode = mode
@color = color
end
def draw
SB.text_helper.write_line(@num.to_s, @x, @y, @mode, @color)
end
end
class MenuButton < Button
include FormElement
attr_reader :back, :text_id
def initialize(y, text_id, back = false, x = 314, fixed_text = false, &action)
text = fixed_text ? text_id : SB.text(text_id)
super(x, y, SB.font, text, :ui_button1, 0, 0x808080, 0, 0, true, true, 0, 0, 0, 0, 0, nil, 2, 2, &action)
@text_id = fixed_text ? nil : text_id
@back = back
@sound = Res.sound(back ? :btn2 : :btn1)
end
private def perform_action
super
SB.play_sound @sound
end
end
class MenuArrowButton < Button
include FormElement
def initialize(x, y, type, &action)
super(x, y, nil, nil, "ui_button#{type}", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nil, 2, 2, &action)
@sound = Res.sound :btn3
end
private def perform_action
super
SB.play_sound @sound
end
end
class MenuTextField < TextField
include FormElement
def initialize(y, x = 314)
super x: x, y: y, font: SB.font, img: :ui_textField, margin_x: 5, margin_y: 3, locale: (SB.lang == :portuguese ? 'pt-br' : 'en-us'), scale_x: 2, scale_y: 2
end
end
class MenuDropDownList < DropDownList
include FormElement
def initialize(y, options, x = 314)
super(x: x, y: y, font: SB.font, img: :ui_ddl, opt_img: :ui_ddlOpt, options: options, text_margin: 5, retro: true, scale_x: 2, scale_y: 2)
end
end
class FormSection
attr_reader :cur_btn, :changing
def initialize(components, visible = false)
@components = components
@buttons = []
@components.each do |c|
if c.is_a? Button or c.is_a? TextField
@buttons << c
@back_btn = c if c.respond_to?(:back) && c.back
end
unless c.initialized
c.init_movement
c.set_position(c.x - C::SCREEN_WIDTH, c.y) unless visible
end
end
@visible = visible
@changing = nil
@cur_btn = @buttons[@cur_btn_index = 0]
end
def update(mouse_moved)
if @changing
@components.each do |c|
if c.update_movement.nil?
@visible = false if @changing == 0
@changing = nil
end
end
elsif @visible
@components.each { |c| c.update }
@buttons.each_with_index do |b, i|
next unless b.is_a? Button
if b.state == :down || mouse_moved && b.state == :over
@cur_btn = @buttons[@cur_btn_index = i]
break
end
end
if SB.key_pressed?(:down) || SB.key_pressed?(:right) && @cur_btn.is_a?(Button)
@cur_btn_index += 1
@cur_btn_index = 0 if @cur_btn_index == @buttons.length
change_cur_btn
elsif SB.key_pressed?(:up) || SB.key_pressed?(:left) && @cur_btn.is_a?(Button)
@cur_btn_index -= 1
@cur_btn_index = @buttons.length - 1 if @cur_btn_index < 0
change_cur_btn
elsif SB.key_pressed?(:confirm)
@cur_btn.click if @cur_btn.respond_to? :click
elsif @back_btn && (SB.key_pressed?(:back) && !@cur_btn.is_a?(TextField) ||
KB.key_pressed?(Gosu::KB_ESCAPE) || KB.key_pressed?(Gosu::GP_0_BUTTON_1))
@back_btn.click
end
end
end
def change_cur_btn
@cur_btn.unfocus if @cur_btn.respond_to? :unfocus
@cur_btn = @buttons[@cur_btn_index]
@cur_btn.focus if @cur_btn.respond_to? :focus
end
def show
@visible = true
@changing = 1
@components.each do |c|
c.move_to(c.start_x, c.y)
c.focus if c.respond_to?(:focus)
end
end
def hide
@changing = 0
@components.each { |c| c.move_to(c.x - C::SCREEN_WIDTH, c.y) }
end
def clear
@components.clear
@buttons.clear
@cur_btn = nil
end
def reset
@cur_btn = @buttons[@cur_btn_index = 0]
@buttons.select { |b| b.is_a?(TextField) }.each { |t| t.text = '' }
end
def update_lang
@components.each do |c|
c.text = SB.text(c.text_id).gsub("\\n", "\n") if c.respond_to?(:text_id) && c.text_id
c.locale = (SB.lang == :portuguese ? 'pt-br' : 'en-us') if c.respond_to? :locale=
end
end
def add(component)
@components << component
if component.is_a? Button
@buttons << component
@cur_btn = @buttons[@cur_btn_index = 0] if @cur_btn.nil?
@back_btn = component if component.respond_to?(:back) && component.back
end
component.init_movement
component.set_position(component.x - C::SCREEN_WIDTH, component.y) unless @visible
end
def draw
@components.each { |c| c.draw } if @visible
end
end
class Form
attr_reader :cur_section_index
def initialize(*section_components)
@sections = [FormSection.new(section_components.shift, true)]
section_components.each do |c|
@sections << FormSection.new(c)
end
@highlight = Res.imgs(:ui_highlight, 3, 3, true)
@highlight_alpha = 102
@highlight_state = 0
@cur_section = @sections[@cur_section_index = 0]
# @cur_section.show
end
def update
mouse_moved = Mouse.x != @mouse_prev_x || Mouse.y != @mouse_prev_y
@mouse_prev_x = Mouse.x
@mouse_prev_y = Mouse.y
@sections.each { |s| s.update(mouse_moved) }
update_highlight unless @cur_section.changing
end
def update_highlight
if @highlight_state == 0
@highlight_alpha += 3
@highlight_state = 1 if @highlight_alpha == 255
else
@highlight_alpha -= 3
@highlight_state = 0 if @highlight_alpha == 102
end
end
def go_to_section(index)
@cur_section.hide
@cur_section = @sections[@cur_section_index = index]
@cur_section.reset
@cur_section.show
end
def section(index)
@sections[index]
end
def reset
@sections.each { |s| s.reset }
go_to_section 0
end
def update_lang
@sections.each { |s| s.update_lang }
end
def draw
@sections.each { |s| s.draw }
draw_highlight unless @cur_section.changing
end
def draw_highlight
btn = @cur_section.cur_btn
x = btn.x; y = btn.y; w = btn.w; h = btn.h
color1 = (@highlight_alpha << 24) | 0xffffff
color2 = (((@highlight_alpha * 0.5).round) << 24) | 0xffffff
draw_sliced(@highlight, x - 2, y - 2, w, h, color1)
draw_sliced(@highlight, x - 4, y - 4, w + 4, h + 4, color2)
end
def draw_sliced(img, x, y, w, h, color)
h_b = 2 * img[0].width
h_scale = w.to_f / img[0].width
v_b = 2 * img[0].height
v_scale = h.to_f / img[0].height
img[0].draw(x, y, 0, 2, 2, color)
img[1].draw(x + h_b, y, 0, h_scale, 2, color)
img[2].draw(x + h_b + w, y, 0, 2, 2, color)
img[3].draw(x, y + v_b, 0, 2, v_scale, color)
# img[4].draw(x + h_b, y + v_b, 0, h_scale, v_scale, color)
img[5].draw(x + h_b + w, y + v_b, 0, 2, v_scale, color)
img[6].draw(x, y + v_b + h, 0, 2, 2, color)
img[7].draw(x + h_b, y + v_b + h, 0, h_scale, 2, color)
img[8].draw(x + h_b + w, y + v_b + h, 0, 2, 2, color)
end
end