Skip to content

Commit

Permalink
Now, can drag and drop skills to reorder them.
Browse files Browse the repository at this point in the history
Related to #23.
Still want to remove the WeaponOrder property,
  having only the order in the DAG matter.
  • Loading branch information
Lunderberg committed Nov 5, 2013
1 parent e9d4102 commit f36c81d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
3 changes: 3 additions & 0 deletions backend/Character.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def Update(self):
self.Events.Execute()
def __getitem__(self,key):
return self.graph[key]
def MoveTo(self,nodeA,nodeB,before=False):
self.graph.MoveTo(nodeA,nodeB,before)
self.Events('Values Reordered',nodeA,nodeB,before)
@property
def Values(self):
return iter(self.graph)
Expand Down
24 changes: 21 additions & 3 deletions backend/DAG.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from utils import listReplace
from collections import defaultdict

from Character import Skill
import Character

def _equals(node,other):
return (node is other) or (other in node.Names)
Expand Down Expand Up @@ -37,7 +37,7 @@ def __iter__(self):
path = [notYielded[0]]
while path:
toYield = path.pop()
if isinstance(toYield,Skill):
if isinstance(toYield,Character.Skill):
path.extend(reversed(self.Children(toYield)))
notYielded.remove(toYield)
yield toYield
Expand Down Expand Up @@ -130,4 +130,22 @@ def Children(self,node):
if isinstance(ch,tuple)]
except KeyError:
raise ValueError("Node not found.", node)

def MoveTo(self,nodeA,nodeB,before=False):
"""
Moves nodeA to the position just after nodeB, or just before, if before is True.
Has no effect if nodeA and nodeB have different skills as parents.
"""
before = bool(before)
parA = [par for par in self.Parents(nodeA) if isinstance(par,Character.Skill)]
parB = [par for par in self.Parents(nodeB) if isinstance(par,Character.Skill)]
if len(parA)==len(parB) and all(p in parB for p in parA):
return
self.nodes.remove(nodeA)
self.nodes.insert(nodeA,self.nodes.index(nodeB)+1-before)
for par in self.Parents(nodeA):
childList = self._children[id(par)]
indexA = next(i for i,tup in enumerate(childList) if tup[0] is nodeA)
indexB = next(i for i,tup in enumerate(childList) if tup[0] is nodeB)
finalPos = indexB + 1 - before
finalPos = finalPos if finalPos<indexA else finalPos-1
childList.insert(finalPos,childList.pop(indexA))
15 changes: 15 additions & 0 deletions gui/MainWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def __init__(self):
self.Connect(self['skillRankBox'],'changed',self.FromActiveSkillRankChange)
self.Connect(self['skillDescriptionBox'].get_buffer(),
'changed',self.FromActiveSkillDescriptionChange)
self['skillView'].enable_model_drag_dest([('text/plain',0,0)],
gtk.gdk.ACTION_DEFAULT | gtk.gdk.ACTION_MOVE)
self['skillView'].enable_model_drag_source(gtk.gdk.BUTTON1_MASK,
[('text/plain',0,0)],
gtk.gdk.ACTION_DEFAULT | gtk.gdk.ACTION_MOVE)
self.Connect(self['skillView'],'drag-data-received',self.FromSkillReordered)

#Weapon reorderings
self.SetUpWeaponSkillView()
Expand Down Expand Up @@ -246,6 +252,7 @@ def LoadChar(self,char):
('Skill Removed',self.skillStore.OnValueRemove),
('Skill Removed',self.skillListStore.OnValueRemove),
('Skill Removed',self.weaponSkillStore.OnValueRemove),
('Values Reordered',self.skillStore.OnValueReorder),
('Resistance Changed',self.OnResistanceChange),
('Item Added',self.itemStore.OnValueAdd),
('Item Changed',self.itemStore.OnValueChange),
Expand Down Expand Up @@ -708,6 +715,14 @@ def FromSkillSelected(self,*args):
self.OnSkillChange(skill)
self.Unblock()
self.SkillSensitivity()
def FromSkillReordered(self,treeview,context,x,y,selection,info,timestamp):
dest_path = treeview.get_dest_row_at_pos(x,y)[0]
model,src_iter = treeview.get_selection().get_selected()
dest_iter = model.get_iter(dest_path)
src_skill = model.get(src_iter,model.col('obj'))[0]
dest_skill = model.get(dest_iter,model.col('obj'))[0]
self.char.MoveTo(src_skill,dest_skill)
self.Update()
def SkillSensitivity(self):
for widName in ['skillNameBox','skillDescriptionBox']:
wid = self[widName]
Expand Down
19 changes: 16 additions & 3 deletions gui/TreeModelHelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def UpdateAll(self,char):
for sk in char.Skills:
parIter = None
for par in sk.Parents:
if (not isinstance(par,str) and
if (not isinstance(par,str) and
par.Name in usedIters):
parIter = usedIters[par.Name]
break
Expand Down Expand Up @@ -176,12 +176,25 @@ def OnValueRemove(self,skill):
if skill is self.get(skIter,self.col('obj'))[0]:
self.remove(skIter)
return
def OnValueReorder(self,nodeA,nodeB,before=False):
iterA = None
iterB = None
for skIter in self.IterAll:
skill = self.get(skIter,self.col('obj'))[0]
if nodeA is skill:
iterA = skIter
if nodeB is skill:
iterB = skIter
if iterA is None or iterB is None:
return
func = self.move_before if before else self.move_after
func(iterA,iterB)

class SkillListStore(ValueListStore):
store_format = SkillTreeStore.store_format
names = {n:i for i,(n,t) in enumerate(store_format)}
def getVals(self,char):
return char.Skills
return char.Skills
UpdateVal = SkillTreeStore.__dict__['UpdateVal']

class WeaponListStore(SkillListStore):
Expand Down Expand Up @@ -234,4 +247,4 @@ def FromHeaderClick(col):
col.connect('clicked',FromHeaderClick)
menuItem.show()
menu.append(menuItem)

24 changes: 12 additions & 12 deletions tests/BigTestChar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ Skill: Tightrope Walking {Movement} : 0

Skill: Combat Training [NoBonus]
Skill: Unarmed Combat {Combat Training, Agility, Strength, Strength} [NoBonus, Weapon]
Skill: Martial Arts Strikes {Unarmed} : 0
Skill: Martial Arts Sweeps {Unarmed} : 0
Skill: Grappling {Unarmed} : 0
Skill: Bites {Unarmed} : 0
Skill: Claws {Unarmed} : 0
Skill: Martial Arts Strikes {Unarmed Combat} : 0
Skill: Martial Arts Sweeps {Unarmed Combat} : 0
Skill: Grappling {Unarmed Combat} : 0
Skill: Bites {Unarmed Combat} : 0
Skill: Claws {Unarmed Combat} : 0
Skill: Melee Combat {Combat Training, Agility, Strength, Strength} [NoBonus, Weapon]
Skill: Broad Sword {Melee} : 0
Skill: Spear {Melee} : 0
Skill: War Hammer {Melee} : 0
Skill: Two\-handed Sword {Melee} : 0
Skill: Broad Sword {Melee Combat} : 0
Skill: Spear {Melee Combat} : 0
Skill: War Hammer {Melee Combat} : 0
Skill: Two\-handed Sword {Melee Combat} : 0
Skill: Thrown Weapons {Combat Training, Agility, Agility, Strength} [NoBonus, Weapon]
Skill: Dagger {Thrown} : 0
Skill: Spear {Thrown} : 0
Skill: Bola {Thrown} : 0
Skill: Dagger {Thrown Weapons} : 0
Skill: Spear {Thrown Weapons} : 0
Skill: Bola {Thrown Weapons} : 0
Skill: Missile {Combat Training, Agility, Agility, Strength} [NoBonus, Weapon]
Skill: Long Bow {Missile} : 0
Skill: Sling {Missile} : 0
Expand Down

0 comments on commit f36c81d

Please sign in to comment.