Thread Tools Display Modes
05-06-18, 12:02 PM   #1
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
Resizing Frames

I've tried a bit of searching both here and Google, and only found a few examples. I'm trying to make JWXPBar and JWRepBar re-sizable. The frame moves regardless of which modifier I use. Here is my code (I've included the entire addon just in case ). Lines 31-34 and 60-79 is the code related to resizing.

Lua Code:
  1. -- Yes Another XP Bar.
  2.  
  3. --Config area
  4. local JWBarHeight = 28
  5. local JWBarWidth = 350
  6. local JWBarPoint = {"CENTER", "JWRepBarFrame","CENTER", 0, 0}
  7. local JWBarTexture = "Interface\\TargetingFrame\\UI-StatusBar"
  8. --local JWBarFont = [[Fonts\FRIZQT__.TTF]]
  9. --local JWBarFont = [[Interface\addons\JWRepBar\ROADWAY_.ttf]]
  10. --local JWBarFontSize = 18
  11. --local JWBarFontFlags = "NONE"
  12.  
  13. --Beyond here be dragons
  14. function comma_value(n)
  15.   return tostring(math.floor(n)):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
  16. end
  17.  
  18. local function tchelper(first, rest)
  19.   return first:upper()..rest:lower()
  20. end
  21.  
  22. local JWRepBarFrame = CreateFrame("Frame", "JWRepBarFrame", UIParent)
  23. JWRepBarFrame:SetFrameStrata("HIGH")
  24. JWRepBarFrame:SetHeight(JWBarHeight)
  25. JWRepBarFrame:SetWidth(JWBarWidth)
  26. if IsAddOnLoaded("JWXPBar") then
  27.     JWRepBarFrame:SetPoint("BOTTOM", "JWXPBar", "TOP", 0, 5)
  28. else
  29.     JWRepBarFrame:SetPoint("CENTER", UIPARENT, "CENTER", 0, -275)
  30. end
  31. JWRepBarFrame:EnableMouse(true)
  32. JWRepBarFrame:SetMovable(true)
  33. JWRepBarFrame:SetClampedToScreen(true)
  34. JWRepBarFrame:SetResizable(true)
  35.  
  36. --Create Background and Border
  37. local backdrop = JWRepBarFrame:CreateTexture(nil, "BACKGROUND")
  38. backdrop:SetHeight(JWBarHeight)
  39. backdrop:SetWidth(JWBarWidth)
  40. backdrop:SetPoint(unpack(JWBarPoint))
  41. backdrop:SetTexture(JWBarTexture)
  42. backdrop:SetVertexColor(0.1, 0.1, 0.1)
  43. JWRepBarFrame.backdrop = backdrop
  44.  
  45. --Rep Bar
  46. local JWRepBar = CreateFrame("StatusBar", "JWRepBar", JWRepBarFrame)
  47. JWRepBar:SetWidth(JWBarWidth)
  48. JWRepBar:SetHeight(JWBarHeight)
  49. JWRepBar:SetPoint(unpack(JWBarPoint))
  50. JWRepBar:SetStatusBarTexture(JWBarTexture)
  51. JWRepBar:GetStatusBarTexture():SetHorizTile(false)
  52. JWRepBarFrame.JWRepBar = JWRepBar
  53.  
  54. --Create XP Text
  55. local Text = JWRepBar:CreateFontString("JWRepBarText", "OVERLAY", "NumberFontNormal") --GameFontNormal
  56. --Text:SetFont(JWBarFont, JWBarFontSize, JWBarFontFlags)
  57. Text:SetPoint("CENTER", JWRepBar, "CENTER",0,1)
  58. Text:SetAlpha(1)
  59.  
  60. JWRepBarFrame:SetScript("OnMouseDown", function(self, button)
  61.   if button == "LeftButton" and (IsShiftKeyDown()) and not self.isMoving then
  62.     self:StartMoving();
  63.     self.isMoving = true;
  64.   elseif (IsShiftKeyDown() ) and (button == "RightButton" and not self.isSizing) then
  65.     self:StartSizing();
  66.     self.isSizing = true;
  67.   end
  68. end)
  69.  
  70. JWRepBarFrame:SetScript("OnMouseUp", function(self, button)
  71.   --if button == "LeftButton" and (IsShiftKeyDown()) and self.isMoving then
  72.   if button == "LeftButton" and self.isMoving then
  73.     self:StopMovingOrSizing();
  74.     self.isMoving = false;
  75.   elseif (button == "RightButton" and self.isSizing) then
  76.     self:StopMovingOrSizing();
  77.     self.isSizing = false;
  78.   end
  79. end)
  80.    
  81. local function UpdateStatus()
  82.     local JWRepName, JWRepStanding, JWRepMin, JWRepMax, JWRepCur = GetWatchedFactionInfo()
  83.     if JWRepName then
  84.         local JWRepColor = FACTION_BAR_COLORS[JWRepStanding]
  85.         JWRepBar:SetStatusBarColor(JWRepColor.r * 0.8, JWRepColor.g * 0.8, JWRepColor.b * 0.8)
  86.         JWRepBar:SetMinMaxValues(JWRepMin, JWRepMax)
  87.         JWRepBar:SetValue(JWRepCur)
  88.         JWRepName = JWRepName:gsub("(%a)([%w_']*)", tchelper) --Proper case
  89.         Text:SetText(format("%s %s/%s ", JWRepName, comma_value(JWRepCur), comma_value(JWRepMax)))
  90.         return
  91.     end
  92. end
  93.  
  94. JWRepBarFrame:RegisterEvent("UPDATE_FACTION")
  95. JWRepBarFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
  96. JWRepBarFrame:SetScript("OnEvent", UpdateStatus)

Any help or pointers in the right direction is appreciated.
__________________
Author of JWExpBar and JWRepBar.
  Reply With Quote
05-06-18, 01:21 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
What is your actual question? You said "the frame moves regardless of which modifier I use", but is that a description something your code does now that you don't want, or a description of something it doesn't do that you do want? Does anything related to moving/resizing work right now in your addon? If so, what works, and how is it different than what you want it to do?
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
05-06-18, 01:36 PM   #3
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
Originally Posted by Phanx View Post
What is your actual question? You said "the frame moves regardless of which modifier I use", but is that a description something your code does now that you don't want, or a description of something it doesn't do that you do want? Does anything related to moving/resizing work right now in your addon? If so, what works, and how is it different than what you want it to do?
Sorry, to clarify. My frames are currently dragable, which is what I want by using the shift key and the left mouse button. What I also want is to be able to re-size them using the shift key and the right mouse button which does not work. When I try to use the shift key and the right mouse button, the frames drag and do not resize. I hope this is a bit clearer.
__________________
Author of JWExpBar and JWRepBar.
  Reply With Quote
05-06-18, 02:01 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I'm not sure how that second part (frames drag when you use the right mouse button) can happen, given the code you posted:

Lua Code:
  1. JWRepBarFrame:SetScript("OnMouseDown", function(self, button)
  2.   if button == "LeftButton" and (IsShiftKeyDown()) and not self.isMoving then
  3.     self:StartMoving();
  4.     self.isMoving = true;
  5.   elseif (IsShiftKeyDown() ) and (button == "RightButton" and not self.isSizing) then
  6.     self:StartSizing();
  7.     self.isSizing = true;
  8.   end
  9. end)

Try adding some print statements to clarify what's happening:

Code:
JWRepBarFrame:SetScript("OnMouseDown", function(self, button)
  if button == "LeftButton" and (IsShiftKeyDown()) and not self.isMoving then
    print("moving");
    self:StartMoving();
    self.isMoving = true;
  elseif (IsShiftKeyDown() ) and (button == "RightButton" and not self.isSizing) then
    print("resizing");
    self:StartSizing();
    self.isSizing = true;
  end
end)
Other notes unrelated to functionality:
  • Semicolons are unnecessary in Lua, so unless you really like typing them, you can leave them out.
  • None of the parentheses in your conditional statements are necessary.
    • IsShiftKeyDown() only returns a single value (true or false) so there's no need to contain it with parentheses anywhere, and even for functions that return multiple values (e.g. UnitBuff) you wouldn't need to contain it in this context, since the parentheses just truncate the list of return values down to one value, but that already happens in an "if X (and/or/then)" statement.
    • There's no reason to wrap the 2nd and 3rd checks in your second conditional list in parentheses; in this context, parentheses are used for grouping, but here you're essentially asking "is this thing true, and both of these other 2 things true?" instead of the simpler "are all 3 of these things true?". It doesn't change the functionality (in this case) but it does make the code more complicated to read and understand. Grouping is mainly useful when you start mixing "and" and "or", as in "if A and (B or C) then" or "if A or (B and C) then".
  • Since IsShiftKeyDown() is required for anything else to happen, I'd remove that from your "if" and "elseif" lines, and just add a single "if not IsShiftKeyDown() then return end" line at the top of the function, to check that first and not bother checking anything else if it's false.
  • I also wouldn't bother checking e.g. if "self.isMoving" isn't true. In the highly unlikely event that you manage to press the left button more than once without releasing it in between, it won't hurt anything to "start moving" when the frame is already moving, and removing clutter from your code is always beneficial.

Compare:
Code:
JWRepBarFrame:SetScript("OnMouseDown", function(self, button)
  if not IsShiftKeyDown() then return end

  if button == "LeftButton" then
    print("moving")
    self:StartMoving()
    self.isMoving = true
  elseif button == "RightButton" then
    print("resizing")
    self:StartSizing()
    self.isSizing = true
  end
end)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
05-06-18, 02:12 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Actually after looking at the grand total of 3 objects in the default UI that are resizable using this method, I think the (main) problem is that you need to specify a direction in which to resize the frame, like:

Code:
object:StartSizing("BOTTOMRIGHT")
References:
If you want your frame to be resizable by any edge or corner, then you'll need to do some math to figure out which edge or corner to use based on the cursor location on mousedown.

The other option would be to add additional "drag handle" buttons on top of your frame, each sized and placed appropriately, and call "object:GetParent():StartSizing(point)" in their mousedown handlers. You can see that technique in use in my chat addon (written so long ago I'd forgotten about it initially) here:

https://github.com/phanx-wow/PhanxCh...esizeEdges.lua

You could make them invisible, or have them only appear when the shift button was pressed (by registering for the MODIFIER_STATE_CHANGED event on your frame).
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
05-06-18, 05:57 PM   #6
JDoubleU00
A Firelord
 
JDoubleU00's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 463
Originally Posted by Phanx View Post
Actually after looking at the grand total of 3 objects in the default UI that are resizable using this method, I think the (main) problem is that you need to specify a direction in which to resize the frame, like:

Code:
object:StartSizing("BOTTOMRIGHT")
References:
If you want your frame to be resizable by any edge or corner, then you'll need to do some math to figure out which edge or corner to use based on the cursor location on mousedown.

The other option would be to add additional "drag handle" buttons on top of your frame, each sized and placed appropriately, and call "object:GetParent():StartSizing(point)" in their mousedown handlers. You can see that technique in use in my chat addon (written so long ago I'd forgotten about it initially) here:

https://github.com/phanx-wow/PhanxCh...esizeEdges.lua

You could make them invisible, or have them only appear when the shift button was pressed (by registering for the MODIFIER_STATE_CHANGED event on your frame).
Thanks, I've already tweaked the code with your suggestions. It will take me some time to look at your code and the Blizz code to decide how to implement it. I guess I did not look hard enough at the chat frame code and I rarely look at XML files.
__________________
Author of JWExpBar and JWRepBar.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Resizing Frames

Thread Tools
Display Modes

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