81 lines
1.9 KiB
Crystal
81 lines
1.9 KiB
Crystal
class Funfriend::ChatterContext < Funfriend::WindowContext
|
|
getter renderer : TextRenderer
|
|
getter parent : WindowContext?
|
|
property parent_relative_pos : Vec2 = Vec2.zero
|
|
getter window_size : Vec2
|
|
property timer : Float64
|
|
|
|
WINDOW_SIZE = {width: 256, height: 32}
|
|
DEFAULT_DURATION = 6.0
|
|
|
|
PADDING = 10
|
|
|
|
def initialize(text : String, position : Vec2, duration : Float64 = DEFAULT_DURATION, parent : WindowContext? = nil)
|
|
sheet = FontMan.parse_bm(File.read "assets/fonts/SpaceMono.fnt")
|
|
|
|
position_data = FontMan.position_text(text, sheet)
|
|
|
|
@window_size = Vec2.new(position_data[:width], position_data[:height]) + PADDING * 2
|
|
|
|
super(
|
|
title: "??__FUNFRIEND__?? > CHATTER",
|
|
width: window_size.x_i, height: window_size.y_i,
|
|
transparent: false
|
|
)
|
|
|
|
# just for initialization, let OpenGL know this is the current context
|
|
window.make_context_current
|
|
|
|
@timer = duration
|
|
@renderer = TextRenderer.new(text, sheet, window_size.x_i, window_size.y_i)
|
|
|
|
window.position = (position - window_size / 2).xy_i
|
|
|
|
@parent = parent
|
|
if parent
|
|
@parent_relative_pos = position - (Vec2.new(parent.window.position) + Vec2.new(parent.window.size) / 2)
|
|
end
|
|
end
|
|
|
|
def update_position
|
|
if parent
|
|
p = parent.not_nil!
|
|
if !p.closed
|
|
window.position = (Vec2.new(p.window.position) + Vec2.new(p.window.size) / 2 + parent_relative_pos - window_size / 2).xy_i
|
|
end
|
|
end
|
|
end
|
|
|
|
def render(dt : Float64)
|
|
# let OpenGL draw to it
|
|
window.make_context_current
|
|
|
|
LibGL.clear_color(0.0, 0.0, 0.0, 1.0)
|
|
LibGL.clear(Buffer::Bit::Color)
|
|
|
|
renderer.render(dt)
|
|
end
|
|
|
|
def update(dt : Float64)
|
|
@timer = @timer - dt
|
|
if @timer <= 0.0
|
|
window.should_close
|
|
end
|
|
|
|
update_position
|
|
|
|
render(dt)
|
|
|
|
window.swap_buffers
|
|
end
|
|
|
|
def bump
|
|
@parent_relative_pos.y -= window_size.y + 10
|
|
update_position
|
|
end
|
|
|
|
def clean_up
|
|
renderer.clean_up
|
|
end
|
|
end
|