Home » U++ Library support » U++ Widgets - General questions or Mixed problems » How to implent a rubber band Class in u++
Re: How to implent a rubber band Class in u++ [message #17298 is a reply to message #17297] |
Wed, 06 August 2008 11:04 ![Go to previous message Go to previous message](theme/default/images/up.png) ![Go to next message Go to previous message](theme/default/images/down.png) |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
I can think of two possible optimisations (besides PolyLine, which is definitely the first one to use)
1- Avoid adding adjacent collinear points. In practice this is a very minor optimisation except for special cases, but it appeals to my sense of correctness.
class RubberBand : public LocalLoop
{
public:
virtual void MouseMove(Point p, dword keyflags) { AddPoint(p); GetMaster().Refresh(); }
virtual void LeftUp(Point p, dword keyflags) { EndLoop(); }
virtual void RightUp(Point p, dword keyflags) { EndLoop(); }
const Vector<Point> & GetPoints() { return points; }
void Clear() { points.Clear(); }
private:
Vector<Point> points;
void AddPoint(const Point &newp);
};
void RubberBand::AddPoint(const Point &newp)
{
if (points.GetCount() < 2)
return points.Add(newp);
// Get line vectors
const Point &p = points[points.GetCount()-2];
Point p1 = points.Top() - p;
Point p2 = newp - p;
// If gradient is different, add the new point
if (p1.x*p2.y - p2.x*p1.y)
points.Add(newp);
// Otherwise update last points
else
points.Top() = newp;
}
2- Use a back buffer. Providing you have a static background (one that will not change while the rubber band is being drawn), you can use a back buffer to cache the background + the current rubber band and only draw the last line.
This was a major improvement for me, eliminating flickering entirely.
RubberBand band;
BackDraw back;
Size backsz;
void LeftDown(Point p, dword keyflags)
{
band.Clear();
band.SetMaster(*this);
Size sz = GetSize();
if (sz != backsz) {
back.Create(sz);
backsz = sz;
}
Paint(back);
band.Run();
Refresh();
}
void Paint(Draw& w)
{
const Vector<Point> &p = band.GetPoints();
if (!band.InLoop()) {
// Normal painting
w.DrawRect(GetSize(), White);
if (p.GetCount() >= 2) {
w.DrawPolyline(p);
w.DrawLine(p[p.GetCount()-1], p[0]);
}
}
else {
// RubberBand painting
if (p.GetCount() >= 2)
back.DrawLine(p[p.GetCount()-2], p.Top());
back.Put(w, 0, 0);
w.DrawText(4, 4, AsString(p.GetCount()));
}
}
|
|
|
![Read Message Read Message](theme/default/images/read.png) |
|
How to implent a rubber band Class in u++
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mrjt on Thu, 25 October 2007 16:44
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Mon, 07 July 2008 18:35
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Fri, 25 July 2008 20:02
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Sat, 26 July 2008 12:03
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sat, 26 July 2008 16:18
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Sat, 26 July 2008 16:49
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sat, 26 July 2008 22:45
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sun, 27 July 2008 01:02
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Fri, 01 August 2008 08:53
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Fri, 01 August 2008 22:33
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sat, 02 August 2008 09:49
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Mon, 04 August 2008 18:56
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Mon, 04 August 2008 19:39
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Mon, 04 August 2008 22:33
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Tue, 05 August 2008 15:44
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Tue, 05 August 2008 17:37
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Tue, 05 August 2008 19:54
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Wed, 06 August 2008 00:29
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mirek on Wed, 06 August 2008 08:34
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: mrjt on Wed, 06 August 2008 11:04
|
![Read Message Read Message](theme/default/images/read.png) |
|
Re: How to implent a rubber band Class in u++
By: tojocky on Sat, 09 August 2008 18:43
|
Goto Forum:
Current Time: Mon Jun 17 06:44:04 CEST 2024
Total time taken to generate the page: 0.02581 seconds
|