ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/gde/GCE/MapEditor.pm
Revision: 1.3
Committed: Thu Feb 9 19:59:37 2006 UTC (18 years, 4 months ago) by root
Branch: MAIN
Changes since 1.2: +73 -72 lines
Log Message:
*** empty log message ***

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