How to use MOAIColor to draw circle

Discussion about using Moai SDK - post questions, bugs and issues here.

Moderators: seebs, franciscotufro

How to use MOAIColor to draw circle

Postby tingnapianhai » Wed Aug 01, 2012 7:30 am

Hi, I want to use MOAIColor to set the color for circle drawing, not just MOAIGfxDevice.setPenColor.

This is the way by using MOAIGfxDevice which already works.
Code: Select all
  1. local function onDraw()

  2.         MOAIGfxDevice.setPenColor(1,0,1,1)

  3.         MOAIDraw.fillCircle(0, 0, self.radius, self.steps)

  4. end



But I want to use MOAIColor to do this. Like:
Code: Select all
  1. local function onDraw()

  2.         color = MOAIColor.new()

  3.         color:setColor(1,0,1,1)

  4.         MOAIGfxDevice.setPenColor(color)

  5.         MOAIDraw.fillCircle(0, 0, self.radius, self.steps)

  6. end



But this doesnot work, anyone knows how to implement it ? Thanks
tingnapianhai
 
Posts: 7
Joined: Sun Jul 15, 2012 4:43 am

Re: How to use MOAIColor to draw circle

Postby pygy79 » Wed Aug 01, 2012 9:14 am

I think that it is possible but more complex than that. I didn't test the method, but I believe it should work.

You must first render your polygons to a script Deck (there's an example in the deck section of the samples). Make them white.
Then, when you instantiate a prop, you can make it inherit its color from your color object.

Schematically:
Code: Select all
  1. prop:setDeck(yourScriptableDeck)

  2. prop:setAttrLink(MOAIColor.INHERIT_COLOR, color, MOAIColor.INHERIT_COLOR)



If this doesn't work, linking each channel individually should definitely work. See here for the constants: http://getmoai.com/docs/class_m_o_a_i_color.html

From there on, updates to the color objects will propagate to your props.

You should also have a look at these articles:

http://getmoai.com/wiki/index.php?title ... s_Part_One, http://getmoai.com/wiki/index.php?title ... s_Part_Two

The second gives more details about the attributes.
Last edited by pygy79 on Wed Aug 01, 2012 9:28 am, edited 2 times in total.
User avatar
pygy79
 
Posts: 69
Joined: Wed Jan 04, 2012 3:13 am

Re: How to use MOAIColor to draw circle

Postby derickdong » Wed Aug 01, 2012 9:19 am

Is there some requirement that makes it necessary for you to draw a circle this way? If you want to render a circle and use MOAIColor to set its color, I'd recommend just using a MOAIDeck/MOAIProp combo with a texture of a circle. If you make the circle in the texture white, you can set it using MOAIColor.
derickdong
 
Posts: 63
Joined: Wed Nov 09, 2011 2:54 pm

Re: How to use MOAIColor to draw circle

Postby pygy79 » Wed Aug 01, 2012 9:30 am

derickdong's method has the advantage that you can have anti-aliased borders if your texture does. MOAIDraw does not support anti-aliasing.
User avatar
pygy79
 
Posts: 69
Joined: Wed Jan 04, 2012 3:13 am

Re: How to use MOAIColor to draw circle

Postby zandegran » Thu Aug 02, 2012 2:16 am

If this doesn't work, linking each channel individually should definitely work. See here for the constants: http://getmoai.com/docs/class_m_o_a_i_color.html
I tried using the attributes as parameters to setpencolor and it is not working..
Code: Select all
  1. color=MOAIColor.new()

  2. color:setColor(1,1,1)

  3. print("Col",color.ATTR_R_COL)

This code prints nil.
As you said it should work...
is there some requirement that makes it necessary for you to draw a circle this way?
yes there is. I need to seek color for a specific time..It also gives me the flexibility of choosing EaseTypes


I'm a colleague of "tingnapianhai"
zandegran
 
Posts: 17
Joined: Thu Jul 12, 2012 3:49 am

Re: How to use MOAIColor to draw circle

Postby derickdong » Thu Aug 02, 2012 9:46 am

zandegran wrote:I tried using the attributes as parameters to setpencolor and it is not working..
Code: Select all
  1. color=MOAIColor.new()

  2. color:setColor(1,1,1)



I just tried the method pygy79 suggested, and it works. However, using setPenColor in the draw function overrides the color value (as it probably should).

Code: Select all
  1. MOAISim.openWindow ( "test", 320, 480 )

  2.  

  3. viewport = MOAIViewport.new ()

  4. viewport:setSize ( 320, 480 )

  5. viewport:setScale ( 320, 480 )

  6.  

  7. layer = MOAILayer2D.new ()

  8. layer:setViewport ( viewport )

  9. MOAISim.pushRenderPass ( layer )

  10.  

  11. function onDraw ( index, xOff, yOff, xFlip, yFlip )

  12.         MOAIDraw.fillCircle ( 0, 0, 64, 32 )

  13. end

  14.  

  15. scriptDeck = MOAIScriptDeck.new ()

  16. scriptDeck:setRect ( -64, -64, 64, 64 )

  17. scriptDeck:setDrawCallback ( onDraw )

  18.  

  19. color = MOAIColor.new()

  20. color:setColor(1, 0, 0, 1)

  21.  

  22. prop = MOAIProp2D.new ()

  23. prop:setDeck ( scriptDeck )

  24. layer:insertProp ( prop )

  25.  

  26. prop:setAttrLink(MOAIColor.INHERIT_COLOR, color, MOAIColor.COLOR_TRAIT)

  27.  



print("Col",color.ATTR_R_COL)[/code] This code prints nil.
As you said it should work...

An instance of MOAIColor doesn't have this attribute. If you want the current red value of color, it would be:

Code: Select all
  1. print(color:getAttr(MOAIColor.ATTR_R_COL))



zandegran wrote:
is there some requirement that makes it necessary for you to draw a circle this way?
yes there is. I need to seek color for a specific time..It also gives me the flexibility of choosing EaseTypes

I was referring to why you were using a MOAIScriptDeck and the MOAIDraw methods for drawing the circle. Rendering a texture of a circle, via a MOAIProp and MOAIGfxQuad2D, is a far superior method in nearly every way. It also allows the use of MOAIColor, and is somewhat easier to boot.
derickdong
 
Posts: 63
Joined: Wed Nov 09, 2011 2:54 pm


Return to Moai SDK

Who is online

Users browsing this forum: PKulik and 0 guests

cron

x