Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an option to display toasts at the provided x/y #1

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Lövely Toasts: toasts for Löve2D
Made and tested in Löve2D v11.3

This is a fork from https://github.com/Loucee/Lovely-Toasts which appears to be abandoned.
All credit goes to Loucee.

![](https://i.imgur.com/yWbfz0l.gif)
## Usage
Step 1: Place the "lovelyToasts.lua" library somewhere in your source folder (e.g. "/lib/soupy.lua")<br/><br/>
Expand Down Expand Up @@ -32,16 +35,37 @@ lovelyToasts.show("Lövely toast :)", 2, "top")
```
This will create a toast messsage at the top of the screen that appears for 2 seconds. Other options for the position are "middle", to put the toast in the center of the screen, "bottom", the default value, or a number for the Y position.

You can specify a screen x and y value to display it anywhere on the screen. This is useful to display a toast message next to some item on the screen.
```lua
lovelyToasts.show("Lövely toast :)", 2, nil, 700, 500)
```

### Styling
Various attributes of the toast message can be changed by changing the values in `lovelyToasts.style`
- **lovelyToasts.style.font** The font to use for the toast message
- **lovelyToasts.style.textColor** The color of the text
```lua
loevelyToasts.textColor = {1,1,1}
```
- **lovelyToasts.style.backgroundColor** The color of the toast
```lua
lovelyToasts.style.backgroundColor = {151,151,151}
```
- **lovelyToasts.style.paddingLR** Left and right padding in the toast
```lua
lovelyToasts.style.paddingLR = 25
```
- **lovelyToasts.style.paddingTB** Top and bottom padding in the toast
```lua
lovelyToasts.style.paddingTB = 10
```

### Other options
### Other options - usually set once in love.load()
- **lovelyToasts.options.tapToDismiss** (set to false by default)<br/>Allows user to tap or click on the toast message to dismiss it
```lua
lovelyToasts.options.tapToDismiss = true
```

- **lovelyToasts.options.queueEnabled** (set to false by default)<br/>When set to true the toasts don't replace, but enter a queue so you can queue multiple toasts in a row
- **lovelyToasts.options.animationDuration** (set to 0.3 by default)<br/>Animation duration for appear and disappear animation

Expand Down
65 changes: 35 additions & 30 deletions lovelyToasts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function lovelyToasts.update(dt)
current._alpha = math.max(0, current._alpha - (100 / lovelyToasts.options.animationDuration * dt))
end
current._yOffset = math.max(0, current._yOffset - (yOffset / lovelyToasts.options.animationDuration * dt))

-- Remove toast when duration ended
if (current._timePassed >= current.duration) then
table.remove(_toasts, 1)
Expand All @@ -54,35 +54,40 @@ function lovelyToasts.draw()
local screenW = lovelyToasts.canvasSize[1] or love.graphics.getWidth()
local current = _toasts[1]

-- Store current font and color to restore later
local r, g, b, a = love.graphics.getColor()
local font = love.graphics.getFont()

local textWidth = lovelyToasts.style.font:getWidth(current.text)
local textHeight = lovelyToasts.style.font:getHeight()
local textX = (screenW / 2) - (textWidth / 2)
local textY = lovelyToasts._yForPosition(current.position) - (textHeight / 2) + current._yOffset

-- Draw toast background
local bgR, bgG, bgB, bgA = unpack(lovelyToasts.style.backgroundColor)
love.graphics.setColor(bgR, bgG, bgB, (bgA or 0.5) * (current._alpha / 100))
love.graphics.rectangle("fill",
textX - lovelyToasts.style.paddingLR,
textY - lovelyToasts.style.paddingTB,
textWidth + (lovelyToasts.style.paddingLR * 2),
textHeight + (lovelyToasts.style.paddingTB * 2),
10, 10, 1000
)

-- Draw toast title
local tR, tG, tB, tA = unpack(lovelyToasts.style.textColor)
love.graphics.setColor(tR, tG, tB, (tA or 1) * (current._alpha / 100))
love.graphics.setFont(lovelyToasts.style.font)
love.graphics.print(current.text, textX, textY)

-- Restore color and font
love.graphics.setColor(r, g, b, a)
love.graphics.setFont(font)
if current.text ~= nil then

-- Store current font and color to restore later
local r, g, b, a = love.graphics.getColor()
local font = love.graphics.getFont()

local textWidth = lovelyToasts.style.font:getWidth(current.text)
local textHeight = lovelyToasts.style.font:getHeight()
local textX = (screenW / 2) - (textWidth / 2)
local textY = lovelyToasts._yForPosition(current.position) - (textHeight / 2) + current._yOffset

-- Draw toast background
local bgR, bgG, bgB, bgA = unpack(lovelyToasts.style.backgroundColor)
love.graphics.setColor(bgR, bgG, bgB, (bgA or 0.5) * (current._alpha / 100))
love.graphics.rectangle("fill",
textX - lovelyToasts.style.paddingLR,
textY - lovelyToasts.style.paddingTB,
textWidth + (lovelyToasts.style.paddingLR * 2),
textHeight + (lovelyToasts.style.paddingTB * 2),
10, 10, 1000
)

-- Draw toast title
local tR, tG, tB, tA = unpack(lovelyToasts.style.textColor)
love.graphics.setColor(tR, tG, tB, (tA or 1) * (current._alpha / 100))
love.graphics.setFont(lovelyToasts.style.font)
love.graphics.print(current.text, textX, textY)

-- Restore color and font
love.graphics.setColor(r, g, b, a)
love.graphics.setFont(font)
else
print("lovelyToasts error: text provided is nil. No toast displayed")
end
end
end

Expand Down