Maschine2 UI: basic widgets
authorRobin Gareus <robin@gareus.org>
Thu, 17 Nov 2016 12:08:38 +0000 (13:08 +0100)
committerPaul Davis <paul@linuxaudiosystems.com>
Mon, 18 Sep 2017 15:40:53 +0000 (11:40 -0400)
libs/surfaces/maschine2/maschine2.cc
libs/surfaces/maschine2/ui_knob.cc [new file with mode: 0644]
libs/surfaces/maschine2/ui_knob.h [new file with mode: 0644]
libs/surfaces/maschine2/ui_menu.cc [new file with mode: 0644]
libs/surfaces/maschine2/ui_menu.h [new file with mode: 0644]
libs/surfaces/maschine2/wscript

index b378639f11b0d855082a833f3f66bc57296a21ec..75f488d9971f0e603229c3ec42dba559154fd8f9 100644 (file)
@@ -48,6 +48,61 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace ArdourSurface;
 
+#if 1 // TEST
+#include "gtkmm2ext/colors.h"
+#include "canvas/line.h"
+#include "canvas/rectangle.h"
+#include "canvas/text.h"
+
+#include "layout.h"
+#include "ui_knob.h"
+#include "ui_menu.h"
+
+class TestLayout : public Maschine2Layout
+{
+       public:
+               TestLayout (Maschine2& m2, Session& s, std::string const & name)
+                       : Maschine2Layout (m2, s, name)
+                       , knob (0)
+                       , menu (0)
+               {
+                       using namespace ArdourCanvas;
+                       bg = new ArdourCanvas::Rectangle (this);
+                       bg->set (ArdourCanvas::Rect (0, 0, display_width(), display_height()));
+                       bg->set_fill_color (0x000000ff);
+
+                       upper_line = new Line (this);
+                       upper_line->set (Duple (0, 16.5), Duple (display_width(), 16.5));
+                       upper_line->set_outline_color (0xffffffff);
+
+                       knob = new Maschine2Knob(&m2, this);
+                       knob->set_position (Duple (64 + 32, 32));
+                       if (_session.master_out ()) {
+                               knob->set_controllable (_session.master_out ()->gain_control());
+                       }
+
+                       std::vector<std::string> strs;
+                       strs.push_back("T|sg1");
+                       strs.push_back("Test2asdjasdlkjasldkjasd");
+                       strs.push_back("Test3");
+                       strs.push_back("Test4");
+                       strs.push_back("Test5");
+                       menu = new Maschine2Menu(&m2, this, strs);
+                       menu->set_position (Duple (0, 19));
+
+               }
+               Maschine2Knob* get_knob () { return knob; }
+               Maschine2Menu* get_menu () { return menu; }
+       private:
+               ArdourCanvas::Rectangle* bg;
+               ArdourCanvas::Line* upper_line;
+               Maschine2Knob* knob;
+               Maschine2Menu* menu;
+};
+
+static TestLayout* tl = NULL;
+#endif
+
 Maschine2::Maschine2 (ARDOUR::Session& s)
        : ControlProtocol (s, string (X_("NI Maschine2")))
        , AbstractUI<Maschine2Request> (name())
@@ -198,6 +253,12 @@ Maschine2::start ()
        read_connection = read_timeout->connect (sigc::mem_fun (*this, &Maschine2::dev_read));
        read_timeout->attach (main_loop ()->get_context());
 
+#if 1 // TEST
+       tl = new TestLayout (*this, *session, "test");
+       tl->get_menu ()->set_control (_ctrl->encoder (1));
+       tl->get_knob ()->set_control (_ctrl->encoder (2));
+#endif
+
        return 0;
 }
 
@@ -284,5 +345,9 @@ Maschine2::dev_write ()
 Maschine2Layout*
 Maschine2::current_layout() const
 {
+#if 1 // TEST
+       return tl;
+#else
        return NULL;
+#endif
 }
diff --git a/libs/surfaces/maschine2/ui_knob.cc b/libs/surfaces/maschine2/ui_knob.cc
new file mode 100644 (file)
index 0000000..f493fd4
--- /dev/null
@@ -0,0 +1,241 @@
+/*
+ * Copyright (C) 2016 Paul Davis
+ * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <cmath>
+
+#include <cairomm/context.h>
+
+#include "ardour/automation_control.h"
+#include "ardour/value_as_string.h"
+#include "ardour/dB.h"
+#include "ardour/utils.h"
+
+#include "gtkmm2ext/colors.h"
+#include "gtkmm2ext/gui_thread.h"
+#include "gtkmm2ext/rgb_macros.h"
+
+#include "canvas/text.h"
+
+#include "maschine2.h"
+#include "m2controls.h"
+
+#include "ui_knob.h"
+
+#include "pbd/i18n.h"
+
+#ifdef __APPLE__
+#define Rect ArdourCanvas::Rect
+#endif
+
+using namespace PBD;
+using namespace ARDOUR;
+using namespace ArdourSurface;
+using namespace ArdourCanvas;
+
+Maschine2Knob::Maschine2Knob (PBD::EventLoop* el, Item* parent)
+       : Container (parent)
+       , _ctrl (0)
+       , _eventloop (el)
+       , _radius (11)
+       , _val (0)
+       , _normal (0)
+{
+       Pango::FontDescription fd ("Sans 10px");
+
+       text = new Text (this);
+       text->set_font_description (fd);
+       text->set_position (Duple (-_radius, _radius + 2));
+       text->set_color (0xffffffff);
+       _bounding_box_dirty = true;
+}
+
+Maschine2Knob::~Maschine2Knob ()
+{
+}
+
+void
+Maschine2Knob::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
+{
+       if (!_controllable) {
+               /* no controllable, nothing to draw */
+               return;
+       }
+
+       // TODO consider a "bar" circular shape + 1bit b/w is ugly
+
+       const float scale = 2.f * _radius;
+       const float pointer_thickness = std::max (1.f, 3.f * (scale / 80.f));
+
+       const float start_angle = ((180.f - 65.f) * M_PI) / 180.f;
+       const float end_angle = ((360.f + 65.f) * M_PI) / 180.f;
+
+       float zero = 0;
+
+       const float value_angle = start_angle + (_val * (end_angle - start_angle));
+       const float zero_angle = start_angle + (zero * (end_angle - start_angle));
+
+       float value_x = cos (value_angle);
+       float value_y = sin (value_angle);
+
+       /* translate so that all coordinates are based on the center of the
+        * knob (which is also its position()
+        */
+       context->save ();
+       Duple origin = item_to_window (Duple (0, 0));
+       context->translate (origin.x - 0.5, origin.y - 0.5);
+       context->begin_new_path ();
+
+       float center_radius = 0.48*scale;
+       float border_width = 0.8;
+
+       const bool arc = true;
+
+       if (arc) {
+               center_radius = scale * 0.33;
+
+               float inner_progress_radius = scale * 0.38;
+               float outer_progress_radius = scale * 0.48;
+               float progress_width = (outer_progress_radius-inner_progress_radius);
+               float progress_radius = inner_progress_radius + progress_width/2.0;
+
+               // draw the arc
+               context->set_source_rgb (1, 1, 1);
+               context->set_line_width (progress_width);
+               if (zero_angle > value_angle) {
+                       context->arc (0, 0, progress_radius, value_angle, zero_angle);
+               } else {
+                       context->arc (0, 0, progress_radius, zero_angle, value_angle);
+               }
+               context->stroke ();
+       }
+
+       // knob body
+       context->set_line_width (border_width);
+       context->set_source_rgb (1, 1, 1);
+       context->arc (0, 0, center_radius, 0, 2.0*G_PI);
+       context->fill ();
+
+       // line
+       context->set_source_rgb (0, 0, 0);
+       context->set_line_cap (Cairo::LINE_CAP_ROUND);
+       context->set_line_width (pointer_thickness);
+       context->move_to ((center_radius * value_x), (center_radius * value_y));
+       context->line_to (((center_radius * 0.2) * value_x), ((center_radius * 0.2) * value_y));
+       context->stroke ();
+
+       /* reset all translations, scaling etc. */
+       context->restore ();
+
+       render_children (area, context);
+}
+
+ void
+Maschine2Knob::compute_bounding_box () const
+{
+       if (!_canvas || _radius == 0) {
+               _bounding_box = Rect ();
+               _bounding_box_dirty = false;
+               return;
+       }
+
+       if (_bounding_box_dirty) {
+               _bounding_box = Rect (- _radius, - _radius, _radius, _radius);
+               _bounding_box_dirty = false;
+       }
+
+       add_child_bounding_boxes ();
+}
+
+void
+Maschine2Knob::set_controllable (boost::shared_ptr<AutomationControl> c)
+{
+       watch_connection.disconnect ();
+
+       if (!c) {
+               _controllable.reset ();
+               return;
+       }
+
+       _controllable = c;
+       _controllable->Changed.connect (watch_connection, invalidator(*this), boost::bind (&Maschine2Knob::controllable_changed, this), _eventloop);
+
+       controllable_changed ();
+       // set _controllable->desc()->label
+}
+
+void
+Maschine2Knob::set_control (M2EncoderInterface* ctrl)
+{
+       encoder_connection.disconnect ();
+       _ctrl = ctrl;
+       if (!ctrl) {
+               return;
+       }
+       ctrl->changed.connect_same_thread (encoder_connection, boost::bind (&Maschine2Knob::encoder_changed, this, _1));
+}
+
+void
+Maschine2Knob::encoder_changed (int delta)
+{
+       if (!_controllable) {
+               return;
+       }
+       const double d = delta * 0.5 / _ctrl->range ();
+       boost::shared_ptr<AutomationControl> ac = _controllable;
+       ac->set_value (ac->interface_to_internal (min (ac->upper(), max (ac->lower(), ac->internal_to_interface (ac->get_value()) + d))), PBD::Controllable::UseGroup);
+}
+
+void
+Maschine2Knob::controllable_changed ()
+{
+       if (_controllable) {
+               _normal = _controllable->internal_to_interface (_controllable->normal());
+               _val = _controllable->internal_to_interface (_controllable->get_value());
+
+               const ParameterDescriptor& desc (_controllable->desc());
+
+               char buf[64];
+               switch (_controllable->parameter().type()) {
+                       case ARDOUR::PanAzimuthAutomation:
+                               snprintf (buf, sizeof (buf), _("L:%3d R:%3d"), (int) rint (100.0 * (1.0 - _val)), (int) rint (100.0 * _val));
+                               text->set (buf);
+                               break;
+
+                       case ARDOUR::PanWidthAutomation:
+                               snprintf (buf, sizeof (buf), "%d%%", (int) floor (_val*100));
+                               text->set (buf);
+                               break;
+
+                       case ARDOUR::GainAutomation:
+                       case ARDOUR::BusSendLevel:
+                       case ARDOUR::TrimAutomation:
+                               snprintf (buf, sizeof (buf), "%+4.1f dB", accurate_coefficient_to_dB (_controllable->get_value()));
+                               text->set (buf);
+                               break;
+
+                       default:
+                               text->set (ARDOUR::value_as_string (desc, _val));
+                               break;
+               }
+       } else {
+               text->set ("---");
+       }
+
+       redraw ();
+}
diff --git a/libs/surfaces/maschine2/ui_knob.h b/libs/surfaces/maschine2/ui_knob.h
new file mode 100644 (file)
index 0000000..fe07dbd
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _ardour_maschine2_knob_h_
+#define _ardour_maschine2_knob_h_
+
+#include <boost/shared_ptr.hpp>
+#include <sigc++/trackable.h>
+
+#include <cairomm/refptr.h>
+
+#include "pbd/signals.h"
+
+#include "canvas/container.h"
+
+namespace ArdourCanvas {
+       class Text;
+}
+
+namespace ARDOUR {
+       class AutomationControl;
+}
+
+namespace Cairo {
+       class Context;
+       class Region;
+}
+
+namespace ArdourSurface {
+
+class Maschine2;
+class M2EncoderInterface;
+
+class Maschine2Knob : public sigc::trackable, public ArdourCanvas::Container
+{
+       public:
+               Maschine2Knob (PBD::EventLoop*, ArdourCanvas::Item*);
+               virtual ~Maschine2Knob ();
+
+               void set_controllable (boost::shared_ptr<ARDOUR::AutomationControl>);
+               void set_control (M2EncoderInterface*);
+               boost::shared_ptr<ARDOUR::AutomationControl> controllable() const { return _controllable; }
+
+               void render (ArdourCanvas::Rect const &, Cairo::RefPtr<Cairo::Context>) const;
+               void compute_bounding_box() const;
+
+       protected:
+               void controllable_changed ();
+               void encoder_changed (int);
+
+               PBD::ScopedConnection watch_connection;
+               PBD::ScopedConnection encoder_connection;
+
+               boost::shared_ptr<ARDOUR::AutomationControl> _controllable;
+               M2EncoderInterface* _ctrl;
+
+       private:
+               PBD::EventLoop* _eventloop;
+
+               float  _radius;
+               float  _val; // current value [0..1]
+               float  _normal; // default value, arc
+
+               ArdourCanvas::Text* text;
+};
+
+} // namespace
+
+#endif
diff --git a/libs/surfaces/maschine2/ui_menu.cc b/libs/surfaces/maschine2/ui_menu.cc
new file mode 100644 (file)
index 0000000..75d41ef
--- /dev/null
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2016 Paul Davis
+ * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <cairomm/context.h>
+#include <cairomm/surface.h>
+#include <cairomm/region.h>
+#include <pangomm/layout.h>
+
+#include "pbd/i18n.h"
+
+#include "gtkmm2ext/colors.h"
+
+#include "canvas/text.h"
+#include "canvas/types.h"
+#include "canvas/rectangle.h"
+
+#include "maschine2.h"
+#include "m2controls.h"
+
+#include "canvas.h"
+#include "ui_menu.h"
+
+#ifdef __APPLE__
+#define Rect ArdourCanvas::Rect
+#endif
+
+using namespace ARDOUR;
+using namespace std;
+using namespace PBD;
+using namespace Glib;
+using namespace ArdourSurface;
+using namespace ArdourCanvas;
+
+Maschine2Menu::Maschine2Menu (PBD::EventLoop* el, Item* parent, const std::vector<std::string>& s, double width)
+       : Container (parent)
+       , _ctrl (0)
+       , _eventloop (el)
+       , _baseline (-1)
+       , _height (-1)
+       , _width (width)
+       , _active (0)
+       , _wrap (false)
+       , _first (0)
+       , _last (0)
+       , _rotary (0)
+{
+       Pango::FontDescription fd ("Sans 10px");
+
+       Maschine2Canvas* m2c = dynamic_cast<Maschine2Canvas*> (canvas());
+       Glib::RefPtr<Pango::Layout> throwaway = Pango::Layout::create (m2c->image_context());
+       throwaway->set_font_description (fd);
+       throwaway->set_text (X_("Hg")); /* ascender + descender) */
+       int h, w;
+       throwaway->get_pixel_size (w, h);
+       _baseline = ceil(h);
+       _height = m2c->height();
+
+       _active_bg = new ArdourCanvas::Rectangle (this);
+       _active_bg->set_fill_color (0xffffffff);
+
+       for (vector<string>::const_iterator i = s.begin(); i != s.end(); ++i) {
+               Text* t = new Text (this);
+               t->set_font_description (fd);
+               t->set_color (0xffffffff);
+               t->set (*i);
+               _displays.push_back (t);
+       }
+       rearrange (0);
+}
+
+Maschine2Menu::~Maschine2Menu ()
+{
+}
+
+void
+Maschine2Menu::rearrange (uint32_t initial_display)
+{
+       vector<Text*>::iterator i = _displays.begin();
+
+       Duple origin = item_to_window (Duple (0, 0));
+
+       for (uint32_t n = 0; n < initial_display; ++n) {
+               (*i)->hide ();
+               ++i;
+       }
+
+       uint32_t index = initial_display;
+       uint32_t row = 0;
+       bool active_shown = false;
+
+       _first = _last = index;
+       while (i != _displays.end()) {
+               Coord y = row * _baseline;
+               if (y + _baseline + origin.y > _height) {
+                       break;
+               }
+               (*i)->set_position (Duple (2, y));
+
+               if (index == _active) {
+                       (*i)->set_color (0x000000ff);
+                       active_shown = true;
+                       _active_bg->set (Rect (0, y - 1, 64, y - 1 + _baseline));
+                       _active_bg->show ();
+               } else {
+                       (*i)->set_color (0xffffffff);
+               }
+               _last = index;
+               (*i)->show ();
+               ++i;
+               ++index;
+               ++row;
+       }
+
+       while (i != _displays.end()) {
+               (*i)->hide ();
+               ++i;
+       }
+
+       if (!active_shown) {
+               _active_bg->hide ();
+       }
+}
+
+void
+Maschine2Menu::render (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
+{
+       context->save ();
+       Duple origin = item_to_window (Duple (0, 0));
+       context->rectangle (origin.x, origin.y, _width, _height);
+       context->clip ();
+       render_children (area, context);
+       context->restore ();
+}
+
+void
+Maschine2Menu::set_active (uint32_t a)
+{
+       if (a == _active || a > items ()) {
+               return;
+       }
+
+       _active = a;
+
+       if (_active < _first) {
+               rearrange (_active);
+       }
+       else if (_active > _last) {
+               rearrange (_active - _last);
+       } else {
+               rearrange (_first);
+       }
+       redraw ();
+}
+
+void
+Maschine2Menu::set_wrap (bool b)
+{
+       if (b == _wrap) {
+               return;
+       }
+       _wrap = b;
+}
+
+void
+Maschine2Menu::set_control (M2EncoderInterface* ctrl)
+{
+       encoder_connection.disconnect ();
+       _ctrl = ctrl;
+       if (!ctrl) {
+               return;
+       }
+       ctrl->changed.connect_same_thread (encoder_connection, boost::bind (&Maschine2Menu::encoder_changed, this, _1));
+}
+
+void
+Maschine2Menu::encoder_changed (int delta)
+{
+       assert (_ctrl);
+       if (items() == 0) {
+               return;
+       }
+       double d = delta * 8. / _ctrl->range ();
+       d = fmodf (d, items());
+       if (_wrap) {
+               _rotary = fmodf (items () + _rotary + d, items());
+       } else {
+               _rotary += + d;
+               if (_rotary < 0) { _rotary = 0; }
+               if (_rotary >= items ()) {  _rotary = items () - 1; }
+       }
+
+       uint32_t a = floor (_rotary);
+
+       if (a == _active) {
+               return;
+       }
+       set_active (a);
+       ActiveChanged (); /* EMIT SIGNAL */
+}
diff --git a/libs/surfaces/maschine2/ui_menu.h b/libs/surfaces/maschine2/ui_menu.h
new file mode 100644 (file)
index 0000000..1f0d79e
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _ardour_maschine2_menu_h_
+#define _ardour_maschine2_menu_h_
+
+#include <vector>
+#include <cairomm/refptr.h>
+
+#include "pbd/signals.h"
+#include "canvas/container.h"
+
+namespace ArdourCanvas {
+       class Text;
+}
+
+namespace ARDOUR {
+       class AutomationControl;
+}
+
+namespace Cairo {
+       class Context;
+}
+
+namespace ArdourSurface {
+
+class Maschine2;
+class M2EncoderInterface;
+
+class Maschine2Menu : public ArdourCanvas::Container
+{
+       public:
+               Maschine2Menu (PBD::EventLoop*, ArdourCanvas::Item*, const std::vector<std::string>&, double width = 64);
+               virtual ~Maschine2Menu ();
+
+               void set_control (M2EncoderInterface*);
+               void set_active (uint32_t index);
+               void set_wrap (bool);
+
+               uint32_t active () const { return _active; }
+               uint32_t items() const { return _displays.size(); }
+
+               PBD::Signal0<void> ActiveChanged;
+
+               void render (ArdourCanvas::Rect const &, Cairo::RefPtr<Cairo::Context>) const;
+
+       private:
+               void rearrange (uint32_t);
+               void encoder_changed (int);
+
+               M2EncoderInterface* _ctrl;
+               PBD::EventLoop* _eventloop;
+               PBD::ScopedConnection encoder_connection;
+
+               std::vector<ArdourCanvas::Text*> _displays;
+               ArdourCanvas::Rectangle* _active_bg;
+
+               double   _baseline;
+               double   _height;
+               double   _width;
+               uint32_t _active;
+               bool     _wrap;
+
+               uint32_t _first;
+               uint32_t _last;
+               double   _rotary;
+};
+
+} // namespace
+
+#endif
index 6194c8c8e38d09c4566434ec9c71c64b65d2f305..aa8cb8a7fafe18ed9865c02443b36abc35e86ba3 100644 (file)
@@ -27,6 +27,8 @@ def build(bld):
            m2_map_mk2.cc
            m2_dev_mikro.cc
            m2_map_mikro.cc
+           ui_knob.cc
+           ui_menu.cc
     '''
     obj.export_includes = ['.']
     obj.defines      = [ 'PACKAGE="ardour_maschine2"' ]