mirror of
https://github.com/Fluffy-Bean/ColouringApp.git
synced 2025-01-29 15:48:27 +00:00
working brush stuff
This commit is contained in:
parent
58d073aa7e
commit
40c43126ca
|
@ -9,7 +9,7 @@ const (
|
|||
WindowHeight = 600
|
||||
|
||||
// WindowFPS Max FPS the game should run at, used to calucalte delta time
|
||||
WindowFPS = 60
|
||||
WindowFPS = 144
|
||||
)
|
||||
|
||||
// Scene IDs used to determine which scene to run
|
||||
|
@ -18,20 +18,19 @@ const (
|
|||
SceneTitle
|
||||
SceneOptions
|
||||
SceneGallery
|
||||
SceneDraw
|
||||
SceneGame
|
||||
SceneDrawing
|
||||
)
|
||||
|
||||
// Directories used to store assets
|
||||
const (
|
||||
DirAssets = "./assets/"
|
||||
DirPlayerData = "./playerData/"
|
||||
DirAssets = "./assets/"
|
||||
DirUserData = "./userData/"
|
||||
)
|
||||
|
||||
var (
|
||||
// ShouldQuit is used to determine if the game should quit
|
||||
ShouldQuit = false
|
||||
|
||||
// CurrentScene is the scene that is currently running
|
||||
// CurrentScene is the scene that is currently running, defaults to loading player data
|
||||
CurrentScene = ScenePlayerData
|
||||
)
|
||||
|
|
6
main.go
6
main.go
|
@ -14,7 +14,7 @@ func main() {
|
|||
raylib.InitAudioDevice()
|
||||
|
||||
raylib.SetTargetFPS(application.WindowFPS)
|
||||
raylib.SetExitKey(0) // disable exit key
|
||||
//raylib.SetExitKey(0) // disable exit key
|
||||
|
||||
// MAIN LOOP
|
||||
for !application.ShouldQuit {
|
||||
|
@ -25,8 +25,8 @@ func main() {
|
|||
scenes.Title()
|
||||
case application.SceneOptions:
|
||||
scenes.Options()
|
||||
case application.SceneGame:
|
||||
scenes.Game()
|
||||
case application.SceneDrawing:
|
||||
scenes.Drawing()
|
||||
default:
|
||||
panic("Unknown scene")
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
package scenes
|
120
scenes/drawing.go
Normal file
120
scenes/drawing.go
Normal file
|
@ -0,0 +1,120 @@
|
|||
package scenes
|
||||
|
||||
import (
|
||||
"ColouringApp/application"
|
||||
gui "github.com/gen2brain/raylib-go/raygui"
|
||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
type stroke struct {
|
||||
Color raylib.Color
|
||||
Size float32
|
||||
Points []raylib.Vector2
|
||||
}
|
||||
|
||||
func Drawing() {
|
||||
var (
|
||||
canvasSize = raylib.NewVector2(500, 430)
|
||||
canvas = raylib.LoadRenderTexture(int32(canvasSize.X), int32(canvasSize.Y))
|
||||
currentStroke = stroke{}
|
||||
strokes = []stroke{currentStroke}
|
||||
undoneStrokes = []stroke{}
|
||||
|
||||
brushSize float32 = 1
|
||||
color = raylib.Orange
|
||||
)
|
||||
|
||||
// Create canvas
|
||||
raylib.BeginTextureMode(canvas)
|
||||
raylib.ClearBackground(raylib.White)
|
||||
raylib.EndTextureMode()
|
||||
|
||||
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, strokes[i].Color)
|
||||
raylib.DrawCircle(int32(endPos.X), int32(endPos.Y), strokes[i].Size/2, strokes[i].Color)
|
||||
}
|
||||
}
|
||||
raylib.EndTextureMode()
|
||||
}
|
||||
|
||||
for !application.ShouldQuit {
|
||||
application.ShouldQuit = raylib.WindowShouldClose()
|
||||
if application.CurrentScene != application.SceneDrawing {
|
||||
break
|
||||
}
|
||||
|
||||
// INPUT
|
||||
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) {
|
||||
currentStroke = stroke{
|
||||
Color: color,
|
||||
Size: brushSize,
|
||||
}
|
||||
}
|
||||
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) {
|
||||
// if mouse is further than 5 pixels from last point, add it
|
||||
var safeZone float32 = 5
|
||||
if len(currentStroke.Points) == 0 {
|
||||
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())
|
||||
}
|
||||
//currentStroke.Points = append(currentStroke.Points, raylib.GetMousePosition())
|
||||
}
|
||||
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && currentStroke.Points != nil {
|
||||
strokes = append(strokes, currentStroke)
|
||||
currentStroke = stroke{}
|
||||
undoneStrokes = []stroke{}
|
||||
refreshCanvas()
|
||||
}
|
||||
|
||||
if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyDown(raylib.KeyLeftShift) && raylib.IsKeyPressed(raylib.KeyZ) {
|
||||
if len(undoneStrokes) > 0 {
|
||||
strokes = append(strokes, undoneStrokes[len(undoneStrokes)-1])
|
||||
undoneStrokes = undoneStrokes[:len(undoneStrokes)-1]
|
||||
}
|
||||
refreshCanvas()
|
||||
} else if raylib.IsKeyDown(raylib.KeyLeftControl) && raylib.IsKeyPressed(raylib.KeyZ) {
|
||||
if len(strokes) > 0 {
|
||||
undoneStrokes = append(undoneStrokes, strokes[len(strokes)-1])
|
||||
strokes = strokes[:len(strokes)-1]
|
||||
}
|
||||
refreshCanvas()
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
|
||||
// DRAW
|
||||
raylib.BeginDrawing()
|
||||
raylib.ClearBackground(raylib.LightGray)
|
||||
|
||||
raylib.BeginScissorMode(10, 10, int32(canvasSize.X), int32(canvasSize.Y))
|
||||
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)
|
||||
for i := 1; i < len(currentStroke.Points); i++ {
|
||||
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.EndScissorMode()
|
||||
raylib.DrawRectangleLines(10, 10, int32(canvasSize.X), int32(canvasSize.Y), raylib.Gray)
|
||||
|
||||
color = gui.ColorPicker(raylib.NewRectangle(float32(30+int32(canvasSize.X)), 10, application.WindowWidth-float32(65+int32(canvasSize.X)), 200), "Color", color)
|
||||
|
||||
brushSize = gui.Slider(raylib.NewRectangle(float32(90+int32(canvasSize.X)), 30+200, 200, 20), "Brush Size", "", brushSize, 1, 100)
|
||||
|
||||
raylib.DrawLine(20+int32(canvasSize.X), 10, 20+int32(canvasSize.X), 10+int32(canvasSize.Y), raylib.Gray)
|
||||
raylib.DrawLine(30+int32(canvasSize.X), 20+200, application.WindowWidth-10, 20+200, raylib.Gray)
|
||||
|
||||
if gui.Button(raylib.NewRectangle(float32(30+int32(canvasSize.X)), application.WindowHeight-35, application.WindowWidth-float32(40+int32(canvasSize.X)), 25), "Main Menu") {
|
||||
application.CurrentScene = application.SceneTitle
|
||||
}
|
||||
|
||||
raylib.EndDrawing()
|
||||
}
|
||||
|
||||
// unload resources here
|
||||
}
|
117
scenes/game.go
117
scenes/game.go
|
@ -1,117 +0,0 @@
|
|||
package scenes
|
||||
|
||||
import (
|
||||
"ColouringApp/application"
|
||||
|
||||
gui "github.com/gen2brain/raylib-go/raygui"
|
||||
raylib "github.com/gen2brain/raylib-go/raylib"
|
||||
)
|
||||
|
||||
func Game() {
|
||||
var (
|
||||
scenePaused = false
|
||||
|
||||
gridPos = raylib.NewVector2(0, 0)
|
||||
gridSize = raylib.NewVector2(100, (application.WindowHeight-20)/5)
|
||||
gridDensity float32 = 5
|
||||
|
||||
brushSize float32 = 1
|
||||
color = raylib.Orange
|
||||
canvas [][]raylib.Color
|
||||
)
|
||||
|
||||
// Create canvas
|
||||
for x := 0; x < int(gridSize.X); x++ {
|
||||
canvas = append(canvas, []raylib.Color{})
|
||||
for y := 0; y < int(gridSize.Y); y++ {
|
||||
canvas[x] = append(canvas[x], raylib.White)
|
||||
}
|
||||
}
|
||||
if gridPos.X >= 0 && gridPos.X < gridSize.X && gridPos.Y >= 0 && gridPos.Y < gridSize.Y {
|
||||
for x := int(gridPos.X - brushSize/2); x < int(gridPos.X+brushSize/2); x++ {
|
||||
for y := int(gridPos.Y - brushSize/2); y < int(gridPos.Y+brushSize/2); y++ {
|
||||
if x >= 0 && x < int(gridSize.X) && y >= 0 && y < int(gridSize.Y) {
|
||||
canvas[x][y] = color
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// load resources here
|
||||
|
||||
for !application.ShouldQuit {
|
||||
application.ShouldQuit = raylib.WindowShouldClose()
|
||||
if application.CurrentScene != application.SceneGame {
|
||||
break
|
||||
}
|
||||
if raylib.IsKeyPressed(raylib.KeyEscape) {
|
||||
scenePaused = !scenePaused
|
||||
}
|
||||
|
||||
// INPUT
|
||||
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) {
|
||||
if gridPos.X >= 0 && gridPos.X < gridSize.X && gridPos.Y >= 0 && gridPos.Y < gridSize.Y {
|
||||
for x := int(gridPos.X - brushSize/2); x < int(gridPos.X+brushSize/2); x++ {
|
||||
for y := int(gridPos.Y - brushSize/2); y < int(gridPos.Y+brushSize/2); y++ {
|
||||
if x >= 0 && x < int(gridSize.X) && y >= 0 && y < int(gridSize.Y) {
|
||||
canvas[x][y] = color
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
|
||||
// DRAW
|
||||
raylib.BeginDrawing()
|
||||
raylib.ClearBackground(raylib.Black)
|
||||
|
||||
gui.Grid(raylib.NewRectangle(10, 10, gridDensity*gridSize.X, gridDensity*gridSize.Y), "", gridDensity, 1, &gridPos)
|
||||
// Default grid doesnt show up
|
||||
raylib.DrawRectangle(0, 0, application.WindowWidth, application.WindowHeight, raylib.LightGray)
|
||||
|
||||
for x := 0; x < int(gridSize.X); x++ {
|
||||
for y := 0; y < int(gridSize.Y); y++ {
|
||||
pos := raylib.NewVector2(float32(x)*gridDensity, float32(y)*gridDensity)
|
||||
raylib.DrawRectangle(int32(pos.X)+10, int32(pos.Y)+10, int32(gridDensity), int32(gridDensity), canvas[x][y])
|
||||
}
|
||||
}
|
||||
raylib.DrawRectangleLines(10, 10, int32(gridDensity*gridSize.X), int32(gridDensity*gridSize.Y), raylib.Gray)
|
||||
|
||||
if gridPos.X >= 0 && gridPos.X < gridSize.X && gridPos.Y >= 0 && gridPos.Y < gridSize.Y {
|
||||
for x := int(gridPos.X - brushSize/2); x < int(gridPos.X+brushSize/2); x++ {
|
||||
for y := int(gridPos.Y - brushSize/2); y < int(gridPos.Y+brushSize/2); y++ {
|
||||
if x >= 0 && x < int(gridSize.X) && y >= 0 && y < int(gridSize.Y) {
|
||||
pos := raylib.NewVector2(float32(x)*gridDensity, float32(y)*gridDensity)
|
||||
raylib.DrawRectangle(int32(pos.X)+10, int32(pos.Y)+10, int32(gridDensity), int32(gridDensity), raylib.Fade(color, 0.5))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
color = gui.ColorPicker(raylib.NewRectangle(float32(30+int32(gridDensity*gridSize.X)), 10, application.WindowWidth-float32(65+int32(gridDensity*gridSize.X)), 200), "Color", color)
|
||||
|
||||
brushSize = gui.Slider(raylib.NewRectangle(float32(90+int32(gridDensity*gridSize.X)), 30+200, 200, 20), "Brush Size", "", brushSize, 1, 10)
|
||||
|
||||
raylib.DrawLine(20+int32(gridDensity*gridSize.X), 10, 20+int32(gridDensity*gridSize.X), 10+int32(gridDensity*gridSize.Y), raylib.Gray)
|
||||
raylib.DrawLine(30+int32(gridDensity*gridSize.X), 20+200, application.WindowWidth-10, 20+200, raylib.Gray)
|
||||
|
||||
if scenePaused {
|
||||
raylib.DrawRectangle(0, 0, application.WindowWidth, application.WindowHeight, raylib.Fade(raylib.Black, 0.5))
|
||||
raylib.DrawText("Paused", 10, 10, 20, raylib.White)
|
||||
raylib.DrawLine(10, 40, 790, 40, raylib.White)
|
||||
if gui.Button(raylib.NewRectangle(application.WindowWidth-110, 10, 100, 20), "Unpause") {
|
||||
scenePaused = false
|
||||
}
|
||||
|
||||
if gui.Button(raylib.NewRectangle(10, 50, 100, 20), "Main Menu") {
|
||||
application.CurrentScene = application.SceneTitle
|
||||
}
|
||||
}
|
||||
|
||||
raylib.EndDrawing()
|
||||
}
|
||||
|
||||
// unload resources here
|
||||
}
|
|
@ -43,7 +43,7 @@ func Title() {
|
|||
raylib.EndScissorMode()
|
||||
|
||||
if gui.Button(raylib.NewRectangle((application.WindowWidth-100)/2, application.WindowHeight-70, 100, 40), "Start") {
|
||||
application.CurrentScene = application.SceneGame
|
||||
application.CurrentScene = application.SceneDrawing
|
||||
}
|
||||
|
||||
raylib.EndDrawing()
|
||||
|
|
Loading…
Reference in a new issue