2023-07-14 00:54:12 +01:00
|
|
|
module Voicemeeter
|
|
|
|
module Events
|
2023-08-18 00:27:43 +01:00
|
|
|
module Director
|
|
|
|
def observers
|
|
|
|
@observers ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def on(event, method = nil, &block)
|
|
|
|
(observers[event] ||= []) << (block || method)
|
2023-07-17 08:34:34 +01:00
|
|
|
end
|
2023-07-14 00:54:12 +01:00
|
|
|
|
2023-08-03 15:07:18 +01:00
|
|
|
def register(cbs)
|
2023-08-29 15:32:23 +01:00
|
|
|
cbs = Array(cbs) unless cbs.respond_to? :each
|
2023-08-18 00:27:43 +01:00
|
|
|
cbs.each { |cb| on(cb.name[3..].to_sym, cb) }
|
2023-07-14 00:54:12 +01:00
|
|
|
end
|
|
|
|
|
2023-08-03 15:07:18 +01:00
|
|
|
def deregister(cbs)
|
2023-08-29 15:32:23 +01:00
|
|
|
cbs = Array(cbs) unless cbs.respond_to? :each
|
2023-08-18 00:27:43 +01:00
|
|
|
cbs.each { |cb| observers[cb.name[3..].to_sym]&.reject! { |o| cbs.include? o } }
|
2023-07-14 00:54:12 +01:00
|
|
|
end
|
|
|
|
|
2023-08-18 00:27:43 +01:00
|
|
|
def fire(event)
|
|
|
|
observers[event]&.each { |block| block.call }
|
2023-07-14 00:54:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Tracker
|
2023-07-16 11:08:24 +01:00
|
|
|
include Logging
|
2023-07-14 11:44:49 +01:00
|
|
|
|
2023-08-30 12:27:50 +01:00
|
|
|
class << self
|
|
|
|
private def attr_events(*params)
|
|
|
|
params.each do |param|
|
|
|
|
define_method("#{param}=") do |value|
|
|
|
|
instance_variable_set("@#{param}", value)
|
|
|
|
info("#{param} #{value ? "added to" : "removed from"}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-14 00:54:12 +01:00
|
|
|
attr_reader :pdirty, :mdirty, :midi, :ldirty
|
2023-08-30 12:27:50 +01:00
|
|
|
attr_events :pdirty, :mdirty, :midi, :ldirty
|
2023-07-14 00:54:12 +01:00
|
|
|
|
2023-07-27 21:54:53 +01:00
|
|
|
def initialize(**kwargs)
|
|
|
|
kwargs.each do |key, value|
|
|
|
|
instance_variable_set("@#{key}", value || false)
|
|
|
|
end
|
2023-07-14 00:54:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2023-07-14 11:44:49 +01:00
|
|
|
self.class.name.split("::").last.to_s
|
2023-07-14 00:54:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def info(msg = nil)
|
|
|
|
info_msg = msg ? ["#{msg} events."] : []
|
2023-07-31 15:04:34 +01:00
|
|
|
info_msg << if any?
|
2023-07-14 11:44:49 +01:00
|
|
|
["Now listening for #{get.join(", ")} events"]
|
2023-07-14 00:54:12 +01:00
|
|
|
else
|
2023-07-14 11:44:49 +01:00
|
|
|
["Not listening for any events"]
|
2023-07-14 00:54:12 +01:00
|
|
|
end
|
2023-08-01 23:41:23 +01:00
|
|
|
logger.info info_msg.join(" ")
|
2023-07-14 00:54:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def get
|
2023-07-20 09:50:44 +01:00
|
|
|
%i[pdirty mdirty midi ldirty].reject { |ev| !send(ev) }
|
2023-07-14 00:54:12 +01:00
|
|
|
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
|
2023-07-14 00:54:12 +01:00
|
|
|
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
|
2023-07-14 00:54:12 +01:00
|
|
|
events.each { |e| send("#{e}=", false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|