NameEntry = {} NameEntry.__index = NameEntry -- Taiko 14 name_entry.swg: the backyard, Don (Katsu for 2P) dropping onto the drum, the name tiles and the kana -- table (row heads, then the row's kana, then its other forms). Rim moves the cursor, face decides; ← deletes, -- → jumps to おわり. Positions are the swg's 640x480 px (K scales them), times are 60 fps frames. local PATH = "entry/t14_name/" local FRAME_MS = 1000 / 60 local K = tex.get_screen_width() / 640 local WHITE, BLACK = { 255, 255, 255, 255 }, { 0, 0, 0, 255 } local MAX_LEN = 5 local TILE_X, TILE_PITCH, TILE_Y = 27, 64, 312.7 local CELL_X, CELL_Y, CELL_PITCH, CELL_ROW, PER_ROW = 38.5, 82.3, 37.05, 37, 7 local DONE_X, DONE_Y = 244.8, 103.3 local DRUM_X, DRUM_Y, CHARA_X, CHARA_Y = 356, 247, 324, 24 local INPUT_AT, TABLE_AT, HELP_VO_AT = 30, 20, 65 local DONE_CELL = 14 -- glyph texture index of a character: ぁ..ん in Unicode order, then the specials local SPECIAL = { ["~"] = 83, ["ー"] = 84, ["?"] = 85, ["?"] = 85, ["!"] = 86, ["!"] = 86 } local BACK, LEFT_ARROW, RIGHT_ARROW, DONE_GLYPH = 87, 88, 89, 90 local function chars(s) local out = {} for c in s:gmatch("[%z\1-\127\194-\244][\128-\191]*") do out[#out + 1] = c end return out end local function glyph_index(c) if SPECIAL[c] then return SPECIAL[c] end local b1, b2, b3 = c:byte(1, 3) if #c ~= 3 or not b3 then return nil end local cp = (b1 - 0xE0) * 4096 + (b2 - 0x80) * 64 + (b3 - 0x80) if cp >= 0x3041 and cp <= 0x3093 then return cp - 0x3041 end return nil end local ROWS = { ja = { "あいうえお", "かきくけこ", "さしすせそ", "たちつてと", "なにぬねの", "はひふへほ", "まみむめも", "やゆよ", "らりるれろ", "わをん" }, latin = { "ABCDE", "FGHIJ", "KLMNO", "PQRST", "UVWXY", "Z-.!?", "01234", "56789" }, } -- base char followed by its other forms local VARIANTS = {} local groups = { "あぁ", "いぃ", "うぅ", "えぇ", "おぉ", "かが", "きぎ", "くぐ", "けげ", "こご", "さざ", "しじ", "すず", "せぜ", "そぞ", "ただ", "ちぢ", "つづっ", "てで", "とど", "はばぱ", "ひびぴ", "ふぶぷ", "へべぺ", "ほぼぽ", "やゃ", "ゆゅ", "よょ", "わゎ" } for c in ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):gmatch(".") do groups[#groups + 1] = c .. c:lower() end for _, group in ipairs(groups) do local list = chars(group) VARIANTS[list[1]] = list end local function clamp(v, lo, hi) return math.max(lo, math.min(hi, v)) end local function lerp(a, b, p) return a + (b - a) * p end local function ease_in(p) return p * p end local function seg(t, from, len) return clamp((t - from) / len, 0, 1) end function NameEntry.new(is_2p) local self = setmetatable({}, NameEntry) self.is_2p = is_2p self.side = is_2p and 1 or 0 self.tex, self.info = {}, {} for _, name in ipairs({ "yard", "header_bar", "header", "bottom_bar", "help", "help_icon", "help_arrow", "tile", "slot_cursor", "glyph", "glyph_small", "table", "table_shadow", "done_button", "done_label", "cell_cursor", "drum", "don", "katsu" }) do self.tex[name] = tex.get_texture(PATH .. name) local info = tex.get_texture_info("t14_name", name) self.info[name] = info and { info.x[1] or 0, info.y[1] or 0, info.x2[1] or 0, info.y2[1] or 0 } or { 0, 0, 0, 0 } end self.chara = is_2p and "katsu" or "don" self.kana = tex.language() == "ja" self.heads = {} for _, row in ipairs(ROWS[self.kana and "ja" or "latin"]) do self.heads[#self.heads + 1] = chars(row) end self.texts = {} self.name = {} self.popped = {} self.done = false self.t0 = tex.get_current_ms() self.level, self.row, self.base = 1, 1, nil self.cursor = 1 self.cheer_at = -1e9 self.help_vo = false return self end function NameEntry:f() return (tex.get_current_ms() - self.t0) / FRAME_MS end function NameEntry:update(current_ms) if not self.help_vo and self:f() >= HELP_VO_AT then self.help_vo = true if audio.has_sound("name_help_vo") then audio.play_sound("name_help_vo", "voice") end end end function NameEntry:is_done() return self.done end function NameEntry:get_name() return table.concat(self.name) end -- the cells of the current level: { glyph = index } or { text = str }, with what deciding them does function NameEntry:cells() local out = {} local function cell(c, act) local g = glyph_index(c) out[#out + 1] = { glyph = g, text = not g and c or nil, act = act, char = c } end if self.level == 1 then for i, row in ipairs(self.heads) do cell(row[1], { "row", i }) end if self.kana then cell("ー", { "add" }) end out[#out + 1] = { glyph = LEFT_ARROW, act = { "delete" } } out[#out + 1] = { glyph = RIGHT_ARROW, act = { "to_done" } } while #out < DONE_CELL - 1 do out[#out + 1] = { blank = true } end out[DONE_CELL] = { done = true, act = { "done" } } return out end local list = self.level == 2 and self.heads[self.row] or VARIANTS[self.base] for _, c in ipairs(list) do local forms = VARIANTS[c] cell(c, (self.level == 2 and forms and #forms > 1) and { "forms", c } or { "add" }) end out[#out + 1] = { glyph = BACK, act = { "back" } } return out end function NameEntry:selectable(cell) return cell and not cell.blank end function NameEntry:move(dir) local cells = self:cells() local i = self.cursor repeat i = (i - 1 + dir) % #cells + 1 until self:selectable(cells[i]) self.cursor = i end function NameEntry:input(action) if self:f() < INPUT_AT or self.done then return end if action == 1 then return self:move(-1) end if action == 2 then return self:move(1) end if action ~= 3 then return end local cell = self:cells()[self.cursor] if not cell or not cell.act then return end local act = cell.act[1] if act == "row" then self.level, self.row, self.cursor = 2, cell.act[2], 1 elseif act == "forms" then self.level, self.base, self.cursor = 3, cell.act[2], 1 elseif act == "add" then if #self.name < MAX_LEN then self.name[#self.name + 1] = cell.char self.popped[#self.name] = self:f() self.cheer_at = self:f() end if self.level > 1 then self.level, self.cursor = 1, self.row end if cell.char == "ー" then self.cursor = #self.heads + 1 end elseif act == "back" then if self.level == 3 then self.level, self.cursor = 2, 1 else self.level, self.cursor = 1, self.row end elseif act == "delete" then self.name[#self.name] = nil elseif act == "to_done" then self.cursor = DONE_CELL elseif act == "done" then self.done = true end end function NameEntry:tl(name, x, y, o) o = o or {} local t = self.tex[name] if not t then return end local i = self.info[name] local s = o.s or 1 tex.draw_texture(t, { frame = o.frame or 0, x = x * K - i[1] - i[3] * (s - 1) / 2, y = y * K - i[2] - i[4] * (s - 1) / 2, x2 = i[3] * (s - 1), y2 = i[4] * (s - 1), fade = o.fade }) end function NameEntry:text(str, size, cx, cy, fade) local key = str .. size local t = self.texts[key] if not t then t = text.create_raw_text(str, math.floor(size * K), WHITE, BLACK, false, 4) self.texts[key] = t end t:draw({ x = cx * K - t.width / 2, y = cy * K - t.height / 2, fade = fade }) end function NameEntry:draw_chara(f) self:tl("drum", DRUM_X, DRUM_Y) local y = f < 20 and lerp(-300, CHARA_Y, ease_in(f / 20)) or CHARA_Y + 3 * math.sin(math.pi * 2 * (f - 20) / 60) local cheer = f - self.cheer_at < 12 if cheer then y = CHARA_Y - 12 * math.sin(math.pi * (f - self.cheer_at) / 12) end self:tl(self.chara, CHARA_X, y, { frame = cheer and 1 or 0 }) end function NameEntry:draw_tiles(f) for i = 1, MAX_LEN do local x = TILE_X + (i - 1) * TILE_PITCH self:tl("tile", x, TILE_Y, { frame = self.side }) local c = self.name[i] if c then local s = lerp(1.25, 1, seg(f, self.popped[i] or 0, 6)) local g = glyph_index(c) if g then self:tl("glyph", x + 8, TILE_Y + 8.5, { frame = g, s = s }) else self:text(c, 34, x + 36, TILE_Y + 36) end elseif i == #self.name + 1 then self:tl("slot_cursor", x + 4, TILE_Y + 4, { fade = 0.25 + 0.2 * math.sin(math.pi * 2 * f / 40) }) end end end function NameEntry:draw_table(f) local fade = seg(f, TABLE_AT, 10) if fade <= 0 then return end self:tl("table_shadow", 27, 70.3, { fade = 0.21 * fade }) self:tl("table", 27, 70.3, { fade = fade }) local pulse = 0.75 + 0.25 * math.sin(math.pi * 2 * f / 30) for i, cell in ipairs(self:cells()) do local col, row = (i - 1) % PER_ROW, math.floor((i - 1) / PER_ROW) local x, y = CELL_X + col * CELL_PITCH, CELL_Y + row * CELL_ROW local here = i == self.cursor if cell.done then if here then self:tl("cell_cursor", DONE_X + 8, DONE_Y + 8, { s = 1.6, fade = pulse * fade }) end self:tl("done_button", DONE_X, DONE_Y, { fade = fade }) self:tl("done_label", DONE_X, DONE_Y, { fade = fade }) elseif not cell.blank then if here then self:tl("cell_cursor", x - 8, y - 8, { fade = pulse * fade }) end if cell.glyph then self:tl("glyph_small", x, y, { frame = cell.glyph, s = here and 1.15 or 1, fade = fade }) else self:text(cell.text, 20, x + 16, y + 16, fade) end end end end function NameEntry:draw() local f = self:f() self:tl("yard", 0, 0) self:draw_chara(f) self:draw_tiles(f) self:draw_table(f) self:tl("header_bar", 0, -1) self:tl("header", 16.3, 12.4, { frame = self.side }) self:tl("bottom_bar", 0, 440) self:tl("help", 123.7, 399.8) self:tl("help_icon", 181.2, 403.5, { frame = 1 }) self:tl("help_icon", 181.2, 403.5, { frame = 0, fade = 0.46 * (0.5 + 0.5 * math.sin(math.pi * 2 * f / 40)) }) self:tl("help_icon", 354.1, 403.5, { frame = 2 }) end