New elements; Fixes
This commit is contained in:
parent
eaf0a90173
commit
d4dd889bb5
@ -8,37 +8,40 @@ require("/koptilnya/libs/utils.txt")
|
||||
EButton = class("EButton", ELabel)
|
||||
EButton:include(require("radius_mixin.txt"))
|
||||
|
||||
accessorFunc(EButton, "_textColor", "TextColor", Color(230, 230, 230))
|
||||
accessorFunc(EButton, "_hoveredColor", "HoveredColor", Color(66, 66, 66))
|
||||
accessorFunc(EButton, "_hoveredTextColor", "HoveredTextColor", Color(230, 230, 230))
|
||||
accessorFunc(EButton, "_disabledColor", "DisabledColor", Color(32, 32, 32))
|
||||
accessorFunc(EButton, "_disabledTextColor", "DisabledTextColor", Color(80, 80, 80))
|
||||
|
||||
function EButton:initialize()
|
||||
ELabel.initialize(self)
|
||||
|
||||
self:setText("Button")
|
||||
self:setColor(Color(46, 46, 46))
|
||||
self:setSize(100, 32)
|
||||
self:setRoundedCorners(true)
|
||||
self:setRadius(5)
|
||||
self:setColorScheme({
|
||||
bg = {
|
||||
Color(46, 46, 46),
|
||||
hover = Color(66, 66, 66),
|
||||
disabled = Color(32, 32, 32)
|
||||
},
|
||||
text = {
|
||||
Color(230, 230, 230),
|
||||
disabled = Color(80, 80, 80)
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
function EButton:paint()
|
||||
local x, y = self:getAbsolutePos()
|
||||
local w, h = self:getSize()
|
||||
function EButton:onClick()
|
||||
end
|
||||
|
||||
function EButton:onMousePressed(x, y, key, keyName)
|
||||
if keyName == "MOUSE1" then
|
||||
self:onClick()
|
||||
end
|
||||
end
|
||||
|
||||
function EButton:paint(x, y, w, h)
|
||||
local textW, textH = self:getTextSize()
|
||||
local rTL, rTR, rBR, rBL = self:getRoundedCorners()
|
||||
local bgColor, textColor
|
||||
if self:isEnabled() then
|
||||
bgColor = self:isHovered() and self:getHoveredColor() or self:getColor()
|
||||
textColor = self:isHovered() and self:getHoveredTextColor() or self:getTextColor()
|
||||
else
|
||||
bgColor = self:getDisabledColor()
|
||||
textColor = self:getDisabledTextColor()
|
||||
end
|
||||
|
||||
render.setColor(bgColor)
|
||||
render.setColor(self:getColorFromScheme("bg"))
|
||||
if self:getRadius() > 0 and (rTL or rTR or rBR or rBL) then
|
||||
render.drawRoundedBoxEx(self:getRadius(), x, y, w, h, rTL, rTR, rBL, rBR)
|
||||
else
|
||||
@ -46,6 +49,6 @@ function EButton:paint()
|
||||
end
|
||||
|
||||
render.setFont(self:getFont())
|
||||
render.setColor(textColor)
|
||||
render.setColor(self:getColorFromScheme("text"))
|
||||
render.drawSimpleText(x + w / 2 - textW / 2, y + h / 2 - textH / 2, self:getText())
|
||||
end
|
||||
|
||||
@ -1,62 +1,58 @@
|
||||
--@include element.txt
|
||||
--@include label.txt
|
||||
--@include /koptilnya/libs/utils.txt
|
||||
|
||||
require("element.txt")
|
||||
require("label.txt")
|
||||
require("/koptilnya/libs/utils.txt")
|
||||
|
||||
ECheckbox = class("ECheckbox", ELabel)
|
||||
|
||||
accessorFunc(ECheckbox, "_textColor", "TextColor", Color(255, 255, 255))
|
||||
accessorFunc(ECheckbox, "_hoveredColor", "HoveredColor", Color(66, 66, 66))
|
||||
accessorFunc(ECheckbox, "_hoveredTextColor", "HoveredTextColor", Color(255, 255, 255))
|
||||
accessorFunc(ECheckbox, "_disabledColor", "DisabledColor", Color(32, 32, 32))
|
||||
accessorFunc(ECheckbox, "_disabledTextColor", "DisabledTextColor", Color(80, 80, 80))
|
||||
accessorFunc(ECheckbox, "_indent", "Indent", 8)
|
||||
ECheckbox = class("ECheckbox", Element)
|
||||
|
||||
function ECheckbox:initialize()
|
||||
ELabel.initialize(self)
|
||||
Element.initialize(self)
|
||||
|
||||
self._checked = false
|
||||
|
||||
self:setText("Checkbox")
|
||||
self:setSize(200, 16)
|
||||
self:setSize(16, 16)
|
||||
self:setColorScheme({
|
||||
border = {
|
||||
Color(209, 209, 209),
|
||||
hover = Color(255, 255, 255),
|
||||
disabled = Color(130, 130, 130)
|
||||
},
|
||||
mark = {
|
||||
Color(81, 92, 107),
|
||||
disabled = Color(40, 46, 53)
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
function ECheckbox:setChecked(state)
|
||||
self._checked = state
|
||||
|
||||
self:onChange()
|
||||
self:onChange(state)
|
||||
end
|
||||
|
||||
function ECheckbox:isChecked()
|
||||
return self._checked
|
||||
end
|
||||
|
||||
function ECheckbox:paint()
|
||||
local x, y = self:getAbsolutePos()
|
||||
local w, h = self:getSize()
|
||||
local textW, textH = self:getTextSize()
|
||||
--[[
|
||||
local bgColor, textColor
|
||||
if self:isEnabled() then
|
||||
bgColor = self:isHovered() and self:getHoveredColor() or self:getColor()
|
||||
textColor = self:isHovered() and self:getHoveredTextColor() or self:getTextColor()
|
||||
else
|
||||
bgColor = self:getDisabledColor()
|
||||
textColor = self:getDisabledTextColor()
|
||||
function ECheckbox:toggle()
|
||||
self:setChecked(!self:isChecked())
|
||||
end
|
||||
|
||||
function ECheckbox:onMousePressed(x, y, key, keyName)
|
||||
if keyName == "MOUSE1" then
|
||||
self:toggle()
|
||||
end
|
||||
]]
|
||||
end
|
||||
|
||||
function ECheckbox:paint(x, y, w, h)
|
||||
render.setColor(self:isChecked() and self:getColorFromScheme("mark") or Color(0, 0, 0, 0))
|
||||
render.drawRectFast(x, y, h, h)
|
||||
|
||||
render.setColor(Color(209, 209, 209))
|
||||
render.drawCircle(x, y, h / 2)
|
||||
--render.drawRectOutline(x, y, h, h, -8)
|
||||
|
||||
--render.setColor(Color(209, 209, 209, 0))
|
||||
--render.drawRectFast(x, y, h - 2, h - 2)
|
||||
|
||||
render.setFont(self:getFont())
|
||||
render.setColor(Color(255, 255, 255))
|
||||
render.drawSimpleText(x + h + self:getIndent(), y + h / 2 - textH / 2, self:getText())
|
||||
render.setColor(self:getColorFromScheme("border"))
|
||||
render.drawRectOutline(x, y, h, h, 1)
|
||||
render.drawRectOutline(x + 1, y + 1, h - 2, h - 2, 1)
|
||||
end
|
||||
|
||||
function ECheckbox:onChange(state)
|
||||
end
|
||||
@ -14,6 +14,7 @@ function Element:initialize()
|
||||
self._visible = true
|
||||
self._draggable = true
|
||||
self._hovered = false
|
||||
self._colorScheme = {}
|
||||
self._parent = nil
|
||||
self._firstChild = nil
|
||||
self._prevSibling = nil
|
||||
@ -107,6 +108,35 @@ function Element:isHovered()
|
||||
return self._hovered
|
||||
end
|
||||
|
||||
function Element:setColorScheme(scheme, overwrite)
|
||||
if overwrite then
|
||||
self._colorScheme = scheme
|
||||
else
|
||||
self._colorScheme = table.merge(self._colorScheme, scheme)
|
||||
end
|
||||
end
|
||||
|
||||
function Element:getColorScheme()
|
||||
return self._colorScheme
|
||||
end
|
||||
|
||||
function Element:getColorFromScheme(key)
|
||||
local scheme = self:getColorScheme()[key]
|
||||
local color
|
||||
|
||||
if scheme then
|
||||
if not self:isEnabled() then
|
||||
color = scheme["disabled"] or scheme[1]
|
||||
elseif self:isHovered() then
|
||||
color = scheme["hover"] or scheme[1]
|
||||
else
|
||||
color = scheme[1]
|
||||
end
|
||||
end
|
||||
|
||||
return color or Color(255, 255, 255)
|
||||
end
|
||||
|
||||
function Element:setParent(parent)
|
||||
checkVarClass(parent, Element)
|
||||
|
||||
@ -238,14 +268,17 @@ function Element:_onThink()
|
||||
end
|
||||
|
||||
function Element:_onPaint()
|
||||
if self:isVisible() then
|
||||
if self:isVisible() then
|
||||
--[[
|
||||
if self:hasParent() then
|
||||
render.enableScissorRect(self:getParent():getBounds())
|
||||
end
|
||||
]]
|
||||
|
||||
self:paint()
|
||||
|
||||
local x, y = self:getAbsolutePos()
|
||||
local w, h = self:getSize()
|
||||
|
||||
self:paint(x, y, w, h)
|
||||
|
||||
if self._firstChild then
|
||||
self._firstChild:_postEventToAllReverse("PAINT")
|
||||
@ -290,10 +323,10 @@ function Element:_onMouseReleased(x, y, key, keyName)
|
||||
end
|
||||
|
||||
function Element:_onMouseMoved(x, y)
|
||||
if self:cursorIntersect(x, y) and self:isEnabled() then
|
||||
local element = self:_postEventToAll("MOUSE_MOVED", x, y)
|
||||
|
||||
if not element then
|
||||
local element = self:_postEventToAll("MOUSE_MOVED", x, y)
|
||||
|
||||
if self:cursorIntersect(x, y) then
|
||||
if self:isEnabled() then
|
||||
if not self:isHovered() then
|
||||
self._hovered = true
|
||||
|
||||
@ -302,17 +335,15 @@ function Element:_onMouseMoved(x, y)
|
||||
|
||||
self:onMouseMoved(x, y)
|
||||
end
|
||||
|
||||
return true
|
||||
else
|
||||
if self:isHovered() then
|
||||
self._hovered = false
|
||||
|
||||
self:onMouseLeave()
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function Element:_onButtonPressed(key, keyName)
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
--@include element.txt
|
||||
|
||||
require("element.txt")
|
||||
|
||||
EEmpty = class("EEmpty", Element)
|
||||
@ -6,17 +6,20 @@ require("/koptilnya/libs/utils.txt")
|
||||
|
||||
ELabel = class("ELabel", Element)
|
||||
|
||||
accessorFunc(ELabel, "_color", "Color", Color(255, 255, 255))
|
||||
accessorFunc(ELabel, "_disabledColor", "DisabledColor", Color(200, 200, 200))
|
||||
|
||||
function ELabel:initialize()
|
||||
Element.initialize(self)
|
||||
self._text = ""
|
||||
self._textWidth = 0
|
||||
self._textHeight = 0
|
||||
|
||||
self:setFont("Trebuchet18")
|
||||
self:setFont(GUI.fonts["main"])
|
||||
self:setText("Label")
|
||||
self:setColorScheme({
|
||||
text = {
|
||||
Color(255, 255, 255),
|
||||
disabled = Color(200, 200, 200)
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
function ELabel:setText(text)
|
||||
@ -25,7 +28,7 @@ function ELabel:setText(text)
|
||||
if self:getFont() then
|
||||
render.setFont(self:getFont())
|
||||
|
||||
self._textWidth, self._textHeight = render.getTextSize(self:getText())
|
||||
self._textWidth, self._textHeight = render.getTextSize(text)
|
||||
end
|
||||
end
|
||||
|
||||
@ -36,7 +39,7 @@ end
|
||||
function ELabel:setFont(font)
|
||||
self._font = font
|
||||
|
||||
render.setFont(self:getFont())
|
||||
render.setFont(font)
|
||||
|
||||
self._textWidth, self._textHeight = render.getTextSize(self:getText())
|
||||
end
|
||||
@ -49,10 +52,12 @@ function ELabel:getTextSize()
|
||||
return self._textWidth, self._textHeight
|
||||
end
|
||||
|
||||
function ELabel:paint()
|
||||
local x, y = self:getAbsolutePos()
|
||||
|
||||
function ELabel:sizeToContents()
|
||||
self:setSize(self:getTextSize())
|
||||
end
|
||||
|
||||
function ELabel:paint(x, y, w, h)
|
||||
render.setFont(self:getFont())
|
||||
render.setColor(self:isEnabled() and self:getColor() or self:getDisabledColor())
|
||||
render.setColor(self:getColorFromScheme("text"))
|
||||
render.drawSimpleText(x, y, self:getText())
|
||||
end
|
||||
69
koptilnya/gui/elements/labeled_checkbox.txt
Normal file
69
koptilnya/gui/elements/labeled_checkbox.txt
Normal file
@ -0,0 +1,69 @@
|
||||
--@include element.txt
|
||||
--@include checkbox.txt
|
||||
--@include label.txt
|
||||
--@include /koptilnya/libs/utils.txt
|
||||
|
||||
require("element.txt")
|
||||
require("checkbox.txt")
|
||||
require("label.txt")
|
||||
require("/koptilnya/libs/utils.txt")
|
||||
|
||||
ELabeledCheckbox = class("ELabeledCheckbox", Element)
|
||||
|
||||
accessorFunc(ELabeledCheckbox, "_indent", "Indent", 8)
|
||||
|
||||
function ELabeledCheckbox:initialize()
|
||||
Element.initialize(self)
|
||||
|
||||
self._checked = false
|
||||
|
||||
self.checkbox = ECheckbox:new()
|
||||
self.checkbox.onChange = function(_, state) self:onChange(state) end
|
||||
self:addChild(self.checkbox)
|
||||
|
||||
self.label = ELabel:new()
|
||||
self.label.onMousePressed = function(_, x, y, key, keyName)
|
||||
if keyName == "MOUSE1" then
|
||||
self:toggle()
|
||||
end
|
||||
end
|
||||
self:addChild(self.label)
|
||||
|
||||
self:setText("Checkbox")
|
||||
end
|
||||
|
||||
function ELabeledCheckbox:setChecked(state)
|
||||
self.checkbox:setChecked(state)
|
||||
end
|
||||
|
||||
function ELabeledCheckbox:isChecked()
|
||||
return self.checkbox:isChecked()
|
||||
end
|
||||
|
||||
function ELabeledCheckbox:toggle()
|
||||
self.checkbox:toggle()
|
||||
end
|
||||
|
||||
function ELabeledCheckbox:setText(text)
|
||||
self.label:setText(text)
|
||||
self:sizeToContents()
|
||||
end
|
||||
|
||||
function ELabeledCheckbox:setFont(font)
|
||||
self.label:setFont(font)
|
||||
self:sizeToContents()
|
||||
end
|
||||
|
||||
function ELabeledCheckbox:sizeToContents()
|
||||
self:performLayout(self:getSize())
|
||||
self:setWidth(self.label:getX() + self.label:getWidth())
|
||||
self:setHeight(math.max(self.checkbox:getHeight(), self.label:getHeight()))
|
||||
end
|
||||
|
||||
function ELabeledCheckbox:performLayout(w, h)
|
||||
self.label:sizeToContents()
|
||||
self.label:setX(self.checkbox:getWidth() + self:getIndent())
|
||||
end
|
||||
|
||||
function ELabeledCheckbox:onChange(state)
|
||||
end
|
||||
@ -10,7 +10,6 @@ require("/koptilnya/libs/utils.txt")
|
||||
|
||||
EPanel = class("EPanel", Element)
|
||||
|
||||
accessorFunc(EPanel, "_backgroundColor", "BackgroundColor", Color(23, 23, 23))
|
||||
accessorFunc(EPanel, "_parentLock", "ParentLock", false)
|
||||
|
||||
function EPanel:initialize()
|
||||
@ -22,23 +21,46 @@ function EPanel:initialize()
|
||||
self.title:setPos(12, 8)
|
||||
self:addChild(self.title)
|
||||
|
||||
local colorScheme = {
|
||||
bg = {
|
||||
Color(0, 0, 0),
|
||||
hover = Color(25, 25, 25),
|
||||
disabled = Color(0, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
self.minimizeButton = EButton:new()
|
||||
self.minimizeButton:setFont(GUI.fonts["icons"])
|
||||
self.minimizeButton:setText("_")
|
||||
self.minimizeButton:setSize(32, 32)
|
||||
self.minimizeButton:setRadius(0)
|
||||
self.minimizeButton:setColor(Color(0, 0, 0))
|
||||
self.minimizeButton:setHoveredColor(Color(25, 25, 25))
|
||||
self.minimizeButton:setColorScheme(colorScheme)
|
||||
self.minimizeButton:setEnabled(false)
|
||||
self:addChild(self.minimizeButton)
|
||||
|
||||
self.closeButton = EButton:new()
|
||||
self.closeButton:setFont(GUI.fonts["icons"])
|
||||
self.closeButton:setText("X")
|
||||
self.closeButton:setSize(32, 32)
|
||||
self.closeButton:setRadius(0)
|
||||
self.closeButton:setColor(Color(0, 0, 0))
|
||||
self.closeButton:setHoveredColor(Color(25, 25, 25))
|
||||
self.closeButton:setColorScheme(colorScheme)
|
||||
self.closeButton.onClick = function()
|
||||
self:close()
|
||||
end
|
||||
self:addChild(self.closeButton)
|
||||
|
||||
self:setTitle("Panel")
|
||||
self:setColorScheme({
|
||||
border = {
|
||||
Color(255, 255, 255, 10)
|
||||
},
|
||||
header = {
|
||||
Color(0, 0, 0)
|
||||
},
|
||||
bg = {
|
||||
Color(23, 23, 23)
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
function EPanel:setTitle(title)
|
||||
@ -57,6 +79,16 @@ function EPanel:isMinimized()
|
||||
return self._minimized
|
||||
end
|
||||
|
||||
function EPanel:close()
|
||||
self:setVisible(false)
|
||||
self:setEnabled(false)
|
||||
end
|
||||
|
||||
function EPanel:open()
|
||||
self:setVisible(false)
|
||||
self:setEnabled(false)
|
||||
end
|
||||
|
||||
function EPanel:onMousePressed(x, y, key, keyName)
|
||||
if keyName == "MOUSE1" then
|
||||
local aX, aY = self:getAbsolutePos()
|
||||
@ -97,16 +129,13 @@ function EPanel:performLayout(w, h)
|
||||
self.minimizeButton:setPos(w - 65, 1)
|
||||
end
|
||||
|
||||
function EPanel:paint()
|
||||
local x, y = self:getAbsolutePos()
|
||||
local w, h = self:getSize()
|
||||
|
||||
render.setColor(Color(255, 255, 255, 10))
|
||||
function EPanel:paint(x, y, w, h)
|
||||
render.setColor(self:getColorFromScheme("border"))
|
||||
render.drawRectFast(x, y, w, h)
|
||||
|
||||
render.setColor(Color(0, 0, 0))
|
||||
render.setColor(self:getColorFromScheme("header"))
|
||||
render.drawRectFast(x + 1, y + 1, w - 2, 32)
|
||||
|
||||
render.setColor(self:getBackgroundColor())
|
||||
render.setColor(self:getColorFromScheme("bg"))
|
||||
render.drawRectFast(x + 1, y + 33, w - 2, h - 34)
|
||||
end
|
||||
@ -7,11 +7,9 @@ require("/koptilnya/libs/utils.txt")
|
||||
EShape = class("EShape", Element)
|
||||
|
||||
accessorFunc(EShape, "_color", "Color", Color(255, 255, 255))
|
||||
accessorFunc(EShape, "_hoveredColor", "HoveredColor", Color(150, 150, 150))
|
||||
|
||||
function EShape:paint()
|
||||
local x, y = self:getAbsolutePos()
|
||||
local w, h = self:getSize()
|
||||
|
||||
render.setColor(self:getColor())
|
||||
function EShape:paint(x, y, w, h)
|
||||
render.setColor(self:isHovered() and self:getHoveredColor() or self:getColor())
|
||||
render.drawRectFast(x, y, w, h)
|
||||
end
|
||||
@ -6,6 +6,11 @@ require("elements/root.txt")
|
||||
|
||||
GUI = class("GUI")
|
||||
|
||||
GUI.static.fonts = {
|
||||
main = render.createFont("Roboto", 16, 700, true),
|
||||
icons = render.createFont("Roboto Mono", 16, 700, true)
|
||||
}
|
||||
|
||||
function GUI:initialize(renderDevice)
|
||||
checkVarClass(renderDevice, RenderDevice)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user