Thursday, April 12, 2012

1st addon. Critique pls.

[:1]So I read a few tutorials and manuals, looked at and "borrowed" some code, got some tips here, and hacked together my 1st addon. It was a great learning experience, but I will learn a lot more if a few people could critique my work. I am sure I did a few things that don't make sense, and am sure some things are not formatted correctly. Let me know what you think.

ChatBox.toc


Code:
## Interface: 30300
## Author: Cognar (Realm - Detheroc)
## Title: ChatBox
## Version : 1.0
## Notes: Makes the default chatframe larger when you are in a city and smaller when you are in combat.
## OptionalDeps: myAddOns
## SavedVariablesPerCharacter: combatHide, leaveGeneral, city, normal, combat
ChatBox.lua
ChatBox.xml

ChatBox.lua


Code:
-- default variables
combatHide = false
leaveGeneral = false
city = 600
normal = 150
combat = 50
-------------------------------------------------------------------

-- Create the frame to receive events
-- I can probably change this since I am making the frame in XML. I will probably need to update the event triggers though.
local CB = CreateFrame("Frame", "ChatBox", UIParent)
--------------------------------------------------------------------

-- output text to chatframe
function out(text)
DEFAULT_CHAT_FRAME:AddMessage(text)
end

-- set up the UI
function ChatBox_OnLoad(panel)
-- loading message
out("ChatBox loaded.");
-- Set the name for the Category for the Panel
panel.name = "ChatBox"
-- When the player clicks okay, run this function.
panel.okay = function (self) ChatBox_Close(); end;
-- When the player clicks cancel, run this function.
panel.cancel = function (self) ChatBox_CancelOrLoad(); end;
-- Add the panel to the Interface Options
InterfaceOptions_AddCategory(panel);
end
--------------------------------------------------------------------

-- Functions that change the height
function ChatHeight_City()
DEFAULT_CHAT_FRAME:SetHeight(city);
end

function ChatHeight_Normal()
DEFAULT_CHAT_FRAME:SetHeight(normal);
end

function ChatHeight_Combat()
DEFAULT_CHAT_FRAME:SetHeight(combat);
end

--------------------------------------------------------------------
-- Function that checks if checbox1 in the options panel is checked
function ChatBox_OnClick()
if (HideChatCheck:GetChecked()) then
combatHide = true;
out("Hiding chat in combat");
else
combatHide = false;
out("Showing chat in combat");
end
end

-- Function that checks if checbox2 in the options panel is checked
function ChatBox2_OnClick()
if (LeaveGeneralCheck:GetChecked()) then
leaveGeneral = true;
out("Leaving general chat in instances");
else
leaveGeneral = false;
out("Not leaving general chat in instances");
end
end

-- these functions update the slider variables. They can be consolidated into one function, but I don't know how to do that yet.
function sliderUpdate1()
normal = slider1:GetValue();
if (select(2, EnumerateServerChannels())) ~= "Trade" and (UnitAffectingCombat("player") ~= 1) then
ChatHeight_Normal();
end
end

function sliderUpdate2()
city = slider2:GetValue();
if (select(2, EnumerateServerChannels())) == "Trade" and (UnitAffectingCombat("player") ~= 1) then
ChatHeight_City();
end
end

function sliderUpdate3()
combat = slider3:GetValue();
if (UnitAffectingCombat("player") == 1) then
ChatHeight_Combat();
end
end

--------------------------------------------------------------------
-- Function to change states on event triggers
function CB.OnEvent(self, event)
local inInstance, instanceType = IsInInstance()
if event == "PLAYER_REGEN_DISABLED" and (combatHide) then
DEFAULT_CHAT_FRAME:SetHeight(-100); -- this hides the tabs from showing on mouseover. I could probably disable mouseover instead, but don't know how to do this.
ChatFrame1:Hide()
ChatFrame2:Hide()
elseif event == "PLAYER_REGEN_DISABLED" and (not combatHide) then
ChatHeight_Combat()
ChatFrame1:Show()
ChatFrame2:Hide()
elseif event == "PLAYER_REGEN_ENABLED" and (select(2, EnumerateServerChannels())) == "Trade" then
ChatHeight_City()
ChatFrame1:Show()
ChatFrame2:Hide()
else
ChatHeight_Normal()
ChatFrame1:Show()
ChatFrame2:Hide()
end
if (event == "ZONE_CHANGED" or event == "ZONE_CHANGED_INDOORS" or event == "WORLD_MAP_UPDATE") then
if (select(2, EnumerateServerChannels())) == "Trade" then ChatHeight_City()
else ChatHeight_Normal()
end
end
if (event == "ZONE_CHANGED" or event == "ZONE_CHANGED_INDOORS" or event == "WORLD_MAP_UPDATE") then
if (IsInInstance()) and (leaveGeneral) then
LeaveChannelByName("General")
else
JoinChannelByName("General");
ChatFrame_AddChannel(DEFAULT_CHAT_FRAME, "General")
end
end
end

CB:SetScript("OnEvent", CB.OnEvent)
CB:RegisterEvent("PLAYER_REGEN_ENABLED")
CB:RegisterEvent("PLAYER_REGEN_DISABLED")
CB:RegisterEvent("ZONE_CHANGED")
CB:RegisterEvent("ZONE_CHANGED_INDOORS")
CB:RegisterEvent("WORLD_MAP_UPDATE")

ChatBox.xml


Code:
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/">

<Frame name="ChatBox" toplevel="true" parent="UIParent" frameStrata="DIALOG"
hidden="true" enableMouse="true">
<Size><AbsDimension x="260" y="280"/></Size>
<Anchors><Anchor point="CENTER"/></Anchors>

<Layers>
<Layer level="OVERLAY">
<FontString name="FontString1" inherits="GameFontNormal" text="ChatBox - An addon to manage your default chat frame.">
<Size>
<AbsDimension x="195" y="58" />
</Size>
<Anchors>
<Anchor point="Center">
<Offset x="0" y="180" />
</Anchor>
</Anchors>
</FontString>
</Layer>
</Layers>

<Scripts>
<OnLoad> ChatBox_OnLoad(self); </OnLoad>
<OnEvent> test(self); </OnEvent>
<OnShow> HideChatCheck:SetChecked(combatHide); LeaveGeneralCheck:SetChecked(leaveGeneral); slider1:SetValue(normal); slider2:SetValue(city); slider3:SetValue(combat); </OnShow>
</Scripts>
<Frames>

<!-- Checkbox for combat toggle -->
<CheckButton name="HideChatCheck" inherits="UICheckButtonTemplate" text="CheckButton1">
<Size>
<AbsDimension x="32" y="32" />
</Size>
<Anchors>
<Anchor point="CENTER">
<Offset x="-100" y="0" />
</Anchor>
</Anchors>
<Scripts>
<OnLoad>
getglobal(this:GetName().."Text"):SetText("Toggle chat frame in combat.");
this:SetChecked(combatHide);
</OnLoad>
<OnClick> ChatBox_OnClick(); </OnClick>
</Scripts>
</CheckButton>

<!-- Checkbox for general chat toggle -->
<CheckButton name="LeaveGeneralCheck" inherits="UICheckButtonTemplate" text="CheckButton2">
<Size>
<AbsDimension x="32" y="32" />
</Size>
<Anchors>
<Anchor point="CENTER">
<Offset x="-100" y="-30" />
</Anchor>
</Anchors>
<Scripts>
<OnLoad>
getglobal(this:GetName().."Text"):SetText("Toggle general chat in instances.");
this:SetChecked(leaveGeneral);
</OnLoad>
<OnClick> ChatBox2_OnClick(); </OnClick>
</Scripts>
</CheckButton>

<!-- Slider 1 -->
<Slider name="slider1" inherits="OptionsSliderTemplate">
<Size>
<AbsDimension x="220" y="16"/>
</Size>
<Anchors>
<Anchor point="CENTER">
<Offset><AbsDimension x="0" y="120"/></Offset>
</Anchor>
</Anchors>
<Scripts>
<OnLoad>
getglobal(this:GetName().."Text"):SetText("Normal Height");
getglobal(this:GetName().."High"):SetText("600");
getglobal(this:GetName().."Low"):SetText("50");
this:SetMinMaxValues(50,600);
this:SetValueStep(10);
this:SetValue(normal);
</OnLoad>
<OnValueChanged> sliderUpdate1(); </OnValueChanged>
</Scripts>
</Slider>
<!-- Slider 2 -->
<Slider name="slider2" inherits="OptionsSliderTemplate">
<Size>
<AbsDimension x="220" y="16"/>
</Size>
<Anchors>
<Anchor point="CENTER">
<Offset><AbsDimension x="0" y="80"/></Offset>
</Anchor>
</Anchors>
<Scripts>
<OnLoad>
getglobal(this:GetName().."Text"):SetText("City Height");
getglobal(this:GetName().."High"):SetText("600");
getglobal(this:GetName().."Low"):SetText("50");
this:SetMinMaxValues(50,600);
this:SetValueStep(10);
this:SetValue(city);
</OnLoad>
<OnValueChanged>sliderUpdate2(); </OnValueChanged>
</Scripts>
</Slider>
<!-- Slider 3 -->
<Slider name="slider3" inherits="OptionsSliderTemplate">
<Size>
<AbsDimension x="220" y="16"/>
</Size>
<Anchors>
<Anchor point="CENTER">
<Offset><AbsDimension x="0" y="40"/></Offset>
</Anchor>
</Anchors>
<Scripts>
<OnLoad>
getglobal(this:GetName().."Text"):SetText("Combat Height");
getglobal(this:GetName().."High"):SetText("600");
getglobal(this:GetName().."Low"):SetText("50");
this:SetMinMaxValues(50,600);
this:SetValueStep(10);
this:SetValue(combat);
</OnLoad>
<OnValueChanged> sliderUpdate3(); </OnValueChanged>
</Scripts>
</Slider>
</Frames>
</Frame>
</Ui>

Thanks,

Cogs|||By all means, load it up on this site's UI site (or the Curse one, or better yet: both )|||I have uploaded it to this site. It is currently "pending." I will upload it to curse after I get some feedback. Perhaps I will make a few changes.

No comments:

Post a Comment