mirror of
https://github.com/Fluffy-Bean/ColouringApp.git
synced 2025-01-15 01:45:13 +00:00
Check if user wants to quit app
This commit is contained in:
parent
ea1322d9ae
commit
3740187ee2
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
@ -19,6 +20,7 @@ type Canvas struct {
|
||||||
Strokes []raylib.Texture2D
|
Strokes []raylib.Texture2D
|
||||||
UndoneStrokes []raylib.Texture2D
|
UndoneStrokes []raylib.Texture2D
|
||||||
|
|
||||||
|
UnsavedChanges bool
|
||||||
Refresh bool
|
Refresh bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +51,7 @@ func (c *Canvas) Update() {
|
||||||
}
|
}
|
||||||
raylib.EndTextureMode()
|
raylib.EndTextureMode()
|
||||||
|
|
||||||
|
c.UnsavedChanges = true
|
||||||
c.Refresh = false
|
c.Refresh = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,10 +111,12 @@ 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, filepath.Join(dirUserData, c.Name+".png"))
|
||||||
|
|
||||||
AddToast("Drawing saved as " + c.Name + ".png")
|
AddToast("Drawing saved as " + c.Name + ".png")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.UnsavedChanges = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCanvas(name string, size, offset raylib.Vector2, background raylib.Texture2D) *Canvas {
|
func NewCanvas(name string, size, offset raylib.Vector2, background raylib.Texture2D) *Canvas {
|
||||||
|
|
110
main.go
110
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -10,75 +11,13 @@ import (
|
||||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
applicationTitle = "Colouring App"
|
|
||||||
applicationMinWindowWidth = int32(800)
|
|
||||||
applicationMinWindowHeight = int32(600)
|
|
||||||
|
|
||||||
defaultProjectName = "NewProject"
|
|
||||||
defaultProjectWidth = 700
|
|
||||||
defaultProjectHeight = 530
|
|
||||||
|
|
||||||
dirAssets = "./assets/"
|
|
||||||
dirUserData = "./userData/"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
StateNormal = iota
|
|
||||||
StateDrawing
|
|
||||||
StateFileMenu
|
|
||||||
StateNewCanvas
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
applicationState = StateNormal
|
|
||||||
applicationShouldQuit = false
|
|
||||||
applicationShowDebugValues = false
|
|
||||||
applicationWindowWidth = applicationMinWindowWidth
|
|
||||||
applicationWindowHeight = applicationMinWindowHeight
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
newPenStroke = penTool{}
|
|
||||||
newPenStrokeSafeZone = 1
|
|
||||||
|
|
||||||
toolPanelWidth = float32(350)
|
|
||||||
toolPanelOffset = applicationWindowWidth - int32(toolPanelWidth)
|
|
||||||
|
|
||||||
toolPanelColourPicker = raylib.Orange
|
|
||||||
toolPanelColourPickerHeight = float32(250)
|
|
||||||
|
|
||||||
toolPanelBrushSize = float32(10)
|
|
||||||
|
|
||||||
isEditingCanvasName = false
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
canvas *Canvas
|
|
||||||
|
|
||||||
shouldCreateNewCanvas = true
|
|
||||||
|
|
||||||
newCanvasName = defaultProjectName
|
|
||||||
isEditingNewCanvasName = false
|
|
||||||
|
|
||||||
newCanvasWidth = defaultProjectWidth
|
|
||||||
isEditingNewCanvasWidth = false
|
|
||||||
|
|
||||||
newCanvasHeight = defaultProjectHeight
|
|
||||||
isEditingNewCanvasHeight = false
|
|
||||||
|
|
||||||
newCanvasColor = raylib.White
|
|
||||||
newCanvasImagePath = ""
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Initialize raylib
|
||||||
raylib.SetConfigFlags(raylib.FlagWindowResizable | raylib.FlagWindowHighdpi | raylib.FlagMsaa4xHint)
|
raylib.SetConfigFlags(raylib.FlagWindowResizable | raylib.FlagWindowHighdpi | raylib.FlagMsaa4xHint)
|
||||||
|
|
||||||
raylib.InitWindow(applicationWindowWidth, applicationWindowHeight, applicationTitle)
|
raylib.InitWindow(applicationWindowWidth, applicationWindowHeight, applicationTitle)
|
||||||
|
raylib.SetWindowMinSize(int(applicationMinWindowWidth), int(applicationMinWindowHeight)) // Set a minimum window size
|
||||||
raylib.SetWindowMinSize(int(applicationMinWindowWidth), int(applicationMinWindowHeight))
|
raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor()))) // Match monitor refresh rate
|
||||||
raylib.SetTargetFPS(int32(raylib.GetMonitorRefreshRate(raylib.GetCurrentMonitor()))) // Set our application to run at the refresh rate of the monitor
|
raylib.SetExitKey(raylib.KeyNull) // disable exit key
|
||||||
raylib.SetExitKey(0) // disable exit key
|
|
||||||
|
|
||||||
// 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) {
|
||||||
|
@ -103,7 +42,13 @@ func main() {
|
||||||
// LOOP
|
// LOOP
|
||||||
for !applicationShouldQuit {
|
for !applicationShouldQuit {
|
||||||
// Update default loop values
|
// Update default loop values
|
||||||
applicationShouldQuit = raylib.WindowShouldClose()
|
if raylib.WindowShouldClose() {
|
||||||
|
if canvas.UnsavedChanges {
|
||||||
|
applicationState = StateWindowWantsToDie
|
||||||
|
} else {
|
||||||
|
applicationShouldQuit = true
|
||||||
|
}
|
||||||
|
}
|
||||||
if raylib.IsWindowResized() {
|
if raylib.IsWindowResized() {
|
||||||
applicationWindowWidth = int32(raylib.GetScreenWidth())
|
applicationWindowWidth = int32(raylib.GetScreenWidth())
|
||||||
applicationWindowHeight = int32(raylib.GetScreenHeight())
|
applicationWindowHeight = int32(raylib.GetScreenHeight())
|
||||||
|
@ -240,7 +185,7 @@ func main() {
|
||||||
if gui.TextBox(raylib.NewRectangle(float32(toolPanelOffset+80), 115+toolPanelColourPickerHeight, toolPanelWidth-90, 20), &canvas.Name, 40, isEditingCanvasName) {
|
if gui.TextBox(raylib.NewRectangle(float32(toolPanelOffset+80), 115+toolPanelColourPickerHeight, toolPanelWidth-90, 20), &canvas.Name, 40, isEditingCanvasName) {
|
||||||
isEditingCanvasName = !isEditingCanvasName
|
isEditingCanvasName = !isEditingCanvasName
|
||||||
}
|
}
|
||||||
raylib.DrawRectangleLines(toolPanelOffset, 0, int32(toolPanelWidth), applicationWindowHeight, raylib.Gray)
|
raylib.DrawLine(toolPanelOffset, 0, toolPanelOffset, applicationWindowHeight, raylib.Black)
|
||||||
}
|
}
|
||||||
raylib.EndScissorMode()
|
raylib.EndScissorMode()
|
||||||
|
|
||||||
|
@ -273,7 +218,6 @@ func main() {
|
||||||
|
|
||||||
// Magic numbers
|
// Magic numbers
|
||||||
raylib.BeginScissorMode(int32(windowPos.X)+1, int32(windowPos.Y)+24, int32(windowPos.Width)-2, int32(windowPos.Height)-25)
|
raylib.BeginScissorMode(int32(windowPos.X)+1, int32(windowPos.Y)+24, int32(windowPos.Width)-2, int32(windowPos.Height)-25)
|
||||||
|
|
||||||
gui.GroupBox(raylib.NewRectangle(windowPos.X+11, windowPos.Y+34, windowPos.Width-22, 200), "Create New")
|
gui.GroupBox(raylib.NewRectangle(windowPos.X+11, windowPos.Y+34, windowPos.Width-22, 200), "Create New")
|
||||||
{
|
{
|
||||||
var err error
|
var err error
|
||||||
|
@ -284,8 +228,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
gui.Label(raylib.NewRectangle(windowPos.X+21, windowPos.Y+74, 100, 20), "Canvas Width")
|
gui.Label(raylib.NewRectangle(windowPos.X+21, windowPos.Y+74, 100, 20), "Canvas Width")
|
||||||
lastWidth := newCanvasWidth
|
|
||||||
width := fmt.Sprintf("%d", newCanvasWidth)
|
width := fmt.Sprintf("%d", newCanvasWidth)
|
||||||
|
lastWidth := newCanvasWidth
|
||||||
if gui.TextBox(raylib.NewRectangle(windowPos.X+131, windowPos.Y+74, windowPos.Width-152, 20), &width, 6, isEditingNewCanvasWidth) {
|
if gui.TextBox(raylib.NewRectangle(windowPos.X+131, windowPos.Y+74, windowPos.Width-152, 20), &width, 6, isEditingNewCanvasWidth) {
|
||||||
isEditingNewCanvasWidth = !isEditingNewCanvasWidth
|
isEditingNewCanvasWidth = !isEditingNewCanvasWidth
|
||||||
}
|
}
|
||||||
|
@ -294,8 +238,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
gui.Label(raylib.NewRectangle(windowPos.X+21, windowPos.Y+104, 100, 20), "Canvas Height")
|
gui.Label(raylib.NewRectangle(windowPos.X+21, windowPos.Y+104, 100, 20), "Canvas Height")
|
||||||
lastHeight := newCanvasHeight
|
|
||||||
height := fmt.Sprintf("%d", newCanvasHeight)
|
height := fmt.Sprintf("%d", newCanvasHeight)
|
||||||
|
lastHeight := newCanvasHeight
|
||||||
if gui.TextBox(raylib.NewRectangle(windowPos.X+131, windowPos.Y+104, windowPos.Width-152, 20), &height, 6, isEditingNewCanvasHeight) {
|
if gui.TextBox(raylib.NewRectangle(windowPos.X+131, windowPos.Y+104, windowPos.Width-152, 20), &height, 6, isEditingNewCanvasHeight) {
|
||||||
isEditingNewCanvasHeight = !isEditingNewCanvasHeight
|
isEditingNewCanvasHeight = !isEditingNewCanvasHeight
|
||||||
}
|
}
|
||||||
|
@ -329,16 +273,16 @@ 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 = filepath.Join(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 = filepath.Join(dirAssets, userDataProjects[i])
|
||||||
splitName := strings.Split(userDataProjects[i], ".")
|
splitName := strings.Split(userDataProjects[i], ".")
|
||||||
newCanvasName = splitName[:len(splitName)-1][0]
|
newCanvasName = filepath.Base(splitName[0])
|
||||||
applicationState = StateNewCanvas
|
applicationState = StateNewCanvas
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -346,6 +290,13 @@ func main() {
|
||||||
|
|
||||||
raylib.EndScissorMode()
|
raylib.EndScissorMode()
|
||||||
case StateNewCanvas:
|
case StateNewCanvas:
|
||||||
|
if !canvas.UnsavedChanges {
|
||||||
|
applicationState = StateNormal
|
||||||
|
shouldCreateNewCanvas = true
|
||||||
|
AddToast("Created New Canvas: " + canvas.Name)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
gui.Unlock()
|
gui.Unlock()
|
||||||
raylib.DrawRectangle(0, 0, applicationWindowWidth, applicationWindowHeight, raylib.Fade(raylib.Black, 0.5))
|
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)
|
windowPos := raylib.NewRectangle(float32((applicationWindowWidth/2)-200), float32((applicationWindowHeight/2)-75), 400, 150)
|
||||||
|
@ -359,6 +310,17 @@ func main() {
|
||||||
shouldCreateNewCanvas = true
|
shouldCreateNewCanvas = true
|
||||||
AddToast("Created New Canvas: " + canvas.Name)
|
AddToast("Created New Canvas: " + canvas.Name)
|
||||||
}
|
}
|
||||||
|
case StateWindowWantsToDie:
|
||||||
|
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, "Unsaved Changes", "You have unsaved changes, are you sure you want to exit?", "No;Yes")
|
||||||
|
|
||||||
|
if choice == 0 || choice == 1 {
|
||||||
|
applicationState = StateNormal
|
||||||
|
} else if choice == 2 {
|
||||||
|
applicationShouldQuit = true
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
65
vars.go
Normal file
65
vars.go
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import raylib "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
|
||||||
|
const (
|
||||||
|
applicationTitle = "Colouring App"
|
||||||
|
applicationMinWindowWidth = int32(800)
|
||||||
|
applicationMinWindowHeight = int32(600)
|
||||||
|
|
||||||
|
defaultProjectName = "NewProject"
|
||||||
|
defaultProjectWidth = 700
|
||||||
|
defaultProjectHeight = 530
|
||||||
|
|
||||||
|
dirAssets = "assets"
|
||||||
|
dirUserData = "userData"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
StateNormal = iota
|
||||||
|
StateDrawing
|
||||||
|
StateFileMenu
|
||||||
|
StateNewCanvas
|
||||||
|
StateWindowWantsToDie
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
applicationState = StateNormal
|
||||||
|
applicationShouldQuit = false
|
||||||
|
applicationShowDebugValues = false
|
||||||
|
applicationWindowWidth = applicationMinWindowWidth
|
||||||
|
applicationWindowHeight = applicationMinWindowHeight
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
newPenStroke = penTool{}
|
||||||
|
newPenStrokeSafeZone = 1
|
||||||
|
|
||||||
|
toolPanelWidth = float32(350)
|
||||||
|
toolPanelOffset = applicationWindowWidth - int32(toolPanelWidth)
|
||||||
|
|
||||||
|
toolPanelColourPicker = raylib.Orange
|
||||||
|
toolPanelColourPickerHeight = float32(250)
|
||||||
|
|
||||||
|
toolPanelBrushSize = float32(10)
|
||||||
|
|
||||||
|
isEditingCanvasName = false
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
canvas *Canvas
|
||||||
|
|
||||||
|
shouldCreateNewCanvas = true
|
||||||
|
|
||||||
|
newCanvasName = defaultProjectName
|
||||||
|
isEditingNewCanvasName = false
|
||||||
|
|
||||||
|
newCanvasWidth = defaultProjectWidth
|
||||||
|
isEditingNewCanvasWidth = false
|
||||||
|
|
||||||
|
newCanvasHeight = defaultProjectHeight
|
||||||
|
isEditingNewCanvasHeight = false
|
||||||
|
|
||||||
|
newCanvasColor = raylib.White
|
||||||
|
newCanvasImagePath = ""
|
||||||
|
)
|
Loading…
Reference in a new issue