NEW TOOOLLL

This commit is contained in:
Michał 2024-02-09 23:09:19 +00:00
parent 59036706ea
commit afd1b0d5f7
4 changed files with 165 additions and 15 deletions

74
main.go
View file

@ -126,6 +126,30 @@ func main() {
newPenStroke = penTool{}
applicationState = StateNormal
}
case toolRectangle:
if raylib.IsMouseButtonPressed(raylib.MouseLeftButton) && applicationState == StateNormal {
if (!raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(float32(toolPanelOffset), 0, float32(toolPanelWidth+toolBarWidth), float32(applicationWindowHeight))) || !toolBarShowPanel) &&
raylib.CheckCollisionPointRec(raylib.GetMousePosition(), raylib.NewRectangle(10, 10, canvas.Size.X, canvas.Size.Y)) {
applicationState = StateDrawing
newRectangleStroke = rectangleTool{
StartPos: raylib.GetMousePosition(),
EndPos: raylib.GetMousePosition(),
Rounded: false,
Color: toolPanelColourPicker,
Size: toolPanelBrushSize,
}
}
}
if raylib.IsMouseButtonDown(raylib.MouseLeftButton) && applicationState == StateDrawing {
newRectangleStroke.EndPos = raylib.GetMousePosition()
}
if raylib.IsMouseButtonReleased(raylib.MouseLeftButton) && applicationState == StateDrawing {
canvas.AddStroke(newRectangleStroke.Render())
newRectangleStroke = rectangleTool{}
applicationState = StateNormal
}
case toolPointer:
fallthrough
default:
@ -167,13 +191,22 @@ func main() {
canvas.Draw()
raylib.BeginScissorMode(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y))
newPenStroke.Draw()
switch newStrokeType {
case toolPen:
newPenStroke.Draw()
case toolRectangle:
newRectangleStroke.Draw()
case toolPointer:
fallthrough
default:
// gwagwa
}
raylib.EndScissorMode()
raylib.DrawRectangleLines(int32(canvas.Offset.X), int32(canvas.Offset.Y), int32(canvas.Size.X), int32(canvas.Size.Y), raylib.DarkGray)
}
// Tool Panel
// Tool Bar
raylib.BeginScissorMode(toolBarOffset, 0, toolBarWidth, applicationWindowHeight)
{
raylib.DrawRectangle(toolBarOffset, 0, toolBarWidth, applicationWindowHeight, raylib.Fade(raylib.White, 0.9))
@ -202,6 +235,15 @@ func main() {
}
gui.SetState(gui.STATE_NORMAL)
if newStrokeType == toolRectangle {
gui.SetState(gui.STATE_PRESSED)
}
if gui.Button(raylib.NewRectangle(float32(toolBarOffset+10), 115, 25, 25), gui.IconText(gui.ICON_PLAYER_STOP, "")) {
newStrokeType = toolRectangle
addToast("Tool: Rectangle")
}
gui.SetState(gui.STATE_NORMAL)
// Bottom
if gui.Button(raylib.NewRectangle(float32(toolBarOffset+10), float32(applicationWindowHeight-35), 25, 25), gui.IconText(gui.ICON_INFO, "")) {
applicationState = StateHelp
@ -417,7 +459,24 @@ func main() {
// Cursor
if raylib.IsCursorOnScreen() {
switch newStrokeType {
case toolPen:
raylib.DrawCircleLines(
int32(raylib.GetMousePosition().X),
int32(raylib.GetMousePosition().Y),
toolPanelBrushSize/2,
raylib.Black,
)
case toolRectangle:
raylib.DrawRectangleLines(
int32(raylib.GetMousePosition().X-(toolPanelBrushSize/2)),
int32(raylib.GetMousePosition().Y-(toolPanelBrushSize/2)),
int32(toolPanelBrushSize),
int32(toolPanelBrushSize),
raylib.Black,
)
case toolPointer:
fallthrough
default:
// Points have to be provided in counter-clockwise order for some reason ??!?!??!?!?
raylib.DrawTriangle(
raylib.NewVector2(raylib.GetMousePosition().X+2, raylib.GetMousePosition().Y+14),
@ -432,11 +491,14 @@ func main() {
raylib.NewVector2(raylib.GetMousePosition().X+2, raylib.GetMousePosition().Y+14),
raylib.Black,
)
case toolPen:
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), toolPanelBrushSize/2, raylib.Black)
}
// Cursor point finder/guide
if newStrokeType != toolPointer {
if toolPanelBrushSize > 20 {
raylib.DrawCircle(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), 1, raylib.White)
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), 2, raylib.Black)
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), 3, raylib.Red)
} else if toolPanelBrushSize < 5 {
raylib.DrawCircleLines(int32(raylib.GetMousePosition().X), int32(raylib.GetMousePosition().Y), 10, raylib.Red)
}
}
}

View file

@ -17,14 +17,14 @@ func (p *penTool) Render() raylib.Texture2D {
raylib.BeginTextureMode(texture)
raylib.ClearBackground(raylib.Fade(raylib.Black, 0))
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)
startPointOffset := raylib.Vector2Add(p.Points[i], offset)
endPointOffset := raylib.Vector2Add(p.Points[i+1], offset)
raylib.DrawLineEx(startPointOffset, endPointOffset, p.Size, p.Color)
raylib.DrawCircle(int32(startPointOffset.X), int32(startPointOffset.Y), p.Size/2, p.Color)
}
if len(p.Points) > 0 {
endPoint := raylib.Vector2Add(p.Points[len(p.Points)-1], offset)
raylib.DrawCircle(int32(endPoint.X), int32(endPoint.Y), p.Size/2, p.Color)
endPointOffset := raylib.Vector2Add(p.Points[len(p.Points)-1], offset)
raylib.DrawCircle(int32(endPointOffset.X), int32(endPointOffset.Y), p.Size/2, p.Color)
}
raylib.EndTextureMode()

86
rectangleTool.go Normal file
View file

@ -0,0 +1,86 @@
package main
import (
raylib "github.com/gen2brain/raylib-go/raylib"
"math"
)
type rectangleTool struct {
StartPos raylib.Vector2
EndPos raylib.Vector2
Rounded bool
Color raylib.Color
Size float32
}
func (r *rectangleTool) Render() raylib.Texture2D {
offset := raylib.Vector2Scale(canvas.Offset, -1)
texture := raylib.LoadRenderTexture(int32(canvas.Size.X), int32(canvas.Size.Y))
startPosOffset := raylib.Vector2Add(r.StartPos, offset)
endPosOffset := raylib.Vector2Add(r.EndPos, offset)
raylib.BeginTextureMode(texture)
raylib.ClearBackground(raylib.Fade(raylib.Black, 0))
if r.Rounded {
// Linesss
raylib.DrawLineEx(startPosOffset, raylib.NewVector2(endPosOffset.X, startPosOffset.Y), r.Size, r.Color)
raylib.DrawLineEx(startPosOffset, raylib.NewVector2(startPosOffset.X, endPosOffset.Y), r.Size, r.Color)
raylib.DrawLineEx(endPosOffset, raylib.NewVector2(endPosOffset.X, startPosOffset.Y), r.Size, r.Color)
raylib.DrawLineEx(endPosOffset, raylib.NewVector2(startPosOffset.X, endPosOffset.Y), r.Size, r.Color)
// Roundy
raylib.DrawCircle(int32(startPosOffset.X), int32(startPosOffset.Y), r.Size/2, r.Color)
raylib.DrawCircle(int32(endPosOffset.X), int32(startPosOffset.Y), r.Size/2, r.Color)
raylib.DrawCircle(int32(startPosOffset.X), int32(endPosOffset.Y), r.Size/2, r.Color)
raylib.DrawCircle(int32(endPosOffset.X), int32(endPosOffset.Y), r.Size/2, r.Color)
} else {
startPos := raylib.NewVector2(
float32(math.Min(float64(startPosOffset.X), float64(endPosOffset.X))),
float32(math.Min(float64(startPosOffset.Y), float64(endPosOffset.Y))),
)
endPos := raylib.NewVector2(
float32(math.Max(float64(startPosOffset.X), float64(endPosOffset.X))),
float32(math.Max(float64(startPosOffset.Y), float64(endPosOffset.Y))),
)
raylib.DrawLineEx(raylib.NewVector2(startPos.X-(r.Size/2), startPos.Y), raylib.NewVector2(endPos.X+(r.Size/2), startPos.Y), r.Size, r.Color)
raylib.DrawLineEx(raylib.NewVector2(endPos.X, startPos.Y-(r.Size/2)), raylib.NewVector2(endPos.X, endPos.Y+(r.Size/2)), r.Size, r.Color)
raylib.DrawLineEx(raylib.NewVector2(startPos.X-(r.Size/2), endPos.Y), raylib.NewVector2(endPos.X+(r.Size/2), endPos.Y), r.Size, r.Color)
raylib.DrawLineEx(raylib.NewVector2(startPos.X, startPos.Y-(r.Size/2)), raylib.NewVector2(startPos.X, endPos.Y+(r.Size/2)), r.Size, r.Color)
}
raylib.EndTextureMode()
return texture.Texture
}
func (r *rectangleTool) Draw() {
if r.Rounded {
// Linesss
raylib.DrawLineEx(r.StartPos, raylib.NewVector2(r.EndPos.X, r.StartPos.Y), r.Size, r.Color)
raylib.DrawLineEx(r.StartPos, raylib.NewVector2(r.StartPos.X, r.EndPos.Y), r.Size, r.Color)
raylib.DrawLineEx(r.EndPos, raylib.NewVector2(r.EndPos.X, r.StartPos.Y), r.Size, r.Color)
raylib.DrawLineEx(r.EndPos, raylib.NewVector2(r.StartPos.X, r.EndPos.Y), r.Size, r.Color)
// Roundy
raylib.DrawCircle(int32(r.StartPos.X), int32(r.StartPos.Y), r.Size/2, r.Color)
raylib.DrawCircle(int32(r.EndPos.X), int32(r.StartPos.Y), r.Size/2, r.Color)
raylib.DrawCircle(int32(r.StartPos.X), int32(r.EndPos.Y), r.Size/2, r.Color)
raylib.DrawCircle(int32(r.EndPos.X), int32(r.EndPos.Y), r.Size/2, r.Color)
} else {
startPos := raylib.NewVector2(
float32(math.Min(float64(r.StartPos.X), float64(r.EndPos.X))),
float32(math.Min(float64(r.StartPos.Y), float64(r.EndPos.Y))),
)
endPos := raylib.NewVector2(
float32(math.Max(float64(r.StartPos.X), float64(r.EndPos.X))),
float32(math.Max(float64(r.StartPos.Y), float64(r.EndPos.Y))),
)
raylib.DrawLineEx(raylib.NewVector2(startPos.X-(r.Size/2), startPos.Y), raylib.NewVector2(endPos.X+(r.Size/2), startPos.Y), r.Size, r.Color)
raylib.DrawLineEx(raylib.NewVector2(endPos.X, startPos.Y-(r.Size/2)), raylib.NewVector2(endPos.X, endPos.Y+(r.Size/2)), r.Size, r.Color)
raylib.DrawLineEx(raylib.NewVector2(startPos.X-(r.Size/2), endPos.Y), raylib.NewVector2(endPos.X+(r.Size/2), endPos.Y), r.Size, r.Color)
raylib.DrawLineEx(raylib.NewVector2(startPos.X, startPos.Y-(r.Size/2)), raylib.NewVector2(startPos.X, endPos.Y+(r.Size/2)), r.Size, r.Color)
}
}

View file

@ -30,6 +30,7 @@ const (
const (
toolPointer = iota
toolPen
toolRectangle
)
var (
@ -42,9 +43,10 @@ var (
)
var (
newStrokeType = toolPen
newPenStroke = penTool{}
newStrokeSafeZone = 1
newStrokeType = toolPen
newPenStroke = penTool{}
newRectangleStroke = rectangleTool{}
newStrokeSafeZone = 1
)
var (