Back to index

easystroke  0.5.5.1
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends
Stroke Class Reference

#include <gesture.h>

List of all members.

Classes

struct  Point

Public Member Functions

 Stroke ()
Glib::RefPtr< Gdk::Pixbuf > draw (int size, double width=2.0, bool inv=false) const
void draw (Cairo::RefPtr< Cairo::Surface > surface, int x, int y, int w, int h, double width=2.0, bool inv=false) const
void draw_svg (std::string filename) const
bool show_icon ()
unsigned int size () const
bool trivial () const
Point points (int n) const
double time (int n) const
bool is_timeout () const

Static Public Member Functions

static RStroke create (PreStroke &s, int trigger_, int button_, bool timeout_)
static RStroke trefoil ()
static int compare (RStroke, RStroke, double &)
static Glib::RefPtr< Gdk::Pixbuf > drawEmpty (int)
static Glib::RefPtr< Gdk::Pixbuf > drawDebug (RStroke, RStroke, int)

Public Attributes

int trigger
int button
bool timeout
boost::shared_ptr< stroke_t > stroke

Private Member Functions

 Stroke (PreStroke &s, int trigger_, int button_, bool timeout_)
Glib::RefPtr< Gdk::Pixbuf > draw_ (int size, double width=2.0, bool inv=false) const
template<class Archive >
void save (Archive &ar, const unsigned int version) const
template<class Archive >
void load (Archive &ar, const unsigned int version)

Static Private Member Functions

static Glib::RefPtr< Gdk::Pixbuf > drawEmpty_ (int)

Private Attributes

Glib::RefPtr< Gdk::Pixbuf > pb [2]

Static Private Attributes

static Glib::RefPtr< Gdk::Pixbuf > pbEmpty

Friends

class PreStroke
class boost::serialization::access
class Stats

Detailed Description

Definition at line 49 of file gesture.h.


Constructor & Destructor Documentation

Stroke::Stroke ( PreStroke s,
int  trigger_,
int  button_,
bool  timeout_ 
) [private]

Definition at line 31 of file gesture.cc.

                                                                      : button(button_), timeout(timeout_) {
       trigger = (trigger_ == get_default_button()) ? 0 : trigger_;

       if (ps.valid()) {
              stroke_t *s = stroke_alloc(ps.size());
              for (std::vector<RTriple>::iterator i = ps.begin(); i != ps.end(); ++i)
                     stroke_add_point(s, (*i)->x, (*i)->y);
              stroke_finish(s);
              stroke.reset(s, &stroke_free);
       }
}

Here is the call graph for this function:

Stroke::Stroke ( ) [inline]

Definition at line 124 of file gesture.h.

: trigger(0), button(0), timeout(false) {}

Here is the caller graph for this function:


Member Function Documentation

int Stroke::compare ( RStroke  a,
RStroke  b,
double &  score 
) [static]

Definition at line 43 of file gesture.cc.

                                                       {
       score = 0.0;
       if (!a || !b)
              return -1;
       if (!a->timeout != !b->timeout)
              return -1;
       if (a->button != b->button)
              return -1;
       if (a->trigger != b->trigger) {
              if (a->trigger && b->trigger)
                     return -1;
              if (a->trigger + b->trigger != get_default_button())
                     return -1;
       }
       if (!a->stroke || !b->stroke) {
              if (!a->stroke && !b->stroke) {
                     score = 1.0;
                     return 1;
              }
              else
                     return -1;
       }
       double cost = stroke_compare(a->stroke.get(), b->stroke.get(), NULL, NULL);
       if (cost >= stroke_infinity)
              return -1;
       score = MAX(1.0 - 2.5*cost, 0.0);
       if (a->timeout)
              return score > 0.85;
       else
              return score > 0.7;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static RStroke Stroke::create ( PreStroke s,
int  trigger_,
int  button_,
bool  timeout_ 
) [inline, static]

Definition at line 125 of file gesture.h.

                                                                                     {
              return RStroke(new Stroke(s, trigger_, button_, timeout_));
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Glib::RefPtr< Gdk::Pixbuf > Stroke::draw ( int  size,
double  width = 2.0,
bool  inv = false 
) const

Definition at line 75 of file gesture.cc.

                                                                         {
       if (size != STROKE_SIZE || (width != 2.0 && width != 4.0) || inv)
              return draw_(size, width, inv);
       int i = width == 2.0;
       if (pb[i])
              return pb[i];
       pb[i] = draw_(size, width);
       return pb[i];
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Stroke::draw ( Cairo::RefPtr< Cairo::Surface >  surface,
int  x,
int  y,
int  w,
int  h,
double  width = 2.0,
bool  inv = false 
) const

Definition at line 24 of file win.cc.

                                                                                                             {
       const Cairo::RefPtr<Cairo::Context> ctx = Cairo::Context::create (surface);
       x += width; y += width; w -= 2*width; h -= 2*width;
       ctx->save();
       ctx->translate(x,y);
       ctx->scale(w,h);
       ctx->set_line_width(2.0*width/(w+h));
       if (size()) {
              ctx->set_line_cap(Cairo::LINE_CAP_ROUND);
              int n = size();
              float lambda = sqrt(3)-2.0;
              float sum = lambda / (1 - lambda);
              std::vector<Point> y(n);
              y[0] = points(0) * sum;
              for (int j = 0; j < n-1; j++)
                     y[j+1] = (y[j] + points(j)) * lambda;
              std::vector<Point> z(n);
              z[n-1] = points(n-1) * (-sum);
              for (int j = n-1; j > 0; j--)
                     z[j-1] = (z[j] - points(j)) * lambda;
              for (int j = 0; j < n-1; j++) {
                     // j -> j+1
                     if (inv)
                            ctx->set_source_rgba(time(j), 0.0, 1.0-time(j), 1.0);
                     else
                            ctx->set_source_rgba(0.0, time(j), 1.0-time(j), 1.0);
                     Point p[4];
                     p[0] = points(j);
                     p[3] = points(j+1);
                     p[1] = p[0] + y[j] + z[j];
                     p[2] = p[3] - y[j+1] - z[j+1];
                     ctx->move_to(p[0].x, p[0].y);
                     ctx->curve_to(p[1].x, p[1].y, p[2].x, p[2].y, p[3].x, p[3].y);
                     ctx->stroke();
              }
       } else if (!button) {
              if (inv)
                     ctx->set_source_rgba(1.0, 1.0, 0.0, 1.0);
              else
                     ctx->set_source_rgba(0.0, 0.0, 1.0, 1.0);
              ctx->move_to(0.33, 0.33);
              ctx->line_to(0.67, 0.67);
              ctx->move_to(0.33, 0.67);
              ctx->line_to(0.67, 0.33);
              ctx->stroke();
       }
       ctx->restore();
       Glib::ustring str;
       if (trigger)
              str = Glib::ustring::compose("%1\xE2\x86\x92", trigger);
       if (timeout)
              str += "x";
       if (button)
              str += Glib::ustring::compose("%1", button);
       if (str == "")
              return;
       if (inv)
              ctx->set_source_rgba(0.0, 1.0, 1.0, 0.8);
       else
              ctx->set_source_rgba(1.0, 0.0, 0.0, 0.8);
       ctx->set_font_size(h*0.5);
       Cairo::TextExtents te;
       ctx->get_text_extents(str, te);
       ctx->move_to(x+w/2 - te.x_bearing - te.width/2, y+h/2 - te.y_bearing - te.height/2);
       ctx->show_text(str);
}

Here is the call graph for this function:

Glib::RefPtr< Gdk::Pixbuf > Stroke::draw_ ( int  size,
double  width = 2.0,
bool  inv = false 
) const [private]

Definition at line 99 of file win.cc.

                                                                          {
       Glib::RefPtr<Gdk::Pixbuf> pb = drawEmpty_(size);
       int w = size;
       int h = size;
       int stride = pb->get_rowstride();
       guint8 *row = pb->get_pixels();
       // This is all pretty messed up
       // http://www.archivum.info/gtkmm-list@gnome.org/2007-05/msg00112.html
       Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create(row, Cairo::FORMAT_ARGB32, w, h, stride);
       draw(surface, 0, 0, pb->get_width(), size, width, inv);
       for (int i = 0; i < w; i++) {
              guint8 *px = row;
              for (int j = 0; j < h; j++) {
                     guint8 a = px[3];
                     guint8 r = px[2];
                     guint8 g = px[1];
                     guint8 b = px[0];
                     if (a) {
                            px[0] = ((((guint)r) << 8) - r) / a;
                            px[1] = ((((guint)g) << 8) - g) / a;
                            px[2] = ((((guint)b) << 8) - b) / a;
                     }
                     px += 4;
              }
              row += stride;
       }
       return pb;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Stroke::draw_svg ( std::string  filename) const

Definition at line 91 of file win.cc.

                                              {
       const int S = 32;
       const int B = 1;
       Cairo::RefPtr<Cairo::SvgSurface> s = Cairo::SvgSurface::create(filename, S, S);
       draw(s, B, B, S-2*B, S-2*B);
}

Here is the call graph for this function:

Glib::RefPtr< Gdk::Pixbuf > Stroke::drawDebug ( RStroke  a,
RStroke  b,
int  size 
) [static]

Definition at line 126 of file stats.cc.

                                                                      {
       // TODO: This is copy'n'paste from win.cc
       Glib::RefPtr<Gdk::Pixbuf> pb = drawEmpty_(size);
       if (!a || !b || !a->stroke || !b->stroke)
              return pb;
       int w = size;
       int h = size;
       int stride = pb->get_rowstride();
       guint8 *row = pb->get_pixels();
       // This is all pretty messed up
       // http://www.archivum.info/gtkmm-list@gnome.org/2007-05/msg00112.html
       Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create(row, Cairo::FORMAT_ARGB32, w, h, stride);
       const Cairo::RefPtr<Cairo::Context> ctx = Cairo::Context::create(surface);

       for (unsigned int s = 0; s+1 < a->size(); s++)
              for (unsigned int t = 0; t+1 < b->size(); t++) {
                     double col = 1.0 - stroke_angle_difference(a->stroke.get(), b->stroke.get(), s, t);
                     ctx->set_source_rgba(col,col,col,1.0);
                     ctx->rectangle(a->time(s)*size, (1.0-b->time(t+1))*size,
                                   (a->time(s+1)-a->time(s))*size, (b->time(t+1)-b->time(t))*size);
                     ctx->fill();
              }
       int path_x[a->size() + b->size()];
       int path_y[a->size() + b->size()];
       stroke_compare(a->stroke.get(), b->stroke.get(), path_x, path_y);
       ctx->set_source_rgba(1,0,0,1);
       ctx->set_line_width(2);
       ctx->move_to(size, 0);
       for (int i = 0;; i++) {
              ctx->line_to(a->time(path_x[i])*size, (1.0-b->time(path_y[i]))*size);
              if (!path_x[i] && !path_y[i])
                     break;
       }
       ctx->stroke();

       for (int i = 0; i < w; i++) {
              guint8 *px = row;
              for (int j = 0; j < h; j++) {
                     guint8 a = px[3];
                     guint8 r = px[2];
                     guint8 g = px[1];
                     guint8 b = px[0];
                     if (a) {
                            px[0] = ((((guint)r) << 8) - r) / a;
                            px[1] = ((((guint)g) << 8) - g) / a;
                            px[2] = ((((guint)b) << 8) - b) / a;
                     }
                     px += 4;
              }
              row += stride;
       }
       return pb;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Glib::RefPtr< Gdk::Pixbuf > Stroke::drawEmpty ( int  size) [static]

Definition at line 87 of file gesture.cc.

                                                {
       if (size != STROKE_SIZE)
              return drawEmpty_(size);
       if (pbEmpty)
              return pbEmpty;
       pbEmpty = drawEmpty_(size);
       return pbEmpty;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Glib::RefPtr< Gdk::Pixbuf > Stroke::drawEmpty_ ( int  size) [static, private]

Definition at line 129 of file win.cc.

                                                 {
       Glib::RefPtr<Gdk::Pixbuf> pb = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB,true,8,size,size);
       pb->fill(0x00000000);
       return pb;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool Stroke::is_timeout ( ) const [inline]

Definition at line 142 of file gesture.h.

{ return timeout; }
template<class Archive >
void Stroke::load ( Archive &  ar,
const unsigned int  version 
) [inline, private]

Definition at line 97 of file gesture.h.

                                                                                   {
              std::vector<Point> ps;
              ar & ps;
              if (ps.size()) {
                     stroke_t *s = stroke_alloc(ps.size());
                     for (std::vector<Point>::iterator i = ps.begin(); i != ps.end(); ++i)
                            stroke_add_point(s, i->x, i->y);
                     stroke_finish(s);
                     stroke.reset(s, &stroke_free);
              }
              if (version == 0) return;
              ar & button;
              if (version >= 2)
                     ar & trigger;
              if (version < 4 && (!button || trigger == get_default_button()))
                     trigger = 0;
              if (version < 3)
                     return;
              ar & timeout;
       }

Here is the call graph for this function:

Point Stroke::points ( int  n) const [inline]

Definition at line 140 of file gesture.h.

{ Point p; stroke_get_point(stroke.get(), n, &p.x, &p.y); return p; }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class Archive >
void Stroke::save ( Archive &  ar,
const unsigned int  version 
) const [inline, private]

Definition at line 88 of file gesture.h.

                                                                                         {
              std::vector<Point> ps;
              for (unsigned int i = 0; i < size(); i++)
                     ps.push_back(points(i));
              ar & ps;
              ar & button;
              ar & trigger;
              ar & timeout;
       }

Here is the call graph for this function:

unsigned int Stroke::size ( ) const [inline]

Definition at line 138 of file gesture.h.

{ return stroke ? stroke_get_size(stroke.get()) : 0; }

Here is the call graph for this function:

Here is the caller graph for this function:

double Stroke::time ( int  n) const [inline]

Definition at line 141 of file gesture.h.

{ return stroke_get_time(stroke.get(), n); }

Here is the call graph for this function:

Here is the caller graph for this function:

RStroke Stroke::trefoil ( ) [static]

Definition at line 97 of file gesture.cc.

                        {
       PreStroke s;
       const int n = 40;
       for (int i = 0; i<=n; i++) {
              double phi = M_PI*(-4.0*i/n)-2.7;
              double r = exp(1.0 + sin(6.0*M_PI*i/n)) + 2.0;
              s.add(create_triple(r*cos(phi), r*sin(phi), i));
       }
       return Stroke::create(s, 0, 0, false);
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool Stroke::trivial ( ) const [inline]

Definition at line 139 of file gesture.h.

{ return size() == 0 && button == 0; }

Here is the call graph for this function:


Friends And Related Function Documentation

friend class boost::serialization::access [friend]

Definition at line 51 of file gesture.h.

friend class PreStroke [friend]

Definition at line 50 of file gesture.h.

friend class Stats [friend]

Definition at line 52 of file gesture.h.


Member Data Documentation

Definition at line 120 of file gesture.h.

Glib::RefPtr<Gdk::Pixbuf> Stroke::pb[2] [mutable, private]

Definition at line 82 of file gesture.h.

Glib::RefPtr< Gdk::Pixbuf > Stroke::pbEmpty [static, private]

Definition at line 85 of file gesture.h.

boost::shared_ptr<stroke_t> Stroke::stroke

Definition at line 122 of file gesture.h.

Definition at line 121 of file gesture.h.

Definition at line 119 of file gesture.h.


The documentation for this class was generated from the following files: