~ghost08/ink

f92db8d7ff11854e81067020bd0ed290998aadc9 — VladimĂ­r Magyar 3 years ago a15cf06
Add op DrawImage
5 files changed, 114 insertions(+), 34 deletions(-)

A example/Makefile
A example/gopher.png
M example/main.go
R inkview.go => ink.go
M op/op.go
A example/Makefile => example/Makefile +12 -0
@@ 0,0 1,12 @@
dist: example.app
	while [ ! -b /dev/sdc ]; do sleep 1; done
	sudo mount -o gid=micro,fmask=113,dmask=002 /dev/sdc /mnt
	cp example.app /mnt/applications
	sudo umount /mnt
	notify-send -t 5000 'ink example' 'example copied to /dev/sdc'

clean:
	rm example.app

example.app:
	CC=/home/micro/Projects/ink/SDK_6.3.0/SDK-B288/usr/bin/arm-linux-gcc GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 go build -x -o example.app

A example/gopher.png => example/gopher.png +0 -0
M example/main.go => example/main.go +58 -34
@@ 4,23 4,29 @@ package main

import (
	"fmt"
	"image"
	"image/color"
	_ "image/png"
	"log"
	"os"
	"time"

	"git.sr.ht/~ghost08/ink"
	"git.sr.ht/~ghost08/ink/font"
	"git.sr.ht/~ghost08/ink/layout"
	"git.sr.ht/~ghost08/ink/layout/widget"
	"git.sr.ht/~ghost08/ink/op"
	"git.sr.ht/~ghost08/ink/op/screen"
	"git.sr.ht/~ghost08/ink/op/ui"
)

var (
	LiberationSans40 *font.Font
	clickCounter     = 0
	button1          = new(widget.Clickable)
	button1Text      = "Click me!"
	button2          = new(widget.Clickable)
	LiberationSans200 *font.Font
	clickCounter      = 0
	button1           = new(widget.Clickable)
	button1Text       = "Click me!"
	button2           = new(widget.Clickable)
	img               image.Image
)

func main() {


@@ 29,7 35,15 @@ func main() {
}

func initApp() {
	LiberationSans40 = font.OpenFont("LiberationSans", 40, false)
	LiberationSans200 = font.OpenFont("LiberationSans", 200, false)
	gopher, err := os.Open("gopher.png")
	if err != nil {
		log.Panic(err)
	}
	img, _, err = image.Decode(gopher)
	if err != nil {
		log.Panic(err)
	}
}

func eventLoop() {


@@ 53,38 67,48 @@ func eventLoop() {
					Dt:    time.Second * 5,
				})
			}
			layout.UniformInset(30).Layout(gtx,
				func(gtx layout.Context) layout.Dimensions {
					return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
						layout.Rigid(button(button1Text)),
						layout.Rigid(label(ink.DeviceModel())),
						layout.Rigid(label("Device:")),
						layout.Rigid(label(ink.SoftwareVersion())),
						layout.Rigid(label(ink.HwAddress())),
						layout.Rigid(button(fmt.Sprintf("message %d", clickCounter))),
					)
				},
			)
			gtx.Ops.Add(op.DrawImage{
				Image:    img,
				DestSize: image.Pt(screen.Size().X, screen.Size().Y),
			})
			/*
				layout.UniformInset(30).Layout(gtx,
					func(gtx layout.Context) layout.Dimensions {
						return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
							layout.Rigid(row(ink.DeviceModel())),
							layout.Rigid(row("Device:")),
							layout.Rigid(row(ink.SoftwareVersion())),
							layout.Rigid(row(ink.HwAddress())),
							layout.Rigid(row(ink.DeviceModel())),
							layout.Rigid(row("Device:")),
							layout.Rigid(row(ink.SoftwareVersion())),
							layout.Rigid(row(ink.HwAddress())),
							layout.Rigid(row(ink.DeviceModel())),
							layout.Rigid(row("Device:")),
							layout.Rigid(row(ink.SoftwareVersion())),
							layout.Rigid(row(ink.HwAddress())),
							layout.Rigid(row(ink.DeviceModel())),
							layout.Rigid(row("Device:")),
							layout.Rigid(row(ink.SoftwareVersion())),
							layout.Rigid(row(ink.HwAddress())),
						)
					},
				)
			*/
			gtx.Ops.Add(screen.SoftUpdate{})
			gtx.Do()
		}
	}
}

func label(s string) layout.Widget {
	return widget.Label{
		Font:  LiberationSans40,
		Color: color.Black,
		Text:  s,
	}.Layout
}

func button(s string) layout.Widget {
	return widget.Button{
		Clickable:  button1,
		Text:       s,
		Font:       LiberationSans40,
		Color:      color.White,
		Background: color.Gray{0xaa},
	}.Layout
func row(s string) layout.Widget {
	return func(gtx layout.Context) layout.Dimensions {
		return layout.UniformInset(50).Layout(gtx,
			widget.Label{
				Font:  LiberationSans200,
				Color: color.Black,
				Text:  s,
			}.Layout,
		)
	}
}

R inkview.go => ink.go +0 -0
M op/op.go => op/op.go +44 -0
@@ 5,6 5,8 @@ package op
import (
	"image"
	"image/color"
	"log"
	"unsafe"

	"git.sr.ht/~ghost08/ink"
	"git.sr.ht/~ghost08/ink/font"


@@ 267,3 269,45 @@ func (o DrawSelection) Do(s OpState) OpState {
	)
	return s
}

type DrawImage struct {
	Image    image.Image
	DestSize image.Point
}

func (o DrawImage) Do(s OpState) OpState {
	var data unsafe.Pointer
	var format, stride C.int
	switch img := o.Image.(type) {
	case *image.RGBA:
		format = C.IMAGE_RGB
		stride = C.int(img.Stride / 4 * 3)
		data = C.CBytes(removeAlpha(img.Pix))
		defer C.free(data)
	case *image.NRGBA:
		format = C.IMAGE_RGB
		stride = C.int(img.Stride / 4 * 3)
		data = C.CBytes(removeAlpha(img.Pix))
		defer C.free(data)
	default:
		log.Printf("%T", o.Image)
	}
	C.Stretch(
		(*C.uchar)(data), format,
		C.int(o.Image.Bounds().Max.X), C.int(o.Image.Bounds().Max.Y), stride,
		C.int(s.Position.X), C.int(s.Position.Y),
		C.int(o.DestSize.X), C.int(o.DestSize.Y),
		C.ROTATE0,
	)
	return s
}

func removeAlpha(pix []byte) []byte {
	ret := make([]byte, len(pix)/4*3)
	for i := 0; i < len(pix)/4; i++ {
		ret[i*3] = pix[i*4]
		ret[i*3+1] = pix[i*4+1]
		ret[i*3+2] = pix[i*4+2]
	}
	return ret
}