Quantcast

Adding new weights works

Kevin Lyles [10-13-09 - 09:49]
Adding new weights works
Filename
WeightsWatcher.xml
config.lua
diff --git a/WeightsWatcher.xml b/WeightsWatcher.xml
index f4fa9ea..074ed9d 100644
--- a/WeightsWatcher.xml
+++ b/WeightsWatcher.xml
@@ -339,7 +339,166 @@
 				</Anchors>
 				<Scripts>
 					<OnClick>
-						print("New weight not yet implemented")
+						configNewWeight()
+					</OnClick>
+				</Scripts>
+			</Button>
+		</Frames>
+	</Frame>
+	<Frame name="ww_newWeight" parent="UIParent" hidden="true" frameStrata="DIALOG">
+		<Size>
+			<AbsDimension x="420" y="120"/>
+		</Size>
+		<Anchors>
+			<Anchor point="CENTER"/>
+		</Anchors>
+		<Layers>
+			<Layer level="ARTWORK">
+				<FontString name="$parentText" parentKey="text" inherits="GameFontHighlight" text="Please choose a class and name for your new weight:">
+					<Size>
+						<AbsDimension x="380" y="0"/>
+					</Size>
+					<Anchors>
+						<Anchor point="TOP">
+							<Offset>
+								<AbsDimension x="0" y="-16"/>
+							</Offset>
+						</Anchor>
+					</Anchors>
+				</FontString>
+			</Layer>
+			<Layer level="BACKGROUND">
+				<Texture setAllPoints="true">
+					<!--Plain black background with 50% alpha-->
+					<Color r="0" g="0" b="0" a="0.5"/>
+				</Texture>
+			</Layer>
+		</Layers>
+		<Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true">
+			<BackgroundInsets>
+				<AbsInset left="11" right="12" top="12" bottom="11"/>
+			</BackgroundInsets>
+			<TileSize>
+				<AbsValue val="32"/>
+			</TileSize>
+			<EdgeSize>
+				<AbsValue val="32"/>
+			</EdgeSize>
+		</Backdrop>
+		<Frames>
+			<Button name="$parentDropDown" parentKey="dropdown" inherits="UIDropDownMenuTemplate">
+				<Anchors>
+					<Anchor point="LEFT"/>
+				</Anchors>
+				<Scripts>
+					<OnShow>
+						ClassDropDownOnShow(self);
+					</OnShow>
+				</Scripts>
+			</Button>
+			<EditBox letters="50" name="$parentEditBox" parentKey="editBox">
+				<Size>
+					<AbsDimension y="32"/>
+				</Size>
+				<Anchors>
+					<Anchor point="BOTTOM" >
+						<Offset>
+							<AbsDimension x="0" y="45"/>
+						</Offset>
+					</Anchor>
+					<Anchor point="RIGHT">
+						<Offset>
+							<AbsDimension x="-25"/>
+						</Offset>
+					</Anchor>
+					<Anchor point="LEFT" relativeTo="$parentDropDown" relativePoint="RIGHT">
+						<Offset>
+							<AbsDimension x="125"/>
+						</Offset>
+					</Anchor>
+				</Anchors>
+				<FontString inherits="ChatFontNormal"/>
+				<Layers>
+					<Layer level="BACKGROUND">
+						<Texture file="Interface\ChatFrame\UI-ChatInputBorder-Left">
+							<Size>
+								<AbsDimension x="128" y="32"/>
+							</Size>
+							<Anchors>
+								<Anchor point="LEFT">
+									<Offset>
+										<AbsDimension x="-10" y="0"/>
+									</Offset>
+								</Anchor>
+							</Anchors>
+							<TexCoords left="0" right="0.5" top="0" bottom="1.0"/>
+						</Texture>
+						<Texture file="Interface\ChatFrame\UI-ChatInputBorder-Right">
+							<Size>
+								<AbsDimension x="128" y="32"/>
+							</Size>
+							<Anchors>
+								<Anchor point="RIGHT">
+									<Offset>
+										<AbsDimension x="10" y="0"/>
+									</Offset>
+								</Anchor>
+							</Anchors>
+							<TexCoords left="0.5" right="1.0" top="0" bottom="1.0"/>
+						</Texture>
+					</Layer>
+				</Layers>
+				<Scripts>
+					<OnLoad>
+						self:SetTextInsets(5, 5, 0, 0)
+					</OnLoad>
+					<OnTextChanged>
+						if self:GetText() == "" then
+							self:GetParent().createButton:Disable()
+						else
+							self:GetParent().createButton:Enable()
+						end
+					</OnTextChanged>
+				</Scripts>
+			</EditBox>
+			<Button name="$parentCreateButton" parentKey="createButton" inherits="UIPanelButtonTemplate" text="Create">
+				<Size>
+					<AbsDimension x="128" y="20"/>
+				</Size>
+				<Anchors>
+					<Anchor point="BOTTOMRIGHT" relativePoint="BOTTOM">
+						<Offset>
+							<AbsDimension x="-6" y="16"/>
+						</Offset>
+					</Anchor>
+				</Anchors>
+				<Scripts>
+					<OnClick>
+						local class = UIDropDownMenu_GetSelectedValue(self:GetParent().dropdown)
+						local name = self:GetParent().editBox:GetText()
+						if ww_vars.weightsList[class][name] then
+							local error = StaticPopup_Show("WW_WEIGHT_EXISTS", classNames[class], name)
+						else
+							setWeight(class, name, {})
+							self:GetParent():Hide()
+						end
+					</OnClick>
+				</Scripts>
+			</Button>
+			<Button name="$parentCancelButton" parentKey="cancelButton" inherits="UIPanelButtonTemplate" text="Cancel">
+				<Size>
+					<AbsDimension x="128" y="20"/>
+				</Size>
+				<Anchors>
+					<Anchor point="BOTTOMLEFT" relativePoint="BOTTOM">
+						<Offset>
+							<AbsDimension x="6" y="16"/>
+						</Offset>
+					</Anchor>
+				</Anchors>
+				<Scripts>
+					<OnClick>
+						self:GetParent():Hide()
 					</OnClick>
 				</Scripts>
 			</Button>
diff --git a/config.lua b/config.lua
index dd930b5..9b78534 100644
--- a/config.lua
+++ b/config.lua
@@ -21,6 +21,16 @@ StaticPopupDialogs["WW_CONFIRM_WEIGHT_DELETE"] = {
 	hideOnEscape = true,
 }

+StaticPopupDialogs["WW_WEIGHT_EXISTS"] = {
+	text = "The %s weight named \"%s\" already exists.  Pick a different name.",
+	button1 = "Okay",
+	enterClicksFirstButton = true,
+	showAlert = true,
+	timeout = 0,
+	whileDead = true,
+	hideOnEscape = true,
+}
+
 function commandHandler(msg)
 	open_config()
 end
@@ -170,6 +180,54 @@ function deleteWeight(weight)
 	ww_config.leftPanel.scrollFrame:GetScript("OnShow")(ww_config.leftPanel.scrollFrame)
 end

+function configNewWeight(class, weight)
+	if class then
+		UIDropDownMenu_SetSelectedValue(ww_newWeight.dropdown, class, false)
+	end
+	ww_newWeight.editBox:SetText("")
+	if weight then
+		ww_newWeight.editBox:SetText(weight)
+	end
+	ww_newWeight:Show()
+end
+
+function setWeight(class, weight, statList)
+	local weightFrame, position
+
+	if not ww_vars.weightsList[class][weight] then
+		for _, classFrame in ipairs(ww_classFrameTable) do
+			if classFrame.class == class then
+				position = classFrame.length
+				weightFrame = CreateFrame("Frame", weight, classFrame, "ww_elementFrame")
+				weightFrame.position = position
+				weightFrame.category = classFrame
+				weightFrame.text:SetText(weight)
+				weightFrame.name = weight
+				weightFrame:SetPoint("TOPLEFT", 0, -20 * position)
+				classFrame.length = classFrame.length + 1
+				weightFrame.text:SetScript("OnClick",
+					function(self)
+						configSelectWeight(weightFrame)
+					end)
+				if classFrame.collapsed then
+					weightFrame:Hide()
+				else
+					classFrame:SetHeight(20 * classFrame.length)
+					table.insert(ww_weightFrameTable, classFrame.position + position, weightFrame)
+					for _, class in ipairs(ww_classFrameTable) do
+						if class.position > classFrame.position then
+							class.position = class.position + 1
+						end
+					end
+				end
+				break
+			end
+		end
+		ww_config.leftPanel.scrollFrame:GetScript("OnShow")(ww_config.leftPanel.scrollFrame)
+	end
+	ww_vars.weightsList[class][weight] = deepTableCopy(statList)
+end
+
 --loads the various class buttons onto the config frame
 function loadClassButtons()
 	local classes, revClassLookup = {}, {}
@@ -303,6 +361,29 @@ function toggleCollapse(categoryFrame, categoryTable, elementTable, elementHeigh
 	scrollBarUpdateFunction()
 end

+function ClassDropDownInitialize(dropdown)
+	local info = {}
+
+	info.func = DropDownOnClick
+	info.arg1 = dropdown
+	for class, name in pairs(classNames) do
+		info.text = name
+		info.value = class
+		info.checked = nil
+		UIDropDownMenu_AddButton(info)
+	end
+end
+
+function ClassDropDownOnShow(dropdown)
+	local _, class = UnitClass("player")
+	UIDropDownMenu_Initialize(dropdown, ClassDropDownInitialize);
+	UIDropDownMenu_SetSelectedValue(dropdown, class)
+end
+
+function DropDownOnClick(choice, dropdown)
+	UIDropDownMenu_SetSelectedValue(dropdown, choice.value, false)
+end
+
 trackedStats = {
 	["General"] = {
 		"Stamina",