[XP][VXA] Design a HUD (WIP)

Alright, so I'm working on a really customizable HUD framework. It uses a language sort of like HTML, only not as great. Right now I'm working on adding new features and elements and stuff like that, but if anyone would like to try it out, here's the script. This is basically a tool for non-scripters to create awesome-looking huds that look exactly how they want them to look.

  1. module DesignHUD
  2.   # This HUD uses an easy to use format for configuration much like html
  3.   #   it has many types of tags, each inserting different information into the
  4.   #   HUD, such as icons, hp/sp/xp bars, and different information about the
  5.   #   actors.
  6.   #
  7.   # Tags:
  8.   #
  9.   #  Formatting Elements:
  10.   #   * <i></i>
  11.   #  - italicizes text
  12.   #   * <b></b>
  13.   #  - bolds text
  14.   #   * <c=R:G:B[:A]></c>
  15.   #  - changes the color to Color.new(R, G, B, A) (A is unnecessary)
  16.   #   * <font=NAME:SIZE></font>
  17.   #  - changes the font to NAME and the font size to SIZE
  18.   #  Graphical Elements:
  19.   #   * <ico=X:Y>
  20.   #  - inserts the icon in the iconset with an offset of X, Y
  21.   #   * <pic='FILENAME'>
  22.   #  - inserts the picture named FILENAME
  23.   #   * <bar='TYPE:STYLE:WIDTH'>
  24.   #  - inserts the blizzart bar (if using blizzart bars) in style STYLE
  25.   #      and with colors corresponding to the stat TYPE with width WIDTH
  26.   #  - types: HP, SP, EXP
  27.   #  Positioning Elements:
  28.   #   * <pos x=VALX y=VALY></pos>
  29.   #  - positions the contents at an x position of VALX and
  30.   #      at a y position of VALY
  31.   #   * <offset x=VALX y=VALY></offset>
  32.   #  - adds VALX and VALY to the position of the contents
  33.   #   * <table></table>
  34.   #  - starts a table
  35.   #   * <tr></tr>
  36.   #  - adds a row to a table
  37.   #   * <td></td>
  38.   #  - adds a cell to a row of a table
  39.   #   * <left></left>
  40.   #  - aligns the contents to the left
  41.   #   * <center></center>
  42.   #  - aligns the contents in the center
  43.   #   * <right></right>
  44.   #  - aligns the contents to the right
  45.   #   * <ln></ln>
  46.   #  - denotes one line in the HUD
  47.   #   * <width=VAL></width>
  48.   #  - sets the width of the contents to VAL
  49.   #   * <height=VAL></height>
  50.   #  - sets the height of the contents to VAL
  51.   #  HUD Properties:
  52.   #   * <hud>
  53.   #  - this tag has many properties, listed below
  54.   #   * skin_tone=R:G:B
  55.   #  - sets the tone of the windowskin
  56.   #   * bg='FILENAME'
  57.   #  - sets the background image of the HUD to the picture named FILENAME
  58.   #   * align=ALIGN
  59.   #  - sets the align of the HUD to ALIGN
  60.   #  - possible aligns: TL, TC, TR, ML, MC, MR, BL, BC, BR (top-left,
  61.   #      top-center, top-right, middle-left, middle-center, middle-right,
  62.   #      bottom-left, bottom-center, bottom-right)
  63.   #   * offsetx=VAL
  64.   #  - sets the xoffset of the HUD to VAL
  65.   #   * offsety=VAL
  66.   #  - and the yoffset of the HUD to VAL
  67.   #   * width=VAL
  68.   #  - sets the width of the HUD to VAL
  69.   #   * height=VAL
  70.   #  - sets the height of the HUD to VAL
  71.   #   * back_opacity=VAL
  72.   #  - sets the back opacity of the HUD to VAL
  73.   #   * contents_opacity=VAL
  74.   #  - sets the contents opacity of the HUD to VAL
  75.   #   * opacity=VAL
  76.   #  - sets the opacity of the HUD to VAL
  77.   HUD = <<-HUD
  78.     <hud bg=Cdd1I windowskin=Window skin_tone=255:-127:10 align='Bc' width=128 offsetx=0 offsety=0 opacity=0 contents_opacity=255>
  79.     <ln><right><c=255:255:0><var=gold>G</c></right></ln>
  80.     <ln><font=Times New Roman:24>Test</font></ln>
  81.   HUD
  82.  
  83.   NO_END = ['ico', 'pic', 'hud', 'var']
  84.  
  85.   BLOCK_ELS = ['ln']
  86.  
  87.   def self.var_expression(name)
  88.     case name
  89.     when 'gold' then return proc { $game_party.gold }
  90.     else return proc {}
  91.     end
  92.   end
  93. end
  94.  
  95. class HUD < Window_Base
  96.   attr_accessor :tmp_attrs
  97.   def initialize(text)
  98.     super(0, 0, 33, 33)
  99.     create_contents
  100.     self.visible = false
  101.     @align = 'TL'
  102.     @xoffset, @yoffset = 0, 0
  103.     @tmp_attrs = {}
  104.     @text = text
  105.     Element.reset_id
  106.     parse_text
  107.     setup_hud
  108.     setup_align
  109.     @dom.delete_if { |el| el.el_type == 'hud' }
  110.     create_contents
  111.     tmp = self.contents
  112.     render_hud
  113.   end
  114.  
  115.  
  116.   def update
  117.     redim = []
  118.     vars = []
  119.     redraw = []
  120.     @var_nodes.each { |el|
  121.       val = el.val
  122.       if val != el.attrs[:val]
  123.         vars.push(el)
  124.         redim |= [el.last_line]
  125.         pos = el.attrs[:pos]
  126.         rect = el.attrs[:size]
  127.         rect.x, rect.y = *pos
  128.         self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  129.        
  130.         el.attrs[:val] = val
  131.       end
  132.     }
  133.     @dom.each { |el|
  134.       comp = false
  135.       el.each_child_ex2(proc { |child|
  136.         comp = false
  137.       }) { |child|
  138.         if child.attrs[:size] and !comp
  139.           pos = child.attrs[:pos]
  140.           rect = child.attrs[:size]
  141.           rect.x, rect.y = *pos
  142.           if vars.select { |el| pos = el.attrs[:pos];tmp = el.attrs[:size];tmp.x, tmp.y = *pos;tmp.intersect?(rect) }.length > 0
  143.             redraw.push(child)
  144.             comp = true
  145.           end
  146.         end
  147.       }
  148.     }
  149.     redraw ||= vars
  150.    
  151.     redetermine_size(redim) if redim.length > 0
  152.     setup_align
  153.     redraw_hud(redraw) if redraw.length > 0
  154.   end
  155.  
  156.   def redetermine_size(els)
  157.     @tmp_attrs[:ox] = 0
  158.     @tmp_attrs[:oy] = 0
  159.    
  160.     pos = [0, 0]
  161.    
  162.     @dom.each { |el|
  163.       ln_height = ln_width = 0
  164.    
  165.       if els.include?(el)
  166.         pos = el.attrs[:pos].clone unless @tmp_attrs[:tmp]
  167.         if @tmp_attrs[:tmp]
  168.           @tmp_attrs[:tmp].push(true)
  169.         else
  170.           @tmp_attrs[:tmp] = [true]
  171.         end
  172.       end
  173.    
  174.       case el.el_type
  175.       when 'ln'
  176.         el.each_child_ex(proc { |child|
  177.           if @tmp_attrs[:tmp]
  178.             unless child.attrs[:size].nil?
  179.               rect = child.attrs[:size]
  180.               rect.x, rect.y = *child.attrs[:pos]
  181.               self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  182.             end
  183.             if @tmp_attrs[:abs_pos]
  184.               child.attrs[:pos] = [@tmp_attrs[:abs_pos].last[0] + @tmp_attrs[:ox], @tmp_attrs[:abs_pos].last[1] + @tmp_attrs[:oy]]
  185.             else
  186.               child.attrs[:pos] = [pos[0] + @tmp_attrs[:ox], pos[1] + @tmp_attrs[:oy]]
  187.             end
  188.           end
  189.           if els.include?(child)
  190.             pos = child.attrs[:pos].clone unless @tmp_attrs[:tmp]
  191.             if @tmp_attrs[:tmp]
  192.               @tmp_attrs[:tmp].push(true)
  193.             else
  194.               @tmp_attrs[:tmp] = [true]
  195.             end
  196.           end
  197.           case child
  198.           when FormatNode
  199.             tmp = child.attrs[:start].call(child, self)
  200.             child.attrs = tmp if tmp and tmp.is_a?(Hash)
  201.           end
  202.         },
  203.         proc { |child|
  204.           case child
  205.           when FormatNode
  206.             tmp = child.attrs[:finish].call(child, self)
  207.             child.attrs = tmp if tmp and tmp.is_a?(Hash)
  208.           end
  209.           if @tmp_attrs[:tmp]
  210.             if els.include?(child)
  211.               @tmp_attrs[:tmp].pop
  212.               @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  213.             end
  214.           end
  215.           unless child.children.empty?
  216.             resize(child)
  217.           end
  218.         }) { |child|
  219.           if @tmp_attrs[:tmp]
  220.             case child
  221.             when TextNode
  222.               size = self.contents.text_size(child.attrs[:val])
  223.               child.attrs[:size] = size
  224.               ln_height = [size.height, ln_height].max
  225.               if @tmp_attrs[:abs_pos]
  226.                 tmp = @tmp_attrs[:abs_pos]
  227.                 ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  228.                 @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  229.               else
  230.                 ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  231.                 pos[0] += size.width
  232.               end
  233.             when VariableNode
  234.               size = self.contents.text_size(child.val)
  235.               child.attrs[:size] = size
  236.               ln_height = [size.height, ln_height].max
  237.               if @tmp_attrs[:abs_pos]
  238.                 tmp = @tmp_attrs[:abs_pos]
  239.                 ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  240.                 @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  241.               else
  242.                 ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  243.                 pos[0] += size.width
  244.               end
  245.             when IconNode, PictureNode
  246.               size = child.attrs[:bitmap].rect
  247.               child.attrs[:size] = size
  248.               ln_height = [size.height, ln_height].max
  249.               if @tmp_attrs[:abs_pos]
  250.                 tmp = @tmp_attrs[:abs_pos]
  251.                 ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  252.                 @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  253.               else
  254.                 ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  255.                 pos[0] += size.width
  256.               end
  257.             end
  258.           end
  259.         }
  260.       end
  261.      
  262.       #pos[1] += ln_height + @tmp_attrs[:oy]
  263.       #ln_widths.push(ln_width)
  264.      
  265.       if @tmp_attrs[:tmp]
  266.         if els.include?(el)
  267.           @tmp_attrs[:tmp].pop
  268.           @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  269.         end
  270.       end
  271.     }
  272.     @dom.each { |child|
  273.       if child.block?
  274.         child.attrs[:size].width = self.width - padding * 2
  275.       end
  276.     }
  277.   end
  278.  
  279.   def redraw_hud(els)
  280.     @tmp_attrs = {}
  281.    
  282.     @dom.each { |el|
  283.    
  284.       align = 0
  285.      
  286.       case el.el_type
  287.       when 'ln'
  288.         if els.include?(el)
  289.           pos = el.attrs[:pos].clone unless @tmp_attrs[:tmp]
  290.           if @tmp_attrs[:tmp]
  291.             @tmp_attrs[:tmp].push(true)
  292.           else
  293.             @tmp_attrs[:tmp] = [true]
  294.           end
  295.         end
  296.         el.each_child_ex(proc { |child|
  297.           if els.include?(child)
  298.             pos = child.attrs[:pos].clone unless @tmp_attrs[:tmp]
  299.             if @tmp_attrs[:tmp]
  300.               @tmp_attrs[:tmp].push(true)
  301.             else
  302.               @tmp_attrs[:tmp] = [true]
  303.             end
  304.           end
  305.           case child
  306.           when FormatNode
  307.             child.attrs[:start].call(child, self)
  308.           end
  309.         },
  310.         proc { |child|
  311.           case child
  312.           when FormatNode
  313.             child.attrs[:finish].call(child, self)
  314.           end
  315.           if @tmp_attrs[:tmp]
  316.             if els.include?(child)
  317.               @tmp_attrs[:tmp].pop
  318.               @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  319.             end
  320.           end
  321.         }) { |child|
  322.           if @tmp_attrs[:tmp]
  323.             case child
  324.             when TextNode
  325.               pos = child.attrs[:pos]
  326.               text = child.attrs[:val]
  327.               size = child.attrs[:size]#self.contents.text_size(text)
  328.               #ln_height = [size.height, ln_height].max
  329.               self.contents.draw_text(Rect.new(pos[0], pos[1], size.width, size.height), text)
  330.             when VariableNode
  331.               pos = child.attrs[:pos]
  332.               text = child.val
  333.               size = child.attrs[:size]
  334.               self.contents.draw_text(Rect.new(pos[0], pos[1], size.width, size.height), text)
  335.             when IconNode, PictureNode
  336.               pos = child.attrs[:pos]
  337.               bitmap = child.attrs[:bitmap]
  338.               self.contents.blt(pos[0], pos[1], bitmap, bitmap.rect)
  339.             end
  340.           end
  341.         }
  342.         #if ind <= text.length - 1
  343.         #  text[ind..text.length - 1].each { |i|
  344.         #   self.contents.draw_text(Rect.new(x, y, self.width, 32), i)
  345.         #   x += self.contents.text_size(i).width
  346.         #  }
  347.         #end
  348.       end
  349.     }
  350.   end
  351.  
  352.   def parse_text
  353.     text = @text.clone
  354.     @dom, @var_nodes = [], []
  355.     index = 0
  356.     current_id = -1
  357.     current_el = nil
  358.     parent = nil
  359.     parent_els = []
  360.     spattern = /<([\w\-]+)(?:=([\w\"'\-\_\:\$\.]+))?((?:\s*\w+=[\w\"'\-\_\:\$\.]+)*)>/
  361.     epattern = /<\/[\w\-]+>/
  362.     while text.length != 0
  363.       s_ind = text.index(spattern)
  364.       if s_ind != nil
  365.         contents = nil
  366.         el_name = $1
  367.         el_val = $2 || ''
  368.         el_attrs = {}
  369.         tmp = $3.strip
  370.         tmp.gsub(/(\w+)\s+=\s+([\w\"'\-\:]+)/, "\\1=\\2").split(/\s+/).each { |el| el_attrs[el.split('=')[0].to_sym] = el.split('=')[1].gsub(/['\"]/, '') }
  371.         el_val.gsub!(/['\"]/, '') if el_val
  372.         el_attrs[:val] = el_val
  373.         level, tmp_name, tmp_ind = 0, '', s_ind
  374.         if !DesignHUD::NO_END.include?(el_name)
  375.           loop do
  376.             text.sub!(spattern, '')
  377.             tmp = [text.index(/(<#{el_name}(?:=[\w\"'\-\:]+)?(?:\s*\w+=[\w\"'\-\:]+)*>)/, tmp_ind)]
  378.             tmp.push(text.index(/<\/#{el_name}>/, tmp_ind))
  379.             if !tmp[1]
  380.               contents = text[s_ind...text.length - 4 - el_name.length]
  381.               text = ''
  382.             elsif !tmp[0]
  383.               contents = text[s_ind...text.length - 4 - el_name.length]
  384.               text = ''
  385.             elsif tmp[1] < tmp[0]
  386.               contents = text[s_ind...tmp[1]]
  387.               text = text[tmp[1]...text.length]
  388.             else
  389.               level += 1
  390.               tmp_ind = tmp[0] + 2 + el_name.length
  391.             end
  392.             break if contents != nil
  393.           end
  394.           case el_name
  395.           when 'ln'
  396.             element = ContainerNode.new('ln', el_attrs, nil, contents)
  397.             @dom.push(element)
  398.           end
  399.         else
  400.           text.sub!(spattern, '')
  401.           case el_name
  402.           when 'hud'
  403.             element = HudNode.new('hud', el_attrs)
  404.             @dom.push(element)
  405.           end
  406.         end
  407.       end
  408.     end
  409.   end
  410.  
  411.   def setup_hud
  412.     @dom.each { |el|
  413.       el.each_child { |tmp|
  414.         if tmp.el_type == 'hud'
  415.           self.width = tmp.attrs[:width].to_i if tmp.attrs[:width]
  416.           self.height = tmp.attrs[:height].to_i if tmp.attrs[:height]
  417.           @align = tmp.attrs[:align] if tmp.attrs[:align]
  418.           @xoffset = tmp.attrs[:offsetx].to_i if tmp.attrs[:offsetx]
  419.           @yoffset = tmp.attrs[:offsety].to_i if tmp.attrs[:offsety]
  420.           self.opacity = tmp.attrs[:opacity].to_i if tmp.attrs[:opacity]
  421.           self.back_opacity = tmp.attrs[:back_opacity].to_i if tmp.attrs[:back_opacity]
  422.           self.contents_opacity = tmp.attrs[:contents_opacity].to_i if tmp.attrs[:contents_opacity]
  423.           begin
  424.             self.windowskin = ((tmp.attrs[:windowskin] == '') ? Bitmap.new(1, 1) : Cache.system(tmp.attrs[:windowskin])) if tmp.attrs[:windowskin]
  425.           rescue
  426.             self.windowskin = Bitmap.new(1, 1)
  427.           end
  428.           self.tone = Tone.new(*tmp.attrs[:skin_tone].split(':').map{|i|i.to_f}) if tmp.attrs[:skin_tone]
  429.           @bg = Sprite.new
  430.           @bg.z = self.z - 1
  431.           if tmp.attrs[:bg]
  432.             @bg.bitmap = Cache.picture(tmp.attrs[:bg])
  433.           else
  434.             @bg.visible = false
  435.           end
  436.         end
  437.       }
  438.     }
  439.    
  440.     tmp_width, tmp_height = get_dimensions
  441.    
  442.     if self.width == 33
  443.       self.width = [tmp_width, 1].max + 32
  444.     end
  445.     if self.height == 33
  446.       self.height = [tmp_height, 1].max + 32
  447.     end
  448.     @dom.each { |child|
  449.       if child.block?
  450.         child.attrs[:size].width = self.width - padding * 2
  451.       end
  452.     }
  453.     @align.downcase!
  454.     xmod = (['l','c'].include?(@align[1..1]) ? 1 : -1)
  455.     ymod = (['t','m'].include?(@align[0..0]) ? 1 : -1)
  456.     xstrt = case @align[1..1]
  457.     when 'l' then 0
  458.     when 'r' then Graphics.width - self.width
  459.     when 'c' then (Graphics.width - self.width) / 2
  460.     end
  461.     ystrt = case @align[0..0]
  462.     when 't' then 0
  463.     when 'b' then Graphics.height - self.height
  464.     when 'm' then (Graphics.height - self.height) / 2
  465.     end
  466.     self.x = xstrt + xmod * @xoffset
  467.     self.y = ystrt + ymod * @yoffset
  468.     if @bg.visible
  469.       @bg.x = self.x + (self.width - @bg.bitmap.width) / 2
  470.       @bg.y = self.y + (self.height - @bg.bitmap.height) / 2
  471.     end
  472.     self.visible = true
  473.   end
  474.  
  475.   def get_dimensions
  476.     ln_widths = []
  477.     height = 0
  478.     @tmp_attrs[:ox] = 0
  479.     @tmp_attrs[:oy] = 0
  480.     @tmp_attrs[:align] = 0
  481.    
  482.     pos = [0, 0]
  483.    
  484.     @dom.each { |el|
  485.    
  486.       pos[0] = 0
  487.       ln_width = 0
  488.       ln_height = 0
  489.      
  490.       case el.el_type
  491.       when 'ln'
  492.         el.attrs[:pos] = pos.clone
  493.         el.attrs[:align] = @tmp_attrs[:abs_pos]
  494.         el.attrs[:abs] = 0
  495.         el.each_child_ex(proc { |child| # start proc
  496.           child.attrs[:align] = (@tmp_attrs[:abs_pos] ? 0 : @tmp_attrs[:align])
  497.           if @tmp_attrs[:abs_pos]
  498.             child.attrs[:pos] = [@tmp_attrs[:abs_pos].last[0] + @tmp_attrs[:ox], @tmp_attrs[:abs_pos].last[1] + @tmp_attrs[:oy]]
  499.             child.attrs[:abs] = @tmp_attrs[:abs_pos].length
  500.           else
  501.             child.attrs[:pos] = [pos[0] + @tmp_attrs[:ox], pos[1] + @tmp_attrs[:oy]]
  502.             child.attrs[:abs] = 0
  503.           end
  504.           case child
  505.           when FormatNode
  506.             tmp = child.attrs[:start].call(child, self)
  507.             child.attrs = tmp if tmp and tmp.is_a?(Hash)
  508.           end
  509.         },
  510.         proc { |child| # end proc
  511.           case child
  512.           when FormatNode
  513.             tmp = child.attrs[:finish].call(child, self)
  514.             child.attrs = tmp if tmp and tmp.is_a?(Hash)
  515.           end
  516.           if !child.children.empty?
  517.             resize(child)
  518.           elsif child.block?
  519.             child.attrs[:size] = Rect.new(0, 0, 0, 0)
  520.           end
  521.         }) { |child| # main proc
  522.           case child
  523.           when TextNode
  524.             size = self.contents.text_size(child.attrs[:val])
  525.             child.attrs[:size] = size
  526.             ln_height = [size.height, ln_height].max
  527.             if @tmp_attrs[:abs_pos]
  528.               tmp = @tmp_attrs[:abs_pos]
  529.               ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  530.               @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  531.             else
  532.               ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  533.               pos[0] += size.width
  534.             end
  535.           when VariableNode
  536.             size = self.contents.text_size(child.val.to_s)
  537.             child.attrs[:size] = size
  538.             ln_height = [size.height, ln_height].max
  539.             if @tmp_attrs[:abs_pos]
  540.               tmp = @tmp_attrs[:abs_pos]
  541.               ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  542.               @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  543.             else
  544.               ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  545.               pos[0] += size.width
  546.             end
  547.             @var_nodes.push(child)
  548.           when IconNode, PictureNode
  549.             size = child.attrs[:bitmap].rect
  550.             child.attrs[:size] = size
  551.             ln_height = [size.height, ln_height].max
  552.             if @tmp_attrs[:abs_pos]
  553.               tmp = @tmp_attrs[:abs_pos]
  554.               ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  555.               @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  556.             else
  557.               ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  558.               pos[0] += size.width
  559.             end
  560.           end
  561.         }
  562.       end
  563.      
  564.       pos[1] += ln_height + @tmp_attrs[:oy]
  565.       ln_widths.push(ln_width)
  566.     }
  567.    
  568.     height = pos[1]
  569.     width = ln_widths.max
  570.     return width, height
  571.   end
  572.  
  573.   def resize(el)
  574.     size = Rect.new(0, 0, 0, 0)
  575.     x1, y1 = 640, 480
  576.     x2 = y2 = 0
  577.     len = el.attrs[:abs]
  578.     el.children.each { |i|
  579.       next if i.attrs[:abs] > len
  580.       x, y = *i.attrs[:pos]
  581.       ns = i.attrs[:size]
  582.       x1 = [x1, x].min
  583.       y1 = [y1, y].min
  584.       x2 = [x2, x + ns.width].max
  585.       y2 = [y2, y + ns.height].max
  586.     }
  587.     size.width = x2 - x1
  588.     size.height = y2 - y1
  589.     pos = [x1, y1]
  590.     el.attrs[:pos], el.attrs[:size] = pos, size
  591.   end
  592.  
  593.   def setup_align
  594.     aligned = nil
  595.     @dom.each { |el|
  596.       case el.el_type
  597.       when 'ln'
  598.         el.each_child_ex(proc {|child|},
  599.         proc { |child| # end proc
  600.           case child
  601.           when FormatNode
  602.             if child.attrs[:tmp]
  603.               child.attrs.delete_at(:tmp)
  604.               aligned.pop
  605.               aligned = nil if aligned.empty?
  606.             end
  607.           end
  608.         }) { |child| # main proc
  609.           break unless aligned.nil?
  610.           if ['right', 'left', 'center'].include?(child.el_type)
  611.             child.attrs[:tmp] = true
  612.             if aligned
  613.               aligned.push(true)
  614.             else
  615.               aligned = [true]
  616.             end
  617.           end
  618.           case child.attrs[:align]
  619.           when 0
  620.             pos = child.attrs[:pos]
  621.             fb = child.last_block
  622.             np = fb.attrs[:pos]
  623.             child.attrs[:pos] = np.clone
  624.             child.each_child_ex(proc{},proc{}) { |i|
  625.               i.attrs[:pos][0] += np[0] - pos[0]
  626.               i.attrs[:pos][1] += np[1] - pos[1]
  627.             }
  628.           when 1
  629.             pos = child.attrs[:pos]
  630.             fb = child.last_block
  631.             fbw = fb.attrs[:size].width
  632.             fbp = fb.attrs[:pos]
  633.             w = child.attrs[:size].width
  634.             np = [(fbw - w) / 2, pos[1]]
  635.             child.attrs[:pos] = np.clone
  636.             child.each_child_ex(proc{},proc{}) { |i|
  637.               i.attrs[:pos][0] += np[0] - pos[0]
  638.               i.attrs[:pos][1] += np[1] - pos[1]
  639.             }
  640.           when 2
  641.             pos = child.attrs[:pos]
  642.             fb = child.last_block
  643.             fbw = fb.attrs[:size].width
  644.             fbp = fb.attrs[:pos]
  645.             w = child.attrs[:size].width
  646.             np = [fbw - w, pos[1]]
  647.             child.attrs[:pos] = np.clone
  648.             child.each_child_ex(proc{},proc{}) { |i|
  649.               i.attrs[:pos][0] += np[0] - pos[0]
  650.               i.attrs[:pos][1] += np[1] - pos[1]
  651.             }
  652.           end
  653.         }
  654.       end
  655.     }
  656.   end
  657.  
  658. #~   def realign
  659. #~   aligned = nil
  660. #~   @dom.each { |el|
  661. #~     case el.el_type
  662. #~     when 'ln'
  663. #~       el.each_child_ex(proc { |child| # start proc
  664. #~         case child
  665. #~         when FormatNode
  666. #~           tmp = child.attrs[:start].call(child, self)
  667. #~           child.attrs = tmp if tmp and tmp.is_a?(Hash)
  668. #~         end
  669. #~       },
  670. #~       proc { |child| # end proc
  671. #~         case child
  672. #~         when FormatNode
  673. #~           tmp = child.attrs[:finish].call(child, self)
  674. #~           child.attrs = tmp if tmp and tmp.is_a?(Hash)
  675. #~           if child.attrs[:tmp]
  676. #~             child.attrs.delete_at(:tmp)
  677. #~             aligned.pop
  678. #~             aligned = nil if aligned.empty?
  679. #~           end
  680. #~         end
  681. #~       }) { |child| # main proc
  682. #~         break unless aligned.nil?
  683. #~         if ['right', 'left', 'center'].include?(child.el_type)
  684. #~           child.attrs[:tmp] = true
  685. #~           if aligned
  686. #~             aligned.push(true)
  687. #~           else
  688. #~             aligned = [true]
  689. #~           end
  690. #~         end
  691. #~         case child.attrs[:align]
  692. #~         when 0
  693. #~           pos = child.attrs[:pos]
  694. #~           fb = child.last_block
  695. #~           np = fb.attrs[:pos]
  696. #~           child.attrs[:pos] = np.clone
  697. #~           child.each_child_ex(proc{},proc{}) { |i|
  698. #~             i.attrs[:pos][0] += np[0] - pos[0]
  699. #~             i.attrs[:pos][1] += np[1] - pos[1]
  700. #~           }
  701. #~         when 1
  702. #~           pos = child.attrs[:pos]
  703. #~           fb = child.last_block
  704. #~           fbw = fb.attrs[:size].width
  705. #~           fbp = fb.attrs[:pos]
  706. #~           w = child.attrs[:size].width
  707. #~           np = [(fbw - w) / 2, pos[1]]
  708. #~           child.attrs[:pos] = np.clone
  709. #~           child.each_child_ex(proc{},proc{}) { |i|
  710. #~             i.attrs[:pos][0] += np[0] - pos[0]
  711. #~             i.attrs[:pos][1] += np[1] - pos[1]
  712. #~           }
  713. #~         when 2
  714. #~           pos = child.attrs[:pos]
  715. #~           fb = child.last_block
  716. #~           fbw = fb.attrs[:size].width
  717. #~           fbp = fb.attrs[:pos]
  718. #~           w = child.attrs[:size].width
  719. #~           np = [fbw - w, pos[1]]
  720. #~           child.attrs[:pos] = np.clone
  721. #~           child.each_child_ex(proc{},proc{}) { |i|
  722. #~             i.attrs[:pos][0] += np[0] - pos[0]
  723. #~             i.attrs[:pos][1] += np[1] - pos[1]
  724. #~           }
  725. #~         end
  726. #~       }
  727. #~     end
  728. #~   }
  729. #~   end
  730.  
  731.   def render_hud
  732.    
  733.     @dom.each { |el|
  734.    
  735.       align = 0
  736.    
  737.       case el.el_type
  738.       when 'ln'
  739.         el.each_child_ex(proc { |child|
  740.           case child
  741.           when FormatNode
  742.             child.attrs[:start].call(child, self)
  743.           end
  744.         },
  745.         proc { |child|
  746.           case child
  747.             when FormatNode
  748.               child.attrs[:finish].call(child, self)
  749.           end
  750.         }) { |child|
  751.           case child
  752.           when TextNode
  753.             pos = child.attrs[:pos]
  754.             text = child.attrs[:val]
  755.             size = child.attrs[:size]#self.contents.text_size(text)
  756.             #ln_height = [size.height, ln_height].max
  757. #~           if @tmp_attrs[:tmpalign]
  758. #~             case @tmp_attrs[:tmpalign].last
  759. #~             when 'r'
  760. #~               pos = pos.clone
  761. #~               pos[0] = contents.width - pos[0] - size.width
  762. #~             end
  763. #~           end
  764.             self.contents.draw_text(Rect.new(pos[0], pos[1], size.width, size.height), text)
  765.           when VariableNode
  766.             pos = child.attrs[:pos]
  767.             text = child.val.to_s
  768.             size = child.attrs[:size]
  769. #~           if @tmp_attrs[:tmpalign]
  770. #~             case @tmp_attrs[:tmpalign].last
  771. #~             when 'r'
  772. #~               pos = pos.clone
  773. #~               pos[0] = contents.width - pos[0] - size.width
  774. #~             end
  775. #~           end
  776.             self.contents.draw_text(Rect.new(pos[0], pos[1], size.width, size.height), text)
  777.           when IconNode, PictureNode
  778.             pos = child.attrs[:pos]
  779.             bitmap = child.attrs[:bitmap]
  780.             self.contents.blt(pos[0], pos[1], bitmap, bitmap.rect)
  781.           end
  782.         }
  783.       end
  784.     }
  785.   end
  786.  
  787.   def get_el(path)
  788.     el = @dom[path[0]]
  789.     if path.length > 1
  790.       path[1..path.length - 1].each { |tmp|
  791.         el = el.children[tmp]
  792.       }
  793.     end
  794.     el
  795.   end
  796. end
  797.  
  798. class Element
  799.   @@id = 0
  800.   attr_accessor :children, :el_type, :attrs, :el_id, :parent
  801.  
  802.   def initialize(el_type, attrs, parent)
  803.     @children = []
  804.     @el_type = el_type.downcase
  805.     @attrs = attrs
  806.     @parent = parent
  807.     @contents = ''
  808.     @el_id = @@id
  809.     @@id += 1
  810.   end
  811.  
  812.   def block?
  813.     return DesignHUD::BLOCK_ELS.include?(@el_type)
  814.   end
  815.  
  816.   def inline?
  817.     return !DesignHUD::BLOCK_ELS.include?(@el_type)
  818.   end
  819.  
  820.   def self.reset_id
  821.     @@id = 0
  822.   end
  823.  
  824.   def parents
  825.     if @parent != nil
  826.       parents = [@parent]
  827.       parent = @parent
  828.       while parent.parent != nil
  829.         parents.push(parent.parent)
  830.         parent = parent.parent
  831.       end
  832.       return parents
  833.     else
  834.       return []
  835.     end
  836.   end
  837.  
  838.   def last_block
  839.     if @parent != nil
  840.       last = self
  841.       parent = @parent
  842.       while last.inline?
  843.         last = parent
  844.         parent = parent.parent
  845.         break if parent.nil?
  846.       end
  847.       return last
  848.     else
  849.       return nil
  850.     end
  851.   end
  852.  
  853.   def last_line
  854.     if @parent != nil
  855.       last = self
  856.       parent = @parent
  857.       while last.el_type != 'ln'
  858.         last = parent
  859.         parent = parent.parent
  860.         break if parent.nil?
  861.       end
  862.       return last
  863.     else
  864.       return nil
  865.     end
  866.   end
  867.  
  868.   def each_child(&block)
  869.     block.call(self)
  870.     path = [0]
  871.     cur = self
  872.     loop {
  873.       child = cur.children[path.last]
  874.       if child
  875.         block.call(child)
  876.         cur = child
  877.         path.push(0)
  878.       else
  879.         cur = cur.parent
  880.         path.pop
  881.         break unless cur
  882.         path[path.length - 1] += 1
  883.       end
  884.     }
  885.   end
  886.  
  887.   def each_child_ex(start, finish, &block)
  888.     path = [0]
  889.     cur = self
  890.     loop {
  891.       child = cur.children[path.last]
  892.       if child
  893.         start.call(child)
  894.         block.call(child)
  895.         cur = child
  896.         path.push(0)
  897.       else
  898.         finish.call(cur)
  899.         cur = cur.parent
  900.         path.pop
  901.         break unless cur and path.length > 0
  902.         path[path.length-1] += 1
  903.       end
  904.     }
  905.   end
  906.  
  907.   def each_child_ex2(finish, &block)
  908.     block.call(self)
  909.     path = [0]
  910.     cur = self
  911.     loop {
  912.       child = cur.children[path.last]
  913.       if child
  914.         block.call(child)
  915.         cur = child
  916.         path.push(0)
  917.       else
  918.         finish.call(cur)
  919.         cur = cur.parent
  920.         path.pop
  921.         break unless cur
  922.         path[path.length - 1] += 1
  923.       end
  924.     }
  925.   end
  926.  
  927.   def parse_contents(text)
  928.     @tmp_attrs = {}
  929.     spattern = /<([\w\-]+)(?:=([\w\"'\-\_\:\$\.\s]+))?((?:\s*\w+=[\w\"'\-\_\:\$\.]+)*)>/
  930.     epattern = /<\/[\w\-]+>/
  931.     while text != ''
  932.       s_ind = text.index(spattern)
  933.       if s_ind != nil
  934.         if s_ind > 0
  935.           contents = text[0...s_ind]
  936.           text = text[s_ind...text.length]
  937.           element = TextNode.new('', self, contents)
  938.           @children.push(element)
  939.         end
  940.         contents = nil
  941.         el_name = $1
  942.         el_val = $2 || ''
  943.         el_attrs = {}
  944.         tmp = $3.strip
  945.         tmp.gsub(/(\w+)\s+=\s+([\w\"'\-\:]+)/, "\\1=\\2").split(/\s+/).each { |el| el_attrs[el.split('=')[0].to_sym] = eval(el.split('=')[1]) }
  946.         el_val.gsub!(/['\"]/, '') if el_val
  947.         el_attrs[:val] = el_val
  948.         level, tmp_name, tmp_ind = 0, '', 0
  949.         if !DesignHUD::NO_END.include?(el_name)
  950.           text.sub!(spattern, '')
  951.           last_ind = nil
  952.           loop do
  953.             tmp = [text.index(/(<#{el_name}(?:=[\w\"'\-\:]+)?(?:\s*\w+=[\w\"'\-\:]+)*>)/, tmp_ind)]
  954.             tmp2 = tmp_ind
  955.             for i in 0..level
  956.               tmp2 = text.index(/<\/#{el_name}>/, tmp2) + 2 + el_name.length
  957.             end
  958.             tmp.push(tmp2 - 2 - el_name.length)
  959.             if !tmp[1]
  960.               contents = text[0...text.length]
  961.               text = ''
  962.             elsif !tmp[0]
  963.               contents = text[0...tmp[1]]
  964.               text = text[tmp[1] + 3 + el_name.length...text.length]
  965.               level = [level - 1, 0].max
  966.             elsif tmp[1] < tmp[0]
  967.               contents = text[0...tmp[1]]
  968.               text = text[tmp[1] + 3 + el_name.length...text.length]
  969.               level = [level - 1, 0].max
  970.             else
  971.               level += 1 unless last_ind != nil and tmp[0] > last_ind[1]
  972.               tmp_ind = tmp[0] + 2 + el_name.length
  973.             end
  974.             last_ind = tmp
  975.             break if contents != nil
  976.           end
  977.           case el_name
  978.           when 'ln'
  979.             element = ContainerNode.new('ln', el_attrs, self, contents)
  980.             @children.push(element)
  981.           when 'b'
  982.             element = FormatNode.new('b', self, contents, proc { |child, hud|
  983.               if !@tmp_attrs[:tmpb]
  984.                 @tmp_attrs[:tmpb] = [hud.contents.font.bold]
  985.               else
  986.                 @tmp_attrs[:tmpb].push(hud.contents.font.bold)
  987.               end
  988.               hud.contents.font.bold = true
  989.             },
  990.             proc { |child, hud|
  991.               hud.contents.font.bold = @tmp_attrs[:tmpb].pop
  992.             })
  993.             @children.push(element)
  994.           when 'i'
  995.             element = FormatNode.new('i', self, contents, proc { |child, hud|
  996.               if !@tmp_attrs[:tmpi]
  997.                 @tmp_attrs[:tmpi] = [hud.contents.font.italic]
  998.               else
  999.                 @tmp_attrs[:tmpi].push(hud.contents.font.italic)
  1000.               end
  1001.               hud.contents.font.italic = true
  1002.             },
  1003.             proc { |child, hud|
  1004.               hud.contents.font.italic = @tmp_attrs[:tmpi].pop
  1005.             })
  1006.             @children.push(element)
  1007.           when 'c'
  1008.             element = FormatNode.new('c', self, contents, proc { |child, hud|
  1009.               if !hud.tmp_attrs[:tmpc]
  1010.                 hud.tmp_attrs[:tmpc] = [hud.contents.font.color.clone]
  1011.               else
  1012.                 hud.tmp_attrs[:tmpc].push(hud.contents.font.color.clone)
  1013.               end
  1014.               hud.contents.font.color = Color.new(*child.attrs[:val].split(':').map { |i| i.to_i })
  1015.             },
  1016.             proc { |child, hud|
  1017.               hud.contents.font.color = hud.tmp_attrs[:tmpc].pop
  1018.             }, el_val)
  1019.             @children.push(element)
  1020.           when 'font'
  1021.             element = FormatNode.new('font', self, contents, proc { |child, hud|
  1022.               if !@tmp_attrs[:tmpfn]
  1023.                 hud.tmp_attrs[:tmpfn] = [[hud.contents.font.name.clone, hud.contents.font.size]]
  1024.               else
  1025.                 hud.tmp_attrs[:tmpfn].push([hud.contents.font.name.clone, hud.contents.font.size])
  1026.               end
  1027.               if Font.exist?(child.attrs[:val].split(':')[0])
  1028.                 hud.contents.font.name = child.attrs[:val].split(':')[0]
  1029.                 hud.contents.font.size = child.attrs[:val].split(':')[1].to_i
  1030.               end
  1031.             },
  1032.             proc { |child, hud|
  1033.               hud.contents.font.name, hud.contents.font.size = *hud.tmp_attrs[:tmpfn].pop
  1034.             }, el_val)
  1035.             @children.push(element)
  1036.           when 'pos'
  1037.             element = FormatNode.new('pos', self, contents, proc { |child, hud|
  1038.             if !hud.tmp_attrs[:abs_pos]
  1039.                 hud.tmp_attrs[:abs_pos] = [[child.attrs[:x], child.attrs[:y]]]
  1040.               else
  1041.                 hud.tmp_attrs[:abs_pos].push([child.attrs[:x], child.attrs[:y]])
  1042.               end
  1043.             },
  1044.             proc { |child, hud|
  1045.               hud.tmp_attrs[:abs_pos].pop
  1046.               hud.tmp_attrs.delete(:abs_pos) if hud.tmp_attrs[:abs_pos].length == 0
  1047.             }, el_val, el_attrs)
  1048.             @children.push(element)
  1049.           when 'offset'
  1050.             element = FormatNode.new('offset', self, contents, proc { |child, hud|
  1051.               if !hud.tmp_attrs[:tmpofst]
  1052.                 hud.tmp_attrs[:tmpofst] = [[hud.tmp_attrs[:ox], hud.tmp_attrs[:oy]]]
  1053.               else
  1054.                 hud.tmp_attrs[:tmpofst].push([hud.tmp_attrs[:ox], hud.tmp_attrs[:oy]])
  1055.               end
  1056.               hud.tmp_attrs[:ox], hud.tmp_attrs[:oy] = child.attrs[:x], child.attrs[:y]
  1057.             },
  1058.             proc { |child, hud|
  1059.               hud.tmp_attrs[:ox], hud.tmp_attrs[:oy] = *hud.tmp_attrs[:tmpofst].pop
  1060.             }, el_val, el_attrs)
  1061.             @children.push(element)
  1062.           when 'right'
  1063.             element = FormatNode.new('right', self, contents, proc { |child, hud|
  1064.               if !hud.tmp_attrs[:tmpalign]
  1065.                 hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1066.               else
  1067.                 hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1068.               end
  1069.               hud.tmp_attrs[:align] = 2
  1070.               child.attrs[:align] = 2
  1071.             },
  1072.             proc { |child, hud|
  1073.               hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1074.             }, el_val, el_attrs)
  1075.             @children.push(element)
  1076.           when 'center'
  1077.             element = FormatNode.new('center', self, contents, proc { |child, hud|
  1078.               if !hud.tmp_attrs[:tmpalign]
  1079.                 hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1080.               else
  1081.                 hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1082.               end
  1083.               hud.tmp_attrs[:align] = 1
  1084.               child.attrs[:align] = 1
  1085.             },
  1086.             proc { |child, hud|
  1087.               hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1088.             }, el_val, el_attrs)
  1089.             @children.push(element)
  1090.           when 'left'
  1091.             element = FormatNode.new('left', self, contents, proc { |child, hud|
  1092.               if !hud.tmp_attrs[:tmpalign]
  1093.                 hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1094.               else
  1095.                 hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1096.               end
  1097.               hud.tmp_attrs[:align] = 0
  1098.               child.attrs[:align] = 0
  1099.             },
  1100.             proc { |child, hud|
  1101.               hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1102.             }, el_val, el_attrs)
  1103.             @children.push(element)
  1104.           end
  1105.         else
  1106.           text.sub!(spattern, '')
  1107.           case el_name
  1108.           when 'hud'
  1109.             element = HudNode.new('hud', el_attrs, self)
  1110.             @children.push(element)
  1111.           when 'var'
  1112.             element = VariableNode.new(self, el_val)
  1113.             @children.push(element)
  1114.           when 'ico'
  1115.             element = IconNode.new(self, el_val)
  1116.             @children.push(element)
  1117.           when 'pic'
  1118.             element = PictureNode.new(self, el_val)
  1119.             @children.push(element)
  1120.           end
  1121.         end
  1122.       else
  1123.         contents = text
  1124.         text = ''
  1125.         element = TextNode.new('', self, contents)
  1126.         @children.push(element)
  1127.       end
  1128.     end
  1129.   end
  1130. end
  1131.  
  1132. class TextNode < Element
  1133.   def initialize(type, parent, text)
  1134.     super(type, {}, parent)
  1135.     @attrs[:val] = text
  1136.   end
  1137. end
  1138.  
  1139. class PictureNode < Element
  1140.   def initialize(parent, val)
  1141.     super('pic', {}, parent)
  1142.     @attrs[:bitmap] = Cache.picture(val)
  1143.   end
  1144. end
  1145.  
  1146. class IconNode < Element
  1147.   def initialize(parent, val)
  1148.     super('ico', {}, parent)
  1149.     offset = val.split(':').map { |i| i.to_i * 24 }
  1150.     @attrs[:bitmap] = Bitmap.new(24, 24)
  1151.     iconset = Cache.system('IconSet')
  1152.     @attrs[:bitmap].blt(0, 0, iconset, Rect.new(offset[0], offset[1], 24, 24))
  1153.   end
  1154. end
  1155.  
  1156. class VariableNode < Element
  1157.   def initialize(parent, name)
  1158.     super('var', {}, parent)
  1159.     @attrs[:expr] = DesignHUD.var_expression(name)
  1160.     @attrs[:val] = @attrs[:expr].call || 0
  1161.   end
  1162.  
  1163.   def val
  1164.     tmp = @attrs[:expr].call
  1165.     return (tmp.nil? ? 0 : tmp)
  1166.   end
  1167. end
  1168.  
  1169. class BarNode < Element
  1170.  
  1171. end
  1172.  
  1173. class HudNode < Element
  1174.   def initialize(type, attrs, parent = nil)
  1175.     super('hud', attrs, parent)
  1176.   end
  1177. end
  1178.  
  1179. class ContainerNode < Element
  1180.   def initialize(type, attrs, parent, contents)
  1181.     super(type, attrs, parent)
  1182.    
  1183.     parse_contents(contents)
  1184.   end
  1185. end
  1186.  
  1187. class FormatNode < Element
  1188.   def initialize(type, parent, contents, start, finish, val=nil, attrs = {})
  1189.     super(type, attrs, parent)
  1190.    
  1191.     parse_contents(contents)
  1192.     @attrs[:val] = val if val
  1193.     @attrs[:start] = start
  1194.     @attrs[:finish] = finish
  1195.   end
  1196. end
  1197.  
  1198. class Rect
  1199.   def intersect?(rect)
  1200.     return false if rect.width * rect.height == 0 or self.width * self.height == 0
  1201.     return true if (self.x.between?(rect.x - 1, rect.x + rect.width) or
  1202.                    (self.x + self.width).between?(rect.x - 1, rect.x + rect.width)) and
  1203.                    (self.y.between?(rect.y - 1, rect.y + rect.height) or
  1204.                    (self.y + self.height).between?(rect.y - 1, rect.y + rect.height))
  1205.     return false
  1206.   end
  1207. end
Expand to see the code.


I originally started making this in RMXP, but ported it over to RMVXA when it appeared that I had found a bug in RMXP's ruby interpreter. However, I just tried back-porting it to RMXP, and it worked. So now, I will resume development in RMXP. Here is the current RMXP version of the script:
  1. module DesignHUD
  2.   # This HUD uses an easy to use format for configuration much like html
  3.   #   it has many types of tags, each inserting different information into the
  4.   #   HUD, such as icons, hp/sp/xp bars, and different information about the
  5.   #   actors.
  6.   #
  7.   # Tags:
  8.   #
  9.   #  Formatting Elements:
  10.   #   * <i></i>
  11.   #  - italicizes text
  12.   #   * <b></b>
  13.   #  - bolds text
  14.   #   * <c=R:G:B[:A]></c>
  15.   #  - changes the color to Color.new(R, G, B, A) (A is unnecessary)
  16.   #   * <font=NAME:SIZE></font>
  17.   #  - changes the font to NAME and the font size to SIZE
  18.   #  Graphical Elements:
  19.   #   * <ico=FILENAME>
  20.   #  - inserts the icon named FILENAME
  21.   #   * <pic=FILENAME>
  22.   #  - inserts the picture named FILENAME
  23.   #   * <bar='TYPE:STYLE:WIDTH'>
  24.   #  - inserts the blizzart bar (if using blizzart bars) in style STYLE
  25.   #      and with colors corresponding to the stat TYPE with width WIDTH
  26.   #  - types: HP, SP, EXP
  27.   #  Positioning Elements:
  28.   #   * <pos x=VALX y=VALY></pos>
  29.   #  - positions the contents at an x position of VALX and
  30.   #      at a y position of VALY
  31.   #   * <offset x=VALX y=VALY></offset>
  32.   #  - adds VALX and VALY to the position of the contents
  33.   #   * <table></table>
  34.   #  - starts a table
  35.   #   * <tr></tr>
  36.   #  - adds a row to a table
  37.   #   * <td></td>
  38.   #  - adds a cell to a row of a table
  39.   #   * <left></left>
  40.   #  - aligns the contents to the left
  41.   #   * <center></center>
  42.   #  - aligns the contents in the center
  43.   #   * <right></right>
  44.   #  - aligns the contents to the right
  45.   #   * <ln></ln>
  46.   #  - denotes one line in the HUD
  47.   #   * <width=VAL></width>
  48.   #  - sets the width of the contents to VAL
  49.   #   * <height=VAL></height>
  50.   #  - sets the height of the contents to VAL
  51.   #  HUD Properties:
  52.   #   * <hud>
  53.   #  - this tag has many properties, listed below
  54.   #   * windowskin='FILENAME'
  55.   #  - sets the windowskin of the HUD to the windowskin named FILENAME
  56.   #   * bg='FILENAME'
  57.   #  - sets the background image of the HUD to the picture named FILENAME
  58.   #   * align=ALIGN
  59.   #  - sets the align of the HUD to ALIGN
  60.   #  - possible aligns: TL, TC, TR, ML, MC, MR, BL, BC, BR (top-left,
  61.   #      top-center, top-right, middle-left, middle-center, middle-right,
  62.   #      bottom-left, bottom-center, bottom-right)
  63.   #   * offsetx=VAL
  64.   #  - sets the xoffset of the HUD to VAL
  65.   #   * offsety=VAL
  66.   #  - and the yoffset of the HUD to VAL
  67.   #   * width=VAL
  68.   #  - sets the width of the HUD to VAL
  69.   #   * height=VAL
  70.   #  - sets the height of the HUD to VAL
  71.   #   * back_opacity=VAL
  72.   #  - sets the back opacity of the HUD to VAL
  73.   #   * contents_opacity=VAL
  74.   #  - sets the contents opacity of the HUD to VAL
  75.   #   * opacity=VAL
  76.   #  - sets the opacity of the HUD to VAL
  77.   HUD = <<-HUD
  78.     <hud bg=Cdd1I windowskin=Window align='Bc' width=128 offsetx=0 offsety=0 opacity=0 contents_opacity=255>
  79.     <ln><right><c=255:255:0><var=gold>G</c></right></ln>
  80.     <ln><font=Times New Roman:24>Test</font></ln>
  81.   HUD
  82.  
  83.   NO_END = ['ico', 'pic', 'hud', 'var']
  84.  
  85.   BLOCK_ELS = ['ln']
  86.  
  87.   def self.var_expression(name)
  88.     case name
  89.     when 'gold' then return proc { $game_party.gold }
  90.     else return proc {}
  91.     end
  92.   end
  93. end
  94. class HUD < Window_Base
  95.   attr_accessor :tmp_attrs
  96.   def initialize(text)
  97.     super(0, 0, 33, 33)
  98.     self.contents = Bitmap.new(1, 1)
  99.     self.visible = false
  100.     @align = 'TL'
  101.     @xoffset, @yoffset = 0, 0
  102.     @tmp_attrs = {}
  103.     @text = text
  104.     Element.reset_id
  105.     parse_text
  106.     setup_hud
  107.     setup_align
  108.     @dom.delete_if { |el| el.el_type == 'hud' }
  109.     self.contents = Bitmap.new(width - 32, height - 32)
  110.     tmp = self.contents
  111.     render_hud
  112.   end
  113.  
  114.  
  115.   def update
  116.     redim = []
  117.     vars = []
  118.     redraw = []
  119.     @var_nodes.each { |el|
  120.       val = el.val
  121.       if val != el.attrs[:val]
  122.         vars.push(el)
  123.         redim |= [el.last_line]
  124.         pos = el.attrs[:pos]
  125.         rect = el.attrs[:size]
  126.         rect.x, rect.y = *pos
  127.         self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  128.        
  129.         el.attrs[:val] = val
  130.       end
  131.     }
  132.     @dom.each { |el|
  133.       comp = false
  134.       el.each_child_ex2(proc { |child|
  135.         comp = false
  136.       }) { |child|
  137.         if child.attrs[:size] and !comp
  138.           pos = child.attrs[:pos]
  139.           rect = child.attrs[:size]
  140.           rect.x, rect.y = *pos
  141.           if vars.select { |el| pos = el.attrs[:pos];tmp = el.attrs[:size];tmp.x, tmp.y = *pos;tmp.intersect?(rect) }.length > 0
  142.             redraw.push(child)
  143.             comp = true
  144.           end
  145.         end
  146.       }
  147.     }
  148.     redraw ||= vars
  149.    
  150.     redetermine_size(redim) if redim.length > 0
  151.     setup_align
  152.     redraw_hud(redraw) if redraw.length > 0
  153.   end
  154.  
  155.   def redetermine_size(els)
  156.     @tmp_attrs[:ox] = 0
  157.     @tmp_attrs[:oy] = 0
  158.    
  159.     pos = [0, 0]
  160.    
  161.     @dom.each { |el|
  162.       ln_height = ln_width = 0
  163.    
  164.       if els.include?(el)
  165.         pos = el.attrs[:pos].clone unless @tmp_attrs[:tmp]
  166.         if @tmp_attrs[:tmp]
  167.           @tmp_attrs[:tmp].push(true)
  168.         else
  169.           @tmp_attrs[:tmp] = [true]
  170.         end
  171.       end
  172.    
  173.       case el.el_type
  174.       when 'ln'
  175.         el.each_child_ex(proc { |child|
  176.           if @tmp_attrs[:tmp]
  177.             unless child.attrs[:size].nil?
  178.               rect = child.attrs[:size]
  179.               rect.x, rect.y = *child.attrs[:pos]
  180.               self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  181.             end
  182.             if @tmp_attrs[:abs_pos]
  183.               child.attrs[:pos] = [@tmp_attrs[:abs_pos].last[0] + @tmp_attrs[:ox], @tmp_attrs[:abs_pos].last[1] + @tmp_attrs[:oy]]
  184.             else
  185.               child.attrs[:pos] = [pos[0] + @tmp_attrs[:ox], pos[1] + @tmp_attrs[:oy]]
  186.             end
  187.           end
  188.           if els.include?(child)
  189.             pos = child.attrs[:pos].clone unless @tmp_attrs[:tmp]
  190.             if @tmp_attrs[:tmp]
  191.               @tmp_attrs[:tmp].push(true)
  192.             else
  193.               @tmp_attrs[:tmp] = [true]
  194.             end
  195.           end
  196.           case child
  197.           when FormatNode
  198.             tmp = child.attrs[:start].call(child, self)
  199.             child.attrs = tmp if tmp and tmp.is_a?(Hash)
  200.           end
  201.         },
  202.         proc { |child|
  203.           case child
  204.           when FormatNode
  205.             tmp = child.attrs[:finish].call(child, self)
  206.             child.attrs = tmp if tmp and tmp.is_a?(Hash)
  207.           end
  208.           if @tmp_attrs[:tmp]
  209.             if els.include?(child)
  210.               @tmp_attrs[:tmp].pop
  211.               @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  212.             end
  213.           end
  214.           unless child.children.empty?
  215.             resize(child)
  216.           end
  217.         }) { |child|
  218.           if @tmp_attrs[:tmp]
  219.             case child
  220.             when TextNode
  221.               size = self.contents.text_size(child.attrs[:val])
  222.               child.attrs[:size] = size
  223.               ln_height = [size.height, ln_height].max
  224.               if @tmp_attrs[:abs_pos]
  225.                 tmp = @tmp_attrs[:abs_pos]
  226.                 ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  227.                 @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  228.               else
  229.                 ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  230.                 pos[0] += size.width
  231.               end
  232.             when VariableNode
  233.               size = self.contents.text_size(child.val.to_s)
  234.               child.attrs[:size] = size
  235.               ln_height = [size.height, ln_height].max
  236.               if @tmp_attrs[:abs_pos]
  237.                 tmp = @tmp_attrs[:abs_pos]
  238.                 ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  239.                 @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  240.               else
  241.                 ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  242.                 pos[0] += size.width
  243.               end
  244.             when IconNode, PictureNode
  245.               size = child.attrs[:bitmap].rect
  246.               child.attrs[:size] = size
  247.               ln_height = [size.height, ln_height].max
  248.               if @tmp_attrs[:abs_pos]
  249.                 tmp = @tmp_attrs[:abs_pos]
  250.                 ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  251.                 @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  252.               else
  253.                 ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  254.                 pos[0] += size.width
  255.               end
  256.             end
  257.           end
  258.         }
  259.       end
  260.      
  261.       #pos[1] += ln_height + @tmp_attrs[:oy]
  262.       #ln_widths.push(ln_width)
  263.      
  264.       if @tmp_attrs[:tmp]
  265.         if els.include?(el)
  266.           @tmp_attrs[:tmp].pop
  267.           @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  268.         end
  269.       end
  270.     }
  271.     @dom.each { |child|
  272.       if child.block?
  273.         child.attrs[:size].width = self.width - 32
  274.       end
  275.     }
  276.   end
  277.  
  278.   def redraw_hud(els)
  279.     @tmp_attrs = {}
  280.    
  281.     @dom.each { |el|
  282.    
  283.       align = 0
  284.      
  285.       case el.el_type
  286.       when 'ln'
  287.         if els.include?(el)
  288.           pos = el.attrs[:pos].clone unless @tmp_attrs[:tmp]
  289.           if @tmp_attrs[:tmp]
  290.             @tmp_attrs[:tmp].push(true)
  291.           else
  292.             @tmp_attrs[:tmp] = [true]
  293.           end
  294.         end
  295.         el.each_child_ex(proc { |child|
  296.           if els.include?(child)
  297.             pos = child.attrs[:pos].clone unless @tmp_attrs[:tmp]
  298.             if @tmp_attrs[:tmp]
  299.               @tmp_attrs[:tmp].push(true)
  300.             else
  301.               @tmp_attrs[:tmp] = [true]
  302.             end
  303.           end
  304.           case child
  305.           when FormatNode
  306.             child.attrs[:start].call(child, self)
  307.           end
  308.         },
  309.         proc { |child|
  310.           case child
  311.           when FormatNode
  312.             child.attrs[:finish].call(child, self)
  313.           end
  314.           if @tmp_attrs[:tmp]
  315.             if els.include?(child)
  316.               @tmp_attrs[:tmp].pop
  317.               @tmp_attrs.delete(:tmp) if @tmp_attrs[:tmp].length == 0
  318.             end
  319.           end
  320.         }) { |child|
  321.           if @tmp_attrs[:tmp]
  322.             case child
  323.             when TextNode
  324.               pos = child.attrs[:pos]
  325.               text = child.attrs[:val]
  326.               size = child.attrs[:size]#self.contents.text_size(text)
  327.               #ln_height = [size.height, ln_height].max
  328.               self.contents.draw_text(Rect.new(pos[0], pos[1], size.width, size.height), text)
  329.             when VariableNode
  330.               pos = child.attrs[:pos]
  331.               text = child.val.to_s
  332.               size = child.attrs[:size]
  333.               self.contents.draw_text(Rect.new(pos[0], pos[1], size.width, size.height), text)
  334.             when IconNode, PictureNode
  335.               pos = child.attrs[:pos]
  336.               bitmap = child.attrs[:bitmap]
  337.               self.contents.blt(pos[0], pos[1], bitmap, bitmap.rect)
  338.             end
  339.           end
  340.         }
  341.         #if ind <= text.length - 1
  342.         #  text[ind..text.length - 1].each { |i|
  343.         #   self.contents.draw_text(Rect.new(x, y, self.width, 32), i)
  344.         #   x += self.contents.text_size(i).width
  345.         #  }
  346.         #end
  347.       end
  348.     }
  349.   end
  350.  
  351.   def parse_text
  352.     text = @text.clone
  353.     @dom, @var_nodes = [], []
  354.     index = 0
  355.     current_id = -1
  356.     current_el = nil
  357.     parent = nil
  358.     parent_els = []
  359.     spattern = /<([\w\-]+)(?:=([\w\"'\-\_\:\$\.]+))?((?:\s*\w+=[\w\"'\-\_\:\$\.]+)*)>/
  360.     epattern = /<\/[\w\-]+>/
  361.     while text.length != 0
  362.       s_ind = text.index(spattern)
  363.       if s_ind != nil
  364.         contents = nil
  365.         el_name = $1
  366.         el_val = $2 || ''
  367.         el_attrs = {}
  368.         tmp = $3.strip
  369.         tmp.gsub(/(\w+)\s+=\s+([\w\"'\-\:]+)/, "\\1=\\2").split(/\s+/).each { |el| el_attrs[el.split('=')[0].to_sym] = el.split('=')[1].gsub(/['\"]/, '') }
  370.         el_val.gsub!(/['\"]/, '') if el_val
  371.         el_attrs[:val] = el_val
  372.         level, tmp_name, tmp_ind = 0, '', s_ind
  373.         if !DesignHUD::NO_END.include?(el_name)
  374.           loop do
  375.             text.sub!(spattern, '')
  376.             tmp = [text.index(/(<#{el_name}(?:=[\w\"'\-\:]+)?(?:\s*\w+=[\w\"'\-\:]+)*>)/, tmp_ind)]
  377.             tmp.push(text.index(/<\/#{el_name}>/, tmp_ind))
  378.             if !tmp[1]
  379.               contents = text[s_ind...text.length - 4 - el_name.length]
  380.               text = ''
  381.             elsif !tmp[0]
  382.               contents = text[s_ind...text.length - 4 - el_name.length]
  383.               text = ''
  384.             elsif tmp[1] < tmp[0]
  385.               contents = text[s_ind...tmp[1]]
  386.               text = text[tmp[1]...text.length]
  387.             else
  388.               level += 1
  389.               tmp_ind = tmp[0] + 2 + el_name.length
  390.             end
  391.             break if contents != nil
  392.           end
  393.           case el_name
  394.           when 'ln'
  395.             element = ContainerNode.new('ln', el_attrs, nil, contents)
  396.             @dom.push(element)
  397.           end
  398.         else
  399.           text.sub!(spattern, '')
  400.           case el_name
  401.           when 'hud'
  402.             element = HudNode.new('hud', el_attrs)
  403.             @dom.push(element)
  404.           end
  405.         end
  406.       end
  407.     end
  408.   end
  409.  
  410.   def setup_hud
  411.     @dom.each { |el|
  412.       el.each_child { |tmp|
  413.         if tmp.el_type == 'hud'
  414.           self.width = tmp.attrs[:width].to_i if tmp.attrs[:width]
  415.           self.height = tmp.attrs[:height].to_i if tmp.attrs[:height]
  416.           @align = tmp.attrs[:align] if tmp.attrs[:align]
  417.           @xoffset = tmp.attrs[:offsetx].to_i if tmp.attrs[:offsetx]
  418.           @yoffset = tmp.attrs[:offsety].to_i if tmp.attrs[:offsety]
  419.           self.opacity = tmp.attrs[:opacity].to_i if tmp.attrs[:opacity]
  420.           self.back_opacity = tmp.attrs[:back_opacity].to_i if tmp.attrs[:back_opacity]
  421.           self.contents_opacity = tmp.attrs[:contents_opacity].to_i if tmp.attrs[:contents_opacity]
  422.           begin
  423.             self.windowskin = ((tmp.attrs[:windowskin] == '') ? Bitmap.new(1, 1) : Cache.system(tmp.attrs[:windowskin])) if tmp.attrs[:windowskin]
  424.           rescue
  425.             self.windowskin = Bitmap.new(1, 1)
  426.           end
  427.           @bg = Sprite.new
  428.           @bg.z = self.z - 1
  429.           if tmp.attrs[:bg]
  430.             @bg.bitmap = RPG::Cache.picture(tmp.attrs[:bg])
  431.           else
  432.             @bg.visible = false
  433.           end
  434.         end
  435.       }
  436.     }
  437.    
  438.     tmp_width, tmp_height = get_dimensions
  439.    
  440.     if self.width == 33
  441.       self.width = [tmp_width, 1].max + 32
  442.     end
  443.     if self.height == 33
  444.       self.height = [tmp_height, 1].max + 32
  445.     end
  446.     @dom.each { |child|
  447.       if child.block?
  448.         child.attrs[:size].width = self.width - 32
  449.       end
  450.     }
  451.     @align.downcase!
  452.     xmod = (['l','c'].include?(@align[1..1]) ? 1 : -1)
  453.     ymod = (['t','m'].include?(@align[0..0]) ? 1 : -1)
  454.     xstrt = case @align[1..1]
  455.     when 'l' then 0
  456.     when 'r' then 640 - self.width
  457.     when 'c' then (640 - self.width) / 2
  458.     end
  459.     ystrt = case @align[0..0]
  460.     when 't' then 0
  461.     when 'b' then 480 - self.height
  462.     when 'm' then (480 - self.height) / 2
  463.     end
  464.     self.x = xstrt + xmod * @xoffset
  465.     self.y = ystrt + ymod * @yoffset
  466.     if @bg.visible
  467.       @bg.x = self.x + (self.width - @bg.bitmap.width) / 2
  468.       @bg.y = self.y + (self.height - @bg.bitmap.height) / 2
  469.     end
  470.     self.visible = true
  471.   end
  472.  
  473.   def get_dimensions
  474.     ln_widths = []
  475.     height = 0
  476.     @tmp_attrs[:ox] = 0
  477.     @tmp_attrs[:oy] = 0
  478.     @tmp_attrs[:align] = 0
  479.    
  480.     pos = [0, 0]
  481.    
  482.     @dom.each { |el|
  483.    
  484.       pos[0] = 0
  485.       ln_width = 0
  486.       ln_height = 0
  487.      
  488.       case el.el_type
  489.       when 'ln'
  490.         el.attrs[:pos] = pos.clone
  491.         el.attrs[:align] = @tmp_attrs[:abs_pos]
  492.         el.attrs[:abs] = 0
  493.         el.each_child_ex(proc { |child| # start proc
  494.           child.attrs[:align] = (@tmp_attrs[:abs_pos] ? 0 : @tmp_attrs[:align])
  495.           if @tmp_attrs[:abs_pos]
  496.             child.attrs[:pos] = [@tmp_attrs[:abs_pos].last[0] + @tmp_attrs[:ox], @tmp_attrs[:abs_pos].last[1] + @tmp_attrs[:oy]]
  497.             child.attrs[:abs] = @tmp_attrs[:abs_pos].length
  498.           else
  499.             child.attrs[:pos] = [pos[0] + @tmp_attrs[:ox], pos[1] + @tmp_attrs[:oy]]
  500.             child.attrs[:abs] = 0
  501.           end
  502.           case child
  503.           when FormatNode
  504.             tmp = child.attrs[:start].call(child, self)
  505.             child.attrs = tmp if tmp and tmp.is_a?(Hash)
  506.           end
  507.         },
  508.         proc { |child| # end proc
  509.           case child
  510.           when FormatNode
  511.             tmp = child.attrs[:finish].call(child, self)
  512.             child.attrs = tmp if tmp and tmp.is_a?(Hash)
  513.           end
  514.           if !child.children.empty?
  515.             resize(child)
  516.           elsif child.block?
  517.             child.attrs[:size] = Rect.new(0, 0, 0, 0)
  518.           end
  519.         }) { |child| # main proc
  520.           case child
  521.           when TextNode
  522.             size = self.contents.text_size(child.attrs[:val])
  523.             child.attrs[:size] = size
  524.             ln_height = [size.height, ln_height].max
  525.             if @tmp_attrs[:abs_pos]
  526.               tmp = @tmp_attrs[:abs_pos]
  527.               ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  528.               @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  529.             else
  530.               ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  531.               pos[0] += size.width
  532.             end
  533.           when VariableNode
  534.             size = self.contents.text_size(child.val.to_s)
  535.             child.attrs[:size] = size
  536.             ln_height = [size.height, ln_height].max
  537.             if @tmp_attrs[:abs_pos]
  538.               tmp = @tmp_attrs[:abs_pos]
  539.               ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  540.               @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  541.             else
  542.               ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  543.               pos[0] += size.width
  544.             end
  545.             @var_nodes.push(child)
  546.           when IconNode, PictureNode
  547.             size = child.attrs[:bitmap].rect
  548.             child.attrs[:size] = size
  549.             ln_height = [size.height, ln_height].max
  550.             if @tmp_attrs[:abs_pos]
  551.               tmp = @tmp_attrs[:abs_pos]
  552.               ln_width = [tmp.last[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  553.               @tmp_attrs[:abs_pos][tmp.length - 1][0] += size.width
  554.             else
  555.               ln_width = [pos[0] + @tmp_attrs[:ox] + size.width, ln_width].max
  556.               pos[0] += size.width
  557.             end
  558.           end
  559.         }
  560.       end
  561.      
  562.       pos[1] += ln_height + @tmp_attrs[:oy]
  563.       ln_widths.push(ln_width)
  564.     }
  565.    
  566.     height = pos[1]
  567.     width = ln_widths.max
  568.     return width, height
  569.   end
  570.  
  571.   def resize(el)
  572.     size = Rect.new(0, 0, 0, 0)
  573.     x1, y1 = 640, 480
  574.     x2 = y2 = 0
  575.     len = el.attrs[:abs]
  576.     el.children.each { |i|
  577.       next if i.attrs[:abs] > len
  578.       x, y = *i.attrs[:pos]
  579.       ns = i.attrs[:size]
  580.       x1 = [x1, x].min
  581.       y1 = [y1, y].min
  582.       x2 = [x2, x + ns.width].max
  583.       y2 = [y2, y + ns.height].max
  584.     }
  585.     size.width = x2 - x1
  586.     size.height = y2 - y1
  587.     pos = [x1, y1]
  588.     el.attrs[:pos], el.attrs[:size] = pos, size
  589.   end
  590.  
  591.   def setup_align
  592.     aligned = nil
  593.     @dom.each { |el|
  594.       case el.el_type
  595.       when 'ln'
  596.         el.each_child_ex(proc {|child|},
  597.         proc { |child| # end proc
  598.           case child
  599.           when FormatNode
  600.             if child.attrs[:tmp]
  601.               child.attrs.delete_at(:tmp)
  602.               aligned.pop
  603.               aligned = nil if aligned.empty?
  604.             end
  605.           end
  606.         }) { |child| # main proc
  607.           break unless aligned.nil?
  608.           if ['right', 'left', 'center'].include?(child.el_type)
  609.             child.attrs[:tmp] = true
  610.             if aligned
  611.               aligned.push(true)
  612.             else
  613.               aligned = [true]
  614.             end
  615.           end
  616.           case child.attrs[:align]
  617.           when 0
  618.             pos = child.attrs[:pos]
  619.             fb = child.last_block
  620.             np = fb.attrs[:pos]
  621.             child.attrs[:pos] = np.clone
  622.             child.each_child_ex(proc{},proc{}) { |i|
  623.               i.attrs[:pos][0] += np[0] - pos[0]
  624.               i.attrs[:pos][1] += np[1] - pos[1]
  625.             }
  626.           when 1
  627.             pos = child.attrs[:pos]
  628.             fb = child.last_block
  629.             fbw = fb.attrs[:size].width
  630.             fbp = fb.attrs[:pos]
  631.             w = child.attrs[:size].width
  632.             np = [(fbw - w) / 2, pos[1]]
  633.             child.attrs[:pos] = np.clone
  634.             child.each_child_ex(proc{},proc{}) { |i|
  635.               i.attrs[:pos][0] += np[0] - pos[0]
  636.               i.attrs[:pos][1] += np[1] - pos[1]
  637.             }
  638.           when 2
  639.             pos = child.attrs[:pos]
  640.             fb = child.last_block
  641.             fbw = fb.attrs[:size].width
  642.             fbp = fb.attrs[:pos]
  643.             w = child.attrs[:size].width
  644.             np = [fbw - w, pos[1]]
  645.             child.attrs[:pos] = np.clone
  646.             child.each_child_ex(proc{},proc{}) { |i|
  647.               i.attrs[:pos][0] += np[0] - pos[0]
  648.               i.attrs[:pos][1] += np[1] - pos[1]
  649.             }
  650.           end
  651.         }
  652.       end
  653.     }
  654.   end
  655.  
  656. #~   def realign
  657. #~   aligned = nil
  658. #~   @dom.each { |el|
  659. #~     case el.el_type
  660. #~     when 'ln'
  661. #~       el.each_child_ex(proc { |child| # start proc
  662. #~         case child
  663. #~         when FormatNode
  664. #~           tmp = child.attrs[:start].call(child, self)
  665. #~           child.attrs = tmp if tmp and tmp.is_a?(Hash)
  666. #~         end
  667. #~       },
  668. #~       proc { |child| # end proc
  669. #~         case child
  670. #~         when FormatNode
  671. #~           tmp = child.attrs[:finish].call(child, self)
  672. #~           child.attrs = tmp if tmp and tmp.is_a?(Hash)
  673. #~           if child.attrs[:tmp]
  674. #~             child.attrs.delete_at(:tmp)
  675. #~             aligned.pop
  676. #~             aligned = nil if aligned.empty?
  677. #~           end
  678. #~         end
  679. #~       }) { |child| # main proc
  680. #~         break unless aligned.nil?
  681. #~         if ['right', 'left', 'center'].include?(child.el_type)
  682. #~           child.attrs[:tmp] = true
  683. #~           if aligned
  684. #~             aligned.push(true)
  685. #~           else
  686. #~             aligned = [true]
  687. #~           end
  688. #~         end
  689. #~         case child.attrs[:align]
  690. #~         when 0
  691. #~           pos = child.attrs[:pos]
  692. #~           fb = child.last_block
  693. #~           np = fb.attrs[:pos]
  694. #~           child.attrs[:pos] = np.clone
  695. #~           child.each_child_ex(proc{},proc{}) { |i|
  696. #~             i.attrs[:pos][0] += np[0] - pos[0]
  697. #~             i.attrs[:pos][1] += np[1] - pos[1]
  698. #~           }
  699. #~         when 1
  700. #~           pos = child.attrs[:pos]
  701. #~           fb = child.last_block
  702. #~           fbw = fb.attrs[:size].width
  703. #~           fbp = fb.attrs[:pos]
  704. #~           w = child.attrs[:size].width
  705. #~           np = [(fbw - w) / 2, pos[1]]
  706. #~           child.attrs[:pos] = np.clone
  707. #~           child.each_child_ex(proc{},proc{}) { |i|
  708. #~             i.attrs[:pos][0] += np[0] - pos[0]
  709. #~             i.attrs[:pos][1] += np[1] - pos[1]
  710. #~           }
  711. #~         when 2
  712. #~           pos = child.attrs[:pos]
  713. #~           fb = child.last_block
  714. #~           fbw = fb.attrs[:size].width
  715. #~           fbp = fb.attrs[:pos]
  716. #~           w = child.attrs[:size].width
  717. #~           np = [fbw - w, pos[1]]
  718. #~           child.attrs[:pos] = np.clone
  719. #~           child.each_child_ex(proc{},proc{}) { |i|
  720. #~             i.attrs[:pos][0] += np[0] - pos[0]
  721. #~             i.attrs[:pos][1] += np[1] - pos[1]
  722. #~           }
  723. #~         end
  724. #~       }
  725. #~     end
  726. #~   }
  727. #~   end
  728.  
  729.   def render_hud
  730.    
  731.     @dom.each { |el|
  732.    
  733.       align = 0
  734.    
  735.       case el.el_type
  736.       when 'ln'
  737.         el.each_child_ex(proc { |child|
  738.           case child
  739.           when FormatNode
  740.             child.attrs[:start].call(child, self)
  741.           end
  742.         },
  743.         proc { |child|
  744.           case child
  745.             when FormatNode
  746.               child.attrs[:finish].call(child, self)
  747.           end
  748.         }) { |child|
  749.           case child
  750.           when TextNode
  751.             pos = child.attrs[:pos]
  752.             text = child.attrs[:val]
  753.             size = child.attrs[:size]#self.contents.text_size(text)
  754.             #ln_height = [size.height, ln_height].max
  755. #~           if @tmp_attrs[:tmpalign]
  756. #~             case @tmp_attrs[:tmpalign].last
  757. #~             when 'r'
  758. #~               pos = pos.clone
  759. #~               pos[0] = contents.width - pos[0] - size.width
  760. #~             end
  761. #~           end
  762.             self.contents.draw_text(Rect.new(pos[0], pos[1], size.width, size.height), text)
  763.           when VariableNode
  764.             pos = child.attrs[:pos]
  765.             text = child.val.to_s
  766.             size = child.attrs[:size]
  767. #~           if @tmp_attrs[:tmpalign]
  768. #~             case @tmp_attrs[:tmpalign].last
  769. #~             when 'r'
  770. #~               pos = pos.clone
  771. #~               pos[0] = contents.width - pos[0] - size.width
  772. #~             end
  773. #~           end
  774.             self.contents.draw_text(Rect.new(pos[0], pos[1], size.width, size.height), text)
  775.           when IconNode, PictureNode
  776.             pos = child.attrs[:pos]
  777.             bitmap = child.attrs[:bitmap]
  778.             self.contents.blt(pos[0], pos[1], bitmap, bitmap.rect)
  779.           end
  780.         }
  781.       end
  782.     }
  783.   end
  784.  
  785.   def get_el(path)
  786.     el = @dom[path[0]]
  787.     if path.length > 1
  788.       path[1..path.length - 1].each { |tmp|
  789.         el = el.children[tmp]
  790.       }
  791.     end
  792.     el
  793.   end
  794. end
  795.  
  796. class Element
  797.   @@id = 0
  798.   attr_accessor :children, :el_type, :attrs, :el_id, :parent
  799.  
  800.   def initialize(el_type, attrs, parent)
  801.     @children = []
  802.     @el_type = el_type.downcase
  803.     @attrs = attrs
  804.     @parent = parent
  805.     @contents = ''
  806.     @el_id = @@id
  807.     @@id += 1
  808.   end
  809.  
  810.   def block?
  811.     return DesignHUD::BLOCK_ELS.include?(@el_type)
  812.   end
  813.  
  814.   def inline?
  815.     return !DesignHUD::BLOCK_ELS.include?(@el_type)
  816.   end
  817.  
  818.   def self.reset_id
  819.     @@id = 0
  820.   end
  821.  
  822.   def parents
  823.     if @parent != nil
  824.       parents = [@parent]
  825.       parent = @parent
  826.       while parent.parent != nil
  827.         parents.push(parent.parent)
  828.         parent = parent.parent
  829.       end
  830.       return parents
  831.     else
  832.       return []
  833.     end
  834.   end
  835.  
  836.   def last_block
  837.     if @parent != nil
  838.       last = self
  839.       parent = @parent
  840.       while last.inline?
  841.         last = parent
  842.         parent = parent.parent
  843.         break if parent.nil?
  844.       end
  845.       return last
  846.     else
  847.       return nil
  848.     end
  849.   end
  850.  
  851.   def last_line
  852.     if @parent != nil
  853.       last = self
  854.       parent = @parent
  855.       while last.el_type != 'ln'
  856.         last = parent
  857.         parent = parent.parent
  858.         break if parent.nil?
  859.       end
  860.       return last
  861.     else
  862.       return nil
  863.     end
  864.   end
  865.  
  866.   def each_child(&block)
  867.     block.call(self)
  868.     path = [0]
  869.     cur = self
  870.     loop {
  871.       child = cur.children[path.last]
  872.       if child
  873.         block.call(child)
  874.         cur = child
  875.         path.push(0)
  876.       else
  877.         cur = cur.parent
  878.         path.pop
  879.         break unless cur
  880.         path[path.length - 1] += 1
  881.       end
  882.     }
  883.   end
  884.  
  885.   def each_child_ex(start, finish, &block)
  886.     path = [0]
  887.     cur = self
  888.     loop {
  889.       child = cur.children[path.last]
  890.       if child
  891.         start.call(child)
  892.         block.call(child)
  893.         cur = child
  894.         path.push(0)
  895.       else
  896.         finish.call(cur)
  897.         cur = cur.parent
  898.         path.pop
  899.         break unless cur and path.length > 0
  900.         path[path.length-1] += 1
  901.       end
  902.     }
  903.   end
  904.  
  905.   def each_child_ex2(finish, &block)
  906.     block.call(self)
  907.     path = [0]
  908.     cur = self
  909.     loop {
  910.       child = cur.children[path.last]
  911.       if child
  912.         block.call(child)
  913.         cur = child
  914.         path.push(0)
  915.       else
  916.         finish.call(cur)
  917.         cur = cur.parent
  918.         path.pop
  919.         break unless cur
  920.         path[path.length - 1] += 1
  921.       end
  922.     }
  923.   end
  924.  
  925.   def parse_contents(text)
  926.     @tmp_attrs = {}
  927.     spattern = /<([\w\-]+)(?:=([\w\"'\-\_\:\$\.\s]+))?((?:\s*\w+=[\w\"'\-\_\:\$\.]+)*)>/
  928.     epattern = /<\/[\w\-]+>/
  929.     while text != ''
  930.       s_ind = text.index(spattern)
  931.       if s_ind != nil
  932.         if s_ind > 0
  933.           contents = text[0...s_ind]
  934.           text = text[s_ind...text.length]
  935.           element = TextNode.new('', self, contents)
  936.           @children.push(element)
  937.         end
  938.         contents = nil
  939.         el_name = $1
  940.         el_val = $2 || ''
  941.         el_attrs = {}
  942.         tmp = $3.strip
  943.         tmp.gsub(/(\w+)\s+=\s+([\w\"'\-\:]+)/, "\\1=\\2").split(/\s+/).each { |el| el_attrs[el.split('=')[0].to_sym] = eval(el.split('=')[1]) }
  944.         el_val.gsub!(/['\"]/, '') if el_val
  945.         el_attrs[:val] = el_val
  946.         level, tmp_name, tmp_ind = 0, '', 0
  947.         if !DesignHUD::NO_END.include?(el_name)
  948.           text.sub!(spattern, '')
  949.           last_ind = nil
  950.           loop do
  951.             tmp = [text.index(/(<#{el_name}(?:=[\w\"'\-\:]+)?(?:\s*\w+=[\w\"'\-\:]+)*>)/, tmp_ind)]
  952.             tmp2 = tmp_ind
  953.             for i in 0..level
  954.               tmp2 = text.index(/<\/#{el_name}>/, tmp2) + 2 + el_name.length
  955.             end
  956.             tmp.push(tmp2 - 2 - el_name.length)
  957.             if !tmp[1]
  958.               contents = text[0...text.length]
  959.               text = ''
  960.             elsif !tmp[0]
  961.               contents = text[0...tmp[1]]
  962.               text = text[tmp[1] + 3 + el_name.length...text.length]
  963.               level = [level - 1, 0].max
  964.             elsif tmp[1] < tmp[0]
  965.               contents = text[0...tmp[1]]
  966.               text = text[tmp[1] + 3 + el_name.length...text.length]
  967.               level = [level - 1, 0].max
  968.             else
  969.               level += 1 unless last_ind != nil and tmp[0] > last_ind[1]
  970.               tmp_ind = tmp[0] + 2 + el_name.length
  971.             end
  972.             last_ind = tmp
  973.             break if contents != nil
  974.           end
  975.           case el_name
  976.           when 'ln'
  977.             element = ContainerNode.new('ln', el_attrs, self, contents)
  978.             @children.push(element)
  979.           when 'b'
  980.             element = FormatNode.new('b', self, contents, proc { |child, hud|
  981.               if !@tmp_attrs[:tmpb]
  982.                 @tmp_attrs[:tmpb] = [hud.contents.font.bold]
  983.               else
  984.                 @tmp_attrs[:tmpb].push(hud.contents.font.bold)
  985.               end
  986.               hud.contents.font.bold = true
  987.             },
  988.             proc { |child, hud|
  989.               hud.contents.font.bold = @tmp_attrs[:tmpb].pop
  990.             })
  991.             @children.push(element)
  992.           when 'i'
  993.             element = FormatNode.new('i', self, contents, proc { |child, hud|
  994.               if !@tmp_attrs[:tmpi]
  995.                 @tmp_attrs[:tmpi] = [hud.contents.font.italic]
  996.               else
  997.                 @tmp_attrs[:tmpi].push(hud.contents.font.italic)
  998.               end
  999.               hud.contents.font.italic = true
  1000.             },
  1001.             proc { |child, hud|
  1002.               hud.contents.font.italic = @tmp_attrs[:tmpi].pop
  1003.             })
  1004.             @children.push(element)
  1005.           when 'c'
  1006.             element = FormatNode.new('c', self, contents, proc { |child, hud|
  1007.               if !hud.tmp_attrs[:tmpc]
  1008.                 hud.tmp_attrs[:tmpc] = [hud.contents.font.color.clone]
  1009.               else
  1010.                 hud.tmp_attrs[:tmpc].push(hud.contents.font.color.clone)
  1011.               end
  1012.               hud.contents.font.color = Color.new(*child.attrs[:val].split(':').map { |i| i.to_i })
  1013.             },
  1014.             proc { |child, hud|
  1015.               hud.contents.font.color = hud.tmp_attrs[:tmpc].pop
  1016.             }, el_val)
  1017.             @children.push(element)
  1018.           when 'font'
  1019.             element = FormatNode.new('font', self, contents, proc { |child, hud|
  1020.               if !@tmp_attrs[:tmpfn]
  1021.                 hud.tmp_attrs[:tmpfn] = [[hud.contents.font.name.clone, hud.contents.font.size]]
  1022.               else
  1023.                 hud.tmp_attrs[:tmpfn].push([hud.contents.font.name.clone, hud.contents.font.size])
  1024.               end
  1025.               if Font.exist?(child.attrs[:val].split(':')[0])
  1026.                 hud.contents.font.name = child.attrs[:val].split(':')[0]
  1027.                 hud.contents.font.size = child.attrs[:val].split(':')[1].to_i
  1028.               end
  1029.             },
  1030.             proc { |child, hud|
  1031.               hud.contents.font.name, hud.contents.font.size = *hud.tmp_attrs[:tmpfn].pop
  1032.             }, el_val)
  1033.             @children.push(element)
  1034.           when 'pos'
  1035.             element = FormatNode.new('pos', self, contents, proc { |child, hud|
  1036.             if !hud.tmp_attrs[:abs_pos]
  1037.                 hud.tmp_attrs[:abs_pos] = [[child.attrs[:x], child.attrs[:y]]]
  1038.               else
  1039.                 hud.tmp_attrs[:abs_pos].push([child.attrs[:x], child.attrs[:y]])
  1040.               end
  1041.             },
  1042.             proc { |child, hud|
  1043.               hud.tmp_attrs[:abs_pos].pop
  1044.               hud.tmp_attrs.delete(:abs_pos) if hud.tmp_attrs[:abs_pos].length == 0
  1045.             }, el_val, el_attrs)
  1046.             @children.push(element)
  1047.           when 'offset'
  1048.             element = FormatNode.new('offset', self, contents, proc { |child, hud|
  1049.               if !hud.tmp_attrs[:tmpofst]
  1050.                 hud.tmp_attrs[:tmpofst] = [[hud.tmp_attrs[:ox], hud.tmp_attrs[:oy]]]
  1051.               else
  1052.                 hud.tmp_attrs[:tmpofst].push([hud.tmp_attrs[:ox], hud.tmp_attrs[:oy]])
  1053.               end
  1054.               hud.tmp_attrs[:ox], hud.tmp_attrs[:oy] = child.attrs[:x], child.attrs[:y]
  1055.             },
  1056.             proc { |child, hud|
  1057.               hud.tmp_attrs[:ox], hud.tmp_attrs[:oy] = *hud.tmp_attrs[:tmpofst].pop
  1058.             }, el_val, el_attrs)
  1059.             @children.push(element)
  1060.           when 'right'
  1061.             element = FormatNode.new('right', self, contents, proc { |child, hud|
  1062.               if !hud.tmp_attrs[:tmpalign]
  1063.                 hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1064.               else
  1065.                 hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1066.               end
  1067.               hud.tmp_attrs[:align] = 2
  1068.               child.attrs[:align] = 2
  1069.             },
  1070.             proc { |child, hud|
  1071.               hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1072.             }, el_val, el_attrs)
  1073.             @children.push(element)
  1074.           when 'center'
  1075.             element = FormatNode.new('center', self, contents, proc { |child, hud|
  1076.               if !hud.tmp_attrs[:tmpalign]
  1077.                 hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1078.               else
  1079.                 hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1080.               end
  1081.               hud.tmp_attrs[:align] = 1
  1082.               child.attrs[:align] = 1
  1083.             },
  1084.             proc { |child, hud|
  1085.               hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1086.             }, el_val, el_attrs)
  1087.             @children.push(element)
  1088.           when 'left'
  1089.             element = FormatNode.new('left', self, contents, proc { |child, hud|
  1090.               if !hud.tmp_attrs[:tmpalign]
  1091.                 hud.tmp_attrs[:tmpalign] = [hud.tmp_attrs[:align]]
  1092.               else
  1093.                 hud.tmp_attrs[:tmpalign].push(hud.tmp_attrs[:align])
  1094.               end
  1095.               hud.tmp_attrs[:align] = 0
  1096.               child.attrs[:align] = 0
  1097.             },
  1098.             proc { |child, hud|
  1099.               hud.tmp_attrs[:align] = hud.tmp_attrs[:tmpalign].pop
  1100.             }, el_val, el_attrs)
  1101.             @children.push(element)
  1102.           end
  1103.         else
  1104.           text.sub!(spattern, '')
  1105.           case el_name
  1106.           when 'hud'
  1107.             element = HudNode.new('hud', el_attrs, self)
  1108.             @children.push(element)
  1109.           when 'var'
  1110.             element = VariableNode.new(self, el_val)
  1111.             @children.push(element)
  1112.           when 'ico'
  1113.             element = IconNode.new(self, el_val)
  1114.             @children.push(element)
  1115.           when 'pic'
  1116.             element = PictureNode.new(self, el_val)
  1117.             @children.push(element)
  1118.           end
  1119.         end
  1120.       else
  1121.         contents = text
  1122.         text = ''
  1123.         element = TextNode.new('', self, contents)
  1124.         @children.push(element)
  1125.       end
  1126.     end
  1127.   end
  1128. end
  1129.  
  1130. class TextNode < Element
  1131.   def initialize(type, parent, text)
  1132.     super(type, {}, parent)
  1133.     @attrs[:val] = text
  1134.   end
  1135. end
  1136.  
  1137. class PictureNode < Element
  1138.   def initialize(parent, val)
  1139.     super('pic', {}, parent)
  1140.     @attrs[:bitmap] = RPG::Cache.picture(val)
  1141.   end
  1142. end
  1143.  
  1144. class IconNode < Element
  1145.   def initialize(parent, val)
  1146.     super('ico', {}, parent)
  1147.     @attrs[:bitmap] = RPG::Cache.icon(el_val).clone
  1148.   end
  1149. end
  1150.  
  1151. class VariableNode < Element
  1152.   def initialize(parent, name)
  1153.     super('var', {}, parent)
  1154.     @attrs[:expr] = DesignHUD.var_expression(name)
  1155.     @attrs[:val] = @attrs[:expr].call || 0
  1156.   end
  1157.  
  1158.   def val
  1159.     tmp = @attrs[:expr].call
  1160.     return (tmp.nil? ? 0 : tmp)
  1161.   end
  1162. end
  1163.  
  1164. class BarNode < Element
  1165.  
  1166. end
  1167.  
  1168. class HudNode < Element
  1169.   def initialize(type, attrs, parent = nil)
  1170.     super('hud', attrs, parent)
  1171.   end
  1172. end
  1173.  
  1174. class ContainerNode < Element
  1175.   def initialize(type, attrs, parent, contents)
  1176.     super(type, attrs, parent)
  1177.    
  1178.     parse_contents(contents)
  1179.   end
  1180. end
  1181.  
  1182. class FormatNode < Element
  1183.   def initialize(type, parent, contents, start, finish, val=nil, attrs = {})
  1184.     super(type, attrs, parent)
  1185.    
  1186.     parse_contents(contents)
  1187.     @attrs[:val] = val if val
  1188.     @attrs[:start] = start
  1189.     @attrs[:finish] = finish
  1190.   end
  1191. end
  1192.  
  1193. class Rect
  1194.   def intersect?(rect)
  1195.     return false if rect.width * rect.height == 0 or self.width * self.height == 0
  1196.     return true if (self.x.between?(rect.x - 1, rect.x + rect.width) or
  1197.                    (self.x + self.width).between?(rect.x - 1, rect.x + rect.width)) and
  1198.                    (self.y.between?(rect.y - 1, rect.y + rect.height) or
  1199.                    (self.y + self.height).between?(rect.y - 1, rect.y + rect.height))
  1200.     return false
  1201.   end
  1202. end
Expand to see the code.


And here's a list of the elements and properties that I've already added:
<hud>
Properties Implemented:
windowskin=FILENAME
bg=FILENAME
width=WIDTH
height=HEIGHT
align=ALIGN
offsetx=OFFSETX
offsety=OFFSETY
back_opacity=OPACITY
contents_opacity=OPACITY
opacity=OPACITY
skin_tone=R:G:B (VXA only)
<ln></ln>
<right></right>
<left></left>
<center></center>
<pos x=X y=Y></pos>
<offset x=X y=Y></pos>
<ico=X:Y> (VXA only)
<ico=FILENAME> (XP only)
<pic=FILENAME>
<b></b>
<i></i>
<c=R:G:B[:A]></c>
<font=NAME:SIZE></font>


Edit - Here's a screenie of some of the different types of gradient bars.
Image
this is looking like a really cool project.

the different types of gradient bars are looking really shitty, the way you presented them, but it gives a representation of what cán be done, which is nice.
this was something I could've used back when I was using RMXP, nice job!
I know that they suck right now, but once I've finished making them look better you'll be able to choose the style, shape, and color scheme of the bar. So the user will basically be able to decide whether to make their bars look awful or good.
cant wait for this to be finished in rmxp.. this is an awesome project you have on the go.
Thanks, I'm not working on it at the moment but soon I'll go back to work and try to finish up the tags. By the way, does anyone have an idea for any tags that I should add?

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=77082