Thread: Resizing Frames
View Single Post
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