Movable frame with buttons inside (lua)
2 Attachment(s)
I'm trying to make a small addon (my first time) and I'm very new to coding - did some html when the internet was young, but that's kinda it.
The addon: The addon is meant to help people lead in Ashran. Sending simple commands to the raid via the click of a button (for testing purposes it's set to "SAY" for now. The addon is not meant to provide any information about the flow of battle, AshranBuddy does that just fine. What is working? After a crashcourse in lua and scavaging code from this site and others, I've managed to make the buttons. They function as they should and are arranged in relation to eachother the way I want them to. The mouseover tooltip also works. What the current issue? The wall I've hit now is getting the buttons to be inside a frame and make the frame (with buttons ofc) movable. I've tried moving the frame part of the code around, but that results in nothing showing or only the frame. I've tried getting the buttons to have the frame as parent and hope they would align, but nothing has worked. I've also tried to only have the frame to see if I could move it, but again my skills feel short. The books I've found use XML in their example addons for the frame and movable frame parts, but I don't think it should be nessesary for such a simple addon. What next? Once the frame is movable and the buttons aligned inside the frame, I want to make sure it remembers where the user has placed the addon after relog/reload. Next is making the frame hide when not in Ashran Afterwards try to allow the user to resize the addon and ofcourse have the addon remember the size the user has set. Lua Code:
toc Code:
|
Ok, am not an expert but will try to help (a bit).
First of all, if you want to have the buttons inside the frame (as achild) you have to create them using the frames name as the parent. Since the frame is called "ACFrame", you have to use that name So instead of using Lua Code:
Use Lua Code:
But as I said, I am no expert. There are people with vastly more knowlegde of lua, its quite possible they have better way. |
As far as I can see, AshranCommander hasn't been defined anywhere (it's not automatically create with the addon) so all the buttons are being parented to nil.
You should create your frame first and name it AshranCommander and then all the buttons will be parented to the frame. Edit: If you leave the frame creation at the end where it currently is, the buttons will still be parented to nil as the frame won't exist. |
You're setting the button's parent as AshranCommander, but the name you gave your frame is ACFrame.
/edit: Yukyuk beat me. :p /edit2: except Yukyuk accidentally told you to put ACFrame in quotes when calling your CreateButton function. ;) |
Quote:
Start with these answers first and we will help you wtih the other problems later. ;) |
I moved the frame to the top and renamed it (i hope it's the right spot) - This now shows the frame, unmovable, but not the buttons.
Am I missing something after frame:StopMovingOrSizing() ? func? Lua Code:
|
Here's some points I need to address before moving on:
The "parent" of a frame refers to another frame that it shares its visibility, scale, and other attributes with. For example, you have one frame representing a dialog box, and another representing a button. Normally, the button would have its parent set to the dialog box so when you show and hide the box, the button would respond accordingly and show or hide itself as well. ACFrame is defined at the bottom, which isn't useful at all. Lua and other programming languages execute code in top-down order. When you reference ACFrame, Lua doesn't know what it is yet and attempts to find a global with that name. At this time, it's simply nil. When Frame:SetPoint() gets this, it positions relative to the screen itself instead of the frame you're creating later. To start, let's rearrange the code so we can get things to work: This can stay where it is. The function only reacts to what's passed to it. Drag handlers registered to move parent. Button:SetNormalTexture() was used to show each icon. Lua Code:
The frame definition is moved here so the code below can use it. Proper registration for dragging has been implemented. New frames are always shown and have no anchors, so Frame:Show() and Frame:ClearAllPoints() respectively are unnecessary. Frame:SetSize() replaces both Frame:SetWidth() and Frame:SetHeight(). Frame:SetScale() was removed as you were sending nil to it and Frame:SetMovable() was properly set. Lua Code:
The following is slightly modified to use the frame pointer stored in frame immediately above. Layout was corrected to fit nicely within frame. Lua Code:
There is more that can be done to optimize the code like creating a layout table and using that in a loop to create your buttons, but for now, this'll do. |
A debugger would also help. If you install BugGrabber and either Bugger or BugSack you will find your coding life greatly enhanced.
|
There were a couple of other problems so this is the full code.
Code:
local CreateButton; do-- Prototype for function |
I've been doing micro-edits on mine. Finished enabling dragging on the main frame, set buttons to drag parent, corrected layout issues, etc. I think I'm done now minus the horrendous button layout code.
|
Thank you guys/girls so much. It's working now and with the cleanup SDPhantom gave it, !buggrabber (ty FizzleMizz) stopped complaining.
EDIT: I should specify what is working. The buttons are now movable and with them the frame. I also changed the first button to align to the frames TOPRIGHT instead Lua Code:
Looking at it, I'm thinking if there's a better way to size the frame based on the button size or vice-versa. Mess with Lua Code:
Lua Code:
Tried changing "40+40" to SecureActionButtonTemplate, hoping it would act similar to Lua Code:
but no, that was not the way :p Current complete Lua Lua Code:
|
You could use Frame:SetScale() to scale everything. For example, Frame:SetScale(0.5) will shrink the frame and its children to 50% of their size. Likewise, Frame:SetScale(1.25) will enlarge the frame and its children by 25%. At any rate, I honestly don't think resizing is going to be an in-demand feature.
PS: Don't forget to add the Frame:EnableMouse(true) that I forgot earlier, otherwise the container frame won't run the OnDrag* scripts. |
Quote:
I tried adding Frame:SetClampedToScreen(enable), but it just returned an error. Lua Code:
I instead added Lua Code:
Lua Code:
which does the job. As far as I can tell it works on the button which the mouse is interacting with, meaning that button cannot be moved out of the screen, but the others can. Since you can then still drag the entire frame back, I suppose that it good enough. You might have a point about the ability to resize. Set scale to 0.8 for now Lua Code:
|
You don't want to actually drag the buttons themselves, you want to activate the container frame onDragxxx
Code:
|
As far as I now SetClampedToScreen(boolean), meaning it has either to be true or false.
|
Quote:
Quote:
|
I think I will de-cook this by one. Best of luck tyroneexe.
|
Quote:
Lua Code:
Lua Code:
With the current code the frame and buttons all move together when dragging on one of the buttons. Feels nice and easy. I suppose if the player, in the heat of battle, could move the frame by accident. On the other hand it's easier to move without having the click the small area which is the frame. On another matter. I'm looking into having the frame only show in Ashran, similar to AshranBuddy. I'm guessing I'm looking for something like Fragment Lua from AshranBuddy Code:
note to self, should remember to load a char which is actually in Ashran for testing this. :D |
Quote:
Current code: Lua Code:
EDIT: did the changes suggested by FizzleMizz and while I could move the entire thing before, I can now "grap" it at the outer edge (frame) as well. :D |
Quote:
|
All times are GMT -6. The time now is 06:25 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI