mirror of
https://github.com/Fluffy-Bean/ColouringApp.git
synced 2025-05-14 07:32:16 +00:00
Fix crash on mouse press x.x
Clean up canvas and toast code
This commit is contained in:
parent
586872a5b7
commit
ea1322d9ae
3 changed files with 65 additions and 81 deletions
14
canvas.go
14
canvas.go
|
@ -108,7 +108,7 @@ func (c *Canvas) Save() {
|
||||||
raylib.ImageRotate(image, 180)
|
raylib.ImageRotate(image, 180)
|
||||||
raylib.ImageFlipHorizontal(image)
|
raylib.ImageFlipHorizontal(image)
|
||||||
|
|
||||||
raylib.ExportImage(*image, DirUserData+c.Name+".png")
|
raylib.ExportImage(*image, dirUserData+c.Name+".png")
|
||||||
|
|
||||||
AddToast("Drawing saved as " + c.Name + ".png")
|
AddToast("Drawing saved as " + c.Name + ".png")
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ func NewCanvas(name string, size, offset raylib.Vector2, background raylib.Textu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBackground(size raylib.Vector2, color raylib.Color) raylib.Texture2D {
|
func NewBackgroundColour(size raylib.Vector2, color raylib.Color) raylib.Texture2D {
|
||||||
texture := raylib.LoadRenderTexture(int32(size.X), int32(size.Y))
|
texture := raylib.LoadRenderTexture(int32(size.X), int32(size.Y))
|
||||||
|
|
||||||
fmt.Println(size)
|
fmt.Println(size)
|
||||||
|
@ -138,3 +138,13 @@ func NewBackground(size raylib.Vector2, color raylib.Color) raylib.Texture2D {
|
||||||
|
|
||||||
return texture.Texture
|
return texture.Texture
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewBackgroundImage(pathToImage string) raylib.Texture2D {
|
||||||
|
loadedImage := raylib.LoadImage(pathToImage)
|
||||||
|
|
||||||
|
// For some reason Images are flipped horizontally and rotated 180 degrees, so we need to undo that...
|
||||||
|
raylib.ImageFlipHorizontal(loadedImage)
|
||||||
|
raylib.ImageRotate(loadedImage, 180)
|
||||||
|
|
||||||
|
return raylib.LoadTextureFromImage(loadedImage)
|
||||||
|
}
|
||||||
|
|
89
main.go
89
main.go
|
@ -18,11 +18,9 @@ const (
|
||||||
defaultProjectName = "NewProject"
|
defaultProjectName = "NewProject"
|
||||||
defaultProjectWidth = 700
|
defaultProjectWidth = 700
|
||||||
defaultProjectHeight = 530
|
defaultProjectHeight = 530
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
dirAssets = "./assets/"
|
||||||
DirAssets = "./assets/"
|
dirUserData = "./userData/"
|
||||||
DirUserData = "./userData/"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -79,38 +77,24 @@ func main() {
|
||||||
raylib.InitWindow(applicationWindowWidth, applicationWindowHeight, applicationTitle)
|
raylib.InitWindow(applicationWindowWidth, applicationWindowHeight, applicationTitle)
|
||||||
|
|
||||||
raylib.SetWindowMinSize(int(applicationMinWindowWidth), int(applicationMinWindowHeight))
|
raylib.SetWindowMinSize(int(applicationMinWindowWidth), int(applicationMinWindowHeight))
|
||||||
raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor())))
|
raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor()))) // Set our application to run at the refresh rate of the monitor
|
||||||
raylib.SetExitKey(0) // disable exit key
|
raylib.SetExitKey(0) // disable exit key
|
||||||
|
|
||||||
if _, err := os.Stat(DirAssets); os.IsNotExist(err) {
|
// Make sure both assets and userData directories exist
|
||||||
|
if _, err := os.Stat(dirAssets); os.IsNotExist(err) {
|
||||||
panic("Assets directory not found")
|
panic("Assets directory not found")
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(DirUserData); os.IsNotExist(err) {
|
if _, err := os.Stat(dirUserData); os.IsNotExist(err) {
|
||||||
if err := os.Mkdir(DirUserData, 0755); err != nil {
|
if err := os.Mkdir(dirUserData, 0755); err != nil {
|
||||||
panic("Could not create userData directory")
|
panic("Could not create userData directory")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load all user data projects
|
||||||
var userDataProjects []string
|
var userDataProjects []string
|
||||||
{
|
if files, err := os.ReadDir(dirUserData); err == nil {
|
||||||
f, err := os.Open(DirUserData)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
err := f.Close()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
files, err := f.Readdir(-1)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.Mode().IsRegular() {
|
if strings.HasSuffix(file.Name(), ".png") {
|
||||||
userDataProjects = append(userDataProjects, file.Name())
|
userDataProjects = append(userDataProjects, file.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,6 +102,7 @@ func main() {
|
||||||
|
|
||||||
// LOOP
|
// LOOP
|
||||||
for !applicationShouldQuit {
|
for !applicationShouldQuit {
|
||||||
|
// Update default loop values
|
||||||
applicationShouldQuit = raylib.WindowShouldClose()
|
applicationShouldQuit = raylib.WindowShouldClose()
|
||||||
if raylib.IsWindowResized() {
|
if raylib.IsWindowResized() {
|
||||||
applicationWindowWidth = int32(raylib.GetScreenWidth())
|
applicationWindowWidth = int32(raylib.GetScreenWidth())
|
||||||
|
@ -125,26 +110,21 @@ func main() {
|
||||||
toolPanelOffset = applicationWindowWidth - int32(toolPanelWidth)
|
toolPanelOffset = applicationWindowWidth - int32(toolPanelWidth)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CREATE CANVAS
|
// Create new canvas if needed
|
||||||
if shouldCreateNewCanvas {
|
if shouldCreateNewCanvas {
|
||||||
var canvasBackground raylib.Texture2D
|
var canvasBackground raylib.Texture2D
|
||||||
|
|
||||||
if newCanvasImagePath != "" {
|
if newCanvasImagePath != "" {
|
||||||
// For some reason Images are flipped horizontally and rotated 180 degrees, so we need to undo that...
|
canvasBackground = NewBackgroundImage(newCanvasImagePath)
|
||||||
loadedImage := raylib.LoadImage(newCanvasImagePath)
|
newCanvasWidth = int(canvasBackground.Width)
|
||||||
raylib.ImageFlipHorizontal(loadedImage)
|
newCanvasHeight = int(canvasBackground.Height)
|
||||||
raylib.ImageRotate(loadedImage, 180)
|
|
||||||
|
|
||||||
canvasBackground = raylib.LoadTextureFromImage(loadedImage)
|
|
||||||
|
|
||||||
newCanvasWidth = int(loadedImage.Width)
|
|
||||||
newCanvasHeight = int(loadedImage.Height)
|
|
||||||
} else {
|
} else {
|
||||||
canvasBackground = NewBackground(raylib.NewVector2(float32(newCanvasWidth), float32(newCanvasHeight)), newCanvasColor)
|
canvasBackground = NewBackgroundColour(raylib.NewVector2(float32(newCanvasWidth), float32(newCanvasHeight)), newCanvasColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas = NewCanvas(newCanvasName, raylib.NewVector2(float32(newCanvasWidth), float32(newCanvasHeight)), raylib.NewVector2(15, 15), canvasBackground)
|
canvas = NewCanvas(newCanvasName, raylib.NewVector2(float32(newCanvasWidth), float32(newCanvasHeight)), raylib.NewVector2(15, 15), canvasBackground)
|
||||||
|
|
||||||
|
// Reset all values
|
||||||
shouldCreateNewCanvas = false
|
shouldCreateNewCanvas = false
|
||||||
newCanvasName = defaultProjectName
|
newCanvasName = defaultProjectName
|
||||||
newCanvasWidth = defaultProjectWidth
|
newCanvasWidth = defaultProjectWidth
|
||||||
|
@ -177,11 +157,9 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && applicationState == StateDrawing {
|
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && applicationState == StateDrawing {
|
||||||
distanceToLastPoint := raylib.Vector2Distance(newPenStroke.Points[len(newPenStroke.Points)-1], raylib.GetMousePosition())
|
if len(newPenStroke.Points) <= 1 {
|
||||||
|
|
||||||
if distanceToLastPoint > float32(newPenStrokeSafeZone) {
|
|
||||||
newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition())
|
newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition())
|
||||||
} else if len(newPenStroke.Points) <= newPenStrokeSafeZone {
|
} else if raylib.Vector2Distance(newPenStroke.Points[len(newPenStroke.Points)-1], raylib.GetMousePosition()) > float32(newPenStrokeSafeZone) {
|
||||||
newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition())
|
newPenStroke.Points = append(newPenStroke.Points, raylib.GetMousePosition())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,13 +183,15 @@ func main() {
|
||||||
|
|
||||||
// UPDATE
|
// UPDATE
|
||||||
{
|
{
|
||||||
UpdateToasts()
|
|
||||||
canvas.Update()
|
canvas.Update()
|
||||||
|
|
||||||
if applicationState != StateNormal {
|
if applicationState != StateNormal {
|
||||||
gui.Lock()
|
gui.Lock()
|
||||||
} else {
|
} else {
|
||||||
gui.Unlock()
|
gui.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UpdateToasts()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DRAW
|
// DRAW
|
||||||
|
@ -220,7 +200,7 @@ func main() {
|
||||||
raylib.ClearBackground(raylib.White)
|
raylib.ClearBackground(raylib.White)
|
||||||
gui.Grid(raylib.NewRectangle(0, 0, float32(applicationWindowWidth), float32(applicationWindowHeight)), "", 30, 1, &raylib.Vector2{})
|
gui.Grid(raylib.NewRectangle(0, 0, float32(applicationWindowWidth), float32(applicationWindowHeight)), "", 30, 1, &raylib.Vector2{})
|
||||||
|
|
||||||
// Canvas stuff
|
// Canvas
|
||||||
{
|
{
|
||||||
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))
|
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()
|
canvas.Draw()
|
||||||
|
@ -232,7 +212,7 @@ func main() {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UI stuff
|
// Tool Panel
|
||||||
raylib.BeginScissorMode(toolPanelOffset, 0, int32(toolPanelWidth), applicationWindowHeight)
|
raylib.BeginScissorMode(toolPanelOffset, 0, int32(toolPanelWidth), applicationWindowHeight)
|
||||||
{
|
{
|
||||||
raylib.DrawRectangle(toolPanelOffset, 0, int32(toolPanelWidth), applicationWindowHeight, raylib.Fade(raylib.White, 0.9))
|
raylib.DrawRectangle(toolPanelOffset, 0, int32(toolPanelWidth), applicationWindowHeight, raylib.Fade(raylib.White, 0.9))
|
||||||
|
@ -264,7 +244,7 @@ func main() {
|
||||||
}
|
}
|
||||||
raylib.EndScissorMode()
|
raylib.EndScissorMode()
|
||||||
|
|
||||||
// Info
|
// Debug Values
|
||||||
if applicationShowDebugValues {
|
if applicationShowDebugValues {
|
||||||
var text string
|
var text string
|
||||||
|
|
||||||
|
@ -281,13 +261,12 @@ func main() {
|
||||||
// Cursor
|
// Cursor
|
||||||
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), toolPanelBrushSize/2, raylib.Black)
|
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), toolPanelBrushSize/2, raylib.Black)
|
||||||
|
|
||||||
|
// Menus
|
||||||
switch applicationState {
|
switch applicationState {
|
||||||
case StateFileMenu:
|
case StateFileMenu:
|
||||||
windowPos := raylib.NewRectangle(float32((applicationWindowWidth/2)-200), float32((applicationWindowHeight/2)-200), 400, 400)
|
|
||||||
|
|
||||||
raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5))
|
|
||||||
|
|
||||||
gui.Unlock()
|
gui.Unlock()
|
||||||
|
raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5))
|
||||||
|
windowPos := raylib.NewRectangle(float32((applicationWindowWidth/2)-200), float32((applicationWindowHeight/2)-200), 400, 400)
|
||||||
if gui.WindowBox(windowPos, "Open or New File") {
|
if gui.WindowBox(windowPos, "Open or New File") {
|
||||||
applicationState = StateNormal
|
applicationState = StateNormal
|
||||||
}
|
}
|
||||||
|
@ -350,14 +329,14 @@ func main() {
|
||||||
gui.GroupBox(raylib.NewRectangle(windowPos.X+11, windowPos.Y+244, windowPos.Width-22, float32(len(userDataProjects)*20)+10), "Open Existing")
|
gui.GroupBox(raylib.NewRectangle(windowPos.X+11, windowPos.Y+244, windowPos.Width-22, float32(len(userDataProjects)*20)+10), "Open Existing")
|
||||||
{
|
{
|
||||||
if gui.Button(raylib.NewRectangle(windowPos.X+21, windowPos.Y+254, windowPos.Width-42, 20), "Maned Wolf") {
|
if gui.Button(raylib.NewRectangle(windowPos.X+21, windowPos.Y+254, windowPos.Width-42, 20), "Maned Wolf") {
|
||||||
newCanvasImagePath = DirAssets + "manedWolf.jpg"
|
newCanvasImagePath = dirAssets + "manedWolf.jpg"
|
||||||
newCanvasName = "ManedWolf"
|
newCanvasName = "ManedWolf"
|
||||||
applicationState = StateNewCanvas
|
applicationState = StateNewCanvas
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(userDataProjects); i++ {
|
for i := 0; i < len(userDataProjects); i++ {
|
||||||
if gui.Button(raylib.NewRectangle(windowPos.X+21, windowPos.Y+274+float32(i*20), windowPos.Width-42, 20), userDataProjects[i]) {
|
if gui.Button(raylib.NewRectangle(windowPos.X+21, windowPos.Y+274+float32(i*20), windowPos.Width-42, 20), userDataProjects[i]) {
|
||||||
newCanvasImagePath = DirUserData + userDataProjects[i]
|
newCanvasImagePath = dirUserData + userDataProjects[i]
|
||||||
splitName := strings.Split(userDataProjects[i], ".")
|
splitName := strings.Split(userDataProjects[i], ".")
|
||||||
newCanvasName = splitName[:len(splitName)-1][0]
|
newCanvasName = splitName[:len(splitName)-1][0]
|
||||||
applicationState = StateNewCanvas
|
applicationState = StateNewCanvas
|
||||||
|
@ -367,11 +346,9 @@ func main() {
|
||||||
|
|
||||||
raylib.EndScissorMode()
|
raylib.EndScissorMode()
|
||||||
case StateNewCanvas:
|
case StateNewCanvas:
|
||||||
windowPos := raylib.NewRectangle(float32((applicationWindowWidth/2)-200), float32((applicationWindowHeight/2)-75), 400, 150)
|
|
||||||
|
|
||||||
raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5))
|
|
||||||
|
|
||||||
gui.Unlock()
|
gui.Unlock()
|
||||||
|
raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5))
|
||||||
|
windowPos := raylib.NewRectangle(float32((applicationWindowWidth/2)-200), float32((applicationWindowHeight/2)-75), 400, 150)
|
||||||
choice := gui.MessageBox(windowPos, "New Canvas", "Are you sure you want to create a new canvas?", "No;Yes")
|
choice := gui.MessageBox(windowPos, "New Canvas", "Are you sure you want to create a new canvas?", "No;Yes")
|
||||||
|
|
||||||
if choice == 0 || choice == 1 {
|
if choice == 0 || choice == 1 {
|
||||||
|
|
43
toast.go
43
toast.go
|
@ -1,40 +1,36 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
toastMaxAge = 1 * time.Second
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
toasts = []toast{}
|
|
||||||
toastHeight = float32(0)
|
|
||||||
)
|
|
||||||
|
|
||||||
type toast struct {
|
type toast struct {
|
||||||
Text string
|
Text string
|
||||||
Age time.Time
|
Age time.Time
|
||||||
|
MaxAge time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
toasts = []toast{}
|
||||||
|
toastDimHeight = float32(0)
|
||||||
|
)
|
||||||
|
|
||||||
func AddToast(text string) {
|
func AddToast(text string) {
|
||||||
toasts = append(toasts, toast{Text: text, Age: time.Now()})
|
t := toast{Text: text, Age: time.Now(), MaxAge: 1 * time.Second}
|
||||||
fmt.Printf("Added toast: '%s'\n", text)
|
toasts = append(toasts, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateToasts() {
|
func UpdateToasts() {
|
||||||
if len(toasts) != 0 {
|
if len(toasts) != 0 {
|
||||||
toastHeight = raylib.Lerp(toastHeight, float32(20*len(toasts))+10, 0.1)
|
toastDimHeight = raylib.Lerp(toastDimHeight, float32(20*len(toasts))+10, 0.1)
|
||||||
} else {
|
} else {
|
||||||
toastHeight = raylib.Lerp(toastHeight, 0, 0.1)
|
toastDimHeight = raylib.Lerp(toastDimHeight, 0, 0.1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(toasts); i += 1 {
|
for i, t := range toasts {
|
||||||
if time.Since(toasts[i].Age) > toastMaxAge {
|
if time.Since(t.Age) > t.MaxAge {
|
||||||
toasts = append(toasts[:i], toasts[i+1:]...)
|
toasts = append(toasts[:i], toasts[i+1:]...)
|
||||||
i -= 1
|
i -= 1
|
||||||
}
|
}
|
||||||
|
@ -42,11 +38,12 @@ func UpdateToasts() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DrawToasts() {
|
func DrawToasts() {
|
||||||
raylib.BeginScissorMode(0, 0, applicationWindowWidth, int32(toastHeight))
|
raylib.BeginScissorMode(0, 0, applicationWindowWidth, int32(toastDimHeight))
|
||||||
raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5))
|
{
|
||||||
for i := 0; i < len(toasts); i++ {
|
raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5))
|
||||||
text := toasts[i].Text
|
for i, t := range toasts {
|
||||||
raylib.DrawText(text, 10, int32(20*i)+10, 10, raylib.White)
|
raylib.DrawText(t.Text, 10, int32(20*i)+10, 10, raylib.White)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
raylib.EndScissorMode()
|
raylib.EndScissorMode()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue