PublicPrintBot/project.go

136 lines
3.7 KiB
Go
Raw Normal View History

2024-09-18 13:18:58 +00:00
package main
import (
"errors"
"git.massivebox.net/massivebox/go-catprinter"
"github.com/makeworld-the-better-one/dither/v2"
"image"
"log"
"strconv"
)
type Project struct {
Og image.Image
User int64
Options *catprinter.PrinterOptions
DitherStrength float32
}
func (ctx *Context) createProject(fileID string, userID int64) {
tmpMsgID := ctx.sendMessage(userID, "⏳ Processing...")
img, err := ctx.fileIDtoImage(fileID)
if err != nil {
if errors.Is(err, image.ErrFormat) {
ctx.sendMessage(userID, "🚫 File can't be parsed as an image. If you've sent a sticker, make sure it's not animated or part of an animated set.")
return
}
log.Println("Got error getting image from FileID", err)
ctx.sendMessage(userID, "🚫 Got an error obtaining image, try again")
return
}
if float32(img.Bounds().Dx())/float32(img.Bounds().Dy()) < ctx.Settings.MaxRatio {
ctx.sendMessage(userID, "🚫 Your image is too tall, please send a shorter image.")
return
}
wip := Project{
Og: img,
Options: catprinter.NewOptions(),
User: userID,
DitherStrength: 1,
}
putId := -1
for i := 0; i < ctx.Settings.MaxProjects; i++ {
if ctx.Projects[i] == nil {
putId = i
break
}
}
if putId == -1 {
ctx.sendMessage(userID, "🚫 We can't accept this project for now, try again later")
return
}
ctx.Projects[putId] = &wip
ctx.sendImage(userID, ctx.Client.FormatImage(wip.Og, wip.Options), getEditKeyboard(wip.Options, putId))
ctx.deleteMessage(tmpMsgID, userID)
}
func changeBlackPointOrDitherStrength(proj *Project, delta float32) bool {
if proj.Options.Dither() {
if (proj.DitherStrength < 0.1 && delta < 0) || (proj.DitherStrength > 0.9 && delta > 0) {
return false
}
proj.DitherStrength += delta
proj.Options.SetDitherAlgo(dither.ErrorDiffusionStrength(dither.FloydSteinberg, proj.DitherStrength))
} else {
if (proj.Options.BlackPoint() < 0.1 && delta < 0) || (proj.Options.BlackPoint() > 0.9 && delta > 0) {
return false
}
proj.Options.SetBlackPoint(proj.Options.BlackPoint() + delta)
}
return true
}
func (ctx *Context) editWip(queryCmd rune, projID int, userID int64, msgID int, cqID string) {
proj := ctx.Projects[projID]
if proj == nil {
ctx.callbackQuery(cqID, "🤨 Project not found", true)
return
}
if proj.User != userID {
ctx.callbackQuery(cqID, "🔒 Can't edit this project", true)
return
}
const limitTxt = "🛑 You've reached the limit"
switch queryCmd {
case 't': // toggle dithering
proj.Options.SetBlackPoint(0.5)
proj.DitherStrength = 1
proj.Options.SetDitherAlgo(dither.ErrorDiffusionStrength(dither.FloydSteinberg, proj.DitherStrength))
proj.Options.SetDither(!proj.Options.Dither())
break
case 'l': // lighter
done := changeBlackPointOrDitherStrength(proj, -0.1)
if !done {
ctx.callbackQuery(cqID, limitTxt, true)
return
}
break
case 'b': // darker
done := changeBlackPointOrDitherStrength(proj, +0.1)
if !done {
ctx.callbackQuery(cqID, limitTxt, true)
return
}
break
case 'p': // print
if len(ctx.Queue) >= ctx.Settings.MaxQueueLength {
ctx.callbackQuery(cqID, "🛑 There are already too many projects in queue, please wait...", true)
return
}
ctx.Queue <- projID
ctx.callbackQuery(cqID, "🖨 Waiting to print... There is/are "+strconv.Itoa(len(ctx.Queue))+" project(s) in queue before you.", true)
ctx.editImage(userID, msgID, ctx.Client.FormatImage(proj.Og, proj.Options))
return
case 'd': // discard
ctx.Projects[projID] = nil
ctx.callbackQuery(cqID, "🗑 Project discarded", true)
ctx.deleteMessage(msgID, userID)
return
}
ctx.callbackQuery(cqID, "⏳ Loading...")
ctx.editImage(userID, msgID, ctx.Client.FormatImage(proj.Og, proj.Options), getEditKeyboard(proj.Options, projID))
}