96 lines
2.5 KiB
Crystal
96 lines
2.5 KiB
Crystal
class Funfriend::ChatterContext < Funfriend::WindowContext
|
|
getter renderer : TextRenderer
|
|
getter parent : WindowContext?
|
|
property parent_relative_pos : NamedTuple(x: Int32, y: Int32)
|
|
getter window_size : NamedTuple(width: Int32, height: Int32)
|
|
property timer : Float64
|
|
|
|
WINDOW_SIZE = {width: 256, height: 32}
|
|
DEFAULT_DURATION = 6.0
|
|
|
|
PADDING = 10
|
|
|
|
def initialize(text : String, position : NamedTuple(x: Int32, y: Int32), 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 = {
|
|
width: position_data[:width] + PADDING * 2,
|
|
height: position_data[:height] + PADDING * 2
|
|
}
|
|
|
|
super(
|
|
title: "??__FUNFRIEND__?? > CHATTER",
|
|
width: window_size[:width], height: window_size[:height],
|
|
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[:width], window_size[:height])
|
|
|
|
window.position = {
|
|
x: position[:x] - window_size[:width]//2,
|
|
y: position[:y] - window_size[:height]//2,
|
|
}
|
|
|
|
@parent = parent
|
|
if parent
|
|
@parent_relative_pos = {
|
|
x: position[:x] - (parent.window.position[:x] + parent.window.size[:width] // 2),
|
|
y: position[:y] - (parent.window.position[:y] + parent.window.size[:height] // 2),
|
|
}
|
|
else
|
|
@parent_relative_pos = {x: 0, y: 0}
|
|
end
|
|
end
|
|
|
|
def update_position
|
|
if parent
|
|
p = parent.not_nil!
|
|
window.position = {
|
|
x: p.window.position[:x] + p.window.size[:width] // 2 + parent_relative_pos[:x] - window_size[:width] // 2,
|
|
y: p.window.position[:y] + p.window.size[:height] // 2 + parent_relative_pos[:y] - window_size[:height] // 2,
|
|
}
|
|
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 = {
|
|
x: parent_relative_pos[:x],
|
|
y: parent_relative_pos[:y] - window_size[:height] - 10
|
|
}
|
|
update_position
|
|
end
|
|
|
|
def clean_up
|
|
renderer.clean_up
|
|
end
|
|
end
|