This commit is contained in:
Michał 2024-02-09 14:16:18 +00:00
parent 72cc0b84d9
commit cb0b83e97b
4 changed files with 89 additions and 45 deletions

View file

@ -75,7 +75,7 @@ func (c *Canvas) Undo() {
c.Strokes = c.Strokes[:len(c.Strokes)-1] c.Strokes = c.Strokes[:len(c.Strokes)-1]
c.Refresh = true c.Refresh = true
AddToast("Undo") addToast("Undo")
} }
} }
@ -85,7 +85,7 @@ func (c *Canvas) Redo() {
c.UndoneStrokes = c.UndoneStrokes[:len(c.UndoneStrokes)-1] c.UndoneStrokes = c.UndoneStrokes[:len(c.UndoneStrokes)-1]
c.Refresh = true c.Refresh = true
AddToast("Redo") addToast("Redo")
} }
} }
@ -104,7 +104,7 @@ func (c *Canvas) Save() {
c.Name = strings.Trim(c.Name, " ") c.Name = strings.Trim(c.Name, " ")
if c.Name == "" { if c.Name == "" {
AddToast("Please enter a file name!") addToast("Please enter a file name!")
} else { } else {
image := raylib.LoadImageFromTexture(c.Target.Texture) image := raylib.LoadImageFromTexture(c.Target.Texture)
@ -113,7 +113,7 @@ func (c *Canvas) Save() {
raylib.ExportImage(*image, filepath.Join(dirUserData, c.Name+".png")) 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 c.UnsavedChanges = false

96
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"math"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -19,6 +20,7 @@ func main() {
raylib.SetWindowMinSize(int(applicationMinWindowWidth), int(applicationMinWindowHeight)) // Set a minimum window size raylib.SetWindowMinSize(int(applicationMinWindowWidth), int(applicationMinWindowHeight)) // Set a minimum window size
raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor()))) // Match monitor refresh rate raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor()))) // Match monitor refresh rate
raylib.SetExitKey(raylib.KeyNull) // disable exit key raylib.SetExitKey(raylib.KeyNull) // disable exit key
raylib.HideCursor() // Hide cursor
// Make sure both assets and userData directories exist // Make sure both assets and userData directories exist
if _, err := os.Stat(dirAssets); os.IsNotExist(err) { if _, err := os.Stat(dirAssets); os.IsNotExist(err) {
@ -77,41 +79,59 @@ func main() {
// INPUT // 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) { if raylib.IsKeyPressed(raylib.KeyF7) {
AddToast("This is a test toast") addToast("This is a test toast")
} }
if raylib.IsKeyPressed(raylib.KeyF8) { if raylib.IsKeyPressed(raylib.KeyF8) {
applicationShowDebugValues = !applicationShowDebugValues applicationShowDebugValues = !applicationShowDebugValues
} }
if raylib.IsKeyPressed(raylib.KeyF12) { if raylib.IsKeyPressed(raylib.KeyF12) {
AddToast("Screenshot saved!") addToast("Screenshot saved!")
} }
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && applicationState == StateNormal { switch newStrokeType {
if !raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(applicationWindowWidth-int32(toolPanelWidth)), 0, toolPanelWidth, float32(applicationWindowHeight))) && case toolPen:
raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) { if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && applicationState == StateNormal {
applicationState = StateDrawing if !raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(applicationWindowWidth-int32(toolPanelWidth)), 0, toolPanelWidth, float32(applicationWindowHeight))) &&
newPenStroke = penTool{ raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) {
Size: toolPanelBrushSize, applicationState = StateDrawing
Color: toolPanelColourPicker, newPenStroke = penTool{
Size: toolPanelBrushSize,
Color: toolPanelColourPicker,
}
} }
} }
}
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && applicationState == StateDrawing { if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && applicationState == StateDrawing {
if len(newPenStroke.Points) <= 1 { if len(newPenStroke.Points) <= 1 {
newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition()) newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition())
} else if raylib.Vector2Distance(newPenStroke.Points[len(newPenStroke.Points)-1], raylib.GetMousePosition()) > float32(newPenStrokeSafeZone) { } else if raylib.Vector2Distance(newPenStroke.Points[len(newPenStroke.Points)-1], raylib.GetMousePosition()) > float32(newStrokeSafeZone) {
newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition()) 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{}
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && newPenStroke.Points != nil { applicationState = StateNormal
canvas.AddStroke(newPenStroke.Render()) }
newPenStroke = penTool{} case toolNone:
applicationState = StateNormal fallthrough
default:
// yyeeeeet
} }
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) {
@ -125,7 +145,10 @@ func main() {
// UPDATE // UPDATE
{ {
canvas.Update() applicationRuntime += 1
if math.Mod(float64(applicationRuntime), 1) == 0 {
// ToDo: check cursor color contrast with background
}
if applicationState != StateNormal { if applicationState != StateNormal {
gui.Lock() gui.Lock()
@ -133,7 +156,8 @@ func main() {
gui.Unlock() gui.Unlock()
} }
UpdateToasts() canvas.Update()
updateToasts()
} }
// DRAW // DRAW
@ -200,9 +224,6 @@ func main() {
gui.StatusBar(raylib.NewRectangle(300, float32(applicationWindowHeight-20), 170, 20), text) 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 // Menus
switch applicationState { switch applicationState {
case StateFileMenu: case StateFileMenu:
@ -289,7 +310,7 @@ func main() {
if !canvas.UnsavedChanges { if !canvas.UnsavedChanges {
applicationState = StateNormal applicationState = StateNormal
shouldCreateNewCanvas = true shouldCreateNewCanvas = true
AddToast("Created New Canvas: " + canvas.Name) addToast("Created New Canvas: " + canvas.Name)
break break
} }
@ -304,7 +325,7 @@ func main() {
} else if choice == 2 { } else if choice == 2 {
applicationState = StateNormal applicationState = StateNormal
shouldCreateNewCanvas = true shouldCreateNewCanvas = true
AddToast("Created New Canvas: " + canvas.Name) addToast("Created New Canvas: " + canvas.Name)
} }
case StateWindowWantsToDie: case StateWindowWantsToDie:
if !canvas.UnsavedChanges { if !canvas.UnsavedChanges {
@ -314,7 +335,7 @@ func main() {
if !canvas.UnsavedChanges { if !canvas.UnsavedChanges {
applicationState = StateNormal applicationState = StateNormal
shouldCreateNewCanvas = true shouldCreateNewCanvas = true
AddToast("Created New Canvas: " + canvas.Name) addToast("Created New Canvas: " + canvas.Name)
break break
} }
gui.Unlock() gui.Unlock()
@ -330,7 +351,20 @@ func main() {
default: 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() raylib.EndDrawing()
} }

View file

@ -13,31 +13,32 @@ type toast struct {
} }
var ( var (
toasts = []toast{} toasts []toast
toastDimHeight = float32(0) toastDimHeight = float32(0)
) )
func AddToast(text string) { func addToast(text string) {
t := toast{Text: text, Age: time.Now(), MaxAge: 1 * time.Second} t := toast{Text: text, Age: time.Now(), MaxAge: 1 * time.Second}
toasts = append(toasts, t) toasts = append(toasts, t)
} }
func UpdateToasts() { func updateToasts() {
if len(toasts) != 0 { if len(toasts) != 0 {
toastDimHeight = raylib.Lerp(toastDimHeight, float32(20*len(toasts))+10, 0.1) toastDimHeight = raylib.Lerp(toastDimHeight, float32(20*len(toasts))+10, 0.1)
} else { } else {
toastDimHeight = raylib.Lerp(toastDimHeight, 0, 0.1) toastDimHeight = raylib.Lerp(toastDimHeight, 0, 0.1)
} }
for i, t := range toasts { var t []toast
if time.Since(t.Age) > t.MaxAge { for i := range toasts {
toasts = append(toasts[:i], toasts[i+1:]...) if time.Since(toasts[i].Age) < toasts[i].MaxAge {
i -= 1 t = append(t, toasts[i])
} }
} }
toasts = t
} }
func DrawToasts() { func drawToasts() {
raylib.BeginScissorMode(0, 0, applicationWindowWidth, int32(toastDimHeight)) raylib.BeginScissorMode(0, 0, applicationWindowWidth, int32(toastDimHeight))
{ {
raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5)) raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5))

13
vars.go
View file

@ -23,17 +23,26 @@ const (
StateWindowWantsToDie StateWindowWantsToDie
) )
const (
toolNone = iota
toolPen
)
var ( var (
applicationState = StateNormal applicationState = StateNormal
applicationShouldQuit = false applicationShouldQuit = false
applicationShowDebugValues = false applicationShowDebugValues = false
applicationWindowWidth = applicationMinWindowWidth applicationWindowWidth = applicationMinWindowWidth
applicationWindowHeight = applicationMinWindowHeight applicationWindowHeight = applicationMinWindowHeight
applicationRuntime = float32(0)
) )
var ( var (
newPenStroke = penTool{} cursorColor = raylib.Black
newPenStrokeSafeZone = 1
newStrokeType = toolNone
newPenStroke = penTool{}
newStrokeSafeZone = 1
toolPanelWidth = float32(350) toolPanelWidth = float32(350)
toolPanelOffset = applicationWindowWidth - int32(toolPanelWidth) toolPanelOffset = applicationWindowWidth - int32(toolPanelWidth)