Re: [clutter] Clutter gradient and text

2010-04-22 Thread Sam Wilson
On Thu, 2010-04-22 at 10:31 +0100, Neil Roberts wrote:
 Hi,
 
 On Sam Wilson tecywiz...@hotmail.com wrote:
  Hello, I hate to bother you all with trivial stuff like this, but I am
  having some serious trouble finding the error in my program.
  
  Essentially, I want to render a shape with a gradient background and a
  text label, so I created an actor that has a single Text actor as a
  child.
  
  The gradient is rendered using a method similar to
  http://lists.o-hand.com/clutter/1787.html written in vala and adapted to
  handle angles.  (Any improvements to this function would also be welcome
  C: )
  
  A screenshot of the incorrect behaviour is located at
  http://sellyourvote.ca/Screenshot-test.png
  
  I know it has something to do with the Text actor, since both rectangles
  render properly if the text is turned off in both of them.
 
 I'm having trouble compiling your Vala sample so I can't test the
 theory,

Hmm... Might be the compiler version, I am using a recent build from
git, but 0.8.0 should also be able to compile it fine


  but my guess is that the problem is because you're not setting a
 material before you paint the gradient. When you paint the first
 gradient on the left you will have the material left over from whatever
 was painted last in the previous scene. I think in this case that would
 be the text. Text is painted using a special material that I think would
 break your gradient. The second gradient works because it will use the
 material left over from painting the outline.
 
 I think all it needs is this line at the top of your draw_gradient
 method:
 
   Cogl.set_source_color({128, 128, 128, 255})
 
 - Neil
 

You sir, are a god!  I can't believe that's all I was missing :P

Thanks,
Sam


smime.p7s
Description: S/MIME cryptographic signature


[clutter] Clutter gradient and text

2010-04-21 Thread Sam Wilson
Hello, I hate to bother you all with trivial stuff like this, but I am
having some serious trouble finding the error in my program.

Essentially, I want to render a shape with a gradient background and a
text label, so I created an actor that has a single Text actor as a
child.

The gradient is rendered using a method similar to
http://lists.o-hand.com/clutter/1787.html written in vala and adapted to
handle angles.  (Any improvements to this function would also be welcome
C: )

A screenshot of the incorrect behaviour is located at
http://sellyourvote.ca/Screenshot-test.png

I know it has something to do with the Text actor, since both rectangles
render properly if the text is turned off in both of them.

Thanks,
Sam



// test.vala - Problem clutter file
// Should render two rounded rectangles with gradients,
// the left one without text, and the right one with text

// On my machine left box only has border, but right box
// is correct.

// Compile with valac --pkg=clutter-1.0 test.vala

using Clutter;
using Cogl;

struct Point
{
float x;
float y;

}

public class TestActor : Actor
{
Text t = new Text.with_text(null, Test);
bool _text;

construct
{
t.set_parent(this);
t.set_size(40, 40);
t.set_position(10, 10);
}

public TestActor(bool draw_text)
{
_text = draw_text;
}

private inline static void rotate_point(ref Point p,
float angle)
{
float oldx = p.x, oldy = p.y;
p.x = oldx*Math.cosf(angle) - oldy*Math.sinf(angle);
p.y = oldy*Math.cosf(angle) + oldx*Math.sinf(angle);
}

private static void draw_gradient(float width,
  float height,
  Cogl.Color c1,
  Cogl.Color c2,
  float angle)
{
if (angle = Math.PI)
{
Cogl.Color temp = c1;
c1 = c2;
c2 = temp;
}

float sin_a = Math.sinf(angle);
float cos_a = Math.cosf(angle);

float A = sin_a*width;
float B = cos_a*width;

float C = sin_a*height;
float D = cos_a*height;

float s1 = A+D;
float s2 = B+C;

float x0 = -s2/2f, x1 = x0 + s2;
float y0 = -s1/2f, y1 = y0 + s1;

Point p[4];

p[0] = {x0, y0};
p[1] = {x0, y1};
p[2] = {x1, y1};
p[3] = {x1, y0};

for (int i = 0; i  p.length; i++)
{
rotate_point(ref p[i], angle);
p[i].x += width/2f;
p[i].y += height/2f;
}

TextureVertex v[4];

v[0] = {p[0].x, p[0].y, 0, 0, 0, c1};
v[1] = {p[1].x, p[1].y, 0, 0, 0, c1};
v[2] = {p[2].x, p[2].y, 0, 0, 0, c2};
v[3] = {p[3].x, p[3].y, 0, 0, 0, c2}; 

Cogl.polygon(v, true);
}

protected override void paint()
{
Geometry geom;
get_allocation_geometry(out geom);

Cogl.Color pri = {0, 255, 0, 255};
Cogl.Color sec = {0, 0, 255, 255};

Cogl.path_round_rectangle(0,
  0,
  geom.width,
  geom.height,
  5,
  (float)Math.PI_4/4f);

Cogl.clip_push_from_path();

draw_gradient(geom.width, geom.height,
  pri,
  sec,
  (float)Math.PI/4);

Cogl.clip_pop();

Cogl.path_round_rectangle(0,
  0,
  geom.width,
  geom.height,
  5,
  (float)Math.PI_4/4f);
  
Cogl.set_source_color({128, 128, 128, 255});
Cogl.path_stroke();

if (_text)
t.paint();
}

protected override void