File: gui_hilight_unit.lua

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (529 lines) | stat: -rw-r--r-- 15,347 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    gui_highlight_unit.lua
--  brief:   highlights the unit/feature under the cursor
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:GetInfo()
  local grey   = "\255\192\192\192"
  local yellow = "\255\255\255\128"
  return {
    name      = "HighlightUnit",
    desc      = "Highlights the unit or feature under the cursor\n"..
                grey.."Hold "..yellow.."META"..grey..
                " to show the unit or feature name",
    author    = "trepan",
    date      = "Apr 16, 2007",
    license   = "GNU GPL, v2 or later",
    layer     = 5,
    enabled   = true  --  loaded by default?
  }
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

-- Automatically generated local definitions

local GL_BACK                   = GL.BACK
local GL_EYE_LINEAR             = GL.EYE_LINEAR
local GL_EYE_PLANE              = GL.EYE_PLANE
local GL_FILL                   = GL.FILL
local GL_FRONT                  = GL.FRONT
local GL_FRONT_AND_BACK         = GL.FRONT_AND_BACK
local GL_INVERT                 = GL.INVERT
local GL_LINE                   = GL.LINE
local GL_ONE                    = GL.ONE
local GL_ONE_MINUS_SRC_ALPHA    = GL.ONE_MINUS_SRC_ALPHA
local GL_POINT                  = GL.POINT
local GL_QUAD_STRIP             = GL.QUAD_STRIP
local GL_SRC_ALPHA              = GL.SRC_ALPHA
local GL_T                      = GL.T
local GL_TEXTURE_GEN_MODE       = GL.TEXTURE_GEN_MODE
local GL_TRIANGLE_FAN           = GL.TRIANGLE_FAN
local glBeginEnd                = gl.BeginEnd
local glBlending                = gl.Blending
local glCallList                = gl.CallList
local glColor                   = gl.Color
local glCreateList              = gl.CreateList
local glCulling                 = gl.Culling
local glDeleteList              = gl.DeleteList
local glDeleteTexture           = gl.DeleteTexture
local glDepthTest               = gl.DepthTest
local glFeature                 = gl.Feature
local glGetTextWidth            = gl.GetTextWidth
local glLineWidth               = gl.LineWidth
local glLogicOp                 = gl.LogicOp
local glPointSize               = gl.PointSize
local glPolygonMode             = gl.PolygonMode
local glPolygonOffset           = gl.PolygonOffset
local glPopMatrix               = gl.PopMatrix
local glPushMatrix              = gl.PushMatrix
local glScale                   = gl.Scale
local glSmoothing               = gl.Smoothing
local glTexCoord                = gl.TexCoord
local glTexGen                  = gl.TexGen
local glText                    = gl.Text
local glTexture                 = gl.Texture
local glTranslate               = gl.Translate
local glUnit                    = gl.Unit
local glVertex                  = gl.Vertex
local spDrawUnitCommands        = Spring.DrawUnitCommands
local spGetFeatureAllyTeam      = Spring.GetFeatureAllyTeam
local spGetFeatureDefID         = Spring.GetFeatureDefID
local spGetFeaturePosition      = Spring.GetFeaturePosition
local spGetFeatureRadius        = Spring.GetFeatureRadius
local spGetFeatureTeam          = Spring.GetFeatureTeam
local spGetModKeyState          = Spring.GetModKeyState
local spGetMouseState           = Spring.GetMouseState
local spGetMyAllyTeamID         = Spring.GetMyAllyTeamID
local spGetMyPlayerID           = Spring.GetMyPlayerID
local spGetMyTeamID             = Spring.GetMyTeamID
local spGetPlayerControlledUnit = Spring.GetPlayerControlledUnit
local spGetPlayerInfo           = Spring.GetPlayerInfo
local spGetTeamColor            = Spring.GetTeamColor
local spGetTeamInfo             = Spring.GetTeamInfo
local spGetUnitAllyTeam         = Spring.GetUnitAllyTeam
local spGetUnitDefID            = Spring.GetUnitDefID
local spGetUnitIsCloaked        = Spring.GetUnitIsCloaked
local spGetUnitTeam             = Spring.GetUnitTeam
local spIsCheatingEnabled       = Spring.IsCheatingEnabled
local spTraceScreenRay          = Spring.TraceScreenRay


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

include("colors.h.lua")
include("fonts.lua")

local font = 'FreeMonoBold'
local fontSize = 16
local fontName = ':n:'..LUAUI_DIRNAME..'Fonts/'..font..'_'..fontSize


local showName = (1 > 0)

local customTex = LUAUI_DIRNAME .. 'Images/highlight_strip.png'
local texName = LUAUI_DIRNAME .. 'Images/highlight_strip.png'
--local texName = 'bitmaps/laserfalloff.tga'

local cylDivs = 64
local cylList = 0

local outlineWidth = 3

local vsx, vsy = widgetHandler:GetViewSizes()
function widget:ViewResize(viewSizeX, viewSizeY)
  vsx = viewSizeX
  vsy = viewSizeY
end

local smoothPolys = (glSmoothing ~= nil) and false


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:Initialize()
  cylList = glCreateList(DrawCylinder, cylDivs)
end


function widget:Shutdown()
  glDeleteList(cylList)
  glDeleteTexture(customTex)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function DrawCylinder(divs)
  local cos = math.cos
  local sin = math.sin
  local divRads = (2.0 * math.pi) / divs
  -- top
  glBeginEnd(GL_TRIANGLE_FAN, function()
    for i = 1, divs do
      local a = i * divRads
      glVertex(sin(a), 1.0, cos(a))
    end
  end)
  -- bottom
  glBeginEnd(GL_TRIANGLE_FAN, function()
    for i = 1, divs do
      local a = -i * divRads
      glVertex(sin(a), -1.0, cos(a))
    end
  end)
  -- sides
  glBeginEnd(GL_QUAD_STRIP, function()
    for i = 0, divs do
      local a = i * divRads
      glVertex(sin(a),  1.0, cos(a))
      glVertex(sin(a), -1.0, cos(a))
    end
  end)
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local function HilightModel(drawFunc, drawData, outline)
  glDepthTest(true)
  glPolygonOffset(-2, -2)
  glBlending(GL_SRC_ALPHA, GL_ONE)

  if (smoothPolys) then
    glSmoothing(nil, nil, true)
  end

  local scale = 20
  local shift = (2 * widgetHandler:GetHourTimer()) % scale
  glTexCoord(0, 0)
  glTexGen(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR)
  glTexGen(GL_T, GL_EYE_PLANE, 0, (1 / scale), 0, shift)
  glTexture(texName)

  drawFunc(drawData)

  glTexture(false)
  glTexGen(GL_T, false)

  -- more edge highlighting
  if (outline) then
    glLineWidth(outlineWidth)
    glPointSize(outlineWidth)
    glPolygonOffset(10, 100)
    glPolygonMode(GL_FRONT_AND_BACK, GL_POINT)
    drawFunc(drawData)
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
    drawFunc(drawData)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    glPointSize(1)
    glLineWidth(1)
  end

  if (smoothPolys) then
    glSmoothing(nil, nil, false)
  end

  glBlending(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  glPolygonOffset(false)
  glDepthTest(false)
end


--------------------------------------------------------------------------------

local function SetUnitColor(unitID, alpha)
  local teamID = spGetUnitTeam(unitID)
  if (teamID == nil) then
    glColor(1.0, 0.0, 0.0, alpha) -- red
  elseif (teamID == spGetMyTeamID()) then
    glColor(0.0, 1.0, 1.0, alpha) -- cyan
  elseif (spGetUnitAllyTeam(unitID) == spGetMyAllyTeamID()) then
    glColor(0.0, 1.0, 0.0, alpha) -- green
  else
    glColor(1.0, 0.0, 0.0, alpha) -- red
  end
end


local function SetFeatureColor(featureID, alpha)
  glColor(1.0, 0.0, 1.0, alpha) -- purple
  do return end  -- FIXME -- wait for feature team/allyteam resolution

  local allyTeamID = spGetFeatureAllyTeam(featureID)
  if ((allyTeamID == nil) or (allyTeamID < 0)) then
    glColor(1.0, 1.0, 1.0, alpha) -- white
  elseif (allyTeamID == spGetMyAllyTeamID()) then
    glColor(0.0, 1.0, 1.0, alpha) -- cyan
  else
    glColor(1.0, 0.0, 0.0, alpha) -- red
  end
end


local function UnitDrawFunc(unitID)
  glUnit(unitID, true)
end


local function FeatureDrawFunc(featureID)
  glFeature(featureID, true)
end


local function HilightUnit(unitID)
  local outline = (spGetUnitIsCloaked(unitID) ~= true)
  SetUnitColor(unitID, outline and 0.5 or 0.25)
  HilightModel(UnitDrawFunc, unitID, outline)
end


local function HilightFeatureModel(featureID)
  SetFeatureColor(featureID, 0.5)
  HilightModel(FeatureDrawFunc, featureID, true)
end


local function HilightFeature(featureID)
  local fDefID = spGetFeatureDefID(featureID)
  local fd = FeatureDefs[fDefID]
  if (fd == nil) then return end

  if (fd.drawType == 0) then
    HilightFeatureModel(featureID)
    return
  end

  local radius = spGetFeatureRadius(featureID)
  if (radius == nil) then
    return
  end

  local px, py, pz = spGetFeaturePosition(featureID)
  if (px == nil) then return end

  local yScale = 4
  glPushMatrix()
  glTranslate(px, py, pz)
  glScale(radius, yScale * radius, radius)
  -- FIXME: needs an 'inside' check

  glDepthTest(true)
  glLogicOp(GL_INVERT)

  glCulling(GL_FRONT)
  glCallList(cylList)

  glCulling(GL_BACK)
  glCallList(cylList)

  glLogicOp(false)
  glCulling(false)
  glDepthTest(false)

  glPopMatrix()
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local GetPlayerControlledUnit = spGetPlayerControlledUnit
local GetMyPlayerID           = spGetMyPlayerID
local TraceScreenRay          = spTraceScreenRay
local GetMouseState           = spGetMouseState
local GetUnitDefID            = spGetUnitDefID
local GetFeatureDefID         = spGetFeatureDefID


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local type, data  --  for the TraceScreenRay() call


function widget:Update()
  local mx, my = GetMouseState()
  type, data = TraceScreenRay(mx, my)
end


function widget:DrawWorld()
  if (type == 'feature') then
    HilightFeature(data)
  elseif (type == 'unit') then
    local unitID = GetPlayerControlledUnit(GetMyPlayerID())
    if (data ~= unitID) then
      HilightUnit(data)
      -- also draw the unit's command queue
      local a,c,m,s = spGetModKeyState()
      if (m) then
        spDrawUnitCommands(data)
      end
    end
  end
end


widget.DrawWorldReflection = widget.DrawWorld


widget.DrawWorldRefraction = widget.DrawWorld


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local teamNames = {}


local function GetTeamName(teamID)
  local name = teamNames[teamID]
  if (name) then
    return name
  end

  local teamNum, teamLeader = spGetTeamInfo(teamID)
  if (teamLeader == nil) then
    return ''
  end

  name = spGetPlayerInfo(teamLeader)
  teamNames[teamID] = name
  return name
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local teamColorStrs = {}


local function GetTeamColorStr(teamID)
  local colorSet = teamColorStrs[teamID]
  if (colorSet) then
    return colorSet[1], colorSet[2]
  end

  local outlineChar = ''
  local r,g,b = spGetTeamColor(teamID)
  if (r and g and b) then
    local function ColorChar(x)
      local c = math.floor(x * 255)
      c = ((c <= 1) and 1) or ((c >= 255) and 255) or c
      return string.char(c)
    end
    local colorStr
    colorStr = '\255'
    colorStr = colorStr .. ColorChar(r)
    colorStr = colorStr .. ColorChar(g)
    colorStr = colorStr .. ColorChar(b)
    local i = (r * 0.299) + (g * 0.587) + (b * 0.114)
    outlineChar = ((i > 0.25) and 'o') or 'O'
    teamColorStrs[teamID] = { colorStr, outlineChar }
    return colorStr, outlineChar
  end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:DrawScreen()
  local a,c,m,s = spGetModKeyState()
  if (not m) then
    return
  end

  local mx, my = GetMouseState()
  local type, data = TraceScreenRay(mx, my)

  local typeStr = ''
  local teamID = nil

  local cheat  = spIsCheatingEnabled()

  if (type == 'unit') then
    local udid = GetUnitDefID(data)
    if (udid == nil) then return end
    local ud = UnitDefs[udid]
    if (ud == nil) then return end
    typeStr = YellowStr .. ud.humanName -- .. ' ' .. CyanStr .. ud.tooltip
    if (cheat) then
      typeStr = typeStr
                .. ' \255\255\128\255(' .. ud.name
                .. ') \255\255\255\255#' .. data
    end
    teamID = spGetUnitTeam(data)
  elseif (type == 'feature') then
    local fdid = GetFeatureDefID(data)
    if (fdid == nil) then return end
    local fd = FeatureDefs[fdid]
    if (fd == nil) then return end
    typeStr = '\255\255\128\255' .. fd.tooltip
    if (cheat) then
      typeStr = typeStr
                .. ' \255\255\255\1(' .. fd.name
                .. ') \255\255\255\255#' .. data
    end
    teamID = spGetFeatureTeam(data)
  end

  local pName = nil
  local colorStr, outlineChar = nil, nil
  if (teamID) then
    pName = GetTeamName(teamID)
    if (pName) then
      colorStr, outlineChar = GetTeamColorStr(teamID)
      if ((colorStr == nil) or (outlineChar == nil)) then
        pName = nil
      end
    end
  end

  local fh = fontHandler
  if (fh.UseFont(fontName)) then

    local f = fh.GetFontSize() * 0.5
    local gx = 12 -- gap x
    local gy = 8  -- gap y

    local lt = fh.GetTextWidth(typeStr)
    local lp = pName and fh.GetTextWidth(pName) or 0
    local lm = (lt > lp) and lt or lp  --  max len

    pName = pName and (colorStr .. pName)

    if ((mx + lm + gx) < vsx) then
      fh.Draw(typeStr, mx + gx, my + gy)
      if (pName) then
        fh.Draw(pName, mx + gx, my - gy - f)
      end
    else
      fh.DrawRight(typeStr, mx - gx, my + gy)
      if (pName) then
        fh.DrawRight(pName, mx - gx, my - gy - f)
      end
    end
  else
    local f = 14
    local gx = 16
    local gy = 8

    local lt = f * glGetTextWidth(typeStr)
    local lp = pName and (f * glGetTextWidth(pName)) or 0
    local lm = (lt > lp) and lt or lp  --  max len

    pName = pName and (colorStr .. pName)

    if ((mx + lm + gx) < vsx) then
      glText(typeStr, mx + gx, my + gy, f, 'o')
      if (pName) then
        glText(pName, mx + gx, my - gy - f, f, outlineChar)
      end
    else
      glText(typeStr, mx - gx, my + gy, f, 'or')
      if (pName) then
        glText(pName, mx - gx, my - gy - f, f, outlineChar .. 'r')
      end
    end
  end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------