Class: ExampleApp::ProgressBar
- Inherits:
-
Zif::CompoundSprite
- Object
- Zif::Sprite
- Zif::CompoundSprite
- ExampleApp::ProgressBar
- Defined in:
- app/ui/components/progress_bar.rb
Overview
It’s a progress bar! A shadow appears for the full length. Filling the shadow is a bar with fixed edges and stretchy center. Setting the progress causes a redraw. If progress is zero, the filled bar disappears.
Constant Summary collapse
- SPRITES_PATH =
'sprites/kenney-uipack-space/PNG'.freeze
- EDGE_MARGIN =
6
- HEIGHT =
26
- SPRITE_NAMES =
{ horizontal: 'barHorizontal', vertical: 'barVertical' }.freeze
- VALID_COLORS =
%i[blue green red white yellow].freeze
Constants inherited from Zif::Sprite
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#filled_bar ⇒ Object
Returns the value of attribute filled_bar.
-
#orientation ⇒ Object
Returns the value of attribute orientation.
-
#shadow ⇒ Object
Returns the value of attribute shadow.
Attributes inherited from Zif::CompoundSprite
Attributes inherited from Zif::Sprite
#a, #angle, #b, #g, #h, #logical_x, #logical_y, #name, #path, #r, #render_target, #source_h, #source_w, #source_x, #source_y, #w, #x, #y, #z_index
Attributes included from Zif::Clickable
#on_mouse_changed, #on_mouse_down, #on_mouse_up
Attributes included from Zif::Actions::Animatable
#animation_sequences, #cur_animation
Attributes included from Zif::Actions::Actionable
Class Method Summary collapse
Instance Method Summary collapse
- #apply_progress ⇒ Object
- #change_color(color) ⇒ Object
-
#initialize(name = Zif.unique_name('progress_bar'), width = 100, progress = 0.0, color = :blue, orientation = :horizontal) ⇒ ProgressBar
constructor
Width and Height assume horizontal orientation, naming is inverted for vertical TODO: :vertical not yet supported..
- #progress=(new_progress) ⇒ Object
- #resize_width(width) ⇒ Object
- #width ⇒ Object
-
#width=(new_width) ⇒ Object
alias for actions.
Methods inherited from Zif::CompoundSprite
Methods inherited from Zif::Sprite
#blend, #blend=, #center, #center_x, #center_y, #clicked?, #dup_and_assign, #exclude_from_serialize, #hide, #rect, rect_array_to_hash, rect_array_to_source_hash, #rect_hash, rect_hash_to_source_hash, #show, #source_as_rect_hash, #source_center, #source_is_set?, #source_rect, #source_rect_hash, #source_wh, #source_xy, #to_h, #view_actual_size!, #wh, #xy, #zoom_factor
Methods included from Zif::Clickable
Methods included from Zif::Actions::Animatable
#new_basic_animation, #new_tiled_animation, #register_animation_sequence, #run_animation_sequence, #stop_animating
Methods included from Zif::Actions::Actionable
#bounce_forever_around, #delayed_action, #fade_in, #fade_out, #fade_out_and_in_forever, #new_action, #perform_actions, #run_action, #running_actions?, #stop_action
Methods included from Zif::Serializable
#exclude_from_serialize, #inspect, #serialize, #to_s
Methods included from Zif::Assignable
Constructor Details
#initialize(name = Zif.unique_name('progress_bar'), width = 100, progress = 0.0, color = :blue, orientation = :horizontal) ⇒ ProgressBar
Width and Height assume horizontal orientation, naming is inverted for vertical TODO: :vertical not yet supported.. pull requests welcome.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/ui/components/progress_bar.rb', line 27 def initialize(name=Zif.unique_name('progress_bar'), width=100, progress=0.0, color=:blue, orientation=:horizontal) super(name) @progress = [[progress, 1.0].min, 0.0].max @orientation = orientation @shadow = [] @filled_bar = [] @h = HEIGHT @shadow_left = Zif::Sprite.new.tap do |s| s.x = 0 s.y = 0 s.w = EDGE_MARGIN s.h = HEIGHT s.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_shadow_left.png" end @shadow_mid = Zif::Sprite.new.tap do |s| s.x = EDGE_MARGIN s.y = 0 s.h = HEIGHT s.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_shadow_mid.png" end @shadow_right = Zif::Sprite.new.tap do |s| s.y = 0 s.w = EDGE_MARGIN s.h = HEIGHT s.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_shadow_right.png" end @shadow = [@shadow_left, @shadow_mid, @shadow_right] @filled_bar_left = Zif::Sprite.new.tap do |s| s.x = 0 s.y = 0 s.w = EDGE_MARGIN s.h = HEIGHT end @filled_bar_mid = Zif::Sprite.new.tap do |s| s.x = EDGE_MARGIN s.y = 0 s.h = HEIGHT end @filled_bar_edge = Zif::Sprite.new.tap do |s| s.y = 0 s.w = EDGE_MARGIN s.h = HEIGHT end @filled_bar = [@filled_bar_left, @filled_bar_mid, @filled_bar_edge] @sprites = @shadow + @filled_bar change_color(color) resize_width(width) end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
10 11 12 |
# File 'app/ui/components/progress_bar.rb', line 10 def color @color end |
#filled_bar ⇒ Object
Returns the value of attribute filled_bar.
10 11 12 |
# File 'app/ui/components/progress_bar.rb', line 10 def @filled_bar end |
#orientation ⇒ Object
Returns the value of attribute orientation.
10 11 12 |
# File 'app/ui/components/progress_bar.rb', line 10 def orientation @orientation end |
#shadow ⇒ Object
Returns the value of attribute shadow.
10 11 12 |
# File 'app/ui/components/progress_bar.rb', line 10 def shadow @shadow end |
Class Method Details
.min_width ⇒ Object
21 22 23 |
# File 'app/ui/components/progress_bar.rb', line 21 def self.min_width EDGE_MARGIN + 1 + EDGE_MARGIN end |
Instance Method Details
#apply_progress ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/ui/components/progress_bar.rb', line 95 def apply_progress cur_width = (@progress * (@w - 2 * EDGE_MARGIN)).round if cur_width.zero? @filled_bar.each(&:hide) else @filled_bar.each(&:show) @filled_bar_mid.w = cur_width @filled_bar_edge.x = cur_width + EDGE_MARGIN end end |
#change_color(color) ⇒ Object
87 88 89 90 91 92 93 |
# File 'app/ui/components/progress_bar.rb', line 87 def change_color(color) @color = VALID_COLORS.include?(color) ? color : :blue @filled_bar_left.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_#{@color}_left.png" @filled_bar_mid.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_#{@color}_mid.png" @filled_bar_edge.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_#{@color}_right.png" end |
#progress=(new_progress) ⇒ Object
107 108 109 110 111 112 113 |
# File 'app/ui/components/progress_bar.rb', line 107 def progress=(new_progress) clamped_progress = [[new_progress, 1.0].min, 0.0].max return unless @progress != clamped_progress @progress = clamped_progress apply_progress end |
#resize_width(width) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'app/ui/components/progress_bar.rb', line 124 def resize_width(width) return if @w == width @w = width @shadow_mid.w = @w - (2 * EDGE_MARGIN) @shadow_right.x = @w - EDGE_MARGIN apply_progress end |
#width ⇒ Object
120 121 122 |
# File 'app/ui/components/progress_bar.rb', line 120 def width @w end |
#width=(new_width) ⇒ Object
alias for actions
116 117 118 |
# File 'app/ui/components/progress_bar.rb', line 116 def width=(new_width) resize_width(new_width) end |