source: golgotha/src/golg/editor/commands/undo.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 5.4 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "editor/editor.hh"
10#include "saver_id.hh"
11#include "editor/e_state.hh"
12#include "editor/e_res.hh"
13#include "lisp/li_init.hh"
14#include "lisp/lisp.hh"
15#include "saver.hh"
16
17void g1_editor_class::show_undo_state()
18{
19  int t=undo.tail, t_undos=0, t_redos=0, rt=undo.redo_tail;
20  while (t!=undo.head)
21  {
22    t_undos++;
23    t=(t+1)%undo.max;
24  }
25
26  while (rt!=undo.head)
27  {
28    t_redos++;
29    rt=(rt+1)%undo.max;
30  }   
31}
32
33
34void g1_editor_class::save_undo_info(w32 sections, i4_const_str &fname)
35{
36  li_call("redraw");
37
38  changed();  // note that map is about to be changed so the user can save if they want
39
40  i4_file_class *fp=i4_open(fname, I4_WRITE);
41  if (!fp)
42  {
43    i4_mkdir(g1_ges("undo_dir"));
44    fp=i4_open(fname, I4_WRITE);
45  }
46   
47  if (fp)
48  {
49    g1_saver_class *sfp=new g1_saver_class(fp, i4_T);
50
51    sfp->mark_section(G1_SECTION_MAP_SECTIONS_V1);
52    sfp->write_32(sections);           
53    get_map()->save(sfp, sections);
54    sfp->begin_data_write();
55
56    sfp->mark_section(G1_SECTION_MAP_SECTIONS_V1);
57    sfp->write_32(sections);
58    get_map()->save(sfp, sections);     
59    delete sfp;
60  }
61}
62
63
64void g1_editor_class::add_undo(w32 sections)
65{
66  if (g1_map_is_loaded() && undo.allow)
67  {
68    sections&=(~G1_MAP_VIEW_POSITIONS);   // don't save changes to the camera positions
69
70    if (sections & G1_MAP_OBJECTS)      // if saving objects, save player info because it has
71      sections |= G1_MAP_PLAYERS;       // object references
72   
73   
74    if (sections & (G1_MAP_CELLS
75                    | G1_MAP_VERTS
76                    | G1_MAP_OBJECTS
77                    | G1_MAP_LIGHTS
78                    | G1_MAP_SELECTED_VERTS))
79      get_map()->recalc |= G1_RECALC_RADAR_VIEW;
80
81    if (sections & (G1_MAP_CELLS | G1_MAP_VERTS | G1_MAP_SELECTED_VERTS))
82      get_map()->recalc |= G1_RECALC_WATER_VERTS;
83
84
85    if (!sections) return;
86
87    g1_edit_state.hide_focus();
88
89    if (((undo.head+1) % undo.max) == undo.tail)
90      undo.tail = (undo.tail+1)%undo.max;
91
92    int cur_undo=undo.head;
93
94    i4_str *undo_file=g1_ges("undo_file").sprintf(100,cur_undo);
95
96    save_undo_info(sections, *undo_file);
97
98
99    delete undo_file;
100
101    can_undo=i4_T;
102    can_redo=i4_F;
103
104    undo.head=(undo.head+1) % undo.max;
105    undo.redo_tail=undo.head;
106
107    g1_edit_state.show_focus();
108  }
109
110  show_undo_state();
111}
112
113
114li_object *g1_add_undo(li_object *o, li_environment *env)
115{
116  g1_editor_instance.add_undo(li_get_int(li_eval(li_car(o,env),env),env));
117  return 0;
118}
119
120li_automatic_add_function(g1_add_undo, "add_undo");
121
122
123void g1_editor_class::do_undo()
124{
125
126
127  if (g1_map_is_loaded() && undo.redo_tail!=undo.tail)
128  {
129    i4_str *old_name=new i4_str(g1_get_map()->get_filename());
130
131    g1_edit_state.hide_focus();
132
133    undo.redo_tail=(undo.redo_tail + undo.max - 1) % undo.max;
134
135    i4_str *undo_file=g1_ges("undo_file").sprintf(100,undo.redo_tail);
136    i4_file_class *fp=i4_open(*undo_file);
137    if (fp)
138    {
139      g1_loader_class *lfp=g1_open_save_file(fp, i4_T);
140      if (lfp->goto_section(G1_SECTION_MAP_SECTIONS_V1))
141      {
142        w32 sections=lfp->read_32();
143     
144        i4_str *redo_file=g1_ges("redo_file").sprintf(100,undo.redo_tail);       
145        save_undo_info(sections, *redo_file);
146
147        delete redo_file;
148
149        undo.allow=0;
150
151        if ((sections|G1_MAP_VIEW_POSITIONS)==G1_MAP_ALL)
152          g1_load_level(*undo_file, 1, 0);   // need to reload textures for this one
153        else
154          get_map()->load(lfp, sections);
155
156        undo.allow=1;
157      }
158
159      delete lfp;
160    }
161    delete undo_file;
162
163    g1_get_map()->set_filename(*old_name);
164    delete old_name;
165
166    if (undo.redo_tail==undo.tail)
167      can_undo=i4_F;
168
169    can_redo=i4_T;
170    li_call("redraw");
171
172    g1_edit_state.show_focus();
173  }
174
175  show_undo_state();
176}
177
178void g1_editor_class::do_redo()
179{
180  if (g1_map_is_loaded() && undo.redo_tail!=undo.head)
181  {
182    i4_str *old_name=new i4_str(g1_get_map()->get_filename());
183
184    g1_edit_state.hide_focus();
185
186    i4_str *redo_file=g1_ges("redo_file").sprintf(100,undo.redo_tail);
187    i4_file_class *fp=i4_open(*redo_file);
188    if (fp)
189    {   
190      g1_loader_class *lfp=g1_open_save_file(fp, i4_T);
191      if (lfp)
192      {
193        if (lfp->goto_section(G1_SECTION_MAP_SECTIONS_V1))
194        {
195          w32 sections=lfp->read_32();
196          undo.allow=0;
197
198          if (sections==G1_MAP_ALL)
199            g1_load_level(*redo_file, 1, 0);   // need to reload textures for this one
200          else
201            get_map()->load(lfp, sections);
202
203          undo.allow=1;
204        }
205        delete lfp;
206      }
207    }
208    delete redo_file;
209
210    g1_get_map()->set_filename(*old_name);
211    delete old_name;
212
213
214    undo.redo_tail = (undo.redo_tail + 1) % undo.max;
215   
216    if (undo.redo_tail==undo.head)
217    {
218      can_redo=i4_F;
219    }
220
221    li_call("redraw");
222
223    g1_edit_state.show_focus();
224  }
225
226  show_undo_state();
227}
Note: See TracBrowser for help on using the repository browser.