mirror of
https://github.com/Fluffy-Bean/ColouringApp.git
synced 2025-05-17 17:04:54 +00:00
Fix drawing when UI overlapping, add toast messages
This commit is contained in:
parent
832fcdc7c4
commit
a2c0d9d825
4 changed files with 184 additions and 41 deletions
49
application/toast.go
Normal file
49
application/toast.go
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package application
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
toasts = []Toast{}
|
||||||
|
toastHeight = float32(0)
|
||||||
|
toastMaxAge = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
type Toast struct {
|
||||||
|
Text string
|
||||||
|
Age time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddToast(text string) {
|
||||||
|
toast := Toast{Text: text, Age: time.Now()}
|
||||||
|
toasts = append(toasts, toast)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateToasts() {
|
||||||
|
if len(toasts) != 0 {
|
||||||
|
toastHeight = raylib.Lerp(toastHeight, float32(20*len(toasts))+10, 0.1)
|
||||||
|
} else {
|
||||||
|
toastHeight = raylib.Lerp(toastHeight, 0, 0.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(toasts); i++ {
|
||||||
|
if time.Since(toasts[i].Age) > toastMaxAge {
|
||||||
|
toasts = append(toasts[:i], toasts[i+1:]...)
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DrawToasts() {
|
||||||
|
raylib.BeginScissorMode(0, 0, WindowWidth, int32(toastHeight))
|
||||||
|
raylib.DrawRectangle(0, 0, WindowWidth, WindowHeight, raylib.Fade(raylib.Black, 0.5))
|
||||||
|
for i := 0; i < len(toasts); i++ {
|
||||||
|
text := fmt.Sprintf("%s (%s)", toasts[i].Text, time.Since(toasts[i].Age).Round(time.Second))
|
||||||
|
raylib.DrawText(text, 10, int32(20*i)+10, 10, raylib.White)
|
||||||
|
}
|
||||||
|
raylib.EndScissorMode()
|
||||||
|
}
|
2
main.go
2
main.go
|
@ -13,7 +13,7 @@ func main() {
|
||||||
raylib.InitWindow(application.WindowWidth, application.WindowHeight, application.WindowTitle)
|
raylib.InitWindow(application.WindowWidth, application.WindowHeight, application.WindowTitle)
|
||||||
raylib.InitAudioDevice()
|
raylib.InitAudioDevice()
|
||||||
|
|
||||||
//raylib.SetTargetFPS(application.WindowFPS)
|
raylib.SetTargetFPS(application.WindowFPS)
|
||||||
//raylib.SetExitKey(0) // disable exit key
|
//raylib.SetExitKey(0) // disable exit key
|
||||||
|
|
||||||
// MAIN LOOP
|
// MAIN LOOP
|
||||||
|
|
|
@ -20,10 +20,14 @@ type stroke struct {
|
||||||
|
|
||||||
func Drawing() {
|
func Drawing() {
|
||||||
var (
|
var (
|
||||||
|
camera = raylib.NewCamera2D(raylib.NewVector2(0, 0), raylib.NewVector2(0, 0), 0, 1)
|
||||||
|
//cameraMoveOffset = raylib.NewVector2(10, 10)
|
||||||
|
|
||||||
canvasSize = raylib.NewVector2(500, 430)
|
canvasSize = raylib.NewVector2(500, 430)
|
||||||
canvas = raylib.LoadRenderTexture(int32(canvasSize.X), int32(canvasSize.Y))
|
canvas = raylib.LoadRenderTexture(int32(canvasSize.X), int32(canvasSize.Y))
|
||||||
|
canvasRefresh = true
|
||||||
|
|
||||||
sidePanelWidth float32 = 300
|
sidePanelWidth = float32(300)
|
||||||
sidePanelRelativeX = application.WindowWidth - int32(sidePanelWidth)
|
sidePanelRelativeX = application.WindowWidth - int32(sidePanelWidth)
|
||||||
|
|
||||||
drawing = false
|
drawing = false
|
||||||
|
@ -34,9 +38,12 @@ func Drawing() {
|
||||||
undoneStrokes = []stroke{}
|
undoneStrokes = []stroke{}
|
||||||
|
|
||||||
colourPickerVal = raylib.Orange
|
colourPickerVal = raylib.Orange
|
||||||
colourPickerHeight float32 = 200
|
colourPickerHeight = float32(200)
|
||||||
|
|
||||||
brushSize float32 = 10
|
brushSize = float32(10)
|
||||||
|
|
||||||
|
fileName = "NewProject"
|
||||||
|
fileNameEditing = false
|
||||||
)
|
)
|
||||||
|
|
||||||
refreshCanvas := func() {
|
refreshCanvas := func() {
|
||||||
|
@ -55,8 +62,42 @@ func Drawing() {
|
||||||
raylib.EndTextureMode()
|
raylib.EndTextureMode()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create canvas
|
undoStroke := func() {
|
||||||
refreshCanvas()
|
fmt.Println("Undo")
|
||||||
|
|
||||||
|
if len(undoneStrokes) > 0 {
|
||||||
|
strokes = append(strokes, undoneStrokes[len(undoneStrokes)-1])
|
||||||
|
undoneStrokes = undoneStrokes[:len(undoneStrokes)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
canvasRefresh = true
|
||||||
|
}
|
||||||
|
redoStroke := func() {
|
||||||
|
fmt.Println("Redo")
|
||||||
|
|
||||||
|
// 1 because I dont know why
|
||||||
|
if len(strokes) > 1 {
|
||||||
|
undoneStrokes = append(undoneStrokes, strokes[len(strokes)-1])
|
||||||
|
strokes = strokes[:len(strokes)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
canvasRefresh = true
|
||||||
|
}
|
||||||
|
|
||||||
|
saveImage := func() {
|
||||||
|
if fileName == "" {
|
||||||
|
application.AddToast("Please enter a file name")
|
||||||
|
} else {
|
||||||
|
image := raylib.LoadImageFromTexture(canvas.Texture)
|
||||||
|
|
||||||
|
raylib.ImageRotate(image, 180)
|
||||||
|
raylib.ImageFlipHorizontal(image)
|
||||||
|
|
||||||
|
raylib.ExportImage(*image, application.DirUserData+fileName+".png")
|
||||||
|
|
||||||
|
application.AddToast("Drawing saved at " + application.DirUserData + fileName + ".png")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for !application.ShouldQuit {
|
for !application.ShouldQuit {
|
||||||
// DEFAULT
|
// DEFAULT
|
||||||
|
@ -75,10 +116,24 @@ func Drawing() {
|
||||||
|
|
||||||
// INPUT
|
// INPUT
|
||||||
{
|
{
|
||||||
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
|
//if raylib.GetMouseWheelMove() != 0 {
|
||||||
drawing = false
|
// camera.Zoom += float32(raylib.GetMouseWheelMove()) * 0.05
|
||||||
|
//}
|
||||||
|
//if raylib.IsMouseButtonPressed(raylib.MouseMiddleButton) {
|
||||||
|
// cameraMoveOffset = raylib.Vector2Subtract(camera.Target, raylib.GetMousePosition())
|
||||||
|
//}
|
||||||
|
//if raylib.IsMouseButtonDown(raylib.MouseMiddleButton) {
|
||||||
|
// camera.Target = raylib.Vector2Subtract(raylib.GetMousePosition(), raylib.Vector2Scale(cameraMoveOffset, -1))
|
||||||
|
//}
|
||||||
|
|
||||||
if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvasSize.X, canvasSize.Y)) {
|
if raylib.IsKeyPressed(raylib.KeyF8) {
|
||||||
|
application.AddToast("This is a toast message!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
|
||||||
|
if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(application.WindowWidth-int32(sidePanelWidth)), 0, sidePanelWidth, float32(application.WindowHeight))) {
|
||||||
|
drawing = false
|
||||||
|
} else if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvasSize.X, canvasSize.Y)) {
|
||||||
drawing = true
|
drawing = true
|
||||||
currentStroke = stroke{
|
currentStroke = stroke{
|
||||||
Color: colourPickerVal,
|
Color: colourPickerVal,
|
||||||
|
@ -102,23 +157,15 @@ func Drawing() {
|
||||||
currentStroke = stroke{}
|
currentStroke = stroke{}
|
||||||
undoneStrokes = []stroke{}
|
undoneStrokes = []stroke{}
|
||||||
|
|
||||||
refreshCanvas()
|
canvasRefresh = true
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
if len(undoneStrokes) > 1 {
|
undoStroke()
|
||||||
strokes = append(strokes, undoneStrokes[len(undoneStrokes)-1])
|
|
||||||
undoneStrokes = undoneStrokes[:len(undoneStrokes)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
refreshCanvas()
|
|
||||||
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyZ) {
|
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyZ) {
|
||||||
if len(strokes) > 1 {
|
redoStroke()
|
||||||
undoneStrokes = append(undoneStrokes, strokes[len(strokes)-1])
|
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyS) {
|
||||||
strokes = strokes[:len(strokes)-1]
|
saveImage()
|
||||||
}
|
|
||||||
|
|
||||||
refreshCanvas()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,6 +176,13 @@ func Drawing() {
|
||||||
} else {
|
} else {
|
||||||
gui.SetState(gui.STATE_NORMAL)
|
gui.SetState(gui.STATE_NORMAL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if canvasRefresh {
|
||||||
|
refreshCanvas()
|
||||||
|
canvasRefresh = false
|
||||||
|
}
|
||||||
|
|
||||||
|
application.UpdateToasts()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DRAW
|
// DRAW
|
||||||
|
@ -138,18 +192,27 @@ func Drawing() {
|
||||||
gui.Grid(raylib.NewRectangle(0, 0, float32(application.WindowWidth), float32(application.WindowHeight)), "", 30, 1, &raylib.Vector2{})
|
gui.Grid(raylib.NewRectangle(0, 0, float32(application.WindowWidth), float32(application.WindowHeight)), "", 30, 1, &raylib.Vector2{})
|
||||||
|
|
||||||
// Canvas stuff
|
// Canvas stuff
|
||||||
raylib.DrawRectangle(20, 20, int32(canvasSize.X), int32(canvasSize.Y), raylib.Fade(raylib.Black, 0.3))
|
raylib.BeginMode2D(camera)
|
||||||
raylib.BeginScissorMode(10, 10, int32(canvasSize.X), int32(canvasSize.Y))
|
|
||||||
{
|
{
|
||||||
|
raylib.DrawRectangle(20, 20, int32(canvasSize.X), int32(canvasSize.Y), raylib.Fade(raylib.Black, 0.3))
|
||||||
raylib.DrawTexturePro(canvas.Texture, raylib.NewRectangle(0, 0, float32(canvas.Texture.Width), float32(-canvas.Texture.Height)), raylib.NewRectangle(10, 10, canvasSize.X, canvasSize.Y), raylib.Vector2{}, 0, raylib.White)
|
raylib.DrawTexturePro(canvas.Texture, raylib.NewRectangle(0, 0, float32(canvas.Texture.Width), float32(-canvas.Texture.Height)), raylib.NewRectangle(10, 10, canvasSize.X, canvasSize.Y), raylib.Vector2{}, 0, raylib.White)
|
||||||
|
|
||||||
|
if drawing {
|
||||||
|
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.DarkGray)
|
||||||
|
} else {
|
||||||
|
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.Gray)
|
||||||
|
}
|
||||||
|
|
||||||
|
raylib.BeginScissorMode(10, 10, int32(canvasSize.X), int32(canvasSize.Y))
|
||||||
for i := 1; i < len(currentStroke.Points); i++ {
|
for i := 1; i < len(currentStroke.Points); i++ {
|
||||||
raylib.DrawLineEx(currentStroke.Points[i-1], currentStroke.Points[i], currentStroke.Size, currentStroke.Color)
|
raylib.DrawLineEx(currentStroke.Points[i-1], currentStroke.Points[i], currentStroke.Size, currentStroke.Color)
|
||||||
raylib.DrawCircle(int32(currentStroke.Points[i].X), int32(currentStroke.Points[i].Y), currentStroke.Size/2, currentStroke.Color)
|
raylib.DrawCircle(int32(currentStroke.Points[i].X), int32(currentStroke.Points[i].Y), currentStroke.Size/2, currentStroke.Color)
|
||||||
}
|
}
|
||||||
|
raylib.EndScissorMode()
|
||||||
|
|
||||||
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
|
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
|
||||||
}
|
}
|
||||||
raylib.EndScissorMode()
|
raylib.EndMode2D()
|
||||||
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.Gray)
|
|
||||||
|
|
||||||
// UI stuff
|
// UI stuff
|
||||||
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight)
|
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight)
|
||||||
|
@ -160,10 +223,26 @@ func Drawing() {
|
||||||
application.CurrentScene = application.SceneTitle
|
application.CurrentScene = application.SceneTitle
|
||||||
}
|
}
|
||||||
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+20+25), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_SAVE, "")) {
|
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+20+25), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_SAVE, "")) {
|
||||||
|
saveImage()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stupid arrows are inverted
|
||||||
|
if gui.Button(raylib.NewRectangle(float32(application.WindowWidth-70), 10, 25, 25), gui.IconText(gui.ICON_UNDO, "")) {
|
||||||
|
redoStroke()
|
||||||
|
}
|
||||||
|
if gui.Button(raylib.NewRectangle(float32(application.WindowWidth-35), 10, 25, 25), gui.IconText(gui.ICON_REDO, "")) {
|
||||||
|
undoStroke()
|
||||||
}
|
}
|
||||||
|
|
||||||
colourPickerVal = gui.ColorPicker(raylib.NewRectangle(float32(sidePanelRelativeX+10), 45, sidePanelWidth-40, colourPickerHeight), "Color", colourPickerVal)
|
colourPickerVal = gui.ColorPicker(raylib.NewRectangle(float32(sidePanelRelativeX+10), 45, sidePanelWidth-40, colourPickerHeight), "Color", colourPickerVal)
|
||||||
brushSize = gui.Slider(raylib.NewRectangle(float32(sidePanelRelativeX+80), 65+colourPickerHeight, 200, 20), "Brush Size", "", brushSize, 1, 100)
|
|
||||||
|
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 55+colourPickerHeight, 200, 20), "Brush Size")
|
||||||
|
brushSize = gui.Slider(raylib.NewRectangle(float32(sidePanelRelativeX+78), 55+colourPickerHeight, 215, 20), "", "", brushSize, 1, 100)
|
||||||
|
|
||||||
|
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 85+colourPickerHeight, 200, 20), "File Name")
|
||||||
|
if gui.TextBox(raylib.NewRectangle(float32(sidePanelRelativeX+78), 85+colourPickerHeight, 215, 20), &fileName, 40, fileNameEditing) {
|
||||||
|
fileNameEditing = !fileNameEditing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
raylib.EndScissorMode()
|
raylib.EndScissorMode()
|
||||||
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight, raylib.Gray)
|
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight, raylib.Gray)
|
||||||
|
@ -178,6 +257,9 @@ func Drawing() {
|
||||||
text = fmt.Sprintf("Canvas Size: %dx%d", int32(canvasSize.X), int32(canvasSize.Y))
|
text = fmt.Sprintf("Canvas Size: %dx%d", int32(canvasSize.X), int32(canvasSize.Y))
|
||||||
gui.StatusBar(raylib.NewRectangle(199, float32(application.WindowHeight-20), 200, 20), text)
|
gui.StatusBar(raylib.NewRectangle(199, float32(application.WindowHeight-20), 200, 20), text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw toasts
|
||||||
|
application.DrawToasts()
|
||||||
}
|
}
|
||||||
raylib.EndDrawing()
|
raylib.EndDrawing()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,25 +2,37 @@ package scenes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"ColouringApp/application"
|
"ColouringApp/application"
|
||||||
|
"os"
|
||||||
|
|
||||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PlayerData() {
|
func PlayerData() {
|
||||||
// Load player data here
|
// Load player data here
|
||||||
for !application.ShouldQuit {
|
for !application.ShouldQuit {
|
||||||
|
// DEFAULT
|
||||||
|
{
|
||||||
application.ShouldQuit = raylib.WindowShouldClose()
|
application.ShouldQuit = raylib.WindowShouldClose()
|
||||||
if application.CurrentScene != application.ScenePlayerData {
|
if application.CurrentScene != application.ScenePlayerData {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if userData exists
|
||||||
|
if _, err := os.Stat(application.DirUserData); os.IsNotExist(err) {
|
||||||
|
err := os.Mkdir(application.DirUserData, 0755)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DRAW
|
||||||
|
{
|
||||||
raylib.BeginDrawing()
|
raylib.BeginDrawing()
|
||||||
raylib.ClearBackground(raylib.Black)
|
raylib.ClearBackground(raylib.Black)
|
||||||
|
|
||||||
raylib.DrawText("Loading...", 10, application.WindowHeight-30, 20, raylib.White)
|
raylib.DrawText("Loading...", 10, application.WindowHeight-30, 20, raylib.White)
|
||||||
|
|
||||||
raylib.EndDrawing()
|
raylib.EndDrawing()
|
||||||
|
}
|
||||||
//time.Sleep(1 * time.Second)
|
|
||||||
|
|
||||||
application.CurrentScene = application.SceneTitle
|
application.CurrentScene = application.SceneTitle
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue