Beispiel #1
0
func (m *Module) Display(display Display, sz size.Event, glctx gl.Context, images *glutil.Images) {
	im := images.NewImage(sz.WidthPx, sz.HeightPx)

	switch {
	case m.err != nil:
		// There was an error of some kind. We should display the error indication until the error
		// is removed from the module by a timeout.
		m.renderInformation(im.RGBA, sz.Size(), errorBackground, "T_T", "T_T")
	case !display.Loaded:
		// We haven't loaded any predictions yet. Dislay the loading screen.
		loadingStr := "Loading" + strings.Repeat(".", int(time.Now().Unix()%4))
		m.renderInformation(im.RGBA, sz.Size(), loadingBackground, loadingStr, "Loading...")
	case !display.NextOK:
		// We don't have a prediction for the next train. This is probably because there are no
		// trains coming for a while (ex., after nightly shutdown).
		m.renderInformation(im.RGBA, sz.Size(), loadingBackground, "No trains :(", "No trains :(")
	default:
		// If everything else is ok, then we have at least 1 prediction. Display it.
		m.render(im.RGBA, display, sz.Size())
	}

	if display.TransitRouteName != "" {
		m.renderTransitRouteName(im.RGBA, sz.Size(), display.TransitRouteName)
	}

	im.Upload()
	im.Draw(
		sz,
		geom.Point{},
		geom.Point{X: sz.WidthPt},
		geom.Point{Y: sz.HeightPt},
		sz.Bounds())
	im.Release()
}
Beispiel #2
0
func main() {
	// Run the CPU profile if we're instructed to.
	if len(flagProfile) > 0 {
		f, err := os.Create(flagProfile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	// Whoops!
	if flag.NArg() == 0 {
		fmt.Fprint(os.Stderr, "\n")
		log.Print("No images specified.\n\n")
		usage()
	}

	driver.Main(func(s screen.Screen) {
		// Decode all images (in parallel).
		names, imgs := decodeImages(findFiles(flag.Args()))

		// Die now if we don't have any images!
		if len(imgs) == 0 {
			log.Fatal("No images specified could be shown. Quitting...")
		}

		winSize := image.Point{flagWidth, flagHeight}
		// Auto-size the window if appropriate.
		if flagAutoResize {
			log.Printf(">>> img[%s]...\n", names[0])
			b := imgs[0].Bounds()
			winSize = image.Point{b.Dx(), b.Dy()}
		}

		w, err := s.NewWindow(&screen.NewWindowOptions{
			Width:  winSize.X,
			Height: winSize.Y,
		})
		if err != nil {
			log.Fatal(err)
		}
		defer w.Release()

		b, err := s.NewBuffer(winSize)
		if err != nil {
			log.Fatal(err)
		}
		defer b.Release()

		w.Fill(b.Bounds(), color.White, draw.Src)
		w.Publish()

		var sz size.Event
		var i int // index of image to display
		for {
			e := w.NextEvent()
			switch e := e.(type) {
			default:

			case mouse.Event:

			case key.Event:
				repaint := false
				switch e.Code {
				case key.CodeEscape, key.CodeQ:
					return
				case key.CodeRightArrow:
					if e.Direction == key.DirPress {
						if i == len(imgs)-1 {
							i = -1
						}
						i++
						repaint = true
						b.Release()
						b, err = s.NewBuffer(sz.Size())
						if err != nil {
							log.Fatal(err)
						}
					}

				case key.CodeLeftArrow:
					if e.Direction == key.DirPress {
						if i == 0 {
							i = len(imgs)
						}
						i--
						repaint = true
						b, err = s.NewBuffer(sz.Size())
						if err != nil {
							log.Fatal(err)
						}
					}

				case key.CodeR:
					if e.Direction == key.DirPress {
						// resize to current image
						r := imgs[i].Bounds()
						sz.HeightPx = r.Dy()
						sz.WidthPx = r.Dx()
						repaint = true
						b, err = s.NewBuffer(sz.Size())
						if err != nil {
							log.Fatal(err)
						}
						w.Publish()

					}
				}
				if repaint {
					w.Send(paint.Event{})
				}

			case paint.Event:
				img := imgs[i]
				draw.Draw(b.RGBA(), b.Bounds(), img, image.Point{}, draw.Src)
				dp := vpCenter(img, sz.WidthPx, sz.HeightPx)
				zero := image.Point{}
				if dp != zero {
					w.Fill(sz.Bounds(), color.Black, draw.Src)
				}
				w.Upload(dp, b, b.Bounds())

			case size.Event:
				sz = e

			case error:
				log.Print(e)
			}
		}
	})
}