Mercurial > fife-parpg
annotate engine/extensions/pychan/compat.py @ 222:c050f257cec3
PyChan: Now Widgets may _in principle_ receive key events.
author | phoku@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Thu, 19 Mar 2009 20:14:49 +0000 |
parents | 6214a0b91eb2 |
children | 48c99636453e |
rev | line source |
---|---|
206
6214a0b91eb2
Fix: pychan/compat.py should not depend on pythonize.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
205
diff
changeset
|
1 # -*- coding: utf-8 -*- |
205
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
2 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
3 in_fife = None |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
4 guichan = None |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
5 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
6 def _import_guichan(): |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
7 global in_fife |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
8 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
9 err_fife = "" |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
10 try: |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
11 import fife |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
12 in_fife = True |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
13 return fife |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
14 except ImportError, e: |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
15 err_fife = str(e) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
16 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
17 try: |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
18 import guichan |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
19 in_fife = False |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
20 return guichan |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
21 except ImportError, e: |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
22 import traceback |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
23 traceback.print_exc() |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
24 raise ImportError("Couldn't import neither fife nor guichan: fife:'%s' guichan:'%s'" % (err_fife,str(e))) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
25 guichan = _import_guichan() |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
26 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
27 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
28 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
29 def _munge_engine_hook(engine): |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
30 engine.translate_mouse_event = getattr(engine,'translate_mouse_event',lambda x : x ) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
31 engine.translate_key_event = getattr(engine,'translate_key_event',lambda x : x ) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
32 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
33 if not in_fife: |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
34 return engine |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
35 if not isinstance(engine,fife.Engine): |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
36 return engine |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
37 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
38 guimanager = engine.getGuiManager() |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
39 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
40 def _fife_load_image(filename): |
206
6214a0b91eb2
Fix: pychan/compat.py should not depend on pythonize.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
205
diff
changeset
|
41 index = engine.getImagePool().addResourceFromFile(filename) |
205
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
42 return guichan.GuiImage(index,engine.getImagePool()) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
43 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
44 class hook: |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
45 pass |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
46 hook = hook() |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
47 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
48 hook.add_widget = guimanager.add |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
49 hook.remove_widget = guimanager.remove |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
50 hook.default_font = engine.getDefaultFont() |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
51 hook.load_image = _fife_load_image |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
52 hook.translate_mouse_event = guimanager.translateMouseEvent |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
53 hook.translate_key_event = guimanager.translateKeyEvent |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
54 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
55 hook.screen_width = engine.getRenderBackend().getScreenWidth() |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
56 hook.screen_height = engine.getRenderBackend().getScreenHeight() |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
57 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
58 hook.engine = engine |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
59 return hook |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
60 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
61 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
62 class _multilistener(guichan.ActionListener,guichan.MouseListener,guichan.KeyListener): |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
63 def __init__(self): |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
64 guichan.ActionListener.__init__(self) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
65 guichan.MouseListener.__init__(self) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
66 guichan.KeyListener.__init__(self) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
67 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
68 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
69 class _point(object): |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
70 def __init__(self,x=0,y=0): |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
71 self.x=0 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
72 self.y=0 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
73 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
74 if in_fife: |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
75 fife = guichan |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
76 guichan.ActionListener._ActionListener_init__ = lambda x : x |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
77 #guichan.MouseListener.__init__ = lambda x : x |
222
c050f257cec3
PyChan: Now Widgets may _in principle_ receive key events.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
206
diff
changeset
|
78 #guichan.KeyListener.__init__ = lambda x : x |
205
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
79 else: |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
80 guichan.Point = _point |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
81 guichan.ScrollArea.SHOW_AUTO = guichan.ScrollArea.ShowAuto |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
82 guichan.ScrollArea.SHOW_NEVER = guichan.ScrollArea.ShowNever |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
83 guichan.ScrollArea.SHOW_ALWAYS = guichan.ScrollArea.ShowAlways |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
84 |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
85 assert isinstance(_multilistener(),guichan.ActionListener) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
86 assert isinstance(_multilistener(),guichan.MouseListener) |
54bfd1015b35
* PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
87 assert isinstance(_multilistener(),guichan.KeyListener) |