Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/draggableobject.py
blob: c60a30727774e363af4396709f748c8a0061fe7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Copyright 2008 by Peter Moxhay and Wade Brainerd.  
# This file is part of Math.
#
# Math is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Math is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Math.  If not, see <http://www.gnu.org/licenses/>.
from objectarea import Object
from droptargetobject import DropTargetObject
from vector import Vector

import gtk, math

# The global grid unit size, in pixels.  Objects will snap to multiples of this value.
GRID_SIZE = 50

# Width and height of region within which we can drag objects.
DRAGGING_RECT_WIDTH = 24*GRID_SIZE
DRAGGING_RECT_HEIGHT = 16*GRID_SIZE

# The global grid angle size, in radians.
RADIAL_GRID_SIZE = math.pi/4

class DraggableObject(Object):
    """
    Extends Objects with the ability to Drag and Drop.
    Derived classes must implement:
    get_bounds() - Returns min, max bounds of the Object.
    """
    def __init__(self):
        Object.__init__(self)
        
        self.draggable = True
        self.drag_type = None
         
        self.drop_origin = None
        self.drop_targets = []
        
        self.drag_offset = Vector(0, 0)
        
        # The mouse press position.
        self.press_pos = Vector(0, 0)
        
        #self.drag_copy = False
        self.moving_by_key_press = False
        
    def get_bounds(self):
        pass
        
    def get_drag_type(self, event):
        if not self.draggable:
            return None
        return 'move'
        
    def on_mouse(self, event):
        if not self.selectable or not self.draggable:
            return
             
        # The position of the mouse event.
        epos = Vector(event.x, event.y)
        
        # If drag is in progress.
        if self.drag_type:
            # Remember the drag position.
            self.press_pos = epos
            drag_type = self.get_drag_type(event)

            # Perform the drag.
            if event.type == gtk.gdk.MOTION_NOTIFY:      
                drag_type = self.get_drag_type(event)
            
                if drag_type == 'move':
                    cursor_type = gtk.gdk.FLEUR
                if self.container:
                    self.container.queue_cursor(cursor_type)
                
                if self.dragged:
                    self.move(epos - self.drag_offset)
                i = 0;
                for o in self.drop_targets:
                    i += 1
                    if o.contains(self.get_bounds()) and not o.full:
                        o.hilite = True
                    else:
                        o.hilite = False
                    
            # End the drag.             
            elif event.type == gtk.gdk.BUTTON_RELEASE:
                self.dragged = False
                if self.container:
                    self.container.snap_to_grid(self)
                
                b = False
                
                #Check whether the drop is inside a drop target.
                for o in self.drop_targets:
                    if o.contains(self.get_bounds()):
                        b = True
                        if o.processDrop(self):
                            return True
                    
                if not b:
                    o.hilite = False
                    self.drop_origin.reset()
                                  
        # If drag is not already in progress.
        elif self.contains_point(epos):
            drag_type = self.get_drag_type(event)
            
            if drag_type == 'move':
                cursor_type = gtk.gdk.FLEUR
            if self.container:
                self.container.queue_cursor(cursor_type)
            
            # Start the drag.
            if event.type == gtk.gdk.BUTTON_PRESS:
                if self.container:
                    self.container.select_object(self)
                
                self.drag_type = self.get_drag_type(event)
                
                if self.drag_type:
                    if self.container:
                        self.container.drag_object = self
                        self.drag_offset = epos - self.pos
            
                    # Remember the press position
                    self.press_pos = epos
                    self.dragged = True
                
            return True
        
    def on_key(self, event):
        if not self.selectable or not self.draggable:
            return
        
        key_name = gtk.gdk.keyval_name(event.keyval)
        #print "key_name =", key_name   

        if key_name == 'Return' or key_name == 'KP_End':
            #print "Return key pressed on a draggable object"
            if self.selected and not self.dragged:
                #print "Draggable object is selected, so make it dragged."
                self.dragged = True
                self.moving_by_key_press = True
                if self.container:
                    self.container.queue_draw()
                            
            elif self.moving_by_key_press and self.dragged:
                #print "Draggable object is moving_by_key_press and dragged, prepare to try to drop it."
                
                #Check whether the drop is inside a drop target.
                for o in self.drop_targets:
                    if o.contains(self.get_bounds()):
                        b = True
                        if o.processDrop(self):
                            return True
                    
                #TODO- ordering of next 4 lines is unclear.
                if self.container:
                    self.container.drag_object = None
                    self.container.snap_to_grid(self)
                #self.drag_type = None
                if self.container:
                    print "DraggableObject on_key calling check_problem_solved"
                    self.container.check_problem_solved()
                
        elif self.moving_by_key_press and self.dragged:
            #print "Draggable object is dragged, so move it."
            if key_name == 'Up' or key_name == 'KP_Up':
                self.move(self.pos + Vector(0, -GRID_SIZE))
            elif key_name == 'Down' or key_name == 'KP_Down':
                self.move(self.pos + Vector(0, GRID_SIZE))
            elif key_name == 'Left' or key_name == 'KP_Left':
                self.move(self.pos + Vector(-GRID_SIZE, 0))
            elif key_name == 'Right' or key_name == 'KP_Right':
                self.move(self.pos + Vector(GRID_SIZE, 0))          
            elif key_name == 'BackSpace' or key_name == 'KP_Page_Down':
                #print "BackSpace pressed, try to stop the drag"
                self.drop_origin.reset()
                return
            
            i = 0;
            for o in self.drop_targets:
                i += 1
                if o.contains(self.get_bounds()) and not o.full:
                    o.hilite = True
                else:
                    o.hilite = False
                
        if self.drop_target:
            if self.drop_target.contains_rectangle(self.get_bounds()):
                self.drop_target.hilite = False
                self.drop_target = None
                
            for o in self.container.objects:
                if isinstance(o, DropTargetObject):
                    # If a drop target contains epos, and doesn't already contain some other object...
                    #if o.contains_point(self.pos) and not o.contents:
                    if o.contains_rectangle(self.get_bounds()) and not o.contents:
                        # ..set the dragged object's drop target to be _this_ drop target...
                        self.drop_target = o
                        # ... and hilight the drop target.
                        self.drop_target.hilite = True
                        if self.container:
                            self.container.queue_draw()