Thread Tools Display Modes
05-10-09, 03:36 PM   #1
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Calendar and Time Manager

So im having issues creating a button that closes and opens the time manager and the calendar via right and left clicking. Im writing it in LUA. ive tried numerious things including just show/hide the TimeManagerFrame and CalendarFrame as well as ToggleCalendar which i have seen in other peoples addons.

Anyone have any ideas? whats the simplest way to do that?
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-10-09, 04:55 PM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
What, exactly, are your issues?
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
05-10-09, 05:08 PM   #3
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Well haha right now what im trying isnt working at all.

This is just the current variation of what i have been trying.

--
clock:RegisterForClicks("LeftButton");

clock:SetScript("OnEvent", OnClick);

function clock:OnClick()
ToggleTimeManager()
end
--

Ive tried a number of dif ways including if statements and such for hide/show the frame and no go.

Also on my time format im showing the leading 0 on 12 h our format IE it shows me that its 05:06 right now. how do i make it not show that leading zero? i think i saw something somewhere on how to remove trailing and leading numbers but cant find it now.

Also doesn't show AM or PM on my realm time. it reads like this,

--
GameTooltip:AddDoubleLine("Server Time", (hour .. ":" .. minute))
--

Edit - oh i forgot what the problem really is.... haha i really don't know what im doing in the slightest
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]

Last edited by Grimsin : 05-10-09 at 05:11 PM.
  Reply With Quote
05-10-09, 05:47 PM   #4
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Oh my general txt line that i want to remove the leading zero on is this.

text:SetText(date("|c00FFFFFF%I|c00FFFFFF:|c00FFFFFF%M|c00FFFFFF %p"))
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-10-09, 07:08 PM   #5
Exawatt
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Feb 2009
Posts: 36
Originally Posted by Grimsin View Post
Oh my general txt line that i want to remove the leading zero on is this.

text:SetText(date("|c00FFFFFF%I|c00FFFFFF:|c00FFFFFF%M|c00FFFFFF %p"))
Here's an inefficient way to do it:

lua Code:
  1. --get hour (01-12)
  2. local hour = date("%I")
  3.  
  4. --check if first char is a leading zero
  5. if( string.sub(hour, 1, 1) == "0" ) then
  6.     --grab everything but leading zero
  7.     hour = string.sub(hour, 2)
  8. end
  9.  
  10. --set text
  11. text:SetText("|c00FFFFFF" .. hour .. date(":%M %p"))

I'll also add that you don't have to keep setting the same color (as it is in your example), so you don't have to pass it all to date().
  Reply With Quote
05-10-09, 08:26 PM   #6
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
You mean more efficient? That looks like it would be more efficient then anything i had.

I have the same problem with my realm time on the tooltip. not only em i unsure of how to pull the realm's am or pm. I have the leading zero issue there as well. I did that one kind of like you are showing there to do the rest of it. put a local line in the beginning that says this...

local hour,minute = GetGameTime();

Tooltip line reads as follows:

GameTooltip:AddDoubleLine("Server Time", (hour .. ":" .. minute))
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-10-09, 09:02 PM   #7
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Here is my lua in its entirety. What you posted worked perfectly. I would think something like that should be possible for the tool tip server time.

Still can not get the right click/ left click functions to work.... tried numerous ways of doing it. Not sure why it doesn't work unless its just that none of the frame names or commands ive used are legit.
Code:
local Clock = CreateFrame("Button", "clock", UIParent)
local shour,sminute = GetGameTime();
local hour = date("%I")
    
function Clock:new()
        self:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
        self:SetWidth(55)
        self:SetHeight(20)
       
        self:SetMovable(true)
        self:SetUserPlaced(true)
       
        self:SetScript("OnUpdate", self.onUpdate)
        self:SetScript("OnEnter", self.onEnter)
        self:SetScript("OnLeave", function() GameTooltip:Hide() end)
   
        text = self:CreateFontString(nil, "ARTWORK")
        text:SetFont("Fonts\\ARIALN.ttf", 11)
        text:SetShadowOffset( 1, -1)
        text:SetPoint("CENTER", self)
end
   
local lastUpdate = 0
function Clock:onUpdate(elapsed)
        lastUpdate = lastUpdate + elapsed
       
       
        if lastUpdate > 1 then
                lastUpdate = 0
                text:SetText("|c00FFFFFF" .. hour .. date(":%M %p"))
                text:SetFont("Fonts\\ARIALN.ttf", 11)
                text:SetShadowOffset( 1, -1)
        end

        if( string.sub(hour, 1, 1) == "0" ) then
    hour = string.sub(hour, 2)
end        
        end

    -----------------------
    -- alt+click to move --
    -----------------------
   
clock:SetScript("OnMouseDown", function()
                if(IsAltKeyDown()) then
                        clock:ClearAllPoints()
                        clock:StartMoving()
                end
        end)
        clock:SetScript("OnMouseUp", function()
                clock:StopMovingOrSizing()
        end)

   
    ---------------------------
    -- Right and left click functions --
    ---------------------------  
clock:RegisterForClicks("LeftButton");

clock:SetScript("OnEvent", OnClick);

function clock:OnClick()
        ToggleTimeManager()
end  
    -----------------
    -- tooltip --
    -----------------
   
function clock:onEnter()
             
        GameTooltip:SetOwner(self, "ANCHOR_TOPLEFT")
              GameTooltip:AddDoubleLine("Today's Date", date("%A, %B %d, %Y"))
	          GameTooltip:AddDoubleLine("Local Time", "|c00FFFFFF" .. hour .. date(":%M %p"))
	          GameTooltip:AddDoubleLine("Server Time", (shour .. ":" .. sminute))
              GameTooltip:AddLine(" ")
              GameTooltip:AddLine("|cffeda55fLeft-Click|r to show Time Manager", 0.2, 1, 0.2)
              GameTooltip:AddLine("|cffeda55fRight-Click|r to show Calendar", 0.2, 1, 0.2)
    GameTooltip:Show()
end

clock:new()
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-10-09, 10:06 PM   #8
Exawatt
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Feb 2009
Posts: 36
Originally Posted by Grimsin View Post
You mean more efficient? That looks like it would be more efficient then anything i had.

I have the same problem with my realm time on the tooltip. not only em i unsure of how to pull the realm's am or pm. I have the leading zero issue there as well. I did that one kind of like you are showing there to do the rest of it. put a local line in the beginning that says this...

local hour,minute = GetGameTime();

Tooltip line reads as follows:

GameTooltip:AddDoubleLine("Server Time", (hour .. ":" .. minute))
GetGameTime() returns in 24-hour format. So...

Code:
hour, minute = GetGameTime()
if hour >= 12 then
    --PM
else
    --AM
end
  Reply With Quote
05-10-09, 10:45 PM   #9
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Where would i enter that if statement? right after the local shour,sminute = GetGameTime();?

hmm im confuzzled on that one.
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-11-09, 12:10 AM   #10
Exawatt
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Feb 2009
Posts: 36
Originally Posted by Grimsin
You mean more efficient?
I meant inefficient. Every time I think my code is good, I'll read something else on the forum where someone pulls this crazy-fast little-known function out of their hat... So now I just always assume my code is bad, and try to find ways to improve it.

Originally Posted by Grimsin View Post
Where would i enter that if statement? right after the local shour,sminute = GetGameTime();?

hmm im confuzzled on that one.
The hour return value will be between 0 and 23.

The server doesn't need to send you hour, minute, and AM/PM, because AM/PM can be determined based on the hour. Just check to see if the hour is 12 (noon) or higher.

You might also want to look into when the server will send you 0 as the hour (12-1 AM). Just replace it with a 12.

I'm really not that good with clicks and event registering, though. Sorry.
  Reply With Quote
05-11-09, 08:23 AM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Sorry I didn't make it back here, Grimsin. iirc, the Blizzard_TimeManager addon is load on demand, so you may be running your code before the frame actually exists. You'll need to watch for the ADDON_LOADED event and run your code when the time manager exists.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
05-11-09, 09:34 AM   #12
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Seerah, what you said makes a lot of sense but how do i check to see which ones are loading first?

better yet... once i figure out if that is the case how would i go about changing the load order?

That makes a lot of sense though because i have tried cutting and pasting other peoples timemanager and calendar open/close snippets and NONE of them worked.
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-11-09, 12:57 PM   #13
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
Code:
clock:SetScript("OnEvent", OnClick);
Should be
Code:
clock:SetScript("OnClick", OnClick);
  Reply With Quote
05-11-09, 08:50 PM   #14
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Changed that and still no button function. with anyluck seerah will explain to me more about how to check whats loading and when.
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-11-09, 09:57 PM   #15
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
OK! sooo i got the clicking to work... sorta lol. this simple line works when inserted with the rest how ever im not sure now how to get the right and left buttons to separate. everytime i try putting in lines to designate which one does what it goes back to not working.

Code:
clock:SetScript("OnMouseDown", function()
                if(IsAltKeyDown()) then
                        clock:ClearAllPoints()
                        clock:StartMoving()
                end
        end)
        clock:SetScript("OnMouseUp", function()
                clock:StopMovingOrSizing()
        end)  
        clock:SetScript("OnClick", function()
                ToggleTimeManager()
        end)
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-11-09, 10:53 PM   #16
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
I think you really just had too many errors working against you at once. The problem Slakah pointed out with the 'OnEnter' script needing to be an 'OnClick' script was right but the OnClick function doesn't exist in that code you posted. Also, the RegisterForClicks method doesn't want 'LeftButton' it wants 'LeftButtonUp' or 'LeftButtonDown'.

As for the 'OnUpdate' handler, it doesn't need to apply the font and shadowing each second. Also, I would cut it back to maybe every 5 seconds over every second since we're talking about minutes so precision isn't critical. You are updating the text for the local time before you correct for the leading zero so there would be a one second delay on the correction.

I'm not sure if I missed something either, but it seemed to me like you grabbed the hour once at the beginning and weren't updating that value at all. Same issue with the server time I think.

I redid the code into a format I prefer just to make it quicker for me to go over and edit. There may still be a couple of cosmetic issues but here is what I came up with (it worked the one time I loaded it up):

Code:
local date, tonumber, GetGameTime = date, tonumber, GetGameTime

local Clock = CreateFrame('Button', "MyCustomClock", UIParent)
Clock:SetPoint('CENTER', UIParent, 'CENTER', 0, 0)
Clock:SetHeight(20)
Clock:SetWidth(55)
Clock:RegisterForClicks('LeftButtonUp', 'RightButtonUp')
Clock:SetMovable(true)
Clock:SetUserPlaced(true)

local Time = Clock:CreateFontString(nil, 'ARTWORK')
Time:SetFont([[Fonts\ARIALN.ttf]], 11)
Time:SetShadowOffset(1, -1)
Time:SetTextColor(1, 1, 1, 1)
Time:SetPoint('CENTER', Clock)

Clock:SetScript('OnClick', function(self, button)
	if self.moving then
		self:StopMovingOrSizing()
		self.moving = nil
	elseif IsAltKeyDown() then
		return
	elseif button == 'LeftButton' then
		ToggleTimeManager()
	else
		ToggleCalendar()
	end
end)

local server_hour, server_minute
local function OnEnter(self)
	if self.tooltip then
		GameTooltip:ClearLines()
	else
		GameTooltip:SetOwner(self, 'ANCHOR_TOPLEFT')
	end
	GameTooltip:AddDoubleLine("Today's Date", date("%A, %B %d, %Y"))
	GameTooltip:AddDoubleLine("Local Time", Time:GetText())
	GameTooltip:AddDoubleLine("Server Time", ("%d:%.02d %sM"):format(server_hour > 12 and server_hour - 12 or server_hour ~= 0 and server_hour or 12, server_minute, server_hour >= 12 and "P" or "A"))
	GameTooltip:AddLine(" ")
	GameTooltip:AddLine("|cffeda55fLeft Click|r toggles the Time Manager", 0.2, 1, 0.2)
	GameTooltip:AddLine("|cffeda55fRight Click|r toggles the Calendar", 0.2, 1, 0.2)
	if not self.tooltip then
		GameTooltip:Show()
		self.tooltip = true
	end
end
Clock:SetScript('OnEnter', OnEnter)

Clock:SetScript('OnLeave', function(self)
	GameTooltip:Hide()
	self.tooltip = nil
end)

Clock:SetScript('OnMouseDown', function(self, button)
	if button ~= 'LeftButton' or not IsAltKeyDown() then return end
	self:ClearAllPoints()
	self:StartMoving()
	self.moving = true
end)

local previousMinute, timer = -1, -1
Clock:SetScript('OnUpdate', function(self, elapsed)
	timer = timer - elapsed
	if timer > 0 then return end
	timer = 5
	server_hour, server_minute = GetGameTime()
	if server_minute == previousMinute then return end
	previousMinute = server_minute
	Time:SetText(("%d:%s"):format(tonumber(date("%I")), date("%M %p")))
	if self.tooltip then
		OnEnter(self)
	end
end)

Last edited by Vrul : 05-12-09 at 05:45 AM. Reason: Corrected the code
  Reply With Quote
05-11-09, 11:18 PM   #17
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
HAHAHA i knew i should have tried that. I thought about tieing the left/right clicking into that piece for moving it... if i had i think i would have had it sooner.

And your right you achieved the look finally that i was after for the return of the times formats.

I plan on removing the move ability of it for the final product to be inserted in my UI, so my question is this following code change should maintain the right left click and remove the move function correct?

from

Code:
Clock:SetScript('OnClick', function(self, button)
	if self.moving then
		self:StopMovingOrSizing()
		self.moving = nil
	elseif IsAltKeyDown() then
		return
	elseif button == 'LeftButton' then
		ToggleTimeManager()
	else
		ToggleCalendar()
	end
end)
to

Code:
Clock:SetScript('OnClick', function(self, button)
	if button == 'LeftButton' then
		ToggleTimeManager()
	else
		ToggleCalendar()
	end
end)
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-12-09, 12:12 AM   #18
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
So i was just thinking about it and scratch the removal of the move ability. Going to see if i can figure out how to make it default load in the spot i want without removing the move function and without needing a variables file.
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
05-12-09, 05:43 AM   #19
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
You will need to make a change to the OnUpdate handler. Change this line:

Time:SetText(strtrim(date("%I"), "0") .. date(":%M %p"))

to:

Time:SetText(("%d:%s"):format(tonumber(date("%I")), date("%M %p")))


You can also change the first line from:

local date, strtrim, GetGameTime = date, strtrim, GetGameTime

to:

local date, tonumber, GetGameTime = date, tonumber, GetGameTime


If you don't make the change then when the hour is 10 it will show as 1. Oops.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Calendar and Time Manager


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off