ColouringApp/main.go

329 lines
11 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-26 14:41:09 +00:00
"os"
"strconv"
gui "github.com/gen2brain/raylib-go/raygui"
raylib "github.com/gen2brain/raylib-go/raylib"
2024-01-08 13:29:40 +00:00
)
2024-01-25 20:02:20 +00:00
const (
WindowTitle = "Colouring App"
WindowMinWidth = int32(800)
WindowMinHeight = int32(600)
)
var (
WindowWidth = WindowMinWidth
WindowHeight = WindowMinHeight
)
const (
DirAssets = "./assets/"
DirUserData = "./userData/"
)
const (
StateNormal = iota
2024-01-25 20:02:20 +00:00
StateDrawing
StateFileMenu
)
var canvas *Canvas
2024-01-26 00:55:21 +00:00
2024-01-25 20:02:20 +00:00
func checkDirs() {
if _, err := os.Stat(DirAssets); os.IsNotExist(err) {
panic("Assets directory not found")
}
2024-01-25 20:02:20 +00:00
if _, err := os.Stat(DirUserData); os.IsNotExist(err) {
if err := os.Mkdir(DirUserData, 0755); err != nil {
panic("Could not create userData directory")
2024-01-25 20:02:20 +00:00
}
}
}
2024-01-08 13:29:40 +00:00
func main() {
checkDirs() // Make sure all the directories exist
2024-01-23 17:23:57 +00:00
raylib.SetConfigFlags(raylib.FlagWindowResizable)
// raylib.SetTraceLogLevel(raylib.LogTrace)
// raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
// raylib.SetConfigFlags(raylib.FlagVsyncHint)
// raylib.SetConfigFlags(raylib.FlagFullscreenMode)
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-26 00:55:21 +00:00
raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor())))
// 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
currentStroke = penTool{}
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
state = StateNormal
2024-01-25 20:02:20 +00:00
appShouldQuit = false
2024-01-26 00:55:21 +00:00
showCursor = true
showDebugStats = false
2024-01-25 15:57:10 +00:00
)
2024-01-26 14:41:09 +00:00
var (
createNewCanvas = true
newProjectName = "NewProject"
newProjectNameEditing = false
newCanvasWidth = 700
newCanvasWidthEditing = false
newCanvasHeight = 530
newCanvasHeightEditing = false
newCanvasBackgroundColor = raylib.White
)
2024-01-26 00:55:21 +00:00
// LOOP
2024-01-25 20:02:20 +00:00
for !appShouldQuit {
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)
}
2024-01-26 14:41:09 +00:00
// CREATE CANVAS
if createNewCanvas {
canvasBackground := NewBackground(raylib.NewVector2(float32(newCanvasWidth), float32(newCanvasHeight)), newCanvasBackgroundColor)
canvas = NewCanvas("NewProject", raylib.NewVector2(float32(newCanvasWidth), float32(newCanvasHeight)), raylib.NewVector2(15, 15), canvasBackground)
createNewCanvas = false
}
2024-01-25 15:57:10 +00:00
// INPUT
{
2024-01-26 11:29:29 +00:00
if raylib.IsKeyPressed(raylib.KeyF7) {
AddToast("This is a test toast")
}
2024-01-26 00:55:21 +00:00
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
2024-01-25 15:57:10 +00:00
currentStroke = penTool{
2024-01-25 20:02:20 +00:00
Size: brushSize,
2024-01-26 00:55:21 +00:00
Color: colourPickerVal,
2024-01-25 15:57:10 +00:00
}
}
}
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && state == StateDrawing {
2024-01-25 15:57:10 +00:00
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())
}
state = StateDrawing
2024-01-25 15:57:10 +00:00
}
2024-01-25 15:57:10 +00:00
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
2024-01-26 00:55:21 +00:00
canvas.AddStroke(currentStroke.Render())
2024-01-25 20:02:20 +00:00
currentStroke = penTool{}
state = StateNormal
2024-01-25 15:57:10 +00:00
}
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
{
UpdateToasts()
canvas.Update()
if state != StateNormal {
2024-01-25 15:57:10 +00:00
gui.SetState(gui.STATE_DISABLED)
} else {
gui.SetState(gui.STATE_NORMAL)
}
2024-01-26 00:55:21 +00:00
if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(WindowWidth-int32(sidePanelWidth)), 0, sidePanelWidth, float32(WindowHeight))) {
showCursor = false
} else {
showCursor = true
}
2024-01-25 15:57:10 +00:00
}
// 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-26 00:55:21 +00:00
currentStroke.Draw()
2024-01-25 20:02:20 +00:00
raylib.EndScissorMode()
2024-01-25 15:57:10 +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.EndMode2D()
// UI stuff
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight)
{
raylib.DrawRectangle(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Fade(raylib.White, 0.7))
2024-01-26 11:29:29 +00:00
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+10), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_OPEN, "")) {
state = StateFileMenu
2024-01-25 15:57:10 +00:00
}
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
}
2024-01-26 11:29:29 +00:00
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Gray)
2024-01-25 15:57:10 +00:00
}
raylib.EndScissorMode()
// Info
2024-01-26 00:55:21 +00:00
if showDebugStats {
2024-01-25 15:57:10 +00:00
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-26 00:55:21 +00:00
gui.StatusBar(raylib.NewRectangle(0, float32(WindowHeight-20), 150, 20), text)
2024-01-25 15:57:10 +00:00
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-26 00:55:21 +00:00
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)
2024-01-25 15:57:10 +00:00
}
2024-01-26 11:29:29 +00:00
// Cursor
if showCursor {
}
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
switch state {
2024-01-25 15:57:10 +00:00
case StateFileMenu:
gui.SetState(gui.STATE_NORMAL)
2024-01-25 15:57:10 +00:00
raylib.DrawRectangle(0, 0, WindowWidth, WindowHeight, raylib.Fade(raylib.Black, 0.5))
2024-01-26 11:29:29 +00:00
2024-01-26 14:41:09 +00:00
windowPos := raylib.NewRectangle(float32((WindowWidth/2)-200), float32((WindowHeight/2)-200), 400, 400)
2024-01-26 11:29:29 +00:00
if gui.WindowBox(windowPos, "Open or New File") {
state = StateNormal
}
// Magic numbers
raylib.BeginScissorMode(int32(windowPos.X)+1, int32(windowPos.Y)+24, int32(windowPos.Width)-2, int32(windowPos.Height)-25)
2024-01-26 14:41:09 +00:00
gui.GroupBox(raylib.NewRectangle(windowPos.X+11, windowPos.Y+34, windowPos.Width-22, 40), "Open Existing")
{
if gui.Button(raylib.NewRectangle(windowPos.X+21, windowPos.Y+44, 120, 20), "Maned Wolf") {
loadedImage := raylib.LoadImage(DirAssets + "manedWolf.jpg")
{
raylib.ImageFlipHorizontal(loadedImage)
raylib.ImageRotate(loadedImage, 180)
}
canvas = NewCanvas("NewProject", raylib.NewVector2(float32(loadedImage.Width), float32(loadedImage.Height)), raylib.NewVector2(15, 15), raylib.LoadTextureFromImage(loadedImage))
2024-01-26 11:29:29 +00:00
2024-01-26 14:41:09 +00:00
raylib.UnloadImage(loadedImage)
state = StateNormal
2024-01-26 11:29:29 +00:00
2024-01-26 14:41:09 +00:00
AddToast("Loaded Maned Wolf")
}
2024-01-25 15:57:10 +00:00
}
2024-01-26 14:41:09 +00:00
gui.GroupBox(raylib.NewRectangle(windowPos.X+11, windowPos.Y+84, windowPos.Width-22, 200), "Create New")
{
gui.Label(raylib.NewRectangle(windowPos.X+21, windowPos.Y+94, 100, 20), "File Name")
if gui.TextBox(raylib.NewRectangle(windowPos.X+131, windowPos.Y+94, windowPos.Width-152, 20), &newProjectName, 40, newProjectNameEditing) {
newProjectNameEditing = !newProjectNameEditing
}
gui.Label(raylib.NewRectangle(windowPos.X+21, windowPos.Y+124, 100, 20), "Canvas Size")
{
var err error
lastWidth := newCanvasWidth
width := fmt.Sprintf("%d", newCanvasWidth)
if gui.TextBox(raylib.NewRectangle(windowPos.X+131, windowPos.Y+124, windowPos.Width-152, 20), &width, 40, newCanvasWidthEditing) {
newCanvasWidthEditing = !newCanvasWidthEditing
}
if newCanvasWidth, err = strconv.Atoi(width); err != nil {
newCanvasWidth = lastWidth
}
lastHeight := newCanvasHeight
height := fmt.Sprintf("%d", newCanvasHeight)
if gui.TextBox(raylib.NewRectangle(windowPos.X+131, windowPos.Y+154, windowPos.Width-152, 20), &height, 40, newCanvasHeightEditing) {
newCanvasHeightEditing = !newCanvasHeightEditing
}
if newCanvasHeight, err = strconv.Atoi(height); err != nil {
newCanvasHeight = lastHeight
}
}
if gui.Button(raylib.NewRectangle(windowPos.X+21, windowPos.Y+184, 120, 20), "Create") {
state = StateNormal
createNewCanvas = true
AddToast("Created New Canvas")
}
}
2024-01-26 11:29:29 +00:00
raylib.EndScissorMode()
default:
2024-01-25 15:57:10 +00:00
}
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
}
// GOODBYE
raylib.CloseWindow()
2024-01-08 13:29:40 +00:00
}