ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MapEditor.pm
Revision: 1.2
Committed: Thu Feb 9 15:45:52 2006 UTC (18 years, 4 months ago) by elmex
Branch: MAIN
Changes since 1.1: +4 -4 lines
Log Message:
improved attribute editor

File Contents

# User Rev Content
1 elmex 1.1 package GCE::MapEditor;
2    
3     =head1 NAME
4    
5     GCE::MapEditor - the map editing widget
6    
7     =cut
8    
9     use Gtk2;
10     use Gtk2::Gdk::Keysyms;
11     use Gtk2::SimpleMenu;
12    
13     use Crossfire;
14     use Crossfire::MapWidget;
15    
16     use GCE::AttrEdit;
17    
18     use Glib::Object::Subclass Gtk2::VBox;
19    
20     use strict;
21    
22     sub show_attr_editor_on_demand {
23     my ($self) = @_;
24    
25     return if $self->{attr_edit};
26    
27     my $w = $self->{attr_edit_win} = Gtk2::Window->new;
28     $w->set_title ("gce - edit attrs");
29     $w->add ($self->{attr_edit} = GCE::AttrEdit->new);
30 elmex 1.2 $w->signal_connect ('delete-event' => sub { delete $self->{attr_edit}; 0 });
31 elmex 1.1 $w->show_all;
32     }
33    
34     sub update_attr_editor {
35 elmex 1.2 my ($self, $arch, $ro) = @_;
36 elmex 1.1
37     $self->show_attr_editor_on_demand ();
38    
39     return 0 unless defined $self->{attr_edit};
40    
41 elmex 1.2 $self->{attr_edit}->set_arch ($arch, $ro);
42 elmex 1.1 $self->{attr_edit_win}->set_title ("gce - edit $arch->{_name}");
43     }
44    
45     sub set_place_arch {
46     my ($self, $arch) = @_;
47    
48     $self->{pick_arch} = $arch;
49     }
50    
51     sub delete_arch {
52     my ($self, $x, $y) = @_;
53    
54     defined $self->{map}
55     or return 0;
56    
57     my $s = map_pop_tile_stack ($self->{map}, $x, $y);
58     $self->{map}->update_map ($x, $y, 1, 1);
59     }
60    
61     sub get_top_arch {
62     my ($self, $x, $y) = @_;
63    
64     defined $self->{map}
65     or return 0;
66    
67     map_get_tile_stack ($self->{map}, $self->{map}->coord ($x, $y))->[-1]
68     }
69    
70     sub place_pick {
71     my ($self, $x, $y) = @_;
72    
73     defined $self->{map}
74     and defined $self->{pick_arch}
75     or return 0;
76    
77     my $s = map_get_tile_stack ($self->{map}, $x, $y);
78     my $arch = { _name => $self->{pick_arch}->{_name} };
79    
80 elmex 1.2 if (not defined ($s->[-1]) or $s->[-1]->{_name} ne $arch->{_name}) {
81 elmex 1.1
82     map_push_tile_stack ($self->{map}, $x, $y, $arch);
83     }
84    
85     $self->{map}->update_map ($x, $y, 1, 1);
86     }
87    
88     sub open_map {
89     my ($self, $path) = @_;
90    
91     $self->{map}->set_map (arch2map read_arch $path);
92     }
93    
94     sub INIT_INSTANCE {
95     my ($self) = @_;
96    
97     my $map = new Crossfire::MapWidget;
98    
99     # XXX: Make a nicer pick-view (with picture)
100     $self->pack_start (my $lbl = Gtk2::Label->new ("no selection"), 0, 1, 0);
101     $self->{pick_view} = $lbl;
102    
103     $self->pack_start ($map, 1, 1, 0);
104     $self->{map} = $map;
105    
106     $map->signal_connect (button_press_event => sub {
107     my ($map, $event) = @_;
108    
109     my ($x, $y) = $map->coord ($event->x, $event->y);
110    
111     if ($event->state * "shift-mask") {
112    
113     if ($event->button == 1) {
114    
115     $self->update_attr_editor ($self->get_top_arch ($event->x, $event->y));
116    
117     return 1;
118     }
119    
120     } else {
121    
122     if ($event->button == 1) {
123    
124     $map->disable_tooltip unless $self->{draw_mode};
125     $self->{draw_mode} = [1, $x, $y];
126     $self->place_pick ($x, $y);
127    
128     return 1;
129    
130     } elsif ($event->button == 3) {
131    
132     $map->disable_tooltip unless $self->{draw_mode};
133     $self->{draw_mode} = [2, $x, $y];
134     $self->delete_arch ($x, $y);
135    
136     return 1;
137     }
138     }
139    
140     0
141     });
142    
143     $map->signal_connect_after (motion_notify_event => sub {
144     my ($map, $event) = @_;
145    
146     $self->{draw_mode}
147     or return;
148    
149     my ($X, $Y) = @{$self->{draw_mode}}[1,2];
150     my ($x, $y) = $map->coord ($map->get_pointer);
151    
152     while ($x != $X || $y != $Y) {
153    
154     $X++ if $X < $x;
155     $X-- if $X > $x;
156     $Y++ if $Y < $y;
157     $Y-- if $Y > $y;
158    
159     if ($self->{draw_mode}[0] == 1) {
160    
161     $self->place_pick ($X, $Y);
162    
163     } elsif ($self->{draw_mode}[0] == 2) {
164    
165     $self->delete_arch ($X, $Y);
166     }
167     }
168    
169     @{$self->{draw_mode}}[1,2] = ($X, $Y);
170    
171     1
172     });
173    
174     $map->signal_connect (button_release_event => sub {
175     my ($map, $event) = @_;
176    
177     $map->enable_tooltip if delete $self->{draw_mode};
178    
179     0
180     });
181     }
182    
183     sub update_pick {
184     my ($self, $arch) = @_;
185    
186     $self->{pick_arch} = $arch;
187     $self->{pick_view}->set_text ($arch->{_name});
188     }
189    
190     =head1 AUTHOR
191    
192     Marc Lehmann <schmorp@schmorp.de>
193     http://home.schmorp.de/
194    
195     Robin Redeker <elmex@ta-sa.org>
196     http://www.ta-sa.org/
197    
198     =cut
199     1;
200