Advertisement
Vlue

Sleek Gauges

Apr 1st, 2014
2,187
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #--# Sleek Gauges v 1.0b
  2. #
  3. # Transform your various gauges in a variety of ways. Compatible with any
  4. #  other script that uses the draw_gauge function... probably.
  5. #
  6. # Usage: Plug and play, customize as needed.
  7. #
  8. #------#
  9. #-- Script by: V.M of D.T
  10. #
  11. #- Questions or comments can be:
  12. #    given by email: sumptuaryspade@live.ca
  13. #    provided on facebook: http://www.facebook.com/DaimoniousTailsGames
  14. #   All my other scripts and projects can be found here: http://daimonioustails.weebly.com/
  15. #
  16. #--- Free to use in any project, commercial or non-commercial, with credit given
  17. #--Though a donation's always a nice way to say thank you~ (I also accept actual thank you's)
  18.  
  19. module SPECIAL_GAUGES
  20.   #Sets the default height of all bars
  21.   DEFAULT_HEIGHT = 8
  22.   #Set to true for static numbers (they don't count down)
  23.   STATIC_NUMBERS = false
  24.   #Set to true for a static bar (Slide style is ignored)
  25.   STATIC_GAUGE = false
  26.   #The style of the gauge. Options are :flat, :round, :slant
  27.   GAUGE_STYLE = :slant
  28.   #The style as the gauge changes. Options are :normal, :fancy, :fall
  29.   GAUGE_SLIDE_STYLE = :fancy
  30. end
  31.  
  32. class Window_Base < Window
  33.   attr_accessor :gauges
  34.   def draw_gauge(x, y, width, rate, color1, color2, height = SPECIAL_GAUGES::DEFAULT_HEIGHT)
  35.     @gauges = {} unless @gauges
  36.     if @gauges[[x,y]]
  37.       @gauges[[x,y]].set_rate(rate)
  38.     else
  39.       @gauges[[x,y]] = Special_Gauge.new(x,y,width,rate,color1,color2,self,height)
  40.     end
  41.   end
  42.   def draw_actor_hp(actor, x, y, width = 124, height = SPECIAL_GAUGES::DEFAULT_HEIGHT, notext = false)
  43.     draw_gauge(x, y, width, actor.hp_rate, hp_gauge_color1, hp_gauge_color2, height)
  44.     @gauges[[x,y]].set_extra(Vocab::hp_a,actor.hp,actor.mhp) unless notext
  45.   end
  46.   def draw_actor_mp(actor, x, y, width = 124, height = SPECIAL_GAUGES::DEFAULT_HEIGHT, notext = false)
  47.     draw_gauge(x, y, width, actor.mp_rate, mp_gauge_color1, mp_gauge_color2, height)
  48.     @gauges[[x,y]].set_extra(Vocab::mp_a,actor.mp,actor.mmp) unless notext
  49.   end
  50.   def draw_actor_tp(actor, x, y, width = 124, height = SPECIAL_GAUGES::DEFAULT_HEIGHT, notext = false)
  51.     draw_gauge(x, y, width, actor.tp_rate, tp_gauge_color1, tp_gauge_color2, height)
  52.     @gauges[[x,y]].set_extra(Vocab::tp_a,actor.tp.to_i,100) unless notext
  53.   end
  54.   alias gauge_update update
  55.   def update
  56.     gauge_update
  57.     if @gauges
  58.       @gauges.each {|k,gauge| gauge.update}
  59.     end
  60.   end
  61. end
  62.  
  63. class Special_Gauge
  64.   attr_accessor :cur_val
  65.   def initialize(x,y,w,r,c1,c2,window,height = SPECIAL_GAUGES::DEFAULT_HEIGHT)
  66.     @x = x
  67.     @y = y
  68.     @width = w
  69.     @cur_rate = r
  70.     @max_rate = r
  71.     @color1 = c1
  72.     @color2 = c2
  73.     @window = window
  74.     @speed = 0
  75.     @speed_rate = 0
  76.     @height = height
  77.     @fall_sprites = []
  78.     refresh
  79.   end
  80.   def update
  81.     update_fall_sprites
  82.     return if @cur_rate == @max_rate && @cur_val == @set_val
  83.     @cur_rate -= @speed_rate if @cur_rate > @max_rate
  84.     @cur_rate += @speed_rate if @cur_rate < @max_rate
  85.     @cur_rate = @max_rate if (@cur_rate - @max_rate).abs < @speed_rate
  86.     @cur_rate = @max_rate if SPECIAL_GAUGES::STATIC_GAUGE
  87.     return unless @vocab
  88.     @cur_val -= @speed if @cur_val > @set_val
  89.     @cur_val += @speed if @cur_val < @set_val
  90.     @cur_val = @set_val if (@cur_val-@set_val).abs < @speed
  91.     @cur_val = @set_val if SPECIAL_GAUGES::STATIC_NUMBERS
  92.     refresh
  93.   end
  94.   def update_fall_sprites
  95.     @fall_sprites.each do |sprite|
  96.       if sprite.opacity < 175
  97.         sprite.y += 1
  98.       else
  99.         sprite.y -= 1
  100.       end
  101.       if !@window.viewport.nil?
  102.         xx = @window.x + @window.padding + @window.viewport.rect.x - @window.viewport.ox
  103.       else
  104.         xx = @window.x + @window.padding;yy = @window.y + @window.padding
  105.       end
  106.       sprite.x = xx + @x + @width * @max_rate
  107.       sprite.opacity -= 5
  108.       sprite.dispose if sprite.opacity == 0
  109.     end
  110.     @fall_sprites = @fall_sprites.select {|sprite| !sprite.disposed? }
  111.   end
  112.   def refresh
  113.     if @vocab
  114.       @window.contents.clear_rect(Rect.new(@x,@y,@width,@window.line_height))
  115.     else
  116.       gauge_y = @y + @window.line_height - @height - 1
  117.       @window.contents.clear_rect(Rect.new(@x,gauge_y,@width,@height))
  118.     end
  119.     draw_gauge(@x,@y,@width,@cur_rate,@color1,@color2)
  120.     draw_text(@x,@y,@width)
  121.   end
  122.   def set_rate(rate)
  123.     reset_speed = rate != @max_rate
  124.     @max_rate = rate
  125.     @speed_rate = (@cur_rate-@max_rate).abs / 60 if reset_speed
  126.     if SPECIAL_GAUGES::GAUGE_SLIDE_STYLE == :fall && reset_speed && !SPECIAL_GAUGES::STATIC_GAUGE
  127.       if @cur_rate > @max_rate
  128.         sprite = Sprite.new()
  129.         if !@window.viewport.nil?
  130.           xx = @window.x + @window.padding + @window.viewport.rect.x - @window.viewport.ox
  131.           yy = @window.y + @window.padding + @window.viewport.rect.y - @window.viewport.oy
  132.         else
  133.           xx = @window.x + @window.padding;yy = @window.y + @window.padding
  134.         end
  135.         sprite.x = xx + @x + @width * @max_rate
  136.         sprite.y = yy + @y + @window.line_height - @height
  137.         sprite.z = @window.z + 1
  138.         width = (@width * @cur_rate).to_i - (@width * @max_rate).to_i
  139.         if width > 1
  140.           sprite.bitmap = Bitmap.new(width,@height-2)
  141.           sprite.bitmap.gradient_fill_rect(sprite.bitmap.rect,@color1,@color2)
  142.           @fall_sprites.push(sprite)
  143.         else
  144.           sprite.dispose
  145.         end
  146.       end
  147.     end
  148.     refresh
  149.   end
  150.   def set_extra(vocab,set_val,max_val)
  151.     @vocab = vocab
  152.     @max_val = max_val
  153.     reset_speed = set_val != @set_val
  154.     if @cur_val
  155.       @set_val = set_val
  156.     else
  157.       @cur_val = set_val
  158.       @set_val = set_val
  159.     end
  160.     @speed = (@cur_val-@set_val).abs / 60 if reset_speed
  161.     @speed = 1 if @speed == 0
  162.     refresh
  163.   end
  164.   def draw_gauge(x, y, width, rate, color1, color2)
  165.     fill_w = (width * rate).to_i
  166.     fill_ww = (width * @max_rate).to_i
  167.     gauge_y = y + @window.line_height - @height - 1
  168.     @window.contents.fill_rect(x, gauge_y, width, @height, @window.gauge_back_color)
  169.     if SPECIAL_GAUGES::GAUGE_SLIDE_STYLE == :fancy
  170.       color1.alpha -= 150;color2.alpha -= 150
  171.       if rate > @max_rate
  172.         @window.contents.gradient_fill_rect(x, gauge_y+1, fill_w, @height-2, color1, color2)
  173.         color1.alpha += 150;color2.alpha += 150
  174.         @window.contents.gradient_fill_rect(x, gauge_y+1, fill_ww, @height-2, color1, color2)
  175.       else rate < @max_rate
  176.         @window.contents.gradient_fill_rect(x, gauge_y+1, fill_ww, @height-2, color1, color2)
  177.         color1.alpha += 150;color2.alpha += 150
  178.         @window.contents.gradient_fill_rect(x, gauge_y+1, fill_w, @height-2, color1, color2)
  179.       end
  180.     elsif SPECIAL_GAUGES::GAUGE_SLIDE_STYLE == :normal
  181.       @window.contents.gradient_fill_rect(x, gauge_y+1, fill_w, @height-2, color1, color2)
  182.     elsif SPECIAL_GAUGES::GAUGE_SLIDE_STYLE == :fall
  183.       if rate > @max_rate
  184.         @window.contents.gradient_fill_rect(x, gauge_y+1, fill_ww, @height-2, color1, color2)
  185.       else
  186.         @window.contents.gradient_fill_rect(x, gauge_y+1, fill_w, @height-2, color1, color2)
  187.       end
  188.     end
  189.     color3 = Color.new(0,0,0,0);color4 = @window.gauge_back_color
  190.      xx = x;yy = gauge_y;inc = 0
  191.     case SPECIAL_GAUGES::GAUGE_STYLE
  192.     when :flat
  193.     when :slant
  194.       inc = @height-1
  195.       @height.times do
  196.         inc.times do
  197.           @window.contents.set_pixel(xx,yy,color3)
  198.           xx += 1
  199.         end
  200.         @window.contents.set_pixel(xx,yy,color4)
  201.         xx = x;yy += 1;inc -= 1
  202.       end
  203.       xx = x + width;yy = gauge_y;inc = 0
  204.       @height.times do
  205.         inc.times do
  206.           @window.contents.set_pixel(xx,yy,color3)
  207.           xx -= 1
  208.         end
  209.         @window.contents.set_pixel(xx,yy,color4)
  210.         xx = x + width;yy += 1;inc += 1
  211.       end
  212.     when :round
  213.       inc = 3
  214.       @height.times do |i|
  215.         inc = 0 if inc < 0
  216.         inc.times do
  217.           @window.contents.set_pixel(xx,yy,color3)
  218.           xx += 1
  219.         end
  220.         @window.contents.set_pixel(xx,yy,color4)
  221.         xx = x;yy += 1;inc -= 1
  222.         if i >= @height - 4
  223.           inc += 2
  224.         end
  225.       end
  226.       xx = x + width;yy = gauge_y;inc = 3
  227.       @height.times do |i|
  228.         inc = 0 if inc < 0
  229.         inc.times do
  230.           @window.contents.set_pixel(xx,yy,color3)
  231.           xx -= 1
  232.         end
  233.         @window.contents.set_pixel(xx,yy,color4)
  234.         xx = x + width;yy += 1;inc -= 1
  235.         if i >= @height - 4
  236.           inc += 2
  237.         end
  238.       end
  239.     end
  240.   end
  241.   def draw_text(x,y,w)
  242.     return unless @cur_val
  243.     @window.change_color(@window.system_color)
  244.     @window.draw_text(x, y, 30, @window.line_height, @vocab)
  245.     @window.change_color(@window.normal_color)
  246.     @window.change_color(@window.crisis_color) if @cur_val < @max_val / 4
  247.     xr = x + w
  248.     if w < 96
  249.       @window.draw_text(xr - 40, y, 42, @window.line_height, @cur_val.to_i, 2)
  250.     else
  251.       @window.draw_text(xr - 92, y, 42, @window.line_height, @cur_val.to_i, 2)
  252.       @window.change_color(@window.normal_color)
  253.       @window.draw_text(xr - 52, y, 12, @window.line_height, "/", 2)
  254.       @window.draw_text(xr - 42, y, 42, @window.line_height, @max_val, 2)
  255.     end
  256.   end
  257. end
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement