Initial commit
This commit is contained in:
commit
e429ef2a70
9 changed files with 233 additions and 0 deletions
19
.gitignore
vendored
Normal file
19
.gitignore
vendored
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
go.work.sum
|
||||
|
||||
# env file
|
||||
.env
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2025 Zervó Zadachin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# GioUtils
|
||||
|
||||
Various utilities for the Gio UI framework.
|
15
go.mod
Normal file
15
go.mod
Normal file
|
@ -0,0 +1,15 @@
|
|||
module gioutils
|
||||
|
||||
go 1.23.5
|
||||
|
||||
require gioui.org v0.8.0
|
||||
|
||||
require (
|
||||
gioui.org/shader v1.0.8 // indirect
|
||||
github.com/go-text/typesetting v0.2.1 // indirect
|
||||
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect
|
||||
golang.org/x/exp/shiny v0.0.0-20240707233637-46b078467d37 // indirect
|
||||
golang.org/x/image v0.18.0 // indirect
|
||||
golang.org/x/sys v0.22.0 // indirect
|
||||
golang.org/x/text v0.16.0 // indirect
|
||||
)
|
21
go.sum
Normal file
21
go.sum
Normal file
|
@ -0,0 +1,21 @@
|
|||
eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d h1:ARo7NCVvN2NdhLlJE9xAbKweuI9L6UgfTbYb0YwPacY=
|
||||
eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d/go.mod h1:OYVuxibdk9OSLX8vAqydtRPP87PyTFcT9uH3MlEGBQA=
|
||||
gioui.org v0.8.0 h1:QV5p5JvsmSmGiIXVYOKn6d9YDliTfjtLlVf5J+BZ9Pg=
|
||||
gioui.org v0.8.0/go.mod h1:vEMmpxMOd/iwJhXvGVIzWEbxMWhnMQ9aByOGQdlQ8rc=
|
||||
gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=
|
||||
gioui.org/shader v1.0.8 h1:6ks0o/A+b0ne7RzEqRZK5f4Gboz2CfG+mVliciy6+qA=
|
||||
gioui.org/shader v1.0.8/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM=
|
||||
github.com/go-text/typesetting v0.2.1 h1:x0jMOGyO3d1qFAPI0j4GSsh7M0Q3Ypjzr4+CEVg82V8=
|
||||
github.com/go-text/typesetting v0.2.1/go.mod h1:mTOxEwasOFpAMBjEQDhdWRckoLLeI/+qrQeBCTGEt6M=
|
||||
github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066 h1:qCuYC+94v2xrb1PoS4NIDe7DGYtLnU2wWiQe9a1B1c0=
|
||||
github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
|
||||
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 h1:uLDX+AfeFCct3a2C7uIWBKMJIR3CJMhcgfrUAqjRK6w=
|
||||
golang.org/x/exp v0.0.0-20240707233637-46b078467d37/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
|
||||
golang.org/x/exp/shiny v0.0.0-20240707233637-46b078467d37 h1:SOSg7+sueresE4IbmmGM60GmlIys+zNX63d6/J4CMtU=
|
||||
golang.org/x/exp/shiny v0.0.0-20240707233637-46b078467d37/go.mod h1:3F+MieQB7dRYLTmnncoFbb1crS5lfQoTfDgQy6K4N0o=
|
||||
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
|
||||
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
|
||||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
7
internal/constants.go
Normal file
7
internal/constants.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package internal
|
||||
|
||||
import "image/color"
|
||||
|
||||
var (
|
||||
DefaultBgColor color.NRGBA = color.NRGBA{R: 255, G: 255, B: 255, A: 255}
|
||||
)
|
26
points/points.go
Normal file
26
points/points.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package points
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"gioui.org/layout"
|
||||
)
|
||||
|
||||
// ContextCenter returns a point representing the center of the provide layout context.
|
||||
func ContextCenter(gtx layout.Context) image.Point {
|
||||
return image.Pt(gtx.Constraints.Max.X/2, gtx.Constraints.Max.Y/2)
|
||||
}
|
||||
|
||||
// ContextCenterSquare returns two points representing the upper left corner (min) and lower right corner (max) of a square around the context center.
|
||||
func ContextCenterSquare(gtx layout.Context, apothem int) (min image.Point, max image.Point) {
|
||||
min = image.Pt(gtx.Constraints.Max.X/2-apothem, gtx.Constraints.Max.Y/2-apothem)
|
||||
max = image.Pt(gtx.Constraints.Max.X/2+apothem, gtx.Constraints.Max.Y/2+apothem)
|
||||
return
|
||||
}
|
||||
|
||||
// CenterSquare returns two points representing the upper left corner (min) and lower right corner (max) of a square around the given center point.
|
||||
func CenterSquare(center image.Point, apothem int) (min image.Point, max image.Point) {
|
||||
min = image.Pt(center.X-apothem, center.Y-apothem)
|
||||
max = image.Pt(center.X+apothem, center.Y+apothem)
|
||||
return
|
||||
}
|
62
shapes/circle.go
Normal file
62
shapes/circle.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package shapes
|
||||
|
||||
import (
|
||||
"gioutils/internal"
|
||||
"gioutils/points"
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/op/paint"
|
||||
)
|
||||
|
||||
type Circle struct {
|
||||
color color.NRGBA
|
||||
min image.Point
|
||||
max image.Point
|
||||
radius int
|
||||
}
|
||||
|
||||
// CircleFromPoints returns a new Circle based on two rectangular corners.
|
||||
func CircleFromPoints(min image.Point, max image.Point) *Circle {
|
||||
return &Circle{
|
||||
color: internal.DefaultBgColor,
|
||||
min: min,
|
||||
max: max,
|
||||
}
|
||||
}
|
||||
|
||||
// CircleFromCenter returns a new Circle based on a central point and a radius.
|
||||
func CircleFromCenter(center image.Point, radius int) *Circle {
|
||||
min, max := points.CenterSquare(center, radius)
|
||||
return &Circle{
|
||||
color: internal.DefaultBgColor,
|
||||
min: min,
|
||||
max: max,
|
||||
radius: radius,
|
||||
}
|
||||
}
|
||||
|
||||
// Color sets the color of the Circle.
|
||||
func (c *Circle) Color(color color.NRGBA) *Circle {
|
||||
c.color = color
|
||||
return c
|
||||
}
|
||||
|
||||
// Draw returns the Circle widget.
|
||||
func (c *Circle) Draw() layout.Widget {
|
||||
return func(gtx layout.Context) layout.Dimensions {
|
||||
circle := clip.Ellipse{
|
||||
Min: c.min,
|
||||
Max: c.max,
|
||||
}.Op(gtx.Ops)
|
||||
paint.FillShape(gtx.Ops, c.color, circle)
|
||||
|
||||
// width := c.max.X - c.min.X
|
||||
// height := c.max.Y - c.min.Y
|
||||
|
||||
diameter := c.radius * 2
|
||||
return layout.Dimensions{Size: image.Pt(diameter, diameter)}
|
||||
}
|
||||
}
|
59
shapes/rectangle.go
Normal file
59
shapes/rectangle.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package shapes
|
||||
|
||||
import (
|
||||
"gioutils/internal"
|
||||
"gioutils/points"
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/op/paint"
|
||||
)
|
||||
|
||||
type Rectangle struct {
|
||||
color color.NRGBA
|
||||
min image.Point
|
||||
max image.Point
|
||||
}
|
||||
|
||||
// RectangleFromPoints returns a new Rectangle based on two corner points.
|
||||
func RectangleFromPoints(min image.Point, max image.Point) *Rectangle {
|
||||
return &Rectangle{
|
||||
color: internal.DefaultBgColor,
|
||||
min: min,
|
||||
max: max,
|
||||
}
|
||||
}
|
||||
|
||||
// RectangleFromCenter returns a new Rectangle based on a central point and a given apothem.
|
||||
func RectangleFromCenter(center image.Point, apothem int) *Rectangle {
|
||||
min, max := points.CenterSquare(center, apothem)
|
||||
return &Rectangle{
|
||||
color: internal.DefaultBgColor,
|
||||
min: min,
|
||||
max: max,
|
||||
}
|
||||
}
|
||||
|
||||
// Color sets the color of the Rectangle.
|
||||
func (r *Rectangle) Color(color color.NRGBA) *Rectangle {
|
||||
r.color = color
|
||||
return r
|
||||
}
|
||||
|
||||
// Draw returns the Rectangle widget.
|
||||
func (r *Rectangle) Draw() layout.Widget {
|
||||
return func(gtx layout.Context) layout.Dimensions {
|
||||
rect := clip.Rect{
|
||||
Min: r.min,
|
||||
Max: r.max,
|
||||
}.Op()
|
||||
paint.FillShape(gtx.Ops, r.color, rect)
|
||||
|
||||
width := r.max.X - r.min.X
|
||||
height := r.max.Y - r.min.Y
|
||||
|
||||
return layout.Dimensions{Size: image.Pt(width, height)}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue