ColouringApp/main.go

240 lines
7.3 KiB
Go
Raw Normal View History

2024-01-08 13:29:40 +00:00
package main
import (
2024-01-25 15:57:10 +00:00
"fmt"
2024-01-25 20:02:20 +00:00
"os"
2024-01-25 15:57:10 +00:00
gui "github.com/gen2brain/raylib-go/raygui"
2024-01-08 13:29:40 +00:00
raylib "github.com/gen2brain/raylib-go/raylib"
)
2024-01-25 20:02:20 +00:00
const (
WindowTitle = "Colouring App"
WindowMinWidth = int32(800)
WindowMinHeight = int32(600)
WindowFPS = int32(144)
)
var (
WindowWidth = WindowMinWidth
WindowHeight = WindowMinHeight
)
const (
DirAssets = "./assets/"
DirUserData = "./userData/"
)
const (
StateNone = iota
StateDrawing
StateFileMenu
)
func checkDirs() {
if _, err := os.Stat(DirUserData); os.IsNotExist(err) {
err := os.Mkdir(DirUserData, 0755)
if err != nil {
panic(err)
}
}
}
2024-01-08 13:29:40 +00:00
func main() {
2024-01-23 17:23:57 +00:00
raylib.SetConfigFlags(raylib.FlagWindowResizable)
2024-01-25 15:57:10 +00:00
raylib.SetTraceLogLevel(raylib.LogTrace)
raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
2024-01-15 12:19:49 +00:00
2024-01-25 15:57:10 +00:00
raylib.InitWindow(WindowWidth, WindowHeight, WindowTitle)
2024-01-25 20:02:20 +00:00
raylib.SetWindowMinSize(int(WindowMinWidth), int(WindowMinHeight))
2024-01-08 13:29:40 +00:00
raylib.InitAudioDevice()
2024-01-25 15:57:10 +00:00
raylib.SetTargetFPS(WindowFPS)
2024-01-23 16:50:36 +00:00
//raylib.SetExitKey(0) // disable exit key
2024-01-08 13:29:40 +00:00
2024-01-25 15:57:10 +00:00
var (
camera = raylib.NewCamera2D(raylib.NewVector2(0, 0), raylib.NewVector2(0, 0), 0, 1)
2024-01-25 20:02:20 +00:00
canvas = NewCanvas("NewProject", raylib.NewVector2(600, 530), raylib.NewVector2(15, 15))
currentStroke = penTool{}
drawing = false
2024-01-25 15:57:10 +00:00
sidePanelWidth = float32(350)
sidePanelRelativeX = WindowWidth - int32(sidePanelWidth)
colourPickerVal = raylib.Orange
2024-01-25 20:02:20 +00:00
colourPickerHeight = float32(250)
2024-01-25 15:57:10 +00:00
2024-01-25 20:02:20 +00:00
brushSize = float32(10)
2024-01-25 15:57:10 +00:00
fileNameEditing = false
menu = StateNone
2024-01-25 20:02:20 +00:00
appShouldQuit = false
2024-01-25 15:57:10 +00:00
)
// check if userData exists
2024-01-25 20:02:20 +00:00
checkDirs()
2024-01-25 15:57:10 +00:00
2024-01-25 20:02:20 +00:00
for !appShouldQuit {
2024-01-25 15:57:10 +00:00
// LOOP
2024-01-25 20:02:20 +00:00
appShouldQuit = raylib.WindowShouldClose()
2024-01-25 15:57:10 +00:00
if raylib.IsWindowResized() {
WindowWidth = int32(raylib.GetScreenWidth())
WindowHeight = int32(raylib.GetScreenHeight())
sidePanelRelativeX = WindowWidth - int32(sidePanelWidth)
}
// INPUT
{
if raylib.IsKeyPressed(raylib.KeyF8) {
AddToast("This is a toast message!")
}
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(WindowWidth-int32(sidePanelWidth)), 0, sidePanelWidth, float32(WindowHeight))) {
drawing = false
2024-01-25 20:02:20 +00:00
} else if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) {
2024-01-25 15:57:10 +00:00
drawing = true
currentStroke = penTool{
2024-01-25 20:02:20 +00:00
Color: colourPickerVal,
Size: brushSize,
2024-01-25 15:57:10 +00:00
}
}
}
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && drawing {
var safeZone float32 = 5
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())
}
}
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
2024-01-25 20:02:20 +00:00
canvas.Strokes = append(canvas.Strokes, currentStroke)
canvas.UndoneStrokes = []penTool{}
canvas.Refresh = true
2024-01-25 15:57:10 +00:00
2024-01-25 20:02:20 +00:00
currentStroke = penTool{}
2024-01-25 15:57:10 +00:00
drawing = false
}
if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyDown(raylib.KeyLeftShift) && raylib.IsKeyPressed(raylib.KeyZ) {
2024-01-25 20:02:20 +00:00
canvas.Redo()
2024-01-25 15:57:10 +00:00
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyZ) {
2024-01-25 20:02:20 +00:00
canvas.Undo()
2024-01-25 15:57:10 +00:00
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyS) {
2024-01-25 20:02:20 +00:00
canvas.Save()
2024-01-25 15:57:10 +00:00
}
}
// UPDATE
{
if drawing {
gui.SetState(gui.STATE_DISABLED)
} else {
gui.SetState(gui.STATE_NORMAL)
}
2024-01-25 20:02:20 +00:00
canvas.Update()
2024-01-25 15:57:10 +00:00
UpdateToasts()
}
// DRAW
raylib.BeginDrawing()
{
raylib.ClearBackground(raylib.White)
gui.Grid(raylib.NewRectangle(0, 0, float32(WindowWidth), float32(WindowHeight)), "", 30, 1, &raylib.Vector2{})
// Canvas stuff
raylib.BeginMode2D(camera)
{
2024-01-25 20:02:20 +00:00
raylib.DrawRectangle(int32(canvas.Offset.X)+10, int32(canvas.Offset.Y)+10, int32(canvas.Size.X), int32(canvas.Size.Y), raylib.Fade(raylib.Black, 0.3))
canvas.Draw()
2024-01-25 15:57:10 +00:00
2024-01-25 20:02:20 +00:00
raylib.BeginScissorMode(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y))
2024-01-25 15:57:10 +00:00
currentStroke.Draw(raylib.NewVector2(0, 0))
2024-01-25 20:02:20 +00:00
raylib.EndScissorMode()
2024-01-25 15:57:10 +00:00
if drawing {
2024-01-25 20:02:20 +00:00
raylib.DrawRectangleLines(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y), raylib.DarkGray)
2024-01-25 15:57:10 +00:00
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
} else {
2024-01-25 20:02:20 +00:00
raylib.DrawRectangleLines(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y), raylib.Gray)
2024-01-25 15:57:10 +00:00
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
}
}
raylib.EndMode2D()
// UI stuff
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight)
{
raylib.DrawRectangle(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Fade(raylib.White, 0.7))
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+10), 10, 25, 25), gui.IconText(gui.ICON_CROSS, "")) {
menu = StateFileMenu
}
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+20+25), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_SAVE, "")) {
2024-01-25 20:02:20 +00:00
canvas.Save()
2024-01-25 15:57:10 +00:00
}
if gui.Button(raylib.NewRectangle(float32(WindowWidth-70), 10, 25, 25), gui.IconText(gui.ICON_UNDO, "")) {
2024-01-25 20:02:20 +00:00
canvas.Undo()
2024-01-25 15:57:10 +00:00
}
if gui.Button(raylib.NewRectangle(float32(WindowWidth-35), 10, 25, 25), gui.IconText(gui.ICON_REDO, "")) {
2024-01-25 20:02:20 +00:00
canvas.Redo()
2024-01-25 15:57:10 +00:00
}
colourPickerVal = gui.ColorPicker(raylib.NewRectangle(float32(sidePanelRelativeX+10), 45, sidePanelWidth-45, colourPickerHeight), "Color", colourPickerVal)
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 55+colourPickerHeight, 60, 20), "Brush Size")
brushSize = gui.Slider(raylib.NewRectangle(float32(sidePanelRelativeX+80), 55+colourPickerHeight, sidePanelWidth-90, 20), "", "", brushSize, 1, 100)
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 115+colourPickerHeight, 60, 20), "File Name")
2024-01-25 20:02:20 +00:00
if gui.TextBox(raylib.NewRectangle(float32(sidePanelRelativeX+80), 115+colourPickerHeight, sidePanelWidth-90, 20), &canvas.Name, 40, fileNameEditing) {
2024-01-25 15:57:10 +00:00
fileNameEditing = !fileNameEditing
}
}
raylib.EndScissorMode()
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Gray)
// Info
{
var text string
2024-01-25 20:02:20 +00:00
text = fmt.Sprintf("Strokes: %d | Points: %d", len(canvas.Strokes), len(currentStroke.Points))
2024-01-25 15:57:10 +00:00
gui.StatusBar(raylib.NewRectangle(0, float32(WindowHeight-20), 200, 20), text)
2024-01-25 20:02:20 +00:00
text = fmt.Sprintf("Canvas Size: %dx%d", int32(canvas.Size.X), int32(canvas.Size.Y))
2024-01-25 15:57:10 +00:00
gui.StatusBar(raylib.NewRectangle(199, float32(WindowHeight-20), 200, 20), text)
}
switch menu {
case StateFileMenu:
raylib.DrawRectangle(0, 0, WindowWidth, WindowHeight, raylib.Fade(raylib.Black, 0.5))
2024-01-25 20:02:20 +00:00
choice := gui.MessageBox(raylib.NewRectangle(float32(WindowWidth/2-200), float32(WindowHeight/2-100), 400, 200), "File", "This is a message box", "Ok")
2024-01-25 15:57:10 +00:00
if choice == 0 || choice == 1 {
menu = StateNone
}
default:
menu = StateNone
}
// Draw toasts
DrawToasts()
2024-01-08 13:29:40 +00:00
}
2024-01-25 15:57:10 +00:00
raylib.EndDrawing()
2024-01-08 13:29:40 +00:00
}
// QUIT
raylib.CloseAudioDevice()
raylib.CloseWindow()
// GOODBYE
}