Project1

标题: 关于日志脚本无法显示中文的问题 [打印本页]

作者: rr302788751    时间: 2019-1-25 15:52
标题: 关于日志脚本无法显示中文的问题


# =============================================================================
# TheoAllen - 故事日志
# Version : 1.1
# Contact : www.rpgmakerid.com (or) http://www.theolized.com
# (This script documentation is written in informal indonesian language)
# =============================================================================
($imported ||={})[:Theo_StoryJournal] = true
# =============================================================================
# Change Logs:
# -----------------------------------------------------------------------------
# 2014.10.21 - Now supported for encrypted project
#            - Added bitmap cache for faster drawing process
#            - Added journal number to support multi parties journal entry
#            - Added word wrap
# 2013.09.26 - Finished documentation
# 2013.09.25 - Finished script
# =============================================================================
=begin

  介绍 :
  本脚本可以在游戏里显示一大段文本作为故事或日志。
  
  ---------------------------------------------------------------------------
  使用方法 :
  本脚本放在插件脚本之下,main之上
  在 Data 文件夹里新建一个 Journal.txt 文件,在里面写日志,格式如下:
  
  
[Title] 日志的标题
内容 内容 内容 内容 内容
内容 内容 内容 内容 内容
内容 内容 内容 内容 内容
<P>2333333内容 内容 内容 内容 内容
内容 内容 内容 内容 内容
内容 内容 内容 内容 内容
  
  使用 <P> 代表来在不同页中创建日志
  可以使用文本代码如 \C[1], \N[1], \V[1] 等 ...
  
  例如 :(拉丁语??)
  ---------------------
[Title] Lipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in ultrices
nulla, vel pharetra diam. Nunc faucibus pretium leo, a lacinia erat posuere
ut. Etiam porttitor enim et bibendum facilisis. Praesent eu elit ut metus
tempus aliquet. Cras tempor mauris sapien, ut laoreet quam condimentum ut.
<P>Aliquam aliquet ut quam vel sollicitudin. Fusce vestibulum semper sapien
nec luctus. Sed rhoncus, neque vitae
  
  ---------------------
  添加/移除日志、清空日志的脚本 :
  - add_journal("标题")
  - remove_journal("标题")
  - clear_journal
  
  如果游戏中你使用多个队伍,你可以使用脚本存储或清空日志内容:
  
  - journal_number(n)
  
  n替换为数字,默认值为1。当你在脚本中将n替换为2时,日志将会被清空,替换为1时
  之前的内容又会重新出现。
  
  进入日志画面的脚本:
  - SceneManager.call(Story_Journal)
  
  ---------------------------------------------------------------------------
  使用规定 :
  署名脚本作者, TheoAllen. 你可以自由编辑此脚本,只要你不声明你是脚本的原作者
  如果你想用此脚本于商业游戏,请和我共享收益.别忘了给我一份免费的游戏拷贝.

  特别鸣谢 :
  - Tsukihime 关于如何引用外部文件的文章
  
=end
# =============================================================================
# 设定 :
# =============================================================================
module Theo
  module Journal
  # --------------------------------------------------------------------------
  # 通用设定
  # --------------------------------------------------------------------------
    InstantRefresh  = true
  # false时,如果要改变日志内容,需要玩家按下行动键。true时,任何标题、内容的
  # 变化都会立即体现(无法理解? 自己试一试就知道)
   
    MainMenu        = true
  # true时, 可以在主菜单中进入日志画面
  
    DisplayPage     = true
  # 对于那些不想在日志中显示"页"的人,可以设定为false
  
    WordWrap        = true
  # 是否开启WordWrap,效果为当文字过长时会自动换行。
  
    ListWidth       = 200
  # 标题列表的窗口宽度
  
  # --------------------------------------------------------------------------
  # 用于
  # --------------------------------------------------------------------------
    VocabPage     = "页码"    # 页码
    VocabCommand  = "日志"      # 主菜单中,日志指令的名称
   
  end
end
# =============================================================================
# 设定结束 ~ !
# =============================================================================
class Game_Interpreter
  
  def add_journal(title)
    return if $game_system.journal_keys.include?(title)
    $game_system.journal_keys.push(title)
  end
  
  def clear_journal
    $game_system.journal_keys.clear
  end
  
  def remove_journal(title)
    $game_system.journal_keys.delete(title)
  end
  
  def journal_number(number)
    $game_system.journal_number = number
  end
  
end

# -----------------------------------------------------------------------------
# Updates version 1.1
# Window content caches
# -----------------------------------------------------------------------------
class << Cache
  
  def journal_cache(key, bmp)
    @journal ||= {}
    @journal[key] = Bitmap.new(bmp.width, bmp.height)
    @journal[key].blt(0,0,bmp,bmp.rect)
  end
  
  def journal_bmp(key)
    @journal ||= {}
    @journal[key]
  end
  
  def journal_clear
    @journal ||= {}
  end
  
end

class << Marshal
  alias theo_storyjournal_load load
  def load(port, proc = nil)
    theo_storyjournal_load(port, proc)  
  rescue TypeError
    if port.kind_of?(File)
      port.rewind
      port.read
    else
      port
    end
  end
end

class << DataManager
  
  def open_journal_text
    $journal = {}
    key = ""
    load_data("Data/Journal.txt").split(/[\r\n]+/).each do |txt|
      next if txt =~ /(^\s*(#|\/\/).*|^\s*$)/
      if txt =~ /^\[Title\]\s*(.+)/i
        key = $1.to_s
        $journal[key] = ""
      else
        txt.gsub!(/\[line\]/i) {"\n"}
        txt.gsub!(/\[page\]/i) {"<P>"}
        $journal[key] += txt
      end
    end
  end
  
  alias theo_storyjournal_load_db load_database
  def load_database
    open_journal_text
    theo_storyjournal_load_db
  end
  
end

class Game_System
  attr_accessor :journal_number
  
  alias theo_storyjournal_init initialize
  def initialize
    theo_storyjournal_init
    @journal_number = 1
    @journals = {}
  end
  
  def journal_keys
    @journals[@journal_number] ||= []
  end
  
end

class Window_JournalList < Window_Selectable
  
  def initialize
    super(0,0,window_width,window_height)
    refresh
    activate
    select(0)
  end
  
  def title_window=(window)
    @title_window = window
    update_help
  end
  
  def window_width
    return Theo::Journal::ListWidth
  end
  
  def window_height
    return Graphics.height
  end
  
  def item_max
    return [$game_system.journal_keys.size,1].max
  end
  
  def draw_item(index)
    rect = item_rect(index)
    rect.x += 4
    draw_text(rect, $game_system.journal_keys[index])
  end
  
  def journal_contents
    $journal[journal_title] || ""
  end
  
  def journal_title
    $game_system.journal_keys[index] || ""
  end
  
  def update_help
    return unless Theo::Journal::InstantRefresh
    if @help_window
      @help_window.set_title(journal_title)
    end
    @title_window.set_title(journal_title) if @title_window
  end
  
end

class Window_JournalTitle < Window_Base
  
  def initialize(x)
    super(x,0,Graphics.width - x,fitting_height(1))
    set_title("")
  end
  
  def set_title(str)
    @title = str
    refresh
  end
  
  def refresh
    contents.clear
    draw_text(contents.rect,@title,1)
  end
  
end

class Window_JournalContents < Window_Base
  
  def initialize(xpos,ypos)
    super(xpos,ypos,Graphics.width - xpos, Graphics.height - ypos)
    @page = 0
    @texts = []
    @title = "方正像素15"
  end
  
  def set_title(title)
    @title = title
    load_journal_data(journal_contents)
  end
  
  def journal_contents
    $journal[@title] || ""
  end
  
  def load_journal_data(str)
    @page = 0
    @texts = str.split(/<P>/i)
    refresh
  end
  
  def get_cache
    Cache.journal_bmp([@title, @page])
  end
  
  def set_cache
    Cache.journal_cache([@title, @page], contents)
  end
  
  def refresh
    contents.clear
    if get_cache
      draw_cached_bitmap
    else
      draw_contents
      draw_current_page if Theo::Journal::DisplayPage
      set_cache
    end
  end
  
  def draw_cached_bitmap
    begin
      contents.blt(0,0,get_cache, get_cache.rect)
    rescue
      Cache.journal_clear
      draw_contents
      draw_current_page if Theo::Journal::DisplayPage
      set_cache
    end
  end
  
  def draw_contents
    draw_text_ex(4,0,@texts[@page])
  end
  
  def draw_current_page
    reset_font_settings
    ypos = contents.height - line_height
    contents.fill_rect(0,ypos-2,contents.width,2,Color.new(255,255,255,128))
    rect = Rect.new(4,ypos,contents.width-4,line_height)
    pg = sprintf("%d / %d",@page+1,@texts.size)
    draw_text(rect,Theo::Journal::VocabPage)
    draw_text(rect,pg,2)
  end
  
  def update
    super
    next_page if Input.trigger?(:RIGHT)
    prev_page if Input.trigger?(:LEFT)
  end
  
  def next_page
    return if @page + 1 >= @texts.size
    @page += 1
    refresh
  end
  
  def prev_page
    return if @page + 1 <= 1
    @page -= 1
    refresh
  end
  
  def process_character(c, text, pos)
    @text = text
    if Theo::Journal::WordWrap && text[0] != ' '
      w = text_size(get_word(text)).width + 6
      if (pos[:x] + w) >= contents.width
        process_new_line(text, pos)
        return
      end
    end
    super
  end
  
  def get_word(text)
    result = ''
    text.each_char do |c|
      break if c =~ /\s/
      result += c
    end
    result
  end
  
end

class Window_MenuCommand < Window_Command
  
  alias theo_storyjournal_add_ori_cmd add_original_commands
  def add_original_commands
    theo_storyjournal_add_ori_cmd
    return unless Theo::Journal::MainMenu
    add_command(Theo::Journal::VocabCommand, :storyjournal)
  end
  
end

class Scene_Menu < Scene_MenuBase
  
  alias theo_storyjournal_cmnd_window create_command_window
  def create_command_window
    theo_storyjournal_cmnd_window
    @command_window.set_handler(:storyjournal, method(:enter_journal))
  end
  
  def enter_journal
    SceneManager.call(Story_Journal)
  end
  
end

class Story_Journal < Scene_MenuBase
  
  def start
    super
    create_journal_list
    create_journal_title
    create_journal_contents
  end
  
  def create_journal_list
    @list = Window_JournalList.new
    @list.set_handler(:ok, method(:on_journal_ok))
    @list.set_handler(:cancel, method(:return_scene))
  end
  
  def create_journal_title
    @title = Window_JournalTitle.new(@list.width)
    @title.set_title(@list.journal_title)
  end
  
  def create_journal_contents
    @contents = Window_JournalContents.new(@list.width,@title.height)
    @contents.load_journal_data(@list.journal_contents)
    @list.help_window = @contents
    @list.title_window = @title
  end
  
  def on_journal_ok
    @list.activate
    return if Theo::Journal::InstantRefresh
    @title.set_title(@list.journal_title)
    @contents.load_journal_data(@list.journal_contents)
  end
  
end

就是我换了好多次,只要内容输入
中文,就会这样,无法显示:

请问一下如何解决呢?QAQ感激不尽……【小白
作者: 张咚咚    时间: 2019-1-25 16:18
搜索load_data("Data/Journal.txt")
替换为File.new("Data/Journal.txt").read

然后txt文本选择转为UTF-8在保存。


作者: rr302788751    时间: 2019-1-25 17:30
张咚咚 发表于 2019-1-25 16:18
搜索load_data("Data/Journal.txt")
替换为File.new("Data/Journal.txt").read

好的!非常感谢你!!




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1