Class: MittensUi::Knob

Inherits:
Core
  • Object
show all
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.

Examples:

Basic knob

knob = MittensUi::Knob.new(min: 0, max: 100, value: 50)
knob.on_change { |v| puts "Value: #{v}" }

With label and custom size

knob = MittensUi::Knob.new(
  min:   0,
  max:   127,
  value: 64,
  size:  80,
  label: "Cutoff",
  color: [0.2, 0.6, 1.0]
)

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

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.

Parameters:

  • options (Hash) (defaults to: {})

    configuration options

Options Hash (options):

  • :min (Float, Integer) — default: 0

    minimum value

  • :max (Float, Integer) — default: 100

    maximum value

  • :value (Float, Integer) — default: 50

    initial value

  • :size (Integer) — default: 60

    diameter of the knob in pixels

  • :label (String, nil) — default: nil

    optional label shown below the knob

  • :color (Array<Float>) — default: [0.2, 0.8, 0.4]

    RGB color of the knob indicator, each value in range 0.0..1.0

  • :width (Symbol) — default: :quarter

    column width in the layout grid

  • :defer_render (Boolean) — default: false

    skip auto-rendering into layout



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(options = {})
  @min       = options[:min]   || 0
  @max       = options[:max]   || 100
  @value     = options[:value] || (@min + @max) / 2.0
  @size      = options[:size]  || 60
  @label     = options[:label] || nil
  @color     = options[:color] || [0.2, 0.8, 0.4]
  @on_change = nil
  @dragging  = false
  @last_y    = 0
  @last_x    = 0

  options[:width] ||= :quarter

  @container = build_widget
  super(@container, options)
end

Instance Method Details

#on_change {|value| ... } ⇒ void

This method returns an undefined value.

Connects a block that fires whenever the knob value changes.

Examples:

knob.on_change { |v| puts "Knob: #{v}" }

Yields:

  • (value)

    called when the value changes

Yield Parameters:

  • value (Float)

    the new value



83
84
85
# File 'lib/mittens_ui/knob.rb', line 83

def on_change(&block)
  @on_change = block
end

#valueFloat

Returns the current value of the knob.

Returns:

  • (Float)

    the current value



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.

Parameters:

  • val (Float, Integer)

    the new value



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