Class: MiniGL::ProgressBar

Inherits:
Component show all
Defined in:
lib/minigl/forms.rb

Overview

Represents a progress bar.

Instance Attribute Summary collapse

Attributes inherited from Component

#enabled, #h, #params, #visible, #w, #x, #y

Instance Method Summary collapse

Constructor Details

#initialize(x, y = nil, w = nil, h = nil, bg = nil, fg = nil, max_value = 100, value = 100, fg_margin_x = 0, fg_margin_y = 0, font = nil, text_color = 0, format = nil, retro = nil, scale_x = 1, scale_y = 1) ⇒ ProgressBar

Creates a progress bar.

Parameters:

x

The x-coordinate of the progress bar on the screen.

y

The y-coordinate of the progress bar on the screen.

w

Width of the progress bar, in pixels. This is the maximum space the bar foreground can occupy. Note that the width of the foreground image (fg) can be less than this, in which case the image will be horizontally repeated to fill all the needed space.

h

Height of the progress bar. This will be the height of the bar foreground when fg is a color (when it is an image, the height of the image will be kept).

bg

A background image (string or symbol that will be passed to Res.img) or color (in RRGGBB hexadecimal format).

fg

A foreground image (string or symbol that will be passed to Res.img) or color (in RRGGBB hexadecimal format). The image will be horizontally repeated when needed, if its width is less than w.

max_value

The maximum value the progress bar can reach (an integer).

value

The starting value for the progress bar.

fg_margin_x

Horizontal margin between the background image and the foreground image (when these are provided).

fg_margin_y

Vertical margin between the background image and the foreground image (when these are provided).

font

Font that will be used to draw a text indicating the value of the progress bar.

text_color

Color of the text.

format

Format to display the value. Specify ‘%’ for a percentage and anything else for absolute values (current/maximum).

retro

Whether the images should be loaded with the ‘retro’ option set (see Gosu::Image for details). If the value is omitted, the Res.retro_images value will be used.

scale_x

Horizontal scale to draw the component with.

scale_y

Vertical scale to draw the component with.

Obs.: This method accepts named parameters, but x, y, w, h, bg and fg are mandatory.



914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'lib/minigl/forms.rb', line 914

def initialize(x, y = nil, w = nil, h = nil, bg = nil, fg = nil,
               max_value = 100, value = 100, fg_margin_x = 0, fg_margin_y = 0, # fg_left = nil, fg_right = nil,
               font = nil, text_color = 0, format = nil, retro = nil, scale_x = 1, scale_y = 1)
  if x.is_a? Hash
    y = x[:y]
    w = x[:w]
    h = x[:h]
    bg = x[:bg]
    fg = x[:fg]
    max_value = x.fetch(:max_value, 100)
    value = x.fetch(:value, 100)
    fg_margin_x = x.fetch(:fg_margin_x, 0)
    fg_margin_y = x.fetch(:fg_margin_y, 0)
    font = x.fetch(:font, nil)
    text_color = x.fetch(:text_color, 0)
    format = x.fetch(:format, nil)
    retro = x.fetch(:retro, nil)
    scale_x = x.fetch(:scale_x, 1)
    scale_y = x.fetch(:scale_y, 1)
    x = x[:x]
  end

  super x, y, font, '', text_color, text_color
  @scale_x = scale_x
  @scale_y = scale_y
  @w = w * @scale_x
  @h = h * @scale_y
  retro = Res.retro_images if retro.nil?
  if bg.is_a? Integer
    @bg_color = bg
  else # String or Symbol
    @bg = Res.img bg, false, false, '.png', retro
  end
  if fg.is_a? Integer
    @fg_color = fg
  else # String or Symbol
    @fg = Res.img fg, false, false, '.png', retro
    @fg_path = "#{Res.prefix}#{Res.img_dir}#{fg.to_s.gsub(Res.separator, '/')}.png"
  end
  @fg_margin_x = fg_margin_x * @scale_x
  @fg_margin_y = fg_margin_y * @scale_y
  # @fg_left = fg_left
  # @fg_right = fg_right
  @max_value = max_value
  self.value = value
  @format = format
  @retro = retro
end

Instance Attribute Details

#max_valueObject (readonly)

The maximum value for this progress bar (when the current value equals the maximum, the bar is full).



872
873
874
# File 'lib/minigl/forms.rb', line 872

def max_value
  @max_value
end

#valueObject

The current value of the progress bar (an integer greater than or equal to zero, and less than or equal to max_value).



876
877
878
# File 'lib/minigl/forms.rb', line 876

def value
  @value
end

Instance Method Details

#decrease(amount) ⇒ Object

Descreases the current value of the progress bar by the given amount.

Parameters:

amount

(Integer) The amount to be subtracted from the current value. If the result is less than zero, it is set to zero.



978
979
980
981
# File 'lib/minigl/forms.rb', line 978

def decrease(amount)
  @value -= amount
  @value = 0 if @value < 0
end

#draw(alpha = 0xff, z_index = 0, color = 0xffffff) ⇒ Object

Draws the progress bar.

Parameters:

alpha

(Fixnum) The opacity with which the progress bar will be drawn. Allowed values vary between 0 (fully transparent) and 255 (fully opaque).

z_index

(Fixnum) The z-order to draw the object. Objects with larger z-orders will be drawn on top of the ones with smaller z-orders.

color

Color to apply a filter to the images (when these are provided).



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/minigl/forms.rb', line 1016

def draw(alpha = 0xff, z_index = 0, color = 0xffffff)
  return unless @visible

  if @bg
    c = (alpha << 24) | color
    @bg.draw @x, @y, z_index, @scale_x, @scale_y, c
  else
    c = (alpha << 24) | @bg_color
    G.window.draw_quad @x, @y, c,
                       @x + @w, @y, c,
                       @x + @w, @y + @h, c,
                       @x, @y + @h, c, z_index
  end
  if @fg
    c = (alpha << 24) | color
    w1 = @fg.width * @scale_x
    w2 = (@value.to_f / @max_value * @w).round
    x0 = @x + @fg_margin_x
    x = 0
    while x <= w2 - w1
      @fg.draw x0 + x, @y + @fg_margin_y, z_index, @scale_x, @scale_y, c
      x += w1
    end
    if w2 - x > 0
      img = Gosu::Image.new(@fg_path, tileable: true, retro: @retro, rect: [0, 0, ((w2 - x) / @scale_x).round, @fg.height])
      img.draw x0 + x, @y + @fg_margin_y, z_index, @scale_x, @scale_y, c
    end
  else
    c = (alpha << 24) | @fg_color
    rect_r = @x + (@value.to_f / @max_value * @w).round
    G.window.draw_quad @x, @y, c,
                       rect_r, @y, c,
                       rect_r, @y + @h, c,
                       @x, @y + @h, c, z_index
  end
  if @font
    c = (alpha << 24) | @text_color
    @text = @format == '%' ? "#{(@value.to_f / @max_value * 100).round}%" : "#{@value}/#{@max_value}"
    @font.draw_rel @text, @x + @fg_margin_x + @w / 2, @y + @fg_margin_y + @h / 2, 0, 0.5, 0.5, @scale_x, @scale_y, c
  end
end

#increase(amount) ⇒ Object

Increases the current value of the progress bar by the given amount.

Parameters:

amount

(Integer) The amount to be added to the current value. If the sum surpasses max_value, it is set to max_value.



968
969
970
971
# File 'lib/minigl/forms.rb', line 968

def increase(amount)
  @value += amount
  @value = @max_value if @value > @max_value
end

#percentage=(pct) ⇒ Object

Sets the value of the progress bar to a given percentage of max_value.

Parameters:

pct

(Numeric) The percentage of max_value to set the current value to. The final result will be changed as needed to be between zero and max_value.



1003
1004
1005
# File 'lib/minigl/forms.rb', line 1003

def percentage=(pct)
  self.value = (pct * @max_value).round
end