Class: MiniGL::ProgressBar
Overview
Represents a progress bar.
Instance Attribute Summary collapse
-
#max_value ⇒ Object
readonly
The maximum value for this progress bar (when the current value equals the maximum, the bar is full).
-
#value ⇒ Object
The current value of the progress bar (an integer greater than or equal to zero, and less than or equal to
max_value).
Attributes inherited from Component
#enabled, #h, #params, #visible, #w, #x, #y
Instance Method Summary collapse
-
#decrease(amount) ⇒ Object
Descreases the current value of the progress bar by the given amount.
-
#draw(alpha = 0xff, z_index = 0) ⇒ Object
Draws the progress bar.
-
#increase(amount) ⇒ Object
Increases the current value of the progress bar by the given amount.
-
#initialize(x, y, w, h, bg, fg, max_value = 100, value = 100, fg_margin_x = 0, fg_margin_y = 0, font = nil, text_color = 0, format = nil) ⇒ ProgressBar
constructor
Creates a progress bar.
-
#percentage=(pct) ⇒ Object
Sets the value of the progress bar to a given percentage of
max_value.
Constructor Details
#initialize(x, y, w, h, bg, fg, max_value = 100, value = 100, fg_margin_x = 0, fg_margin_y = 0, font = nil, text_color = 0, format = nil) ⇒ 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. - h
-
Height of the progress bar. This will be the height of the bar foreground when
fgis 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 thanw. - 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).
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
# File 'lib/minigl/forms.rb', line 802 def initialize(x, y, w, h, bg, fg, 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) super x, y, font, '', text_color, text_color @w = w @h = h if bg.is_a? Integer @bg_color = bg else # String or Symbol @bg = Res.img bg end if fg.is_a? Integer @fg_color = fg else # String or Symbol @fg = Res.img fg @fg_path = "#{Res.prefix}img/#{fg.to_s.gsub('_', '/')}.png" puts @fg_path end @fg_margin_x = fg_margin_x @fg_margin_y = fg_margin_y # @fg_left = fg_left # @fg_right = fg_right @max_value = max_value self.value = value @format = format end |
Instance Attribute Details
#max_value ⇒ Object (readonly)
The maximum value for this progress bar (when the current value equals the maximum, the bar is full).
769 770 771 |
# File 'lib/minigl/forms.rb', line 769 def max_value @max_value end |
#value ⇒ Object
The current value of the progress bar (an integer greater than or equal to zero, and less than or equal to max_value).
773 774 775 |
# File 'lib/minigl/forms.rb', line 773 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.
843 844 845 846 |
# File 'lib/minigl/forms.rb', line 843 def decrease(amount) @value -= amount @value = 0 if @value < 0 end |
#draw(alpha = 0xff, z_index = 0) ⇒ 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.
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 |
# File 'lib/minigl/forms.rb', line 880 def draw(alpha = 0xff, z_index = 0) return unless @visible if @bg c = (alpha << 24) | 0xffffff @bg.draw @x, @y, z_index, 1, 1, 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) | 0xffffff w1 = @fg.width 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, 1, 1, c x += w1 end if w2 - x > 0 img = Gosu::Image.new(G.window, @fg_path, true, 0, 0, w2 - x, @fg.height) img.draw x0 + x, @y + @fg_margin_y, z_index, 1, 1, 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, 1, 1, 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 surpassesmax_value, it is set tomax_value.
833 834 835 836 |
# File 'lib/minigl/forms.rb', line 833 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 ofmax_valueto set the current value to. The final result will be changed as needed to be between zero andmax_value.
868 869 870 |
# File 'lib/minigl/forms.rb', line 868 def percentage=(pct) self.value = (pct * @max_value).round end |