René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

ScrollBar [Ming wrapper class]

The source

ScrollBar.h
#ifndef SCROLLBAR_H
#define SCROLLBAR_H

#include "SwfMovie.h"

class ScrollBar : public MovieClip {
  public:
    ScrollBar(int width, int length, float start_value, float end_value);

    ScrollBar();

    void ScrollFunction(std::string const&);

    void WidthLength(int  , int  );
    void StartEnd   (float, float);

  protected:
    int   width_;
    int   length_;
    float start_value_;
    float end_value_;

   // int   length_thumb_;

    Shape     back_;
    MovieClip thumb_;

  enum {z_back=1,
        z_thumb };

  static Action thMouseDown_;
  static Action thMouseUp_  ;
  static Action thMouseMove_;

  static Action baMouseDown_;
  //static Action baMouseUp_  ;
  
  /* See MovieClip_::BeforeAdding */
  virtual void BeforeAdding();

  /* See MovieClip_::AfterAdding */
  virtual void AfterAdding(DisplayClip&);

  private:
    void CreateThumb();
};

#endif
ScrollBar.cpp
#include "ScrollBar.h"

Action ScrollBar::thMouseDown_ (
  "if (hitTest(_root._xmouse,_root._ymouse, true)) { "
  "  var point = {x:_root._xmouse, y:_root._ymouse};"
  "  _parent.globalToLocal(point); "
  "  this.y_drag_start=point.y-_y; "
  "  this.isDragging = true;"
  "};"
  );

Action ScrollBar::thMouseUp_("this.isDragging =false;");

Action ScrollBar::thMouseMove_(
  "if (this.isDragging) {"
  "  var point = {x:_root._xmouse, y:_root._ymouse};"
  "  _parent.globalToLocal(point); "
  "  var new_y = point.y-this.y_drag_start;"
  "  if (new_y > _parent.scrollable_length) { new_y = _parent.scrollable_length;}"
  "  if (new_y < 0) {new_y = 0;}"
  "  _y = new_y;"
  "  _parent.scroll_func(_parent.pointToValue(new_y));"
  "}"
);

Action ScrollBar::baMouseDown_ (
   "if (!this.thumb.isDragging && this.hitTest(_root._xmouse,_root._ymouse, true)) { "
   "  var point = {x:_root._xmouse, y:_root._ymouse};"
   "  this.globalToLocal(point); "
   "  var new_y=point.y-this.thumb_length/2;"
   "  if (new_y < 0) {"
   "    new_y = 0;"  
   "  }"
   "  else if (new_y > scrollable_length) {"
   "    new_y = scrollable_length;"
   "  }"
   "  this.thumb._y=new_y;"
   "  scroll_func(pointToValue(new_y));"
   "};"
  );

// TODO: not used anymore?
//Action ScrollBar::baMouseUp_(
//  "if ( this.mouse_pressed) { "
//  "  this.mouse_pressed =false; "
//  "};"
//  );

ScrollBar::ScrollBar() :
  width_      (0),
  length_     (0),
  start_value_(0),
  end_value_  (0)
{
}

ScrollBar::ScrollBar(int width, int length, float start_value, float end_value) :
  width_       (width      ),
  length_      (length     ),
  start_value_ (start_value),
  end_value_   (end_value  )
{
}

void ScrollBar::WidthLength(int w, int l) {
  width_  = w;
  length_ = l;
}

void ScrollBar::StartEnd(float s, float e) {
  start_value_ = s;
  end_value_   = e;
}

void ScrollBar::CreateThumb() {
  int length_thumb  = width_-2;
  Shape shape;

  shape.FillStyle(0x55, 0x55, 0x55);

  shape.MoveTo(1,1);

  shape.Line( length_thumb, 0       );
  shape.Line( 0           , width_-2);
  shape.Line(-length_thumb, 0       );
  shape.Line( 0           ,-width_+2);

  thumb_.Add(shape);

  DisplayClip iThumb = Add(thumb_, z_thumb);
  iThumb.Name("thumb");

  iThumb.OnMouseDown(thMouseDown_);
  iThumb.OnMouseUp  (thMouseUp_  );
  iThumb.OnMouseMove(thMouseMove_);

  Add(Action(std::string(
        "this.scrollable_length=") + ntos(length_ - length_thumb) + 
        ";this.start_value="       + ntos(start_value_          ) +
        ";this.end_value="         + ntos(end_value_            ) +
        ";this.thumb_length="      + ntos(length_thumb          ) +
        ";"
        ));

  thumb_.NextFrame();
}

void ScrollBar::BeforeAdding() {
  MovieClip::BeforeAdding();

  //length_thumb_ = width_-2;

  back_.LineStyle(1, 0x33, 0x33, 0x33);
  back_.FillStyle(0x99, 0x99, 0x99);

  back_.Line( width_, 0      );
  back_.Line( 0     , length_);
  back_.Line(-width_, 0      );
  back_.Line( 0     ,-length_);
  
  Add(back_, z_back);

  CreateThumb();

  NextFrame();
}

void ScrollBar::AfterAdding(DisplayClip& d) {
  d.OnMouseDown(baMouseDown_);
  MovieClip::AfterAdding(d);
}

void ScrollBar::ScrollFunction(std::string const& f) {
  Action pointToValue (
    "function pointToValue(y) {"
    "  return this.start_value + y/this.scrollable_length*(this.end_value-this.start_value);"
    "}"
);
  Add(pointToValue);
  Add(Action(std::string("function scroll_func(value) {") + f + "}"));
}

Selected methods

ScrollFunction

Examples

The following examples demonstrate this class: