You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.1KB

  1. #pragma once
  2. #ifdef OSC_ENABLE
  3. #include "oscCommunicator.hpp"
  4. #include "oscControl.hpp"
  5. class OSCDriver
  6. {
  7. public:
  8. OSCDriver(Module *pmod, int scene)
  9. {
  10. m_scene = scene;
  11. pModule = pmod;
  12. comm = new oscCommunicator(m_scene);
  13. lastCheck = 0;
  14. initConnection(true);
  15. }
  16. ~OSCDriver()
  17. {
  18. initConnection(false);
  19. for (std::map<int, oscControl *>::iterator it = m_bindings.begin(); it != m_bindings.end(); ++it)
  20. {
  21. if (it->second != NULL)
  22. delete it->second;
  23. }
  24. m_bindings.clear();
  25. delete comm;
  26. }
  27. bool Connected() { return comm->Connected(); }
  28. void Reset(int lp) { comm->Clear(); }
  29. void ProcessOSC()
  30. {
  31. if(Connected())
  32. {
  33. processGUI();
  34. processOSCMsg();
  35. } else if((GetTickCount() - lastCheck) >= 2000)
  36. {
  37. initConnection(true);
  38. }
  39. }
  40. void sendMsg(const char *address, float value)
  41. {
  42. OSCMsg msg;
  43. msg.set(m_scene, address, value);
  44. comm->Write(&msg);
  45. }
  46. void Add(oscControl *ctrl, ParamWidget *p)
  47. {
  48. ctrl->bindWidget(p);
  49. int id = ctrl->ID();
  50. m_bindings[id] = ctrl;
  51. }
  52. void Add(oscControl *ctrl, ModuleLightWidget *p)
  53. {
  54. ctrl->bindWidget(p);
  55. int id = ctrl->ID();
  56. m_bindings[0x8000 | id] = ctrl;
  57. }
  58. private:
  59. oscCommunicator *comm;
  60. Module *pModule;
  61. int m_scene;
  62. uint32_t lastCheck;
  63. std::map<int, oscControl *>m_bindings;
  64. void initConnection(bool registr)
  65. {
  66. lastCheck = GetTickCount();
  67. comm->Open();
  68. }
  69. void redrawCache()
  70. {
  71. for (std::map<int, oscControl *>::iterator it = m_bindings.begin(); it != m_bindings.end(); ++it)
  72. {
  73. it->second->Draw(this, true);
  74. }
  75. }
  76. void processGUI()
  77. {
  78. for (std::map<int, oscControl *>::iterator it = m_bindings.begin(); it != m_bindings.end(); ++it)
  79. {
  80. if (it->second->DetectGUIChanges())
  81. {
  82. it->second->ChangeFromGUI(this);
  83. }
  84. }
  85. }
  86. void processOSCMsg()
  87. {
  88. OSCMsg msg;
  89. while (comm->Read(&msg))
  90. {
  91. for (std::map<int, oscControl *>::iterator it = m_bindings.begin(); it != m_bindings.end(); ++it)
  92. {
  93. if(msg.scene == m_scene && it->second->Intersect(msg.address))
  94. {
  95. #ifdef DEBUG
  96. info("MSG: scene=%i, address= %s", msg.scene, msg.address);
  97. #endif
  98. it->second->onOscMsg(pModule, msg);
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. };
  105. #endif //OSC