ColouringApp/scenes/drawing.go

187 lines
6.1 KiB
Go
Raw Normal View History

2024-01-23 16:50:36 +00:00
package scenes
import (
"ColouringApp/application"
2024-01-24 11:36:36 +00:00
"fmt"
2024-01-23 16:50:36 +00:00
gui "github.com/gen2brain/raylib-go/raygui"
raylib "github.com/gen2brain/raylib-go/raylib"
)
2024-01-24 11:36:36 +00:00
const (
ModeDrawing = iota
ModeLine
)
2024-01-23 16:50:36 +00:00
type stroke struct {
Color raylib.Color
Size float32
Points []raylib.Vector2
}
func Drawing() {
var (
2024-01-24 11:36:36 +00:00
canvasSize = raylib.NewVector2(500, 430)
canvas = raylib.LoadRenderTexture(int32(canvasSize.X), int32(canvasSize.Y))
sidePanelWidth float32 = 300
sidePanelRelativeX = application.WindowWidth - int32(sidePanelWidth)
drawing = false
//drawingMode = ModeDrawing
2024-01-23 16:50:36 +00:00
currentStroke = stroke{}
strokes = []stroke{currentStroke}
undoneStrokes = []stroke{}
2024-01-23 17:23:57 +00:00
colourPickerVal = raylib.Orange
colourPickerHeight float32 = 200
2024-01-24 11:36:36 +00:00
brushSize float32 = 10
2024-01-23 17:23:57 +00:00
)
2024-01-23 16:50:36 +00:00
refreshCanvas := func() {
raylib.BeginTextureMode(canvas)
raylib.ClearBackground(raylib.White)
2024-01-23 17:23:57 +00:00
2024-01-23 16:50:36 +00:00
for i := 1; i < len(strokes); i++ {
for j := 1; j < len(strokes[i].Points); j++ {
startPos := raylib.Vector2Subtract(strokes[i].Points[j-1], raylib.NewVector2(10, 10))
endPos := raylib.Vector2Subtract(strokes[i].Points[j], raylib.NewVector2(10, 10))
raylib.DrawLineEx(startPos, endPos, strokes[i].Size, strokes[i].Color)
raylib.DrawCircle(int32(endPos.X), int32(endPos.Y), strokes[i].Size/2, strokes[i].Color)
}
}
2024-01-23 17:23:57 +00:00
2024-01-23 16:50:36 +00:00
raylib.EndTextureMode()
}
2024-01-23 17:23:57 +00:00
// Create canvas
refreshCanvas()
2024-01-23 16:50:36 +00:00
for !application.ShouldQuit {
2024-01-24 11:36:36 +00:00
// DEFAULT
{
application.ShouldQuit = raylib.WindowShouldClose()
if application.CurrentScene != application.SceneDrawing {
break
}
if raylib.IsWindowResized() {
application.WindowWidth = int32(raylib.GetScreenWidth())
application.WindowHeight = int32(raylib.GetScreenHeight())
sidePanelRelativeX = application.WindowWidth - int32(sidePanelWidth)
}
2024-01-23 17:23:57 +00:00
}
2024-01-23 16:50:36 +00:00
// INPUT
2024-01-23 17:23:57 +00:00
{
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
2024-01-24 11:36:36 +00:00
drawing = false
if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvasSize.X, canvasSize.Y)) {
drawing = true
currentStroke = stroke{
Color: colourPickerVal,
Size: brushSize,
}
2024-01-23 17:23:57 +00:00
}
2024-01-23 16:50:36 +00:00
}
2024-01-24 11:36:36 +00:00
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && drawing {
var safeZone float32 = 5
2024-01-23 17:23:57 +00:00
if len(currentStroke.Points) <= 1 {
currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition())
} else if raylib.Vector2Distance(currentStroke.Points[len(currentStroke.Points)-1], raylib.GetMousePosition()) > safeZone {
currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition())
}
2024-01-23 16:50:36 +00:00
}
2024-01-23 17:23:57 +00:00
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
2024-01-24 11:36:36 +00:00
drawing = false
2024-01-23 17:23:57 +00:00
strokes = append(strokes, currentStroke)
currentStroke = stroke{}
undoneStrokes = []stroke{}
2024-01-24 11:36:36 +00:00
2024-01-23 17:23:57 +00:00
refreshCanvas()
2024-01-23 16:50:36 +00:00
}
2024-01-23 17:23:57 +00:00
if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyDown(raylib.KeyLeftShift) && raylib.IsKeyPressed(raylib.KeyZ) {
2024-01-24 11:36:36 +00:00
if len(undoneStrokes) > 1 {
2024-01-23 17:23:57 +00:00
strokes = append(strokes, undoneStrokes[len(undoneStrokes)-1])
undoneStrokes = undoneStrokes[:len(undoneStrokes)-1]
}
2024-01-24 11:36:36 +00:00
2024-01-23 17:23:57 +00:00
refreshCanvas()
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyZ) {
2024-01-24 11:36:36 +00:00
if len(strokes) > 1 {
2024-01-23 17:23:57 +00:00
undoneStrokes = append(undoneStrokes, strokes[len(strokes)-1])
strokes = strokes[:len(strokes)-1]
}
2024-01-24 11:36:36 +00:00
2024-01-23 17:23:57 +00:00
refreshCanvas()
2024-01-23 16:50:36 +00:00
}
}
// UPDATE
2024-01-24 11:36:36 +00:00
{
if drawing {
gui.SetState(gui.STATE_DISABLED)
} else {
gui.SetState(gui.STATE_NORMAL)
}
}
2024-01-23 16:50:36 +00:00
// DRAW
raylib.BeginDrawing()
2024-01-23 17:23:57 +00:00
{
raylib.ClearBackground(raylib.White)
2024-01-24 11:36:36 +00:00
gui.Grid(raylib.NewRectangle(0, 0, float32(application.WindowWidth), float32(application.WindowHeight)), "", 30, 1, &raylib.Vector2{})
2024-01-23 17:23:57 +00:00
// Canvas stuff
2024-01-24 11:36:36 +00:00
raylib.DrawRectangle(20, 20, int32(canvasSize.X), int32(canvasSize.Y), raylib.Fade(raylib.Black, 0.3))
2024-01-23 17:23:57 +00:00
raylib.BeginScissorMode(10, 10, int32(canvasSize.X), int32(canvasSize.Y))
{
raylib.DrawTexturePro(canvas.Texture, raylib.NewRectangle(0, 0, float32(canvas.Texture.Width), float32(-canvas.Texture.Height)), raylib.NewRectangle(10, 10, canvasSize.X, canvasSize.Y), raylib.Vector2{}, 0, raylib.White)
for i := 1; i < len(currentStroke.Points); i++ {
raylib.DrawLineEx(currentStroke.Points[i-1], currentStroke.Points[i], currentStroke.Size, currentStroke.Color)
raylib.DrawCircle(int32(currentStroke.Points[i].X), int32(currentStroke.Points[i].Y), currentStroke.Size/2, currentStroke.Color)
}
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
}
raylib.EndScissorMode()
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.Gray)
2024-01-23 16:50:36 +00:00
2024-01-23 17:23:57 +00:00
// UI stuff
2024-01-24 11:36:36 +00:00
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight)
{
raylib.DrawRectangle(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight, raylib.Fade(raylib.White, 0.7))
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+10), 10, 25, 25), gui.IconText(gui.ICON_CROSS, "")) {
application.CurrentScene = application.SceneTitle
}
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+20+25), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_SAVE, "")) {
}
colourPickerVal = gui.ColorPicker(raylib.NewRectangle(float32(sidePanelRelativeX+10), 45, sidePanelWidth-40, colourPickerHeight), "Color", colourPickerVal)
brushSize = gui.Slider(raylib.NewRectangle(float32(sidePanelRelativeX+80), 65+colourPickerHeight, 200, 20), "Brush Size", "", brushSize, 1, 100)
2024-01-23 17:23:57 +00:00
}
2024-01-24 11:36:36 +00:00
raylib.EndScissorMode()
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight, raylib.Gray)
// Info
{
var text string
2024-01-23 16:50:36 +00:00
2024-01-24 11:36:36 +00:00
text = fmt.Sprintf("Strokes: %d | Stroke Points: %d", len(strokes), len(currentStroke.Points))
gui.StatusBar(raylib.NewRectangle(0, float32(application.WindowHeight-20), 200, 20), text)
2024-01-23 16:50:36 +00:00
2024-01-24 11:36:36 +00:00
text = fmt.Sprintf("Canvas Size: %dx%d", int32(canvasSize.X), int32(canvasSize.Y))
gui.StatusBar(raylib.NewRectangle(199, float32(application.WindowHeight-20), 200, 20), text)
}
2024-01-23 16:50:36 +00:00
}
raylib.EndDrawing()
}
// unload resources here
}