|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +---Keyboard button display overlay. |
| 12 | +-- |
| 13 | +-- Overlay type which displays a keyboard key button symbol. |
| 14 | +local ButtonOverlay_mt = Class(ButtonOverlay) |
| 15 | + |
| 16 | + |
| 17 | +--- |
| 18 | +function ButtonOverlay.new(customMt) |
| 19 | + local self = setmetatable({}, customMt or ButtonOverlay_mt) |
| 20 | + |
| 21 | + self.textSizeFactor = 0.55 -- ratio of text size to button height |
| 22 | + self.textYOffsetFactor = 0.17 -- compensates base line offset for size factor 0.5 TODO: get proper vertical alignment from engine in renderText() |
| 23 | + |
| 24 | + self.buttonScaleOverlay = g_overlayManager:createOverlay("gui.button_middle", 0, 0, 0, 0) |
| 25 | + self.buttonLeftOverlay = g_overlayManager:createOverlay("gui.button_left", 0, 0, 0, 0) |
| 26 | + self.buttonRightOverlay = g_overlayManager:createOverlay("gui.button_right", 0, 0, 0, 0) |
| 27 | + self.buttonLeftWidthToHeightRatio = 6 / 25 -- 6px width, 25px height |
| 28 | + self.leftRightPaddingHeightRatio = 12 / 25 |
| 29 | + |
| 30 | + self:setColor(1, 1, 1, 1) |
| 31 | + |
| 32 | + self.minWidth = nil |
| 33 | + |
| 34 | + self.debugEnabled = nil |
| 35 | + |
| 36 | + return self |
| 37 | +end |
| 38 | + |
| 39 | + |
| 40 | +---Delete this button overlay. |
| 41 | +function ButtonOverlay:delete() |
| 42 | + if self.buttonScaleOverlay ~= nil then |
| 43 | + self.buttonScaleOverlay:delete() |
| 44 | + self.buttonScaleOverlay = nil |
| 45 | + end |
| 46 | + if self.buttonLeftOverlay ~= nil then |
| 47 | + self.buttonLeftOverlay:delete() |
| 48 | + self.buttonLeftOverlay = nil |
| 49 | + end |
| 50 | + if self.buttonRightOverlay ~= nil then |
| 51 | + self.buttonRightOverlay:delete() |
| 52 | + self.buttonRightOverlay = nil |
| 53 | + end |
| 54 | +end |
| 55 | + |
| 56 | + |
| 57 | +---Set this overlay's background color. |
| 58 | +-- @param float? r Red channel for text [0, 1] |
| 59 | +-- @param float? g Green channel for text [0, 1] |
| 60 | +-- @param float? b Blue channel for text [0, 1] |
| 61 | +-- @param float? a Alpha (transparency) channel for text [0, 1], 0 is fully transparent, 1 is opaque |
| 62 | +-- @param float? r2 Red channel for background [0, 1], if nil "r" will be used |
| 63 | +-- @param float? g2 Green channel for background [0, 1], if nil "g" will be used |
| 64 | +-- @param float? b2 Blue channel for background [0, 1], if nil "b" will be used |
| 65 | +-- @param float? a2 Alpha (transparency) channel for background [0, 1], 0 is fully transparent, 1 is opaque, if nil "a" will be used |
| 66 | +function ButtonOverlay:setColor(r, g, b, a, r2, g2, b2, a2) |
| 67 | + self.r = r or self.r |
| 68 | + self.g = g or self.g |
| 69 | + self.b = b or self.b |
| 70 | + self.a = a or self.a |
| 71 | + |
| 72 | + self.r2 = r2 or self.r2 or r |
| 73 | + self.g2 = g2 or self.g2 or g |
| 74 | + self.b2 = b2 or self.b2 or b |
| 75 | + self.a2 = a2 or self.a2 or a |
| 76 | + |
| 77 | + if self.buttonScaleOverlay ~= nil then |
| 78 | + self.buttonScaleOverlay:setColor(self.r2, self.g2, self.b2, self.a2) |
| 79 | + end |
| 80 | + if self.buttonLeftOverlay ~= nil then |
| 81 | + self.buttonLeftOverlay:setColor(self.r2, self.g2, self.b2, self.a2) |
| 82 | + end |
| 83 | + if self.buttonRightOverlay ~= nil then |
| 84 | + self.buttonRightOverlay:setColor(self.r2, self.g2, self.b2, self.a2) |
| 85 | + end |
| 86 | +end |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +---Render this overlay with the given parameters. |
| 108 | +-- @param buttonText Text to display as the key value, e.g. "A", "Space", "Ctrl", etc. |
| 109 | +-- @param posX Screen x position |
| 110 | +-- @param posY Screen y position |
| 111 | +-- @param height Button display height |
| 112 | +function ButtonOverlay:renderButton(buttonText, posX, posY, height, colorText, clipX1, clipY1, clipX2, clipY2, customOffsetLeft, customButtonInputText) |
| 113 | + customOffsetLeft = customOffsetLeft or 0 |
| 114 | + buttonText = utf8ToUpper(tostring(buttonText)) |
| 115 | + |
| 116 | + local width, widthNoOffset = self:getButtonWidth(buttonText, height, customOffsetLeft, customButtonInputText) |
| 117 | + self:renderBackground(posX, posY, width, height, clipX1, clipY1, clipX2, clipY2) |
| 118 | + |
| 119 | + local textSize = height * self.textSizeFactor |
| 120 | + setTextBold(true) |
| 121 | + setTextAlignment(RenderText.ALIGN_CENTER) |
| 122 | + setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_MIDDLE) |
| 123 | + if clipX1 ~= nil then |
| 124 | + setTextClipArea(clipX1, clipY1, clipX2, clipY2) |
| 125 | + end |
| 126 | + |
| 127 | + if colorText then |
| 128 | + setTextColor(self.r, self.g, self.b, self.a) |
| 129 | + else |
| 130 | + setTextColor(1, 1, 1, self.a) |
| 131 | + end |
| 132 | + |
| 133 | + local textPosX = self.buttonRightOverlay.x + self.buttonRightOverlay.width - widthNoOffset*0.5-- + customOffsetLeft |
| 134 | + local textPosY = posY + height * 0.5 + textSize * self.textYOffsetFactor |
| 135 | + renderText(textPosX, textPosY, textSize, buttonText)-- + textSize * self.textYOffsetFactor |
| 136 | + |
| 137 | + setTextAlignment(RenderText.ALIGN_LEFT) |
| 138 | + if customButtonInputText ~= nil then |
| 139 | + setTextColor(0.5, 0.5, 0.5, 1) |
| 140 | + renderText(self.buttonScaleOverlay.x, textPosY, textSize, customButtonInputText) |
| 141 | + end |
| 142 | + |
| 143 | + if clipX1 ~= nil then |
| 144 | + setTextClipArea(0, 0, 1, 1) |
| 145 | + end |
| 146 | + setTextBold(false) |
| 147 | + setTextColor(1, 1, 1, 1) |
| 148 | + setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_BASELINE) |
| 149 | + |
| 150 | + local totalWidth = self.buttonRightOverlay.x + self.buttonRightOverlay.width - self.buttonLeftOverlay.x |
| 151 | + |
| 152 | + if self.debugEnabled or g_uiDebugEnabled then |
| 153 | + setOverlayColor(GuiElement.debugOverlay, 1, 0, 1, 0.5) |
| 154 | + |
| 155 | + renderOverlay(GuiElement.debugOverlay, posX - g_pixelSizeX, posY - g_pixelSizeY, totalWidth + 2 * g_pixelSizeX, g_pixelSizeY) |
| 156 | + renderOverlay(GuiElement.debugOverlay, posX - g_pixelSizeX, posY + height, totalWidth + 2 * g_pixelSizeX, g_pixelSizeY) |
| 157 | + renderOverlay(GuiElement.debugOverlay, posX - g_pixelSizeX, posY, g_pixelSizeX, height) |
| 158 | + renderOverlay(GuiElement.debugOverlay, posX + totalWidth, posY, g_pixelSizeX, height) |
| 159 | + end |
| 160 | + |
| 161 | + return totalWidth |
| 162 | +end |
| 163 | + |
| 164 | + |
| 165 | +---Get the total display width of this button overlay for a given button text and height |
| 166 | +function ButtonOverlay:getButtonWidth(buttonText, height, customOffsetLeft, customButtonInputText) |
| 167 | + customOffsetLeft = customOffsetLeft or 0 |
| 168 | + local textSize = height * self.textSizeFactor |
| 169 | + |
| 170 | + setTextBold(true) |
| 171 | + buttonText = utf8ToUpper(tostring(buttonText)) |
| 172 | + local textWidth = getTextWidth(textSize, buttonText) |
| 173 | + local textWidthCustom = 0 |
| 174 | + if customButtonInputText ~= nil then |
| 175 | + textWidthCustom = getTextWidth(textSize, customButtonInputText) |
| 176 | + end |
| 177 | + setTextBold(false) |
| 178 | + |
| 179 | + local leftRightPadding = (height*self.leftRightPaddingHeightRatio) / g_screenAspectRatio |
| 180 | + |
| 181 | + local widthNoOffset = textWidth + 2*leftRightPadding |
| 182 | + local width = widthNoOffset + customOffsetLeft + textWidthCustom |
| 183 | + |
| 184 | + if self.minWidth ~= nil then |
| 185 | + width = math.max(width, self.minWidth) |
| 186 | + widthNoOffset = math.max(widthNoOffset, self.minWidth) |
| 187 | + end |
| 188 | + |
| 189 | + return width, widthNoOffset |
| 190 | +end |
0 commit comments