#pragma once #include "rbx/signal.h" #include "V8DataModel/PartInstance.h" #include "V8DataModel/ModelInstance.h" #include "V8Tree/Instance.h" namespace RBX { class CustomEvent; extern const char* const sCustomEventReceiver; class CustomEventReceiver : public DescribedCreatable { private: typedef DescribedCreatable Super; // prevent copy and assign: this class does sensitive pointer // management, which would be complicated by allowing copies/assigns. CustomEventReceiver(const CustomEventReceiver& other); CustomEventReceiver& operator=(const CustomEventReceiver& other); weak_ptr sourceEvent; rbx::signals::scoped_connection sourceValueChangedConnection; float lastReceivedValue; public: // public for interoperation with CustomEvent rbx::signal sourceValueChanged; // connection signals public for testing rbx::signal)> eventConnected; rbx::signal)> eventDisconnected; CustomEventReceiver() :Super(sCustomEventReceiver), lastReceivedValue(0) { sourceValueChangedConnection = sourceValueChanged.connect( boost::bind(&CustomEventReceiver::setCurrentValue, this, _1)); } static Reflection::RefPropDescriptor prop_Source; static Reflection::EventDesc event_SourceValueChanged; static Reflection::BoundFuncDesc func_GetCurrentValue; static Reflection::EventDesc)> event_EventConnected; static Reflection::EventDesc)> event_EventDisconnected; /*override*/ bool askSetParent(const Instance* instance) const { return Instance::fastDynamicCast(instance) != NULL; } /*override*/ bool askForbidChild(const Instance* instance) const { return true; } // needs to be forward declared because it depends on CustomEvent /*override*/// virtual void onAncestorChanged(const AncestorChanged& event); // should only be used for serialization Instance* const getSource() const { return (Instance*) sourceEvent.lock().get(); } // should only be used for serialization void setSource(Instance* sourceEvent); void setCurrentValue(float newValue) { RBXASSERT(newValue != lastReceivedValue); lastReceivedValue = newValue; } float getCurrentValue() { return lastReceivedValue; } protected: /*override*/ void onServiceProvider(ServiceProvider* oldProvider, ServiceProvider* newProvider); }; }