├── LICENSE ├── README.md └── multicolortext.lua /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 EmperorSuper 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MultiColorText 2 | 3 | 4 | ## Example: 5 | ```lua 6 | hook.Add("HUDPaint", "HUDPaint_MultiColorText_Example", function() 7 | draw.MultiColorText("Default", ScrW()/2, ScrH()/2, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, Color(255, 0, 0), "This text ", Color(0, 255, 0), "has multiple ", Color(0, 0, 255), "different colors") 8 | end) 9 | ``` 10 | 11 | 12 | 13 | ## API Context: 14 | **Font**: A font created with [surface.CreateFont](https://wiki.facepunch.com/gmod/surface.CreateFont). 15 | 16 | **x**: The X coordinate on the screen. 17 | 18 | **y**: The Y coordinate on the screen. 19 | 20 | **xAlign**: The alignment of the X coordinate using [Enums/TEXT_ALIGN](https://wiki.facepunch.com/gmod/Enums/TEXT_ALIGN). 21 | 22 | **yAlign**: The alignment of the Y coordinate using [Enums/TEXT_ALIGN](https://wiki.facepunch.com/gmod/Enums/TEXT_ALIGN). 23 | 24 | **vararg**: A [vararg](https://wiki.facepunch.com/gmod/vararg) of colors and strings. Any non string value will be converted to a string with [tostring()](https://wiki.facepunch.com/gmod/Global.tostring). 25 | 26 | ``` 27 | draw.MultiColorText(Font, x, y, xAlign, yAlign, vararg) 28 | ``` 29 | -------------------------------------------------------------------------------- /multicolortext.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | MIT License 3 | 4 | Copyright (c) 2020 EmperorSuper 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | ]]-- 24 | 25 | local Color_White = Color(255, 255, 255) 26 | function draw.MultiColorText(Font, x, y, xAlign, yAlign, ...) 27 | surface.SetFont(Font) 28 | local CurX = x 29 | local CurColor = nil 30 | local AllText = "" 31 | for k, v in pairs{...} do 32 | if not IsColor(v) then 33 | AllText = AllText .. tostring(v) 34 | end 35 | end 36 | local w, h = surface.GetTextSize(AllText) 37 | if xAlign == TEXT_ALIGN_CENTER then 38 | CurX = x - w/2 39 | elseif xAlign == TEXT_ALIGN_RIGHT then 40 | CurX = x - w 41 | end 42 | 43 | if yAlign == TEXT_ALIGN_CENTER then 44 | y = y - h/2 45 | elseif yAlign == TEXT_ALIGN_BOTTOM then 46 | y = y - h 47 | end 48 | 49 | for k, v in pairs{...} do 50 | if IsColor(v) then 51 | CurColor = v 52 | continue 53 | elseif CurColor == nil then 54 | CurColor = Color_White 55 | end 56 | local Text = tostring(v) 57 | surface.SetTextColor(CurColor) 58 | surface.SetTextPos(CurX, y) 59 | surface.DrawText(Text) 60 | CurX = CurX + surface.GetTextSize(Text) 61 | end 62 | end 63 | --------------------------------------------------------------------------------