def on_mouse_release(self, x, y, buttons, modifiers): print('$global', x, y) from cocos.rect import Rect aabb2 = self.explosion2.get_AABB() global_2_bl = self.explosion.point_to_world(aabb2.bottomleft) global_2_tr = self.explosion.point_to_world(aabb2.topright) rect2 = Rect(*global_2_bl, *(global_2_tr - global_2_bl)) print('%local_rect2', self.explosion2.get_rect()) print('%aabb2', self.explosion2.get_AABB()) print('%rect2', rect2) print('Contains:', rect2.contains(x, y)) print()
def cellInView(self, col, row, autofocus=False, force_focus=False): if col < 0 or row < 0 or col >= self.map.numCols or row >= self.map.numRows: return False window_size = director.get_window_size() view_width = window_size[0] - Board.BOARD.TILE_SIZE * 6 view_height = window_size[1] - Board.BOARD.TILE_SIZE * 6 view_bottom_left = self.scroller.screen_to_world(Board.BOARD.TILE_SIZE * 3, Board.BOARD.TILE_SIZE * 3) view_rect = Rect(view_bottom_left[0], view_bottom_left[1], view_width, view_height) cell_screen_pos = Board.board_to_layer(col, row) cell_rect = Rect(cell_screen_pos[0], cell_screen_pos[1], Board.BOARD.TILE_SIZE, Board.BOARD.TILE_SIZE) intersects = cell_rect.intersects(view_rect) if force_focus or (autofocus and not intersects): self.scroller.set_focus(cell_screen_pos[0] + Board.BOARD.TILE_SIZE // 2, cell_screen_pos[1] + Board.BOARD.TILE_SIZE // 2) return intersects
def delete_level(self, x, y): r = Rect(self.delete_level_label.x, self.delete_level_label.y, 130, 20) if r.contains(x, y): status = True self.yes_label.visible = status self.no_label.visible = status self.delete_is_press = status if self.delete_is_press: yes = Rect(self.yes_label.x, self.yes_label.y, 15, 20) no = Rect(self.no_label.x, self.no_label.y, 15, 20) status = False if yes.contains(x, y): path = 'levelfile/level'+str(self.save_as)+'.txt' if os.path.exists(path): os.remove(path) self.save_as = self.get_all_level()[-1] self.yes_label.visible = status self.no_label.visible = status self.delete_is_press = status self.reset_blocks() self.reset_level_select() elif no.contains(x, y): self.delete_is_press = status self.yes_label.visible = status self.no_label.visible = status
def get_rect(self): """Get a :class:`cocos.rect.Rect` for this sprite. Note that this rect's position is most likely NOT the same as the Sprite's position - in fact by default the rect's center is the Sprite's position. If you move the rect around and wish to reflect this change in the Sprite, you will probably have to do something like (again with the default image anchor in the center):: rect = sprite.get_rect() rect.midbottom = (0, 100) sprite.position = rect.center Returns: :class:`cocos.rect.Rect`: The bounding box for this sprite. """ x, y = self.position x -= self.image_anchor_x y -= self.image_anchor_y return Rect(x, y, self.width, self.height)
def __init__(self, viewport_h): self.viewport_height = viewport_h viewport = Rect(0, self.viewport_height, director.window.width, director.window.height - self.viewport_height) #director.window ScrollingManager.__init__(self, viewport) self.config = Config() self.gallery = Gallery() self.is_event_handler = True self.sprite_list = {} # scrolling self.inscroll = False self.scroll_direction = (0, 0) self.marked_tile = (-1, -1) self.combatants = {} self.valid_squares = {} self.move_path = [] #self.fire_path = [] self.spawn_unit = "na" self.bracket = Sprite(self.gallery.content["trn"]["bracket"]) self.bracket.image_anchor = 0, 0 self.bracket.scale = 1 self.bracket.x = -1000 self.bracket.y = -1000 self.bracket_sel = Sprite(self.gallery.content["trn"]["bracket_sel"]) self.bracket_sel.image_anchor = 0, 0 self.bracket_sel.scale = 1 self.bracket_sel.x = -1000 self.bracket_sel.y = -1000 self.adjacent_tiles = OrderedDict() self.adjacent_tiles["N"] = (0, 1) self.adjacent_tiles["E"] = (1, 0) self.adjacent_tiles["S"] = (0, -1) self.adjacent_tiles["W"] = (-1, 0) self.adjacent_tiles["B"] = (1, 1) self.adjacent_tiles["C"] = (1, -1) self.adjacent_tiles["D"] = (-1, -1) self.adjacent_tiles["A"] = (-1, 1)
def __init__(self, viewport_h): """ initializer """ self.viewport_height = viewport_h viewport = Rect(0, self.viewport_height, director.window.width, director.window.height - self.viewport_height) #director.window ScrollingManager.__init__(self, viewport) self.config = Config() self.gallery = Gallery() self.is_event_handler = True # scrolling self.inscroll = False self.scroll_direction = () if len(self.config.loaded_objects) > 0: self.POI = self.config.loaded_objects["POI"] else: self.POI = {} self.transarctica_actor = TransarcticaActor(self) self.transarctica = director.core.query_mover("Transarctica") self.vutrain = [] self.vutrain_actor = [] for id in range(self.config.vutrain_count): self.vutrain_actor.append(id) self.vutrain_actor[id] = VUTrainActor(self, id) self.vutrain.append(id) self.vutrain[id] = director.core.query_mover("VUTrain" + str(id)) self.roamer = [] self.roamer_actor = [] for id in range(self.config.roamer_count): self.roamer_actor.append(id) self.roamer_actor[id] = RoamerActor(self, id) self.roamer.append(id) self.roamer[id] = director.core.query_mover("Roamer" + str(id))
def add_button(): y = 758 x = 10 while 1: indent, but, label = yield y -= but.image.height but.position = (x + indent, y) if isinstance(but.info, str): name = but.info else: name = but.info.name self.add(but, z=1, name=name) lx = x + indent + but.image.width + 10 ly = y + 12 if '\n' in label: ly += 12 if label.count('\n') > 1: y -= 24 * (label.count('\n') - 1) l = Label(label, multiline=True, width=400, position=(lx, ly), color=(255,255,255,255), font_name='Prototype') l.rect = Rect(l.x, l.y, l.element.content_width, l.element.content_height) self.add(l) but.label_ob = l
def find_facing(self, entity, with_behavior=None): # Find nearby things that respond to use # TODO really need to figure out where this kind of code goes. surely # in the entity layer. # TODO also, goddamn, Rect blows. # XXX the idea here is that the "activation" rectangle is a fixed size # in front of the searching entity, extended inwards to touch the # entity's center. this could probably be improved or better-commented # or something. # TODO this makes poor use of the concept of "shape". luckily that # isn't implemented yet. # TODO seems the search area should be round, and yet, it is not. eh? target = Rect( 0, 0, SEARCH_DISTANCE + abs(entity._angle.vector[0]) * entity.entity_type.shape, SEARCH_DISTANCE + abs(entity._angle.vector[1]) * entity.entity_type.shape, ) if entity._angle is flora.util.direction.UP: target.midbottom = entity.position elif entity._angle is flora.util.direction.DOWN: target.midtop = entity.position elif entity._angle is flora.util.direction.LEFT: target.midright = entity.position elif entity._angle is flora.util.direction.RIGHT: target.midleft = entity.position for z, other in self.children_names["entities"].children: if other is entity: continue if with_behavior is not None and with_behavior not in other.behaviors: continue # TODO again, Rect sucks if target.contains(*other.position): # OK, within range # TODO need to, like, pick more cleverly than just the first return other
def draw(self): x, y, w, h = self.ninepatch.draw_around( self.label.element.x, self.label.element.y, self.label.element.content_width, self.label.element.content_height) self.rect = Rect(x, y, w, h)
def new_level(self, x, y): r = Rect(self.new_level_label.x, self.new_level_label.y, 70, 20) if r.contains(x, y): self.create_new_level()
def test_clippedBy__encompassing(): r1 = Rect(0, 0, 10, 10) r2 = Rect(0, 0, 10, 10) assert not r1.clippedBy(r2)
def test_clippedBy__partial_overlappinng(): r1 = Rect(0, 0, 10, 10) r2 = Rect(5, 5, 10, 10) assert r1.clippedBy(r2)
def test_clippedBy__disjoint(): r1 = Rect(0, 0, 10, 10) r2 = Rect(20, 20, 10, 10) assert r1.clippedBy(r2)
def __init__(self, i, j, width, height, properties, tile): Rect.__init__(self, i*width, j*height, width, height) Cell.__init__(self, i, j, width, height, properties, tile)
def on_mouse_press(self, x, y, button, modifiers): if Rect(*self.patch_dimensions).contains(x, y): self.callback(self.parent) self.callback = None self.kill() return True
def __init__(self, i, j, width, height, properties, tile): Rect.__init__(self, i * width, j * height, width, height) Cell.__init__(self, i, j, width, height, properties, tile)
def has_point(self, x, y): px, py = self.position px -= self.width // 2 py -= self.height // 2 r = Rect(px, py, self.width, self.height) return r.contains(x, y)
def get_rect(self): ppos = self.cshape.center r = self.cshape.r # FIXME: Use top, bottom, left, right return Rect(ppos.x - r, ppos.y - r, r * 2, r * 2)
def is_at(self, x, y): p = Rect(x, y, 1, 1) r = Rect(self.x, self.y, self.width, self.height) return p.intersects(r)