KK20

TDS HUD [XP] - Switch Visible Toggle

Aug 13th, 2024
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 62.79 KB | None | 0 0
  1.  
  2. module DesignHUD
  3.   # This HUD uses an easy to use format for configuration much like html
  4.   #   it has many types of tags, each inserting different information into the
  5.   #   HUD, such as icons, hp/sp/xp bars, and different information about the
  6.   #   actors.
  7.   #
  8.   # Tags:
  9.   #
  10.   #  Formatting Elements:
  11.   #   * <i></i>
  12.   #     - italicizes text
  13.   #   * <b></b>
  14.   #     - bolds text
  15.   #   * <c=R:G:B[:A]></c>
  16.   #     - changes the color to Color.new(R, G, B, A) (A is unnecessary)
  17.   #   * <font=NAME:SIZE></font>
  18.   #     - changes the font to NAME and the font size to SIZE
  19.   #  Graphical Elements:
  20.   #   * <ico=FILENAME>
  21.   #     - inserts the icon named FILENAME
  22.   #   * <pic=FILENAME>
  23.   #     - inserts the picture named FILENAME
  24.   #   * <bar style=STYLE type=TYPE cstyle=COLORSTYLE1:COLORSTYLE2... text=TEXT
  25.   #      neaten=NEATEN lines=LINES shape=SHAPE dimensions=WIDTH:HEIGHT>
  26.   #     - inserts a gradient bar with certain parameters that you can customize
  27.   #     - types: :hp, :mp, :exp, :lvl
  28.   #     - styles: 0 - 34
  29.   #         0-6 - Linear top-to-bottom
  30.   #           some are ease in or ease out or bilinear (starts from center)
  31.   #         7-13 - Linear top-to-bottom stripes
  32.   #           same as 0-6 except it uses stripes defined by four color styles
  33.   #             instead of 2
  34.   #         14-34 - Linear left-to-right
  35.   #           some are ease in or ease out or bilinear or fade out on the top
  36.   #             and bottom
  37.   #     - shapes: 0 - 3
  38.   #         0 - Rectangular
  39.   #         1 - Slant Left
  40.   #         2 - Rounded
  41.   #         3 - Slant Right
  42.   #     - color styles: 0 - 12
  43.   #         color styles define the two endpoints of the gradient's colors
  44.   #           for styles 7 - 13 use four color styles
  45.   #         main two colors defined by the type
  46.   #           :hp - Red and Green
  47.   #           :mp - Red and Blue
  48.   #           :exp - Orange and Yellow
  49.   #           :lvl - Blue and Purple
  50.   #         two mixed colors are defined by the percentage of the bar remaining
  51.   #           Mixed1 - At 0% full, Color1, at 100% full, Color2
  52.   #           Mixed2 - At 0% full, Color2, at 100% full, Color1
  53.   #         real colors defined by color style
  54.   #           0 - Color1
  55.   #           1 - Color2
  56.   #           2 - Black
  57.   #           3 - Mixed1
  58.   #           4 - Mixed2
  59.   #           5 - Highlight of Color1
  60.   #           6 - Highlight of Color2
  61.   #           7 - Highlight of Mixed1
  62.   #           8 - Highlight of Mixed2
  63.   #           9 - Shadow of Color1
  64.   #           10 - Shadow of Color2
  65.   #           11 - Shadow of Mixed1
  66.   #           12 - Shadow of Mixed2
  67.   #  Positioning Elements:
  68.   #   * <pos x=VALX y=VALY></pos>
  69.   #     - positions the contents at an x position of VALX and
  70.   #         at a y position of VALY
  71.   #   * <offset x=VALX y=VALY></offset>
  72.   #     - adds VALX and VALY to the position of the contents
  73.   #   * <table></table>
  74.   #     - starts a table
  75.   #   * <tr></tr>
  76.   #     - adds a row to a table
  77.   #   * <td></td>
  78.   #     - adds a cell to a row of a table
  79.   #   * <left></left>
  80.   #     - aligns the contents to the left
  81.   #   * <center></center>
  82.   #     - aligns the contents in the center
  83.   #   * <right></right>
  84.   #     - aligns the contents to the right
  85.   #   * <ln></ln>
  86.   #     - denotes one line in the HUD
  87.   #   * <width=VAL></width>
  88.   #     - sets the width of the contents to VAL
  89.   #   * <height=VAL></height>
  90.   #     - sets the height of the contents to VAL
  91.   #  HUD Properties:
  92.   #   * <hud>
  93.   #     - this tag has many properties, listed below
  94.   #   * windowskin='FILENAME'
  95.   #     - sets the windowskin of the HUD to the windowskin named FILENAME
  96.   #   * bg='FILENAME'
  97.   #     - sets the background image of the HUD to the picture named FILENAME
  98.   #   * align=ALIGN
  99.   #     - sets the align of the HUD to ALIGN
  100.   #     - possible aligns: TL, TC, TR, ML, MC, MR, BL, BC, BR (top-left,
  101.   #         top-center, top-right, middle-left, middle-center, middle-right,
  102.   #         bottom-left, bottom-center, bottom-right)
  103.   #   * offsetx=VAL
  104.   #     - sets the xoffset of the HUD to VAL
  105.   #   * offsety=VAL
  106.   #     - and the yoffset of the HUD to VAL
  107.   #   * width=VAL
  108.   #     - sets the width of the HUD to VAL
  109.   #   * height=VAL
  110.   #     - sets the height of the HUD to VAL
  111.   #   * back_opacity=VAL
  112.   #     - sets the back opacity of the HUD to VAL
  113.   #   * contents_opacity=VAL
  114.   #     - sets the contents opacity of the HUD to VAL
  115.   #   * opacity=VAL
  116.   #     - sets the opacity of the HUD to VAL
  117.  
  118.   HUD = <<-HUD
  119.     <hud width=220 align=TL offsetx=16 offsety=16 opacity=0>
  120.     <ln><center><var=name></center></ln>
  121.     <ln>HP: <right><bar neaten=true lines=true text=false style=20 cstyle=0:3 shape=2></right></ln>
  122.     <ln>SP: <right><bar type=:mp neaten=true lines=true text=false style=20 cstyle=0:3 shape=2></right></ln>
  123.     <ln>EXP: <right><bar type=:exp neaten=true lines=true text=false style=20 cstyle=0:3 shape=2></right></ln>
  124.     <ln>Level: <right><bar type=:lvl neaten=true lines=true text=false style=20 cstyle=0:3 shape=2></right></ln>
  125.   HUD
  126.  
  127.   NO_END = ['ico', 'pic', 'hud', 'var', 'bar', 'shape']
  128.  
  129.   BLOCK_ELS = ['ln']
  130.  
  131.   HUD_TOGGLE_SWITCH_ID = 1
  132.  
  133.   def self.var_expression(name)
  134.     case name
  135.     when 'gold' then return proc { $game_party.gold }
  136.     when 'name' then return proc { $game_party.actors[0].name }
  137.     else return proc {}
  138.     end
  139.   end
  140.  
  141.   SPATTERN = /<([\w\-]+)(?:\s*=\s*([\w\"\'\-\_\:\$\.\s\+]+))?((?:\s*\w+\s*=\s*[\w\"\'\-\_\:\$\.\s\+]+)*)>/
  142. end
  143. class HUD < Window_Base
  144.  attr_accessor :tmp_attrs
  145.  def initialize(text)
  146.    super(0, 0, 33, 33)
  147.    self.contents = Bitmap.new(1, 1)
  148.    self.visible = false
  149.    @align = 'TL'
  150.    @xoffset, @yoffset = 0, 0
  151.    @tmp_attrs = {}
  152.    @text = text
  153.    Element.reset_id
  154.    parse_text
  155.    setup_hud
  156.    setup_align
  157.    @dom.delete_if { |el| el.el_type == 'hud' }
  158.    self.width = [self.width, 33].max
  159.    self.height = [self.height, 33].max
  160.    self.contents = Bitmap.new(width - 32, height - 32)
  161.    tmp = self.contents
  162.    render_hud
  163.  end
  164.  
  165.  
  166.  def update
  167.    redim = []
  168.    vars = []
  169.    redraw = []
  170.    @var_nodes.each { |el|
  171.      val = el.val
  172.      if val != el.attrs[:val]
  173.        vars.push(el)
  174.        redim |= [el.last_line]
  175.        pos = el.attrs[:pos]
  176.        rect = el.attrs[:size]
  177.        rect.x, rect.y = *pos
  178.        rect.x += el.attrs[:ox]
  179.        rect.y += el.attrs[:oy]
  180.        self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  181.        el.attrs[:val] = val
  182.      end
  183.    }
  184.    @dom.each { |el|
  185.      comp = false
  186.      el.each_child_ex2(proc { |child|
  187.        comp = false
  188.      }) { |child|
  189.        if child.attrs[:size] and !comp
  190.          pos = child.attrs[:pos]
  191.          rect = child.attrs[:size]
  192.          rect.x, rect.y = *pos
  193.          rect.x += child.attrs[:ox] if child.attrs[:ox]
  194.          rect.y += child.attrs[:oy] if child.attrs[:oy]
  195.          if vars.select { |el|
  196.            pos = el.attrs[:pos]
  197.            tmp = el.attrs[:size]
  198.            tmp.x, tmp.y = *pos
  199.            tmp.x += el.attrs[:ox] if el.attrs[:ox]
  200.            tmp.y += el.attrs[:oy] if el.attrs[:oy]
  201.            tmp.intersect?(rect)
  202.          }.length > 0
  203.            redraw.push(child)
  204.            self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  205.            comp = true
  206.          end
  207.        end
  208.      }
  209.    }
  210.    redraw ||= vars
  211.    
  212.    if redim.length > 0
  213.      redetermine_size(redim)
  214.      setup_align
  215.    end
  216.    if redraw.length > 0
  217.      redraw_hud(redraw)
  218.    end
  219.  end
  220.  
  221.  def redetermine_size(els)
  222.    @tmp_attrs[:ox] = 0
  223.    @tmp_attrs[:oy] = 0
  224.    
  225.    pos = [0, 0]
  226.    
  227.    @dom.each { |el|
  228.      ln_height = ln_width = 0
  229.    
  230.      if els.include?(el)
  231.        pos = el.attrs[:pos].clone unless @tmp_attrs[:tmp]
  232.        if @tmp_attrs[:tmp]
  233.          @tmp_attrs[:tmp].push(true)
  234.        else
  235.          @tmp_attrs[:tmp] = [true]
  236.        end
  237.      end
  238.  
  239.      case el.el_type
  240.      when 'ln'
  241.        el.each_child_ex(proc { |child|
  242.          if @tmp_attrs[:tmp]
  243.            unless child.attrs[:size].nil?
  244.              rect = child.attrs[:size]
  245.              rect.x, rect.y = *child.attrs[:pos]
  246.              self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  247.            end
  248.            if @tmp_attrs[:abs_pos]
  249.              child.attrs[:pos] = [@tmp_attrs[:abs_pos].last[0], @tmp_attrs[:abs_pos].last[1]]
  250.            else
  251.              child.attrs[:pos] = [pos[0], pos[1]]
  252.            end
  253.            child.attrs[:ox], child.attrs[:oy] = @tmp_attrs[:ox], @tmp_attrs[:oy]
  254.          end
  255.          if els.include?(child)
  256.            pos = child.attrs[:pos].clone unless @tmp_attrs[:tmp]
  257.            if @tmp_attrs[:tmp]
  258.              @tmp_attrs[:tmp].push(true)
  259.            else
  260.              @tmp_attrs[:tmp] = [true]
  261.            end
  262.          end
  263.          case child
  264.          when FormatNode
  265.            tmp = child.attrs[:start].call(child, self)
  266.            child.attrs = tmp if tmp and tmp.is_a?(Hash)
  267.          end
  268.        },
  269.        proc { |child|
  270.          case child
  271.          when FormatNode
  272.            tmp = child.attrs[:finish].call(child, self)
  273.            child.attrs = tmp if tmp and tmp.is_a?(Hash)
  274.          end
  275.          if @tmp_attrs[:tmp]
  276.            if els.include?(child)
  277.              @tmp_attrs[:tmp].pop
  278.              @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  279.            end
  280.          end
  281.          unless child.children.empty?
  282.            resize(child)
  283.          end
  284.        }) { |child|
  285.          if @tmp_attrs[:tmp]
  286.            case child
  287.            when TextNode
  288.              size = self.contents.text_size(child.attrs[:val])
  289.              child.attrs[:size] = size
  290.              ln_height = [size.height, ln_height].max
  291.              if @tmp_attrs[:abs_pos]
  292.                tmp = @tmp_attrs[:abs_pos]
  293.                ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  294.                @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  295.              else
  296.                ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  297.                pos[0] += size.width
  298.              end
  299.            when VariableNode
  300.              size = self.contents.text_size(child.val.to_s)
  301.              child.attrs[:size] = size
  302.              ln_height = [size.height, ln_height].max
  303.              if @tmp_attrs[:abs_pos]
  304.                tmp = @tmp_attrs[:abs_pos]
  305.                ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  306.                @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  307.              else
  308.                ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  309.                pos[0] += size.width
  310.              end
  311.            when IconNode, PictureNode, ShapeNode
  312.              size = child.attrs[:bitmap].rect
  313.              child.attrs[:size] = size
  314.              ln_height = [size.height, ln_height].max
  315.              if @tmp_attrs[:abs_pos]
  316.                tmp = @tmp_attrs[:abs_pos]
  317.                ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  318.                @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  319.              else
  320.                ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  321.                pos[0] += size.width
  322.              end
  323.            when BarNode
  324.              size = Rect.new(0,0,*child.attrs[:dimensions])
  325.              child.attrs[:size] = size
  326.              ln_height = [size.height, ln_height].max
  327.              if @tmp_attrs[:abs_pos]
  328.                tmp = @tmp_attrs[:abs_pos]
  329.                ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  330.                @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  331.              else
  332.                ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  333.                pos[0] += size.width
  334.              end
  335.            end
  336.          end
  337.        }
  338.      end
  339.      
  340.      if @tmp_attrs[:tmp]
  341.        if els.include?(el)
  342.          @tmp_attrs[:tmp].pop
  343.          @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  344.        end
  345.      end
  346.    }
  347.    @dom.each { |child|
  348.      if child.block?
  349.        child.attrs[:size].width = self.width - 32
  350.        child.attrs[:size].height = child.attrs[:height] || child.attrs[:size].height
  351.        child.attrs[:size].width = child.attrs[:width] || child.attrs[:size].width
  352.      end
  353.    }
  354.  end
  355.  
  356.  def redraw_hud(els)
  357.    @tmp_attrs = {}
  358.    
  359.    @dom.each { |el|
  360.    
  361.      align = 0
  362.      
  363.      case el.el_type
  364.      when 'ln'
  365.        if els.include?(el)
  366.          pos = el.attrs[:pos].clone unless @tmp_attrs[:tmp]
  367.          if @tmp_attrs[:tmp]
  368.            @tmp_attrs[:tmp].push(true)
  369.          else
  370.            @tmp_attrs[:tmp] = [true]
  371.          end
  372.        end
  373.        el.each_child_ex(proc { |child|
  374.          if els.include?(child)
  375.            pos = child.attrs[:pos].clone unless @tmp_attrs[:tmp]
  376.            if @tmp_attrs[:tmp]
  377.              @tmp_attrs[:tmp].push(true)
  378.            else
  379.              @tmp_attrs[:tmp] = [true]
  380.            end
  381.          end
  382.          case child
  383.          when FormatNode
  384.            child.attrs[:start].call(child, self)
  385.          end
  386.        },
  387.        proc { |child|
  388.          case child
  389.          when FormatNode
  390.            child.attrs[:finish].call(child, self)
  391.          end
  392.          if @tmp_attrs[:tmp]
  393.            if els.include?(child)
  394.              @tmp_attrs[:tmp].pop
  395.              @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  396.            end
  397.          end
  398.        }) { |child|
  399.          if @tmp_attrs[:tmp]
  400.            draw_node(child)
  401.          end
  402.        }
  403.      end
  404.    }
  405.  end
  406.  
  407.  def draw_node(node)
  408.    case node
  409.    when TextNode
  410.      pos = node.attrs[:pos]
  411.      ox, oy = node.attrs[:ox], node.attrs[:oy]
  412.      text = node.attrs[:val]
  413.      size = node.attrs[:size]
  414.      self.contents.draw_text(Rect.new(pos[0] + ox, pos[1] + oy, size.width, size.height), text)
  415.    when VariableNode
  416.      pos = node.attrs[:pos]
  417.      ox, oy = node.attrs[:ox], node.attrs[:oy]
  418.      text = node.val.to_s
  419.      size = node.attrs[:size]
  420.      self.contents.draw_text(Rect.new(pos[0] + ox, pos[1] + oy, size.width, size.height), text)
  421.    when IconNode, PictureNode, ShapeNode
  422.      pos = node.attrs[:pos]
  423.      ox, oy = node.attrs[:ox], node.attrs[:oy]
  424.      bitmap = node.attrs[:bitmap]
  425.      self.contents.blt(pos[0] + ox, pos[1] + oy, bitmap, bitmap.rect)
  426.    when BarNode
  427.      ln = node.last_line
  428.      pos = node.attrs[:pos]
  429.      ox, oy = node.attrs[:ox], node.attrs[:oy]
  430.      size = node.attrs[:size]
  431.      oy += (ln.attrs[:size].height - size.height) / 2
  432.      type = node.attrs[:type]
  433.      style = node.attrs[:style]
  434.      shape = node.attrs[:shape]
  435.      cstyle = node.attrs[:cstyle]
  436.      text = node.attrs[:text]
  437.      lines = node.attrs[:lines]
  438.      neaten = node.attrs[:neaten]
  439.      actor = node.attrs[:actor]
  440.      self.contents.gradient_bar(type, style, shape, cstyle, actor, pos[0] + ox, pos[1] + oy, size.width, size.height, text, lines, neaten)
  441.    end
  442.  end
  443.  
  444.  def parse_text
  445.    text = @text.clone
  446.    @dom, @var_nodes = [], []
  447.    index = 0
  448.    current_id = -1
  449.    current_el = nil
  450.    parent = nil
  451.    parent_els = []
  452.    spattern = /<([\w\-]+)(?:\s*=\s*([\w\"\'\-\_\:\$\.\s\+]+))?((?:\s*\w+\s*=\s*[\w\"\'\-\_\:\$\.\s]+)*)>/
  453.    epattern = /<\/[\w\-]+>/
  454.    while text.length != 0
  455.      s_ind = text.index(DesignHUD::SPATTERN)
  456.      if s_ind != nil
  457.        contents = nil
  458.        el_name = $1
  459.        el_val = $2 || ''
  460.        el_attrs = {}
  461.        tmp = $3.strip
  462.        tmp.gsub(/(\w+)\s+=\s+([\w\"\'\-\:\+]+)/, "\\1=\\2").split(/\s+/).each { |el| el_attrs[el.split('=')[0].to_sym] = get_val(el.split('=')[1]) }
  463.        el_val.gsub!(/[\'\"]/, '') if el_val
  464.        el_attrs[:val] = el_val
  465.        level, tmp_name, tmp_ind = 0, '', s_ind
  466.        if !DesignHUD::NO_END.include?(el_name)
  467.          loop do
  468.            text.sub!(DesignHUD::SPATTERN, '')
  469.            tmp = [text.index(/(<#{el_name}(?:=[\w\"\'\-\:]+)?(?:\s*\w+=[\w\"\'\-\:]+)*>)/, tmp_ind)]
  470.            tmp.push(text.index(/<\/#{el_name}>/, tmp_ind))
  471.            if !tmp[1]
  472.              contents = text[s_ind...text.length - 4 - el_name.length]
  473.              text = ''
  474.            elsif !tmp[0]
  475.              contents = text[s_ind...text.length - 4 - el_name.length]
  476.              text = ''
  477.            elsif tmp[1] < tmp[0]
  478.              contents = text[s_ind...tmp[1]]
  479.              text = text[tmp[1]...text.length]
  480.            else
  481.              level += 1
  482.              tmp_ind = tmp[0] + 2 + el_name.length
  483.            end
  484.            break if contents != nil
  485.          end
  486.          case el_name
  487.          when 'ln'
  488.            element = ContainerNode.new('ln', el_attrs, nil, contents)
  489.            @dom.push(element)
  490.          end
  491.        else
  492.          text.sub!(DesignHUD::SPATTERN, '')
  493.          case el_name
  494.          when 'hud'
  495.            element = HudNode.new('hud', el_attrs)
  496.            @dom.push(element)
  497.          end
  498.        end
  499.      end
  500.    end
  501.  end
  502.  
  503.  def setup_hud
  504.    @bg = Sprite.new
  505.    @bg.z = self.z - 1
  506.    @bg.visible = false
  507.    @dom.each { |el|
  508.      el.each_child { |tmp|
  509.        if tmp.el_type == 'hud'
  510.          self.width = tmp.attrs[:width].to_i if tmp.attrs[:width]
  511.          self.height = tmp.attrs[:height].to_i if tmp.attrs[:height]
  512.          @align = tmp.attrs[:align] if tmp.attrs[:align]
  513.          @xoffset = tmp.attrs[:offsetx].to_i if tmp.attrs[:offsetx]
  514.          @yoffset = tmp.attrs[:offsety].to_i if tmp.attrs[:offsety]
  515.          self.opacity = tmp.attrs[:opacity].to_i if tmp.attrs[:opacity]
  516.          self.back_opacity = tmp.attrs[:back_opacity].to_i if tmp.attrs[:back_opacity]
  517.          self.contents_opacity = tmp.attrs[:contents_opacity].to_i if tmp.attrs[:contents_opacity]
  518.          begin
  519.            self.windowskin = ((tmp.attrs[:windowskin] == '') ? Bitmap.new(1, 1) : RPG::Cache.windowskin(tmp.attrs[:windowskin])) if tmp.attrs[:windowskin]
  520.          rescue
  521.            self.windowskin = Bitmap.new(1, 1)
  522.          end
  523.          if tmp.attrs[:bg]
  524.            @bg.bitmap = RPG::Cache.picture(tmp.attrs[:bg])
  525.            @bg.visible = true
  526.          end
  527.        end
  528.      }
  529.    }
  530.    
  531.    tmp_width, tmp_height = get_dimensions
  532.    
  533.    if self.width == 33
  534.      self.width = [tmp_width, 1].max + 32
  535.    end
  536.    if self.height == 33
  537.      self.height = [tmp_height, 1].max + 32
  538.    end
  539.    amnt = 0
  540.    @dom.each_with_index { |child, ind|
  541.      if child.block?
  542.        child.attrs[:size].width = self.width - 32
  543.        #child.attrs[:size].height = child.attrs[:height] || child.attrs[:size].height
  544.        child.attrs[:size].width = child.attrs[:width] || child.attrs[:size].width
  545.        if child.attrs[:height]
  546.          tmp = child.attrs[:size].height
  547.          amnt += child.attrs[:height] - tmp
  548.          child.attrs[:size].height = child.attrs[:height]
  549.          unless ind == @dom.length - 1
  550.            @dom[ind + [email protected]].each { |i|
  551.              i.attrs[:pos][1] += child.attrs[:height] - tmp
  552.            }
  553.          end
  554.        end
  555.      end
  556.    }
  557.    self.height += amnt
  558.    @align.downcase!
  559.    xmod = (['l','c'].include?(@align[1..1]) ? 1 : -1)
  560.    ymod = (['t','m'].include?(@align[0..0]) ? 1 : -1)
  561.    xstrt = case @align[1..1]
  562.    when 'l' then 0
  563.    when 'r' then 640 - self.width
  564.    when 'c' then (640 - self.width) / 2
  565.    end
  566.    ystrt = case @align[0..0]
  567.    when 't' then 0
  568.    when 'b' then 480 - self.height
  569.    when 'm' then (480 - self.height) / 2
  570.    end
  571.    self.x = xstrt + xmod * @xoffset
  572.    self.y = ystrt + ymod * @yoffset
  573.    if @bg.visible
  574.      @bg.x = self.x + (self.width - @bg.bitmap.width) / 2
  575.      @bg.y = self.y + (self.height - @bg.bitmap.height) / 2
  576.    end
  577.    self.visible = $game_switches[HUD_TOGGLE_SWITCH_ID]
  578.  end
  579.  
  580.  def get_dimensions
  581.    ln_widths = []
  582.    height = 0
  583.    @tmp_attrs[:ox] = 0
  584.    @tmp_attrs[:oy] = 0
  585.    @tmp_attrs[:align] = 0
  586.    
  587.    pos = [0, 0]
  588.    
  589.    @dom.each { |el|
  590.    
  591.      pos[0] = 0
  592.      ln_width = 0
  593.      ln_height = 0
  594.      
  595.      case el.el_type
  596.      when 'ln'
  597.        el.attrs[:pos] = pos.clone
  598.        el.attrs[:align] = @tmp_attrs[:abs_pos]
  599.        el.attrs[:abs] = 0
  600.        el.each_child_ex(proc { |child| # start proc
  601.          child.attrs[:align] = (@tmp_attrs[:abs_pos] ? 0 : @tmp_attrs[:align])
  602.          if @tmp_attrs[:abs_pos]
  603.            child.attrs[:pos] = @tmp_attrs[:abs_pos].last.clone
  604.            child.attrs[:abs] = @tmp_attrs[:abs_pos].length
  605.          else
  606.            child.attrs[:pos] = pos.clone
  607.            child.attrs[:abs] = 0
  608.          end
  609.          child.attrs[:ox], child.attrs[:oy] = @tmp_attrs[:ox], @tmp_attrs[:oy]
  610.          case child
  611.          when FormatNode
  612.            tmp = child.attrs[:start].call(child, self)
  613.            child.attrs = tmp if tmp and tmp.is_a?(Hash)
  614.          end
  615.        },
  616.        proc { |child| # end proc
  617.          case child
  618.          when FormatNode
  619.            tmp = child.attrs[:finish].call(child, self)
  620.            child.attrs = tmp if tmp and tmp.is_a?(Hash)
  621.          end
  622.          if !child.children.empty?
  623.            resize(child)
  624.          elsif child.block? or ![TextNode, VariableNode, IconNode, PictureNode, BarNode, ShapeNode].include?(child.class)
  625.            child.attrs[:size] = Rect.new(0, 0, 0, 0)
  626.          end
  627.        }) { |child| # main proc
  628.          case child
  629.          when TextNode
  630.            size = self.contents.text_size(child.attrs[:val])
  631.            child.attrs[:size] = size
  632.            ln_height = [size.height, ln_height].max
  633.            if @tmp_attrs[:abs_pos]
  634.              tmp = @tmp_attrs[:abs_pos]
  635.              ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  636.              @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  637.            else
  638.              ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  639.              pos[0] += size.width
  640.            end
  641.          when VariableNode
  642.            size = self.contents.text_size(child.val.to_s)
  643.            child.attrs[:size] = size
  644.            ln_height = [size.height, ln_height].max
  645.            if @tmp_attrs[:abs_pos]
  646.              tmp = @tmp_attrs[:abs_pos]
  647.              ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  648.              @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  649.            else
  650.              ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  651.              pos[0] += size.width
  652.            end
  653.            @var_nodes.push(child)
  654.          when IconNode, PictureNode, ShapeNode
  655.            size = child.attrs[:bitmap].rect
  656.            child.attrs[:size] = size
  657.            ln_height = [size.height, ln_height].max
  658.            if @tmp_attrs[:abs_pos]
  659.              tmp = @tmp_attrs[:abs_pos]
  660.              ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  661.              @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  662.            else
  663.              ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  664.              pos[0] += size.width
  665.            end
  666.          when BarNode
  667.            size = Rect.new(0,0,*child.attrs[:dimensions])
  668.            child.attrs[:size] = size
  669.            ln_height = [size.height, ln_height].max
  670.            if @tmp_attrs[:abs_pos]
  671.              tmp = @tmp_attrs[:abs_pos]
  672.              ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  673.              @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  674.            else
  675.              ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  676.              pos[0] += size.width
  677.            end
  678.            @var_nodes.push(child)
  679.          end
  680.        }
  681.      end
  682.      
  683.      pos[1] += ln_height + @tmp_attrs[:oy]
  684.      ln_widths.push(ln_width)
  685.    }
  686.    
  687.    height = pos[1]
  688.    width = ln_widths.max
  689.    return width, height
  690.  end
  691.  
  692.  def resize(el)
  693.    size = Rect.new(0, 0, 0, 0)
  694.    x1, y1 = 640, 480
  695.    x2 = y2 = 0
  696.    len = el.attrs[:abs]
  697.    el.children.each { |i|
  698.      next if i.attrs[:abs] > len
  699.      x, y = *i.attrs[:pos]
  700.      ns = i.attrs[:size]
  701.      x1 = [x1, x].min
  702.      y1 = [y1, y].min
  703.      x2 = [x2, x + ns.width].max
  704.      y2 = [y2, y + ns.height].max
  705.    }
  706.    size.width = x2 - x1
  707.    size.height = y2 - y1
  708.    pos = el.block? ? el.attrs[:pos] : [x1, y1]
  709.    el.attrs[:pos], el.attrs[:size] = pos, size
  710.  end
  711.  
  712.  def setup_align
  713.    aligned = nil
  714.    la = 0
  715.    blocks = []
  716.    @dom.each { |el|
  717.      case el.el_type
  718.      when 'ln'
  719.        el.each_child_ex(proc{|child|},proc{|child|
  720.          if child.attrs[:tmp]
  721.            child.attrs.delete_if { |i| i[0] == :tmp }
  722.            aligned = nil
  723.          end
  724.        }) { |child|
  725.          unless child.attrs[:abs] > 0 or !aligned.nil?
  726.            aligned = true
  727.            child.attrs[:tmp] = true
  728.            la = child.attrs[:align]
  729.            blocks |= [child.last_block]
  730.            child.last_block.attrs[:aels] = (child.last_block.attrs[:aels] || []).concat([child])
  731.          end
  732.        }
  733.      end
  734.    }
  735.    blocks.each { |el|
  736.      rw = el.attrs[:aels].inject(0) { |s, n| s + (n.attrs[:align] == 2 ? n.attrs[:size].width : 0) }
  737.      cw = el.attrs[:aels].inject(0) { |s, n| s + (n.attrs[:align] == 1 ? n.attrs[:size].width : 0) }
  738.      lx = el.attrs[:pos][0]
  739.      rx = el.attrs[:pos][0] + el.attrs[:size].width - rw
  740.      cx = el.attrs[:pos][0] + (el.attrs[:size].width) / 2
  741.      el.attrs[:aels].each { |child|
  742.        case child.attrs[:align]
  743.        when 0
  744.          ox = child.attrs[:pos][0]
  745.          child.attrs[:pos][0] = lx
  746.          child.each_child_ex(proc{},proc{}) { |i|
  747.            i.attrs[:pos][0] += lx - ox
  748.          }
  749.          lx += child.attrs[:size].width
  750.        when 1
  751.          cx -= child.attrs[:size].width / 2
  752.        when 2
  753.          ox = child.attrs[:pos][0]
  754.          child.attrs[:pos][0] = rx
  755.          child.each_child_ex(proc{},proc{}) { |i|
  756.            i.attrs[:pos][0] += rx - ox
  757.          }
  758.          rx += child.attrs[:size].width
  759.        end
  760.      }
  761.      el.attrs[:aels].select { |i| i.attrs[:align] == 1 }.each { |child|
  762.        ox = child.attrs[:pos][0]
  763.        child.attrs[:pos][0] = cx
  764.        child.each_child_ex(proc{},proc{}) { |i|
  765.          i.attrs[:pos][0] += cx - ox
  766.        }
  767.        cx += child.attrs[:size].width
  768.      }
  769.      el.attrs.delete(:aels)
  770.    }
  771.  end
  772.  
  773.  def render_hud
  774.    
  775.    @dom.each { |el|
  776.    
  777.      align = 0
  778.    
  779.      case el.el_type
  780.      when 'ln'
  781.        el.each_child_ex(proc { |child|
  782.          case child
  783.          when FormatNode
  784.            child.attrs[:start].call(child, self)
  785.          end
  786.        },
  787.        proc { |child|
  788.          case child
  789.            when FormatNode
  790.              child.attrs[:finish].call(child, self)
  791.          end
  792.        }) { |child|
  793.          draw_node(child)
  794.        }
  795.      end
  796.    }
  797.  end
  798.  
  799.  def get_val(str)
  800.    separators = [':', '.', ',']
  801.    val = nil
  802.    vp = []
  803.    sp = []
  804.    loop do
  805.      tmpstr = eval('str' + sp.map{|i|'['+i.to_s+']'}.join(''))
  806.      if tmpstr.nil?
  807.        sp.pop
  808.        vp.pop
  809.        if vp.length == 0
  810.          break
  811.        end
  812.        sp[-1] += 1
  813.        vp[-1] += 1
  814.        next
  815.      end
  816.      if tmpstr.index(separators[sp.length]) != nil
  817.        begin
  818.          eval('val' + vp.map{|i|'['+i.to_s+']'}.join('') + "=eval('#{tmpstr}')")
  819.          if sp.length > 0 and sp.last < eval('str' + sp[0..sp.length - 2].map{|i|'['+i.to_s+']'}.join('')).length
  820.            sp[-1] += 1
  821.            vp[-1] += 1
  822.          else
  823.            break if vp.length == 0
  824.            vp.pop
  825.            sp.pop
  826.            break if vp.length == 0
  827.            sp[-1] += 1
  828.            vp[-1] += 1
  829.          end
  830.        rescue Exception
  831.          eval('str' + sp.map{|i|'['+i.to_s+']'}.join('') + '=tmpstr.split(separators[sp.length])')
  832.          eval('val' + vp.map{|i|'['+i.to_s+']'}.join('') + '=[]')
  833.          vp.push(0)
  834.          sp.push(0)
  835.        end
  836.      else
  837.        begin
  838.          eval('val' + vp.map{|i|'['+i.to_s+']'}.join('') + "=eval('#{tmpstr}')")
  839.        rescue Exception
  840.          eval('val' + vp.map{|i|'['+i.to_s+']'}.join('') + '=tmpstr')
  841.        end
  842.        etxt = 'str'
  843.        etxt += sp[0..sp.length - 2].map{|i|'['+i.to_s+']'}.join('') if sp.length > 1
  844.        if sp.length > 0 and sp.last < eval(etxt).length
  845.          sp[-1] += 1
  846.          vp[-1] += 1
  847.        else
  848.          break if vp.length == 0
  849.          vp.pop
  850.          sp.pop
  851.          break if vp.length == 0
  852.          sp[-1] += 1
  853.          vp[-1] += 1
  854.        end
  855.      end
  856.    end
  857.    val
  858.  end
  859.  
  860.  def get_el(path)
  861.    el = @dom[path[0]]
  862.    if path.length > 1
  863.      path[1..path.length - 1].each { |tmp|
  864.        el = el.children[tmp]
  865.      }
  866.    end
  867.    el
  868.  end
  869. end
  870.  
  871. class Element
  872.  @@id = 0
  873.  attr_accessor :children, :el_type, :attrs, :el_id, :parent
  874.  
  875.  def initialize(el_type, attrs, parent)
  876.    @children = []
  877.    @el_type = el_type.downcase
  878.    @attrs = attrs
  879.    @parent = parent
  880.    @contents = ''
  881.    @el_id = @@id
  882.    @@id += 1
  883.  end
  884.  
  885.  def get_val(str)
  886.    separators = [':', '.', ',']
  887.    val = nil
  888.    vp = []
  889.    sp = []
  890.    loop do
  891.      tmpstr = eval('str' + sp.map{|i|'['+i.to_s+']'}.join(''))
  892.      if tmpstr.nil?
  893.        sp.pop
  894.        vp.pop
  895.        if vp.length == 0
  896.          break
  897.        end
  898.        sp[-1] += 1
  899.        vp[-1] += 1
  900.        next
  901.      end
  902.      if tmpstr.index(separators[sp.length]) != nil
  903.        begin
  904.          eval('val' + vp.map{|i|'['+i.to_s+']'}.join('') + "=eval('#{tmpstr}')")
  905.          if sp.length > 0 and sp.last < eval('str' + sp[0..sp.length - 2].map{|i|'['+i.to_s+']'}.join('')).length
  906.            sp[-1] += 1
  907.            vp[-1] += 1
  908.          else
  909.            break if vp.length == 0
  910.            vp.pop
  911.            sp.pop
  912.            break if vp.length == 0
  913.            sp[-1] += 1
  914.            vp[-1] += 1
  915.          end
  916.        rescue Exception
  917.          eval('str' + sp.map{|i|'['+i.to_s+']'}.join('') + '=tmpstr.split(separators[sp.length])')
  918.          eval('val' + vp.map{|i|'['+i.to_s+']'}.join('') + '=[]')
  919.          vp.push(0)
  920.          sp.push(0)
  921.        end
  922.      else
  923.        begin
  924.          eval('val' + vp.map{|i|'['+i.to_s+']'}.join('') + "=eval('#{tmpstr}')")
  925.        rescue Exception
  926.          eval('val' + vp.map{|i|'['+i.to_s+']'}.join('') + '=tmpstr')
  927.        end
  928.        etxt = 'str'
  929.        etxt += sp[0..sp.length - 2].map{|i|'['+i.to_s+']'}.join('') if sp.length > 1
  930.        if sp.length > 0 and sp.last < eval(etxt).length
  931.          sp[-1] += 1
  932.          vp[-1] += 1
  933.        else
  934.          break if vp.length == 0
  935.          vp.pop
  936.          sp.pop
  937.          break if vp.length == 0
  938.          sp[-1] += 1
  939.          vp[-1] += 1
  940.        end
  941.      end
  942.    end
  943.    val
  944.  end
  945.  
  946.  def block?
  947.    return DesignHUD::BLOCK_ELS.include?(@el_type)
  948.  end
  949.  
  950.  def inline?
  951.    return !DesignHUD::BLOCK_ELS.include?(@el_type)
  952.  end
  953.  
  954.  def self.reset_id
  955.    @@id = 0
  956.  end
  957.  
  958.  def parents
  959.    if @parent != nil
  960.      parents = [@parent]
  961.      parent = @parent
  962.      while parent.parent != nil
  963.        parents.push(parent.parent)
  964.        parent = parent.parent
  965.      end
  966.      return parents
  967.    else
  968.      return []
  969.    end
  970.  end
  971.  
  972.  def last_block
  973.    if @parent != nil
  974.      last = self
  975.      parent = @parent
  976.      while last.inline?
  977.        last = parent
  978.        parent = parent.parent
  979.        break if parent.nil?
  980.      end
  981.      return last
  982.    else
  983.      return nil
  984.    end
  985.  end
  986.  
  987.  def last_line
  988.    if @parent != nil
  989.      last = self
  990.      parent = @parent
  991.      while last.el_type != 'ln'
  992.        last = parent
  993.        parent = parent.parent
  994.        break if parent.nil?
  995.      end
  996.      return last
  997.    else
  998.      return nil
  999.    end
  1000.  end
  1001.  
  1002.  def each_child(&block)
  1003.    block.call(self)
  1004.    path = [0]
  1005.    cur = self
  1006.    loop {
  1007.      child = cur.children[path.last]
  1008.      if child
  1009.        block.call(child)
  1010.        cur = child
  1011.        path.push(0)
  1012.      else
  1013.        cur = cur.parent
  1014.        path.pop
  1015.        break unless cur
  1016.        path[path.length - 1] += 1
  1017.      end
  1018.    }
  1019.  end
  1020.  
  1021.  def each_child_ex(start, finish, &block)
  1022.    path = [0]
  1023.    cur = self
  1024.    loop {
  1025.      child = cur.children[path.last]
  1026.      if child
  1027.        start.call(child)
  1028.        block.call(child)
  1029.        cur = child
  1030.        path.push(0)
  1031.      else
  1032.        finish.call(cur)
  1033.        cur = cur.parent
  1034.        path.pop
  1035.        break unless cur and path.length > 0
  1036.        path[path.length-1] += 1
  1037.      end
  1038.    }
  1039.  end
  1040.  
  1041.  def each_child_ex2(finish, &block)
  1042.    block.call(self)
  1043.    path = [0]
  1044.    cur = self
  1045.    loop {
  1046.      child = cur.children[path.last]
  1047.      if child
  1048.        block.call(child)
  1049.        cur = child
  1050.        path.push(0)
  1051.      else
  1052.        finish.call(cur)
  1053.        cur = cur.parent
  1054.        path.pop
  1055.        break unless cur
  1056.        path[path.length - 1] += 1
  1057.      end
  1058.    }
  1059.  end
  1060.  
  1061.  def parse_contents(text)
  1062.    @tmp_attrs = {}
  1063.    spattern = /<([\w\-]+)(?:\s*=\s*([\w\"\'\-\_\:\$\.\s]+))?((?:\s*\w+\s*=\s*[\w\"\'\-\_\:\$\.\s]+)*)>/
  1064.    epattern = /<\/[\w\-]+>/
  1065.    while text != ''
  1066.      s_ind = text.index(DesignHUD::SPATTERN)
  1067.      if s_ind != nil
  1068.        if s_ind > 0
  1069.          contents = text[0...s_ind]
  1070.          text = text[s_ind...text.length]
  1071.          element = TextNode.new('', self, contents)
  1072.          @children.push(element)
  1073.        end
  1074.        contents = nil
  1075.        el_name = $1
  1076.        el_val = $2 || ''
  1077.        el_attrs = {}
  1078.        tmp = $3.strip
  1079.        tmp.gsub(/(\w+)\s+=\s+([\w\"\'\-\:]+)/, "\\1=\\2").split(/\s+/).each { |el| el_attrs[el.split('=')[0].to_sym] = get_val(el.split('=')[1]) }
  1080.        el_val.gsub!(/[\'\"]/, '') if el_val
  1081.        el_attrs[:val] = el_val
  1082.        level, tmp_name, tmp_ind = 0, '', 0
  1083.        if !DesignHUD::NO_END.include?(el_name)
  1084.          text.sub!(DesignHUD::SPATTERN, '')
  1085.          last_ind = nil
  1086.          loop do
  1087.            tmp = [text.index(/(<#{el_name}(?:=[\w\"\'\-\:]+)?(?:\s*\w+=[\w\"\'\-\:]+)*>)/, tmp_ind)]
  1088.            tmp2 = tmp_ind
  1089.            (level+1).times {
  1090.              i = text.index(/<\/#{el_name}>/, tmp2)
  1091.              tmp2 = i + 2 + el_name.length unless i.nil?
  1092.            }
  1093.            tmp.push(tmp2 - 2 - el_name.length)
  1094.            if !tmp[1]
  1095.              contents = text[0...text.length]
  1096.              text = ''
  1097.            elsif !tmp[0]
  1098.              contents = text[0...tmp[1]]
  1099.              text = text[tmp[1] + 3 + el_name.length...text.length]
  1100.              level = [level - 1, 0].max
  1101.            elsif tmp[1] < tmp[0]
  1102.              contents = text[0...tmp[1]]
  1103.              text = text[tmp[1] + 3 + el_name.length...text.length]
  1104.              level = [level - 1, 0].max
  1105.            else
  1106.              level += 1 unless last_ind != nil and tmp[0] > last_ind[1]
  1107.              tmp_ind = tmp[0] + 2 + el_name.length
  1108.            end
  1109.            last_ind = tmp
  1110.            break if contents != nil
  1111.          end
  1112.          case el_name
  1113.          when 'ln'
  1114.            element = ContainerNode.new('ln', el_attrs, self, contents)
  1115.            @children.push(element)
  1116.          when 'b'
  1117.            element = FormatNode.new('b', self, contents, proc { |child, hud|
  1118.              if !@tmp_attrs[:tmpb]
  1119.                @tmp_attrs[:tmpb] = [hud.contents.font.bold]
  1120.              else
  1121.                @tmp_attrs[:tmpb].push(hud.contents.font.bold)
  1122.              end
  1123.              hud.contents.font.bold = true
  1124.            },
  1125.            proc { |child, hud|
  1126.              hud.contents.font.bold = @tmp_attrs[:tmpb].pop
  1127.            })
  1128.            @children.push(element)
  1129.          when 'i'
  1130.            element = FormatNode.new('i', self, contents, proc { |child, hud|
  1131.              if !@tmp_attrs[:tmpi]
  1132.                @tmp_attrs[:tmpi] = [hud.contents.font.italic]
  1133.              else
  1134.                @tmp_attrs[:tmpi].push(hud.contents.font.italic)
  1135.              end
  1136.              hud.contents.font.italic = true
  1137.            },
  1138.            proc { |child, hud|
  1139.              hud.contents.font.italic = @tmp_attrs[:tmpi].pop
  1140.            })
  1141.            @children.push(element)
  1142.          when 'c'
  1143.            element = FormatNode.new('c', self, contents, proc { |child, hud|
  1144.              if !hud.tmp_attrs[:tmpc]
  1145.                hud.tmp_attrs[:tmpc] = [hud.contents.font.color.clone]
  1146.              else
  1147.                hud.tmp_attrs[:tmpc].push(hud.contents.font.color.clone)
  1148.              end
  1149.              hud.contents.font.color = Color.new(*child.attrs[:val].split(':').map { |i| i.to_i })
  1150.            },
  1151.            proc { |child, hud|
  1152.              hud.contents.font.color = hud.tmp_attrs[:tmpc].pop
  1153.            }, el_val)
  1154.            @children.push(element)
  1155.          when 'font'
  1156.            element = FormatNode.new('font', self, contents, proc { |child, hud|
  1157.              if !@tmp_attrs[:tmpfn]
  1158.                hud.tmp_attrs[:tmpfn] = [[hud.contents.font.name.clone, hud.contents.font.size]]
  1159.              else
  1160.                hud.tmp_attrs[:tmpfn].push([hud.contents.font.name.clone, hud.contents.font.size])
  1161.              end
  1162.              if Font.exist?(child.attrs[:val].split(':')[0])
  1163.                hud.contents.font.name = child.attrs[:val].split(':')[0]
  1164.                hud.contents.font.size = child.attrs[:val].split(':')[1].to_i
  1165.              end
  1166.            },
  1167.            proc { |child, hud|
  1168.              hud.contents.font.name, hud.contents.font.size = *hud.tmp_attrs[:tmpfn].pop
  1169.            }, el_val)
  1170.            @children.push(element)
  1171.          when 'pos'
  1172.            element = FormatNode.new('pos', self, contents, proc { |child, hud|
  1173.            if !hud.tmp_attrs[:abs_pos]
  1174.                hud.tmp_attrs[:abs_pos] = [[child.attrs[:x], child.attrs[:y]]]
  1175.              else
  1176.                hud.tmp_attrs[:abs_pos].push([child.attrs[:x], child.attrs[:y]])
  1177.              end
  1178.            },
  1179.            proc { |child, hud|
  1180.              hud.tmp_attrs[:abs_pos].pop
  1181.              hud.tmp_attrs.delete(:abs_pos) if hud.tmp_attrs[:abs_pos].length == 0
  1182.            }, el_val, el_attrs)
  1183.            @children.push(element)
  1184.          when 'offset'
  1185.            element = FormatNode.new('offset', self, contents, proc { |child, hud|
  1186.              if !hud.tmp_attrs[:tmpofst]
  1187.                hud.tmp_attrs[:tmpofst] = [[hud.tmp_attrs[:ox], hud.tmp_attrs[:oy]]]
  1188.              else
  1189.                hud.tmp_attrs[:tmpofst].push([hud.tmp_attrs[:ox], hud.tmp_attrs[:oy]])
  1190.              end
  1191.              hud.tmp_attrs[:ox], hud.tmp_attrs[:oy] = child.attrs[:x], child.attrs[:y]
  1192.            },
  1193.            proc { |child, hud|
  1194.              hud.tmp_attrs[:ox], hud.tmp_attrs[:oy] = *hud.tmp_attrs[:tmpofst].pop
  1195.            }, el_val, el_attrs)
  1196.            @children.push(element)
  1197.          when 'right'
  1198.            element = FormatNode.new('right', self, contents, proc { |child, hud|
  1199.              if !hud.tmp_attrs[:tmpalign]
  1200.                hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1201.              else
  1202.                hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1203.              end
  1204.              hud.tmp_attrs[:align] = 2
  1205.              child.attrs[:align] = 2
  1206.            },
  1207.            proc { |child, hud|
  1208.              hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1209.            }, el_val, el_attrs)
  1210.            @children.push(element)
  1211.          when 'center'
  1212.            element = FormatNode.new('center', self, contents, proc { |child, hud|
  1213.              if !hud.tmp_attrs[:tmpalign]
  1214.                hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1215.              else
  1216.                hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1217.              end
  1218.              hud.tmp_attrs[:align] = 1
  1219.              child.attrs[:align] = 1
  1220.            },
  1221.            proc { |child, hud|
  1222.              hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1223.            }, el_val, el_attrs)
  1224.            @children.push(element)
  1225.          when 'left'
  1226.            element = FormatNode.new('left', self, contents, proc { |child, hud|
  1227.              if !hud.tmp_attrs[:tmpalign]
  1228.                hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1229.              else
  1230.                hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1231.              end
  1232.              hud.tmp_attrs[:align] = 0
  1233.              child.attrs[:align] = 0
  1234.            },
  1235.            proc { |child, hud|
  1236.              hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1237.            }, el_val, el_attrs)
  1238.            @children.push(element)
  1239.          end
  1240.        else
  1241.          text.sub!(DesignHUD::SPATTERN, '')
  1242.          case el_name
  1243.          when 'hud'
  1244.            element = HudNode.new('hud', el_attrs, self)
  1245.            @children.push(element)
  1246.          when 'var'
  1247.            element = VariableNode.new(self, el_val)
  1248.            @children.push(element)
  1249.          when 'ico'
  1250.            element = IconNode.new(self, el_val)
  1251.            @children.push(element)
  1252.          when 'pic'
  1253.            element = PictureNode.new(self, el_val)
  1254.            @children.push(element)
  1255.          when 'bar'
  1256.            element = BarNode.new(self, el_attrs)
  1257.            @children.push(element)
  1258.          when 'shape'
  1259.            element = ShapeNode.new(self, el_attrs)
  1260.            @children.push(element)
  1261.          end
  1262.        end
  1263.      else
  1264.        contents = text
  1265.        text = ''
  1266.        element = TextNode.new('', self, contents)
  1267.        @children.push(element)
  1268.      end
  1269.    end
  1270.  end
  1271. end
  1272.  
  1273. class TextNode < Element
  1274.  def initialize(type, parent, text)
  1275.    super(type, {}, parent)
  1276.    @attrs[:val] = text
  1277.  end
  1278. end
  1279.  
  1280. class PictureNode < Element
  1281.  def initialize(parent, val)
  1282.    super('pic', {}, parent)
  1283.    @attrs[:bitmap] = RPG::Cache.picture(val)
  1284.  end
  1285. end
  1286.  
  1287. class IconNode < Element
  1288.  def initialize(parent, val)
  1289.    super('ico', {}, parent)
  1290.    @attrs[:bitmap] = RPG::Cache.icon(el_val).clone
  1291.  end
  1292. end
  1293.  
  1294. class VariableNode < Element
  1295.  def initialize(parent, name)
  1296.    super('var', {}, parent)
  1297.    @attrs[:expr] = DesignHUD.var_expression(name)
  1298.    @attrs[:val] = @attrs[:expr].call || 0
  1299.  end
  1300.  
  1301.  def val
  1302.    tmp = @attrs[:expr].call
  1303.    return (tmp.nil? ? 0 : tmp)
  1304.  end
  1305. end
  1306.  
  1307. class ShapeNode < Element
  1308.  def initialize(parent, attrs)
  1309.    super('shape', attrs, parent)
  1310.    @attrs[:width] ||= 50
  1311.    @attrs[:height] ||= 50
  1312.    bitmap = Bitmap.new(@attrs[:width], @attrs[:height])
  1313.    case @attrs[:type].to_s.downcase
  1314.    when 'rect'
  1315.      @attrs[:color] ||= [0, 0, 0]
  1316.      bitmap.fill_rect(bitmap.rect, Color.new(*@attrs[:color]))
  1317.    when 'hgrect'
  1318.      @attrs[:color] ||= [[0, 0, 0], [255, 255, 255]]
  1319.      tmp = @attrs[:color]
  1320.      bitmap.width.times { |i|
  1321.        r = tmp[0][0] * (bitmap.width - 1 - i) / (bitmap.width - 1) + tmp[1][0] * i / (bitmap.width - 1)
  1322.        g = tmp[0][1] * (bitmap.width - 1 - i) / (bitmap.width - 1) + tmp[1][1] * i / (bitmap.width - 1)
  1323.        b = tmp[0][2] * (bitmap.width - 1 - i) / (bitmap.width - 1) + tmp[1][2] * i / (bitmap.width - 1)
  1324.        bitmap.fill_rect(i, 0, 1, bitmap.height, Color.new(r, g, b))
  1325.      }
  1326.    when 'vgrect'
  1327.      @attrs[:color] ||= [[0, 0, 0], [255, 255, 255]]
  1328.      tmp = @attrs[:color]
  1329.      bitmap.height.times { |i|
  1330.        r = tmp[0][0] * (bitmap.height - 1 - i) / (bitmap.height - 1) + tmp[1][0] * i / (bitmap.height - 1)
  1331.        g = tmp[0][1] * (bitmap.height - 1 - i) / (bitmap.height - 1) + tmp[1][1] * i / (bitmap.height - 1)
  1332.        b = tmp[0][2] * (bitmap.height - 1 - i) / (bitmap.height - 1) + tmp[1][2] * i / (bitmap.height - 1)
  1333.        bitmap.fill_rect(0, i, bitmap.width, 1, Color.new(r, g, b))
  1334.      }
  1335.    end
  1336.    @attrs[:bitmap] = bitmap
  1337.  end
  1338. end
  1339.  
  1340. class BarNode < Element
  1341.  def initialize(parent, attrs)
  1342.    super('bar', attrs, parent)
  1343.    @attrs[:type] ||= :hp
  1344.    @attrs[:style] ||= 0
  1345.    @attrs[:shape] ||= 0
  1346.    @attrs[:cstyle] ||= [3, 2, 4, 2]
  1347.    @attrs[:lines] = false if @attrs[:lines].nil?
  1348.    @attrs[:neaten] = true if @attrs[:neaten].nil?
  1349.    @attrs[:dimensions] ||= [120, 16]
  1350.    @attrs[:text] = true if @attrs[:text].nil?
  1351.    @attrs[:actor] ||= 1
  1352.    @attrs[:actor] = $game_actors[@attrs[:actor]]
  1353.    @attrs[:val] = val
  1354.  end
  1355.  
  1356.  def val
  1357.    case @attrs[:type]
  1358.    when :hp
  1359.      return [@attrs[:actor].hp, @attrs[:actor].maxhp]
  1360.    when :mp
  1361.      return [@attrs[:actor].sp, @attrs[:actor].maxsp]
  1362.    when :exp
  1363.      return @attrs[:actor].exp
  1364.    when :lvl
  1365.      return @attrs[:actor].level
  1366.    end
  1367.  end
  1368. end
  1369.  
  1370. class HudNode < Element
  1371.  def initialize(type, attrs, parent = nil)
  1372.    super('hud', attrs, parent)
  1373.  end
  1374. end
  1375.  
  1376. class ContainerNode < Element
  1377.  def initialize(type, attrs, parent, contents)
  1378.    super(type, attrs, parent)
  1379.    
  1380.    parse_contents(contents)
  1381.  end
  1382. end
  1383.  
  1384. class FormatNode < Element
  1385.  def initialize(type, parent, contents, start, finish, val=nil, attrs = {})
  1386.    super(type, attrs, parent)
  1387.    
  1388.    parse_contents(contents)
  1389.    @attrs[:val] = val if val
  1390.    @attrs[:start] = start
  1391.    @attrs[:finish] = finish
  1392.  end
  1393. end
  1394.  
  1395. class Rect
  1396.  def intersect?(rect)
  1397.    return false if rect.width * rect.height == 0 or self.width * self.height == 0
  1398.    return true if (self.x.between?(rect.x - 1, rect.x + rect.width) or
  1399.                   (self.x + self.width).between?(rect.x - 1, rect.x + rect.width)) and
  1400.                   (self.y.between?(rect.y - 1, rect.y + rect.height) or
  1401.                   (self.y + self.height).between?(rect.y - 1, rect.y + rect.height))
  1402.    return false
  1403.  end
  1404. end
  1405.  
  1406. class Bitmap
  1407.  def get_colors(type, actor, cstyle)
  1408.    bc = Color.new(0, 0, 0)
  1409.    case type
  1410.    when :hp
  1411.      c1 = Color.new(200, 0, 0)
  1412.      c2 = Color.new(0, 200, 0)
  1413.      p = actor.hp * 100 / actor.maxhp
  1414.    when :mp
  1415.      c1 = Color.new(200, 0, 0)
  1416.      c2 = Color.new(0, 0, 200)
  1417.      p = actor.sp * 100 / actor.maxsp
  1418.    when :exp
  1419.      c1 = Color.new(170, 85, 0)
  1420.      c2 = Color.new(150, 150, 0)
  1421.      elist = actor.instance_variable_get(:@exp_list)
  1422.      if elist[actor.level + 1]
  1423.        nexp = elist[actor.level + 1] - elist[actor.level]
  1424.        cexp = actor.exp - elist[actor.level]
  1425.        p = (nexp <= 0 ? 100 : cexp * 100 / nexp)
  1426.      else
  1427.        p = 100
  1428.      end
  1429.    when :lvl
  1430.      c1 = Color.new(0, 85, 170)
  1431.      c2 = Color.new(150, 0, 150)
  1432.      p = actor.level * 100 / (actor.respond_to?(:final_level) ? actor.final_level : $data_actors[actor.id].final_level)
  1433.    end
  1434.    r = c2.red * p / 100 + c1.red * (100 - p) / 100
  1435.    g = c2.green * p / 100 + c1.green * (100 - p) / 100
  1436.    b = c2.blue * p / 100 + c1.blue * (100 - p) / 100
  1437.    pc1 = Color.new(r, g, b)
  1438.    r = c1.red * p / 100 + c2.red * (100 - p) / 100
  1439.    g = c1.green * p / 100 + c2.green * (100 - p) / 100
  1440.    b = c1.blue * p / 100 + c2.blue * (100 - p) / 100
  1441.    pc2 = Color.new(r, g, b)
  1442.    c = cstyle.map { |i|
  1443.      case i
  1444.      when 0 then c1
  1445.      when 1 then c2
  1446.      when 2 then bc
  1447.      when 3 then pc1
  1448.      when 4 then pc2
  1449.      when 5 then lcol(c1)
  1450.      when 6 then lcol(c2)
  1451.      when 7 then lcol(pc1)
  1452.      when 8 then lcol(pc2)
  1453.      when 9 then dcol(c1)
  1454.      when 10 then dcol(c2)
  1455.      when 11 then dcol(pc1)
  1456.      when 12 then dcol(pc2)
  1457.      end
  1458.    }
  1459.    return c, bc
  1460.  end
  1461.  
  1462.  def lcol(c)
  1463.    return Color.new(c.red * 1.3, c.green * 1.3, c.blue * 1.3)
  1464.  end
  1465.  
  1466.  def dcol(c)
  1467.    return Color.new(c.red * 0.7, c.green * 0.7, c.blue * 0.7)
  1468.  end
  1469.  
  1470.  def gradient_bar_back(color, shape, x, y, w, h, lines, extend = true)
  1471.    rw = w
  1472.    w += 10 if extend
  1473.    fill_rect(x, y, w, h, color)
  1474.    case shape
  1475.    when 0
  1476.      unless lines
  1477.        fill_rect(x + 1, y + 1, rw - 2, 1, Color.new(255, 255, 255))
  1478.        fill_rect(x + 1, y + h - 2, rw - 2, 1, Color.new(255, 255, 255))
  1479.        fill_rect(x + 1, y + 1, 1, h - 2, Color.new(255, 255, 255))
  1480.        fill_rect(x + w - 2, y + 1, 1, h - 2, Color.new(255, 255, 255))
  1481.      end
  1482.    when 1
  1483.      h.times { |i|
  1484.        fill_rect(x + i, y + i + 1, 1, h - 1 - i, Color.new(0, 0, 0, 0))
  1485.        fill_rect(x + w - 1 - i, y, 1, h - 1 - i, Color.new(0, 0, 0, 0))
  1486.        set_pixel(x + i + 1, y + i, Color.new(255, 255, 255)) unless i == 0 or i == h - 1
  1487.      }
  1488.      fill_rect(x + h, y + h - 2, rw - 2 - h, 1, Color.new(255, 255, 255))
  1489.    when 2
  1490.      fill_rect(x, y, w, 1, Color.new(0, 0, 0, 127))
  1491.      fill_rect(x, y + h - 1, w, 1, Color.new(0, 0, 0, 127))
  1492.      t = (h / 2.0).floor
  1493.      h.times { |i|
  1494.        v = ((t + 1) * (1 - Math.sin(Math::PI * (i + 1) / (h + 1)))).floor
  1495.        unless v == 0
  1496.          fill_rect(x, y + i, v, 1, Color.new(0, 0, 0, 0))
  1497.          fill_rect(x + w - v, y + i, v, 1, Color.new(0, 0, 0, 0))
  1498.        end
  1499.        unless [0, h - 1].include?(i)
  1500.          set_pixel(x + v, y + i, Color.new(0, 0, 0, 85))
  1501.          set_pixel(x + w - v, y + i, Color.new(0, 0, 0, 85))
  1502.        end
  1503.      }
  1504.    when 3
  1505.      h.times { |i|
  1506.        fill_rect(x + i, y, 1, h - 1 - i, Color.new(0, 0, 0, 0))
  1507.        fill_rect(x + w - 1 - i, y + i + 1, 1, h - 1 - i, Color.new(0, 0, 0, 0))
  1508.        set_pixel(x + h - i, y + i, Color.new(255, 255, 255)) unless i == 0 or i == h - 1
  1509.      }
  1510.      fill_rect(x + 2, y + h - 2, w - (extend ? 7 : 2) - h, 1, Color.new(255, 255, 255))
  1511.      #w -= h - 1
  1512.    end
  1513.    if extend
  1514.      tmp = self.clone
  1515.      fill_rect(x + w - 20, y, 20, h, Color.new(0, 0, 0, 0))
  1516.      20.times { |i|
  1517.        blt(x + w - 20 + i, y, tmp, Rect.new(x + w - 20 + i, y, 1, h), 255 * (19 - i) / 19)
  1518.      }
  1519.    end
  1520.  end
  1521.  
  1522.  def gradient_bar_front(colors, bgcolor, style, shape, sx, sy, w, h, rate)
  1523.    sbmap = self.clone
  1524.    t = (h / 2.0).ceil
  1525.    if style.between?(0, 6)
  1526.      h.times { |i|
  1527.        rect = Rect.new(sx, sy + i, w * rate, 1)
  1528.        case shape
  1529.        when 1
  1530.          rect.width -= h - 1
  1531.          rect.x += i
  1532.        when 2
  1533.          t = (h / 2.0).floor
  1534.          v = ((t + 1) * (1 - Math.sin(Math::PI * (i + 1) / (h + 1)))).floor
  1535.          rect.x += v
  1536.          rect.width -= 2 * v
  1537.        when 3
  1538.          rect.width -= h - 1
  1539.          rect.x += h - 1 - i
  1540.        end
  1541.        case style
  1542.        when 0
  1543.          r = colors[1].red * i / (h - 1) + colors[0].red * (h - 1 - i) / (h - 1)
  1544.          g = colors[1].green * i / (h - 1) + colors[0].green * (h - 1 - i) / (h - 1)
  1545.          b = colors[1].blue * i / (h - 1) + colors[0].blue * (h - 1 - i) / (h - 1)
  1546.        when 1
  1547.          t = (h / 2.0).ceil
  1548.          if i >= t
  1549.            d = i - t + (h % 2 == 1 ? 1 : 0)
  1550.          else
  1551.            d = t - i - 1
  1552.          end
  1553.          t -= 1 if h % 2 == 1
  1554.          r = colors[1].red * d / (t) + colors[0].red * (t - d) / (t)
  1555.          g = colors[1].green * d / (t) + colors[0].green * (t - d) / (t)
  1556.          b = colors[1].blue * d / (t) + colors[0].blue * (t - d) / (t)
  1557.        when 2
  1558.          s = Math.sin(Math::PI * i / (h - 1) / 2)
  1559.          r = colors[1].red * s + colors[0].red * (1 - s)
  1560.          g = colors[1].green * s + colors[0].green * (1 - s)
  1561.          b = colors[1].blue * s + colors[0].blue * (1 - s)
  1562.        when 3
  1563.          s = 2 * Math.sin(Math::PI * i / (h - 1) / 6)
  1564.          r = colors[1].red * s + colors[0].red * (1 - s)
  1565.          g = colors[1].green * s + colors[0].green * (1 - s)
  1566.          b = colors[1].blue * s + colors[0].blue * (1 - s)
  1567.        when 4
  1568.          s = 1 - 2 * Math.sin(Math::PI * i / (h - 1) / 6)
  1569.          r = colors[0].red * s + colors[1].red * (1 - s)
  1570.          g = colors[0].green * s + colors[1].green * (1 - s)
  1571.          b = colors[0].blue * s + colors[1].blue * (1 - s)
  1572.        when 5
  1573.          s = Math.sin(Math::PI * i / (h - 1))
  1574.          r = colors[0].red * s + colors[1].red * (1 - s)
  1575.          g = colors[0].green * s + colors[1].green * (1 - s)
  1576.          b = colors[0].blue * s + colors[1].blue * (1 - s)
  1577.        when 6
  1578.          s = Math.sin(Math::PI / 2 + Math::PI * i / (h - 1)).abs
  1579.          r = colors[1].red * s + colors[0].red * (1 - s)
  1580.          g = colors[1].green * s + colors[0].green * (1 - s)
  1581.          b = colors[1].blue * s + colors[0].blue * (1 - s)
  1582.        end
  1583.        fill_rect(rect, Color.new(r, g, b))
  1584.      }
  1585.    elsif style.between?(7, 13)
  1586.      amnt = 8
  1587.      case shape
  1588.      when 1, 3
  1589.        w -= h - 1
  1590.      end
  1591.      at = (w.to_f * rate / amnt).ceil
  1592.      h.times { |i|
  1593.        at.times { |e|
  1594.          rect = Rect.new(sx + amnt * e, sy + i, amnt, 1)
  1595.          dw = w * rate
  1596.          case shape
  1597.          when 0
  1598.            rect.width = [rect.width, sx + dw - rect.x].min
  1599.          when 1
  1600.            rect.x += i
  1601.            rect.width = [rect.width, sx + dw + i - rect.x].min
  1602.          when 2
  1603.            v = ((t + 1) * (1 - Math.sin(Math::PI * (i + 1) / (h + 1)))).floor
  1604.            rect.x += v if e == 0
  1605.            tmp = 0
  1606.            if e == 0
  1607.              rect.width -= v
  1608.              tmp += v
  1609.            end
  1610.            if e == at - 1
  1611.              rect.width -= v
  1612.              tmp += v
  1613.            end
  1614.            rect.width = [rect.width, sx + dw - tmp - rect.x].min
  1615.          when 3
  1616.            rect.x += h - 1 - i
  1617.            rect.width = [rect.width, sx + dw + h - 1 - i - rect.x].min
  1618.          end
  1619.          tmpcolors = (e % 2 == 0 ? colors[0..1] : colors[2..3])
  1620.          case style
  1621.          when 7
  1622.            r = tmpcolors[1].red * i / (h - 1) + tmpcolors[0].red * (h - 1 - i) / (h - 1)
  1623.            g = tmpcolors[1].green * i / (h - 1) + tmpcolors[0].green * (h - 1 - i) / (h - 1)
  1624.            b = tmpcolors[1].blue * i / (h - 1) + tmpcolors[0].blue * (h - 1 - i) / (h - 1)
  1625.          when 8
  1626.            if i >= t
  1627.              d = i - t + (h % 2 == 1 ? 1 : 0)
  1628.            else
  1629.              d = t - i - 1
  1630.            end
  1631.            t -= 1 if h % 2 == 1
  1632.            r = tmpcolors[1].red * d / (t) + tmpcolors[0].red * (t - d) / (t)
  1633.            g = tmpcolors[1].green * d / (t) + tmpcolors[0].green * (t - d) / (t)
  1634.            b = tmpcolors[1].blue * d / (t) + tmpcolors[0].blue * (t - d) / (t)
  1635.          when 9
  1636.            s = Math.sin(Math::PI * i / (h - 1) / 2)
  1637.            r = tmpcolors[1].red * s + tmpcolors[0].red * (1 - s)
  1638.            g = tmpcolors[1].green * s + tmpcolors[0].green * (1 - s)
  1639.            b = tmpcolors[1].blue * s + tmpcolors[0].blue * (1 - s)
  1640.          when 10
  1641.            s = 2 * Math.sin(Math::PI * i / (h - 1) / 6)
  1642.            r = tmpcolors[1].red * s + tmpcolors[0].red * (1 - s)
  1643.            g = tmpcolors[1].green * s + tmpcolors[0].green * (1 - s)
  1644.            b = tmpcolors[1].blue * s + tmpcolors[0].blue * (1 - s)
  1645.          when 11
  1646.            s = 1 - 2 * Math.sin(Math::PI * i / (h - 1) / 6)
  1647.            r = tmpcolors[0].red * s + tmpcolors[1].red * (1 - s)
  1648.            g = tmpcolors[0].green * s + tmpcolors[1].green * (1 - s)
  1649.            b = tmpcolors[0].blue * s + tmpcolors[1].blue * (1 - s)
  1650.          when 12
  1651.            s = Math.sin(Math::PI * i / (h - 1))
  1652.            r = tmpcolors[0].red * s + tmpcolors[1].red * (1 - s)
  1653.            g = tmpcolors[0].green * s + tmpcolors[1].green * (1 - s)
  1654.            b = tmpcolors[0].blue * s + tmpcolors[1].blue * (1 - s)
  1655.          when 13
  1656.            s = Math.sin(Math::PI / 2 + Math::PI * i / (h - 1)).abs
  1657.            r = tmpcolors[1].red * s + tmpcolors[0].red * (1 - s)
  1658.            g = tmpcolors[1].green * s + tmpcolors[0].green * (1 - s)
  1659.            b = tmpcolors[1].blue * s + tmpcolors[0].blue * (1 - s)
  1660.          end
  1661.          fill_rect(rect, Color.new(r, g, b))
  1662.        }
  1663.      }
  1664.    elsif style.between?(14, 34)
  1665.      case shape
  1666.      when 2
  1667.        t = (h / 2.0).floor
  1668.        v = []
  1669.        h.times { |i|
  1670.          v.push(((t + 1) * (1 - Math.sin(Math::PI * (i + 1) / (h + 1)))).floor)
  1671.        }
  1672.      end
  1673.      dw = (w * rate).to_i
  1674.      dw.times { |i|
  1675.        rect = Rect.new(sx + i, sy, 1, h)
  1676.        case shape
  1677.        when 1
  1678.          if i < h
  1679.            rect.height -= h - 1 - i
  1680.          end
  1681.          if i > dw - h
  1682.            rect.y += i - dw + h
  1683.            rect.height -= i - dw + h
  1684.          end
  1685.        when 2
  1686.          if i <= v.max
  1687.            ind = v.index(v.detect { |val| val <= i })
  1688.            rect.y += ind
  1689.            rect.height -= ind * 2
  1690.          elsif w - 1 - i <= v.max
  1691.            ind = v.index(v.detect { |val| val <= w - 1 - i })
  1692.            rect.y += ind
  1693.            rect.height -= ind * 2
  1694.          end
  1695.        when 3
  1696.          if i < h
  1697.            rect.y += h - 1 - i
  1698.            rect.height -= h - 1 - i
  1699.          end
  1700.          if i > w - h
  1701.            rect.height -= i - w + h
  1702.          end
  1703.        end
  1704.        case style
  1705.        when 14, 17, 20, 23, 26, 29, 32
  1706.          r = colors[1].red * i / (w - 1) + colors[0].red * (w - 1 - i) / (w - 1)
  1707.          g = colors[1].green * i / (w - 1) + colors[0].green * (w - 1 - i) / (w - 1)
  1708.          b = colors[1].blue * i / (w - 1) + colors[0].blue * (w - 1 - i) / (w - 1)
  1709.        when 15, 18, 21, 24, 27, 30, 33
  1710.          s = Math.sin(Math::PI * i / (w - 1) / 2)
  1711.          r = colors[1].red * s + colors[0].red * (1 - s)
  1712.          g = colors[1].green * s + colors[0].green * (1 - s)
  1713.          b = colors[1].blue * s + colors[0].blue * (1 - s)
  1714.        when 16, 19, 22, 25, 28, 31, 34
  1715.          s = 2 * Math.sin(Math::PI * i / (w - 1) / 6)
  1716.          r = colors[1].red * s + colors[0].red * (1 - s)
  1717.          g = colors[1].green * s + colors[0].green * (1 - s)
  1718.          b = colors[1].blue * s + colors[0].blue * (1 - s)
  1719.        end
  1720.        fill_rect(rect, Color.new(r, g, b))
  1721.      }
  1722.      if style.between?(17, 34)
  1723.        bmap = self.clone
  1724.        h.times { |i|
  1725.          case style
  1726.          when 17, 18, 19
  1727.            t = (h / 2.0).ceil
  1728.            if i >= t
  1729.              d = i - t + (h % 2 == 1 ? 1 : 0)
  1730.            else
  1731.              d = t - i - 1
  1732.            end
  1733.            t -= 1 if h % 2 == 1
  1734.            a = 255 * (t - d) / t
  1735.          when 20, 21, 22
  1736.            s = Math.sin(Math::PI * i / (h - 1))
  1737.            a = 255 * s
  1738.          when 23, 24, 25
  1739.            s = 1 - Math.sin(Math::PI / 2 + Math::PI * i / (h - 1)).abs
  1740.            a = 255 * s
  1741.          when 26, 27, 28
  1742.            a = 255 * (h - 1 - i) / (h - 1)
  1743.          when 29, 30, 31
  1744.            s = 1 - Math.sin(Math::PI * i / (h - 1) / 2)
  1745.            a = 255 * s
  1746.          when 32, 33, 34
  1747.            s = 1 - 2 * Math.sin(Math::PI * i / (h - 1) / 6)
  1748.            a = 255 * s
  1749.          end
  1750.          r = Rect.new(sx, sy + i, w, 1)
  1751.          blt(r.x, r.y, sbmap, r)
  1752.          blt(r.x, r.y, bmap, r, a)
  1753.        }
  1754.      end
  1755.    end
  1756.    case shape
  1757.    when 2
  1758.      bmap = self.clone
  1759.      t.times { |i|
  1760.        r = Rect.new(sx + i, sy, 1, h)
  1761.        blt(r.x, r.y, sbmap, r)
  1762.        blt(r.x, r.y, bmap, r, 255 * i / t)
  1763.        r = Rect.new(sx + w * rate - 1 - i, sy, 1, h)
  1764.        blt(r.x, r.y, sbmap, r)
  1765.        blt(r.x, r.y, bmap, r, 255 * i / t)
  1766.        bmap = self.clone
  1767.      }
  1768.    end
  1769.  end
  1770.  
  1771.  def gradient_bar(type, style, shape, cstyle, actor, x, y, w, h, text = false, lines = false, fix = true)
  1772.    colors, bgcolor = get_colors(type, actor, cstyle)
  1773.    if fix
  1774.      case shape
  1775.      when 0
  1776.        w = (w - (lines ? 2 : 4)) / 8 * 8 + (lines ? 2 : 4)
  1777.      when 1
  1778.        w = (w - 3 - h + 1)
  1779.        w = w / 8 * 8
  1780.        w = w + h - 1 + 3
  1781.      when 2
  1782.        w = (w - 4) / 8 * 8 + 4
  1783.      when 3
  1784.        w = (w - 2 - h + 1)
  1785.        w = w / 8 * 8
  1786.        w = w + h - 1 + 2
  1787.      end
  1788.    end
  1789.    gradient_bar_back(bgcolor, shape, x, y, w, h, lines, false)
  1790.    case shape
  1791.    when 0
  1792.      x += (lines ? 1 : 2)
  1793.      y += (lines ? 1 : 2)
  1794.      w -= (lines ? 2 : 4)
  1795.      h -= (lines ? 2 : 4)
  1796.    when 1
  1797.      x += 3
  1798.      y += 1
  1799.      w -= 6
  1800.      h -= 3
  1801.    when 2
  1802.      x += 2
  1803.      y += 2
  1804.      w -= 4
  1805.      h -= 4
  1806.    when 3
  1807.      x += 3
  1808.      y += 1
  1809.      w -= 5
  1810.      h -= 3
  1811.    end
  1812.    case type
  1813.    when :hp
  1814.      rate = actor.hp.to_f / actor.maxhp
  1815.      str = actor.hp.to_s + '/' + actor.maxhp.to_s
  1816.    when :mp
  1817.      rate = actor.sp.to_f / actor.maxsp
  1818.      str = actor.sp.to_s + '/' + actor.maxsp.to_s
  1819.    when :exp
  1820.      elist = actor.instance_variable_get(:@exp_list)
  1821.      if elist[actor.level + 1]
  1822.        nexp = elist[actor.level + 1] - elist[actor.level]
  1823.        cexp = actor.exp - elist[actor.level]
  1824.        rate = (nexp <= 0 ? 1 : cexp.to_f / nexp)
  1825.      else
  1826.        rate = 1
  1827.      end
  1828.      str = actor.exp_s + '/' + actor.next_exp_s
  1829.    when :lvl
  1830.      rate = actor.level.to_f / (actor.respond_to?(:final_level) ? actor.final_level : $data_actors[actor.id].final_level)
  1831.      str = actor.level.to_s + '/' + (actor.respond_to?(:final_level) ? actor.final_level : $data_actors[actor.id].final_level).to_s
  1832.    end
  1833.    bgcolors = get_bgcolors(type, cstyle)
  1834.    gradient_bar_front(bgcolors, bgcolor, style, shape, x, y, w, h, 1)
  1835.    gradient_bar_front(colors, bgcolor, style, shape, x, y, w, h, rate)
  1836.    draw_lines(bgcolor, shape, x, y, w, h) if lines
  1837.    psize, pcol = font.size, font.color
  1838.    font.size = h
  1839.    font.color = Color.new(255, 255, 255)
  1840.    draw_text(x, y, w, h, str, 1) if text
  1841.    font.size, font.color = psize, pcol
  1842.  end
  1843.  
  1844.  def draw_lines(color, shape, x, y, w, h)
  1845.    case shape
  1846.    when 1, 3
  1847.      w -= h - 1
  1848.    when 2
  1849.      t = (h / 2.0).floor
  1850.      v = []
  1851.      h.times { |i|
  1852.        v.push(((t + 1) * (1 - Math.sin(Math::PI * (i + 1) / (h + 1)))).floor)
  1853.      }
  1854.    end
  1855.    w.times { |i|
  1856.      if [0, 7].include?(i % 8)
  1857.        case shape
  1858.        when 0
  1859.          fill_rect(x + i, y, 1, h, color)
  1860.        when 1
  1861.          h.times { |e|
  1862.            set_pixel(x + i + e, y + e, color)
  1863.          }
  1864.        when 2
  1865.          if i <= v.max
  1866.            ind = v.index(v.detect { |val| val <= i })
  1867.          elsif w - 1 - i <= v.max
  1868.            ind = v.index(v.detect { |val| val <= w - 1 - i })
  1869.          else
  1870.            ind = 0
  1871.          end
  1872.          fill_rect(x + i, y + ind, 1, h - (ind) * 2, color)
  1873.        when 3
  1874.          h.times { |e|
  1875.            set_pixel(x + i + h - 1 - e, y + e, color)
  1876.          }
  1877.        end
  1878.      end
  1879.    }
  1880.  end
  1881.  
  1882.  def get_bgcolors(type, cstyle)
  1883.    bc = Color.new(0, 0, 0)
  1884.    case type
  1885.    when :hp
  1886.      c1 = Color.new(100, 30, 30)
  1887.      c2 = Color.new(30, 100, 30)
  1888.    when :mp
  1889.      c1 = Color.new(100, 30, 30)
  1890.      c2 = Color.new(30, 30, 100)
  1891.    when :exp
  1892.      c1 = Color.new(100, 47, 22)
  1893.      c2 = Color.new(80, 80, 22)
  1894.    when :lvl
  1895.      c1 = Color.new(22, 47, 100)
  1896.      c2 = Color.new(80, 22, 80)
  1897.    end
  1898.    c = cstyle.map { |i|
  1899.      case i
  1900.      when 0 then c1
  1901.      when 1 then c2
  1902.      when 2 then bc
  1903.      when 3 then c2
  1904.      when 4 then c1
  1905.      when 5 then lcol(c1)
  1906.      when 6 then lcol(c2)
  1907.      when 7 then lcol(c2)
  1908.      when 8 then lcol(c1)
  1909.      when 9 then dcol(c1)
  1910.      when 10 then dcol(c2)
  1911.      when 11 then dcol(c2)
  1912.      when 12 then dcol(c1)
  1913.      end
  1914.    }
  1915.    return c
  1916.  end
  1917. end
  1918.  
  1919.  
  1920. class Scene_Map
  1921.  attr_reader :hud
  1922.  alias tdks_hud_main main
  1923.  def main
  1924.    @hud = HUD.new(DesignHUD::HUD)
  1925.    tdks_hud_main
  1926.    @hud.dispose
  1927.  end
  1928.  
  1929.  alias tdks_hud_updt update
  1930.  def update
  1931.    @hud.update
  1932.    tdks_hud_updt
  1933.  end
  1934. end
Advertisement
Add Comment
Please, Sign In to add comment