Class: MittensUi::Knob
- Includes:
- Helpers
- Defined in:
- lib/mittens_ui/knob.rb
Overview
A rotary knob widget that mimics the feel of a synthesizer knob. Built on Gtk::DrawingArea and drawn with Cairo. Click and drag up/right to increase the value, drag down/left to decrease it. Scroll wheel also works.
Instance Attribute Summary
Attributes inherited from Core
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Knob
constructor
Creates a new Knob widget.
-
#on_change {|value| ... } ⇒ void
Connects a block that fires whenever the knob value changes.
-
#value ⇒ Float
Returns the current value of the knob.
-
#value=(val) ⇒ void
Sets the knob value programmatically.
Methods included from Helpers
#icon_map, #list_system_icons, #set_margin_from_opts_for
Methods inherited from Core
#hidden?, #hide, #keyboard_shortcut, #remove, #remove_keyboard_shortcut, #render, #shortcuts, #show
Constructor Details
#initialize(options = {}) ⇒ Knob
Creates a new Knob widget.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mittens_ui/knob.rb', line 40 def initialize( = {}) @min = [:min] || 0 @max = [:max] || 100 @value = [:value] || (@min + @max) / 2.0 @size = [:size] || 60 @label = [:label] || nil @color = [:color] || [0.2, 0.8, 0.4] @on_change = nil @dragging = false @last_y = 0 @last_x = 0 [:width] ||= :quarter @container = super(@container, ) end |
Instance Method Details
#on_change {|value| ... } ⇒ void
This method returns an undefined value.
Connects a block that fires whenever the knob value changes.
83 84 85 |
# File 'lib/mittens_ui/knob.rb', line 83 def on_change(&block) @on_change = block end |
#value ⇒ Float
Returns the current value of the knob.
61 62 63 |
# File 'lib/mittens_ui/knob.rb', line 61 def value @value.round(2) end |
#value=(val) ⇒ void
This method returns an undefined value.
Sets the knob value programmatically. Value is clamped to the min/max range.
70 71 72 73 74 |
# File 'lib/mittens_ui/knob.rb', line 70 def value=(val) @value = val.clamp(@min.to_f, @max.to_f) @drawing_area.queue_draw @on_change&.call(value) end |