1194 lines
39 KiB
Ruby
1194 lines
39 KiB
Ruby
# encoding: UTF-8
|
|
|
|
begin
|
|
require "json" unless defined?(JSON)
|
|
rescue LoadError, ScriptError
|
|
end
|
|
|
|
module MPNickStyle
|
|
JSON_PATH = "ModScripts/nick_styles.json"
|
|
|
|
DEFAULT_STYLE = {
|
|
"id" => 0,
|
|
"code" => "classic",
|
|
"name" => "Classic",
|
|
"font_size" => 16,
|
|
"bold" => false,
|
|
"shadow" => true,
|
|
"text_color" => [255, 255, 255, 255]
|
|
}
|
|
|
|
RAINBOW_STYLE = {
|
|
"id" => 1,
|
|
"code" => "js_rainbow_black_outline",
|
|
"name" => "Rainbow JS",
|
|
"font_size" => 16,
|
|
"bold" => false,
|
|
"shadow" => false,
|
|
"outline_size" => 1,
|
|
"outline_color" => [0, 0, 0, 255],
|
|
"frame_step" => 4,
|
|
"speed" => 1.0
|
|
}
|
|
|
|
MUSIC_STYLE = {
|
|
"id" => 2,
|
|
"code" => "js_music_equalizer_black_outline",
|
|
"name" => "Musik",
|
|
"font_size" => 16,
|
|
"bold" => false,
|
|
"shadow" => false,
|
|
"text_color" => [255, 255, 255, 255],
|
|
"outline_size" => 1,
|
|
"outline_color" => [0, 0, 0, 255],
|
|
"frame_step" => 4,
|
|
"speed" => 1.0,
|
|
"bars" => 5,
|
|
"rows" => 7,
|
|
"bar_alpha" => 235,
|
|
"cell_gap" => 2
|
|
}
|
|
|
|
BLOOD_STYLE = {
|
|
"id" => 3,
|
|
"code" => "js_blood_red_black_outline",
|
|
"name" => "BLOOD",
|
|
"font_size" => 16,
|
|
"bold" => false,
|
|
"shadow" => false,
|
|
"text_color" => [220, 16, 20, 255],
|
|
"outline_size" => 1,
|
|
"outline_color" => [0, 0, 0, 255],
|
|
"frame_step" => 4,
|
|
"speed" => 1.0,
|
|
"drip_height" => 12,
|
|
"drip_count" => 6,
|
|
"drip_min" => 2,
|
|
"drip_max" => 10,
|
|
"drip_alpha" => 245
|
|
}
|
|
|
|
CONCHA_STYLE = {
|
|
"id" => 4,
|
|
"code" => "js_concha_flash_splat",
|
|
"name" => "CONCHA",
|
|
"font_size" => 16,
|
|
"bold" => false,
|
|
"shadow" => false,
|
|
"text_color" => [255, 245, 170, 255],
|
|
"flash_text_color" => [255, 255, 255, 255],
|
|
"outline_size" => 1,
|
|
"outline_color" => [0, 0, 0, 255],
|
|
"frame_step" => 4,
|
|
"speed" => 1.0,
|
|
"splat_alpha" => 210,
|
|
"splat_count" => 6,
|
|
"splat_min_radius" => 7,
|
|
"splat_max_radius" => 12,
|
|
"flash_window" => 2
|
|
}
|
|
|
|
NEON_STYLE = {
|
|
"id" => 5,
|
|
"code" => "js_neon_black_rainbow_outline",
|
|
"name" => "NEON",
|
|
"font_size" => 16,
|
|
"bold" => false,
|
|
"shadow" => false,
|
|
"text_color" => [0, 0, 0, 255],
|
|
"outline_size" => 1,
|
|
"outline_color" => [255, 0, 0, 255],
|
|
"frame_step" => 4,
|
|
"speed" => 1.0,
|
|
"neon_hold" => 1
|
|
}
|
|
|
|
|
|
ROMASHKA_STYLE = {
|
|
"id" => 6,
|
|
"code" => "js_romashka_white_yellow_outline",
|
|
"name" => "ROMASHKA",
|
|
"font_size" => 16,
|
|
"bold" => false,
|
|
"shadow" => false,
|
|
"text_color" => [255, 255, 255, 255],
|
|
"outline_size" => 1,
|
|
"outline_color" => [248, 210, 64, 255],
|
|
"frame_step" => 4,
|
|
"speed" => 1.0,
|
|
"flower_alpha" => 190,
|
|
"flower_count" => 2,
|
|
"petal_count" => 10,
|
|
"petal_radius" => 3,
|
|
"petal_distance" => 6,
|
|
"center_radius" => 3,
|
|
"orbit_distance" => 6,
|
|
"anchor_gap" => 8,
|
|
"rotation_speed" => 1.0
|
|
}
|
|
|
|
DEFAULT_CONFIG = {
|
|
"default_style_id" => 0,
|
|
"styles" => {
|
|
"0" => DEFAULT_STYLE,
|
|
"1" => RAINBOW_STYLE,
|
|
"2" => MUSIC_STYLE,
|
|
"3" => BLOOD_STYLE,
|
|
"4" => CONCHA_STYLE,
|
|
"5" => NEON_STYLE,
|
|
"6" => ROMASHKA_STYLE
|
|
}
|
|
}
|
|
|
|
def self.bool_value(value, fallback = false)
|
|
return value if value == true || value == false
|
|
return fallback if value.nil?
|
|
case value.to_s.strip.downcase
|
|
when "1", "true", "yes", "on" then true
|
|
when "0", "false", "no", "off" then false
|
|
else
|
|
fallback
|
|
end
|
|
rescue
|
|
fallback
|
|
end
|
|
|
|
def self.int_value(value, fallback = 0, min_value = nil)
|
|
out = begin
|
|
Integer(value)
|
|
rescue
|
|
fallback
|
|
end
|
|
out = min_value if !min_value.nil? && out < min_value
|
|
out
|
|
rescue
|
|
fallback
|
|
end
|
|
|
|
def self.color_array(value, fallback)
|
|
arr = value.is_a?(Array) ? value : fallback
|
|
arr = fallback if arr.nil? || arr.empty?
|
|
out = []
|
|
4.times do |idx|
|
|
base = fallback[idx] || 255
|
|
out << [[int_value(arr[idx], base), 0].max, 255].min
|
|
end
|
|
out
|
|
rescue
|
|
fallback
|
|
end
|
|
|
|
def self.default_style_for(id_value, raw_style = nil)
|
|
raw = raw_style.is_a?(Hash) ? raw_style : {}
|
|
code = raw["code"].to_s
|
|
return ROMASHKA_STYLE if id_value.to_i == 6 || code == ROMASHKA_STYLE["code"]
|
|
return NEON_STYLE if id_value.to_i == 5 || code == NEON_STYLE["code"]
|
|
return CONCHA_STYLE if id_value.to_i == 4 || code == CONCHA_STYLE["code"]
|
|
return BLOOD_STYLE if id_value.to_i == 3 || code == BLOOD_STYLE["code"]
|
|
return MUSIC_STYLE if id_value.to_i == 2 || code == MUSIC_STYLE["code"]
|
|
return RAINBOW_STYLE if id_value.to_i == 1 || code == RAINBOW_STYLE["code"]
|
|
DEFAULT_STYLE
|
|
rescue
|
|
DEFAULT_STYLE
|
|
end
|
|
|
|
def self.normalize_style(id_value, raw_style)
|
|
raw = raw_style.is_a?(Hash) ? raw_style : {}
|
|
base = default_style_for(id_value, raw)
|
|
{
|
|
"id" => int_value(raw["id"], id_value.to_i, 0),
|
|
"code" => (raw["code"] || base["code"]).to_s,
|
|
"name" => (raw["name"] || base["name"]).to_s,
|
|
"font_size" => int_value(raw["font_size"], base["font_size"], 1),
|
|
"bold" => bool_value(raw["bold"], base["bold"]),
|
|
"shadow" => bool_value(raw["shadow"], base["shadow"]),
|
|
"text_color" => color_array(raw["text_color"], base["text_color"] || [255, 255, 255, 255]),
|
|
"outline_size" => int_value(raw["outline_size"], base["outline_size"] || 0, 0),
|
|
"outline_color" => color_array(raw["outline_color"], base["outline_color"] || [0, 0, 0, 255]),
|
|
"frame_step" => int_value(raw["frame_step"], base["frame_step"] || 4, 1),
|
|
"bars" => int_value(raw["bars"], base["bars"] || 5, 1),
|
|
"rows" => int_value(raw["rows"], base["rows"] || 7, 2),
|
|
"bar_alpha" => int_value(raw["bar_alpha"], base["bar_alpha"] || 235, 0),
|
|
"cell_gap" => int_value(raw["cell_gap"], base["cell_gap"] || 2, 0),
|
|
"drip_height" => int_value(raw["drip_height"], base["drip_height"] || 12, 1),
|
|
"drip_count" => int_value(raw["drip_count"], base["drip_count"] || 6, 1),
|
|
"drip_min" => int_value(raw["drip_min"], base["drip_min"] || 2, 1),
|
|
"drip_max" => int_value(raw["drip_max"], base["drip_max"] || 10, 1),
|
|
"drip_alpha" => int_value(raw["drip_alpha"], base["drip_alpha"] || 245, 0),
|
|
"flash_text_color" => color_array(raw["flash_text_color"], base["flash_text_color"] || base["text_color"] || [255, 255, 255, 255]),
|
|
"splat_alpha" => int_value(raw["splat_alpha"], base["splat_alpha"] || 215, 0),
|
|
"splat_count" => int_value(raw["splat_count"], base["splat_count"] || 3, 1),
|
|
"splat_min_radius" => int_value(raw["splat_min_radius"], base["splat_min_radius"] || 7, 2),
|
|
"splat_max_radius" => int_value(raw["splat_max_radius"], base["splat_max_radius"] || 12, 3),
|
|
"flash_window" => int_value(raw["flash_window"], base["flash_window"] || 2, 1),
|
|
"neon_hold" => int_value(raw["neon_hold"], base["neon_hold"] || 1, 1),
|
|
"flower_alpha" => int_value(raw["flower_alpha"], base["flower_alpha"] || 210, 0),
|
|
"flower_count" => int_value(raw["flower_count"], base["flower_count"] || 2, 1),
|
|
"petal_count" => int_value(raw["petal_count"], base["petal_count"] || 12, 6),
|
|
"petal_radius" => int_value(raw["petal_radius"], base["petal_radius"] || 5, 2),
|
|
"petal_distance" => int_value(raw["petal_distance"], base["petal_distance"] || 10, 4),
|
|
"center_radius" => int_value(raw["center_radius"], base["center_radius"] || 5, 2),
|
|
"orbit_distance" => int_value(raw["orbit_distance"], base["orbit_distance"] || 18, 4),
|
|
"rotation_speed" => begin
|
|
Float(raw["rotation_speed"] || base["rotation_speed"] || 1.0)
|
|
rescue
|
|
1.0
|
|
end,
|
|
"speed" => begin
|
|
Float(raw["speed"] || base["speed"] || 1.0)
|
|
rescue
|
|
1.0
|
|
end
|
|
}
|
|
rescue
|
|
base
|
|
end
|
|
|
|
def self.load_config
|
|
mtime = begin
|
|
File.exist?(JSON_PATH) ? File.mtime(JSON_PATH).to_f : -1.0
|
|
rescue
|
|
-1.0
|
|
end
|
|
return @config if @config && @config_mtime == mtime
|
|
|
|
data = {}
|
|
if File.exist?(JSON_PATH)
|
|
begin
|
|
raw = Ruby310Compat.read_text_file(JSON_PATH)
|
|
raw.force_encoding("UTF-8") if raw.respond_to?(:force_encoding)
|
|
data = JSON.parse(raw) if defined?(JSON)
|
|
rescue
|
|
data = {}
|
|
end
|
|
end
|
|
|
|
cfg = DEFAULT_CONFIG.dup
|
|
cfg["default_style_id"] = int_value((data || {})["default_style_id"], DEFAULT_CONFIG["default_style_id"], 0)
|
|
cfg["styles"] = {}
|
|
|
|
merged_styles = {}
|
|
DEFAULT_CONFIG["styles"].each { |k, v| merged_styles[k.to_s] = (v || {}).dup }
|
|
if data.is_a?(Hash) && data["styles"].is_a?(Hash)
|
|
data["styles"].each do |sid, style_hash|
|
|
key = sid.to_s
|
|
merged_styles[key] = (merged_styles[key] || {}).merge(style_hash.is_a?(Hash) ? style_hash : {})
|
|
end
|
|
end
|
|
|
|
merged_styles.each do |sid, style_hash|
|
|
cfg["styles"][sid.to_s] = normalize_style(sid.to_i, style_hash)
|
|
end
|
|
|
|
@config = cfg
|
|
@config_mtime = mtime
|
|
@config
|
|
rescue
|
|
@config = DEFAULT_CONFIG
|
|
@config_mtime = mtime
|
|
@config
|
|
end
|
|
|
|
def self.default_style_id
|
|
int_value((load_config || {})["default_style_id"], 0, 0)
|
|
rescue
|
|
0
|
|
end
|
|
|
|
def self.style(style_id)
|
|
cfg = load_config || DEFAULT_CONFIG
|
|
styles = cfg["styles"].is_a?(Hash) ? cfg["styles"] : {}
|
|
sid = int_value(style_id, default_style_id, 0)
|
|
styles[sid.to_s] || styles[default_style_id.to_s] || DEFAULT_STYLE
|
|
rescue
|
|
DEFAULT_STYLE
|
|
end
|
|
|
|
def self.animated_style?(style_id)
|
|
code = style(style_id)["code"].to_s
|
|
code == "js_rainbow_black_outline" || code == "js_music_equalizer_black_outline" || code == "js_blood_red_black_outline" || code == "js_concha_flash_splat" || code == "js_neon_black_rainbow_outline" || code == "js_romashka_white_yellow_outline"
|
|
rescue
|
|
false
|
|
end
|
|
|
|
def self.render_tick(style_id, phase_seed = 0)
|
|
return 0 unless animated_style?(style_id)
|
|
cfg = style(style_id)
|
|
step = int_value(cfg["frame_step"], 4, 1)
|
|
((Graphics.frame_count rescue 0) / step) + int_value(phase_seed, 0, 0)
|
|
rescue
|
|
0
|
|
end
|
|
|
|
def self.color_from_array(arr)
|
|
values = color_array(arr, [255, 255, 255, 255])
|
|
Color.new(values[0], values[1], values[2], values[3])
|
|
rescue
|
|
Color.new(255, 255, 255, 255)
|
|
end
|
|
|
|
def self.hsv_to_rgb(h, s, v)
|
|
h = h.to_f % 360.0
|
|
s = [[s.to_f, 0.0].max, 1.0].min
|
|
v = [[v.to_f, 0.0].max, 1.0].min
|
|
c = v * s
|
|
x = c * (1.0 - (((h / 60.0) % 2.0) - 1.0).abs)
|
|
m = v - c
|
|
r1, g1, b1 =
|
|
if h < 60
|
|
[c, x, 0]
|
|
elsif h < 120
|
|
[x, c, 0]
|
|
elsif h < 180
|
|
[0, c, x]
|
|
elsif h < 240
|
|
[0, x, c]
|
|
elsif h < 300
|
|
[x, 0, c]
|
|
else
|
|
[c, 0, x]
|
|
end
|
|
[
|
|
((r1 + m) * 255).to_i,
|
|
((g1 + m) * 255).to_i,
|
|
((b1 + m) * 255).to_i
|
|
]
|
|
rescue
|
|
[255, 255, 255]
|
|
end
|
|
|
|
def self.animated_text_color(style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
speed = begin
|
|
Float(cfg["speed"] || 1.0)
|
|
rescue
|
|
1.0
|
|
end
|
|
hue = (((Graphics.frame_count rescue 0) + int_value(phase_seed, 0, 0) * 11) * speed * 4.5) % 360.0
|
|
rgb = hsv_to_rgb(hue, 1.0, 1.0)
|
|
Color.new(rgb[0], rgb[1], rgb[2], 255)
|
|
rescue
|
|
Color.new(255, 255, 255, 255)
|
|
end
|
|
|
|
def self.apply_font(bitmap, font_name, style_id, fallback_size = 16)
|
|
cfg = style(style_id)
|
|
bitmap.font.name = font_name
|
|
bitmap.font.size = int_value(cfg["font_size"], fallback_size, 1)
|
|
bitmap.font.bold = bool_value(cfg["bold"], false)
|
|
bitmap.font.outline = false
|
|
bitmap.font.shadow = false
|
|
rescue
|
|
end
|
|
|
|
def self.measure(text, font_name, style_id, fallback_size = 16)
|
|
text = text.to_s
|
|
temp = Bitmap.new(1, 1)
|
|
apply_font(temp, font_name, style_id, fallback_size)
|
|
base = temp.text_size(text)
|
|
cfg = style(style_id)
|
|
outline_size = int_value(cfg["outline_size"], 0, 0)
|
|
code = cfg["code"].to_s
|
|
pad = 8 + outline_size * 4
|
|
pad += 8 if code == MUSIC_STYLE["code"]
|
|
width = [base.width + pad, 8].max
|
|
height = [base.height + outline_size * 4, temp.font.size + 6].max
|
|
if code == BLOOD_STYLE["code"]
|
|
height += int_value(cfg["drip_height"], 12, 1) + outline_size + 2
|
|
elsif code == CONCHA_STYLE["code"]
|
|
width += 18
|
|
height += 8
|
|
elsif code == ROMASHKA_STYLE["code"]
|
|
flower_extent = int_value(cfg["center_radius"], 3, 1) + int_value(cfg["petal_distance"], 6, 1) + int_value(cfg["petal_radius"], 3, 1) + int_value(cfg["orbit_distance"], 6, 0)
|
|
width += [[flower_extent / 2, 6].max, 12].min
|
|
height += [[flower_extent / 3, 4].max, 8].min
|
|
end
|
|
temp.dispose
|
|
[width, height]
|
|
rescue
|
|
[text.to_s.length * fallback_size, fallback_size + 6]
|
|
end
|
|
|
|
def self.music_equalizer_heights(style_id, columns, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
rows = int_value(cfg["rows"], 7, 2)
|
|
speed = begin
|
|
Float(cfg["speed"] || 1.0)
|
|
rescue
|
|
1.0
|
|
end
|
|
tick = ((Graphics.frame_count rescue 0) / int_value(cfg["frame_step"], 4, 1)).to_f
|
|
heights = []
|
|
columns.times do |idx|
|
|
phase = tick * (0.52 * speed) + (phase_seed.to_i * 0.19) + idx * 0.85
|
|
wave = Math.sin(phase) * 0.52 + Math.sin(phase * 1.7 + idx * 0.35) * 0.23 + 0.75
|
|
value = [[wave, 0.0].max, 1.0].min
|
|
h = 1 + (value * (rows - 1)).round
|
|
heights << [[h, 1].max, rows].min
|
|
end
|
|
heights
|
|
rescue
|
|
Array.new(columns, 1)
|
|
end
|
|
|
|
def self.music_bar_color(row_index, rows, alpha)
|
|
idx = row_index.to_i
|
|
total = [rows.to_i, 1].max
|
|
if idx >= total - 2
|
|
Color.new(255, 32, 16, alpha)
|
|
elsif idx >= total - 4
|
|
Color.new(255, 232, 0, alpha)
|
|
else
|
|
Color.new(24, 255, 0, alpha)
|
|
end
|
|
rescue
|
|
Color.new(24, 255, 0, alpha)
|
|
end
|
|
|
|
def self.draw_music_equalizer(bitmap, style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
cols = int_value(cfg["bars"], 5, 1)
|
|
rows = int_value(cfg["rows"], 7, 2)
|
|
gap = int_value(cfg["cell_gap"], 2, 0)
|
|
alpha = [[int_value(cfg["bar_alpha"], 235, 0), 0].max, 255].min
|
|
inner_left = 4
|
|
inner_top = 1
|
|
inner_width = [bitmap.width - inner_left * 2, 1].max
|
|
inner_height = [bitmap.height - 2, 1].max
|
|
col_w = [((inner_width - gap * (cols - 1)) / cols.to_f).floor, 3].max
|
|
total_w = cols * col_w + gap * (cols - 1)
|
|
start_x = inner_left + [(inner_width - total_w) / 2, 0].max
|
|
cell_h = [((inner_height - gap * (rows - 1)) / rows.to_f).floor, 2].max
|
|
total_h = rows * cell_h + gap * (rows - 1)
|
|
start_y = inner_top + [(inner_height - total_h) / 2, 0].max
|
|
heights = music_equalizer_heights(style_id, cols, phase_seed)
|
|
|
|
cols.times do |cx|
|
|
active_rows = heights[cx].to_i
|
|
next if active_rows <= 0
|
|
x = start_x + cx * (col_w + gap)
|
|
active_rows.times do |ri|
|
|
y = start_y + (rows - 1 - ri) * (cell_h + gap)
|
|
bitmap.fill_rect(x - 1, y - 1, col_w + 2, cell_h + 2, Color.new(0, 0, 0, 220))
|
|
bitmap.fill_rect(x, y, col_w, cell_h, music_bar_color(ri, rows, alpha))
|
|
end
|
|
end
|
|
rescue
|
|
end
|
|
|
|
def self.draw_blood_drips(bitmap, text, style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
drip_height = [int_value(cfg["drip_height"], 12, 1), 24].min
|
|
drip_count = [int_value(cfg["drip_count"], 6, 1), 16].min
|
|
drip_min = int_value(cfg["drip_min"], 2, 1)
|
|
drip_max = int_value(cfg["drip_max"], 10, 1)
|
|
drip_max = [[drip_max, drip_min].max, drip_height].min
|
|
drip_alpha = [[int_value(cfg["drip_alpha"], 245, 0), 0].max, 255].min
|
|
outline_size = int_value(cfg["outline_size"], 1, 0)
|
|
text_width = begin
|
|
[bitmap.text_size(text.to_s).width, 8].max
|
|
rescue
|
|
[text.to_s.length * (bitmap.font.size / 2), 8].max
|
|
end
|
|
usable_width = [text_width - 2, 6].max
|
|
left = [(bitmap.width - text_width) / 2, 2 + outline_size].max
|
|
base_y = [bitmap.height - drip_height - 3, 4 + outline_size].max
|
|
speed = begin
|
|
Float(cfg["speed"] || 1.0)
|
|
rescue
|
|
1.0
|
|
end
|
|
tick = ((Graphics.frame_count rescue 0) / [int_value(cfg["frame_step"], 4, 1), 1].max.to_f) * speed
|
|
fill_color = color_from_array(cfg["text_color"])
|
|
fill_color = Color.new(fill_color.red, fill_color.green, fill_color.blue, drip_alpha)
|
|
outline_color = Color.new(0, 0, 0, [drip_alpha, 235].max)
|
|
|
|
count = [[drip_count, (usable_width / 7.0).round].max, 3].max
|
|
count = [count, 16].min
|
|
|
|
count.times do |idx|
|
|
span = count <= 1 ? 0.5 : idx.to_f / (count - 1).to_f
|
|
x = left + (span * usable_width).round
|
|
wave = Math.sin(tick * 0.72 + idx * 0.93 + phase_seed.to_i * 0.17) * 0.5 +
|
|
Math.sin(tick * 1.21 + idx * 0.37 + phase_seed.to_i * 0.09) * 0.22 + 0.5
|
|
value = [[wave, 0.0].max, 1.0].min
|
|
len = drip_min + (value * (drip_max - drip_min)).round
|
|
width = 1 + ((((Math.sin(tick * 0.41 + idx * 1.11 + phase_seed.to_i * 0.13) + 1.0) * 0.5) * 2).round)
|
|
width = [[width, 1].max, 3].min
|
|
x = [[x, 2].max, bitmap.width - width - 3].min
|
|
y = base_y + ((Math.sin(tick * 0.55 + idx * 0.64 + phase_seed.to_i * 0.21) + 1.0) * 0.5).round
|
|
y = [[y, 2].max, bitmap.height - 4].min
|
|
len = [[len, 1].max, bitmap.height - y - 3].min
|
|
next if len <= 0
|
|
bitmap.fill_rect(x - 1, y - 1, width + 2, len + 2, outline_color)
|
|
bitmap.fill_rect(x, y, width, len, fill_color)
|
|
blob_y = [y + len - 1, bitmap.height - 3].min
|
|
bitmap.fill_rect(x - 1, blob_y, width + 2, 2, outline_color)
|
|
bitmap.fill_rect(x, blob_y, width, 2, fill_color)
|
|
if width <= 2 && blob_y + 1 < bitmap.height - 1
|
|
bitmap.fill_rect(x - 1, blob_y + 1, width + 2, 1, outline_color)
|
|
bitmap.fill_rect(x, blob_y + 1, width + 1, 1, fill_color)
|
|
end
|
|
end
|
|
rescue
|
|
end
|
|
|
|
|
|
def self.draw_pixel_safe(bitmap, x, y, color)
|
|
bitmap.set_pixel(x, y, color)
|
|
rescue
|
|
end
|
|
|
|
def self.draw_concha_splat(bitmap, center_x, center_y, radius, fill_color, outline_color, seed = 0)
|
|
return if radius.to_i <= 1
|
|
angle_step = 16
|
|
points = []
|
|
angle_step.times do |idx|
|
|
ang = (Math::PI * 2.0 * idx) / angle_step
|
|
wave_a = Math.sin(seed.to_i * 0.71 + idx * 1.37)
|
|
wave_b = Math.cos(seed.to_i * 0.39 + idx * 0.83)
|
|
r = radius.to_f * (0.82 + wave_a * 0.22 + wave_b * 0.11)
|
|
if idx % 3 == 0
|
|
r *= 1.28
|
|
elsif idx % 5 == 0
|
|
r *= 0.88
|
|
end
|
|
r = [[r, radius * 0.55].max, radius * 1.45].min
|
|
px = center_x + Math.cos(ang) * r
|
|
py = center_y + Math.sin(ang) * r
|
|
points << [px, py]
|
|
end
|
|
|
|
min_x = [[points.map { |p| p[0] }.min.floor - 2, 0].max, bitmap.width - 1].min
|
|
max_x = [[points.map { |p| p[0] }.max.ceil + 2, 0].max, bitmap.width - 1].min
|
|
min_y = [[points.map { |p| p[1] }.min.floor - 2, 0].max, bitmap.height - 1].min
|
|
max_y = [[points.map { |p| p[1] }.max.ceil + 2, 0].max, bitmap.height - 1].min
|
|
|
|
py = min_y
|
|
while py <= max_y
|
|
px = min_x
|
|
while px <= max_x
|
|
inside = false
|
|
j = points.length - 1
|
|
i = 0
|
|
while i < points.length
|
|
xi = points[i][0]
|
|
yi = points[i][1]
|
|
xj = points[j][0]
|
|
yj = points[j][1]
|
|
intersect = ((yi > py) != (yj > py)) && (px < (xj - xi) * (py - yi).to_f / ((yj - yi).nonzero? || 0.0001) + xi)
|
|
inside = !inside if intersect
|
|
j = i
|
|
i += 1
|
|
end
|
|
if inside
|
|
edge = false
|
|
points.each do |vx, vy|
|
|
if ((vx - px).abs + (vy - py).abs) <= 2.35
|
|
edge = true
|
|
break
|
|
end
|
|
end
|
|
if !edge
|
|
points.each_with_index do |p1, idx|
|
|
p2 = points[(idx + 1) % points.length]
|
|
x1, y1 = p1
|
|
x2, y2 = p2
|
|
dx = x2 - x1
|
|
dy = y2 - y1
|
|
denom = dx * dx + dy * dy
|
|
next if denom <= 0.0
|
|
t = (((px - x1) * dx) + ((py - y1) * dy)).to_f / denom
|
|
t = [[t, 0.0].max, 1.0].min
|
|
proj_x = x1 + dx * t
|
|
proj_y = y1 + dy * t
|
|
dist = ((proj_x - px) ** 2 + (proj_y - py) ** 2) ** 0.5
|
|
if dist <= 1.35
|
|
edge = true
|
|
break
|
|
end
|
|
end
|
|
end
|
|
draw_pixel_safe(bitmap, px, py, edge ? outline_color : fill_color)
|
|
end
|
|
px += 1
|
|
end
|
|
py += 1
|
|
end
|
|
|
|
2.times do |drip_idx|
|
|
next unless drip_idx == (seed.to_i % 2) || radius > 10
|
|
drip_x = center_x + ((drip_idx == 0 ? -1 : 1) * (radius * 0.28 + (seed % 3))).round
|
|
drip_len = [[(radius * 0.75 + ((seed + drip_idx) % 4)).round, 4].max, radius + 6].min
|
|
drip_y = center_y + (radius * 0.35).round
|
|
2.times do |w|
|
|
bitmap.fill_rect(drip_x + w - 1, drip_y - 1, 2, drip_len + 2, outline_color)
|
|
bitmap.fill_rect(drip_x + w, drip_y, 1, drip_len, fill_color)
|
|
end
|
|
blob_y = [drip_y + drip_len - 1, bitmap.height - 2].min
|
|
bitmap.fill_rect(drip_x - 1, blob_y, 4, 2, outline_color)
|
|
bitmap.fill_rect(drip_x, blob_y, 2, 1, fill_color)
|
|
end
|
|
rescue
|
|
end
|
|
|
|
def self.mix_color(color_a, color_b, t)
|
|
blend = [[t.to_f, 0.0].max, 1.0].min
|
|
Color.new(
|
|
(color_a.red + (color_b.red - color_a.red) * blend).round,
|
|
(color_a.green + (color_b.green - color_a.green) * blend).round,
|
|
(color_a.blue + (color_b.blue - color_a.blue) * blend).round,
|
|
(color_a.alpha + (color_b.alpha - color_a.alpha) * blend).round
|
|
)
|
|
rescue
|
|
color_b || color_a || Color.new(255, 255, 255, 255)
|
|
end
|
|
|
|
def self.concha_wave_tick(style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
step = [int_value(cfg["frame_step"], 4, 1), 1].max
|
|
speed = begin
|
|
Float(cfg["speed"] || 1.0)
|
|
rescue
|
|
1.0
|
|
end
|
|
(((Graphics.frame_count rescue 0) / step.to_f) * speed) + phase_seed.to_i * 0.37
|
|
rescue
|
|
phase_seed.to_i.to_f
|
|
end
|
|
|
|
def self.draw_concha_splats(bitmap, text, style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
candidate_count = [[int_value(cfg["splat_count"], 6, 1), 3].max, 6].min
|
|
alpha_max = [[int_value(cfg["splat_alpha"], 210, 0), 0].max, 255].min
|
|
min_r = int_value(cfg["splat_min_radius"], 7, 2)
|
|
max_r = [int_value(cfg["splat_max_radius"], 12, 3), min_r].max
|
|
tick = concha_wave_tick(style_id, phase_seed).floor
|
|
|
|
fill_base = color_from_array(cfg["text_color"])
|
|
outline_base = color_from_array(cfg["outline_color"])
|
|
|
|
text_width = begin
|
|
[bitmap.text_size(text.to_s).width, 12].max
|
|
rescue
|
|
[text.to_s.length * (bitmap.font.size / 2), 12].max
|
|
end
|
|
cx = bitmap.width / 2
|
|
cy = [bitmap.height / 2 - 1, 8].max
|
|
spread = [text_width / 3, 10].max
|
|
offset_pool = [
|
|
[-spread - 4, -2],
|
|
[-spread / 2, 1],
|
|
[0, -3],
|
|
[spread / 2, 0],
|
|
[spread + 4, -1],
|
|
[0, 2]
|
|
]
|
|
positions = offset_pool[0, candidate_count].map { |ox, oy| [cx + ox.to_i, cy + oy.to_i] }
|
|
|
|
cycle_len = 16
|
|
fade_len = 10.0
|
|
cycle = tick / cycle_len
|
|
local = tick % cycle_len
|
|
hash = ((cycle + 1) * 1103515245 + (phase_seed.to_i + 7) * 12345) & 0x7fffffff
|
|
active_count = (hash % 100) < 45 ? 2 : 1
|
|
idx_a = hash % positions.length
|
|
idx_b = ((hash / 97) + 1) % positions.length
|
|
idx_b = (idx_b + 1) % positions.length if idx_b == idx_a
|
|
events = [[idx_a, 0]]
|
|
events << [idx_b, 3] if active_count >= 2
|
|
|
|
events.each_with_index do |(spot_idx, appear_tick), draw_idx|
|
|
age = local - appear_tick
|
|
next if age < 0 || age >= fade_len
|
|
fade = 1.0 - (age.to_f / fade_len)
|
|
alpha = [[(alpha_max * fade).round, 0].max, 255].min
|
|
next if alpha <= 0
|
|
px, py = positions[spot_idx]
|
|
radius_seed = ((hash / (17 + draw_idx * 11)) + spot_idx * 23) & 0x7fffffff
|
|
radius = min_r + (radius_seed % (max_r - min_r + 1))
|
|
fill_color = Color.new(fill_base.red, fill_base.green, fill_base.blue, alpha)
|
|
outline_alpha = [[alpha + 20, 0].max, 255].min
|
|
outline_color = Color.new(outline_base.red, outline_base.green, outline_base.blue, outline_alpha)
|
|
draw_concha_splat(bitmap, px, py, radius, fill_color, outline_color, phase_seed.to_i * 31 + tick * 7 + draw_idx * 19 + spot_idx * 29)
|
|
end
|
|
rescue
|
|
end
|
|
|
|
def self.concha_text_color(style_id, ratio = 0.5, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
base_color = color_from_array(cfg["text_color"])
|
|
flash_color = color_from_array(cfg["flash_text_color"])
|
|
tick = concha_wave_tick(style_id, phase_seed)
|
|
wave = (Math.sin(tick * 0.32 + ratio.to_f * Math::PI * 2.0) + 1.0) * 0.5
|
|
blend = 0.2 + wave * 0.8
|
|
mix_color(base_color, flash_color, blend)
|
|
rescue
|
|
Color.new(255, 245, 170, 255)
|
|
end
|
|
|
|
def self.draw_concha_gradient_text(bitmap, text, style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
outline = [int_value(cfg["outline_size"], 1, 0), 3].min
|
|
outline_color = color_from_array(cfg["outline_color"])
|
|
chars = text.to_s.each_char.to_a
|
|
chars = [text.to_s] if chars.empty?
|
|
widths = chars.map do |ch|
|
|
begin
|
|
[bitmap.text_size(ch).width, 1].max
|
|
rescue
|
|
[bitmap.font.size / 2, 1].max
|
|
end
|
|
end
|
|
total_width = widths.inject(0) { |sum, v| sum + v }
|
|
start_x = ((bitmap.width - total_width) / 2.0).floor
|
|
draw_y = outline
|
|
height = [bitmap.height - draw_y * 2 + outline, 1].max
|
|
offsets = []
|
|
if outline > 0
|
|
(-outline..outline).each do |ox|
|
|
(-outline..outline).each do |oy|
|
|
next if ox == 0 && oy == 0
|
|
next if ox.abs + oy.abs > outline + 1
|
|
offsets << [ox, oy]
|
|
end
|
|
end
|
|
end
|
|
|
|
x = start_x
|
|
chars.each_with_index do |ch, idx|
|
|
cw = widths[idx]
|
|
ratio = (x + cw * 0.5 - start_x).to_f / [total_width.to_f, 1.0].max
|
|
ratio = [[ratio, 0.0].max, 1.0].min
|
|
color = concha_text_color(style_id, ratio, phase_seed)
|
|
if outline > 0
|
|
bitmap.font.color = outline_color
|
|
offsets.each do |ox, oy|
|
|
bitmap.draw_text(x + ox, draw_y + oy, cw + outline * 4, height, ch, 0)
|
|
end
|
|
end
|
|
bitmap.font.color = color
|
|
bitmap.draw_text(x, draw_y, cw + outline * 4, height, ch, 0)
|
|
x += cw
|
|
end
|
|
rescue
|
|
outline_color = color_from_array(style(style_id)["outline_color"])
|
|
draw_outlined_text(bitmap, text.to_s, outline_color, concha_text_color(style_id, 0.5, phase_seed), style(style_id)["outline_size"])
|
|
end
|
|
|
|
def self.neon_palette
|
|
[
|
|
[255, 0, 0],
|
|
[255, 128, 0],
|
|
[255, 255, 0],
|
|
[0, 255, 0],
|
|
[0, 255, 255],
|
|
[0, 128, 255],
|
|
[0, 0, 255],
|
|
[255, 0, 255]
|
|
]
|
|
rescue
|
|
[[255, 0, 0]]
|
|
end
|
|
|
|
def self.neon_outline_color(style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
palette = neon_palette
|
|
hold = [int_value(cfg["neon_hold"], 1, 1), 1].max
|
|
tick = render_tick(style_id, phase_seed).to_i / hold
|
|
hash = ((tick + 1) * 1103515245 + (phase_seed.to_i + 19) * 12345) & 0x7fffffff
|
|
rgb = palette[hash % palette.length] || palette[0]
|
|
Color.new(rgb[0], rgb[1], rgb[2], 255)
|
|
rescue
|
|
Color.new(255, 0, 0, 255)
|
|
end
|
|
|
|
def self.draw_neon_text(bitmap, text, style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
outline_color = neon_outline_color(style_id, phase_seed)
|
|
text_color = color_from_array(cfg["text_color"])
|
|
draw_thin_outlined_text(bitmap, text.to_s, outline_color, text_color)
|
|
rescue
|
|
draw_thin_outlined_text(bitmap, text.to_s, Color.new(255, 0, 0, 255), Color.new(0, 0, 0, 255))
|
|
end
|
|
|
|
def self.draw_circle(bitmap, center_x, center_y, radius, color)
|
|
r = [radius.to_i, 1].max
|
|
min_x = [center_x - r, 0].max
|
|
max_x = [center_x + r, bitmap.width - 1].min
|
|
min_y = [center_y - r, 0].max
|
|
max_y = [center_y + r, bitmap.height - 1].min
|
|
rr = r * r
|
|
y = min_y
|
|
while y <= max_y
|
|
x = min_x
|
|
while x <= max_x
|
|
dx = x - center_x
|
|
dy = y - center_y
|
|
draw_pixel_safe(bitmap, x, y, color) if dx * dx + dy * dy <= rr
|
|
x += 1
|
|
end
|
|
y += 1
|
|
end
|
|
rescue
|
|
end
|
|
|
|
def self.draw_romashka_flower(bitmap, center_x, center_y, flower_radius, angle, alpha, petal_count = 12, petal_distance = nil, petal_radius = nil)
|
|
outline_color = Color.new(0, 0, 0, [[alpha.to_i + 10, 0].max, 255].min)
|
|
petal_fill = Color.new(255, 255, 255, [[alpha.to_i, 0].max, 255].min)
|
|
center_fill = Color.new(248, 202, 58, [[alpha.to_i, 0].max, 255].min)
|
|
center_glow = Color.new(255, 170, 0, [[alpha.to_i, 0].max, 255].min)
|
|
petal_count = [[petal_count.to_i, 6].max, 18].min
|
|
petal_distance = [petal_distance.to_i > 0 ? petal_distance.to_i : flower_radius + 2, 6].max
|
|
petal_radius = [petal_radius.to_i > 0 ? petal_radius.to_i : (flower_radius * 0.52).round, 3].max
|
|
petal_count.times do |idx|
|
|
ang = angle + (Math::PI * 2.0 * idx) / petal_count.to_f
|
|
px = center_x + Math.cos(ang) * petal_distance
|
|
py = center_y + Math.sin(ang) * petal_distance
|
|
draw_circle(bitmap, px.round, py.round, petal_radius + 1, outline_color)
|
|
draw_circle(bitmap, px.round, py.round, petal_radius, petal_fill)
|
|
end
|
|
draw_circle(bitmap, center_x, center_y, flower_radius + 1, outline_color)
|
|
draw_circle(bitmap, center_x, center_y, flower_radius, center_fill)
|
|
draw_circle(bitmap, center_x, center_y, [flower_radius - 2, 2].max, center_glow)
|
|
rescue
|
|
end
|
|
|
|
def self.draw_romashka_flowers(bitmap, text, style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
alpha = [[int_value(cfg["flower_alpha"], 210, 0), 0].max, 255].min
|
|
count = [[int_value(cfg["flower_count"], 2, 1), 1].max, 3].min
|
|
orbit_distance = [int_value(cfg["orbit_distance"], 18, 4), 4].max
|
|
rotation_speed = begin
|
|
Float(cfg["rotation_speed"] || 1.0)
|
|
rescue
|
|
1.0
|
|
end
|
|
tick = ((Graphics.frame_count rescue 0) / [int_value(cfg["frame_step"], 4, 1), 1].max.to_f) * rotation_speed + phase_seed.to_i * 0.29
|
|
|
|
text_width = begin
|
|
[bitmap.text_size(text.to_s).width, 16].max
|
|
rescue
|
|
[text.to_s.length * (bitmap.font.size / 2), 16].max
|
|
end
|
|
flower_radius = [int_value(cfg["center_radius"], 5, 2), 2].max
|
|
petal_count = [int_value(cfg["petal_count"], 12, 6), 18].min
|
|
petal_radius = [int_value(cfg["petal_radius"], 5, 2), 8].min
|
|
petal_distance = [int_value(cfg["petal_distance"], 10, 4), 16].min
|
|
cx = bitmap.width / 2
|
|
cy = [bitmap.height / 2 - 1, 8].max
|
|
anchor_gap = [int_value(cfg["anchor_gap"], flower_radius + petal_distance - 1, 4), 4].max
|
|
anchors = []
|
|
if count == 1
|
|
anchors << [cx, cy]
|
|
elsif count == 2
|
|
anchors << [cx - anchor_gap, cy]
|
|
anchors << [cx + anchor_gap, cy]
|
|
else
|
|
anchors << [cx - anchor_gap, cy + 1]
|
|
anchors << [cx + anchor_gap, cy + 1]
|
|
anchors << [cx, cy - orbit_distance / 2]
|
|
end
|
|
|
|
anchors.each_with_index do |(ax, ay), idx|
|
|
rot = tick * 0.18 + idx * 1.91
|
|
ox = Math.cos(rot) * orbit_distance
|
|
oy = Math.sin(rot * 0.93 + idx * 0.7) * (orbit_distance * 0.42)
|
|
angle = tick * 0.28 + idx * 2.17
|
|
local_alpha = [[alpha - idx * 14, 100].max, 255].min
|
|
draw_romashka_flower(bitmap, (ax + ox).round, (ay + oy).round, flower_radius, angle, local_alpha, petal_count, petal_distance, petal_radius)
|
|
end
|
|
rescue
|
|
end
|
|
|
|
def self.draw_romashka_text(bitmap, text, style_id, phase_seed = 0)
|
|
cfg = style(style_id)
|
|
outline_color = color_from_array(cfg["outline_color"])
|
|
text_color = color_from_array(cfg["text_color"])
|
|
draw_outlined_text(bitmap, text.to_s, outline_color, text_color, cfg["outline_size"])
|
|
rescue
|
|
draw_outlined_text(bitmap, text.to_s, Color.new(248, 210, 64, 255), Color.new(255, 255, 255, 255), 1)
|
|
end
|
|
|
|
def self.draw_thin_outlined_text(bitmap, text, outline_color, text_color)
|
|
draw_x = 3
|
|
draw_y = 1
|
|
width = [bitmap.width - 6, 1].max
|
|
height = [bitmap.height - 2, 1].max
|
|
bitmap.font.color = outline_color
|
|
[
|
|
[-1, 0], [1, 0], [0, -1], [0, 1],
|
|
[-1, -1], [1, -1], [-1, 1], [1, 1]
|
|
].each do |ox, oy|
|
|
bitmap.draw_text(draw_x + ox, draw_y + oy, width, height, text, 1)
|
|
end
|
|
bitmap.font.color = text_color
|
|
bitmap.draw_text(draw_x, draw_y, width, height, text, 1)
|
|
rescue
|
|
bitmap.font.color = text_color
|
|
bitmap.draw_text(0, 0, bitmap.width, bitmap.height, text, 1)
|
|
end
|
|
|
|
def self.draw_outlined_text(bitmap, text, outline_color, text_color, outline_size = 1)
|
|
cfg = style(0)
|
|
outline = [int_value(outline_size, 1, 0), 3].min
|
|
draw_x = 2 + outline
|
|
draw_y = outline
|
|
width = [bitmap.width - draw_x * 2 + outline, 1].max
|
|
height = [bitmap.height - draw_y * 2 + outline, 1].max
|
|
bitmap.font.color = outline_color
|
|
if outline > 0
|
|
offsets = []
|
|
(-outline..outline).each do |ox|
|
|
(-outline..outline).each do |oy|
|
|
next if ox == 0 && oy == 0
|
|
next if ox.abs + oy.abs > outline + 1
|
|
offsets << [ox, oy]
|
|
end
|
|
end
|
|
offsets.each do |ox, oy|
|
|
bitmap.draw_text(draw_x + ox, draw_y + oy, width, height, text, 1)
|
|
end
|
|
end
|
|
bitmap.font.color = text_color
|
|
bitmap.draw_text(draw_x, draw_y, width, height, text, 1)
|
|
rescue
|
|
bitmap.font.color = text_color
|
|
bitmap.draw_text(0, 0, bitmap.width, bitmap.height, text, 1)
|
|
end
|
|
|
|
def self.redraw_sprite(sprite, text, font_name, style_id, fallback_size = 16, phase_seed = 0)
|
|
return if sprite.nil?
|
|
width, height = measure(text, font_name, style_id, fallback_size)
|
|
begin
|
|
if sprite.bitmap.nil? || sprite.bitmap.width != width || sprite.bitmap.height != height
|
|
old_bmp = sprite.bitmap
|
|
sprite.bitmap = Bitmap.new(width, height)
|
|
old_bmp.dispose if old_bmp
|
|
end
|
|
rescue
|
|
sprite.bitmap = Bitmap.new(width, height)
|
|
end
|
|
|
|
bitmap = sprite.bitmap
|
|
bitmap.clear
|
|
apply_font(bitmap, font_name, style_id, fallback_size)
|
|
cfg = style(style_id)
|
|
code = cfg["code"].to_s
|
|
|
|
if code == "js_rainbow_black_outline"
|
|
text_color = animated_text_color(style_id, phase_seed)
|
|
outline_color = color_from_array(cfg["outline_color"])
|
|
draw_outlined_text(bitmap, text.to_s, outline_color, text_color, cfg["outline_size"])
|
|
elsif code == "js_music_equalizer_black_outline"
|
|
draw_music_equalizer(bitmap, style_id, phase_seed)
|
|
text_color = color_from_array(cfg["text_color"])
|
|
outline_color = color_from_array(cfg["outline_color"])
|
|
draw_outlined_text(bitmap, text.to_s, outline_color, text_color, cfg["outline_size"])
|
|
elsif code == "js_blood_red_black_outline"
|
|
draw_blood_drips(bitmap, text.to_s, style_id, phase_seed)
|
|
text_color = color_from_array(cfg["text_color"])
|
|
outline_color = color_from_array(cfg["outline_color"])
|
|
draw_outlined_text(bitmap, text.to_s, outline_color, text_color, cfg["outline_size"])
|
|
elsif code == "js_concha_flash_splat"
|
|
draw_concha_splats(bitmap, text.to_s, style_id, phase_seed)
|
|
draw_concha_gradient_text(bitmap, text.to_s, style_id, phase_seed)
|
|
elsif code == "js_neon_black_rainbow_outline"
|
|
draw_neon_text(bitmap, text.to_s, style_id, phase_seed)
|
|
elsif code == "js_romashka_white_yellow_outline"
|
|
draw_romashka_flowers(bitmap, text.to_s, style_id, phase_seed)
|
|
draw_romashka_text(bitmap, text.to_s, style_id, phase_seed)
|
|
else
|
|
bitmap.font.shadow = bool_value(cfg["shadow"], true)
|
|
bitmap.font.color = color_from_array(cfg["text_color"])
|
|
bitmap.draw_text(0, 0, bitmap.width, bitmap.height, text.to_s, 1)
|
|
end
|
|
|
|
sprite.ox = bitmap.width / 2
|
|
sprite.oy = bitmap.height
|
|
rescue
|
|
end
|
|
end
|
|
|
|
if defined?(WebSocket)
|
|
module WebSocket
|
|
@nickname_styles ||= {}
|
|
|
|
def self.nickname_style_for(player_id)
|
|
@nickname_styles ||= {}
|
|
pid = player_id.to_i
|
|
return MPNickStyle.default_style_id if pid <= 0
|
|
MPNickStyle.style(@nickname_styles[pid])["id"].to_i
|
|
rescue
|
|
MPNickStyle.default_style_id
|
|
end
|
|
|
|
def self.set_nickname_style(player_id, style_id)
|
|
@nickname_styles ||= {}
|
|
pid = player_id.to_i
|
|
return if pid <= 0
|
|
@nickname_styles[pid] = MPNickStyle.style(style_id)["id"].to_i
|
|
rescue
|
|
end
|
|
|
|
def self.clear_nickname_styles
|
|
@nickname_styles = {}
|
|
rescue
|
|
end
|
|
|
|
class << self
|
|
alias mp_nick_style_parse parse
|
|
def parse(msg)
|
|
if msg =~ /^nick_style:(\d+):(-?\d+)$/
|
|
set_nickname_style($1.to_i, $2.to_i)
|
|
return
|
|
end
|
|
mp_nick_style_parse(msg)
|
|
end
|
|
|
|
alias mp_nick_style_disconnect disconnect
|
|
def disconnect
|
|
clear_nickname_styles
|
|
mp_nick_style_disconnect
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
class Sprite_Character < Sprite_Base
|
|
def mp_dispose_net_nickname
|
|
if @mp_nick_sprite
|
|
@mp_nick_sprite.bitmap.dispose if @mp_nick_sprite.bitmap
|
|
@mp_nick_sprite.dispose
|
|
@mp_nick_sprite = nil
|
|
end
|
|
@mp_nick_text = nil
|
|
@mp_nick_style_id = nil
|
|
@mp_nick_render_tick = nil
|
|
rescue
|
|
@mp_nick_sprite = nil
|
|
@mp_nick_text = nil
|
|
@mp_nick_style_id = nil
|
|
@mp_nick_render_tick = nil
|
|
end
|
|
|
|
def mp_update_net_nickname
|
|
pid = nil
|
|
begin
|
|
if @character && @character.instance_variable_defined?(:@net_player_id)
|
|
pid = @character.instance_variable_get(:@net_player_id)
|
|
end
|
|
rescue
|
|
pid = nil
|
|
end
|
|
|
|
if pid.nil?
|
|
mp_dispose_net_nickname
|
|
return
|
|
end
|
|
|
|
nick = nil
|
|
begin
|
|
nick = WebSocket.nickname_for(pid) if defined?(WebSocket)
|
|
rescue
|
|
nick = nil
|
|
end
|
|
nick = nick.to_s
|
|
if nick.strip == ""
|
|
mp_dispose_net_nickname
|
|
return
|
|
end
|
|
|
|
style_id = begin
|
|
defined?(WebSocket) ? WebSocket.nickname_style_for(pid) : MPNickStyle.default_style_id
|
|
rescue
|
|
MPNickStyle.default_style_id
|
|
end
|
|
render_tick = MPNickStyle.render_tick(style_id, pid.to_i)
|
|
|
|
if @mp_nick_sprite.nil?
|
|
@mp_nick_sprite = Sprite.new(self.viewport)
|
|
end
|
|
|
|
if @mp_nick_text != nick || @mp_nick_style_id != style_id || @mp_nick_render_tick != render_tick
|
|
MPNickStyle.redraw_sprite(@mp_nick_sprite, nick, MPSafeFont.nick_name, style_id, 16, pid.to_i)
|
|
@mp_nick_text = nick
|
|
@mp_nick_style_id = style_id
|
|
@mp_nick_render_tick = render_tick
|
|
end
|
|
|
|
return if @mp_nick_sprite.nil?
|
|
@mp_nick_sprite.x = self.x
|
|
@mp_nick_sprite.y = self.y - height - 1
|
|
@mp_nick_sprite.z = self.z + 1
|
|
@mp_nick_sprite.visible = self.visible
|
|
rescue
|
|
mp_dispose_net_nickname
|
|
end
|
|
|
|
def create_nickname_sprite
|
|
@nickname_sprite = Sprite.new(self.viewport) if @nickname_sprite.nil?
|
|
text = NicknameInput.nickname.to_s
|
|
style_id = begin
|
|
if defined?(WebSocket) && WebSocket.respond_to?(:my_id) && WebSocket.my_id
|
|
WebSocket.nickname_style_for(WebSocket.my_id)
|
|
else
|
|
MPNickStyle.default_style_id
|
|
end
|
|
rescue
|
|
MPNickStyle.default_style_id
|
|
end
|
|
render_tick = MPNickStyle.render_tick(style_id, 0)
|
|
MPNickStyle.redraw_sprite(@nickname_sprite, text, NicknameInput.ui_font_name, style_id, 16, 0)
|
|
@nickname_text = text
|
|
@nickname_style_id = style_id
|
|
@nickname_render_tick = render_tick
|
|
rescue
|
|
end
|
|
|
|
def update_nickname_sprite
|
|
return dispose_nickname_sprite unless display_nickname?
|
|
|
|
text = NicknameInput.nickname.to_s
|
|
style_id = begin
|
|
if defined?(WebSocket) && WebSocket.respond_to?(:my_id) && WebSocket.my_id
|
|
WebSocket.nickname_style_for(WebSocket.my_id)
|
|
else
|
|
MPNickStyle.default_style_id
|
|
end
|
|
rescue
|
|
MPNickStyle.default_style_id
|
|
end
|
|
render_tick = MPNickStyle.render_tick(style_id, 0)
|
|
|
|
create_nickname_sprite if @nickname_sprite.nil?
|
|
if @nickname_text != text || @nickname_style_id != style_id || @nickname_render_tick != render_tick
|
|
create_nickname_sprite
|
|
end
|
|
|
|
return if @nickname_sprite.nil?
|
|
@nickname_sprite.x = self.x
|
|
@nickname_sprite.y = self.y - height - 1
|
|
@nickname_sprite.z = self.z + 10
|
|
@nickname_sprite.visible = self.visible
|
|
rescue
|
|
dispose_nickname_sprite
|
|
end
|
|
|
|
def dispose_nickname_sprite
|
|
if @nickname_sprite
|
|
@nickname_sprite.bitmap.dispose if @nickname_sprite.bitmap
|
|
@nickname_sprite.dispose
|
|
@nickname_sprite = nil
|
|
end
|
|
@nickname_text = nil
|
|
@nickname_style_id = nil
|
|
@nickname_render_tick = nil
|
|
rescue
|
|
@nickname_sprite = nil
|
|
@nickname_text = nil
|
|
@nickname_style_id = nil
|
|
@nickname_render_tick = nil
|
|
end
|
|
end
|