Skip to content

Commit

Permalink
upgrade selecting
Browse files Browse the repository at this point in the history
Clicking will now have a small radius around it, so that small things (like path points) are easier to click.

I also added the ability to right click a path point, and delete it (as well as using the delete key). Currently it does not automatically decrease the other path point numbers, but that may come later.

I also made sure that using the arrow keys does not move the selection in the object selector (by just shifting focus to the level canvas after clicking).
  • Loading branch information
ego-lay-atman-bay committed Jul 11, 2024
1 parent 9ab9a8b commit eec2e6a
Showing 1 changed file with 132 additions and 74 deletions.
206 changes: 132 additions & 74 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,15 @@ def updateObject(self, obj : wmwpy.classes.Object | None):
image = obj.foreground_PhotoImage,
tags = ('object', 'foreground', id)
)

if len(obj._foreground) == 0 and len(obj._background) == 0:
self.level_canvas.create_image(
canvas_pos[0],
canvas_pos[1],
anchor = 'c',
image = ImageTk.PhotoImage(Image.new('RGBA', (1, 1), 'black')),
tags = ('object', 'foreground', id),
)

if (obj == self.selectedObject or self.settings.get('view.radius', True)) and obj.Type is not None:
properties = filter(lambda name : obj.Type.PROPERTIES[name].get('type', 'string') == 'radius', obj.Type.PROPERTIES)
Expand Down Expand Up @@ -924,21 +933,22 @@ def updateObject(self, obj : wmwpy.classes.Object | None):
tags = ('part', 'path', property, 'pathPoint', id),
)

if is_closed:
line = self.level_canvas.create_polygon(
path_canvas_points,
fill = '',
outline = 'black',
width = 2,
tags = ('passthrough', 'part', 'path', property, 'pathLine', id),
)
else:
line = self.level_canvas.create_line(
path_canvas_points,
fill = 'black',
width = 2,
tags = ('passthrough', 'part', 'path', property, 'pathLine', id),
)
if len(path_canvas_points) > 1:
if is_closed:
line = self.level_canvas.create_polygon(
path_canvas_points,
fill = '',
outline = 'black',
width = 2,
tags = ('passthrough', 'part', 'path', property, 'pathLine', id),
)
else:
line = self.level_canvas.create_line(
path_canvas_points,
fill = 'black',
width = 2,
tags = ('passthrough', 'part', 'path', property, 'pathLine', id),
)

# self.level_canvas.tag_bind(line, '<Button-1>', self.onLevelClick)

Expand All @@ -952,27 +962,34 @@ def updateObject(self, obj : wmwpy.classes.Object | None):

self.updateSelectionRectangle()
self.updateLevelScroll()


def onLevelClick(self, event : tk.Event):
logging.debug('level')

if self.level == None:
return

mouse = (self.level_canvas.canvasx(event.x), self.level_canvas.canvasy(event.y))

def selectObjectAt(
self,
pos: tuple[float, float] | list[float] | tk.Event,
halo: int | float = 5,
):
event = None
if isinstance(pos, tk.Event):
event = pos
logging.debug(f'select obj event: {event.__dict__}')
pos = (pos.x, pos.y)

pos = (
self.level_canvas.canvasx(pos[0]),
self.level_canvas.canvasy(pos[1])
)

# objects = self.level_canvas.find_overlapping(*mouse, *mouse)
objects = self.level_canvas.find_overlapping(
mouse[0] - 5, mouse[1] - 5,
mouse[0] + 5, mouse[1] + 5,
pos[0] - halo, pos[1] - halo,
pos[0] + halo, pos[1] + halo,
)
logging.debug(f'under mouse: {objects}')
# logging.debug(f'close: {close}')
length = len(objects)

for id in reversed(objects):
tags = self.level_canvas.gettags(id)
logging.debug(f'tags: {tags}')
if tags[0] in ['selection', 'level']:
continue

Expand All @@ -983,6 +1000,7 @@ def onLevelClick(self, event : tk.Event):

if tags[obj_tag].startswith('object-'):
obj = self.level.getObjectById(tags[obj_tag][7::])
logging.debug(f'obj: {obj}')
if obj == None:
continue
else:
Expand All @@ -994,24 +1012,36 @@ def onLevelClick(self, event : tk.Event):
continue
elif tags[0] == 'object':
logging.debug(f'selecting obj: {obj.name}')
self.selectObject(obj)
return
self.selectObject(obj, event)
return 'object', obj
elif tags[0] == 'part':
self.selectPart(
obj,
tags[1],
id,
tags[2],
)
return
return 'part', obj, self.selectedPart

self.selectObject(None)

return None, None

def onLevelClick(self, event : tk.Event):
logging.debug('level')

if self.level == None:
return

self.selectObjectAt(event)



def onLevelMove(self, event: tk.Event):
if self.selectedPart:
if self.selectedPart['type'] != None:
self.dragPart(event)
elif self.selectedObject:
self.dragObject(self.selectedObject, event)


def createLevelContextMenu(self):
Expand All @@ -1022,19 +1052,15 @@ def createLevelContextMenu(self):
def onLevelRightClick(self, event):
logging.debug('level context menu')

mouse = (self.level_canvas.canvasx(event.x), self.level_canvas.canvasy(event.y))
selected_type, selected_obj, *extra = self.selectObjectAt(event)

objects = self.level_canvas.find_overlapping(*mouse, *mouse)
logging.debug(objects)
length = len(objects)

if length <= 1:
if length == 1:
if objects[0] != self.level_images['background']:
return

self.selectObject(None)
if selected_type == None:
self.showPopup(self.levelContextMenu, event)
elif selected_type == 'object':
self.showPopup(self.createObjectContextMenu(selected_obj), event)
elif selected_type == 'part':
self.showPopup(self.createPartContextMenu(selected_obj, extra[0]), event)


def bindObject(self, id, obj : wmwpy.classes.Object | None = None):
if obj == None:
Expand All @@ -1043,26 +1069,26 @@ def bindObject(self, id, obj : wmwpy.classes.Object | None = None):
else:
return

self.level_canvas.tag_bind(
id,
'<Button1-Motion>',
lambda e, object = obj: self.dragObject(object, e)
)
# self.level_canvas.tag_bind(
# id,
# '<Button1-Motion>',
# lambda e, object = obj: self.dragObject(object, e)
# )

context_menu = self.createObjectContextMenu(obj)

if platform.system() == 'Darwin':
self.level_canvas.tag_bind(
id,
'<Button-2>',
lambda e, object = obj, menu = context_menu: self.showPopup(menu, e, callback = lambda : self.selectObject(object))
)
else:
self.level_canvas.tag_bind(
id,
'<Button-3>',
lambda e, object = obj, menu = context_menu: self.showPopup(menu, e, callback = lambda : self.selectObject(object))
)
# if platform.system() == 'Darwin':
# self.level_canvas.tag_bind(
# id,
# '<Button-2>',
# lambda e, object = obj, menu = context_menu: self.showPopup(menu, e, callback = lambda : self.selectObject(object))
# )
# else:
# self.level_canvas.tag_bind(
# id,
# '<Button-3>',
# lambda e, object = obj, menu = context_menu: self.showPopup(menu, e, callback = lambda : self.selectObject(object))
# )

def unbindObject(self, id):
self.level_canvas.tag_unbind(
Expand Down Expand Up @@ -1092,6 +1118,23 @@ def createObjectContextMenu(self, obj : wmwpy.classes.Object):
self.objectContextMenu.add_command(label = 'delete', command = lambda *args : self.deleteObject(obj), accelerator = 'Del')

return self.objectContextMenu

def createPartContextMenu(
self,
obj: wmwpy.classes.Object,
part: dict[
typing.Literal[
'type',
'id',
'property'
], str | None,
],
):

self.objectContextMenu.delete(0, 3)
self.objectContextMenu.add_command(label = 'delete', command = lambda *args : self.deleteProperty(obj, part['property']), accelerator = 'Del')

return self.objectContextMenu

def showPopup(self, menu : tk.Menu, event : tk.Event = None, callback : typing.Callable = None):
try:
Expand Down Expand Up @@ -1140,16 +1183,27 @@ def deleteObject(self, obj : wmwpy.classes.Object = None):
if obj == None:
return

self.level_canvas.delete(f'object-{str(obj.id)}')

if obj in self.level.objects:
index = self.level.objects.index(obj)
del self.level.objects[index]

if obj == self.selectedObject:
self.selectObject(None)

self.updateObjectSelector()
if self.selectedPart['type']:
self.deleteProperty(obj, self.selectedPart['property'])
else:
self.level_canvas.delete(f'object-{str(obj.id)}')

if obj in self.level.objects:
index = self.level.objects.index(obj)
del self.level.objects[index]

if obj == self.selectedObject:
self.selectObject(None)

self.updateObjectSelector()

def deleteProperty(self, obj: wmwpy.classes.Object, property: str):
if property in obj.properties:
del obj.properties[property]

self.updateObject(obj)
if self.selectedObject == obj:
self.updateProperties()

def checkLevelFocus(
self,
Expand Down Expand Up @@ -1729,6 +1783,7 @@ def selectObject(event: tk.Event):
item = self.object_selector['treeview'].selection()
# logging.debug(f"selection: {self.object_selector['treeview'].selection()}")
item = self.object_selector['treeview'].item(item)
self.level_canvas.focus_set()

if 'object' in item['tags']:
id = item['values'][2]
Expand Down Expand Up @@ -1799,7 +1854,6 @@ def rightClick(event):
self.showPopup(self.object_selector['menu'], event)

self.object_selector['treeview'].bind('<ButtonRelease-1>', selectObject)
self.object_selector['treeview'].bind('<Return>', selectObject)

if platform.system() == 'Darwin':
self.object_selector['treeview'].bind('<Button-2>', rightClick)
Expand Down Expand Up @@ -1918,7 +1972,7 @@ def getRelativeMousePos(self, pos : tuple, widget : tk.Widget):
def selectObject(
self,
obj : wmwpy.classes.Object = None,
event: tk.Event = None,
event: tk.Event | None = None,
partInfo: dict[str, str] = None,
):
self.selectedPart = {
Expand All @@ -1936,21 +1990,24 @@ def selectObject(
self.selectedPart['id'] = partInfo.get('id', None)
self.selectedPart['property'] = partInfo.get('property', None)

if event:
obj_pos = self.getObjectPosition(obj.pos, obj.offset)
self.dragInfo['offset'] = numpy.array((obj_pos[0], obj_pos[1])) - (self.level_canvas.canvasx(event.x), self.level_canvas.canvasy(event.y))

# logging.debug(obj.name)
logging.debug('object')

self.updateObject(obj)
self.updateProperties()
self.updateSelectionRectangle()
if event:
obj_pos = self.getObjectPosition(obj.pos, obj.offset)
self.dragInfo['offset'] = numpy.array((obj_pos[0], obj_pos[1])) - (self.level_canvas.canvasx(event.x), self.level_canvas.canvasy(event.y))
else:
self.dragInfo['offset'] = (0,0)

if self.selectedObject == None:
if len(self.object_selector['treeview'].selection()) > 0:
self.object_selector['treeview'].selection_remove(self.object_selector['treeview'].selection()[0])
else:

children = self.object_selector['treeview'].get_children('')

selected = None
Expand Down Expand Up @@ -1985,6 +2042,7 @@ def selectPart(
}
)
logging.debug(f'selected part: {self.selectedPart}')
return self.selectedPart

def dragPart(
self,
Expand Down

0 comments on commit eec2e6a

Please sign in to comment.