Convert pen strokes to texture

This commit is contained in:
Michał 2024-01-26 00:55:21 +00:00
parent 9cfdd651b7
commit f2d2a539ea
4 changed files with 83 additions and 27 deletions

View file

@ -1,6 +1,8 @@
package main
import (
"strings"
raylib "github.com/gen2brain/raylib-go/raylib"
)
@ -12,8 +14,8 @@ type Canvas struct {
Target raylib.RenderTexture2D
Strokes []penTool
UndoneStrokes []penTool
Strokes []raylib.Texture2D
UndoneStrokes []raylib.Texture2D
Refresh bool
}
@ -24,8 +26,15 @@ func (c *Canvas) Update() {
raylib.BeginTextureMode(c.Target)
raylib.ClearBackground(raylib.White)
for _, mark := range c.Strokes {
mark.Draw(raylib.Vector2Scale(c.Offset, -1))
for _, stroke := range c.Strokes {
raylib.DrawTexturePro(
stroke,
raylib.NewRectangle(0, 0, c.Size.X, -c.Size.Y),
raylib.NewRectangle(0, 0, c.Size.X, c.Size.Y),
raylib.Vector2Zero(),
0,
raylib.White,
)
}
raylib.EndTextureMode()
@ -33,6 +42,12 @@ func (c *Canvas) Update() {
}
}
func (c *Canvas) AddStroke(stroke raylib.Texture2D) {
c.Strokes = append(c.Strokes, stroke)
c.UndoneStrokes = []raylib.Texture2D{}
c.Refresh = true
}
func (c *Canvas) Undo() {
if len(c.Strokes) > 0 {
c.UndoneStrokes = append(c.UndoneStrokes, c.Strokes[len(c.Strokes)-1])
@ -65,6 +80,8 @@ func (c *Canvas) Draw() {
}
func (c *Canvas) Save() {
c.Name = strings.Trim(c.Name, " ")
if c.Name == "" {
AddToast("Please enter a file name!")
} else {
@ -85,8 +102,8 @@ func NewCanvas(name string, size, offset raylib.Vector2) *Canvas {
Size: size,
Offset: offset,
Target: raylib.LoadRenderTexture(int32(size.X), int32(size.Y)),
Strokes: []penTool{},
UndoneStrokes: []penTool{},
Strokes: []raylib.Texture2D{},
UndoneStrokes: []raylib.Texture2D{},
Refresh: true,
}
}

50
main.go
View file

@ -12,7 +12,6 @@ const (
WindowTitle = "Colouring App"
WindowMinWidth = int32(800)
WindowMinHeight = int32(600)
WindowFPS = int32(144)
)
var (
@ -31,6 +30,10 @@ const (
StateFileMenu
)
var (
canvas *Canvas
)
func checkDirs() {
if _, err := os.Stat(DirAssets); os.IsNotExist(err) {
panic("Assets directory not found")
@ -46,19 +49,18 @@ func main() {
checkDirs() // Make sure all the directories exist
raylib.SetConfigFlags(raylib.FlagWindowResizable)
raylib.SetConfigFlags(raylib.FlagVsyncHint)
//raylib.SetTraceLogLevel(raylib.LogTrace)
//raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
raylib.InitWindow(WindowWidth, WindowHeight, WindowTitle)
raylib.SetWindowMinSize(int(WindowMinWidth), int(WindowMinHeight))
raylib.SetTargetFPS(WindowFPS)
raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor())))
//raylib.SetExitKey(0) // disable exit key
var (
camera = raylib.NewCamera2D(raylib.NewVector2(0, 0), raylib.NewVector2(0, 0), 0, 1)
canvas = NewCanvas("NewProject", raylib.NewVector2(700, 530), raylib.NewVector2(15, 15))
currentStroke = penTool{}
sidePanelWidth = float32(350)
@ -73,8 +75,14 @@ func main() {
state = StateNormal
appShouldQuit = false
showCursor = true
showDebugStats = false
)
// init canvas
canvas = NewCanvas("NewProject", raylib.NewVector2(700, 530), raylib.NewVector2(15, 15))
// LOOP
for !appShouldQuit {
appShouldQuit = raylib.WindowShouldClose()
@ -86,13 +94,17 @@ func main() {
// INPUT
{
if raylib.IsKeyPressed(raylib.KeyF8) {
showDebugStats = !showDebugStats
}
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && state == StateNormal {
if !raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(WindowWidth-int32(sidePanelWidth)), 0, sidePanelWidth, float32(WindowHeight))) &&
raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) {
state = StateDrawing
currentStroke = penTool{
Color: colourPickerVal,
Size: brushSize,
Color: colourPickerVal,
}
}
}
@ -110,10 +122,7 @@ func main() {
}
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
canvas.Strokes = append(canvas.Strokes, currentStroke)
canvas.UndoneStrokes = []penTool{}
canvas.Refresh = true
canvas.AddStroke(currentStroke.Render())
currentStroke = penTool{}
state = StateNormal
}
@ -136,6 +145,12 @@ func main() {
} else {
gui.SetState(gui.STATE_NORMAL)
}
if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(WindowWidth-int32(sidePanelWidth)), 0, sidePanelWidth, float32(WindowHeight))) {
showCursor = false
} else {
showCursor = true
}
}
// DRAW
@ -151,14 +166,18 @@ func main() {
canvas.Draw()
raylib.BeginScissorMode(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y))
currentStroke.Draw(raylib.NewVector2(0, 0))
currentStroke.Draw()
raylib.EndScissorMode()
raylib.DrawRectangleLines(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y), raylib.DarkGray)
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
}
raylib.EndMode2D()
// Cursor
if showCursor {
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
}
// UI stuff
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight)
{
@ -192,14 +211,17 @@ func main() {
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Gray)
// Info
{
if showDebugStats {
var text string
text = fmt.Sprintf("Strokes: %d | Points: %d", len(canvas.Strokes), len(currentStroke.Points))
gui.StatusBar(raylib.NewRectangle(0, float32(WindowHeight-20), 200, 20), text)
gui.StatusBar(raylib.NewRectangle(0, float32(WindowHeight-20), 150, 20), text)
text = fmt.Sprintf("Canvas Size: %dx%d", int32(canvas.Size.X), int32(canvas.Size.Y))
gui.StatusBar(raylib.NewRectangle(199, float32(WindowHeight-20), 200, 20), text)
gui.StatusBar(raylib.NewRectangle(150, float32(WindowHeight-20), 150, 20), text)
text = fmt.Sprintf("FPS: %d | DT: %f", raylib.GetFPS(), raylib.GetFrameTime())
gui.StatusBar(raylib.NewRectangle(300, float32(WindowHeight-20), 170, 20), text)
}
switch state {

View file

@ -5,22 +5,41 @@ import (
)
type penTool struct {
Color raylib.Color
Size float32
Color raylib.Color
Points []raylib.Vector2
}
func (p *penTool) Draw(offset raylib.Vector2) {
func (p *penTool) Render() raylib.Texture2D {
offset := raylib.Vector2Scale(canvas.Offset, -1)
texture := raylib.LoadRenderTexture(int32(canvas.Size.X), int32(canvas.Size.Y))
raylib.BeginTextureMode(texture)
raylib.ClearBackground(raylib.Fade(raylib.Black, 0))
for i := 0; i < len(p.Points)-1; i++ {
startPoint := raylib.Vector2Add(p.Points[i], offset)
endPoint := raylib.Vector2Add(p.Points[i+1], offset)
raylib.DrawLineEx(startPoint, endPoint, p.Size, p.Color)
raylib.DrawCircle(int32(startPoint.X), int32(startPoint.Y), p.Size/2, p.Color)
}
// Add a circle at the end of the stroke
if len(p.Points) > 0 {
endPoint := raylib.Vector2Add(p.Points[len(p.Points)-1], offset)
raylib.DrawCircle(int32(endPoint.X), int32(endPoint.Y), p.Size/2, p.Color)
}
raylib.EndTextureMode()
return texture.Texture
}
func (p *penTool) Draw() {
for i := 0; i < len(p.Points)-1; i++ {
startPoint := p.Points[i]
endPoint := p.Points[i+1]
raylib.DrawLineEx(startPoint, endPoint, p.Size, p.Color)
raylib.DrawCircle(int32(startPoint.X), int32(startPoint.Y), p.Size/2, p.Color)
}
if len(p.Points) > 0 {
endPoint := p.Points[len(p.Points)-1]
raylib.DrawCircle(int32(endPoint.X), int32(endPoint.Y), p.Size/2, p.Color)
}
}

View file

@ -22,9 +22,7 @@ type toast struct {
}
func AddToast(text string) {
toast := toast{Text: text, Age: time.Now()}
toasts = append(toasts, toast)
toasts = append(toasts, toast{Text: text, Age: time.Now()})
fmt.Printf("Added toast: '%s'\n", text)
}