TextTag

Scripts

14 years
edited 8 years
Code (jass) Select
1
2
//==============================================================================
3
//  TEXT TAG - Floating text system by Cohadar - v5.0
4
//==============================================================================
5
//
6
//  PURPOUSE:
7
//       * Displaying floating text - the easy way
8
//       * Has a set of useful and commonly needed texttag functions
9
//  
10
//  CREDITS:
11
//       * DioD - for extracting proper color, fadepoint and lifespan parameters
12
//         for default warcraft texttags (from miscdata.txt)
13
//
14
//  HOW TO IMPORT:
15
//       * Just create a trigger named TextTag
16
//         convert it to text and replace the whole trigger text with this one
17
//==============================================================================
18
19
library TextTag
20
21
globals    
22
    // for custom centered texttags
23
    private constant real MEAN_CHAR_WIDTH = 5.5
24
    private constant real MAX_TEXT_SHIFT = 200.0
25
    private constant real DEFAULT_HEIGHT = 16.0
26
27
    // for default texttags
28
    private constant real   SIGN_SHIFT = 16.0
29
    private constant real   FONT_SIZE = 0.024
30
    private constant string MISS = "miss"
31
endglobals
32
33
//===========================================================================
34
//   Custom centered texttag on (x,y) position
35
//   color is in default wc3 format, for example "|cFFFFCC00"
36
//===========================================================================
37
public function XY takes real x, real y, string text, string color returns nothing
38
    local texttag tt = CreateTextTag()
39
    local real shift = RMinBJ(StringLength(text)*MEAN_CHAR_WIDTH, MAX_TEXT_SHIFT)
40
    call SetTextTagText(tt, color+text, FONT_SIZE)
41
    call SetTextTagPos(tt, x-shift, y, DEFAULT_HEIGHT)
42
    call SetTextTagVelocity(tt, 0.0, 0.04)
43
    call SetTextTagVisibility(tt, true)
44
    call SetTextTagFadepoint(tt, 2.5)
45
    call SetTextTagLifespan(tt, 4.0)
46
    call SetTextTagPermanent(tt, false)
47
    set tt = null
48
endfunction
49
50
//===========================================================================
51
//   Custom centered texttag above unit
52
//===========================================================================
53
public function Unit takes unit whichUnit, string text, string color returns nothing
54
    local texttag tt = CreateTextTag()
55
    local real shift = RMinBJ(StringLength(text)*MEAN_CHAR_WIDTH, MAX_TEXT_SHIFT)
56
    call SetTextTagText(tt, color+text, FONT_SIZE)
57
    call SetTextTagPos(tt, GetUnitX(whichUnit)-shift, GetUnitY(whichUnit), DEFAULT_HEIGHT)
58
    call SetTextTagVelocity(tt, 0.0, 0.04)
59
    call SetTextTagVisibility(tt, true)
60
    call SetTextTagFadepoint(tt, 2.5)
61
    call SetTextTagLifespan(tt, 4.0)
62
    call SetTextTagPermanent(tt, false)    
63
    set tt = null
64
endfunction
65
66
//===========================================================================
67
//  Standard wc3 gold bounty texttag, displayed only to killing player 
68
//===========================================================================
69
public function GoldBounty takes unit whichUnit, integer bounty, player killer returns nothing
70
    local texttag tt = CreateTextTag()
71
    local string text = "+" + I2S(bounty)
72
    call SetTextTagText(tt, text, FONT_SIZE)
73
    call SetTextTagPos(tt, GetUnitX(whichUnit)-SIGN_SHIFT, GetUnitY(whichUnit), 0.0)
74
    call SetTextTagColor(tt, 255, 220, 0, 255)
75
    call SetTextTagVelocity(tt, 0.0, 0.03)
76
    call SetTextTagVisibility(tt, GetLocalPlayer()==killer)
77
    call SetTextTagFadepoint(tt, 2.0)
78
    call SetTextTagLifespan(tt, 3.0)
79
    call SetTextTagPermanent(tt, false)
80
    set text = null
81
    set tt = null
82
endfunction
83
84
//==============================================================================
85
public function LumberBounty takes unit whichUnit, integer bounty, player killer returns nothing
86
    local texttag tt = CreateTextTag()
87
    local string text = "+" + I2S(bounty)
88
    call SetTextTagText(tt, text, FONT_SIZE)
89
    call SetTextTagPos(tt, GetUnitX(whichUnit)-SIGN_SHIFT, GetUnitY(whichUnit), 0.0)
90
    call SetTextTagColor(tt, 0, 200, 80, 255)
91
    call SetTextTagVelocity(tt, 0.0, 0.03)
92
    call SetTextTagVisibility(tt, GetLocalPlayer()==killer)
93
    call SetTextTagFadepoint(tt, 2.0)
94
    call SetTextTagLifespan(tt, 3.0)
95
    call SetTextTagPermanent(tt, false)
96
    set text = null
97
    set tt = null
98
endfunction
99
100
//===========================================================================
101
public function ManaBurn takes unit whichUnit, integer dmg returns nothing
102
    local texttag tt = CreateTextTag()
103
    local string text = "-" + I2S(dmg)
104
    call SetTextTagText(tt, text, FONT_SIZE)
105
    call SetTextTagPos(tt, GetUnitX(whichUnit)-SIGN_SHIFT, GetUnitY(whichUnit), 0.0)
106
    call SetTextTagColor(tt, 82, 82 ,255 ,255)
107
    call SetTextTagVelocity(tt, 0.0, 0.04)
108
    call SetTextTagVisibility(tt, true)
109
    call SetTextTagFadepoint(tt, 2.0)
110
    call SetTextTagLifespan(tt, 5.0)
111
    call SetTextTagPermanent(tt, false)    
112
    set text = null
113
    set tt = null
114
endfunction
115
116
//===========================================================================
117
public function Miss takes unit whichUnit returns nothing
118
    local texttag tt = CreateTextTag()
119
    call SetTextTagText(tt, MISS, FONT_SIZE)
120
    call SetTextTagPos(tt, GetUnitX(whichUnit), GetUnitY(whichUnit), 0.0)
121
    call SetTextTagColor(tt, 255, 0, 0, 255)
122
    call SetTextTagVelocity(tt, 0.0, 0.03)
123
    call SetTextTagVisibility(tt, true)
124
    call SetTextTagFadepoint(tt, 1.0)
125
    call SetTextTagLifespan(tt, 3.0)
126
    call SetTextTagPermanent(tt, false)
127
    set tt = null
128
endfunction
129
130
//===========================================================================
131
public function CriticalStrike takes unit whichUnit, integer dmg returns nothing
132
    local texttag tt = CreateTextTag()
133
    local string text = I2S(dmg) + "!"
134
    call SetTextTagText(tt, text, FONT_SIZE)
135
    call SetTextTagPos(tt, GetUnitX(whichUnit), GetUnitY(whichUnit), 0.0)
136
    call SetTextTagColor(tt, 255, 0, 0, 255)
137
    call SetTextTagVelocity(tt, 0.0, 0.04)
138
    call SetTextTagVisibility(tt, true)
139
    call SetTextTagFadepoint(tt, 2.0)
140
    call SetTextTagLifespan(tt, 5.0)
141
    call SetTextTagPermanent(tt, false)
142
    set text = null
143
    set tt = null    
144
endfunction
145
146
//===========================================================================
147
public function ShadowStrike takes unit whichUnit, integer dmg, boolean initialDamage returns nothing
148
    local texttag tt = CreateTextTag()
149
    local string text = I2S(dmg)
150
    if initialDamage then
151
        set text = text + "!"
152
    endif
153
    call SetTextTagText(tt, text, FONT_SIZE)
154
    call SetTextTagPos(tt, GetUnitX(whichUnit), GetUnitY(whichUnit), 0.0)
155
    call SetTextTagColor(tt, 160, 255, 0, 255)
156
    call SetTextTagVelocity(tt, 0.0, 0.04)
157
    call SetTextTagVisibility(tt, true)
158
    call SetTextTagFadepoint(tt, 2.0)
159
    call SetTextTagLifespan(tt, 5.0)
160
    call SetTextTagPermanent(tt, false)    
161
    set text = null
162
    set tt = null
163
endfunction
164
165
166
endlibrary
167

In the demo map you will find a set of abilities, items and dummy units that prove that some functions in this library create default wc3 texttags.

Spoiler

A word of advice for system-maker-wannabies:
The secret of making good libraries is in having patience.
There is nothing more useless than random pieces of code,
and nothing more useful than a properly coded and tested set of functions that behave exactly as everyone expects them to behave.

I also recommend reading this:
http://www.canonical.org/~kragen/tao-of-programming.html
  • TextTag_v5.0.w3x (16.89 KB)