📄 gui_hilight_unit.lua
字号:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------- 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 = false -- loaded by default? }end------------------------------------------------------------------------------------------------------------------------------------------------------------------ Automatically generated local definitionslocal GL_BACK = GL.BACKlocal GL_EYE_LINEAR = GL.EYE_LINEARlocal GL_EYE_PLANE = GL.EYE_PLANElocal GL_FILL = GL.FILLlocal GL_FRONT = GL.FRONTlocal GL_FRONT_AND_BACK = GL.FRONT_AND_BACKlocal GL_INVERT = GL.INVERTlocal GL_LINE = GL.LINElocal GL_ONE = GL.ONElocal GL_ONE_MINUS_SRC_ALPHA = GL.ONE_MINUS_SRC_ALPHAlocal GL_POINT = GL.POINTlocal GL_QUAD_STRIP = GL.QUAD_STRIPlocal GL_SRC_ALPHA = GL.SRC_ALPHAlocal GL_T = GL.Tlocal GL_TEXTURE_GEN_MODE = GL.TEXTURE_GEN_MODElocal GL_TRIANGLE_FAN = GL.TRIANGLE_FANlocal glBeginEnd = gl.BeginEndlocal glBlending = gl.Blendinglocal glCallList = gl.CallListlocal glColor = gl.Colorlocal glCreateList = gl.CreateListlocal glCulling = gl.Cullinglocal glDeleteList = gl.DeleteListlocal glDeleteTexture = gl.DeleteTexturelocal glDepthTest = gl.DepthTestlocal glFeature = gl.Featurelocal glGetTextWidth = gl.GetTextWidthlocal glLineWidth = gl.LineWidthlocal glLogicOp = gl.LogicOplocal glPointSize = gl.PointSizelocal glPolygonMode = gl.PolygonModelocal glPolygonOffset = gl.PolygonOffsetlocal glPopMatrix = gl.PopMatrixlocal glPushMatrix = gl.PushMatrixlocal glScale = gl.Scalelocal glSmoothing = gl.Smoothinglocal glTexCoord = gl.TexCoordlocal glTexGen = gl.TexGenlocal glText = gl.Textlocal glTexture = gl.Texturelocal glTranslate = gl.Translatelocal glUnit = gl.Unitlocal glVertex = gl.Vertexlocal spDrawUnitCommands = Spring.DrawUnitCommandslocal spGetFeatureAllyTeam = Spring.GetFeatureAllyTeamlocal spGetFeatureDefID = Spring.GetFeatureDefIDlocal spGetFeaturePosition = Spring.GetFeaturePositionlocal spGetFeatureRadius = Spring.GetFeatureRadiuslocal spGetFeatureTeam = Spring.GetFeatureTeamlocal spGetModKeyState = Spring.GetModKeyStatelocal spGetMouseState = Spring.GetMouseStatelocal spGetMyAllyTeamID = Spring.GetMyAllyTeamIDlocal spGetMyPlayerID = Spring.GetMyPlayerIDlocal spGetMyTeamID = Spring.GetMyTeamIDlocal spGetPlayerControlledUnit = Spring.GetPlayerControlledUnitlocal spGetPlayerInfo = Spring.GetPlayerInfolocal spGetTeamColor = Spring.GetTeamColorlocal spGetTeamInfo = Spring.GetTeamInfolocal spGetUnitAllyTeam = Spring.GetUnitAllyTeamlocal spGetUnitDefID = Spring.GetUnitDefIDlocal spGetUnitIsCloaked = Spring.GetUnitIsCloakedlocal spGetUnitTeam = Spring.GetUnitTeamlocal spIsCheatingEnabled = Spring.IsCheatingEnabledlocal spTraceScreenRay = Spring.TraceScreenRay----------------------------------------------------------------------------------------------------------------------------------------------------------------include("colors.h.lua")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 = 64local cylList = 0local outlineWidth = 3local vsx, vsy = widgetHandler:GetViewSizes()function widget:ViewResize(viewSizeX, viewSizeY) vsx = viewSizeX vsy = viewSizeYendlocal smoothPolys = (glSmoothing ~= nil) and false----------------------------------------------------------------------------------------------------------------------------------------------------------------function widget:Initialize() cylList = glCreateList(DrawCylinder, cylDivs)endfunction 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 endendlocal 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 endendlocal function UnitDrawFunc(unitID) glUnit(unitID, true)endlocal function FeatureDrawFunc(featureID) glFeature(featureID, true)endlocal function HilightUnit(unitID) local outline = (spGetUnitIsCloaked(unitID) ~= true) SetUnitColor(unitID, outline and 0.5 or 0.25) HilightModel(UnitDrawFunc, unitID, outline)endlocal function HilightFeatureModel(featureID) SetFeatureColor(featureID, 0.5) HilightModel(FeatureDrawFunc, featureID, true)endlocal 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 -- 0.75b2 compatibility if (not spGetFeatureRadius) then 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 = spGetPlayerControlledUnitlocal GetMyPlayerID = spGetMyPlayerIDlocal TraceScreenRay = spTraceScreenRaylocal GetMouseState = spGetMouseStatelocal GetUnitDefID = spGetUnitDefIDlocal GetFeatureDefID = spGetFeatureDefID----------------------------------------------------------------------------------------------------------------------------------------------------------------local type, data -- for the TraceScreenRay() callfunction widget:Update() local mx, my = GetMouseState() type, data = TraceScreenRay(mx, my)endfunction 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 endendwidget.DrawWorldReflection = widget.DrawWorldwidget.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 nameend----------------------------------------------------------------------------------------------------------------------------------------------------------------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 endend----------------------------------------------------------------------------------------------------------------------------------------------------------------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 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 endend----------------------------------------------------------------------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -