Esempio n. 1
0
func TestContainer_RemoveWidget(t *testing.T) {
	c := new(Container)

	fake_1 := &fakeWidget{}
	fake_1.draw = func() *sdl.Surface {
		return sdl.CreateRGBSurface(0, 10, 5, 32, 0, 0, 0, 0)
	}
	c.Add(fake_1)

	fake_2 := &fakeWidget{}
	fake_2.draw = func() *sdl.Surface {
		return sdl.CreateRGBSurface(0, 5, 10, 32, 0, 0, 0, 0)
	}
	c.Add(fake_2)

	c.Remove(fake_1)

	surface := c.Draw()
	defer surface.Free()
	if surface.W != 5 {
		t.Errorf("expected surface width to be %d, but was %d", 5, surface.W)
	}
	if surface.H != 10 {
		t.Errorf("expected surface height to be %d, but was %d", 10, surface.H)
	}
}
Esempio n. 2
0
func TestContainer_PackNone(t *testing.T) {
	c := &Container{Pack: PackNone}

	fake_1 := &fakeWidget{}
	fake_1.draw = func() *sdl.Surface {
		return sdl.CreateRGBSurface(0, 10, 5, 32, 0, 0, 0, 0)
	}
	c.AddWithPosition(fake_1, 0, 10)

	fake_2 := &fakeWidget{}
	fake_2.draw = func() *sdl.Surface {
		return sdl.CreateRGBSurface(0, 5, 10, 32, 0, 0, 0, 0)
	}
	c.AddWithPosition(fake_2, 10, 0)

	surface := c.Draw()
	if surface == nil {
		t.Fatal("surface was nil")
	}
	defer surface.Free()

	if surface.W != 15 {
		t.Errorf("expected surface width to be %d, but was %d", 15, surface.W)
	}
	if surface.H != 15 {
		t.Errorf("expected surface height to be %d, but was %d", 15, surface.H)
	}
}
Esempio n. 3
0
func NewImageWithColor(color uint32, w int, h int) (image *Image) {
	surface := sdl.CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0)
	surface.FillRect(nil, color)

	image = &Image{surface}
	return
}
Esempio n. 4
0
func TestContainer_Handle(t *testing.T) {
	for testNum, tt := range eventTests {
		// setup
		c := &Container{Pack: tt.pack}

		var handled [2][]interface{}
		child_1 := &fakeWidget{}
		child_1.draw = func() *sdl.Surface {
			return sdl.CreateRGBSurface(0, 10, 5, 32, 0, 0, 0, 0)
		}
		child_1.handle = func(event interface{}) bool {
			handled[0] = append(handled[0], event)
			return true
		}
		c.Add(child_1)

		child_2 := &fakeWidget{}
		child_2.draw = func() *sdl.Surface {
			return sdl.CreateRGBSurface(0, 5, 10, 32, 0, 0, 0, 0)
		}
		child_2.handle = func(event interface{}) bool {
			handled[1] = append(handled[1], event)
			return true
		}
		c.Add(child_2)

		surface := c.Draw()
		defer surface.Free()

		// send event
		if !c.Handle(tt.event) {
			t.Errorf("test %d: expected event to be handled, but wasn't", testNum)
		}
		if len(handled[tt.handledBy]) != 1 {
			t.Errorf("test %d: widget %d didn't handle the event", testNum, tt.handledBy)
		} else if event := handled[tt.handledBy][0]; event != tt.expectedEvent {
			t.Errorf("test %d: expected %+v, got %+v", testNum, tt.expectedEvent, event)
		}
	}
}
Esempio n. 5
0
func TestWindow(t *testing.T) {
	c := &Container{}

	fake := &fakeWidget{}
	fake.draw = func() *sdl.Surface {
		return sdl.CreateRGBSurface(0, 10, 5, 32, 0, 0, 0, 0)
	}
	c.Add(fake)

	win, err := NewWindow(c, 20, 20)
	if err != nil {
		t.Fatal(err)
	}
	defer win.Free()
	win.Draw()
}
Esempio n. 6
0
func TestContainer_Draw_OneWidget(t *testing.T) {
	c := &Container{}

	fake := &fakeWidget{}
	fake.draw = func() *sdl.Surface {
		return sdl.CreateRGBSurface(0, 10, 5, 32, 0, 0, 0, 0)
	}
	c.Add(fake)

	surface := c.Draw()
	defer surface.Free()
	if surface.W != 10 {
		t.Errorf("expected surface width to be %d, but was %d", 10, surface.W)
	}
	if surface.H != 5 {
		t.Errorf("expected surface height to be %d, but was %d", 5, surface.H)
	}
}
Esempio n. 7
0
func CreateSurface(hw bool, display_format bool, x, y uint16) (*sdl.Surface, error) {
	var flags uint32 = 0
	if hw {
		flags = sdl.HWSURFACE
	} else {
		flags = sdl.SWSURFACE
	}

	surface := sdl.CreateRGBSurface(flags, int(x), int(y), 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)
	if surface == nil {
		return nil, fmt.Errorf("Failed to load texture: %s", sdl.GetError())
	}

	if display_format {
		surface = surface.DisplayFormat()
		if surface == nil {
			return nil, fmt.Errorf("Failed to convert texture: %s", sdl.GetError())
		}
	}

	return surface, nil
}
Esempio n. 8
0
func (img *Image) Draw() *sdl.Surface {
	surface := sdl.CreateRGBSurface(0, int(img.image.W), int(img.image.H), 32, 0, 0, 0, 0)
	surface.Blit(nil, img.image, nil)
	return surface
}
Esempio n. 9
0
func (c *Container) Draw() (result *sdl.Surface) {
	var (
		surfaces []*sdl.Surface
		w, h     int16
	)

	// determine surface dimensions
	c.mutex.Lock()
	for _, child := range c.Children {
		surface := child.Widget.Draw()
		child.W = int16(surface.W)
		child.H = int16(surface.H)

		surfaces = append(surfaces, surface)
		switch c.Pack {
		case PackLayer:
			child.X = 0
			if child.W > w {
				w = child.W
			}
			child.Y = 0
			if child.H > h {
				h = child.H
			}
		case PackHorizontal:
			child.X = w
			w += child.W
			child.Y = 0
			if child.H > h {
				h = child.H
			}
		case PackVertical:
			child.X = 0
			if child.W > w {
				w = child.W
			}
			child.Y = h
			h += child.H
		case PackNone:
			// X and Y coordinates are fixed
			if (child.X + child.W) > w {
				w = child.X + child.W
			}
			if (child.Y + child.H) > h {
				h = child.Y + child.H
			}
		}
	}
	c.mutex.Unlock()

	// blit surfaces
	if w > 0 && h > 0 {
		result = sdl.CreateRGBSurface(0, int(w), int(h), 32, 0, 0, 0, 0)
		rect := new(sdl.Rect)
		for i, surface := range surfaces {
			placement := c.Children[i]
			rect.X = placement.X
			rect.Y = placement.Y
			result.Blit(rect, surface, nil)
			surface.Free()
		}
	}
	return
}