From cb0b83e97b2c5bd156de1128ec2e8a1fa2cb86aa Mon Sep 17 00:00:00 2001 From: Fluffy <michal-gdula@protonmail.com> Date: Fri, 9 Feb 2024 14:16:18 +0000 Subject: [PATCH] Oon oon --- canvas.go | 8 ++--- main.go | 96 +++++++++++++++++++++++++++++++++++++------------------ toast.go | 17 +++++----- vars.go | 13 ++++++-- 4 files changed, 89 insertions(+), 45 deletions(-) diff --git a/canvas.go b/canvas.go index 746c6c6..7014cf9 100644 --- a/canvas.go +++ b/canvas.go @@ -75,7 +75,7 @@ func (c *Canvas) Undo() { c.Strokes = c.Strokes[:len(c.Strokes)-1] c.Refresh = true - AddToast("Undo") + addToast("Undo") } } @@ -85,7 +85,7 @@ func (c *Canvas) Redo() { c.UndoneStrokes = c.UndoneStrokes[:len(c.UndoneStrokes)-1] c.Refresh = true - AddToast("Redo") + addToast("Redo") } } @@ -104,7 +104,7 @@ func (c *Canvas) Save() { c.Name = strings.Trim(c.Name, " ") if c.Name == "" { - AddToast("Please enter a file name!") + addToast("Please enter a file name!") } else { image := raylib.LoadImageFromTexture(c.Target.Texture) @@ -113,7 +113,7 @@ func (c *Canvas) Save() { raylib.ExportImage(*image, filepath.Join(dirUserData, c.Name+".png")) - AddToast("Drawing saved as " + c.Name + ".png") + addToast("Drawing saved as " + c.Name + ".png") } c.UnsavedChanges = false diff --git a/main.go b/main.go index a1dde67..a1e9589 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "math" "os" "path/filepath" "strconv" @@ -19,6 +20,7 @@ func main() { raylib.SetWindowMinSize(int(applicationMinWindowWidth), int(applicationMinWindowHeight)) // Set a minimum window size raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor()))) // Match monitor refresh rate raylib.SetExitKey(raylib.KeyNull) // disable exit key + raylib.HideCursor() // Hide cursor // Make sure both assets and userData directories exist if _, err := os.Stat(dirAssets); os.IsNotExist(err) { @@ -77,41 +79,59 @@ func main() { // INPUT { + if applicationState == StateNormal { + if raylib.IsKeyPressed(raylib.KeyF1) { + newStrokeType = toolNone + addToast("Tool: None") + } + if raylib.IsKeyPressed(raylib.KeyF2) { + newStrokeType = toolPen + addToast("Tool: Pen") + } + } + if raylib.IsKeyPressed(raylib.KeyF7) { - AddToast("This is a test toast") + addToast("This is a test toast") } if raylib.IsKeyPressed(raylib.KeyF8) { applicationShowDebugValues = !applicationShowDebugValues } if raylib.IsKeyPressed(raylib.KeyF12) { - AddToast("Screenshot saved!") + addToast("Screenshot saved!") } - if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && applicationState == StateNormal { - if !raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(applicationWindowWidth-int32(toolPanelWidth)), 0, toolPanelWidth, float32(applicationWindowHeight))) && - raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) { - applicationState = StateDrawing - newPenStroke = penTool{ - Size: toolPanelBrushSize, - Color: toolPanelColourPicker, + switch newStrokeType { + case toolPen: + if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && applicationState == StateNormal { + if !raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(applicationWindowWidth-int32(toolPanelWidth)), 0, toolPanelWidth, float32(applicationWindowHeight))) && + raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) { + applicationState = StateDrawing + newPenStroke = penTool{ + Size: toolPanelBrushSize, + Color: toolPanelColourPicker, + } } } - } - if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && applicationState == StateDrawing { - if len(newPenStroke.Points) <= 1 { - newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition()) - } else if raylib.Vector2Distance(newPenStroke.Points[len(newPenStroke.Points)-1], raylib.GetMousePosition()) > float32(newPenStrokeSafeZone) { - newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition()) + if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && applicationState == StateDrawing { + if len(newPenStroke.Points) <= 1 { + newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition()) + } else if raylib.Vector2Distance(newPenStroke.Points[len(newPenStroke.Points)-1], raylib.GetMousePosition()) > float32(newStrokeSafeZone) { + newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition()) + } + + applicationState = StateDrawing } - applicationState = StateDrawing - } - - if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && newPenStroke.Points != nil { - canvas.AddStroke(newPenStroke.Render()) - newPenStroke = penTool{} - applicationState = StateNormal + if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && newPenStroke.Points != nil { + canvas.AddStroke(newPenStroke.Render()) + newPenStroke = penTool{} + applicationState = StateNormal + } + case toolNone: + fallthrough + default: + // yyeeeeet } if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyDown(raylib.KeyLeftShift) && raylib.IsKeyPressed(raylib.KeyZ) { @@ -125,7 +145,10 @@ func main() { // UPDATE { - canvas.Update() + applicationRuntime += 1 + if math.Mod(float64(applicationRuntime), 1) == 0 { + // ToDo: check cursor color contrast with background + } if applicationState != StateNormal { gui.Lock() @@ -133,7 +156,8 @@ func main() { gui.Unlock() } - UpdateToasts() + canvas.Update() + updateToasts() } // DRAW @@ -200,9 +224,6 @@ func main() { gui.StatusBar(raylib.NewRectangle(300, float32(applicationWindowHeight-20), 170, 20), text) } - // Cursor - raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), toolPanelBrushSize/2, raylib.Black) - // Menus switch applicationState { case StateFileMenu: @@ -289,7 +310,7 @@ func main() { if !canvas.UnsavedChanges { applicationState = StateNormal shouldCreateNewCanvas = true - AddToast("Created New Canvas: " + canvas.Name) + addToast("Created New Canvas: " + canvas.Name) break } @@ -304,7 +325,7 @@ func main() { } else if choice == 2 { applicationState = StateNormal shouldCreateNewCanvas = true - AddToast("Created New Canvas: " + canvas.Name) + addToast("Created New Canvas: " + canvas.Name) } case StateWindowWantsToDie: if !canvas.UnsavedChanges { @@ -314,7 +335,7 @@ func main() { if !canvas.UnsavedChanges { applicationState = StateNormal shouldCreateNewCanvas = true - AddToast("Created New Canvas: " + canvas.Name) + addToast("Created New Canvas: " + canvas.Name) break } gui.Unlock() @@ -330,7 +351,20 @@ func main() { default: } - DrawToasts() + // Cursor + switch newStrokeType { + case toolNone: + raylib.DrawTriangleLines( + raylib.NewVector2(raylib.GetMousePosition().X, raylib.GetMousePosition().Y), + raylib.NewVector2(raylib.GetMousePosition().X+10, raylib.GetMousePosition().Y+10), + raylib.NewVector2(raylib.GetMousePosition().X, raylib.GetMousePosition().Y+14), + cursorColor, + ) + case toolPen: + raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), toolPanelBrushSize/2, cursorColor) + } + + drawToasts() } raylib.EndDrawing() } diff --git a/toast.go b/toast.go index dcb6cad..a675c35 100644 --- a/toast.go +++ b/toast.go @@ -13,31 +13,32 @@ type toast struct { } var ( - toasts = []toast{} + toasts []toast toastDimHeight = float32(0) ) -func AddToast(text string) { +func addToast(text string) { t := toast{Text: text, Age: time.Now(), MaxAge: 1 * time.Second} toasts = append(toasts, t) } -func UpdateToasts() { +func updateToasts() { if len(toasts) != 0 { toastDimHeight = raylib.Lerp(toastDimHeight, float32(20*len(toasts))+10, 0.1) } else { toastDimHeight = raylib.Lerp(toastDimHeight, 0, 0.1) } - for i, t := range toasts { - if time.Since(t.Age) > t.MaxAge { - toasts = append(toasts[:i], toasts[i+1:]...) - i -= 1 + var t []toast + for i := range toasts { + if time.Since(toasts[i].Age) < toasts[i].MaxAge { + t = append(t, toasts[i]) } } + toasts = t } -func DrawToasts() { +func drawToasts() { raylib.BeginScissorMode(0, 0, applicationWindowWidth, int32(toastDimHeight)) { raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5)) diff --git a/vars.go b/vars.go index be55bd0..a65ed7a 100644 --- a/vars.go +++ b/vars.go @@ -23,17 +23,26 @@ const ( StateWindowWantsToDie ) +const ( + toolNone = iota + toolPen +) + var ( applicationState = StateNormal applicationShouldQuit = false applicationShowDebugValues = false applicationWindowWidth = applicationMinWindowWidth applicationWindowHeight = applicationMinWindowHeight + applicationRuntime = float32(0) ) var ( - newPenStroke = penTool{} - newPenStrokeSafeZone = 1 + cursorColor = raylib.Black + + newStrokeType = toolNone + newPenStroke = penTool{} + newStrokeSafeZone = 1 toolPanelWidth = float32(350) toolPanelOffset = applicationWindowWidth - int32(toolPanelWidth)