Example #1
0
// Draw the droid on a certain position.
func Draw(gc draw2d.GraphicContext, x, y float64) {
	// set the fill and stroke color of the droid
	gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
	gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})

	// set line properties
	gc.SetLineCap(draw2d.RoundCap)
	gc.SetLineWidth(5)

	// head
	gc.MoveTo(x+30, y+70)
	gc.ArcTo(x+80, y+70, 50, 50, 180*(math.Pi/180), 180*(math.Pi/180))
	gc.Close()
	gc.FillStroke()
	gc.MoveTo(x+60, y+25)
	gc.LineTo(x+50, y+10)
	gc.MoveTo(x+100, y+25)
	gc.LineTo(x+110, y+10)
	gc.Stroke()

	// left eye
	draw2dkit.Circle(gc, x+60, y+45, 5)
	gc.FillStroke()

	// right eye
	draw2dkit.Circle(gc, x+100, y+45, 5)
	gc.FillStroke()

	// body
	draw2dkit.RoundedRectangle(gc, x+30, y+75, x+30+100, y+75+90, 10, 10)
	gc.FillStroke()
	draw2dkit.Rectangle(gc, x+30, y+75, x+30+100, y+75+80)
	gc.FillStroke()

	// left arm
	draw2dkit.RoundedRectangle(gc, x+5, y+80, x+5+20, y+80+70, 10, 10)
	gc.FillStroke()

	// right arm
	draw2dkit.RoundedRectangle(gc, x+135, y+80, x+135+20, y+80+70, 10, 10)
	gc.FillStroke()

	// left leg
	draw2dkit.RoundedRectangle(gc, x+50, y+150, x+50+20, y+150+50, 10, 10)
	gc.FillStroke()

	// right leg
	draw2dkit.RoundedRectangle(gc, x+90, y+150, x+90+20, y+150+50, 10, 10)
	gc.FillStroke()
}
Example #2
0
// Draw the image frame with certain parameters.
func Draw(gc draw2d.GraphicContext, png string,
	dw, dh, margin, lineWidth float64) error {
	// Draw frame
	draw2dkit.RoundedRectangle(gc, lineWidth, lineWidth, dw-lineWidth, dh-lineWidth, 100, 100)
	gc.SetLineWidth(lineWidth)
	gc.FillStroke()

	// load the source image
	source, err := draw2dimg.LoadFromPngFile(png)
	if err != nil {
		return err
	}
	// Size of source image
	sw, sh := float64(source.Bounds().Dx()), float64(source.Bounds().Dy())
	// Draw image to fit in the frame
	// TODO Seems to have a transform bug here on draw image
	scale := math.Min((dw-margin*2)/sw, (dh-margin*2)/sh)
	gc.Save()
	gc.Translate((dw-sw*scale)/2, (dh-sh*scale)/2)
	gc.Scale(scale, scale)
	gc.Rotate(0.2)

	gc.DrawImage(source)
	gc.Restore()
	return nil
}
Example #3
0
// FillString draws a filled and stroked string.
func FillString(gc draw2d.GraphicContext, x, y, width, height float64) {
	sx, sy := width/100, height/100
	gc.Save()
	gc.SetStrokeColor(image.Black)
	gc.SetLineWidth(1)
	draw2dkit.RoundedRectangle(gc, x+sx*5, y+sy*5, x+sx*95, y+sy*95, sx*10, sy*10)
	gc.FillStroke()
	gc.SetFillColor(image.Black)
	gc.SetFontSize(height / 6)
	gc.Translate(x+sx*6, y+sy*52)
	gc.SetFontData(draw2d.FontData{
		Name:   "luxi",
		Family: draw2d.FontFamilyMono,
		Style:  draw2d.FontStyleBold | draw2d.FontStyleItalic})
	w := gc.FillString("Hug")
	gc.Translate(w+sx, 0)
	left, top, right, bottom := gc.GetStringBounds("cou")
	gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80})
	draw2dkit.Rectangle(gc, left, top, right, bottom)
	gc.SetLineWidth(height / 50)
	gc.Stroke()
	gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xff, 0xff})
	gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff})
	gc.SetLineWidth(height / 100)
	gc.StrokeString("Hug")
	gc.Restore()
}
Example #4
0
// Draw "Hello World"
func Draw(gc draw2d.GraphicContext, text string) {
	// Draw a rounded rectangle using default colors
	draw2dkit.RoundedRectangle(gc, 5, 5, 135, 95, 10, 10)
	gc.FillStroke()

	// Set the font luximbi.ttf
	gc.SetFontData(draw2d.FontData{Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleBold | draw2d.FontStyleItalic})
	// Set the fill text color to black
	gc.SetFillColor(image.Black)
	gc.SetFontSize(14)
	// Display Hello World
	gc.FillStringAt("Hello World", 8, 52)
}
Example #5
0
func display() {
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

	gl.LineWidth(1)
	gc := draw2dgl.NewGraphicContext(width, height)
	gc.SetFontData(draw2d.FontData{
		Name:   "luxi",
		Family: draw2d.FontFamilyMono,
		Style:  draw2d.FontStyleBold | draw2d.FontStyleItalic})

	gc.BeginPath()
	draw2dkit.RoundedRectangle(gc, 200, 200, 600, 600, 100, 100)

	gc.SetFillColor(color.RGBA{0, 0, 0, 0xff})
	gc.Fill()

	gl.Flush() /* Single buffered, so needs a flush. */
}
Example #6
0
//Creates a new engine and populates it with the core functions
func LoadGraphics(e *Engine) *Engine {

	e = add(e, "glfw.Init", NewCode("glfw.Init", 0, func(e *Engine, c *Thingy) *Engine {
		err := glfw.Init()
		if err != nil {
			panic(err)
		}
		return e
	}))

	e = add(e, "GLFWEVENTLOOP", NewCode("GLFWEVENTLOOP", 1, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		window := el1._structVal.(*glfw.Window)
		window.MakeContextCurrent()

		if err := gl.Init(); err != nil {
			panic(err)
		}

		texture = newTexture("square.png")
		defer gl.DeleteTextures(1, &texture)

		setupScene()
		for !window.ShouldClose() {
			drawScene()
			window.SwapBuffers()
			glfw.PollEvents()
		}

		return e
	}))

	e = add(e, "wg.GetStringBounds", NewCode("wg.GetStringBounds", 0, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		left, top, right, bottom := gc.GetStringBounds(el1.getString())
		e.dataStack = append(e.dataStack, NewString(fmt.Sprintf("%v", bottom), c))
		e.dataStack = append(e.dataStack, NewString(fmt.Sprintf("%v", right), c))
		e.dataStack = append(e.dataStack, NewString(fmt.Sprintf("%v", top), c))
		e.dataStack = append(e.dataStack, NewString(fmt.Sprintf("%v", left), c))
		return e
	}))

	e = add(e, "wg.SetFillColor", NewCode("wg.SetFillColor", 1, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		col := el1._structVal.(color.RGBA)
		EnsureGC()
		gc.SetFillColor(col)
		return e
	}))

	e = add(e, "wg.SetStrokeColor", NewCode("wg.SetStrokeColor", 1, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		col := el1._structVal.(color.RGBA)
		EnsureGC()
		gc.SetStrokeColor(col)
		return e
	}))

	e = add(e, "wg.CloseAndFill", NewCode("wg.CloseAndFill", 0, func(e *Engine, c *Thingy) *Engine {
		EnsureGC()
		gc.Close()
		gc.FillStroke()
		return e
	}))

	e = add(e, "wg.MoveTo", NewCode("wg.MoveTo", 2, func(e *Engine, c *Thingy) *Engine {
		var el1, el2 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseFloat(el1.getString(), 32)
		x2, _ := strconv.ParseFloat(el2.getString(), 32)
		EnsureGC()
		gc.MoveTo(float64(x1), float64(x2)) // should always be called first for a new path
		return e
	}))

	e = add(e, "wg.LineTo", NewCode("wg.LineTo", 2, func(e *Engine, c *Thingy) *Engine {
		var el1, el2 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseFloat(el1.getString(), 32)
		x2, _ := strconv.ParseFloat(el2.getString(), 32)
		EnsureGC()
		gc.LineTo(float64(x1), float64(x2)) // should always be called first for a new path
		return e
	}))

	e = add(e, "wg.RoundedRectangle", NewCode("wg.RoundedRectangle", 2, func(e *Engine, c *Thingy) *Engine {
		var el1, el2, el3, el4 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		el3, e.dataStack = popStack(e.dataStack)
		el4, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseFloat(el1.getString(), 32)
		x2, _ := strconv.ParseFloat(el2.getString(), 32)
		x3, _ := strconv.ParseFloat(el3.getString(), 32)
		x4, _ := strconv.ParseFloat(el4.getString(), 32)
		EnsureGC()
		draw2dkit.RoundedRectangle(gc, x1, x2, x3, x4, 10, 10)
		gc.FillStroke()
		return e
	}))

	e = add(e, "wg.FillStringAt", NewCode("wg.FillStringAt", 3, func(e *Engine, c *Thingy) *Engine {
		var el1, el2, el3 *Thingy
		el3, e.dataStack = popStack(e.dataStack)
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseFloat(el1.getString(), 32)
		x2, _ := strconv.ParseFloat(el2.getString(), 32)
		EnsureGC()
		// Set the font luximbi.ttf
		gc.SetFontData(draw2d.FontData{Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleBold | draw2d.FontStyleItalic})
		// Set the fill text color to black
		gc.SetFillColor(image.White)
		gc.SetFontSize(15)
		gc.FillStringAt(el3.getString(), float64(x1), float64(x2)) // should always be called first for a new path
		return e
	}))

	e = add(e, "WG.CLEAR", NewCode("WG.CLEAR", 1, func(e *Engine, c *Thingy) *Engine {
		var el3 *Thingy
		el3, e.dataStack = popStack(e.dataStack)
		col := el3._structVal.(color.RGBA)
		wg.ClearImage(col)
		return e
	}))

	e = add(e, "WG.TEST", NewCode("WG.TEST", 0, func(e *Engine, c *Thingy) *Engine {
		/*var el1, el2,el3 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		el3, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseInt( el1.getString(), 10, 32 )
		x2, _ := strconv.ParseInt( el2.getString(), 10, 32 )
		col := el3._structVal.(color.RGBA) */
		EnsureGC()
		// Set some properties
		gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
		gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
		gc.SetLineWidth(5)

		// Draw a closed shape
		gc.MoveTo(100, 100) // should always be called first for a new path
		gc.LineTo(600, 150)
		gc.QuadCurveTo(600, 10, 10, 10)
		gc.Close()
		gc.FillStroke()
		return e
	}))

	e = add(e, "WG.SETPOINT", NewCode("WG.SETPOINT", 3, func(e *Engine, c *Thingy) *Engine {
		var el1, el2, el3 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		el3, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseInt(el1.getString(), 10, 32)
		x2, _ := strconv.ParseInt(el2.getString(), 10, 32)
		col := el3._structVal.(color.RGBA)
		wg.GetImage().Set(int(x1), int(x2), col)
		return e
	}))

	e = add(e, "RGBA", NewCode("RGBA", 3, func(e *Engine, c *Thingy) *Engine {
		var el1, el2, el3, el4 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		el2, e.dataStack = popStack(e.dataStack)
		el3, e.dataStack = popStack(e.dataStack)
		el4, e.dataStack = popStack(e.dataStack)
		x1, _ := strconv.ParseInt(el1.getString(), 10, 32)
		x2, _ := strconv.ParseInt(el2.getString(), 10, 32)
		x3, _ := strconv.ParseInt(el3.getString(), 10, 32)
		x4, _ := strconv.ParseInt(el4.getString(), 10, 32)
		col := color.RGBA{uint8(x1), uint8(x2), uint8(x3), uint8(x4)}
		e.dataStack = append(e.dataStack, NewWrapper(col))
		return e
	}))

	e = add(e, "WG.GETKEY", NewCode("WG.GETKEY", 0, func(e *Engine, c *Thingy) *Engine {
		var el1 *Thingy
		el1, e.dataStack = popStack(e.dataStack)
		xx, _ := strconv.ParseInt(el1.getString(), 10, 32)
		var ret *Thingy
		if wg.GetKey(int(xx)) {
			ret = NewBool(1)
		} else {
			ret = NewBool(0)
		}
		e.dataStack = append(e.dataStack, ret)
		return e
	}))
	e = add(e, "WG.START", NewCode("WG.START", 1, func(ne *Engine, c *Thingy) *Engine {
		CallbackState = ne
		var port *Thingy
		port, ne.dataStack = popStack(ne.dataStack)
		portnum, _ := strconv.ParseInt(port.getString(), 10, 32)
		wg.Start(800, 600, int(portnum), WgCallback)
		return ne
	}))

	e = add(e, "glfw.CreateWindow", NewCode("glfw.CreateWindow", 2, func(ne *Engine, c *Thingy) *Engine {
		var x, y, title *Thingy

		x, ne.dataStack = popStack(ne.dataStack)
		y, ne.dataStack = popStack(ne.dataStack)
		title, ne.dataStack = popStack(ne.dataStack)

		xx, _ := strconv.ParseInt(x.getString(), 10, 32)
		yy, _ := strconv.ParseInt(y.getString(), 10, 32)

		glfw.WindowHint(glfw.Resizable, glfw.False)
		glfw.WindowHint(glfw.ContextVersionMajor, 2)
		glfw.WindowHint(glfw.ContextVersionMinor, 1)

		window, err := glfw.CreateWindow(int(xx), int(yy), title.getString(), nil, nil)
		if err != nil {
			panic(err)
		}

		ne.dataStack = pushStack(ne.dataStack, NewWrapper(window))
		return ne
	}))
	return e
}
Example #7
0
// Draw a gopher head (not rotated)
func Draw(gc draw2d.GraphicContext, x, y, w, h float64) {
	h23 := (h * 2) / 3

	blf := color.RGBA{0, 0, 0, 0xff}          // black
	wf := color.RGBA{0xff, 0xff, 0xff, 0xff}  // white
	nf := color.RGBA{0x8B, 0x45, 0x13, 0xff}  // brown opaque
	brf := color.RGBA{0x8B, 0x45, 0x13, 0x99} // brown transparant
	brb := color.RGBA{0x8B, 0x45, 0x13, 0xBB} // brown transparant

	// round head top
	gc.MoveTo(x, y+h*1.002)
	gc.CubicCurveTo(x+w/4, y-h/3, x+3*w/4, y-h/3, x+w, y+h*1.002)
	gc.Close()
	gc.SetFillColor(brb)
	gc.Fill()

	// rectangle head bottom
	draw2dkit.RoundedRectangle(gc, x, y+h, x+w, y+h+h, h/5, h/5)
	gc.Fill()

	// left ear outside
	draw2dkit.Circle(gc, x, y+h, w/12)
	gc.SetFillColor(brf)
	gc.Fill()

	// left ear inside
	draw2dkit.Circle(gc, x, y+h, 0.5*w/12)
	gc.SetFillColor(nf)
	gc.Fill()

	// right ear outside
	draw2dkit.Circle(gc, x+w, y+h, w/12)
	gc.SetFillColor(brf)
	gc.Fill()

	// right ear inside
	draw2dkit.Circle(gc, x+w, y+h, 0.5*w/12)
	gc.SetFillColor(nf)
	gc.Fill()

	// left eye outside white
	draw2dkit.Circle(gc, x+w/3, y+h23, w/9)
	gc.SetFillColor(wf)
	gc.Fill()

	// left eye black
	draw2dkit.Circle(gc, x+w/3+w/24, y+h23, 0.5*w/9)
	gc.SetFillColor(blf)
	gc.Fill()

	// left eye inside white
	draw2dkit.Circle(gc, x+w/3+w/24+w/48, y+h23, 0.2*w/9)
	gc.SetFillColor(wf)
	gc.Fill()

	// right eye outside white
	draw2dkit.Circle(gc, x+w-w/3, y+h23, w/9)
	gc.Fill()

	// right eye black
	draw2dkit.Circle(gc, x+w-w/3+w/24, y+h23, 0.5*w/9)
	gc.SetFillColor(blf)
	gc.Fill()

	// right eye inside white
	draw2dkit.Circle(gc, x+w-(w/3)+w/24+w/48, y+h23, 0.2*w/9)
	gc.SetFillColor(wf)
	gc.Fill()

	// left tooth
	gc.SetFillColor(wf)
	draw2dkit.RoundedRectangle(gc, x+w/2-w/8, y+h+h/2.5, x+w/2-w/8+w/8, y+h+h/2.5+w/6, w/10, w/10)
	gc.Fill()

	// right tooth
	draw2dkit.RoundedRectangle(gc, x+w/2, y+h+h/2.5, x+w/2+w/8, y+h+h/2.5+w/6, w/10, w/10)
	gc.Fill()

	// snout
	draw2dkit.Ellipse(gc, x+(w/2), y+h+h/2.5, w/6, w/12)
	gc.SetFillColor(nf)
	gc.Fill()

	// nose
	draw2dkit.Ellipse(gc, x+(w/2), y+h+h/7, w/10, w/12)
	gc.SetFillColor(blf)
	gc.Fill()
}