Files
number_factory/dialouge_box.rb

74 lines
2.1 KiB
Ruby

# Nathan Hinotn
# Creates a simple dialouge box that can be spawned from any location
TEXT_SIZE = 20
TEXT_MARGIN = 3
class DialougeBox
# @param draw_x [Integer]
# @param draw_y [Integer]
# @param size_x [Integer] The width that this should open at (in pixels at zero zoom)
# @param size_y [Integer] The height that this should open at (in pixels at zero zoom)
def initialize(x, y, size_x, size_y, screen_width, screen_height, text, box_size, hover_time)
@text = text
@longest_line = ((@text.split("\n").map { |ss| ss.length}.max) / 2.25).to_i
@line_count = @text.split("\n").length
@x = x
@y = y
@size_x = size_x
@size_y = size_y
@screen_width = screen_width
@screen_height = screen_height
@box_size = box_size
@hover_time = hover_time
@show = false
@image = Gosu.render(((TEXT_MARGIN * 2) + TEXT_SIZE) * @longest_line, (TEXT_SIZE + TEXT_MARGIN) * @line_count) {
Gosu.draw_rect(
0,
0,
(((TEXT_MARGIN * 2) + TEXT_SIZE) * @longest_line) + @x,
((TEXT_SIZE + TEXT_MARGIN) * @line_count) + @y,
Gosu::Color.new(0xC0, 0xFF, 0xFF, 0xFF),
0,
)
Gosu::Image.from_text(text, TEXT_SIZE, font: FONT_PATH).draw(TEXT_MARGIN, TEXT_MARGIN)
}
@timer = 0
@draw_x = 0
@draw_y = 0
end
# Used to move the dialouge box around
# @param x [Integer] The x position to move to.
# @param y [Integer] The y position to move to.
def move(x, y)
@draw_x = x
@draw_y = y
end
# Update called every delta time
def update(delta_mult, camera_zoom, mouse_x, mouse_y)
@camera_zoom = camera_zoom
if 0 < mouse_x - @draw_x && mouse_x - @draw_x < (@size_x * @camera_zoom) && 0 < mouse_y - @draw_y && mouse_y - @draw_y < (@size_y * @camera_zoom)
@timer += 60.0 * delta_mult
if @timer > @hover_time
@show = true
end
else
@timer = 0
@show = false
end
end
## Draw the dialouge if it is supposed to be shown.
def draw()
if @show
@image.draw(@draw_x, @draw_y, 0, @camera_zoom, @camera_zoom)
end
end
end