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
- THICKNESS =
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
- #horizontal? ⇒ Boolean
-
#initialize(name = Zif.unique_name('progress_bar'), length = 100, progress = 0.0, color = :blue, orientation = :horizontal) ⇒ ProgressBar
constructor
When orientation is horizontal, length is Width otherwise it is Height.
- #length ⇒ Object
-
#length=(new_length) ⇒ Object
for actions.
- #progress=(new_progress) ⇒ Object
- #resize_length(length) ⇒ Object
Methods inherited from Zif::CompoundSprite
#draw_override, #source_rect, #transform
Methods inherited from Zif::Sprite
#blendmode_enum=, #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'), length = 100, progress = 0.0, color = :blue, orientation = :horizontal) ⇒ ProgressBar
When orientation is horizontal, length is Width otherwise it is Height
26 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/ui/components/progress_bar.rb', line 26 def initialize(name=Zif.unique_name('progress_bar'), length=100, progress=0.0, color=:blue, orientation=:horizontal) super(name) @progress = [[progress, 1.0].min, 0.0].max @orientation = orientation @shadow = [] @filled_bar = [] if horizontal? @h = THICKNESS else @w = THICKNESS end @shadow_lo = Zif::Sprite.new.tap do |s| s.x = 0 s.y = 0 if horizontal? s.w = EDGE_MARGIN s.h = THICKNESS s.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_shadow_left.png" else s.w = THICKNESS s.h = EDGE_MARGIN s.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_shadow_bottom.png" end end @shadow_mid = Zif::Sprite.new.tap do |s| if horizontal? s.x = EDGE_MARGIN s.y = 0 s.h = THICKNESS else s.x = 0 s.y = EDGE_MARGIN s.w = THICKNESS end s.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_shadow_mid.png" end @shadow_hi = Zif::Sprite.new.tap do |s| if horizontal? s.y = 0 s.w = EDGE_MARGIN s.h = THICKNESS s.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_shadow_right.png" else s.x = 0 s.w = THICKNESS s.h = EDGE_MARGIN s.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_shadow_top.png" end end @shadow = [@shadow_lo, @shadow_mid, @shadow_hi] @filled_bar_lo = Zif::Sprite.new.tap do |s| s.x = 0 s.y = 0 if horizontal? s.w = EDGE_MARGIN s.h = THICKNESS else s.w = THICKNESS s.h = EDGE_MARGIN end end @filled_bar_mid = Zif::Sprite.new.tap do |s| if horizontal? s.x = EDGE_MARGIN s.y = 0 s.h = THICKNESS else s.x = 0 s.y = EDGE_MARGIN s.w = THICKNESS end end @filled_bar_hi = Zif::Sprite.new.tap do |s| if horizontal? s.y = 0 s.w = EDGE_MARGIN s.h = THICKNESS else s.x = 0 s.w = THICKNESS s.h = EDGE_MARGIN end end @filled_bar = [@filled_bar_lo, @filled_bar_mid, @filled_bar_hi] @sprites = @shadow + @filled_bar change_color(color) resize_length(length) 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_length ⇒ Object
21 22 23 |
# File 'app/ui/components/progress_bar.rb', line 21 def self.min_length EDGE_MARGIN + 1 + EDGE_MARGIN end |
Instance Method Details
#apply_progress ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'app/ui/components/progress_bar.rb', line 147 def apply_progress cur_length = (@progress * (length - 2 * EDGE_MARGIN)).round if cur_length.zero? @filled_bar.each(&:hide) else @filled_bar.each(&:show) if horizontal? @filled_bar_mid.w = cur_length @filled_bar_hi.x = cur_length + EDGE_MARGIN else @filled_bar_mid.h = cur_length @filled_bar_hi.y = cur_length + EDGE_MARGIN end end end |
#change_color(color) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'app/ui/components/progress_bar.rb', line 133 def change_color(color) @color = VALID_COLORS.include?(color) ? color : :blue @filled_bar_mid.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_#{@color}_mid.png" if horizontal? @filled_bar_lo.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_#{@color}_left.png" @filled_bar_hi.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_#{@color}_right.png" else @filled_bar_lo.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_#{@color}_bottom.png" @filled_bar_hi.path = "#{SPRITES_PATH}/#{SPRITE_NAMES[@orientation]}_#{@color}_top.png" end end |
#horizontal? ⇒ Boolean
129 130 131 |
# File 'app/ui/components/progress_bar.rb', line 129 def horizontal? @orientation == :horizontal end |
#length ⇒ Object
177 178 179 |
# File 'app/ui/components/progress_bar.rb', line 177 def length horizontal? ? @w : @h end |
#length=(new_length) ⇒ Object
for actions
173 174 175 |
# File 'app/ui/components/progress_bar.rb', line 173 def length=(new_length) resize_length(new_length) end |
#progress=(new_progress) ⇒ Object
164 165 166 167 168 169 170 |
# File 'app/ui/components/progress_bar.rb', line 164 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_length(length) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/ui/components/progress_bar.rb', line 181 def resize_length(length) return if @w == length && horizontal? || @h == length && !horizontal? if horizontal? @w = length @shadow_mid.w = @w - (2 * EDGE_MARGIN) @shadow_hi.x = @w - EDGE_MARGIN else @h = length @shadow_mid.h = @h - (2 * EDGE_MARGIN) @shadow_hi.y = @h - EDGE_MARGIN end apply_progress end |