Quantcast

Added filtering to ww_statFrame's editboxes (now allows only numbers)

Kevin Lyles [10-22-09 - 19:58]
Added filtering to ww_statFrame's editboxes (now allows only numbers)
Filename
WeightsWatcher.xml
config.lua
diff --git a/WeightsWatcher.xml b/WeightsWatcher.xml
index 27752f5..376a15c 100644
--- a/WeightsWatcher.xml
+++ b/WeightsWatcher.xml
@@ -90,6 +90,21 @@
 					<OnLoad>
 						self:SetTextInsets(5, 5, 0, 0)
 					</OnLoad>
+					<OnTextChanged>
+						local text = self:GetText()
+						if self:GetNumber() ~= 0 or text:match("^[0.]+$") or text == "" then
+							self.number = text
+						end
+					</OnTextChanged>
+					<OnChar>
+						if validateNumber(text, self:GetText()) then
+							self.number = self:GetText()
+						else
+							local cursorPosition = self:GetCursorPosition() - 1
+							self:SetText(self.number)
+							self:SetCursorPosition(cursorPosition)
+						end
+					</OnChar>
 				</Scripts>
 			</EditBox>
 		</Frames>
diff --git a/config.lua b/config.lua
index 4b87262..bbc4cd9 100644
--- a/config.lua
+++ b/config.lua
@@ -27,6 +27,23 @@ function open_config()
 	end
 end

+function validateNumber(newChar, newText)
+	if string.find(newChar, "^[0-9]$") then
+		return true
+	elseif newChar == '.' then
+		local first = newText:find(".", 1, true)
+		local second = newText:find(".", first + 1, true)
+		if not second then
+			return true
+		end
+	elseif newChar == '-' then
+		if not string.find(newText, "-", 2) then
+			return true
+		end
+	end
+	return false
+end
+
 function scrollBarUpdate(scrollFrame, scrolledFrame, buttonTable, buttonHeight, initialOffset, numShown)
 	local i
 	local offset = FauxScrollFrame_GetOffset(scrollFrame)