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 #ifndef FlashControl_H_
23 #define FlashControl_H_
24 
25 #include <windows.h>
26 #include "FlashValue.h"
27 #include <MyGUI.h>
28 
29 namespace ShockwaveFlashObjects
30 {
31 	struct IShockwaveFlash;
32 }
33 struct IOleObject;
34 class IOleInPlaceObjectWindowless;
35 
36 namespace Hikari
37 {
38 
39 	class FlashSite;
40 	class FlashHandler;
41 	class RenderBuffer;
42 
43 	/**
44 	* Used by FlashControl::setQuality, defines the Flash rendering quality.
45 	*
46 	* <ul>
47 	* <li>RQ_LOW - Favors playback speed over appearance and never uses anti-aliasing.
48 	* <li>RQ_MEDIUM - Applies some anti-aliasing and does not smooth bitmaps. It produces a better quality than the Low setting, but lower quality than the High setting.
49 	* <li>RQ_HIGH - Favors appearance over playback speed and always applies anti-aliasing. If the movie does not contain animation, bitmaps are smoothed; if the movie has animation, bitmaps are not smoothed.
50 	* <li>RQ_BEST - Provides the best display quality and does not consider playback speed. All output is anti-aliased and all bitmaps are smoothed.
51 	* <li>RQ_AUTOLOW - Emphasizes speed at first but improves appearance whenever possible. Playback begins with anti-aliasing turned off. If the Flash Player detects that the processor can handle it, anti-aliasing is turned on.
52 	* <li>RQ_AUTOHIGH - Emphasizes playback speed and appearance equally at first but sacrifices appearance for playback speed if necessary. Playback begins with anti-aliasing turned on. If the actual frame rate drops below the specified frame rate, anti-aliasing is turned off to improve playback speed.
53 	* </ul>
54 	*/
55 	enum RenderQuality
56 	{
57 		RQ_LOW,
58 		RQ_MEDIUM,
59 		RQ_HIGH,
60 		RQ_BEST,
61 		RQ_AUTOLOW,
62 		RQ_AUTOHIGH
63 	};
64 
65 	/**
66 	* Used by FlashControl::setScaleMode, defines the scaling mode to use when the aspect ratio of the control does not match that of the movie.
67 	*
68 	* <ul>
69 	* <li>SM_SHOWALL - Preserves the movie's aspect ratio by adding borders. (Default)
70 	* <li>SM_NOBORDER - Preserves the movie's aspect ratio by cropping the sides.
71 	* <li>SM_EXACTFIT - Does not preserve the movie's aspect ratio, scales the movie to the dimensions of the control.
72 	*/
73 	enum ScaleMode
74 	{
75 		SM_SHOWALL,
76 		SM_NOBORDER,
77 		SM_EXACTFIT
78 	};
79 
80 	class HikariWidget;
81 	typedef MyGUI::delegates::CDelegate3<HikariWidget*, const Arguments&, FlashValue&> FlashDelegate;
82 
83 	/**
84 	* The FlashControl class is an instance of the Flash Player that is rendered dynamically to a texture and
85 	* wrapped in an optional movable overlay using the Ogre3D engine.
86 	*/
87 	class FlashControl
88 	{
89 		friend class FlashSite;
90 		friend class FlashHandler;
91 
92 	public:
93 		FlashControl(HikariWidget* _owner, HMODULE _lib);
94 		~FlashControl();
95 
96 		/**
97 		* Loads a movie (a .swf file) into this FlashControl and begins playing.
98 		*
99 		* @param	movieFilename	The filename of the movie to load.
100 		*
101 		* @note	The specified movie should reside in the "assetsDirectory" that
102 		*		was declared when the HikariManager was instantiated.
103 		*/
104 		void load(const std::string& movieFilename);
105 
106 		/**
107 		* Sets whether or not the currently-loaded movie should use a
108 		* transparent background instead of the default background-color.
109 		*
110 		* @param	isTransparent	Whether or not the movie should use "transparent" rendering.
111 		* @param	useAlphaHack	With some Flash versions, there are certain glitches with
112 		*							transparent rendering (usually with text and aliased geometry).
113 		*							Set this parameter to 'true' to use an alternative alpha-rendering
114 		*							hack that may mitigate these issues at the cost of some performance.
115 		*/
116 		void setTransparent(bool isTransparent);
117 
118 		/**
119 		* Sets the Flash rendering quality for the currently-loaded movie.
120 		*
121 		* @param	renderQuality	The RenderQuality to use.
122 		*/
123 		void setQuality(short renderQuality);
124 
125 		/**
126 		* Sets the scaling mode to use when the aspect ratio of the movie and control do not match.
127 		*
128 		* @param	scaleMode	The ScaleMode to use.
129 		*/
130 		void setScaleMode(short scaleMode);
131 
132 		/**
133 		* Injects a mouse-move event into this FlashControl (in the control's local coordinate-space).
134 		*
135 		* @param	xPos	The local X-coordinate.
136 		* @param	yPos	The local Y-coordinate.
137 		*/
138 		void injectMouseMove(int xPos, int yPos);
139 
140 		/**
141 		* Injects a mouse-down event into this FlashControl (in the control's local coordinate-space).
142 		*
143 		* @param	xPos	The local X-coordinate.
144 		* @param	yPos	The local Y-coordinate.
145 		*/
146 		void injectMouseDown(int xPos, int yPos);
147 
148 		/**
149 		* Injects a mouse-up event into this FlashControl (in the control's local coordinate-space).
150 		*
151 		* @param	xPos	The local X-coordinate.
152 		* @param	yPos	The local Y-coordinate.
153 		*/
154 		void injectMouseUp(int xPos, int yPos);
155 
156 		/**
157 		* Injects a mouse-wheel event into this FlashControl (in the control's local coordinate-space).
158 		*
159 		* @param	relScroll	The relative scroll amount of the mouse-wheel.
160 		* @param	xPos	The local X-coordinate of the mouse.
161 		* @param	yPos	The local Y-coordinate of the mouse.
162 		*/
163 		void injectMouseWheel(int relScroll, int xPos, int yPos);
164 
165 		/**
166 		* Binds a local callback to a certain function name so that your Flash movie can call the function
167 		* from ActionScript using ExternalInterface.call('functionName').
168 		*
169 		* @param	funcName	The name to bind this callback to.
170 		* @param	callback	The local function to call, see below for examples of declaring a FlashDelegate.
171 		*
172 		*	\code
173 		*	// Example declaration of a compatible function (static function):
174 		*	FlashValue myStaticFunction(FlashControl* caller, const Arguments& args)
175 		*	{
176 		*		// Handle the callback here
177 		*		return FLASH_VOID;
178 		*	}
179 		*
180 		*	// Example declaration of a compatible function (member function):
181 		*	FlashValue MyClass::myMemberFunction(FlashControl* caller, const Arguments& args)
182 		*	{
183 		*		// Handle the callback here
184 		*		return "Some return value!";
185 		*	}
186 		*
187 		*	// FlashDelegate (member function) instantiation:
188 		*	FlashDelegate callback(this, &MyClass::myMemberFunction); // within a class
189 		*	FlashDelegate callback2(pointerToClassInstance, &MyClass::myMemberFunction);
190 		*
191 		*	// FlashDelegate (static function) instantiation:
192 		*	FlashDelegate callback(&myStaticFunction);
193 		*	\endcode
194 		*/
195 		void bind(const MyGUI::UString& funcName, FlashDelegate::IDelegate* callback);
196 
197 		/**
198 		* Un-binds the specified callback.
199 		*
200 		* @param	funcName	The name that the callback was bound to.
201 		*/
202 		void unbind(const MyGUI::UString& funcName);
203 
204 		/**
205 		* Attempts to call a function declared as a callback in the ActionScript of the currently-loaded movie.
206 		*
207 		* @param	funcName	The name of the callback that was declared using 'ExternalInterface.addCallback(funcName, function)'
208 		*						in the ActionScript of the currently-loaded movie.
209 		* @param	args	The arguments to pass to the ActionScript function.
210 		*
211 		* @return	If the invocation was successful and the ActionScript function returned a value, returns a FlashValue with a non-null type.
212 		*
213 		* @note	It is highly recommended to use the 'Args(arg1)(arg2)(arg3)...' helper class to pass arguments.
214 		*/
215 
216 		FlashValue callFunction(MyGUI::UString funcName, const Arguments& args = Args());
217 
218 		void setSize(int _width, int _height);
219 
getWidth()220 		int getWidth() const
221 		{
222 			return width;
223 		}
getHeight()224 		int getHeight() const
225 		{
226 			return height;
227 		}
228 
229 		void invalidateTotally();
230 		void update();
231 
getFlashInterface()232 		ShockwaveFlashObjects::IShockwaveFlash* getFlashInterface()
233 		{
234 			return flashInterface;
235 		}
236 
237 		void handleKeyEvent(UINT msg, WPARAM wParam, LPARAM lParam);
238 
239 	protected:
240 		void createControl(HMODULE _lib);
241 		void handleFlashCall(const std::wstring& xmlString);
242 
243 	protected:
244 		FlashSite* site;
245 		FlashHandler* handler;
246 		ShockwaveFlashObjects::IShockwaveFlash* flashInterface;
247 		IOleObject*	oleObject;
248 		IOleInPlaceObjectWindowless* windowlessObject;
249 
250 		int width, height;
251 		int state;
252 		int comCount;
253 		HDC mainContext, altContext;
254 		HBITMAP mainBitmap, altBitmap;
255 		BYTE* mainBuffer, *altBuffer;
256 		RenderBuffer* renderBuffer;
257 		RECT dirtyBounds;
258 		bool isClean, isTotallyDirty;
259 		bool isTransparent;
260 		bool mIsInitialise;
261 
262 		HikariWidget* mOwner;
263 
264 		typedef std::map<MyGUI::UString, FlashDelegate> DelegateMap;
265 		typedef DelegateMap::iterator DelegateIter;
266 		DelegateMap delegateMap;
267 	};
268 
269 }
270 
271 #endif
272