source: golgotha/src/golg/editor/e_state.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: 9.0 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 "gui/button.hh"
10#include "gui/butbox.hh"
11#include "editor/editor.hh"
12#include "editor/e_state.hh"
13#include "gui/text.hh"
14#include "device/event.hh"
15#include "gui/deco_win.hh"
16#include "gui/image_win.hh"
17#include "loaders/load.hh"
18#include "window/colorwin.hh"
19#include "window/win_evt.hh"
20#include "editor/dialogs/tile_picker.hh"
21#include "editor/dialogs/object_picker.hh"
22#include "editor/contedit.hh"
23#include "player.hh"
24#include "app/app.hh"
25#include "map_man.hh"
26#include "editor/e_res.hh"
27#include "lisp/li_init.hh"
28
29g1_edit_state_class g1_edit_state;
30
31g1_controller_edit_class *g1_edit_state_class::current_focus()
32{
33  if (g1_editor_instance.in_editor() &&  g1_current_controller.get())
34    return ((g1_controller_edit_class *)g1_current_controller.get());
35  else
36    return 0;
37}
38
39void g1_edit_state_class::hide_focus()
40{
41  if (current_focus())
42    current_focus()->hide_focus();
43}
44
45
46void g1_edit_state_class::show_focus()
47{
48  if (current_focus())
49    current_focus()->show_focus();
50}
51
52i4_graphical_style_class *g1_edit_state_class::get_style()
53{
54  return i4_current_app->get_style();
55}
56
57
58i4_button_class *g1_edit_state_class::create_button(char *res_name,
59                                                    w32 evalue,
60                                                    i4_bool popup,
61                                                    i4_event_handler_class *send_to,
62                                                    i4_event *send_event)
63{
64  if (send_to==0)
65    send_to=this;
66
67  i4_const_str s=g1_editor_instance.get_editor_string(res_name);
68  if (s.null())
69    i4_error("missing %s from res/editor.res",res_name);
70
71  i4_const_str::iterator i=s.begin();
72  sw32 icon_num=i.read_number();
73
74  load_icons();
75
76  if (icon_num<0 || icon_num>=total_icons)
77    i4_error("bad icon #, %d",icon_num);
78
79  // change this to an icon at some point
80  i4_image_window_class *t=new i4_image_window_class(icons[icon_num]);
81
82  if (!send_event)
83    send_event=new i4_object_message_event_class(send_to, evalue);
84
85  i4_event_reaction_class *r=new i4_event_reaction_class(send_to, send_event);
86
87  char help[30];
88  sprintf(help,"%s_help",res_name);
89  i4_button_class *b=new i4_button_class(&g1_ges(help), t, get_style(), r);
90
91
92  if (popup)
93    b->set_popup(popup);
94
95
96  return b;
97}
98
99void g1_edit_state_class::add_but(i4_button_box_class *box,                     
100                                  char *res_name,
101                                  w32 evalue,
102                                  i4_bool down,
103                                  i4_event *send_event)
104{
105  char help[30];
106  sprintf(help,"%s_help",res_name);
107  i4_button_class *b=create_button(res_name, evalue, i4_F, 0, send_event);
108
109  box->add_button(0,0, b);
110 
111  if (down)
112    box->push_button(b, i4_F);
113
114}
115
116
117i4_window_class *g1_edit_state_class::create_buttons(w32 height)
118{
119  i4_graphical_style_class *style=get_style();
120
121  i4_button_box_class *box=new i4_button_box_class(this);
122
123  int i=0;
124  g1_mode_creator *mc, *cur=0;
125
126  for (mc=g1_mode_creator::first; mc; mc=mc->next, i++)
127  {
128    if (strcmp(major_mode, mc->name())==0)
129      cur=mc;
130 
131    add_but(box, mc->name(), 0, (i4_bool) (cur==mc), new g1_set_major_mode_event(mc->name()));
132  }
133
134  if (cur)
135  {
136    box->arrange_right_down();
137
138    i4_parent_window_class *minor_container=new i4_color_window_class(0, height,
139                                                                      style->color_hint->neutral(),
140                                                                      style);
141    cur->create_buttons(minor_container);
142    minor_container->resize_to_fit_children();
143   
144    i4_deco_window_class *d;
145    d=new i4_deco_window_class(box->width() + minor_container->width(), height, i4_F, style);
146
147
148    d->add_child(d->get_x1(), d->get_y1(), box);
149    d->add_child(d->get_x1() + box->width(), d->get_y1(), minor_container);
150   
151    return d;
152  }
153
154  if (box)
155    delete box;
156
157  return 0;
158}
159
160void g1_edit_state_class::show_tools(i4_parent_window_class *p,
161                                     w32 _win_h)
162{
163  win_h=_win_h;
164  parent=p;
165
166  if (tools)
167    delete tools;
168  tools=create_buttons(win_h);
169  parent->add_child(parent->width()-tools->width(), 0, tools);
170 
171}
172
173void g1_edit_state_class::hide_tools()
174{
175  if (tools)
176  {
177    delete tools;
178    tools=0;
179    parent=0;
180  }
181
182  for (g1_mode_creator *mc=g1_mode_creator::first; mc; mc=mc->next)
183    mc->cleanup();
184}
185
186void g1_edit_state_class::snap_point(i4_3d_point_class &p, int do_z)
187{
188  switch (snap)
189  {
190    case SNAP_CENTER :
191    {           
192      p.x=((int)(p.x-0.5))+0.5;
193      p.y=((int)(p.y-0.5))+0.5;
194      if (do_z)
195        p.z=((int)(p.z-0.5))+0.5;
196    } break;
197
198    case SNAP_ORIGIN :
199    {           
200      p.x=((int)p.x);
201      p.y=((int)p.y);
202      if (do_z)
203        p.y=((int)p.y);
204    } break;
205  }
206}
207
208void g1_edit_state_class::set_current_team(int team_num)
209{
210  current_team=team_num;
211}
212
213g1_edit_state_class::g1_edit_state_class()
214{
215  current_team=0;
216
217  snap=NO_SNAP;
218  icons=0;
219  strcpy(major_mode, "LIGHT");
220  tools=0;
221  parent=0;
222 }
223
224g1_mode_creator *g1_edit_state_class::get_major_mode()
225{
226  for (g1_mode_creator *mc=g1_mode_creator::first; mc; mc=mc->next)
227  {
228    if (strcmp(major_mode, mc->name())==0)
229      return mc;
230  }
231
232  return 0;
233}
234
235i4_bool g1_edit_state_class::set_major_mode(char *mode_name)
236{
237  hide_focus();
238
239  for (g1_mode_creator *mc=g1_mode_creator::first; mc; mc=mc->next)
240  {
241    if (strcmp(mode_name, mc->name())==0)
242    {
243      strcpy(major_mode, mc->name());
244
245      if (tools)
246        delete tools;
247
248      tools=create_buttons(win_h);
249      parent->add_child(parent->width()-tools->width(), 0, tools);
250
251      show_focus();     
252 
253      g1_editor_instance.major_mode_change();
254
255      return i4_T;
256    }
257  }     
258  return i4_F;
259}
260
261i4_bool g1_edit_state_class::set_minor_mode(char *major_mode_name, w32 minor_mode)
262{
263  i4_bool ret;
264  hide_focus();
265
266
267  if (strcmp(major_mode, major_mode_name)==0 || set_major_mode(major_mode_name))
268  {
269    ret=get_major_mode()->set_minor_mode(minor_mode);
270   
271    if (tools)
272    {
273      delete tools;
274      tools=create_buttons(win_h);
275      parent->add_child(parent->width()-tools->width(), 0, tools);
276    }
277  }
278  else
279    ret=i4_F;
280 
281  show_focus();
282
283  return ret;
284}
285
286void g1_edit_state_class::receive_event(i4_event *ev)
287{
288  CAST_PTR(oev, i4_object_message_event_class, ev);
289
290  if (ev->type()==i4_event::USER_MESSAGE)
291  {
292    CAST_PTR(uev, i4_user_message_event_class, ev);
293    if (uev->sub_type==G1_SET_MAJOR_MODE)
294    {
295      CAST_PTR(sm, g1_set_major_mode_event, ev);
296      set_major_mode(sm->name);
297    }
298    else if (uev->sub_type==G1_SET_MINOR_MODE)
299    {     
300      CAST_PTR(sm, g1_set_minor_mode_event, ev);
301      set_minor_mode(sm->name, sm->minor_mode);
302    }
303  }
304}
305
306
307void g1_edit_state_class::load_icons()
308{
309  if (icons) return;
310
311  i4_const_str *e_cons=g1_editor_instance.get_editor_array("e_icons"), *e;
312  e=e_cons;
313
314  for (total_icons=0; !e->null(); total_icons++, e++);
315  e=e_cons;
316
317  icons=(i4_image_class **)i4_malloc(sizeof(i4_image_class *) * total_icons, "icons");
318
319  for (w32 i=0; i<total_icons; i++, e++)
320  {
321    if (e->null())
322      i4_error("not enough icons in e_icons array");
323
324    icons[i]=i4_load_image(*e);
325  }
326
327  i4_free(e_cons);
328}
329
330
331void g1_edit_state_class::uninit()
332{
333  hide_tools();
334  int i;
335 
336
337  for (i=0; i<total_icons; i++)
338    if (icons[i])
339    {
340      delete icons[i];
341      icons[i]=0;
342    }
343
344  if (icons)
345    i4_free(icons);
346  icons=0;
347  total_icons=0;
348}
349
350w32 g1_edit_state_class::tools_width()
351{
352  if (tools)
353    return tools->width();
354  else return 0;
355}
356
357
358
359
360void g1_edit_state_class::context_help_struct::show(const i4_const_str &help,
361                                                    int _mx, int _my)
362{
363  hide();
364  mx=_mx; my=_my;
365  window=i4_current_app->get_style()->create_quick_context_help(_mx, _my+15, help);
366}
367
368void g1_edit_state_class::context_help_struct::hide()
369{
370  if (window.get())
371  {
372    i4_kernel.delete_handler(window.get());
373    window=0;
374  }
375}
376
377li_object *g1_edit_selected(li_object *o, li_environment *env)
378{
379  if (g1_edit_state.current_focus())
380  {
381    if (g1_edit_state.current_focus()->get_mode())
382      g1_edit_state.current_focus()->get_mode()->edit_selected();
383  }
384 
385  return 0;
386 
387}
388
389li_automatic_add_function (g1_edit_selected, "edit_selected");
Note: See TracBrowser for help on using the repository browser.