Guest User

scummvm bada 2.0

a guest
Sep 13th, 2011
168
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ScummVM - Graphic Adventure Engine
  2. *
  3. * ScummVM is the legal property of its developers, whose names
  4. * are too numerous to list here. Please refer to the COPYRIGHT
  5. * file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. */
  22.  
  23. #include <FAppApplication.h>
  24.  
  25. #include "common/translation.h"
  26. #include "base/main.h"
  27.  
  28. #include "backends/platform/bada/form.h"
  29. #include "backends/platform/bada/system.h"
  30.  
  31. using namespace Osp::Base::Runtime;
  32. using namespace Osp::Base;
  33. using namespace Osp::Ui;
  34. using namespace Osp::Ui::Controls;
  35.  
  36. // number of volume levels
  37. #define LEVEL_RANGE 5
  38.  
  39. // round down small Y touch values to 1 to allow the
  40. // cursor to be positioned at the top of the screen
  41. #define MIN_TOUCH_Y 10
  42.  
  43. // block for up to 2.5 seconds during shutdown to
  44. // allow the game thread to exit gracefully.
  45. #define EXIT_SLEEP_STEP 10
  46. #define EXIT_SLEEP 250
  47.  
  48. //
  49. // BadaAppForm
  50. //
  51. BadaAppForm::BadaAppForm() :
  52. _gameThread(0),
  53. _ticks(0),
  54. _state(InitState),
  55. _shortcut(ShowKeypad){
  56. _eventQueueLock = new Mutex();
  57. _eventQueueLock->Create();
  58. }
  59.  
  60. result BadaAppForm::Construct() {
  61. result r = Form::Construct(Controls::FORM_STYLE_NORMAL);
  62. if (IsFailed(r)) {
  63. return r;
  64. }
  65.  
  66. BadaSystem *badaSystem = NULL;
  67. _gameThread = NULL;
  68.  
  69. badaSystem = new BadaSystem(this);
  70. r = badaSystem != NULL ? E_SUCCESS : E_OUT_OF_MEMORY;
  71.  
  72. if (!IsFailed(r)) {
  73. r = badaSystem->Construct();
  74. }
  75.  
  76. if (!IsFailed(r)) {
  77. _gameThread = new Thread();
  78. r = _gameThread != NULL ? E_SUCCESS : E_OUT_OF_MEMORY;
  79. }
  80.  
  81. if (!IsFailed(r)) {
  82. r = _gameThread->Construct(*this);
  83. }
  84.  
  85. if (IsFailed(r)) {
  86. if (badaSystem != NULL) {
  87. delete badaSystem;
  88. }
  89. if (_gameThread != NULL) {
  90. delete _gameThread;
  91. _gameThread = NULL;
  92. }
  93. } else {
  94. g_system = badaSystem;
  95. }
  96.  
  97. return r;
  98. }
  99.  
  100. BadaAppForm::~BadaAppForm() {
  101. logEntered();
  102.  
  103. if (_gameThread && _state != ErrorState) {
  104. terminate();
  105.  
  106. _gameThread->Stop();
  107. if (_state != ErrorState) {
  108. _gameThread->Join();
  109. }
  110.  
  111. delete _gameThread;
  112. _gameThread = NULL;
  113. }
  114.  
  115. if (_eventQueueLock) {
  116. delete _eventQueueLock;
  117. _eventQueueLock = NULL;
  118. }
  119.  
  120. logLeaving();
  121. }
  122.  
  123. //
  124. // abort the game thread
  125. //
  126. void BadaAppForm::terminate() {
  127. if (_state == ActiveState) {
  128. ((BadaSystem *)g_system)->setMute(true);
  129.  
  130. _eventQueueLock->Acquire();
  131.  
  132. Common::Event e;
  133. e.type = Common::EVENT_QUIT;
  134. _eventQueue.push(e);
  135. _state = ClosingState;
  136.  
  137. _eventQueueLock->Release();
  138.  
  139. // block while thread ends
  140. AppLog("waiting for shutdown");
  141. for (int i = 0; i < EXIT_SLEEP_STEP && _state == ClosingState; i++) {
  142. Thread::Sleep(EXIT_SLEEP);
  143. }
  144.  
  145. if (_state == ClosingState) {
  146. // failed to terminate - Join() will freeze
  147. _state = ErrorState;
  148. }
  149. }
  150. }
  151.  
  152. void BadaAppForm::exitSystem() {
  153. _state = ErrorState;
  154.  
  155. if (_gameThread) {
  156. _gameThread->Stop();
  157. delete _gameThread;
  158. _gameThread = NULL;
  159. }
  160. }
  161.  
  162. result BadaAppForm::OnInitializing(void) {
  163. logEntered();
  164.  
  165. result r=Osp::System::SystemInfo::GetValue(String("PlatformVersionMajor"),_badaVersion);
  166. if(IsFailed(r))
  167. _badaVersion=1;
  168. SetOrientation(ORIENTATION_LANDSCAPE);
  169. AddOrientationEventListener(*this);
  170. AddTouchEventListener(*this);
  171. AddKeyEventListener(*this);
  172.  
  173. // set focus to enable receiving key events
  174. SetFocusable(true);
  175. SetFocus();
  176.  
  177. return E_SUCCESS;
  178. }
  179.  
  180. result BadaAppForm::OnDraw(void) {
  181. logEntered();
  182.  
  183. if (g_system) {
  184. BadaSystem *system = (BadaSystem *)g_system;
  185. BadaGraphicsManager *graphics = system->getGraphics();
  186. if (graphics && graphics->isReady()) {
  187. g_system->updateScreen();
  188. }
  189. }
  190.  
  191. return E_SUCCESS;
  192. }
  193.  
  194. bool BadaAppForm::pollEvent(Common::Event &event) {
  195. bool result = false;
  196.  
  197. _eventQueueLock->Acquire();
  198. if (!_eventQueue.empty()) {
  199. event = _eventQueue.pop();
  200. result = true;
  201. }
  202. _eventQueueLock->Release();
  203.  
  204. return result;
  205. }
  206.  
  207. void BadaAppForm::pushEvent(Common::EventType type,
  208. const Point &currentPosition) {
  209. BadaSystem *system = (BadaSystem *)g_system;
  210. BadaGraphicsManager *graphics = system->getGraphics();
  211. if (graphics) {
  212. // graphics could be NULL at startup or when
  213. // displaying the system error screen
  214. Common::Event e;
  215. e.type = type;
  216. e.mouse.x = currentPosition.x;
  217. e.mouse.y = currentPosition.y > MIN_TOUCH_Y ? currentPosition.y : 1;
  218.  
  219. bool moved = graphics->moveMouse(e.mouse.x, e.mouse.y);
  220.  
  221. _eventQueueLock->Acquire();
  222.  
  223. if (moved && type != Common::EVENT_MOUSEMOVE) {
  224. Common::Event moveEvent;
  225. moveEvent.type = Common::EVENT_MOUSEMOVE;
  226. moveEvent.mouse = e.mouse;
  227. _eventQueue.push(moveEvent);
  228. }
  229.  
  230. _eventQueue.push(e);
  231. _eventQueueLock->Release();
  232. }
  233. }
  234.  
  235. void BadaAppForm::pushKey(Common::KeyCode keycode) {
  236. Common::Event e;
  237. e.synthetic = false;
  238. e.kbd.keycode = keycode;
  239. e.kbd.ascii = keycode;
  240. e.kbd.flags = 0;
  241.  
  242. _eventQueueLock->Acquire();
  243.  
  244. e.type = Common::EVENT_KEYDOWN;
  245. _eventQueue.push(e);
  246. e.type = Common::EVENT_KEYUP;
  247. _eventQueue.push(e);
  248.  
  249. _eventQueueLock->Release();
  250. }
  251.  
  252. void BadaAppForm::OnOrientationChanged(const Control &source,
  253. OrientationStatus orientationStatus) {
  254. logEntered();
  255. if (_state == InitState) {
  256. _state = ActiveState;
  257. _gameThread->Start();
  258. }
  259. }
  260.  
  261. Object *BadaAppForm::Run(void) {
  262. scummvm_main(0, 0);
  263.  
  264. if (_state == ActiveState) {
  265. Application::GetInstance()->SendUserEvent(USER_MESSAGE_EXIT, NULL);
  266. }
  267. _state = DoneState;
  268. return NULL;
  269. }
  270. void BadaAppForm::showShortcut(){
  271.  
  272. switch (_shortcut) {
  273. case EscapeKey:
  274. g_system->displayMessageOnOSD(_("Escape Key"));
  275. break;
  276.  
  277. case ShowKeypad:
  278. g_system->displayMessageOnOSD(_("Show Keypad"));
  279. break;
  280.  
  281. case GameMenu:
  282. g_system->displayMessageOnOSD(_("Game Menu"));
  283. break;
  284. }
  285.  
  286. }
  287. void BadaAppForm::setShortcut() {
  288. // cycle to the next shortcut
  289. switch (_shortcut) {
  290. case EscapeKey:
  291. g_system->displayMessageOnOSD(_("Game Menu"));
  292. _shortcut = GameMenu;
  293. break;
  294.  
  295. case GameMenu:
  296. g_system->displayMessageOnOSD(_("Show Keypad"));
  297. _shortcut = ShowKeypad;
  298. break;
  299.  
  300. case ShowKeypad:
  301. g_system->displayMessageOnOSD(_("Escape Key"));
  302. _shortcut = EscapeKey;
  303. break;
  304. }
  305. }
  306.  
  307. void BadaAppForm::setVolume(bool up, bool minMax) {
  308. if(_badaVersion>1)
  309. return;
  310. int level = ((BadaSystem *)g_system)->setVolume(up, minMax);
  311. if (level != -1) {
  312. char message[32];
  313. char ind[LEVEL_RANGE]; // 1..5 (0=off)
  314. int j = LEVEL_RANGE - 1; // 0..4
  315. for (int i = 1; i <= LEVEL_RANGE; i++) {
  316. ind[j--] = level >= i ? '|' : ' ';
  317. }
  318. snprintf(message, sizeof(message), "Volume: [ %c%c%c%c%c ]",
  319. ind[0], ind[1], ind[2], ind[3], ind[4]);
  320. g_system->displayMessageOnOSD(message);
  321. }
  322. }
  323.  
  324. void BadaAppForm::showKeypad() {
  325. // display the soft keyboard
  326. pushKey(Common::KEYCODE_F7);
  327. }
  328.  
  329. void BadaAppForm::OnTouchDoublePressed(const Control &source,
  330. const Point &currentPosition,
  331. const TouchEventInfo &touchInfo) {
  332. if(_buttonState==None){
  333. _buttonState=Right;
  334. pushEvent(Common::EVENT_RBUTTONDOWN,
  335. currentPosition);
  336. }
  337. }
  338.  
  339. void BadaAppForm::OnTouchFocusIn(const Control &source,
  340. const Point &currentPosition,
  341. const TouchEventInfo &touchInfo) {
  342. }
  343.  
  344. void BadaAppForm::OnTouchFocusOut(const Control &source,
  345. const Point &currentPosition,
  346. const TouchEventInfo &touchInfo) {
  347. }
  348.  
  349. void BadaAppForm::OnTouchLongPressed(const Control &source,
  350. const Point &currentPosition,
  351. const TouchEventInfo &touchInfo) {
  352. pushKey(Common::KEYCODE_RETURN);
  353. }
  354.  
  355. void BadaAppForm::OnTouchMoved(const Control &source,
  356. const Point &currentPosition,
  357. const TouchEventInfo &touchInfo) {
  358. pushEvent(Common::EVENT_MOUSEMOVE, currentPosition);
  359. }
  360.  
  361. void BadaAppForm::OnTouchPressed(const Control &source,
  362. const Point &currentPosition,
  363. const TouchEventInfo &touchInfo) {
  364. _buttonState=Left;
  365. pushEvent(Common::EVENT_LBUTTONDOWN, currentPosition);
  366. }
  367.  
  368. void BadaAppForm::OnTouchReleased(const Control &source,
  369. const Point &currentPosition,
  370. const TouchEventInfo &touchInfo) {
  371. pushEvent(_buttonState == Left ? Common::EVENT_LBUTTONUP : Common::EVENT_RBUTTONUP,
  372. currentPosition);
  373. _buttonState=None;
  374. // flick to skip dialog
  375. if (touchInfo.IsFlicked()) {
  376. pushKey(Common::KEYCODE_PERIOD);
  377. }
  378. }
  379.  
  380. void BadaAppForm::OnKeyLongPressed(const Control &source, KeyCode keyCode) {
  381. logEntered();
  382. switch (keyCode) {
  383. case KEY_CAMERA:
  384. switch (_shortcut) {
  385. case EscapeKey:
  386. pushKey(Common::KEYCODE_ESCAPE);
  387. break;
  388.  
  389. case GameMenu:
  390. pushKey(Common::KEYCODE_F5);
  391. break;
  392.  
  393. case ShowKeypad:
  394. showKeypad();
  395. break;
  396.  
  397. default:
  398. break;
  399. }
  400. default:
  401. break;
  402. }
  403. }
  404.  
  405. void BadaAppForm::OnKeyPressed(const Control &source, KeyCode keyCode) {
  406. long long lTicks;
  407. Osp::System::SystemTime::GetTicks(lTicks);
  408. switch (keyCode) {
  409. case KEY_SIDE_UP:
  410. setVolume(true, false);
  411. return;
  412. case KEY_SIDE_DOWN:
  413. setVolume(false, false);
  414. return;
  415. case KEY_CAMERA:
  416. if(lTicks-_ticks<4000)
  417. setShortcut();
  418. else
  419. showShortcut();
  420. _ticks=lTicks;
  421. break;
  422. default:
  423. break;
  424. }
  425. }
  426.  
  427. void BadaAppForm::OnKeyReleased(const Control &source, KeyCode keyCode) {
  428. }
RAW Paste Data