#include "ui/ui_extensions.lua" Teardown = {} Teardown.Button = {} Teardown.Button.TextOverflow = { AsIs = 0, Slide = 1, --not implemented Truncate = 2, Fade = 3, --not implemented Resize = 4, Count = 5 } function Teardown.GenericButton(obj) local btn = Ui.Object:new { id = "genericbutton", w = 100, h = 20, text = "button", textColor = {0.96, 0.96, 0.96, 0.8}, disabledTextColor = {1,1,1, 0.2}, textHoverColor = {0.96, 0.96, 0.48, 0.8}, fontSize = 27, colorFilter = {1, 1, 1, 1}, disabledColorFilter = {1,1,1,0.2}, overflowHandling = Teardown.Button.TextOverflow.Resize, hoveredImageBox = {"ui/common/box-outline-fill-6.png", 6, 6, hexrgb(0xFFFF7F)}, imageBox = {"ui/common/box-outline-fill-6.png", 6, 6}, disabled = false, wasDisabled = false, -- HACK: need refactor this flag onPressedOverride = true, onPressed = function(self) end, setHovered = function(self,hovered) if hovered then self.keepHovered = true self._currentTextColor = self.textHoverColor else self:setUnhovered() end end, setUnhovered = function(self) self.keepHovered = false self._currentImageBox = self.imageBox self._currentTextColor = self.textColor end, onMouseEnter = function(self) if not self.disabled then self._currentHoverColor = 0xFFFFFF self._currentImageBox = self.hoveredImageBox if not self.keepHovered then self._currentTextColor = self.textHoverColor end end end, onMouseLeave = function(self) self._currentImageBox = self.imageBox if not self.keepHovered then self._currentTextColor = self.textColor end if not self.disabled then self._currentHoverColor = 0xFFFF7F end end, disable = function(self) self._currentTextColor = {1,1,0,0.5} self.disabled = true end, enable = function(self) self._currentTextColor = self.textColor self.disabled = false end, onCreate = function(self) if obj.disabled then self:disable() end end, draw = function(self) self._currentHoverColor = self._currentHoverColor or 0xFFFF7F self._currentTextColor = self._currentTextColor or self.textColor self._currentImageBox = self._currentImageBox or self.imageBox if self.disabled then if not self.wasDisabled then self.wasDisabled = true self._currentHoverColor = 0xFFFFFF self:onMouseLeave() end else self._currentHoverColor = 0xFFFF7F self.wasDisabled = false end self.onPressedOverride = not self.textOnly UiPush() self.prevw = self.prevw or self.w self.prevh = self.prevh or self.h local w, h = self.w, self.h UiFont("medium.ttf", self.fontSize) UiButtonHoverColor(hexrgb(self._currentHoverColor)) if self._currentImageBox and not self.textOnly then UiButtonImageBox(unpack(self._currentImageBox)) end UiButtonTextHandling(self.overflowHandling) UiTextUnderline(false) if self.fillColor then UiPush() w = self.imageBox[2] h = self.imageBox[3] UiColor(unpack(self.fillColor)) UiImageBox(string.format("ui/common/box-solid-%d.png", w), self.w, self.h) UiPop() end UiColor(unpack(self._currentTextColor)) if self.disabled then UiDisableInput() UiColorFilter(unpack(self.disabledColorFilter)) end if self.textOnly then h = self.originalH w = Ui.GetTextSize(self.text) UiTranslate(w/2, h/2) UiAlign("center middle") UiTextAlignment("center") UiFrameOccupy(1, h) else w,h = self.originalW,self.originalH end if UiTextButton(self.text, w, h) then self:onPressed() end if not self.disabled and self.button then if InputPressed(self.button) then self:onPressed() end end UiPop() end, getSize = function(self) self.originalH = self.originalH or self.h self.originalW = self.originalW or self.w UiPush() UiTranslateToPositionOnScreen(-1000, -1000) UiIgnoreNavigation() UiDisableInput() UiColorFilter(0,0,0,0) local sx,sy = UiGetScale() UiBeginFrame() self:originalDraw() local w, h = UiEndFrame() w = sx ~= 0 and (w / sx) or self.w h = sy ~= 0 and (h / sy) or self.h UiPop() self.w, self.h = w, h return self.w, self.h end, } return btn:new(obj) end