Finally, the code is clean enough to add more stuff

This commit is contained in:
Michał 2024-01-25 20:29:32 +00:00
parent d0ebc1ee54
commit 00c449abff
3 changed files with 46 additions and 54 deletions

View file

@ -5,13 +5,17 @@ import (
) )
type Canvas struct { type Canvas struct {
Name string Name string
Size raylib.Vector2
Offset raylib.Vector2 Size raylib.Vector2
Target raylib.RenderTexture2D Offset raylib.Vector2
Target raylib.RenderTexture2D
Strokes []penTool Strokes []penTool
UndoneStrokes []penTool UndoneStrokes []penTool
Refresh bool
Refresh bool
} }
func (c *Canvas) Update() { func (c *Canvas) Update() {

81
main.go
View file

@ -26,30 +26,32 @@ const (
) )
const ( const (
StateNone = iota StateNormal = iota
StateDrawing StateDrawing
StateFileMenu StateFileMenu
) )
func checkDirs() { func checkDirs() {
if _, err := os.Stat(DirAssets); os.IsNotExist(err) {
panic("Assets directory not found")
}
if _, err := os.Stat(DirUserData); os.IsNotExist(err) { if _, err := os.Stat(DirUserData); os.IsNotExist(err) {
err := os.Mkdir(DirUserData, 0755) if err := os.Mkdir(DirUserData, 0755); err != nil {
if err != nil { panic("Could not create userData directory")
panic(err)
} }
} }
} }
func main() { func main() {
checkDirs() // Make sure all the directories exist
raylib.SetConfigFlags(raylib.FlagWindowResizable) raylib.SetConfigFlags(raylib.FlagWindowResizable)
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.InitAudioDevice()
raylib.SetTargetFPS(WindowFPS) raylib.SetTargetFPS(WindowFPS)
//raylib.SetExitKey(0) // disable exit key //raylib.SetExitKey(0) // disable exit key
@ -58,7 +60,6 @@ func main() {
canvas = NewCanvas("NewProject", raylib.NewVector2(600, 530), raylib.NewVector2(15, 15)) canvas = NewCanvas("NewProject", raylib.NewVector2(600, 530), raylib.NewVector2(15, 15))
currentStroke = penTool{} currentStroke = penTool{}
drawing = false
sidePanelWidth = float32(350) sidePanelWidth = float32(350)
sidePanelRelativeX = WindowWidth - int32(sidePanelWidth) sidePanelRelativeX = WindowWidth - int32(sidePanelWidth)
@ -70,18 +71,13 @@ func main() {
fileNameEditing = false fileNameEditing = false
menu = StateNone state = StateNormal
appShouldQuit = false appShouldQuit = false
) )
// check if userData exists // LOOP
checkDirs()
for !appShouldQuit { for !appShouldQuit {
// LOOP
appShouldQuit = raylib.WindowShouldClose() appShouldQuit = raylib.WindowShouldClose()
if raylib.IsWindowResized() { if raylib.IsWindowResized() {
WindowWidth = int32(raylib.GetScreenWidth()) WindowWidth = int32(raylib.GetScreenWidth())
WindowHeight = int32(raylib.GetScreenHeight()) WindowHeight = int32(raylib.GetScreenHeight())
@ -90,22 +86,18 @@ func main() {
// INPUT // INPUT
{ {
if raylib.IsKeyPressed(raylib.KeyF8) { if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && state == StateNormal {
AddToast("This is a toast message!") 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
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(WindowWidth-int32(sidePanelWidth)), 0, sidePanelWidth, float32(WindowHeight))) {
drawing = false
} else if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) {
drawing = true
currentStroke = penTool{ currentStroke = penTool{
Color: colourPickerVal, Color: colourPickerVal,
Size: brushSize, Size: brushSize,
} }
} }
} }
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && drawing {
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && state == StateDrawing {
var safeZone float32 = 5 var safeZone float32 = 5
if len(currentStroke.Points) <= 1 { if len(currentStroke.Points) <= 1 {
@ -113,14 +105,17 @@ func main() {
} else if raylib.Vector2Distance(currentStroke.Points[len(currentStroke.Points)-1], raylib.GetMousePosition()) > safeZone { } else if raylib.Vector2Distance(currentStroke.Points[len(currentStroke.Points)-1], raylib.GetMousePosition()) > safeZone {
currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition()) currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition())
} }
state = StateDrawing
} }
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil { if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
canvas.Strokes = append(canvas.Strokes, currentStroke) canvas.Strokes = append(canvas.Strokes, currentStroke)
canvas.UndoneStrokes = []penTool{} canvas.UndoneStrokes = []penTool{}
canvas.Refresh = true canvas.Refresh = true
currentStroke = penTool{} currentStroke = penTool{}
drawing = false state = StateNormal
} }
if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyDown(raylib.KeyLeftShift) && raylib.IsKeyPressed(raylib.KeyZ) { if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyDown(raylib.KeyLeftShift) && raylib.IsKeyPressed(raylib.KeyZ) {
@ -134,15 +129,13 @@ func main() {
// UPDATE // UPDATE
{ {
if drawing { UpdateToasts()
canvas.Update()
if state != StateNormal {
gui.SetState(gui.STATE_DISABLED) gui.SetState(gui.STATE_DISABLED)
} else { } else {
gui.SetState(gui.STATE_NORMAL) gui.SetState(gui.STATE_NORMAL)
} }
canvas.Update()
UpdateToasts()
} }
// DRAW // DRAW
@ -161,13 +154,8 @@ func main() {
currentStroke.Draw(raylib.NewVector2(0, 0)) currentStroke.Draw(raylib.NewVector2(0, 0))
raylib.EndScissorMode() raylib.EndScissorMode()
if drawing { 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.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
} else {
raylib.DrawRectangleLines(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y), raylib.Gray)
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
}
} }
raylib.EndMode2D() raylib.EndMode2D()
@ -177,7 +165,7 @@ func main() {
raylib.DrawRectangle(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Fade(raylib.White, 0.7)) 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, "")) { if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+10), 10, 25, 25), gui.IconText(gui.ICON_CROSS, "")) {
menu = StateFileMenu state = StateFileMenu
} }
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+20+25), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_SAVE, "")) { if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+20+25), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_SAVE, "")) {
canvas.Save() canvas.Save()
@ -214,26 +202,21 @@ func main() {
gui.StatusBar(raylib.NewRectangle(199, float32(WindowHeight-20), 200, 20), text) gui.StatusBar(raylib.NewRectangle(199, float32(WindowHeight-20), 200, 20), text)
} }
switch menu { switch state {
case StateFileMenu: case StateFileMenu:
gui.SetState(gui.STATE_NORMAL)
raylib.DrawRectangle(0, 0, WindowWidth, WindowHeight, raylib.Fade(raylib.Black, 0.5)) raylib.DrawRectangle(0, 0, WindowWidth, WindowHeight, raylib.Fade(raylib.Black, 0.5))
choice := gui.MessageBox(raylib.NewRectangle(float32(WindowWidth/2-200), float32(WindowHeight/2-100), 400, 200), "File", "This is a message box", "Ok") choice := gui.MessageBox(raylib.NewRectangle(float32(WindowWidth/2-200), float32(WindowHeight/2-100), 400, 200), "File", "This is a message box", "Ok")
if choice == 0 || choice == 1 { if choice == 0 || choice == 1 {
menu = StateNone state = StateNormal
} }
default:
menu = StateNone
} }
// Draw toasts
DrawToasts() DrawToasts()
} }
raylib.EndDrawing() raylib.EndDrawing()
} }
// QUIT
raylib.CloseAudioDevice()
raylib.CloseWindow()
// GOODBYE // GOODBYE
raylib.CloseWindow()
} }

View file

@ -18,4 +18,9 @@ func (p *penTool) Draw(offset raylib.Vector2) {
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 {
endPoint := raylib.Vector2Add(p.Points[len(p.Points)-1], offset)
raylib.DrawCircle(int32(endPoint.X), int32(endPoint.Y), p.Size/2, p.Color)
}
} }