Reworked the dialouge system. Now the main.rb function can register and delete the dialouges.

This commit is contained in:
2026-06-17 10:50:14 -06:00
parent 564c600150
commit 931773fb12
4 changed files with 87 additions and 66 deletions
+40 -26
View File
@@ -3,18 +3,26 @@
TEXT_SIZE = 20
TEXT_MARGIN = 3
class DialougeBox
attr_accessor :show
def initialize(parent_x, parent_y, screen_width, screen_height, text)
# @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 = parent_x
@y = parent_y
@show = false
@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,
@@ -26,34 +34,40 @@ class DialougeBox
)
Gosu::Image.from_text(text, TEXT_SIZE, font: FONT_PATH).draw(TEXT_MARGIN, TEXT_MARGIN)
}
@timer = 0
@draw_x = 0
@draw_y = 0
end
def update(x, y, camera_zoom, newtext: nil)
if !newtext.nil?
@text = newtext
@longest_line = ((@text.split("\n").map { |ss| ss.length}.max) / 3.5).to_i
@line_count = @text.split("\n").length
@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(newtext, TEXT_SIZE, font: FONT_PATH).draw(TEXT_MARGIN, TEXT_MARGIN)
}
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
@x = x
@y = y
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(@x, @y, 0, @camera_zoom, @camera_zoom)
@image.draw(@draw_x, @draw_y, 0, @camera_zoom, @camera_zoom)
end
end
end