Debian Codegraph @

src/gengine/InputAgent.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "InputAgent.h"
10
11 #include "KeyBinder.h"
12 #include "InputHandler.h"
13
14 #include "MessagerAgent.h"
15 #include "SimpleMsg.h"
16 #include "UnknownMsgException.h"
17 #include "Name.h"
18 #include "MouseStroke.h"
19
20 #include "SDL.h"
21
22 //-----------------------------------------------------------------
23 /**
24 * Enable SDL_UNICODE.
25 * Enable key repeat.
26 * Set console handler to ScriptAgent.
27 *
28 * NOTE: every SDL_InitSubSystem will disable UNICODE
29 * hence InputAgent init must be after VideoAgent and SoundAgent.
30 * NOTE: KeyConsole() use Path which asks OptionAgent
31 */
32 void
33 InputAgent::own_init()
34 {
35 m_keyBinder = new KeyBinder();
36 m_handler = NULL;
37 m_keys = SDL_GetKeyState(NULL);
38
39 SDL_EnableUNICODE(1);
40 }
41 //-----------------------------------------------------------------
42 void
43 InputAgent::own_update()
44 {
45 SDL_Event event;
46 while (SDL_PollEvent(&event)) {
47 switch (event.type) {
48 case SDL_QUIT:
49 {
50 BaseMsg *msg = new SimpleMsg(Name::APP_NAME, "quit");
51 MessagerAgent::agent()->forwardNewMsg(msg);
52 break;
53 }
54 case SDL_KEYDOWN:
55 m_keyBinder->keyDown(event.key.keysym);
56 if (m_handler) {
57 m_handler->keyEvent(KeyStroke(event.key.keysym));
58 }
59 break;
60 case SDL_KEYUP:
61 if (m_handler) {
62 m_handler->keyUp(KeyStroke(event.key.keysym));
63 }
64 break;
65 case SDL_MOUSEBUTTONDOWN:
66 if (m_handler) {
67 m_handler->mouseEvent(MouseStroke(event.button));
68 }
69 break;
70 default:
71 break;
72 }
73 }
74
75 if (m_handler) {
76 Uint8 buttons;
77 V2 mouseLoc = getMouseState(&buttons);
78 m_handler->mouseState(mouseLoc, buttons);
79 }
80 }
81 //-----------------------------------------------------------------
82 /**
83 * Delete console.
84 */
85 void
86 InputAgent::own_shutdown()
87 {
88 delete m_keyBinder;
89 }
90 //-----------------------------------------------------------------
91 void
92 InputAgent::installHandler(InputHandler *handler)
93 {
94 if (m_handler) {
95 m_handler->takePressed(NULL);
96 m_handler->mouseState(V2(-1, -1), 0);
97 }
98 m_handler = handler;
99 if (m_handler) {
100 m_handler->takePressed(m_keys);
101 Uint8 buttons;
102 V2 mouseLoc = getMouseState(&buttons);
103 m_handler->mouseState(mouseLoc, buttons);
104 }
105 }
106 //-----------------------------------------------------------------
107 /**
108 * Return mouse location.
109 * @param out_buttons place where to store state of buttons
110 * @return (mouseX, mouseY)
111 */
112 V2
113 InputAgent::getMouseState(Uint8 *out_buttons)
114 {
115 int x;
116 int y;
117 Uint8 pressed = SDL_GetMouseState(&x, &y);
118 if (out_buttons) {
119 *out_buttons = pressed;
120 }
121 return V2(x, y);
122 }
123
124