=begin
###############################################################################
# #
# Всплывающие сообщения #
# #
###############################################################################
Автор: Денис Кузнецов (http://vk.com/id8137201)
Версия: 1.0
Релиз от: 28.10.14
Описание:
Отображение сообщений с текстом на экране игры.
Инструкция: вызвать скрипт
add_notification(text, time = -1, icon = 0, icon_id = 0, position = 1, window_color = 0)
text - текст сообщения
time - время отображения в секундах
(если time = -1, то рассчитывается исходя из длины текста)
icon - 0 (ничего), 1 (balloon), 2 (иконка)
icon_id - индекс иконки (0 - ничего)
position - 1 (сверху), 2 (по центру), 3 (внизу)
window_color - 0 (черный), 1 (белый), 2 (зеленый), 3 (красный), 4 (синий)
=end
#### НАСТРОЙКА СКРИПТА #######################################################
module Notification_Window_Settings
# Высота окна
WINDOW_HEIGHT = 50
# Глубина окна
WINDOW_Z = 100
end
#### КОНЕЦ НАСТРОЙКИ #### Ниже не трогать ! Аррррр ###########################
class Scene_Map < Scene_Base
alias denis_kyznetsov_notific_window_scn_map_update update
def update
denis_kyznetsov_notific_window_scn_map_update
if @create_notification_window != nil
if @create_notification_window.disposing
@create_notification_window.disposing = false
@create_notification_window.dispose
@create_notification_window = nil
end
end
end
def create_notification_window(text, time, icon, icon_id, position, window_color)
@create_notification_window = Notification_Window_Class.new(text, time, icon, icon_id, position, window_color)
end
alias denis_kyznetsov_notific_window_scn_map_terminate terminate
def terminate
denis_kyznetsov_notific_window_scn_map_terminate
if @create_notification_window != nil
@create_notification_window.dispose
@create_notification_window = nil
end
end
end # class Scene_Map < Scene_Base
class Notification_Window_Class < Window_Base
attr_accessor :disposing
include Notification_Window_Settings
def initialize(text, time, icon, icon_id, position, window_color)
@text = text
@time = time
@time = @text.size/10 if @time == -1
@icon = icon
@icon_id = icon_id
@icon_id = nil if @icon == 0
@position = position
@window_color = window_color
@scroll_down = true
@draw_text = false
@text_fade_in = false
@big_text_scroll = false
@text_check = false
@time_check = 0
@scroll_up = false
@disposing = false
@tone = 40
case @position
when 1
y = -WINDOW_HEIGHT
when 2
y = Graphics.height/2 - WINDOW_HEIGHT/2
when 3
y = Graphics.height + WINDOW_HEIGHT
end
super(-2, y, @text.size > 50 ? Graphics.width + 4 : [100, 48 + @text.size * 11].max + 4, WINDOW_HEIGHT)
self.z = WINDOW_Z
self.opacity = 0 if @position == 2
modify_window
update_window_tone
self.back_opacity = @max_window_opacity
self.contents_opacity = 0
update_window_contents
case @icon # 0 - нет ничего, 1 - иконка
when 2 # балун
@balloon_id = @icon_id
start_balloon if @balloon_id != nil
#~ when 3 # персонаж
#~ start_character if @icon_id != nil
end
end
def start_character
@character_sprite = Sprite.new
@character_sprite.bitmap = Cache.character(@icon_id)
#~ case @position
#~ when 1
#~ y = 40
#~ when 2
#~ y = Graphics.height/2 - WINDOW_HEIGHT/2 + 40
#~ when 3
#~ y = Graphics.height - 40
#~ end
@character_sprite.x = 22
@character_sprite.y = y # 40
#~ @character_sprite.z = 100
@character_sprite.ox = 16
@character_sprite.oy = 32
@character_sprite.src_rect.set(32, 0, 32, 32)
@character_sprite.opacity = 0
end
def start_balloon
@balloon_sprite = Sprite.new
@balloon_sprite.bitmap = Cache.system("Balloon")
case @position
when 1
y = 8
when 2
y = Graphics.height/2 - WINDOW_HEIGHT/2 + 8
when 3
y = Graphics.height - 40
end
@balloon_sprite.y = y
@balloon_sprite.z = WINDOW_Z + 1 # 100
@balloon_sprite.opacity = 0
@balloon_sprite.ox = -4
@balloon_duration = 8 * 8 + 12
sx = balloon_frame_index * 32
sy = (@balloon_id - 1) * 32
@balloon_sprite.src_rect.set(sx, sy, 32, 32)
end
def balloon_frame_index
return 7 - [(@balloon_duration - 12) / 8, 0].max
end
def modify_window
dup_skin = self.windowskin.dup
dup_skin.clear_rect(64, 0, 64, 64)
dup_skin.clear_rect(64, 64, 32, 32)
self.windowskin = dup_skin
end
def update_window_contents
contents.clear
self.contents = Bitmap.new([100, @text.size * 12].max, contents_height)
end
def update_text
dx = 0
@text.each_line do |line|
draw_text_ex(dx + (@icon_id == nil ? 0 : 30), 0, line)
dx += text_size(line).width - 8
end
end
def update_balloon
if @balloon_duration > 0
@balloon_duration -= 1
if @balloon_duration > 0
sx = balloon_frame_index * 32
sy = (@balloon_id - 1) * 32
@balloon_sprite.src_rect.set(sx, sy, 32, 32)
else
return @balloon_duration = 8 * 8 + 12 + 64 if @time >= 4
end
end
end
def update_balloon_opacity(down = false)
@balloon_sprite.opacity += down ? -2 : 2
end
def dispose_balloon
if @balloon_sprite
@balloon_sprite.dispose
@balloon_sprite = nil
end
end
def update_window_tone(down = false)
@tone += down ? -1 : 1
@tone = 40 if @tone < 40
@tone = 212 if @tone > 212
@max_window_opacity = 148
case @window_color
when 0
self.tone.set(0, 0, 0, @tone)
@max_window_opacity = 200
when 1
self.tone.set(@tone, @tone, @tone, @tone)
when 2
self.tone.set(@tone, 0, 0, @tone)
when 3
self.tone.set(0, @tone, 0, @tone)
when 4
self.tone.set(0, 0, @tone, @tone)
end
end
def update
return if @disposing
if @scroll_down ## опускаем окно вниз
case @position
when 1
self.y += 1
if self.y > 0
self.y = 0
continue = true
end
when 2
self.opacity += 2
continue = true if self.opacity == 255
when 3
self.y -= 1
if self.y < Graphics.height - WINDOW_HEIGHT
self.y = Graphics.height - WINDOW_HEIGHT
continue = true
end
end
if continue
@scroll_down = false
@draw_text = true
continue = false
end
end
if @draw_text ## рисуем текст
draw_icon(@icon_id, 0, 0) if @icon_id != nil && @icon == 1
update_text
@draw_text = false
@text_fade_in = true
end
if @text_fade_in ## увеличение тона и непрозрачности текста
update_window_tone
update_balloon_opacity if @balloon_sprite
self.contents_opacity += 2
if self.contents_opacity >= 255
self.contents_opacity = 255
@text_fade_in = false
@big_text_scroll = true if @text.size > 50
@text_check = true if !@big_text_scroll
update_balloon if @balloon_sprite
end
end
if @big_text_scroll ## если текст большой, то прокручиваем
update_balloon if @balloon_sprite
self.ox += 1
if self.ox >= text_size(@text).width + (@icon_id == nil ? 0 : 30)
@big_text_scroll = false
@text_fade_out = true
end
end
if @text_check ## ждем нужное время
update_balloon if @balloon_sprite
if Graphics.frame_count % 60 == 0
@time_check += 1
if @time_check >= @time
@text_fade_out = true
@time_check = 0
@text_check = false
end
end
end
if @text_fade_out ## уменьшаем тон и непрозрачность текста
update_window_tone(true)
update_balloon_opacity(true) if @balloon_sprite
self.contents_opacity -= 2
if self.contents_opacity == 0
self.contents_opacity = 0
@text_fade_out = false
@scroll_up = true
end
end
if @scroll_up ## убираем окно вверх
case @position
when 1
self.y -= 1
continue = true if self.y <= -WINDOW_HEIGHT
when 2
self.opacity -= 2
continue = true if self.opacity <= 0
when 3
self.y += 1
continue = true if self.y >= Graphics.height + WINDOW_HEIGHT
end
if continue
self.y = -WINDOW_HEIGHT
@scroll_up = false
@disposing = true
dispose_balloon
end
end
end
end
class Game_Interpreter
def add_notification(text, time = -1, icon = 0, icon_id = 0, position = 1, window_color = 0)
return if !SceneManager.scene.is_a?(Scene_Map)
SceneManager.scene.create_notification_window(text, time, icon, icon_id, position, window_color)
end
end