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.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

96
main.go
View file

@ -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()
}

View file

@ -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))

13
vars.go
View file

@ -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)