mirror of
https://github.com/Fluffy-Bean/ColouringApp.git
synced 2025-01-14 17:35:12 +00:00
Convert pen strokes to texture
This commit is contained in:
parent
9cfdd651b7
commit
f2d2a539ea
29
canvas.go
29
canvas.go
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,8 +14,8 @@ type Canvas struct {
|
||||||
|
|
||||||
Target raylib.RenderTexture2D
|
Target raylib.RenderTexture2D
|
||||||
|
|
||||||
Strokes []penTool
|
Strokes []raylib.Texture2D
|
||||||
UndoneStrokes []penTool
|
UndoneStrokes []raylib.Texture2D
|
||||||
|
|
||||||
Refresh bool
|
Refresh bool
|
||||||
}
|
}
|
||||||
|
@ -24,8 +26,15 @@ func (c *Canvas) Update() {
|
||||||
|
|
||||||
raylib.BeginTextureMode(c.Target)
|
raylib.BeginTextureMode(c.Target)
|
||||||
raylib.ClearBackground(raylib.White)
|
raylib.ClearBackground(raylib.White)
|
||||||
for _, mark := range c.Strokes {
|
for _, stroke := range c.Strokes {
|
||||||
mark.Draw(raylib.Vector2Scale(c.Offset, -1))
|
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()
|
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() {
|
func (c *Canvas) Undo() {
|
||||||
if len(c.Strokes) > 0 {
|
if len(c.Strokes) > 0 {
|
||||||
c.UndoneStrokes = append(c.UndoneStrokes, c.Strokes[len(c.Strokes)-1])
|
c.UndoneStrokes = append(c.UndoneStrokes, c.Strokes[len(c.Strokes)-1])
|
||||||
|
@ -65,6 +80,8 @@ func (c *Canvas) Draw() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Canvas) Save() {
|
func (c *Canvas) Save() {
|
||||||
|
c.Name = strings.Trim(c.Name, " ")
|
||||||
|
|
||||||
if c.Name == "" {
|
if c.Name == "" {
|
||||||
AddToast("Please enter a file name!")
|
AddToast("Please enter a file name!")
|
||||||
} else {
|
} else {
|
||||||
|
@ -85,8 +102,8 @@ func NewCanvas(name string, size, offset raylib.Vector2) *Canvas {
|
||||||
Size: size,
|
Size: size,
|
||||||
Offset: offset,
|
Offset: offset,
|
||||||
Target: raylib.LoadRenderTexture(int32(size.X), int32(size.Y)),
|
Target: raylib.LoadRenderTexture(int32(size.X), int32(size.Y)),
|
||||||
Strokes: []penTool{},
|
Strokes: []raylib.Texture2D{},
|
||||||
UndoneStrokes: []penTool{},
|
UndoneStrokes: []raylib.Texture2D{},
|
||||||
Refresh: true,
|
Refresh: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
50
main.go
50
main.go
|
@ -12,7 +12,6 @@ const (
|
||||||
WindowTitle = "Colouring App"
|
WindowTitle = "Colouring App"
|
||||||
WindowMinWidth = int32(800)
|
WindowMinWidth = int32(800)
|
||||||
WindowMinHeight = int32(600)
|
WindowMinHeight = int32(600)
|
||||||
WindowFPS = int32(144)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -31,6 +30,10 @@ const (
|
||||||
StateFileMenu
|
StateFileMenu
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
canvas *Canvas
|
||||||
|
)
|
||||||
|
|
||||||
func checkDirs() {
|
func checkDirs() {
|
||||||
if _, err := os.Stat(DirAssets); os.IsNotExist(err) {
|
if _, err := os.Stat(DirAssets); os.IsNotExist(err) {
|
||||||
panic("Assets directory not found")
|
panic("Assets directory not found")
|
||||||
|
@ -46,19 +49,18 @@ func main() {
|
||||||
checkDirs() // Make sure all the directories exist
|
checkDirs() // Make sure all the directories exist
|
||||||
|
|
||||||
raylib.SetConfigFlags(raylib.FlagWindowResizable)
|
raylib.SetConfigFlags(raylib.FlagWindowResizable)
|
||||||
|
raylib.SetConfigFlags(raylib.FlagVsyncHint)
|
||||||
//raylib.SetTraceLogLevel(raylib.LogTrace)
|
//raylib.SetTraceLogLevel(raylib.LogTrace)
|
||||||
//raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
|
//raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
|
||||||
|
|
||||||
raylib.InitWindow(WindowWidth, WindowHeight, WindowTitle)
|
raylib.InitWindow(WindowWidth, WindowHeight, WindowTitle)
|
||||||
raylib.SetWindowMinSize(int(WindowMinWidth), int(WindowMinHeight))
|
raylib.SetWindowMinSize(int(WindowMinWidth), int(WindowMinHeight))
|
||||||
|
raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor())))
|
||||||
raylib.SetTargetFPS(WindowFPS)
|
|
||||||
//raylib.SetExitKey(0) // disable exit key
|
//raylib.SetExitKey(0) // disable exit key
|
||||||
|
|
||||||
var (
|
var (
|
||||||
camera = raylib.NewCamera2D(raylib.NewVector2(0, 0), raylib.NewVector2(0, 0), 0, 1)
|
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{}
|
currentStroke = penTool{}
|
||||||
|
|
||||||
sidePanelWidth = float32(350)
|
sidePanelWidth = float32(350)
|
||||||
|
@ -73,8 +75,14 @@ func main() {
|
||||||
|
|
||||||
state = StateNormal
|
state = StateNormal
|
||||||
appShouldQuit = false
|
appShouldQuit = false
|
||||||
|
|
||||||
|
showCursor = true
|
||||||
|
showDebugStats = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// init canvas
|
||||||
|
canvas = NewCanvas("NewProject", raylib.NewVector2(700, 530), raylib.NewVector2(15, 15))
|
||||||
|
|
||||||
// LOOP
|
// LOOP
|
||||||
for !appShouldQuit {
|
for !appShouldQuit {
|
||||||
appShouldQuit = raylib.WindowShouldClose()
|
appShouldQuit = raylib.WindowShouldClose()
|
||||||
|
@ -86,13 +94,17 @@ func main() {
|
||||||
|
|
||||||
// INPUT
|
// INPUT
|
||||||
{
|
{
|
||||||
|
if raylib.IsKeyPressed(raylib.KeyF8) {
|
||||||
|
showDebugStats = !showDebugStats
|
||||||
|
}
|
||||||
|
|
||||||
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && state == StateNormal {
|
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && state == StateNormal {
|
||||||
if !raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(WindowWidth-int32(sidePanelWidth)), 0, sidePanelWidth, float32(WindowHeight))) &&
|
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)) {
|
raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) {
|
||||||
state = StateDrawing
|
state = StateDrawing
|
||||||
currentStroke = penTool{
|
currentStroke = penTool{
|
||||||
Color: colourPickerVal,
|
|
||||||
Size: brushSize,
|
Size: brushSize,
|
||||||
|
Color: colourPickerVal,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,10 +122,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
|
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
|
||||||
canvas.Strokes = append(canvas.Strokes, currentStroke)
|
canvas.AddStroke(currentStroke.Render())
|
||||||
canvas.UndoneStrokes = []penTool{}
|
|
||||||
canvas.Refresh = true
|
|
||||||
|
|
||||||
currentStroke = penTool{}
|
currentStroke = penTool{}
|
||||||
state = StateNormal
|
state = StateNormal
|
||||||
}
|
}
|
||||||
|
@ -136,6 +145,12 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
gui.SetState(gui.STATE_NORMAL)
|
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
|
// DRAW
|
||||||
|
@ -151,14 +166,18 @@ func main() {
|
||||||
canvas.Draw()
|
canvas.Draw()
|
||||||
|
|
||||||
raylib.BeginScissorMode(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y))
|
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.EndScissorMode()
|
||||||
|
|
||||||
raylib.DrawRectangleLines(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y), raylib.DarkGray)
|
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()
|
raylib.EndMode2D()
|
||||||
|
|
||||||
|
// Cursor
|
||||||
|
if showCursor {
|
||||||
|
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
|
||||||
|
}
|
||||||
|
|
||||||
// UI stuff
|
// UI stuff
|
||||||
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight)
|
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight)
|
||||||
{
|
{
|
||||||
|
@ -192,14 +211,17 @@ func main() {
|
||||||
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Gray)
|
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Gray)
|
||||||
|
|
||||||
// Info
|
// Info
|
||||||
{
|
if showDebugStats {
|
||||||
var text string
|
var text string
|
||||||
|
|
||||||
text = fmt.Sprintf("Strokes: %d | Points: %d", len(canvas.Strokes), len(currentStroke.Points))
|
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))
|
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 {
|
switch state {
|
||||||
|
|
27
penTool.go
27
penTool.go
|
@ -5,22 +5,41 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type penTool struct {
|
type penTool struct {
|
||||||
Color raylib.Color
|
|
||||||
Size float32
|
Size float32
|
||||||
|
Color raylib.Color
|
||||||
Points []raylib.Vector2
|
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++ {
|
for i := 0; i < len(p.Points)-1; i++ {
|
||||||
startPoint := raylib.Vector2Add(p.Points[i], offset)
|
startPoint := raylib.Vector2Add(p.Points[i], offset)
|
||||||
endPoint := raylib.Vector2Add(p.Points[i+1], offset)
|
endPoint := raylib.Vector2Add(p.Points[i+1], offset)
|
||||||
|
|
||||||
raylib.DrawLineEx(startPoint, endPoint, p.Size, p.Color)
|
raylib.DrawLineEx(startPoint, endPoint, p.Size, p.Color)
|
||||||
raylib.DrawCircle(int32(startPoint.X), int32(startPoint.Y), p.Size/2, 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 {
|
if len(p.Points) > 0 {
|
||||||
endPoint := raylib.Vector2Add(p.Points[len(p.Points)-1], offset)
|
endPoint := raylib.Vector2Add(p.Points[len(p.Points)-1], offset)
|
||||||
raylib.DrawCircle(int32(endPoint.X), int32(endPoint.Y), p.Size/2, p.Color)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
4
toast.go
4
toast.go
|
@ -22,9 +22,7 @@ type toast struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddToast(text string) {
|
func AddToast(text string) {
|
||||||
toast := toast{Text: text, Age: time.Now()}
|
toasts = append(toasts, toast{Text: text, Age: time.Now()})
|
||||||
toasts = append(toasts, toast)
|
|
||||||
|
|
||||||
fmt.Printf("Added toast: '%s'\n", text)
|
fmt.Printf("Added toast: '%s'\n", text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue