Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Implement blend mode code
Browse files Browse the repository at this point in the history
UI2 support not added yet since I won't be able to test them yet
  • Loading branch information
ShamblesSM committed Jan 26, 2023
1 parent 1a77bcc commit b5a1897
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 13 deletions.
11 changes: 9 additions & 2 deletions src/Essentials/Font.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,17 @@ function Font:getTextSize(text)
end
end

function Font:draw(text, pos, align, color, alpha)
function Font:draw(text, pos, align, color, alpha, blendMode)
align = align or Vec2(0.5)
color = color or Color()
alpha = alpha or 1
alpha = alpha or 1
blendMode = blendMode or "alpha"
-- see Sprite.lua on why this reassigment exists
if blendMode == "none" then
blendMode = "alpha"
end
---@diagnostic disable-next-line: param-type-mismatch
love.graphics.setBlendMode(blendMode)

if self.type == "image" then
love.graphics.setColor(color.r, color.g, color.b, alpha)
Expand Down
24 changes: 22 additions & 2 deletions src/Essentials/Sprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,40 @@ end
---@param color Color? The sprite color.
---@param alpha number? Sprite transparency. `0` is fully transparent. `1` is fully opaque.
---@param scale Vector2? The scale of this sprite.
function Sprite:draw(pos, align, state, frame, rot, color, alpha, scale)
---@param blendMode "none"|"alpha"|"screen"|"add"|"subtract"|"multiply"|"lighten"|"darken"? The blending mode to use.
function Sprite:draw(pos, align, state, frame, rot, color, alpha, scale, blendMode)
align = align or Vec2()
state = state or 1
frame = frame or Vec2(1)
rot = rot or 0
color = color or Color()
alpha = alpha or 1
scale = scale or Vec2(1)
scale = scale or Vec2(1)
blendMode = blendMode or "alpha"
-- this is for convenience reasons so json files can use "none"
-- instead of "alpha", but the option is there regardless
if blendMode == "none" then
blendMode = "alpha"
end
pos = _PosOnScreen(pos - (align * scale * self.frameSize):rotate(rot))
if color.r then -- temporary chunk
love.graphics.setColor(color.r, color.g, color.b, alpha)
else
love.graphics.setColor(unpack(color), alpha)
end

--[[
local blendAlphaMode = "alphamultiply"
local modesToPremultiply = { "add", "subtract", "multiply", "lighten", "darken" }
for _,mode in pairs(modesToPremultiply) do
if mode == blendMode then
blendAlphaMode = "premultiplied"
end
end
]]
---@diagnostic disable-next-line: param-type-mismatch
love.graphics.setBlendMode(blendMode)

self.img:draw(self:getFrame(state, frame), pos.x, pos.y, rot, scale.x * _GetResolutionScale(), scale.y * _GetResolutionScale())
end

Expand Down
5 changes: 3 additions & 2 deletions src/Particle/Piece.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ function ParticlePiece:new(manager, spawner, data)
self.lifetime = self.lifespan
self.time = 0

self.sprite = _Game.resourceManager:getSprite(data.sprite)
self.sprite = _Game.resourceManager:getSprite(data.sprite)
self.blendMode = data.blendMode
self.animationSpeed = data.animationSpeed
self.animationFrameCount = data.animationFrameCount
self.animationLoop = data.animationLoop
Expand Down Expand Up @@ -165,7 +166,7 @@ end

function ParticlePiece:draw(layer)
if self.layer == layer then
self.sprite:draw(self:getPos(), Vec2(0.5), nil, Vec2(math.min(math.floor(self.animationFrame), self.animationFrameCount), 1), nil, self:getColor(), self:getAlpha())
self.sprite:draw(self:getPos(), Vec2(0.5), nil, Vec2(math.min(math.floor(self.animationFrame), self.animationFrameCount), 1), nil, self:getColor(), self:getAlpha(), nil, self.blendMode)
end
end

Expand Down
1 change: 1 addition & 0 deletions src/UI/Widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function UIWidget:new(name, data, parent)
self.children[childN] = UIWidget(childN, child, self)
end
end
self.blendMode = data.blendMode

self.inheritShow = data.inheritShow
self.inheritHide = data.inheritHide
Expand Down
2 changes: 1 addition & 1 deletion src/UI/WidgetSprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end


function UIWidgetSprite:draw()
self.sprite:draw(self.parent:getPos(), self.parent.align / self.sprite.size, nil, nil, self.parent.angle, nil, self.parent:getAlpha())
self.sprite:draw(self.parent:getPos(), self.parent.align / self.sprite.size, nil, nil, self.parent.angle, nil, self.parent:getAlpha(), nil, self.parent.blendMode)
end

return UIWidgetSprite
2 changes: 1 addition & 1 deletion src/UI/WidgetSpriteButton.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function UIWidgetSpriteButton:draw()
end
end

self.sprite:draw(pos, nil, self:getState(), nil, nil, nil, alpha)
self.sprite:draw(pos, nil, self:getState(), nil, nil, nil, alpha, nil, self.parent.blendMode)
end

function UIWidgetSpriteButton:getState()
Expand Down
2 changes: 1 addition & 1 deletion src/UI/WidgetSpriteProgress.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function UIWidgetSpriteProgress:draw(variables)
local pos = self.parent:getPos()
local pos2 = _PosOnScreen(pos)
love.graphics.setScissor(pos2.x, pos2.y, self.size.x * _GetResolutionScale() * self.value, self.size.y * _GetResolutionScale())
self.sprite:draw(pos, nil, nil, nil, nil, nil, self.parent:getAlpha())
self.sprite:draw(pos, nil, nil, nil, nil, nil, self.parent:getAlpha(), nil, self.parent.blendMode)
love.graphics.setScissor()
end

Expand Down
2 changes: 1 addition & 1 deletion src/UI/WidgetSpriteProgressBlitz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function UIWidgetSpriteProgressBlitz:draw(variables)
-- mark only these pixels as the pixels which can be affected
love.graphics.setStencilTest("equal", 1)
-- draw the circle
self.sprite:draw(self.parent:getPos(), nil, nil, nil, nil, nil, self.parent:getAlpha())
self.sprite:draw(self.parent:getPos(), nil, nil, nil, nil, nil, self.parent:getAlpha(), nil, self.parent.blendMode)
-- reset the mask
love.graphics.setStencilTest()
end
Expand Down
2 changes: 1 addition & 1 deletion src/UI/WidgetText.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ end


function UIWidgetText:draw(variables)
self.font:draw(self.textTmp, self.parent:getPos(), self.align, nil, self.parent:getAlpha())
self.font:draw(self.textTmp, self.parent:getPos(), self.align, nil, self.parent:getAlpha(), nil, self.parent.blendMode)
end

function UIWidgetText:getSize()
Expand Down
4 changes: 2 additions & 2 deletions src/UI/WidgetTextInput.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ end
function UIWidgetTextInput:draw(variables)
local pos = self.parent:getPos()
local alpha = self.parent:getAlpha()
self.font:draw(self.text, pos, self.align, nil, alpha)
self.font:draw(self.text, pos, self.align, nil, alpha, nil, self.parent.blendMode)
if self.cursorSprite then
local cpos = pos + Vec2(self:getSize().x * (1 - self.align.x), 0)
local frame = math.floor(self.cursorSpriteBlink * 2) + 1
self.cursorSprite:draw(cpos, nil, nil, Vec2(frame, 1), nil, nil, alpha)
self.cursorSprite:draw(cpos, nil, nil, Vec2(frame, 1), nil, nil, alpha, nil, self.parent.blendMode)
end
end

Expand Down

0 comments on commit b5a1897

Please sign in to comment.