ruby: add curview, fix item_find/unit_find wrt gview mode, fix refers-to to use binsearch

develop
jj 2012-07-03 19:51:52 +02:00
parent 501fd43dda
commit 2e42e31754
4 changed files with 50 additions and 15 deletions

@ -298,8 +298,21 @@ sub render_field_reftarget {
return if (!$tg); return if (!$tg);
my $tgvec = $tg->getAttribute('instance-vector'); my $tgvec = $tg->getAttribute('instance-vector');
return if (!$tgvec); return if (!$tgvec);
my $idx = $tg->getAttribute('key-field') || 'id';
render_field_refto($parent, $name, $tgvec); $tgvec =~ s/^\$global/df/;
return if $tgvec !~ /^[\w\.]+$/;
my $tgname = "${name}_tg";
$tgname =~ s/_id(.?.?)_tg/_tg$1/;
for my $othername (map { $_->getAttribute('name') } $parent->findnodes('child::ld:field')) {
$tgname .= '_' if ($othername and $tgname eq $othername);
}
my $fidx = '';
$fidx = ', :' . $idx if ($idx ne 'id');
push @lines_rb, "def $tgname ; ${tgvec}.binsearch($name$fidx) ; end";
} }
sub render_field_refto { sub render_field_refto {
@ -329,9 +342,9 @@ sub render_container_reftarget {
return if (!$tg); return if (!$tg);
my $tgvec = $tg->getAttribute('instance-vector'); my $tgvec = $tg->getAttribute('instance-vector');
return if (!$tgvec); return if (!$tgvec);
my $idx = $tg->getAttribute('key-field') || 'id';
$tgvec =~ s/^\$global/df/; $tgvec =~ s/^\$global/df/;
$tgvec =~ s/\[\$\]$//;
return if $tgvec !~ /^[\w\.]+$/; return if $tgvec !~ /^[\w\.]+$/;
my $tgname = "${name}_tg"; my $tgname = "${name}_tg";
@ -341,7 +354,9 @@ sub render_container_reftarget {
$tgname .= '_' if ($othername and $tgname eq $othername); $tgname .= '_' if ($othername and $tgname eq $othername);
} }
push @lines_rb, "def $tgname ; $name.map { |i| ${tgvec}[i] } ; end"; my $fidx = '';
$fidx = ', :' . $idx if ($idx ne 'id');
push @lines_rb, "def $tgname ; $name.map { |i| $tgvec.binsearch(i$fidx) } ; end";
} }
sub render_class_vmethods { sub render_class_vmethods {

@ -4,10 +4,18 @@ module DFHack
# arg similar to unit.rb/unit_find; no arg = 'k' menu # arg similar to unit.rb/unit_find; no arg = 'k' menu
def item_find(what=:selected) def item_find(what=:selected)
if what == :selected if what == :selected
case ui.main.mode if curview._rtti_classname == :viewscreen_itemst
when :LookAround ref = curview.entry_ref[curview.cursor_pos]
k = ui_look_list.items[ui_look_cursor] ref.item_tg if ref.kind_of?(GeneralRefItem)
k.item if k.type == :Item else
case ui.main.mode
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.item if k.type == :Item
when :BuildingItems
bld = world.selected_building
bld.contained_items[ui_building_item_cursor].item if bld
end
end end
elsif what.kind_of?(Integer) elsif what.kind_of?(Integer)
world.items.all.binsearch(what) world.items.all.binsearch(what)

@ -1,6 +1,13 @@
# df user-interface related methods # df user-interface related methods
module DFHack module DFHack
class << self class << self
# returns the current active viewscreen
def curview
ret = gview.view
ret = ret.child while ret.child
ret
end
# center the DF screen on something # center the DF screen on something
# updates the cursor position if visible # updates the cursor position if visible
def center_viewscreen(x, y=nil, z=nil) def center_viewscreen(x, y=nil, z=nil)

@ -6,14 +6,19 @@ module DFHack
# with an argument that respond to x/y/z (eg cursor), find first unit at this position # with an argument that respond to x/y/z (eg cursor), find first unit at this position
def unit_find(what=:selected) def unit_find(what=:selected)
if what == :selected if what == :selected
case ui.main.mode if curview._rtti_classname == :viewscreen_itemst
when :ViewUnits ref = curview.entry_ref[curview.cursor_pos]
# nobody selected => idx == 0 ref.unit_tg if ref.kind_of?(GeneralRefUnit)
v = world.units.active[ui_selected_unit] else
v if v and v.pos.z == cursor.z case ui.main.mode
when :LookAround when :ViewUnits
k = ui_look_list.items[ui_look_cursor] # nobody selected => idx == 0
k.unit if k.type == :Unit v = world.units.active[ui_selected_unit]
v if v and v.pos.z == cursor.z
when :LookAround
k = ui_look_list.items[ui_look_cursor]
k.unit if k.type == :Unit
end
end end
elsif what.kind_of?(Integer) elsif what.kind_of?(Integer)
world.units.all.binsearch(what) world.units.all.binsearch(what)