Skip to content

Commit

Permalink
Latest Release
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaaqziyan committed Nov 23, 2023
1 parent ae7c3bc commit 81fff18
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 47 deletions.
3 changes: 1 addition & 2 deletions apptype/apptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (

type BrushType = int

type PXCanvasConfig struct {
type PxCanvasConfig struct {
DrawingArea fyne.Size
CanvasOffset fyne.Position
PxRows, PxCols int
PxSize int
}

type State struct {
BrushColor color.Color
BrushType int
Expand Down
4 changes: 2 additions & 2 deletions pixl/pixl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func main() {
SwatchSelected: 0,
}

pixlCanvasConfig := apptype.PXCanvasConfig{
pixlCanvasConfig := apptype.PxCanvasConfig{
DrawingArea: fyne.NewSize(600, 600),
CanvasOffset: fyne.NewPos(0, 0),
PxRows: 10,
PxCols: 10,
PxSize: 30,
}

pixlCanvas := pxcanvas.NewPxcanvas(&state, pixlCanvasConfig)
pixlCanvas := pxcanvas.NewPxCanvas(&state, pixlCanvasConfig)

appInit := ui.AppInit{
PixlCanvas: pixlCanvas,
Expand Down
3 changes: 2 additions & 1 deletion pxcanvas/brush/brush.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
Pixel = iota
)

func Cursor(config apptype.PXCanvasConfig, brush apptype.BrushType, ev *desktop.MouseEvent, x int, y int) []fyne.CanvasObject {
func Cursor(config apptype.PxCanvasConfig, brush apptype.BrushType, ev *desktop.MouseEvent, x int, y int) []fyne.CanvasObject {
var objects []fyne.CanvasObject
switch {
case brush == Pixel:
Expand Down Expand Up @@ -47,6 +47,7 @@ func Cursor(config apptype.PXCanvasConfig, brush apptype.BrushType, ev *desktop.
}
return objects
}

func TryBrush(appState *apptype.State, canvas apptype.Brushable, ev *desktop.MouseEvent) bool {
switch {
case appState.BrushType == Pixel:
Expand Down
6 changes: 4 additions & 2 deletions pxcanvas/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ func (pxCanvas *PxCanvas) Scrolled(ev *fyne.ScrollEvent) {
func (pxCanvas *PxCanvas) MouseMoved(ev *desktop.MouseEvent) {
if x, y := pxCanvas.MouseToCanvasXY(ev); x != nil && y != nil {
brush.TryBrush(pxCanvas.appState, pxCanvas, ev)
cursor := brush.Cursor(pxCanvas.PXCanvasConfig, pxCanvas.appState.BrushType, ev, *x, *y)
cursor := brush.Cursor(pxCanvas.PxCanvasConfig, pxCanvas.appState.BrushType, ev, *x, *y)
pxCanvas.renderer.SetCursor(cursor)
pxCanvas.showMouse = false
} else {
pxCanvas.showMouse = true
pxCanvas.renderer.SetCursor(make([]fyne.CanvasObject, 0))
}
pxCanvas.TryPan(pxCanvas.mouseState.previousCoord, ev)
Expand All @@ -27,7 +29,7 @@ func (pxCanvas *PxCanvas) MouseMoved(ev *desktop.MouseEvent) {
func (pxCanvas *PxCanvas) MouseIn(ev *desktop.MouseEvent) {}
func (pxCanvas *PxCanvas) MouseOut() {}

func (pxCanvas *PxCanvas) MouseUp(ev *desktop.MouseEvent) {}
func (pxCanvas *PxCanvas) MouseDown(ev *desktop.MouseEvent) {
brush.TryBrush(pxCanvas.appState, pxCanvas, ev)
}
func (pxCanvas *PxCanvas) MouseUp(ev *desktop.MouseEvent) {}
1 change: 0 additions & 1 deletion pxcanvas/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ func (pxCanvas *PxCanvas) scale(direction int) {
}
default:
pxCanvas.PxSize = 10

}
}
50 changes: 29 additions & 21 deletions pxcanvas/pxcanvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ type PxCanvasMouseState struct {

type PxCanvas struct {
widget.BaseWidget
apptype.PXCanvasConfig
apptype.PxCanvasConfig
renderer *PxCanvasRenderer
PixelData image.Image
mouseState PxCanvasMouseState
appState *apptype.State
reloadImage bool
showMouse bool
}

func (pxCanvas *PxCanvas) Cursor() desktop.Cursor {
if pxCanvas.showMouse {
return desktop.DefaultCursor
} else {
return desktop.HiddenCursor
}
}

func (pxCanvas *PxCanvas) Bounds() image.Rectangle {
Expand All @@ -36,14 +45,14 @@ func (pxCanvas *PxCanvas) Bounds() image.Rectangle {
func InBounds(pos fyne.Position, bounds image.Rectangle) bool {
if pos.X >= float32(bounds.Min.X) &&
pos.X < float32(bounds.Max.X) &&
pos.Y >= float32(bounds.Max.Y) &&
pos.Y >= float32(bounds.Min.Y) {
pos.Y >= float32(bounds.Min.Y) &&
pos.Y < float32(bounds.Max.Y) {
return true
}
return false
}

func newBlankImage(cols, rows int, c color.Color) image.Image {
func NewBlankImage(cols, rows int, c color.Color) image.Image {
img := image.NewNRGBA(image.Rect(0, 0, cols, rows))
for y := 0; y < rows; y++ {
for x := 0; x < cols; x++ {
Expand All @@ -53,12 +62,12 @@ func newBlankImage(cols, rows int, c color.Color) image.Image {
return img
}

func NewPxcanvas(state *apptype.State, config apptype.PXCanvasConfig) *PxCanvas {
func NewPxCanvas(state *apptype.State, config apptype.PxCanvasConfig) *PxCanvas {
pxCanvas := &PxCanvas{
PXCanvasConfig: config,
PxCanvasConfig: config,
appState: state,
}
pxCanvas.PixelData = newBlankImage(pxCanvas.PxCols, pxCanvas.PxRows, color.NRGBA{128, 128, 128, 255})
pxCanvas.PixelData = NewBlankImage(pxCanvas.PxCols, pxCanvas.PxRows, color.NRGBA{128, 128, 128, 255})
pxCanvas.ExtendBaseWidget(pxCanvas)
return pxCanvas
}
Expand All @@ -68,35 +77,34 @@ func (pxCanvas *PxCanvas) CreateRenderer() fyne.WidgetRenderer {
canvasImage.ScaleMode = canvas.ImageScalePixels
canvasImage.FillMode = canvas.ImageFillContain

//border
canvasBorder := make([]canvas.Line, 4)
for i := 0; i < len(canvasBorder); i++ {
canvasBorder[i].StrokeColor = color.NRGBA{100, 100, 100, 255}
canvasBorder[i].StrokeWidth = 2
}

renderer := &PxCanvasRenderer{
PxCanvas: pxCanvas,
pxCanvas: pxCanvas,
canvasImage: canvasImage,
canvasBorder: canvasBorder,
}
pxCanvas.renderer = renderer
return renderer
}

func (pxcanvas *PxCanvas) TryPan(previousCoord *fyne.PointEvent, ev *desktop.MouseEvent) {
func (pxCanvas *PxCanvas) TryPan(previousCoord *fyne.PointEvent, ev *desktop.MouseEvent) {
if previousCoord != nil && ev.Button == desktop.MouseButtonTertiary {
pxcanvas.Pan(*previousCoord, ev.PointEvent)
pxCanvas.Pan(*previousCoord, ev.PointEvent)
}
}

// Brushable Interface
// Brushable interface
func (pxCanvas *PxCanvas) SetColor(c color.Color, x, y int) {
if nrgba, ok := pxCanvas.PixelData.(*image.NRGBA); ok {
nrgba.Set(x, y, c)
}

if rgba, ok := pxCanvas.PixelData.(*image.NRGBA); ok {
if rgba, ok := pxCanvas.PixelData.(*image.RGBA); ok {
rgba.Set(x, y, c)
}
pxCanvas.Refresh()
Expand All @@ -121,18 +129,18 @@ func (pxCanvas *PxCanvas) MouseToCanvasXY(ev *desktop.MouseEvent) (*int, *int) {
func (pxCanvas *PxCanvas) LoadImage(img image.Image) {
dimensions := img.Bounds()

pxCanvas.PXCanvasConfig.PxCols = dimensions.Dx()
pxCanvas.PXCanvasConfig.PxRows = dimensions.Dy()
pxCanvas.PxCanvasConfig.PxCols = dimensions.Dx()
pxCanvas.PxCanvasConfig.PxRows = dimensions.Dy()

pxCanvas.PixelData = img
pxCanvas.reloadImage = true
pxCanvas.Refresh()
}

func (pxcanvas *PxCanvas) NewDrawing(cols, rows int) {
pxcanvas.appState.SetFilePath("")
pxcanvas.PxCols = cols
pxcanvas.PxRows = rows
pixelData := newBlankImage(cols, rows, color.NRGBA{128, 128, 128, 255})
pxcanvas.LoadImage(pixelData)
func (pxCanvas *PxCanvas) NewDrawing(cols, rows int) {
pxCanvas.appState.SetFilePath("")
pxCanvas.PxCols = cols
pxCanvas.PxRows = rows
pixelData := NewBlankImage(cols, rows, color.NRGBA{128, 128, 128, 255})
pxCanvas.LoadImage(pixelData)
}
34 changes: 16 additions & 18 deletions pxcanvas/pxcanvasrenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type PxCanvasRenderer struct {
PxCanvas *PxCanvas
pxCanvas *PxCanvas
canvasImage *canvas.Image
canvasBorder []canvas.Line
canvasCursor []fyne.CanvasObject
Expand All @@ -16,12 +16,12 @@ func (renderer *PxCanvasRenderer) SetCursor(objects []fyne.CanvasObject) {
renderer.canvasCursor = objects
}

// WidgetRenderer
// WidgetRenderer interface implementation.
func (renderer *PxCanvasRenderer) MinSize() fyne.Size {
return renderer.PxCanvas.DrawingArea
return renderer.pxCanvas.DrawingArea
}

// PxcanvasRenderer
// WidgetRenderer interface implementation.
func (renderer *PxCanvasRenderer) Objects() []fyne.CanvasObject {
objects := make([]fyne.CanvasObject, 0, 5)
for i := 0; i < len(renderer.canvasBorder); i++ {
Expand All @@ -32,38 +32,37 @@ func (renderer *PxCanvasRenderer) Objects() []fyne.CanvasObject {
return objects
}

// Destroy
// WidgetRenderer interface implementation.
func (renderer *PxCanvasRenderer) Destroy() {}

// Main Layout
// WidgetRenderer interface implementation.
func (renderer *PxCanvasRenderer) Layout(size fyne.Size) {
renderer.LayoutCanvas(size)
renderer.LayoutBorder(size)

}

// Refresh fUnction
// WidgetRenderer interface implementation.
func (renderer *PxCanvasRenderer) Refresh() {
if renderer.PxCanvas.reloadImage {
renderer.canvasImage = canvas.NewImageFromImage(renderer.PxCanvas.PixelData)
if renderer.pxCanvas.reloadImage {
renderer.canvasImage = canvas.NewImageFromImage(renderer.pxCanvas.PixelData)
renderer.canvasImage.ScaleMode = canvas.ImageScalePixels
renderer.canvasImage.FillMode = canvas.ImageFillContain
renderer.PxCanvas.reloadImage = false
renderer.pxCanvas.reloadImage = false
}
renderer.Layout(renderer.PxCanvas.Size())
renderer.Layout(renderer.pxCanvas.Size())
canvas.Refresh(renderer.canvasImage)
}

func (renderer *PxCanvasRenderer) LayoutCanvas(size fyne.Size) {
imgPxWidth := renderer.PxCanvas.PxCols
imgPxHeight := renderer.PxCanvas.PxRows
pxSize := renderer.PxCanvas.PxSize
renderer.canvasImage.Move(fyne.NewPos(renderer.PxCanvas.CanvasOffset.X, renderer.PxCanvas.CanvasOffset.Y))
imgPxWidth := renderer.pxCanvas.PxCols
imgPxHeight := renderer.pxCanvas.PxRows
pxSize := renderer.pxCanvas.PxSize
renderer.canvasImage.Move(fyne.NewPos(renderer.pxCanvas.CanvasOffset.X, renderer.pxCanvas.CanvasOffset.Y))
renderer.canvasImage.Resize(fyne.NewSize(float32(imgPxWidth*pxSize), float32(imgPxHeight*pxSize)))
}

func (renderer *PxCanvasRenderer) LayoutBorder(size fyne.Size) {
offset := renderer.PxCanvas.CanvasOffset
offset := renderer.pxCanvas.CanvasOffset
imgHeight := renderer.canvasImage.Size().Height
imgWidth := renderer.canvasImage.Size().Width

Expand All @@ -82,5 +81,4 @@ func (renderer *PxCanvasRenderer) LayoutBorder(size fyne.Size) {
bottom := &renderer.canvasBorder[3]
bottom.Position1 = fyne.NewPos(offset.X, offset.Y+imgHeight)
bottom.Position2 = fyne.NewPos(offset.X+imgWidth, offset.Y+imgHeight)

}

0 comments on commit 81fff18

Please sign in to comment.