]> git.sesse.net Git - casparcg/blobdiff - common/diagnostics/graph.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / common / diagnostics / graph.cpp
index 73fab59156e0c95909469780eb034fdfa4f1555c..0b4348e0bba706d3d4e389dccbf8b6eb721357cc 100644 (file)
@@ -48,7 +48,7 @@ struct drawable : public sf::Drawable
 \r
 class context : public drawable\r
 {      \r
-       sf::RenderWindow window_;\r
+       std::unique_ptr<sf::RenderWindow> window_;\r
        \r
        std::list<std::shared_ptr<drawable>> drawables_;\r
                \r
@@ -71,31 +71,51 @@ public:
                        get_instance().do_register_drawable(drawable);\r
                });\r
        }\r
-                       \r
+\r
+       static void show(bool value)\r
+       {\r
+               begin_invoke([=]\r
+               {       \r
+                       get_instance().do_show(value);\r
+               });\r
+       }\r
+                               \r
 private:\r
        context() : executor_(L"diagnostics")\r
        {\r
-               executor_.begin_invoke([this]\r
-               {                       \r
-                       SetThreadPriority(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS);\r
-                       window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");\r
-                       window_.SetPosition(0, 0);\r
-                       window_.SetActive();\r
-                       glEnable(GL_BLEND);\r
-                       glEnable(GL_LINE_SMOOTH);\r
-                       glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
-                       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
-                       tick();\r
-               });\r
+       }\r
+\r
+       void do_show(bool value)\r
+       {\r
+               if(value)\r
+               {\r
+                       if(!window_)\r
+                       {\r
+                               SetThreadPriority(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS);\r
+                               window_.reset(new sf::RenderWindow(sf::VideoMode(600, 1000), "CasparCG Diagnostics"));\r
+                               window_->SetPosition(0, 0);\r
+                               window_->SetActive();\r
+                               glEnable(GL_BLEND);\r
+                               glEnable(GL_LINE_SMOOTH);\r
+                               glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
+                               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
+                               tick();\r
+                       }\r
+               }\r
+               else\r
+                       window_.reset();\r
        }\r
 \r
        void tick()\r
        {\r
+               if(!window_)\r
+                       return;\r
+\r
                sf::Event e;\r
-               while(window_.GetEvent(e)){}            \r
+               while(window_->GetEvent(e)){}           \r
                glClear(GL_COLOR_BUFFER_BIT);\r
-               window_.Draw(*this);\r
-               window_.Display();\r
+               window_->Draw(*this);\r
+               window_->Display();\r
                boost::this_thread::sleep(boost::posix_time::milliseconds(20));\r
                executor_.begin_invoke([this]{tick();});\r
        }\r
@@ -112,8 +132,8 @@ private:
                        auto& drawable = *it;\r
                        if(!drawable.unique())\r
                        {\r
-                               drawable->SetScale(static_cast<float>(window_.GetWidth()), static_cast<float>(target_dy*window_.GetHeight()));\r
-                               float target_y = std::max(last_y, static_cast<float>(n * window_.GetHeight())*target_dy);\r
+                               drawable->SetScale(static_cast<float>(window_->GetWidth()), static_cast<float>(target_dy*window_->GetHeight()));\r
+                               float target_y = std::max(last_y, static_cast<float>(n * window_->GetHeight())*target_dy);\r
                                drawable->SetPosition(0.0f, target_y);                  \r
                                target.Draw(*drawable);                         \r
                                ++it;           \r
@@ -125,7 +145,8 @@ private:
        \r
        void do_register_drawable(const std::shared_ptr<drawable>& drawable)\r
        {\r
-               drawables_.push_back(drawable);\r
+               if(std::find(drawables_.begin(), drawables_.end(), drawable) == drawables_.end())\r
+                       drawables_.push_back(drawable);\r
        }\r
        \r
        static context& get_instance()\r
@@ -168,12 +189,13 @@ class line : public drawable
        boost::optional<diagnostics::guide> guide_;\r
        boost::circular_buffer<std::pair<double, bool>> line_data_;\r
 \r
-       std::vector<float>              tick_data_;\r
-       bool                                    tick_tag_;\r
+       boost::circular_buffer<double>  tick_data_;\r
+       bool                                                    tick_tag_;\r
        color c_;\r
 public:\r
        line(size_t res = 600)\r
                : line_data_(res)\r
+               , tick_data_(50)\r
                , tick_tag_(false)\r
                , c_(1.0f, 1.0f, 1.0f)\r
        {\r
@@ -232,7 +254,7 @@ public:
                        target.Draw(*guide_);\r
                \r
                glBegin(GL_LINE_STRIP);\r
-               glColor4f(c_.red, c_.green, c_.blue, 0.7f);             \r
+               glColor4f(c_.red, c_.green, c_.blue, 0.8f);             \r
                for(size_t n = 0; n < line_data_.size(); ++n)           \r
                        if(line_data_[n].first > -0.5)\r
                                glVertex3d(x+n*dx, std::max(0.05, std::min(0.95, (1.0f-line_data_[n].first)*0.8 + 0.1f)), 0.0);         \r
@@ -258,19 +280,17 @@ public:
 struct graph::implementation : public drawable\r
 {\r
        std::map<std::string, diagnostics::line> lines_;\r
-       const printer parent_printer_;\r
        std::string name_;\r
-\r
-       int counter_;\r
-\r
+       std::string text_;\r
+       \r
        implementation(const std::string& name) \r
                : name_(name)\r
-               , counter_(0){}\r
-\r
-       implementation(const printer& parent_printer) \r
-               : parent_printer_(parent_printer)\r
-               , name_(parent_printer_ ? narrow(parent_printer_()) : "")\r
-               , counter_(0){}\r
+               , text_(name_){}\r
+       \r
+       void set_text(const std::string& value)\r
+       {\r
+               text_ = value;\r
+       }\r
 \r
        void update(const std::string& name, double value)\r
        {\r
@@ -300,17 +320,11 @@ struct graph::implementation : public drawable
 private:\r
        void render(sf::RenderTarget& target)\r
        {\r
-               if(counter_++ > 25) // Don't update name too often since print can be implemented with locks.\r
-               {\r
-                       counter_ = 0;\r
-                       if(parent_printer_)\r
-                               name_ = narrow(parent_printer_());\r
-               }\r
                const size_t text_size = 15;\r
                const size_t text_margin = 2;\r
                const size_t text_offset = (text_size+text_margin*2)*2;\r
 \r
-               sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
+               sf::String text(text_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
                text.SetStyle(sf::String::Italic);\r
                text.Move(text_margin, text_margin);\r
                \r
@@ -356,84 +370,76 @@ private:
        implementation& operator=(implementation&);\r
 };\r
        \r
-graph::graph(const std::string& name) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(name) : nullptr)\r
+graph::graph() : impl_(new implementation(""))\r
 {\r
-       if(impl_)\r
-               context::register_drawable(impl_);\r
+\r
 }\r
 \r
-graph::graph(const printer& parent_printer) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(parent_printer) : nullptr)\r
+void graph::set_text(const std::string& value)\r
 {\r
-       if(impl_)\r
-               context::register_drawable(impl_);\r
+       auto p = impl_;\r
+       context::begin_invoke([=]\r
+       {       \r
+               p->set_text(value);\r
+       });\r
+}\r
+\r
+void graph::set_text(const std::wstring& value)\r
+{\r
+       set_text(narrow(value));\r
 }\r
 \r
 void graph::update_value(const std::string& name, double value)\r
 {\r
-       if(impl_)\r
+       auto p = impl_;\r
+       context::begin_invoke([=]\r
        {       \r
-               auto p = impl_;\r
-               context::begin_invoke([=]\r
-               {       \r
-                       p->update(name, value);\r
-               });\r
-       }\r
+               p->update(name, value);\r
+       });\r
 }\r
 void graph::set_value(const std::string& name, double value)\r
-{\r
-       if(impl_)\r
-       {               \r
-               auto p = impl_;\r
-               context::begin_invoke([=]\r
-               {       \r
-                       p->set(name, value);\r
-               });\r
-       }\r
+{      \r
+       auto p = impl_;\r
+       context::begin_invoke([=]\r
+       {       \r
+               p->set(name, value);\r
+       });     \r
 }\r
 void graph::set_color(const std::string& name, color c)\r
-{      \r
-       if(impl_)\r
-       {               \r
-               auto p = impl_;\r
-               context::begin_invoke([=]\r
-               {       \r
-                       p->set_color(name, c);\r
-               });\r
-       }\r
+{              \r
+       auto p = impl_;\r
+       context::begin_invoke([=]\r
+       {       \r
+               p->set_color(name, c);\r
+       });\r
 }\r
 void graph::add_tag(const std::string& name)\r
-{      \r
-       if(impl_)\r
-       {               \r
-               auto p = impl_;\r
-               context::begin_invoke([=]\r
-               {       \r
-                       p->tag(name);\r
-               });\r
-       }\r
+{              \r
+       auto p = impl_;\r
+       context::begin_invoke([=]\r
+       {       \r
+               p->tag(name);\r
+       });\r
 }\r
 void graph::add_guide(const std::string& name, double value)\r
 {      \r
-       if(impl_)\r
-       {               \r
-               auto p = impl_;\r
-               context::begin_invoke([=]\r
-               {       \r
-                       p->guide(name, value);\r
-               });\r
-       }\r
+       auto p = impl_;\r
+       context::begin_invoke([=]\r
+       {       \r
+               p->guide(name, value);\r
+       });\r
 }\r
 \r
-safe_ptr<graph> create_graph(const std::string& name)\r
+void register_graph(const safe_ptr<graph>& graph)\r
 {\r
-       return safe_ptr<graph>(new graph(name));\r
+       context::register_drawable(graph->impl_);\r
 }\r
-safe_ptr<graph> create_graph(const printer& parent_printer)\r
+\r
+void show_graphs(bool value)\r
 {\r
-       return safe_ptr<graph>(new graph(parent_printer));\r
+       context::show(value);\r
 }\r
 \r
-\r
 //namespace v2\r
 //{    \r
 //     \r