예제 #1
0
func (mw MainWindow) Create() error {
	w, err := walk.NewMainWindow()
	if err != nil {
		return err
	}

	w.SetSuspended(true)

	tlwi := topLevelWindowInfo{
		Name:               mw.Name,
		Font:               mw.Font,
		ToolTipText:        "",
		MinSize:            mw.MinSize,
		MaxSize:            mw.MaxSize,
		ContextMenuActions: mw.ContextMenuActions,
		OnKeyDown:          mw.OnKeyDown,
		OnMouseDown:        mw.OnMouseDown,
		OnMouseMove:        mw.OnMouseMove,
		OnMouseUp:          mw.OnMouseUp,
		OnSizeChanged:      mw.OnSizeChanged,
		DataBinder:         mw.DataBinder,
		Layout:             mw.Layout,
		Children:           mw.Children,
	}

	builder := NewBuilder(nil)

	return builder.InitWidget(tlwi, w, func() error {
		if err := w.SetTitle(mw.Title); err != nil {
			return err
		}

		if err := w.SetSize(mw.Size.toW()); err != nil {
			return err
		}

		if err := addToActionList(w.Menu().Actions(), mw.MenuActions); err != nil {
			return err
		}

		imageList, err := walk.NewImageList(walk.Size{16, 16}, 0)
		if err != nil {
			return err
		}
		w.ToolBar().SetImageList(imageList)

		if err := addToActionList(w.ToolBar().Actions(), mw.ToolBarActions); err != nil {
			return err
		}

		w.Show()

		if mw.AssignTo != nil {
			*mw.AssignTo = w
		}

		return nil
	})
}
예제 #2
0
func main() {
	walk.Initialize(walk.InitParams{PanicOnError: true})
	defer walk.Shutdown()

	mainWnd, _ := walk.NewMainWindow()

	mw := &MainWindow{MainWindow: mainWnd}
	mw.SetLayout(walk.NewVBoxLayout())
	mw.SetTitle("Walk Image Viewer Example")

	mw.tabWidget, _ = walk.NewTabWidget(mw)

	imageList, _ := walk.NewImageList(walk.Size{16, 16}, 0)
	mw.ToolBar().SetImageList(imageList)

	fileMenu, _ := walk.NewMenu()
	fileMenuAction, _ := mw.Menu().Actions().AddMenu(fileMenu)
	fileMenuAction.SetText("&File")

	openBmp, _ := walk.NewBitmapFromFile("../img/open.png")

	openAction := walk.NewAction()
	openAction.SetImage(openBmp)
	openAction.SetText("&Open")
	openAction.Triggered().Attach(func() { mw.openImage() })
	fileMenu.Actions().Add(openAction)
	mw.ToolBar().Actions().Add(openAction)

	exitAction := walk.NewAction()
	exitAction.SetText("E&xit")
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	fileMenu.Actions().Add(exitAction)

	helpMenu, _ := walk.NewMenu()
	helpMenuAction, _ := mw.Menu().Actions().AddMenu(helpMenu)
	helpMenuAction.SetText("&Help")

	aboutAction := walk.NewAction()
	aboutAction.SetText("&About")
	aboutAction.Triggered().Attach(func() {
		walk.MsgBox(mw, "About", "Walk Image Viewer Example", walk.MsgBoxOK|walk.MsgBoxIconInformation)
	})
	helpMenu.Actions().Add(aboutAction)

	mw.SetMinMaxSize(walk.Size{320, 240}, walk.Size{})
	mw.SetSize(walk.Size{800, 600})
	mw.Show()

	mw.Run()
}
예제 #3
0
파일: mainwindow.go 프로젝트: yahame/walk
func (mw MainWindow) Create() error {
	w, err := walk.NewMainWindow()
	if err != nil {
		return err
	}

	tlwi := topLevelWindowInfo{
		Name:               mw.Name,
		Disabled:           mw.Disabled,
		Hidden:             mw.Hidden,
		Font:               mw.Font,
		MinSize:            mw.MinSize,
		MaxSize:            mw.MaxSize,
		ContextMenuActions: mw.ContextMenuActions,
		DataBinder:         mw.DataBinder,
		Layout:             mw.Layout,
		Children:           mw.Children,
	}

	return InitWidget(tlwi, w, func() error {
		if err := w.SetTitle(mw.Title); err != nil {
			return err
		}

		if err := w.SetSize(mw.Size.toW()); err != nil {
			return err
		}

		if err := addToActionList(w.Menu().Actions(), mw.MenuActions); err != nil {
			return err
		}

		imageList, err := walk.NewImageList(walk.Size{16, 16}, 0)
		if err != nil {
			return err
		}
		w.ToolBar().SetImageList(imageList)

		if err := addToActionList(w.ToolBar().Actions(), mw.ToolBarActions); err != nil {
			return err
		}

		if mw.AssignTo != nil {
			*mw.AssignTo = w
		}

		return nil
	})
}
예제 #4
0
파일: toolbar.go 프로젝트: 2105666566/walk
func (tb ToolBar) Create(builder *Builder) (err error) {
	var w *walk.ToolBar
	if tb.Orientation == Vertical {
		w, err = walk.NewVerticalToolBar(builder.Parent())
	} else {
		w, err = walk.NewToolBar(builder.Parent())
	}
	if err != nil {
		return
	}

	return builder.InitWidget(tb, w, func() error {
		imageList, err := walk.NewImageList(walk.Size{16, 16}, 0)
		if err != nil {
			return err
		}
		w.SetImageList(imageList)

		mtr := tb.MaxTextRows
		if mtr < 1 {
			mtr = 1
		}
		if err := w.SetMaxTextRows(mtr); err != nil {
			return err
		}

		if err := addToActionList(w.Actions(), tb.Actions); err != nil {
			return err
		}

		if tb.AssignTo != nil {
			*tb.AssignTo = w
		}

		return nil
	})
}
예제 #5
0
파일: toolbar.go 프로젝트: Ryanker/walk
func (tb ToolBar) Create(builder *Builder) error {
	w, err := walk.NewToolBarWithOrientationAndButtonStyle(builder.Parent(), walk.Orientation(tb.Orientation), walk.ToolBarButtonStyle(tb.ButtonStyle))
	if err != nil {
		return err
	}

	return builder.InitWidget(tb, w, func() error {
		imageList, err := walk.NewImageList(walk.Size{16, 16}, 0)
		if err != nil {
			return err
		}
		w.SetImageList(imageList)

		mtr := tb.MaxTextRows
		if mtr < 1 {
			mtr = 1
		}
		if err := w.SetMaxTextRows(mtr); err != nil {
			return err
		}

		if len(tb.Items) > 0 {
			builder.deferBuildActions(w.Actions(), tb.Items)
		} else {
			if err := addToActionList(w.Actions(), tb.Actions); err != nil {
				return err
			}
		}

		if tb.AssignTo != nil {
			*tb.AssignTo = w
		}

		return nil
	})
}
예제 #6
0
파일: mainwindow.go 프로젝트: etel/walk
func (mw MainWindow) Create(parent walk.Container) error {
	w, err := walk.NewMainWindow()
	if err != nil {
		return err
	}

	return InitWidget(mw, w, func() error {
		if err := w.SetTitle(mw.Title); err != nil {
			return err
		}

		if err := w.SetSize(mw.Size.toW()); err != nil {
			return err
		}

		if err := addToActionList(w.Menu().Actions(), mw.MenuActions); err != nil {
			return err
		}

		imageList, err := walk.NewImageList(walk.Size{16, 16}, 0)
		if err != nil {
			return err
		}
		w.ToolBar().SetImageList(imageList)

		if err := addToActionList(w.ToolBar().Actions(), mw.ToolBarActions); err != nil {
			return err
		}

		if mw.AssignTo != nil {
			*mw.AssignTo = w
		}

		return nil
	})
}
예제 #7
0
파일: main.go 프로젝트: hemaolong/Rabbit
func (mw *MainWindow) initPoseInfo() {
	if modelItem == nil {
		return
	}

	model := modelItem.img
	if model == nil {
		return
	}
	// Get the pose count
	poseCnt := 1
	x := model.Bounds().Min.X
	y := model.Bounds().Min.Y
	w := model.Bounds().Dx()
	h := model.Bounds().Dy()
	for i := 2; i < POSE_CNT_MAX; i++ {
		sh := h / i
		for j := 1; j < i; j++ {
			beginY := sh * j
			// Erase the boundary by 1 pix to handel the neighbor pix
			for z := 1; z < w-1; z++ {
				_, _, _, a := model.At(x+z, y+beginY).RGBA()
				if a != 0 {
					_, _, _, la := model.At(x+z-1, y+beginY).RGBA()
					_, _, _, ra := model.At(x+z+1, y+beginY).RGBA()
					_, _, _, ta := model.At(x+z, y+beginY-1).RGBA()
					_, _, _, da := model.At(x+z, y+beginY+1).RGBA()
					if la != 0 && ra != 0 && ta != 0 && da != 0 {
						fmt.Println("Pose alpha:", x+z, y+beginY, a)
						goto nextPose
					}
				}
			}
		}
		poseCnt = i
		break

	nextPose:
	}

	fmt.Println("Pose count: ", poseCnt)

	// Init the pose list
	imageW = w / int(mw.uiFrameCnt.Value())
	imageH = h / poseCnt

	mw.resetImageList()
	boundary = image.Rect(0, 0, imageW, imageH)
	tmpBound := boundary
	// Read all png images
	for i := 0; i < poseCnt; i++ {
		for j := 0; j < int(mw.uiFrameCnt.Value()); j++ {
			deltaX := imageW * j
			deltaY := imageH * i
			tmpBound = boundary.Add(image.Point{deltaX, deltaY})

			newImg := new(ImageItem)
			newImg.fname = ""
			newImg.img = modelItem.img.SubImage(tmpBound).(ImageExt)
			newImg.bm, _ = walk.NewBitmapFromImage(newImg.img)

			imgList = append(imgList, newImg)
		}
	}

}

/*
func (mw *MainWindow) onUiSetFrameCnt() {
	if modelItem == nil {
		return
	}

	// imageW = modelItem.img.Bounds().Dx()
	// imageH = modelItem.img.Bounds().Dy()

	// poseCnt := mw.getPoseCnt()
	playPose = int(mw.uiPlayPose.Value())
	mw.setImageSize()
}*/

func (mw *MainWindow) refreshToolBar(mode int) {
	mw.uiConvirm.SetEnabled(false)
	mw.uiComposeAction.SetEnabled(false)
	mw.uiPoseCnt.SetEnabled(false)

	mw.mode = mode
	mw.uiFrameCnt.SetEnabled(false)
	if mw.mode == MODE_INVALID {
		return
	}

	if mw.mode == MODE_PLAY {
		return
	}
	if mw.mode == MODE_COMPOSE {
		mw.uiFrameCnt.SetEnabled(true)
		mw.uiComposeAction.SetEnabled(true)
	}
}

func (mw *MainWindow) getPoseInfo() (int, int) {
	totalFrame := len(imgList)
	poseCnt := mw.getPoseCnt()

	if poseCnt >= totalFrame {
		return 1, totalFrame
	}
	if totalFrame%poseCnt != 0 {
		return 1, totalFrame
	}
	return poseCnt, totalFrame / poseCnt
}

func (mw *MainWindow) composeImg(fullname string) {
	poseCnt, frame := mw.getPoseInfo()
	if frame == 0 {
		return
	}

	sw := boundary.Dx()
	sh := boundary.Dy() + yBoundAdd

	//var rgba bool
	_newBound := image.Rect(0, 0, sw*frame, sh*poseCnt)
	// No need to check the source image type.
	var result draw.Image
	firstImg := imgList[0].img
	switch firstImg.(type) {
	case *image.RGBA:
		result = image.NewRGBA(_newBound)
	case *image.RGBA64:
		result = image.NewRGBA64(_newBound)
	case *image.NRGBA:
		result = image.NewNRGBA(_newBound)
	case *image.NRGBA64:
		result = image.NewNRGBA64(_newBound)
	default:
		fmt.Println("image type: ", reflect.TypeOf(firstImg))
		println("Unsupported image type")
		return
	}
	// Compress to RGBA32, Stride
	// result := image.NewRGBA(_newBound)
	// result.Stride = result.Bounds().Dx()

	singleBound := image.Rect(0, 0, sw, sh)
	for i, _img := range imgList {
		_subImg := _img.img.SubImage(boundary)
		col := i % frame
		row := i / frame
		drawBound := singleBound.Add(image.Point{sw * col, sh * row})
		draw.Draw(result, drawBound, _subImg, _subImg.Bounds().Min, draw.Src)
	}
	// Modify stride
	// fmt.Println("Stride ", result.Stride)

	f, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, os.ModePerm)
	if err != nil {
		panic(err)
		return
	}
	defer f.Close()

	f.Truncate(0)
	// buf := bufio.NewWriterSize(f, 1024 * 1000)
	buf := bufio.NewWriter(f)
	png.Encode(buf, result)
}

func setIcon(ui *walk.Action, fname string) {
	fpath := "./img/" + fname
	_, err := os.Stat(fpath)
	if err != nil {
		fmt.Println(err)
		return
	}
	img, _ := walk.NewBitmapFromFile(fpath)
	ui.SetImage(img)
}

func (mw *MainWindow) initMenu() {
	fileMenu, _ := walk.NewMenu()
	fileMenuAction, _ := mw.Menu().Actions().AddMenu(fileMenu)
	fileMenuAction.SetText("&File")

	imageList, _ := walk.NewImageList(walk.Size{TB_H, TB_H}, 0)
	mw.ToolBar().SetImageList(imageList)

	openAction := walk.NewAction()
	setIcon(openAction, "open.png")
	openAction.SetText("&Open")
	openAction.Triggered().Attach(func() { go mw.openImage(MODE_COMPOSE) })
	fileMenu.Actions().Add(openAction)
	mw.ToolBar().Actions().Add(openAction)

	///
	// Load
	loadAction := walk.NewAction()
	setIcon(loadAction, "load.png")
	loadAction.SetText("&Load")
	loadAction.Triggered().Attach(func() { mw.openImage(MODE_PLAY) })
	fileMenu.Actions().Add(loadAction)
	mw.ToolBar().Actions().Add(loadAction)

	helpMenu, _ := walk.NewMenu()
	helpMenuAction, _ := mw.Menu().Actions().AddMenu(helpMenu)
	helpMenuAction.SetText("&Help")

	aboutAction := walk.NewAction()
	helpMenu.Actions().Add(aboutAction)
	aboutAction.SetText("&About")
	aboutAction.Triggered().Attach(func() {
		walk.MsgBox(mw, "About", "Image composer V0.1\nAuthor:heml",
			walk.MsgBoxOK|walk.MsgBoxIconInformation)
	})

	// Image operations
	// Save
	mw.uiComposeAction = walk.NewAction()
	setIcon(mw.uiComposeAction, "save.png")
	mw.uiComposeAction.SetText("&Save")
	mw.uiComposeAction.Triggered().Attach(func() { mw.saveImage() })
	fileMenu.Actions().Add(mw.uiComposeAction)
	mw.ToolBar().Actions().Add(mw.uiComposeAction)

	// Exit
	exitAction := walk.NewAction()
	exitAction.SetText("E&xit")
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	fileMenu.Actions().Add(exitAction)
}

func (mw *MainWindow) initCanvas() {
	for i := 0; i < POSE_CNT_MAX; i++ {
		iv, _ := selfWidget.NewMyImageView(mw)
		mw.imageView[i] = iv
	}
}
func (mw *MainWindow) initOtherBars() {
	sp, _ := walk.NewSplitter(mw)
	sp.SetSize(walk.Size{400, 20})

	lab, _ := walk.NewLabel(sp)
	lab.SetSize(walk.Size{16, 30})
	// lab.SetText("Pose")

	// others
	mw.uiFrameCnt, _ = walk.NewNumberEdit(sp)
	//mw.uiFrameCnt.SetSize(walk.Size{42, TB_H})
	mw.uiFrameCnt.SetRange(1, 100)
	mw.uiFrameCnt.SetDecimals(0)
	mw.uiFrameCnt.SetValue(8)
	mw.uiFrameCnt.SetEnabled(false)
	mw.uiFrameCnt.SetToolTipText(ttPlayPose)

	mw.uiPoseCnt, _ = walk.NewNumberEdit(sp)
	//mw.uiPoseCnt.SetSize(walk.Size{42, TB_H})
	mw.uiPoseCnt.SetRange(1, 100)
	mw.uiPoseCnt.SetValue(1)
	mw.uiPoseCnt.SetDecimals(0)
	mw.uiPoseCnt.SetToolTipText(ttPosCnt)

	mw.uiAddBoundY, _ = walk.NewNumberEdit(sp)
	mw.uiAddBoundY.SetRange(1, 1000)
	mw.uiAddBoundY.SetValue(0)
	mw.uiAddBoundY.SetDecimals(0)
	mw.uiAddBoundY.ValueChanged().Attach(func() {
		yBoundAdd = int(mw.uiAddBoundY.Value())
		if yBoundAdd < -imageH {
			yBoundAdd = -imageH
		}
		if yBoundAdd > (imageH - boundary.Max.Y) {
			yBoundAdd = imageH - boundary.Max.Y
		}
		mw.uiAddBoundY.SetValue(float64(yBoundAdd))
		mw.setImageSize()
	})

	mw.uiConvirm, _ = walk.NewPushButton(sp)
	mw.uiConvirm.SetText("OK")
	mw.uiConvirm.Clicked().Attach(func() {
		// Get some fresh data.
		// mw.onUiSetFrameCnt()
	})

	walk.InitWidget(sp, mw, FREEZEIZE_CLASS,
		winapi.CCS_NORESIZE,
		winapi.WS_EX_TOOLWINDOW|winapi.WS_EX_WINDOWEDGE)
}

func newMainWindow() {
	walk.SetPanicOnError(true)
	mainWnd, _ := walk.NewMainWindow()

	mw := &MainWindow{MainWindow: mainWnd}
	mw.viewGrid = walk.NewGridLayout()
	mw.SetLayout(mw.viewGrid)
	mw.viewGrid.SetRowStretchFactor(GRID_CNT, 2)
	mw.viewGrid.SetColumnStretchFactor(GRID_CNT, 2)
	mw.viewGrid.SetMargins(walk.Margins{6, 28, 2, 6})

	mw.SetTitle("Image composer")

	mw.initMenu()
	mw.initOtherBars()
	mw.initCanvas()

	mw.SetMinMaxSize(walk.Size{800, 600}, walk.Size{})
	mw.SetSize(walk.Size{800, 600})

	mw.refreshToolBar(MODE_INVALID)
	mw.Show()
	mw.Run()
}

func init() {
	walk.MustRegisterWindowClass(FREEZEIZE_CLASS)
	runtime.GOMAXPROCS(2)

	screenW = int(winapi.GetSystemMetrics(winapi.SM_CXSCREEN))
	screenH = int(winapi.GetSystemMetrics(winapi.SM_CYSCREEN))
}

func main() {
	newMainWindow()
}
예제 #8
0
파일: codytive.go 프로젝트: CodyGuo/Go-Cody
func (mw MainWindow) CreateCody() error {
	w, err := walk.NewMainWindowCody()
	if err != nil {
		return err
	}

	tlwi := topLevelWindowInfo{
		Name:             mw.Name,
		Font:             mw.Font,
		ToolTipText:      "",
		MinSize:          mw.MinSize,
		MaxSize:          mw.MaxSize,
		ContextMenuItems: mw.ContextMenuItems,
		OnKeyDown:        mw.OnKeyDown,
		OnKeyPress:       mw.OnKeyPress,
		OnKeyUp:          mw.OnKeyUp,
		OnMouseDown:      mw.OnMouseDown,
		OnMouseMove:      mw.OnMouseMove,
		OnMouseUp:        mw.OnMouseUp,
		OnSizeChanged:    mw.OnSizeChanged,
		DataBinder:       mw.DataBinder,
		Layout:           mw.Layout,
		Children:         mw.Children,
	}

	builder := NewBuilder(nil)

	w.SetSuspended(true)
	builder.Defer(func() error {
		w.SetSuspended(false)
		return nil
	})

	builder.deferBuildMenuActions(w.Menu(), mw.MenuItems)

	return builder.InitWidget(tlwi, w, func() error {
		if len(mw.ToolBar.Items) > 0 {
			var tb *walk.ToolBar
			if mw.ToolBar.AssignTo == nil {
				mw.ToolBar.AssignTo = &tb
			}

			if err := mw.ToolBar.Create(builder); err != nil {
				return err
			}

			old := w.ToolBar()
			w.SetToolBar(*mw.ToolBar.AssignTo)
			old.Dispose()
		} else {
			builder.deferBuildActions(w.ToolBar().Actions(), mw.ToolBarItems)
		}

		if err := w.SetTitle(mw.Title); err != nil {
			return err
		}

		if err := w.SetSize(mw.Size.toW()); err != nil {
			return err
		}
		// 设置屏幕居中
		w.SetScreenCenter(true)

		imageList, err := walk.NewImageList(walk.Size{16, 16}, 0)
		if err != nil {
			return err
		}
		w.ToolBar().SetImageList(imageList)

		if mw.OnDropFiles != nil {
			w.DropFiles().Attach(mw.OnDropFiles)
		}

		if mw.AssignTo != nil {
			*mw.AssignTo = w
		}

		builder.Defer(func() error {
			w.Show()

			return nil
		})

		return nil
	})
}