def test_selected_property(self):
        Selectable._reset_global()
        e = Entity()
        selectable = e.add_component(Selectable())
        mocked_selected = mock.MagicMock()
        mocked_deselected = mock.MagicMock()
        e.register_callback("selected",mocked_selected)
        e.register_callback("deselected",mocked_deselected)

        mocked_selected.reset_mock()
        mocked_deselected.reset_mock()
        selectable.selected = True
        mocked_selected.assert_called_once_with(selectable)
        mocked_deselected.assert_not_called()
        
        mocked_selected.reset_mock()
        mocked_deselected.reset_mock()
        selectable.selected = True
        mocked_selected.assert_not_called()
        mocked_deselected.assert_not_called()
        
        mocked_selected.reset_mock()
        mocked_deselected.reset_mock()
        selectable.selected = False
        mocked_selected.assert_not_called()
        mocked_deselected.assert_called_once_with(selectable)

        mocked_selected.reset_mock()
        mocked_deselected.reset_mock()
        selectable.selected = False
        mocked_selected.assert_not_called()
        mocked_deselected.assert_not_called()
Exemple #2
0
    def test_moving(self):
        Component._reset_global()
        Selectable._reset_global()

        e0 = Entity()
        e0.add_component(GuiManager())

        e1 = e0.add_entity(Entity())
        gui = e1.add_component(GuiElement((0,0),(10,10)))
        s = e1.add_component(Selectable())
        e1.add_component(SelectedWhileMouseDown())
        e1.add_component(FetchMouseCallbacksWhileSelected())

        e0.fire_callbacks("awake")

        draggable = e1.add_component(Draggable(0))

        cursor_start = (5,5) # inside of gui element

        # generate random walk
        n = 30
        dxy = np.random.normal(0,5,(n,2))
        dxy[0] = 0
        xy = np.cumsum(dxy,axis=0) + cursor_start
        xy = np.round(xy)

        # start dragging
        e0.fire_callbacks("mousebuttondown",Event(pos=xy[0],button=1))
        assert draggable.dragging == True
        assert gui.position[0] == 0
        assert gui.position[1] == 0
        np.testing.assert_almost_equal(draggable.last_pos, xy[0])

        # move around n times with random walk
        # omit first pos, as these is for initial buttondown
        for i in range(1,n-1):
            e0.fire_callbacks("mousemotion",Event(pos=xy[i]))
            # assert gui element was moved
            assert gui.position[0] == xy[i,0] - cursor_start[0]
            assert gui.position[1] == xy[i,1] - cursor_start[1]

        # end moving by releasing the mouse button
        e0.fire_callbacks("mousebuttonup",Event(pos=xy[-1]))
        # NOTE: the position from mousebuttonup won't be used
        # for moving, because gui element is deselected 
        assert gui.position[0] == xy[-2,0] - cursor_start[0]
        assert gui.position[1] == xy[-2,1] - cursor_start[1]
        assert draggable.dragging == False
    def test(self):
        Selectable._reset_global()
        e0 = Entity()
        s0 = e0.add_component(Selectable())
        e1 = Entity()
        s1 = e1.add_component(Selectable())
        e2 = Entity()
        s2 = e2.add_component(Selectable())
        assert s0.selected == False
        assert s1.selected == False
        assert s2.selected == False
        
        s0.select()
        assert Selectable.selected_component == s0
        assert s0.selected == True
        assert s1.selected == False
        assert s2.selected == False

        s1.select()
        assert Selectable.selected_component == s1
        assert s0.selected == False
        assert s1.selected == True
        assert s2.selected == False

        s2.select()
        assert Selectable.selected_component == s2
        assert s0.selected == False
        assert s1.selected == False
        assert s2.selected == True

        s2.deselect()
        assert Selectable.selected_component == None
        assert s0.selected == False
        assert s1.selected == False
        assert s2.selected == False
    
        s0.deselect()        
        s1.deselect()        
        assert Selectable.selected_component == None
        assert s0.selected == False
        assert s1.selected == False
        assert s2.selected == False