# 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 . from objectarea import Object from vector import Vector import gtk from droptargetobject import DropTargetObject from symbolobject import SymbolObject from symbolmovableobject import SymbolMovableObject class SignsDropTarget(DropTargetObject): """Rectangular area into which other objects can be dropped.""" def __init__(self, pos, container): DropTargetObject.__init__(self, pos, Vector(100, 100), container) self.draggable = False self.selectable = False self.hilite = False self.contents = None self.drop_origin = None self.drop_targets = None self.full = False self.shape1 = None self.shape2 = None self.answer = 'equal' def draw(self, cr): cr.rectangle(self.pos.x, self.pos.y, self.size.x, self.size.y) cr.set_source_rgb(0, 0, 0) cr.set_line_width(4.0) if self.hilite: cr.set_dash((10, 10), 0) cr.stroke() def get_bounds(self): return self.pos - Vector (2, 2), self.pos + self.size def contains(self, other_bounds): self_bounds_mn, self_bounds_mx = self.get_bounds() other_bounds_mn, other_bounds_mx = other_bounds if other_bounds_mn.x > self_bounds_mn.x \ and other_bounds_mx.x < self_bounds_mx.x \ and other_bounds_mn.y > self_bounds_mn.y \ and other_bounds_mx.y < self_bounds_mx.y : return True else: return False def processDrop(self, draggable_object): if not isinstance(draggable_object, SymbolObject) and not isinstance(draggable_object, SymbolMovableObject): return False if self.full: self.drop_origin.reset() return False #last_selected = self.drop_origin.ltsymbol # #if self.drop_origin.ltsymbol.selected == True: # last_selected = self.drop_origin.ltsymbol #elif self.drop_origin.eqsymbol.selected == True: # last_selected = self.drop_origin.eqsymbol #elif self.drop_origin.gtsymbol.selected == True: # last_selected = self.drop_origin.gtsymbol if isinstance(draggable_object, SymbolMovableObject): movable_object = SymbolMovableObject(self.pos + Vector(50, 50), draggable_object.symbol, self.drop_targets, self.container, size=80) draggable_object.move(Vector(10000, 10000)) draggable_object.dragged = False draggable_object.selected = False draggable_object.draggable = False draggable_object.selectable = False draggable_object.drag_type = None # Get rid of the incoming MovableObject. self.remove_object(draggable_object) movable_object.dragged = False movable_object.selected = False movable_object.draggable = True movable_object.selectable = True movable_object.drop_targets = self.drop_targets movable_object.drop_origin = self.drop_origin self.container.add_object(movable_object) self.contents = movable_object self.full = True self.hilite = False self.queue_draw() if self.drop_origin: self.container.select_object(self.drop_origin.ltsymbol) else: movable_object = SymbolMovableObject(self.pos + Vector(50, 50), draggable_object.symbol, self.drop_targets, self.container, size=80) draggable_object.move(Vector(10000, 10000)) draggable_object.dragged = False draggable_object.selected = False draggable_object.draggable = False draggable_object.selectable = False draggable_object.drag_type = None movable_object.dragged = False movable_object.selected = False movable_object.draggable = True movable_object.selectable = True movable_object.drop_targets = self.drop_targets movable_object.drop_origin = self.drop_origin if self.container: self.container.add_object(movable_object) self.contents = movable_object self.full = True self.hilite = False self.queue_draw() if self.drop_origin: if self.container: self.container.select_object(self.drop_origin.ltsymbol) if not self.answer_correct() and self.all_drop_targets_full(): self.container.show_hint() self.reset_all_drop_targets() self.container.register_error() elif self.answer_correct(): self.container.selected_object = None self.freeze_drop_targets() if self.answer == 'greater': self.container.increment_n_greater_than_problems_correct() elif self.answer == 'equal': self.container.increment_n_equals_problems_correct() elif self.answer == 'less': self.container.increment_n_less_than_problems_correct() self.container.finish_problem_stage3() if self.drop_origin: self.drop_origin.reset() return True def freeze_drop_targets(self): for o in self.drop_targets: o.contents.selectable = False o.contents.draggable = False o.full = False def reset_all_drop_targets(self): for o in self.drop_targets: o.reset() o.full = False def reset(self): self.remove_object(self.contents) def all_drop_targets_full(self): b = True for o in self.drop_targets: if not o.full: b = False break return b def answer_correct(self): b = False if self.full: if self.answer == 'greater' \ and self.contents.symbol == '>': b = True elif self.answer == 'equal' \ and self.contents.symbol == '=': b = True elif self.answer == 'less' \ and self.contents.symbol == '<': b = True return b