1 /* -*- c++ -*-
2 FILE: EventHandler.h
3 RCS REVISION: $Revision: 1.25 $
4 
5 COPYRIGHT: (c) 1999 -- 2003 Melinda Green, Don Hatch, and Jay Berkenbilt - Superliminal Software
6 
7 LICENSE: Free to use and modify for non-commercial purposes as long as the
8     following conditions are adhered to:
9     1) Obvious credit for the source of this code and the designs it embodies
10        are clearly made, and
11     2) Ports and derived versions of 4D Magic Cube programs are not distributed
12        without the express written permission of the authors.
13 
14 DESCRIPTION:
15     Generic EventHandler class.  This class is designed to be
16     machine-dependent.  New ports of MagicCube4d can derive new
17     Machine and Widgets classes to use this.  There are a few issues
18     with this collection of interfaces, however:
19 */
20 
21 ////  1.  Only the UNIX code uses it right now.
22 ////
23 ////  2.  Some of the macro support lives in the X Machine and Widget
24 ////      classes and would not exist in a port based soley on the current
25 ////      interfaces.  (For example, the fast automoves button is created in
26 ////      MachineX directly.)
27 
28 #ifndef EVENTHANDLER_H
29 #define EVENTHANDLER_H
30 
31 #include <stdio.h>
32 #include "MagicCube.h"
33 #include "Preferences.h"
34 
35 class Machine;
36 class Widgets;
37 class PolygonManager4D;
38 class PuzzleState;
39 class History;
40 class MacroManager;
41 
42 class EventHandler
43 {
44 public:
45     typedef void (EventHandler::*callback_fn)(void*);
46 
47     enum event_type_e { EXPOSE, RESIZE, BUTTONDOWN, BUTTONUP,
48                         KEYPRESS, KEYRELEASE, DRAG };
49     enum button_e { LEFTBUTTON, MIDDLEBUTTON, RIGHTBUTTON };
50 
51     // Event should really be its own class in its own file, but it's
52     // not objectified enough to warrant this.  Better to hide it in
53     // here.
54     class Event
55     {
56     public:
57         event_type_e type;
58         int x;
59         int y;
60         int w;
61         int h;
62         // dx, dy are deltas for drag event
63         int dx;
64         int dy;
65         button_e button;
66         int key;
67         bool shift_is_down;
68         bool control_is_down;
69     };
70 
71     class Callback
72     {
73     public:
Callback(EventHandler * e,callback_fn cb,void * arg)74         Callback(EventHandler* e, callback_fn cb, void *arg) :
75             event_handler(e), callback(cb), arg(arg)
76         {
77         };
apply()78         void apply()
79         {
80             (event_handler->*callback)(arg);
81         }
82     private:
83         EventHandler* event_handler;
84         callback_fn callback;
85         void *arg;
86     };
87 
88     EventHandler(int argc, char** argv, char const* machine_type);
89     ~EventHandler();
90     Callback* createCallback(callback_fn, void*);
91     void run();
92     void undo_cb(void*);
93     void redo_cb(void*);
94     void add_cb(void*);
95     void delete_cb(void*);
96     void toggleFast_cb(void*);
97     void scramble_cb(void *);
98     void cheat_cb(void *);
99     void reset_cb(void *);
100     void save_cb(void *);
101     void quit_cb(void *);
102     void print_cb(void *);
103     void newPuzzle_cb(void *);
104     void applyMacro_cb(void *);
105     void invertMacro_cb(void *);
106     void expose_handler(Event*, void *);
107     void buttonDown_handler(Event *event, void *);
108     void keyPress_handler(Event *event, void *);
109     void keyRelease_handler(Event *event, void *);
110     void drag_handler(Event *event, void *);
111 
112 private:
113     // Prohibit copying and assignment
114     EventHandler(EventHandler const&);
115     EventHandler& operator=(EventHandler const&);
116 
117     void readLogfile(char *filename);
118     void dump(FILE *fp);
119 
120     // Return true on success
121     bool writeLogfile(char *filename);
122 
123     void showAnimation(struct stickerspec *grip, int dir, int slicesmask);
124 
125     void getAReferenceSticker(Event* event, void *);
126 
127     // Button callback functions...
128     void cancelAdd_cb(void *arg = 0);
129     void applyMacroCBAfterGotRefStickers(void *arg = 0);
130     void cancelApplyMacro_cb(void *);
131 
132     int  macro_which;
133     int  macro_invert;
134     static int const nrefs = 3;
135     int  refs[3][4];  // FIX THIS-- this is getting really hacky
136 
137     void applyMacroCommon(void *w, bool invert);
138     void doneAdd_cb(void *);
139     void addCBAfterGotRefStickers(void *arg = 0);
140     void cancelDelete_cb(void *);
141 
142     Preferences preferences;
143 
144     Machine* machine;
145     Widgets* widgets;
146     PolygonManager4D* polymgr;
147     PuzzleState* puzzle_state;
148     History* history;
149     MacroManager* macromgr;
150 
151     // If deleting_a_macro is true, then hitting the apply button
152     // causes that macro to be deleted.  If creating_a_macro is true,
153     // then undos and redos are not allowed
154     bool creating_a_macro;
155     bool deleting_a_macro;
156 
157     // Support being able to set slicesmask on next twist.
158     unsigned int next_slicesmask;
159 
160     // Whether applying macros should display each move
161     bool fast_automoves;
162 
163     // In certain cases the user is asked to click on three reference
164     // stickers.  In this case the following variables are used...
165 
166     int  number_of_reference_stickers_needed;
167     int  (*reference_stickers_needed)[4];
168     void (EventHandler::*what_to_do_after_got_reference_stickers) (void *arg);
169     void* cur_ui_data;
170 
171     bool quick_mode;
172     struct frame untwisted_frame;
173     bool dragging;
174     int nscramblechen;
175 
176     static float const DEF_TWIST_INCREMENT;
177 };
178 
179 
180 #endif
181 
182 // Local Variables:
183 // c-basic-offset: 4
184 // c-comment-only-line-offset: 0
185 // c-file-offsets: ((defun-block-intro . +) (block-open . 0) (substatement-open . 0) (statement-cont . +) (statement-case-open . +4) (arglist-intro . +) (arglist-close . +) (inline-open . 0))
186 // indent-tabs-mode: nil
187 // End:
188