voicemeeter-rb/lib/voicemeeter/event.rb

85 lines
2.0 KiB
Ruby
Raw Normal View History

require_relative "logger"
2023-07-14 11:44:49 +01:00
module Voicemeeter
module Events
module Callback
private def callbacks
@callbacks ||= []
end
def register(*cbs)
cbs.each { |cb| callbacks << cb unless callbacks.include? cb }
end
def deregister(*cbs)
callbacks.reject! { |cb| cbs.include? cb }
end
private def trigger(event)
callbacks.each do |callback|
if callback.is_a? Method
callback.call if callback.name == event
elsif callback.respond_to? :on_update
2023-07-18 07:30:04 +01:00
callback.on_update event.to_s[3..]
end
end
end
end
class Tracker
include Logging
2023-07-14 11:44:49 +01:00
attr_reader :pdirty, :mdirty, :midi, :ldirty
def initialize(**kwargs)
make_writer_methods :pdirty, :mdirty, :midi, :ldirty
kwargs.each do |key, value|
instance_variable_set("@#{key}", value || false)
end
end
def to_s
2023-07-14 11:44:49 +01:00
self.class.name.split("::").last.to_s
end
def info(msg = nil)
info_msg = msg ? ["#{msg} events."] : []
2023-07-14 11:44:49 +01:00
info_msg += if any?
["Now listening for #{get.join(", ")} events"]
else
2023-07-14 11:44:49 +01:00
["Not listening for any events"]
end
logger.info(info_msg.join(" "))
end
private def make_writer_methods(*params)
params.each do |param|
define_singleton_method("#{param}=") do |value|
instance_variable_set("@#{param}", value)
info("#{param} #{send(param) ? "added to" : "removed from"}")
end
end
end
def get
2023-07-20 09:50:44 +01:00
%i[pdirty mdirty midi ldirty].reject { |ev| !send(ev) }
end
def any?
[pdirty, mdirty, midi, ldirty].any?
end
def add(events)
2023-07-22 16:44:24 +01:00
events = [events] unless events.respond_to? :each
events.each { |e| send("#{e}=", true) }
end
def remove(events)
2023-07-22 16:44:24 +01:00
events = [events] unless events.respond_to? :each
events.each { |e| send("#{e}=", false) }
end
end
end
end