]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/staff-symbol-referencer.cc
patch::: 1.3.32.hwn2
[lilypond.git] / lily / staff-symbol-referencer.cc
index 22b29af5397857dab5258a1f9dd9daaf2d13aeb7..00172d198547f3665f7dca7ce29c3cd3f956323c 100644 (file)
 #include "paper-def.hh"
 #include "dimension-cache.hh"
 
-Staff_symbol_referencer::Staff_symbol_referencer ()
+Staff_symbol_referencer_interface::Staff_symbol_referencer_interface (Score_element const *sc)
 {
-  staff_symbol_l_ =0;
-  position_f_ =0;
+  elt_l_ = (Score_element*)sc;
 }
 
 void
-Staff_symbol_referencer::do_substitute_element_pointer (Score_element *o,
-                                                       Score_element*n)
+Staff_symbol_referencer_interface::set_interface ()
 {
-  if (staff_symbol_l_ == o)
-    {
-      staff_symbol_l_ = dynamic_cast<Staff_symbol*> (n);
-    }
+  elt_l_->set_elt_property ("staff-position", gh_double2scm (0.0));
+  elt_l_->dim_cache_[Y_AXIS]->off_callbacks_.push (callback);
 }
 
-int
-Staff_symbol_referencer::lines_i () const
+
+bool
+Staff_symbol_referencer_interface::has_interface_b ()
 {
-  return (staff_symbol_l_) ?  staff_symbol_l_->no_lines_i_ : 5;
+  return unsmob_element (elt_l_->get_elt_property ("staff-symbol"))
+    || gh_number_p (elt_l_->get_elt_property ("staff-position"));
 }
 
-void
-Staff_symbol_referencer::set_staff_symbol (Staff_symbol*s)
+
+int
+Staff_symbol_referencer_interface::line_count () const
 {
-  staff_symbol_l_ =s;
-  add_dependency (s);
+  Staff_symbol *st = staff_symbol_l ();
+  return st  ?  st->line_count () : 0;
 }
 
 Staff_symbol*
-Staff_symbol_referencer::staff_symbol_l () const
+Staff_symbol_referencer_interface::staff_symbol_l () const
 {
-  return staff_symbol_l_;
+  SCM st = elt_l_->get_elt_property ("staff-symbol");
+  return dynamic_cast<Staff_symbol* > (unsmob_element(st));
 }
 
 Real
-Staff_symbol_referencer::staff_line_leading_f () const
+Staff_symbol_referencer_interface::staff_space () const
 {
-  if (staff_symbol_l_)
-    return  staff_symbol_l_->staff_line_leading_f_;
-  else if (pscore_l_ && paper_l ())
-    paper_l ()->get_var ("interline");
+  Staff_symbol * st = staff_symbol_l ();
+  if (st)
+    return st->staff_space ();
+  else if (elt_l_->pscore_l_ && elt_l_->paper_l ())
+    return elt_l_->paper_l ()->get_var ("interline");
  
   return 0.0;
 }
 
+
 Real
-Staff_symbol_referencer::position_f () const
+Staff_symbol_referencer_interface::position_f () const
 {
-  Real p = position_f_;
-  if (staff_symbol_l_ )
+  Real p =0.0;
+  Staff_symbol * st = staff_symbol_l ();
+  Score_element * c = st ? elt_l_->common_refpoint (st, Y_AXIS) : 0;
+  if (st && c)
     {
-      Graphical_element * c = common_refpoint (staff_symbol_l_, Y_AXIS);
-      Real y = relative_coordinate (c, Y_AXIS) - staff_symbol_l_->relative_coordinate (c, Y_AXIS);
+      Real y = elt_l_->relative_coordinate (c, Y_AXIS)
+       - st->relative_coordinate (c, Y_AXIS);
 
-      p += 2.0 * y / staff_line_leading_f ();
+      p += 2.0 * y / st->staff_space ();
+    }
+  else
+    {
+      SCM pos = elt_l_->get_elt_property ("staff-position");
+      if (gh_number_p (pos))
+       return gh_scm2double (pos);
     }
+  
   return  p;
 }
 
@@ -78,23 +89,74 @@ Staff_symbol_referencer::position_f () const
 /*
   should use offset callback!
  */
-void
-Staff_symbol_referencer::do_pre_processing ()
+Real
+Staff_symbol_referencer_interface::callback (Dimension_cache const * c)
 {
-  translate_axis (position_f_ * staff_line_leading_f () /2.0, Y_AXIS);
-  position_f_ =0;
+  Score_element * sc = dynamic_cast<Score_element*> (c->element_l ());
+
+  
+  SCM pos = sc->get_elt_property ("staff-position");
+  Real off =0.0;
+  if (gh_number_p (pos))
+    {
+      Real space = staff_symbol_referencer (sc).staff_space ();
+      off = gh_scm2double (pos) * space/2.0;
+    }
+  sc->set_elt_property ("staff-position", gh_double2scm (0.0));
+
+  return off;
 }
 
+/*
+  
+  This sets the position relative to the center of the staff symbol.
 
+  The function is hairy, because it can be callled in two situations:
+
+  1.  There is no staff yet; we must set staff-position
+
+  2.  There is a staff, and perhaps someone even applied a
+  translate_axis (). Then we must compensate for the translation
+  
+  In either case, we set a callback to be sure that our new position
+  will be extracted from staff-position
+  
+ */
 void
-Staff_symbol_referencer::set_position (Real p)
+Staff_symbol_referencer_interface::set_position (Real p)
 {
-  Real halfspace =  staff_line_leading_f ()* 0.5;
-  
-  translate_axis (- halfspace * position_f (), Y_AXIS);
-  if (staff_symbol_l_)
-    translate_axis (halfspace * p, Y_AXIS);
+  Staff_symbol * st = staff_symbol_l ();
+  if (st && elt_l_->common_refpoint(st, Y_AXIS))
+    {
+      Real oldpos = position_f ();
+      elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
+    }
   else
-    position_f_ =   p;
+    {
+      elt_l_->set_elt_property ("staff-position",
+                               gh_double2scm (p));
+
+    }
+
+  Array<Offset_cache_callback> &callbacks (elt_l_->dim_cache_[Y_AXIS]->off_callbacks_);
+  for (int i=0; i < callbacks.size ();i++)
+    if (callbacks[i] == callback)
+      return ;
+
+  callbacks.push (callback);
+}
+
+Staff_symbol_referencer_interface
+staff_symbol_referencer (Score_element const*e)
+{
+  return e;                    // gee, I'm so smart!
 }
 
+int
+compare_position (Score_element *const  &a, Score_element * const &b)
+{
+  Staff_symbol_referencer_interface s1(a);
+  Staff_symbol_referencer_interface s2(b);      
+
+  return sign(s1.position_f () - s2.position_f ());
+}