- Joined
- Nov 30, 2007
- Messages
- 1,202
Suggestions? Could use some help on what to do on Deindex/death.
JASS:
library NameTags
globals
private hashtable hash = InitHashtable()
private constant real INTERVAL = 0.06
private constant real TAG_SIZE = 0.020
endglobals
struct NameTag
thistype next
thistype prev
unit u
integer id
real x
real y
texttag tt
private static integer n = 0
private static trigger t = CreateTrigger()
static method getMember takes integer id returns integer
return LoadInteger(hash, id, 0)
endmethod
static method isUnitTagged takes unit u returns boolean
return thistype.getMember(GetHandleId(u)) != null
endmethod
static method isIdTagged takes integer id returns boolean
return thistype.getMember(id) != null
endmethod
static method rem takes unit u, integer id returns nothing
local thistype a = LoadInteger(hash, GetHandleId(u), 0)
local thistype b = LoadInteger(hash, id, 0)
local thistype this
if a != b and not (a == null) and not (b == null) then
call BJDebugMsg("ERROR - NameTag: tried to remove two elements at the same time!")
return
endif
if a != null then
set this = a
else
set this = b
endif
call FlushChildHashtable(hash, GetHandleId(this.u))
call FlushChildHashtable(hash, this.id)
set this.next.prev = this.prev
set this.prev.next = this.next
set thistype.n = thistype.n - 1
call SetTextTagVisibility(this.tt, false)
call DestroyTextTag(this.tt)
if thistype.n == 0 then
call DisableTrigger(thistype.t)
endif
call this.deallocate()
endmethod
static method add takes unit u, integer id, string name returns nothing
local thistype this
local integer uid = GetHandleId(u)
if LoadInteger(hash, id, 0) != null or LoadInteger(hash, uid, 0) != null then
call thistype.rem(u, id)
endif
set this = thistype.allocate()
call SaveInteger(hash, uid, 0, this)
call SaveInteger(hash, id, 0, this)
set this.u = u
set this.id = id
set this.x = this.x - 1
set this.prev = thistype(0)
set this.next = thistype(0).next
set this.next.prev = this
set thistype(0).next = this
set thistype.n = thistype.n + 1
set this.tt = CreateTextTag()
call SetTextTagText(this.tt, name, TAG_SIZE)
call SetTextTagVisibility(this.tt, true)
if thistype.n == 1 then
call EnableTrigger(thistype.t)
endif
endmethod
private static method periodic takes nothing returns nothing
local thistype index = thistype(0).next
local real uX
local real uY
loop
exitwhen index.u == null
set uX = GetUnitX(index.u)
set uY = GetUnitY(index.u)
if not (index.x == uX and index.y == uY) then
set index.x = uX
set index.y = uY
call SetTextTagPos(index.tt, uX, uY, 220)
endif
set index = index.next
endloop
endmethod
private static method onInit takes nothing returns nothing
call TriggerRegisterTimerEvent(thistype.t, INTERVAL, true)
call TriggerAddAction(thistype.t, function thistype.periodic)
call DisableTrigger(thistype.t)
endmethod
endstruct
endlibrary
Last edited: