SO3Engine
Hikari.cpp
Go to the documentation of this file.
1/*
2 This file is part of Hikari, a library that allows developers
3 to use Flash in their Ogre3D applications.
4
5 Copyright (C) 2008 Adam J. Simmons
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library 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 GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21/****************************************************************************************************
22 ___ ___ .__ __ .__
23 / | \|__| | _______ _______|__|
24 / ~ \ | |/ /\__ \\_ __ \ |
25 \ Y / | < / __ \| | \/ |
26 \___|_ /|__|__|_ \‍(____ /__| |__| v0.4
27 \/ \/ \/
28
29
30 * Zed Games PC Development Team - Jaime Crespillo Vilchez (jcrespillo@zed.com)
31 * Build: 0.1 - Date: 13/10/2008
32 * Undocked version of Hikari Lib
33 * Brief: Removed createFlashOverlay methods, added setFlashControl, new interface class IFlashControl
34 ******************************************************************************************************/
35
38#include <objbase.h>
39#include <direct.h>
40#include <stdlib.h>
41#include <string>
42#include <vector>
43#include <windows.h>
44
45using namespace Hikari;
46//Singleton instance
48
50// freak function to know our system path
52{
53 std::string workingDirectory = "";
54 char currentPath[_MAX_PATH];
55 _getcwd(currentPath, _MAX_PATH);
56 workingDirectory = currentPath;
57
58 return workingDirectory + "\\";
59}
60
61HikariManager::HikariManager() : focusedControl(0), mouseXPos(0), mouseYPos(0),
62mouseButtonRDown(false), flashLib(0), keyboardHook(0)
63{
64 if (mInstance)
65 return;
66
67 HRESULT r = CoInitializeEx(NULL, COINIT_MULTITHREADED);
68 //HRESULT r = CoInitialize(0);
69
70 OleInitialize(0);
71
72 // Attempt to load 'Flash.ocx', it's alrite if this fails, we have a fallback strategy
73 std::string workingDirectory = getCurrentWorkingDirectory();
74
75 flashLib = LoadLibrary((workingDirectory + "Flash.ocx").c_str());
76
78
80
81 mInstance = this;
82}
83
85{
86 /*************************************************************************************
87 * we could not delete IFlashControl objects in this layer of app to avoid destructor errors
88 * client app must do this job for each FlashControl. Just clear the objects Map.
89 ***************************************************************************************
90
91 for(ControlMap::iterator i = controls.begin(); i != controls.end();)
92 {
93 IFlashControl* toDelete = i->second;
94 i = controls.erase(i);
95 delete toDelete;
96 }
97 ***************************************************************************************/
99
100 if (keyboardHook)
101 delete keyboardHook;
102
103 /**************************************************************************************
104 * we can not get free flashLib cause Vision crash when unload shared dlls */
105 //if(flashLib) FreeLibrary(flashLib);
106 /***************************************************************************************/
107
108 CoUninitialize();
109
110 mInstance = 0;
111}
112
117
119{
120 if (!mInstance)
121 mInstance = new HikariManager();
122
123 return *mInstance;
124}
125
127{
128 if (!mInstance)
129 mInstance = new HikariManager();
130
131 return mInstance;
132}
133
134void HikariManager::setPath(const std::string& assetsDirectory){
135
136 std::string workingDirectory = getCurrentWorkingDirectory();
137 this->basePath = workingDirectory + assetsDirectory;
138
139 if (this->basePath.at(this->basePath.length() - 1) != '\\')
140 this->basePath.push_back('\\');
141}
142
143
145{
146 if (controlToDestroy){
147 ControlMap::iterator iter = controls.find(controlToDestroy->name);
148 if (iter != controls.end())
149 iter = controls.erase(iter);
150
151 if (focusedControl == controlToDestroy)
152 focusedControl = 0;
153 }
154}
155
156void HikariManager::destroyFlashControl(const std::string& controlName)
157{
158 IFlashControl* control = getFlashControl(controlName);
159 if (control)
160 control->okayToDelete = true;
161}
162
164{
165 for (ControlMap::iterator iter = controls.begin(); iter != controls.end(); iter++)
166 iter->second->okayToDelete = true;
167
168 focusedControl = 0;
169}
170
172{
173 if (controls.size() > 0){
174 for (ControlMap::iterator iter = controls.begin(); iter != controls.end(); iter++){
175 if (iter->second)
176 iter->second = 0;
177 }
178 controls.clear();
179 focusedControl = 0;
180 }
181}
182
183IFlashControl* HikariManager::getFlashControl(const std::string& controlName) const
184{
185 ControlMap::const_iterator i = controls.find(controlName);
186 if (i != controls.end())
187 return i->second;
188
189 return 0;
190}
191
193{
194 for (ControlMap::iterator iter = controls.begin(); iter != controls.end();)
195 {
196 int dirtyWidth = iter->second->dirtyBounds.right - iter->second->dirtyBounds.left;
197 int dirtyHeight = iter->second->dirtyBounds.bottom - iter->second->dirtyBounds.top;
198
199 int dirtyBufSize = dirtyWidth * dirtyHeight * 4;
200 int actualBufferSize = 4 * (iter->second->dirtyBounds.right - iter->second->dirtyBounds.left);
201
202 if (iter->second->okayToDelete)
203 {
204 IFlashControl* controlToDelete = iter->second;
205 iter = controls.erase(iter);
206 if (focusedControl == controlToDelete)
207 focusedControl = 0;
208 //delete controlToDelete;
209 }
210 else
211 {
212
213 IFlashControl* i = iter->second;
214
215 if (i){
216
217 i->update();
218 iter++;
219 }
220 }
221 }
222}
223
225{
226 /*if( focusedControl )
227 return focusedControl;*/
228
229 return NULL;
230}
231
232
234{
235 //return !!focusedControl;
236 return false;
237}
238
240{
241 //focusedControl = 0;
242}
243
244bool HikariManager::injectMouseMove(short x, short y, int btn, bool ontex)
245{
246 bool eventHandled = false;
247
248 /*if(mouseButtonRDown && focusedControl)
249 {
250 if(focusedControl->mouseEnable && focusedControl->isDraggable)
251 {
252 focusedControl->move(x-mouseXPos, y-mouseYPos);
253 eventHandled = true;
254 }
255 }
256 for(ControlMap::iterator iter = controls.begin(); iter != controls.end(); iter++)
257 {
258 //$BB add test ontex to control ouside mouse events only on billboard
259 if(iter->second->isInputListener && iter->second->isVisible && iter->second->mouseEnable && ((!iter->second->isOverlay && ontex) || (iter->second->isOverlay && !ontex)))
260 {
261 iter->second->injectMouseMove(iter->second->getRelativeX(x), iter->second->getRelativeY(y),btn);
262
263 if(!eventHandled)
264 if(iter->second->isPointOverMe(x, y))
265 eventHandled = true;
266 }
267 }
268 mouseXPos = x;
269 mouseYPos = y;*/
270 return eventHandled;
271}
272
274{
275 bool eventHandled = false;
276 /*for(ControlMap::iterator iter = controls.begin(); iter != controls.end(); iter++)
277 {
278 if(iter->second->isInputListener && iter->second->isVisible)
279 {
280 if(iter->second->isPointOverMe(x, y))
281 {
282 eventHandled = true;
283 }
284
285 }
286 }*/
287 return eventHandled;
288}
289
291{
292 /*if(buttonID == LeftMouseButton)
293 {
294 if(focusControl(mouseXPos, mouseYPos))
295 {
296 if(!focusedControl->mouseEnable)
297 return false;
298 int relX = focusedControl->getRelativeX(mouseXPos);
299 int relY = focusedControl->getRelativeY(mouseYPos);
300
301 focusedControl->injectMouseDown(relX, relY,buttonID);
302 }
303 }
304 else if(buttonID == RightMouseButton)
305 {
306 if(!focusedControl->mouseEnable)
307 return false;
308 if(focusControl(mouseXPos, mouseYPos))
309 {
310 int relX = focusedControl->getRelativeX(mouseXPos);
311 int relY = focusedControl->getRelativeY(mouseYPos);
312
313 focusedControl->injectMouseDown(relX, relY,buttonID);
314 }
315 mouseButtonRDown = true;
316 focusControl(mouseXPos, mouseYPos);
317 }
318 else if(buttonID == MiddleMouseButton)
319 {
320 if(!focusedControl->mouseEnable)
321 return false;
322 if(focusControl(mouseXPos, mouseYPos))
323 {
324 int relX = focusedControl->getRelativeX(mouseXPos);
325 int relY = focusedControl->getRelativeY(mouseYPos);
326
327 focusedControl->injectMouseDown(relX, relY,buttonID);
328 }
329 }
330
331 if(focusedControl)
332 return true;*/
333
334 return false;
335}
336
338{
339 /*if(buttonID == LeftMouseButton && focusedControl)
340 {
341 if(focusedControl)
342 {
343 if(!focusedControl->mouseEnable)
344 return false;
345 focusedControl->injectMouseUp(focusedControl->getRelativeX(mouseXPos), focusedControl->getRelativeY(mouseYPos),buttonID);
346 }
347 }
348 else if(buttonID == RightMouseButton)
349 {
350 if(focusedControl)
351 {
352 if(!focusedControl->mouseEnable)
353 return false;
354 focusedControl->injectMouseUp(focusedControl->getRelativeX(mouseXPos), focusedControl->getRelativeY(mouseYPos),buttonID);
355 }
356 mouseButtonRDown = false;
357 }
358 else if(buttonID == MiddleMouseButton)
359 {
360 if(focusedControl)
361 {
362 if(!focusedControl->mouseEnable)
363 return false;
364 focusedControl->injectMouseUp(focusedControl->getRelativeX(mouseXPos), focusedControl->getRelativeY(mouseYPos),buttonID);
365 }
366 }
367
368 if(focusedControl)
369 return true;*/
370
371 return false;
372}
373
375{
376 /*if(focusedControl)
377 {
378 if(focusedControl->isPointOverMe(mouseXPos, mouseYPos)&& focusedControl->mouseEnable)
379 {
380 focusedControl->injectMouseWheel(relScroll, focusedControl->getRelativeX(mouseXPos), focusedControl->getRelativeY(mouseYPos));
381 return true;
382 }
383 }*/
384
385 return false;
386}
387
388void HikariManager::injectKeyEvent(UINT msg, WPARAM wParam, LPARAM lParam)
389{
390 /*if(focusedControl && !focusedControl->isBlocked && focusedControl->keyBoardEnable)
391 {
392 mInjectingKeyboardToFlash = true;
393 focusedControl->handleKeyEvent(msg, wParam, lParam);
394 }
395 else
396 {
397 mInjectingKeyboardToFlash = false;
398 }*/
399}
400
402{
403 if (isEnabled)
404 {
405 if (!keyboardHook)
407 }
408 else
409 {
410 if (keyboardHook)
411 {
412 delete keyboardHook;
413 keyboardHook = 0;
414 }
415 }
416}
417
418bool HikariManager::focusControl(int x, int y, IFlashControl* selection)
419{
420 /*defocusAll();
421 if(selection)
422 {
423 if(!selection)
424 {
425 focusedControl = selection;
426 return true;
427 }
428 }
429
430 IFlashControl* controlToFocus = selection? selection : getTopControl(x, y);
431
432 if(!controlToFocus)
433 return false;
435 if(!controlToFocus->mouseEnable)
436 return false;
437 if(!controlToFocus->isOverlay)
438 {
439 focusedControl = controlToFocus;
440 return true;
441 }
442 if(!controlToFocus->topOnFocus)
443 {
444 focusedControl = controlToFocus;
445 return true;
446 }
447
448 // Recalculate the Z Order
449 std::vector<IFlashControl*> sortedControls;
450 std::map<std::string, IFlashControl*>::iterator iter;
451 for(iter = controls.begin(); iter != controls.end(); iter++)
452 if(iter->second)
453 sortedControls.push_back(iter->second);
454 struct compare { bool operator()(IFlashControl* a, IFlashControl* b){ return(a->getZOrder() > b->getZOrder()); }};
455 std::sort(sortedControls.begin(), sortedControls.end(), compare());
456 if(sortedControls.size())
457 {
458 if(sortedControls.at(0) != controlToFocus)
459 {
460 unsigned int popIdx = 0;
461 for(; popIdx < sortedControls.size(); popIdx++)
462 if(sortedControls.at(popIdx) == controlToFocus)
463 break;
464
465 unsigned short highestZ = sortedControls.at(0)->getZOrder();
466 int fgidx = 0;
467 if(!sortedControls.at(popIdx)->Foreground)
468 {
469 for(unsigned int i = 0; i < sortedControls.size(); i++)
470 {
471 if(!sortedControls.at(i)->Foreground)
472 {
473 highestZ = sortedControls.at(i)->getZOrder();
474 fgidx = i;
475 break;
476 }
477 }
478 }
479 for(fgidx; fgidx < popIdx; fgidx++)
480 {
481 sortedControls.at(fgidx)->setZOrder(sortedControls.at(fgidx+1)->getZOrder());
482 }
483 sortedControls.at(popIdx)->setZOrder(highestZ);
484 }
485 }
486 for(unsigned int i = 0; i < sortedControls.size(); i++) sortedControls.at(i)->currentZOrder = sortedControls.at(i)->getZOrder();
487
488 focusedControl = controlToFocus;*/
489
490 return true;
491}
492
494{
495 IFlashControl* top = 0;
496
497 /*std::map<std::string, IFlashControl*>::iterator iter;
498 for(iter = controls.begin(); iter != controls.end(); iter++)
499 {
500 if(!iter->second->isPointOverMe(x, y))
501 continue;
502
503 if(!top)
504 top = iter->second;
505 else
506 top = top->getZOrder() > iter->second->getZOrder() ? top : iter->second;
507 }*/
508
509 return top;
510}
511
513// this is the SETTER method to inject FlashControls to hikari
515{
516 if (controls.find(flashControl->name) == controls.end())
517 controls[flashControl->name] = flashControl;
518}
unsigned int UINT
Definition SO3Android.h:58
IFlashControl * getFlashControl(const std::string &controlName) const
Definition Hikari.cpp:183
std::string getCurrentWorkingDirectory()
Definition Hikari.cpp:51
void setFlashControl(IFlashControl *flashControl)
Definition Hikari.cpp:514
static HikariManager & getSingleton()
Definition Hikari.cpp:118
void destroyFlashControl(IFlashControl *controlToDestroy)
Definition Hikari.cpp:144
std::string basePath
Definition Hikari.h:257
IFlashControl * focusedControl
Definition Hikari.h:262
bool isPointOverHikari(short x, short y)
Definition Hikari.cpp:273
Impl::KeyboardHook * keyboardHook
Definition Hikari.h:276
ControlMap controls
Definition Hikari.h:264
static HikariManager * getSingletonPtr()
Definition Hikari.cpp:126
void setKeyboardHookEnabled(bool isEnabled)
Definition Hikari.cpp:401
bool focusControl(int x, int y, IFlashControl *selection=0)
Definition Hikari.cpp:418
static HikariManager * mInstance
Definition Hikari.h:269
void injectKeyEvent(UINT msg, WPARAM wParam, LPARAM lParam)
Definition Hikari.cpp:388
bool mInjectingKeyboardToFlash
Definition Hikari.h:282
void setPath(const std::string &assetsDirectory="")
Definition Hikari.cpp:134
IFlashControl * getFocusedControl()
Definition Hikari.cpp:224
bool injectMouseWheel(int relScroll)
Definition Hikari.cpp:374
bool injectMouseDown(int buttonID)
Definition Hikari.cpp:290
bool injectMouseUp(int buttonID)
Definition Hikari.cpp:337
bool injectMouseMove(short x, short y, int btn, bool ontex=true)
Definition Hikari.cpp:244
IFlashControl * getTopControl(int x, int y)
Definition Hikari.cpp:493
virtual void update()=0