mirror of
https://github.com/Fluffy-Bean/ColouringApp.git
synced 2025-01-29 15:48:27 +00:00
Redesign structure of application
This commit is contained in:
parent
be4c6c7fcf
commit
105785e6c0
281
main.go
281
main.go
|
@ -1,37 +1,282 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"ColouringApp/application"
|
||||
"ColouringApp/scenes"
|
||||
|
||||
"fmt"
|
||||
gui "github.com/gen2brain/raylib-go/raygui"
|
||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
raylib.SetConfigFlags(raylib.FlagWindowResizable)
|
||||
raylib.SetTraceLogLevel(raylib.LogTrace)
|
||||
raylib.SetConfigFlags(raylib.FlagMsaa4xHint)
|
||||
|
||||
raylib.InitWindow(application.WindowWidth, application.WindowHeight, application.WindowTitle)
|
||||
raylib.InitWindow(WindowWidth, WindowHeight, WindowTitle)
|
||||
raylib.InitAudioDevice()
|
||||
|
||||
raylib.SetTargetFPS(application.WindowFPS)
|
||||
raylib.SetTargetFPS(WindowFPS)
|
||||
//raylib.SetExitKey(0) // disable exit key
|
||||
|
||||
// MAIN LOOP
|
||||
for !application.ShouldQuit {
|
||||
switch application.CurrentScene {
|
||||
case application.ScenePlayerData:
|
||||
scenes.PlayerData()
|
||||
case application.SceneTitle:
|
||||
scenes.Title()
|
||||
case application.SceneOptions:
|
||||
scenes.Options()
|
||||
case application.SceneDrawing:
|
||||
scenes.Drawing()
|
||||
default:
|
||||
panic("Unknown scene")
|
||||
var (
|
||||
camera = raylib.NewCamera2D(raylib.NewVector2(0, 0), raylib.NewVector2(0, 0), 0, 1)
|
||||
//cameraMoveOffset = raylib.NewVector2(10, 10)
|
||||
|
||||
canvasSize = raylib.NewVector2(500, 430)
|
||||
canvasScale = float32(1)
|
||||
canvas = raylib.LoadRenderTexture(int32(canvasSize.X), int32(canvasSize.Y))
|
||||
canvasRefresh = true
|
||||
|
||||
sidePanelWidth = float32(350)
|
||||
sidePanelRelativeX = WindowWidth - int32(sidePanelWidth)
|
||||
|
||||
drawing = false
|
||||
//drawingMode = ModeDrawing
|
||||
|
||||
currentStroke = penTool{}
|
||||
strokes = []penTool{currentStroke}
|
||||
undoneStrokes = []penTool{}
|
||||
|
||||
colourPickerVal = raylib.Orange
|
||||
colourPickerHeight = float32(200)
|
||||
|
||||
brushSize = float32(10)
|
||||
brushOpacity = float32(1)
|
||||
|
||||
fileName = "NewProject"
|
||||
fileNameEditing = false
|
||||
|
||||
menu = StateNone
|
||||
)
|
||||
|
||||
// check if userData exists
|
||||
if _, err := os.Stat(DirUserData); os.IsNotExist(err) {
|
||||
err := os.Mkdir(DirUserData, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
WindowWidth = int32(raylib.GetScreenWidth())
|
||||
WindowHeight = int32(raylib.GetScreenHeight())
|
||||
|
||||
refreshCanvas := func() {
|
||||
raylib.BeginTextureMode(canvas)
|
||||
raylib.ClearBackground(raylib.White)
|
||||
|
||||
for i := 1; i < len(strokes); i++ {
|
||||
strokes[i].Draw(raylib.NewVector2(-10, -10))
|
||||
}
|
||||
|
||||
raylib.EndTextureMode()
|
||||
}
|
||||
|
||||
redoStroke := func() {
|
||||
if len(undoneStrokes) > 0 {
|
||||
strokes = append(strokes, undoneStrokes[len(undoneStrokes)-1])
|
||||
undoneStrokes = undoneStrokes[:len(undoneStrokes)-1]
|
||||
|
||||
AddToast("Redo")
|
||||
canvasRefresh = true
|
||||
}
|
||||
}
|
||||
undoStroke := func() {
|
||||
// 1 because I dont know why
|
||||
if len(strokes) > 1 {
|
||||
undoneStrokes = append(undoneStrokes, strokes[len(strokes)-1])
|
||||
strokes = strokes[:len(strokes)-1]
|
||||
|
||||
canvasRefresh = true
|
||||
AddToast("Undo")
|
||||
}
|
||||
}
|
||||
|
||||
saveImage := func() {
|
||||
if fileName == "" {
|
||||
AddToast("Please enter a file name")
|
||||
} else {
|
||||
image := raylib.LoadImageFromTexture(canvas.Texture)
|
||||
|
||||
raylib.ImageRotate(image, 180)
|
||||
raylib.ImageFlipHorizontal(image)
|
||||
|
||||
raylib.ExportImage(*image, DirUserData+fileName+".png")
|
||||
|
||||
AddToast("Drawing saved at " + DirUserData + fileName + ".png")
|
||||
}
|
||||
}
|
||||
|
||||
for !ShouldQuit {
|
||||
// LOOP
|
||||
ShouldQuit = raylib.WindowShouldClose()
|
||||
|
||||
if raylib.IsWindowResized() {
|
||||
WindowWidth = int32(raylib.GetScreenWidth())
|
||||
WindowHeight = int32(raylib.GetScreenHeight())
|
||||
|
||||
sidePanelRelativeX = WindowWidth - int32(sidePanelWidth)
|
||||
}
|
||||
|
||||
// INPUT
|
||||
{
|
||||
if raylib.GetMouseWheelMove() != 0 && !drawing {
|
||||
canvasScale += 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.IsKeyPressed(raylib.KeyF8) {
|
||||
AddToast("This is a toast message!")
|
||||
}
|
||||
|
||||
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
|
||||
if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(WindowWidth-int32(sidePanelWidth)), 0, sidePanelWidth, float32(WindowHeight))) {
|
||||
drawing = false
|
||||
} else if raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvasSize.X, canvasSize.Y)) {
|
||||
drawing = true
|
||||
currentStroke = penTool{
|
||||
Color: colourPickerVal,
|
||||
Size: brushSize,
|
||||
Opacity: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && drawing {
|
||||
var safeZone float32 = 5
|
||||
|
||||
if len(currentStroke.Points) <= 1 {
|
||||
currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition())
|
||||
} else if raylib.Vector2Distance(currentStroke.Points[len(currentStroke.Points)-1], raylib.GetMousePosition()) > safeZone {
|
||||
currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition())
|
||||
}
|
||||
}
|
||||
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
|
||||
strokes = append(strokes, currentStroke)
|
||||
currentStroke = penTool{}
|
||||
undoneStrokes = []penTool{}
|
||||
|
||||
drawing = false
|
||||
canvasRefresh = true
|
||||
}
|
||||
|
||||
if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyDown(raylib.KeyLeftShift) && raylib.IsKeyPressed(raylib.KeyZ) {
|
||||
redoStroke()
|
||||
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyZ) {
|
||||
undoStroke()
|
||||
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyS) {
|
||||
saveImage()
|
||||
}
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
{
|
||||
if drawing {
|
||||
gui.SetState(gui.STATE_DISABLED)
|
||||
} else {
|
||||
gui.SetState(gui.STATE_NORMAL)
|
||||
}
|
||||
|
||||
if canvasRefresh {
|
||||
refreshCanvas()
|
||||
canvasRefresh = false
|
||||
}
|
||||
|
||||
UpdateToasts()
|
||||
}
|
||||
|
||||
// DRAW
|
||||
raylib.BeginDrawing()
|
||||
{
|
||||
raylib.ClearBackground(raylib.White)
|
||||
gui.Grid(raylib.NewRectangle(0, 0, float32(WindowWidth), float32(WindowHeight)), "", 30, 1, &raylib.Vector2{})
|
||||
|
||||
// Canvas stuff
|
||||
raylib.BeginMode2D(camera)
|
||||
{
|
||||
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.BeginScissorMode(10, 10, int32(canvasSize.X), int32(canvasSize.Y))
|
||||
currentStroke.Draw(raylib.NewVector2(0, 0))
|
||||
//raylib.EndScissorMode()
|
||||
|
||||
if drawing {
|
||||
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.DarkGray)
|
||||
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
|
||||
} else {
|
||||
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.Gray)
|
||||
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
|
||||
}
|
||||
}
|
||||
raylib.EndMode2D()
|
||||
|
||||
// UI stuff
|
||||
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight)
|
||||
{
|
||||
raylib.DrawRectangle(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Fade(raylib.White, 0.7))
|
||||
|
||||
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+10), 10, 25, 25), gui.IconText(gui.ICON_CROSS, "")) {
|
||||
menu = StateFileMenu
|
||||
}
|
||||
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+20+25), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_SAVE, "")) {
|
||||
saveImage()
|
||||
}
|
||||
|
||||
if gui.Button(raylib.NewRectangle(float32(WindowWidth-70), 10, 25, 25), gui.IconText(gui.ICON_UNDO, "")) {
|
||||
undoStroke()
|
||||
}
|
||||
if gui.Button(raylib.NewRectangle(float32(WindowWidth-35), 10, 25, 25), gui.IconText(gui.ICON_REDO, "")) {
|
||||
redoStroke()
|
||||
}
|
||||
|
||||
colourPickerVal = gui.ColorPicker(raylib.NewRectangle(float32(sidePanelRelativeX+10), 45, sidePanelWidth-45, colourPickerHeight), "Color", colourPickerVal)
|
||||
|
||||
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 55+colourPickerHeight, 60, 20), "Brush Size")
|
||||
brushSize = gui.Slider(raylib.NewRectangle(float32(sidePanelRelativeX+80), 55+colourPickerHeight, sidePanelWidth-90, 20), "", "", brushSize, 1, 100)
|
||||
|
||||
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 85+colourPickerHeight, 60, 20), "Brush Opacity")
|
||||
brushOpacity = gui.Slider(raylib.NewRectangle(float32(sidePanelRelativeX+80), 85+colourPickerHeight, sidePanelWidth-90, 20), "", "", brushOpacity, 0, 1)
|
||||
|
||||
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 115+colourPickerHeight, 60, 20), "File Name")
|
||||
if gui.TextBox(raylib.NewRectangle(float32(sidePanelRelativeX+80), 115+colourPickerHeight, sidePanelWidth-90, 20), &fileName, 40, fileNameEditing) {
|
||||
fileNameEditing = !fileNameEditing
|
||||
}
|
||||
}
|
||||
raylib.EndScissorMode()
|
||||
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), WindowHeight, raylib.Gray)
|
||||
|
||||
// Info
|
||||
{
|
||||
var text string
|
||||
|
||||
text = fmt.Sprintf("Strokes: %d | Points: %d", len(strokes), len(currentStroke.Points))
|
||||
gui.StatusBar(raylib.NewRectangle(0, float32(WindowHeight-20), 200, 20), text)
|
||||
|
||||
text = fmt.Sprintf("Canvas Size: %dx%d | Scale: %v", int32(canvasSize.X), int32(canvasSize.Y), canvasScale)
|
||||
gui.StatusBar(raylib.NewRectangle(199, float32(WindowHeight-20), 200, 20), text)
|
||||
}
|
||||
|
||||
switch menu {
|
||||
case StateFileMenu:
|
||||
raylib.DrawRectangle(0, 0, WindowWidth, WindowHeight, raylib.Fade(raylib.Black, 0.5))
|
||||
choice := gui.MessageBox(raylib.NewRectangle(float32(WindowWidth/2-200), float32(WindowHeight/2-100), 400, 200), "File", "This is a message box", "OK")
|
||||
if choice == 0 || choice == 1 {
|
||||
menu = StateNone
|
||||
}
|
||||
default:
|
||||
menu = StateNone
|
||||
}
|
||||
|
||||
// Draw toasts
|
||||
DrawToasts()
|
||||
}
|
||||
raylib.EndDrawing()
|
||||
}
|
||||
|
||||
// QUIT
|
||||
raylib.CloseAudioDevice()
|
||||
raylib.CloseWindow()
|
||||
|
|
22
penTool.go
Normal file
22
penTool.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
type penTool struct {
|
||||
Color raylib.Color
|
||||
Size float32
|
||||
Opacity float32
|
||||
Points []raylib.Vector2
|
||||
}
|
||||
|
||||
func (p *penTool) Draw(offset raylib.Vector2) {
|
||||
for i := 0; i < len(p.Points)-1; i++ {
|
||||
startPoint := raylib.Vector2Add(p.Points[i], offset)
|
||||
endPoint := raylib.Vector2Add(p.Points[i+1], offset)
|
||||
|
||||
raylib.DrawLineEx(startPoint, endPoint, p.Size, p.Color)
|
||||
raylib.DrawCircle(int32(startPoint.X), int32(startPoint.Y), p.Size/2, p.Color)
|
||||
}
|
||||
}
|
|
@ -1,281 +0,0 @@
|
|||
package scenes
|
||||
|
||||
import (
|
||||
"ColouringApp/application"
|
||||
"fmt"
|
||||
gui "github.com/gen2brain/raylib-go/raygui"
|
||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
const (
|
||||
ModeDrawing = iota
|
||||
ModeLine
|
||||
)
|
||||
|
||||
type freeHand struct {
|
||||
Color raylib.Color
|
||||
Size float32
|
||||
Opacity float32
|
||||
Points []raylib.Vector2
|
||||
}
|
||||
|
||||
//type line struct {
|
||||
// Color raylib.Color
|
||||
// Size float32
|
||||
// Start raylib.Vector2
|
||||
// End raylib.Vector2
|
||||
//}
|
||||
|
||||
func Drawing() {
|
||||
var (
|
||||
camera = raylib.NewCamera2D(raylib.NewVector2(0, 0), raylib.NewVector2(0, 0), 0, 1)
|
||||
//cameraMoveOffset = raylib.NewVector2(10, 10)
|
||||
|
||||
canvasSize = raylib.NewVector2(500, 430)
|
||||
canvasScale = float32(1)
|
||||
canvas = raylib.LoadRenderTexture(int32(canvasSize.X), int32(canvasSize.Y))
|
||||
canvasRefresh = true
|
||||
|
||||
sidePanelWidth = float32(300)
|
||||
sidePanelRelativeX = application.WindowWidth - int32(sidePanelWidth)
|
||||
|
||||
drawing = false
|
||||
//drawingMode = ModeDrawing
|
||||
|
||||
currentStroke = freeHand{}
|
||||
strokes = []freeHand{currentStroke}
|
||||
undoneStrokes = []freeHand{}
|
||||
|
||||
colourPickerVal = raylib.Orange
|
||||
colourPickerHeight = float32(200)
|
||||
|
||||
brushSize = float32(10)
|
||||
brushOpacity = float32(1)
|
||||
|
||||
fileName = "NewProject"
|
||||
fileNameEditing = false
|
||||
)
|
||||
|
||||
application.WindowWidth = int32(raylib.GetScreenWidth())
|
||||
application.WindowHeight = int32(raylib.GetScreenHeight())
|
||||
|
||||
refreshCanvas := func() {
|
||||
raylib.BeginTextureMode(canvas)
|
||||
raylib.ClearBackground(raylib.White)
|
||||
|
||||
for i := 1; i < len(strokes); i++ {
|
||||
for j := 1; j < len(strokes[i].Points); j++ {
|
||||
startPos := raylib.Vector2Subtract(strokes[i].Points[j-1], raylib.NewVector2(10, 10))
|
||||
endPos := raylib.Vector2Subtract(strokes[i].Points[j], raylib.NewVector2(10, 10))
|
||||
raylib.DrawLineEx(startPos, endPos, strokes[i].Size, raylib.Fade(strokes[i].Color, strokes[i].Opacity))
|
||||
raylib.DrawCircle(int32(endPos.X), int32(endPos.Y), strokes[i].Size/2, raylib.Fade(strokes[i].Color, strokes[i].Opacity))
|
||||
}
|
||||
}
|
||||
|
||||
raylib.EndTextureMode()
|
||||
}
|
||||
|
||||
redoStroke := func() {
|
||||
if len(undoneStrokes) > 0 {
|
||||
strokes = append(strokes, undoneStrokes[len(undoneStrokes)-1])
|
||||
undoneStrokes = undoneStrokes[:len(undoneStrokes)-1]
|
||||
|
||||
application.AddToast("Redo")
|
||||
canvasRefresh = true
|
||||
}
|
||||
}
|
||||
undoStroke := func() {
|
||||
// 1 because I dont know why
|
||||
if len(strokes) > 1 {
|
||||
undoneStrokes = append(undoneStrokes, strokes[len(strokes)-1])
|
||||
strokes = strokes[:len(strokes)-1]
|
||||
|
||||
canvasRefresh = true
|
||||
application.AddToast("Undo")
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
// DEFAULT
|
||||
{
|
||||
application.ShouldQuit = raylib.WindowShouldClose()
|
||||
if application.CurrentScene != application.SceneDrawing {
|
||||
break
|
||||
}
|
||||
if raylib.IsWindowResized() {
|
||||
application.WindowWidth = int32(raylib.GetScreenWidth())
|
||||
application.WindowHeight = int32(raylib.GetScreenHeight())
|
||||
|
||||
sidePanelRelativeX = application.WindowWidth - int32(sidePanelWidth)
|
||||
}
|
||||
}
|
||||
|
||||
// INPUT
|
||||
{
|
||||
if raylib.GetMouseWheelMove() != 0 && !drawing {
|
||||
canvasScale += 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.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
|
||||
currentStroke = freeHand{
|
||||
Color: colourPickerVal,
|
||||
Size: brushSize,
|
||||
Opacity: 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && drawing {
|
||||
var safeZone float32 = 5
|
||||
|
||||
if len(currentStroke.Points) <= 1 {
|
||||
currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition())
|
||||
} else if raylib.Vector2Distance(currentStroke.Points[len(currentStroke.Points)-1], raylib.GetMousePosition()) > safeZone {
|
||||
currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition())
|
||||
}
|
||||
}
|
||||
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
|
||||
strokes = append(strokes, currentStroke)
|
||||
currentStroke = freeHand{}
|
||||
undoneStrokes = []freeHand{}
|
||||
|
||||
drawing = false
|
||||
canvasRefresh = true
|
||||
}
|
||||
|
||||
if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyDown(raylib.KeyLeftShift) && raylib.IsKeyPressed(raylib.KeyZ) {
|
||||
redoStroke()
|
||||
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyZ) {
|
||||
undoStroke()
|
||||
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyS) {
|
||||
saveImage()
|
||||
}
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
{
|
||||
if drawing {
|
||||
gui.SetState(gui.STATE_DISABLED)
|
||||
} else {
|
||||
gui.SetState(gui.STATE_NORMAL)
|
||||
}
|
||||
|
||||
if canvasRefresh {
|
||||
refreshCanvas()
|
||||
canvasRefresh = false
|
||||
}
|
||||
|
||||
application.UpdateToasts()
|
||||
}
|
||||
|
||||
// DRAW
|
||||
raylib.BeginDrawing()
|
||||
{
|
||||
raylib.ClearBackground(raylib.White)
|
||||
gui.Grid(raylib.NewRectangle(0, 0, float32(application.WindowWidth), float32(application.WindowHeight)), "", 30, 1, &raylib.Vector2{})
|
||||
|
||||
// Canvas stuff
|
||||
raylib.BeginMode2D(camera)
|
||||
{
|
||||
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.BeginScissorMode(10, 10, int32(canvasSize.X), int32(canvasSize.Y))
|
||||
for i := 1; i < len(currentStroke.Points); i++ {
|
||||
raylib.DrawLineEx(currentStroke.Points[i-1], currentStroke.Points[i], currentStroke.Size, raylib.Fade(currentStroke.Color, currentStroke.Opacity))
|
||||
raylib.DrawCircle(int32(currentStroke.Points[i].X), int32(currentStroke.Points[i].Y), currentStroke.Size/2, raylib.Fade(currentStroke.Color, currentStroke.Opacity))
|
||||
}
|
||||
//raylib.EndScissorMode()
|
||||
|
||||
if drawing {
|
||||
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.DarkGray)
|
||||
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
|
||||
} else {
|
||||
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.Gray)
|
||||
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), brushSize/2, raylib.Black)
|
||||
}
|
||||
}
|
||||
raylib.EndMode2D()
|
||||
|
||||
// UI stuff
|
||||
raylib.BeginScissorMode(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight)
|
||||
{
|
||||
raylib.DrawRectangle(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight, raylib.Fade(raylib.White, 0.7))
|
||||
|
||||
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+10), 10, 25, 25), gui.IconText(gui.ICON_CROSS, "")) {
|
||||
application.CurrentScene = application.SceneTitle
|
||||
}
|
||||
if gui.Button(raylib.NewRectangle(float32(sidePanelRelativeX+20+25), 10, 25, 25), gui.IconText(gui.ICON_FOLDER_SAVE, "")) {
|
||||
saveImage()
|
||||
}
|
||||
|
||||
if gui.Button(raylib.NewRectangle(float32(application.WindowWidth-70), 10, 25, 25), gui.IconText(gui.ICON_UNDO, "")) {
|
||||
undoStroke()
|
||||
}
|
||||
if gui.Button(raylib.NewRectangle(float32(application.WindowWidth-35), 10, 25, 25), gui.IconText(gui.ICON_REDO, "")) {
|
||||
redoStroke()
|
||||
}
|
||||
|
||||
colourPickerVal = gui.ColorPicker(raylib.NewRectangle(float32(sidePanelRelativeX+10), 45, sidePanelWidth-45, colourPickerHeight), "Color", colourPickerVal)
|
||||
|
||||
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 55+colourPickerHeight, 60, 20), "Brush Size")
|
||||
brushSize = gui.Slider(raylib.NewRectangle(float32(sidePanelRelativeX+80), 55+colourPickerHeight, sidePanelWidth-90, 20), "", "", brushSize, 1, 100)
|
||||
|
||||
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 85+colourPickerHeight, 60, 20), "Brush Opacity")
|
||||
brushOpacity = gui.Slider(raylib.NewRectangle(float32(sidePanelRelativeX+80), 85+colourPickerHeight, sidePanelWidth-90, 20), "", "", brushOpacity, 0, 1)
|
||||
|
||||
gui.Label(raylib.NewRectangle(float32(sidePanelRelativeX+10), 115+colourPickerHeight, 60, 20), "File Name")
|
||||
if gui.TextBox(raylib.NewRectangle(float32(sidePanelRelativeX+80), 115+colourPickerHeight, sidePanelWidth-90, 20), &fileName, 40, fileNameEditing) {
|
||||
fileNameEditing = !fileNameEditing
|
||||
}
|
||||
}
|
||||
raylib.EndScissorMode()
|
||||
raylib.DrawRectangleLines(sidePanelRelativeX, 0, int32(sidePanelWidth), application.WindowHeight, raylib.Gray)
|
||||
|
||||
// Info
|
||||
{
|
||||
var text string
|
||||
|
||||
text = fmt.Sprintf("Strokes: %d | Points: %d", len(strokes), len(currentStroke.Points))
|
||||
gui.StatusBar(raylib.NewRectangle(0, float32(application.WindowHeight-20), 200, 20), text)
|
||||
|
||||
text = fmt.Sprintf("Canvas Size: %dx%d | Scale: %v", int32(canvasSize.X), int32(canvasSize.Y), canvasScale)
|
||||
gui.StatusBar(raylib.NewRectangle(199, float32(application.WindowHeight-20), 200, 20), text)
|
||||
}
|
||||
|
||||
// Draw toasts
|
||||
application.DrawToasts()
|
||||
}
|
||||
raylib.EndDrawing()
|
||||
}
|
||||
|
||||
// unload resources here
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
package scenes
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
package scenes
|
||||
|
||||
import (
|
||||
"ColouringApp/application"
|
||||
"fmt"
|
||||
|
||||
gui "github.com/gen2brain/raylib-go/raygui"
|
||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
func Options() {
|
||||
var (
|
||||
titlePos float32 = 10
|
||||
titleForwardPos float32 = 45
|
||||
|
||||
centerPos float32 = 10
|
||||
backPos = float32(-application.WindowWidth + 10)
|
||||
forwardPos = float32(application.WindowWidth + 10)
|
||||
|
||||
rootPanel = true
|
||||
controlsPanel = false
|
||||
graphicPanel = false
|
||||
|
||||
rootPos = centerPos
|
||||
controlsPos = forwardPos
|
||||
graphicPos = forwardPos
|
||||
|
||||
titleControls = titleForwardPos
|
||||
titleGraphics = titleForwardPos
|
||||
)
|
||||
// load resources here
|
||||
|
||||
fmt.Println("Options")
|
||||
|
||||
for !application.ShouldQuit {
|
||||
application.ShouldQuit = raylib.WindowShouldClose()
|
||||
if application.CurrentScene != application.SceneOptions {
|
||||
break
|
||||
}
|
||||
|
||||
if rootPanel {
|
||||
rootPos = raylib.Lerp(rootPos, centerPos, 0.1)
|
||||
} else {
|
||||
rootPos = raylib.Lerp(rootPos, backPos, 0.1)
|
||||
}
|
||||
if controlsPanel {
|
||||
controlsPos = raylib.Lerp(controlsPos, centerPos, 0.1)
|
||||
titleControls = raylib.Lerp(titleControls, titlePos, 0.1)
|
||||
} else {
|
||||
controlsPos = raylib.Lerp(controlsPos, forwardPos, 0.1)
|
||||
titleControls = raylib.Lerp(titleControls, titleForwardPos, 0.1)
|
||||
}
|
||||
if graphicPanel {
|
||||
graphicPos = raylib.Lerp(graphicPos, centerPos, 0.1)
|
||||
titleGraphics = raylib.Lerp(titleGraphics, titlePos, 0.1)
|
||||
} else {
|
||||
graphicPos = raylib.Lerp(graphicPos, forwardPos, 0.1)
|
||||
titleGraphics = raylib.Lerp(titleGraphics, titleForwardPos, 0.1)
|
||||
}
|
||||
|
||||
raylib.BeginDrawing()
|
||||
raylib.ClearBackground(raylib.Black)
|
||||
|
||||
raylib.DrawText("Options", 10, 10, 20, raylib.White)
|
||||
raylib.BeginScissorMode(0, 0, application.WindowWidth, 40)
|
||||
raylib.DrawText("| Controls", 95, int32(titleControls), 20, raylib.White)
|
||||
raylib.DrawText("| Graphics", 95, int32(titleGraphics), 20, raylib.White)
|
||||
raylib.EndScissorMode()
|
||||
|
||||
raylib.DrawLine(10, 40, 790, 40, raylib.White)
|
||||
if gui.Button(raylib.NewRectangle(float32(application.WindowWidth-110), 10, 100, 20), "Main Menu") {
|
||||
application.CurrentScene = application.SceneTitle
|
||||
}
|
||||
|
||||
// ROOT PANEL FOR SETTINGS
|
||||
if gui.Button(raylib.NewRectangle(rootPos, 50, 100, 20), "Controls") {
|
||||
rootPanel = false
|
||||
controlsPanel = true
|
||||
}
|
||||
if gui.Button(raylib.NewRectangle(rootPos, 80, 100, 20), "Graphics") {
|
||||
rootPanel = false
|
||||
graphicPanel = true
|
||||
}
|
||||
|
||||
// CONTROLS PANEL
|
||||
raylib.DrawText("Controls", int32(controlsPos), 50, 20, raylib.White)
|
||||
if gui.Button(raylib.NewRectangle(controlsPos, 80, 100, 20), "Back") {
|
||||
rootPanel = true
|
||||
controlsPanel = false
|
||||
}
|
||||
|
||||
// GRAPHICS PANEL
|
||||
raylib.DrawText("Graphics", int32(graphicPos), 50, 20, raylib.White)
|
||||
if gui.Button(raylib.NewRectangle(graphicPos, 80, 100, 20), "Back") {
|
||||
rootPanel = true
|
||||
graphicPanel = false
|
||||
}
|
||||
|
||||
raylib.EndDrawing()
|
||||
}
|
||||
|
||||
// unload resources here
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package scenes
|
||||
|
||||
import (
|
||||
"ColouringApp/application"
|
||||
"os"
|
||||
|
||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
func PlayerData() {
|
||||
// Load player data here
|
||||
for !application.ShouldQuit {
|
||||
// DEFAULT
|
||||
{
|
||||
application.ShouldQuit = raylib.WindowShouldClose()
|
||||
if application.CurrentScene != application.ScenePlayerData {
|
||||
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.ClearBackground(raylib.Black)
|
||||
raylib.DrawText("Loading...", 10, application.WindowHeight-30, 20, raylib.White)
|
||||
raylib.EndDrawing()
|
||||
}
|
||||
|
||||
application.CurrentScene = application.SceneTitle
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
package scenes
|
||||
|
||||
import (
|
||||
"ColouringApp/application"
|
||||
"os"
|
||||
|
||||
gui "github.com/gen2brain/raylib-go/raygui"
|
||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
func Title() {
|
||||
var (
|
||||
titleText = application.WindowTitle
|
||||
gallery []raylib.Texture2D
|
||||
)
|
||||
|
||||
application.WindowWidth = int32(raylib.GetScreenWidth())
|
||||
application.WindowHeight = int32(raylib.GetScreenHeight())
|
||||
|
||||
// Load gallery here
|
||||
files, err := os.ReadDir(application.DirUserData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
continue
|
||||
}
|
||||
gallery = append(gallery, raylib.LoadTexture(application.DirUserData+file.Name()))
|
||||
}
|
||||
|
||||
for !application.ShouldQuit {
|
||||
// DEFAULT
|
||||
{
|
||||
application.ShouldQuit = raylib.WindowShouldClose()
|
||||
if application.CurrentScene != application.SceneTitle {
|
||||
break
|
||||
}
|
||||
if raylib.IsWindowResized() {
|
||||
application.WindowWidth = int32(raylib.GetScreenWidth())
|
||||
application.WindowHeight = int32(raylib.GetScreenHeight())
|
||||
}
|
||||
}
|
||||
|
||||
// INPUT
|
||||
|
||||
// UPDATE
|
||||
{
|
||||
application.UpdateToasts()
|
||||
}
|
||||
|
||||
// DRAW
|
||||
{
|
||||
raylib.BeginDrawing()
|
||||
raylib.ClearBackground(raylib.White)
|
||||
|
||||
if gui.Button(raylib.NewRectangle(10, 10, 40, 40), gui.IconText(gui.ICON_CROSS, "")) {
|
||||
application.ShouldQuit = true
|
||||
}
|
||||
raylib.DrawText(titleText, (application.WindowWidth-raylib.MeasureText(titleText, 20))/2, 20, 20, raylib.Black)
|
||||
if gui.Button(raylib.NewRectangle(float32(application.WindowWidth-50), 10, 40, 40), gui.IconText(gui.ICON_GEAR, "")) {
|
||||
application.CurrentScene = application.SceneOptions
|
||||
}
|
||||
|
||||
for i := 0; i < len(gallery); i++ {
|
||||
raylib.DrawTexturePro(gallery[i], raylib.NewRectangle(0, 0, float32(gallery[i].Width), float32(-gallery[i].Height)), raylib.NewRectangle(float32(10+(i%5)*100), float32(70+(i/5)*100), 100, 100), raylib.Vector2{}, 0, raylib.White)
|
||||
}
|
||||
|
||||
if gui.Button(raylib.NewRectangle(float32((application.WindowWidth-100)/2), float32(application.WindowHeight-70), 100, 40), "Start") {
|
||||
application.CurrentScene = application.SceneDrawing
|
||||
}
|
||||
|
||||
application.DrawToasts()
|
||||
|
||||
raylib.EndDrawing()
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < len(gallery); i++ {
|
||||
raylib.UnloadTexture(gallery[i])
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package application
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -7,22 +7,25 @@ import (
|
|||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
var (
|
||||
toasts = []Toast{}
|
||||
toastHeight = float32(0)
|
||||
toastMaxAge = 5 * time.Second
|
||||
const (
|
||||
toastMaxAge = 1 * time.Second
|
||||
)
|
||||
|
||||
type Toast struct {
|
||||
var (
|
||||
toasts = []toast{}
|
||||
toastHeight = float32(0)
|
||||
)
|
||||
|
||||
type toast struct {
|
||||
Text string
|
||||
Age time.Time
|
||||
}
|
||||
|
||||
func AddToast(text string) {
|
||||
toast := Toast{Text: text, Age: time.Now()}
|
||||
toast := toast{Text: text, Age: time.Now()}
|
||||
toasts = append(toasts, toast)
|
||||
|
||||
fmt.Printf("Added Toast: '%s'\n", text)
|
||||
fmt.Printf("Added toast: '%s'\n", text)
|
||||
}
|
||||
|
||||
func UpdateToasts() {
|
||||
|
@ -32,10 +35,10 @@ func UpdateToasts() {
|
|||
toastHeight = raylib.Lerp(toastHeight, 0, 0.1)
|
||||
}
|
||||
|
||||
for i := 0; i < len(toasts); i++ {
|
||||
for i := 0; i < len(toasts); i += 1 {
|
||||
if time.Since(toasts[i].Age) > toastMaxAge {
|
||||
toasts = append(toasts[:i], toasts[i+1:]...)
|
||||
i--
|
||||
i -= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +47,8 @@ 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))
|
||||
//text := fmt.Sprintf("%s (%s)", toasts[i].Text, time.Since(toasts[i].Age).Round(time.Second))
|
||||
text := toasts[i].Text
|
||||
raylib.DrawText(text, 10, int32(20*i)+10, 10, raylib.White)
|
||||
}
|
||||
raylib.EndScissorMode()
|
|
@ -1,4 +1,4 @@
|
|||
package application
|
||||
package main
|
||||
|
||||
var (
|
||||
WindowTitle = "Colouring App"
|
||||
|
@ -8,10 +8,9 @@ var (
|
|||
)
|
||||
|
||||
const (
|
||||
ScenePlayerData = iota
|
||||
SceneTitle
|
||||
SceneOptions
|
||||
SceneDrawing
|
||||
StateNone = iota
|
||||
StateFileMenu
|
||||
StateDrawing
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -20,6 +19,5 @@ const (
|
|||
)
|
||||
|
||||
var (
|
||||
ShouldQuit = false
|
||||
CurrentScene = ScenePlayerData
|
||||
ShouldQuit = false
|
||||
)
|
Loading…
Reference in a new issue