Thread Tools Display Modes
10-30-10, 02:07 PM   #1
cerement
A Murloc Raider
 
cerement's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 9
Simplifying code when "drawing" shapes?

I'm wondering what "best practices" are when drawing single color lines and rectangles.

Standard method is the usual: create a master frame, create the texture(s), size, position, SetTexture, SetVertexColor:
lua Code:
  1. local mmBackdrop = CreateFrame("Frame", nil, Minimap)
  2. local mmBG = mmBackdrop:CreateTexture(nil, "ARTWORK", mmBackdrop)
  3.  
  4. mmBG:SetSize(Minimap:GetWidth() +  6, Minimap:GetHeight() + 6)
  5. mmBG:SetPoint("CENTER", Minimap, "CENTER", 0, 0)
  6. mmBG:SetTexture("Interface\\Buttons\\WHITE8x8")
  7. mmBG:SetVertexColor(0, 0, 0, 0.6)
But the description of :SetTexture() on WoWWiki and WoWProgramming mentions that :SetTexture() can take either "texture" as an argument or R,G,B[,A] as an argument, shortening things by one line. But I've found out that if I have multiple textures attached to a frame, at least one needs to be "seeded" with a "texture", otherwise the rest won't show correctly. So, instead of writing the whole thing as :SetTexture(R, G, B [,A]), I have to do something like the following:
lua Code:
  1. mmBGT:SetTexture("Interface\\Buttons\\WHITE8x8")
  2. mmBGT:SetVertexColor(0.4, 0.4, 0.4, 0.8)
  3.  
  4. mmBGR:SetTexture(0.4, 0.4, 0.4, 0.8)
  5.  
  6. mmBGB:SetTexture(0.4, 0.4, 0.4, 0.8)
  7.  
  8. mmBGL:SetTexture(0.4, 0.4, 0.4, 0.8)
Is there a way around this "seeding" process or do I start risking things being a little inconsistent? Should I just stick with the classic :SetTexture("texture"), :SetVertexColor(R, G, B [,A]) combination to keep things clean and avoid any risk of things messing up?
  Reply With Quote
10-30-10, 05:40 PM   #2
Mischback
A Cobalt Mageweaver
 
Mischback's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 221
If you don't specify a texture but rgba-values, the whole texture will be colored solidly (regarding the alpha, of course).

So, for example

lua Code:
  1. local f = CreateFrame('Frame')
  2. f:SetWidth(200)
  3. f:SetHeight(200)
  4. f:SetPoint('CENTER')
  5.  
  6. f.t = CreateTexture(nil, 'ARTWORK', f)
  7. f.t:SetAllPoints(f)
  8. f.t:SetTexture(1, 0, 0, 1)

this will create a 200x200px frame in the middle of the screen, colored red.
__________________
  Reply With Quote
10-30-10, 06:48 PM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
You don't need to "seed" or set a texture file. If colors are given instead, it's just colored solid with that color, as already mentioned.

What exactly do you mean by "the rest won't show correctly"?
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
10-31-10, 12:30 AM   #4
cerement
A Murloc Raider
 
cerement's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 9
Originally Posted by Seerah View Post
What exactly do you mean by "the rest won't show correctly"?
Just that exactly.

From the larger example below, there's five textures:
1 medium-transparent black rectangle, and
4 mostly-opaque medium-gray "lines" (one on each edge)

With the first rectangle specified with :SetTexture("texture"), :SetVertexColor(R, G, B, [,A]) as below, all five shapes show up correctly. But if I change the :SetTexture() to using R, G, B [,A] and remove the :SetVertexColor(), then the first gray rectangle shows up, but the four lines do not show up.
lua Code:
  1. local mmBackdrop = CreateFrame("Frame", nil, Minimap)
  2. mmBackdrop:SetFrameLevel(max(1, Minimap:GetFrameLevel()-2))
  3.  
  4. local mmBG = mmBackdrop:CreateTexture(nil, "ARTWORK", mmBackdrop)
  5.  
  6. mmBG:SetSize(Minimap:GetWidth() +  6, Minimap:GetHeight() + 6)
  7. mmBG:SetPoint("CENTER", Minimap, "CENTER", 0, 0)
  8. -- this works
  9. mmBG:SetTexture("Interface\\Buttons\\WHITE8x8")
  10. mmBG:SetVertexColor(0, 0, 0, 0.6)
  11. -- but if I used the following line, then the later artwork doesn't show
  12. -- mmBG:SetTexture(0, 0, 0, 0.6)
  13.  
  14. local mmBGT = mmBackdrop:CreateTexture(nil, "ARTWORK", mmBackdrop)
  15. local mmBGR = mmBackdrop:CreateTexture(nil, "ARTWORK", mmBackdrop)
  16. local mmBGB = mmBackdrop:CreateTexture(nil, "ARTWORK", mmBackdrop)
  17. local mmBGL = mmBackdrop:CreateTexture(nil, "ARTWORK", mmBackdrop)
  18.  
  19. mmBGT:SetSize(mmBG:GetWidth() - 2, 1)
  20. mmBGT:SetPoint("TOP", mmBG, "TOP", 0, -1)
  21. mmBGT:SetTexture(0.4, 0.4, 0.4, 0.8)
  22.  
  23. mmBGR:SetSize(1, mmBG:GetHeight() - 2)
  24. mmBGR:SetPoint("RIGHT", mmBG, "RIGHT", -1, 0)
  25. mmBGR:SetTexture(0.4, 0.4, 0.4, 0.8)
  26.  
  27. mmBGB:SetSize(mmBG:GetWidth() - 2, 1)
  28. mmBGB:SetPoint("BOTTOM", mmBG, "BOTTOM", 0, 1)
  29. mmBGB:SetTexture(0.4, 0.4, 0.4, 0.8)
  30.  
  31. mmBGL:SetSize(1, mmBG:GetHeight() - 2)
  32. mmBGL:SetPoint("LEFT", mmBG, "LEFT", 1, 0)
  33. mmBGL:SetTexture(0.4, 0.4, 0.4, 0.8)


(and yes, I know it's easier to do all this with one :SetBackdrop(), this was just a proof of concept)
  Reply With Quote
10-31-10, 03:57 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
The third arg for CreateTexture is an optional template to inherit from. You have it inheriting from you mmBackground frame. If you remove that, then your code works perfectly.

/edit: http://wowprogramming.com/docs/widge.../CreateTexture
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
10-31-10, 06:41 PM   #6
cerement
A Murloc Raider
 
cerement's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 9
Originally Posted by Seerah View Post
If you remove that, then your code works perfectly.
Gah! Thanks! That's what I get for copy-pasting code without checking it ...
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Simplifying code when "drawing" shapes?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off