Gnash  0.8.11dev
DefineButtonTag.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 //
19 
20 #ifndef GNASH_SWF_DEFINEBUTTONTAG_H
21 #define GNASH_SWF_DEFINEBUTTONTAG_H
22 
23 #include <vector>
24 #include <boost/ptr_container/ptr_vector.hpp>
25 #include <memory>
26 #include <cstdint>
27 #include <memory>
28 
29 #include "DefinitionTag.h"
30 #include "SWFMatrix.h"
31 #include "SWFCxForm.h"
32 #include "action_buffer.h"
33 #include "filter_factory.h"
34 #include "Filters.h"
35 #include "TypesParser.h"
36 #include "DefineButtonSoundTag.h"
37 #include "SWF.h"
38 #include "Button.h"
39 
40 // Forward declarations
41 namespace gnash {
42  class movie_definition;
43  class event_id;
44  class SWFStream;
45  class DisplayObject;
46 }
47 
48 namespace gnash {
49 namespace SWF {
50 
51 
54 {
55 
56 public:
57 
59  :
60  _blendMode(0),
61  _hitTest(false),
62  _down(false),
63  _over(false),
64  _up(false),
65  _definitionTag(nullptr),
66  _buttonLayer(0)
67  {
68  }
69 
70  ButtonRecord(ButtonRecord&&) = default;
71 
73  //
78  DisplayObject* instantiate(Button* button, bool name = true) const;
79 
81  //
85  bool hasState(Button::MouseState st) const;
86 
88  //
92  _cxform = readCxFormRGB(in);
93  }
94 
96  //
103  unsigned long endPos);
104 
106  //
109  bool valid() const {
110  return _definitionTag.get();
111  }
112 
113 private:
114 
117  //
119  Filters _filters;
120 
123  //
125  std::uint8_t _blendMode;
126 
127  bool _hitTest;
128  bool _down;
129  bool _over;
130  bool _up;
131 
132  // This is a ref-counted resource, so not owned by anyone.
133  boost::intrusive_ptr<const DefinitionTag> _definitionTag;
134 
135  int _buttonLayer;
136 
137  SWFMatrix _matrix;
138 
139  SWFCxForm _cxform;
140 
141 };
142 
145 {
146 public:
147 
148  // TODO: define ownership of list elements !!
150 
158  ButtonAction(SWFStream& in, TagType t, unsigned long endPos,
159  movie_definition& mdef);
160 
162  bool triggeredBy(const event_id& ev) const;
163 
165  bool triggeredByKeyPress() const {
166  return (_conditions & KEYPRESS);
167  }
168 
170  //
172  int getKeyCode() const {
173  return (_conditions & KEYPRESS) >> 9;
174  }
175 
176 private:
177 
178  enum Condition
179  {
180  IDLE_TO_OVER_UP = 1 << 0,
181  OVER_UP_TO_IDLE = 1 << 1,
182  OVER_UP_TO_OVER_DOWN = 1 << 2,
183  OVER_DOWN_TO_OVER_UP = 1 << 3,
184  OVER_DOWN_TO_OUT_DOWN = 1 << 4,
185  OUT_DOWN_TO_OVER_DOWN = 1 << 5,
186  OUT_DOWN_TO_IDLE = 1 << 6,
187  IDLE_TO_OVER_DOWN = 1 << 7,
188  OVER_DOWN_TO_IDLE = 1 << 8,
189  KEYPRESS = 0xFE00 // highest 7 bits
190  };
191 
192  std::uint16_t _conditions;
193 
194 };
195 
198 {
199 public:
200 
202  static void loader(SWFStream& in, TagType tag, movie_definition& m,
203  const RunResources& r);
204 
205  typedef std::vector<ButtonRecord> ButtonRecords;
206  typedef boost::ptr_vector<ButtonAction> ButtonActions;
207 
208  virtual ~DefineButtonTag();
209 
212  const;
213 
216  ButtonRecords& buttonRecords() { return _buttonRecords; }
217 
219  const ButtonRecords& buttonRecords() const { return _buttonRecords; }
220 
222  bool hasSound() const { return (_soundTag.get()); }
223 
226  void addSoundTag(std::unique_ptr<SWF::DefineButtonSoundTag> soundTag) {
227  // Do not replace a sound tag.
228  assert(!_soundTag.get());
229  _soundTag.reset(soundTag.release());
230  }
231 
233  //
236  const DefineButtonSoundTag::ButtonSound& buttonSound(size_t index) const {
237  assert(_soundTag.get());
238  return _soundTag->getSound(index);
239  }
240 
242  int getSWFVersion() const;
243 
245  bool trackAsMenu() const {
246  return _trackAsMenu;
247  }
248 
249  bool hasKeyPressHandler() const;
250 
252  //
255  template <class E>
256  void forEachTrigger(const event_id& ev, E& f) const {
257  for (size_t i = 0, e = _buttonActions.size(); i < e; ++i) {
258  const ButtonAction& ba = _buttonActions[i];
259  if (ba.triggeredBy(ev)) f(ba._actions);
260  }
261  }
262 
264  //
267  template<class E>
268  void visitKeyCodes(E& f) const {
269  std::for_each(_buttonActions.begin(), _buttonActions.end(),
270  std::bind(f, std::bind(
272  }
273 
274 private:
275 
277  friend class DefineButton2Tag;
278 
280  //
283  std::uint16_t id);
284 
286  void readDefineButtonTag(SWFStream& in, movie_definition& m);
287 
289  void readDefineButton2Tag(SWFStream& in, movie_definition& m);
290 
291  std::unique_ptr<SWF::DefineButtonSoundTag> _soundTag;
292 
293  ButtonRecords _buttonRecords;
294 
295  ButtonActions _buttonActions;
296 
298  bool _trackAsMenu;
299 
301  movie_definition& _movieDef;
302 };
303 
305 //
309 {
310 public:
312  static void loader(SWFStream& in, TagType tag, movie_definition& m,
313  const RunResources& r);
314 };
315 
316 }
317 } // end namespace gnash
318 
319 
320 #endif // GNASH_BUTTON_CHARACTER_DEF_H
321 
322 
323 // Local Variables:
324 // mode: C++
325 // c-basic-offset: 8
326 // tab-width: 8
327 // indent-tabs-mode: t
328 // End:
gnash::readSWFMatrix
SWFMatrix readSWFMatrix(SWFStream &in)
Read a SWFMatrix from input stream.
Definition: TypesParser.cpp:49
action_buffer.h
gnash::SWF::ButtonAction::getKeyCode
int getKeyCode() const
Return the keycode triggering this action.
Definition: DefineButtonTag.h:172
gnash::SWF::DefineButtonTag::ButtonRecords
std::vector< ButtonRecord > ButtonRecords
Definition: DefineButtonTag.h:205
TypesParser.h
gnash::SWF::DEFINEBUTTON
@ DEFINEBUTTON
Definition: SWF.h:39
name
std::string name
Definition: LocalConnection_as.cpp:149
gnash::log_swferror
void log_swferror(StringType msg, Args... args)
Definition: log.h:325
gnash::as_object
The base class for all ActionScript objects.
Definition: as_object.h:162
gnash::SWF::ButtonRecord::hasState
bool hasState(Button::MouseState st) const
Check if this ButtonRecord has a DisplayObject for a particular state.
Definition: DefineButtonTag.cpp:338
gnash::SWF::TagType
TagType
SWF tag types. Symbolic names copied from Ming.
Definition: SWF.h:31
gnash::SWF::DefineButton2Tag::loader
static void loader(SWFStream &in, TagType tag, movie_definition &m, const RunResources &r)
Load a DefineButton2 tag.
Definition: DefineButtonTag.cpp:93
DefineButtonSoundTag.h
gnash::DisplayObject::staticDepthOffset
static const int staticDepthOffset
Definition: DisplayObject.h:205
gnash::SWF::ButtonRecord::read
bool read(SWFStream &in, TagType t, movie_definition &m, unsigned long endPos)
Read a ButtonRecord from the SWF stream.
Definition: DefineButtonTag.cpp:351
gnash::SWF::DefinitionTag
Immutable data representing the definition of a movie display element.
Definition: DefinitionTag.h:48
Button.h
gnash::key::i
@ i
Definition: GnashKey.h:155
LOG_ONCE
#define LOG_ONCE(x)
Definition: log.h:49
gnash::typeName
std::string typeName(const T &inst)
Definition: utility.h:93
gnash::Button::MOUSESTATE_HIT
@ MOUSESTATE_HIT
Definition: Button.h:71
_
#define _(String)
Definition: log.h:44
Filters.h
gnash::SWF::DefineButtonTag::buttonSound
const DefineButtonSoundTag::ButtonSound & buttonSound(size_t index) const
Return one of the four sounds associated with this Button.
Definition: DefineButtonTag.h:236
gnash::event_id
A class to identify 'static' SWF events (system events).
Definition: event_id.h:53
gnash::SWF::ButtonAction::ButtonAction
ButtonAction(SWFStream &in, TagType t, unsigned long endPos, movie_definition &mdef)
Definition: DefineButtonTag.cpp:262
gnash::SWF::DefineButtonTag::hasKeyPressHandler
bool hasKeyPressHandler() const
Definition: DefineButtonTag.cpp:251
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::SWF::ButtonAction::_actions
action_buffer _actions
Definition: DefineButtonTag.h:149
gnash::event_id::id
EventCode id() const
Return the identifier for this event type.
Definition: event_id.h:141
gnash::event_id::ROLL_OVER
@ ROLL_OVER
Definition: event_id.h:65
gnash::DisplayObject::getNextUnnamedInstanceName
ObjectURI getNextUnnamedInstanceName()
Used to assign a name to unnamed instances.
Definition: DisplayObject.cpp:134
GnashKey.h
gnash::key::m
@ m
Definition: GnashKey.h:159
gnash::SWF::DefineButtonTag::~DefineButtonTag
virtual ~DefineButtonTag()
Definition: DefineButtonTag.cpp:70
gnash::SWF::DefineButtonTag::createDisplayObject
DisplayObject * createDisplayObject(Global_as &gl, DisplayObject *parent) const
Create a mutable instance of our definition.
Definition: DefineButtonTag.cpp:236
Global_as.h
gnash::SWFCxForm
Color transformation record.
Definition: SWFCxForm.h:35
gnash::SWF::DefineButtonTag
A class for parsing DefineButton and DefineButton2 tags.
Definition: DefineButtonTag.h:198
gnash::filter_factory::read
static int read(SWFStream &in, bool read_multiple, Filters *store)
Read one, possibly multiple filters from the stream, and push them into the vector store....
Definition: filter_factory.cpp:41
gnash::getGlobal
Global_as & getGlobal(const as_environment &env)
Definition: as_environment.cpp:651
gnash::SWF::ButtonAction::triggeredByKeyPress
bool triggeredByKeyPress() const
Return true if this action is triggered by a keypress.
Definition: DefineButtonTag.h:165
as_function.h
gnash::movie_definition::get_version
virtual int get_version() const =0
gnash::movie_definition
Client program's interface to the definition of a movie or sprite.
Definition: movie_definition.h:96
SWFCxForm.h
gnash::SWF::DefineButtonTag::hasSound
bool hasSound() const
Does this button have an associated DefineButtonSoundTag?
Definition: DefineButtonTag.h:222
gnash::Button::MOUSESTATE_OVER
@ MOUSESTATE_OVER
Definition: Button.h:70
gnash::SWF::DefineButtonTag::visitKeyCodes
void visitKeyCodes(E &f) const
Invoke a functor for each key code that should trigger an action.
Definition: DefineButtonTag.h:268
IF_VERBOSE_MALFORMED_SWF
#define IF_VERBOSE_MALFORMED_SWF(x)
Definition: log.h:404
gnash::key::t
@ t
Definition: GnashKey.h:166
gnash::event_id::DRAG_OVER
@ DRAG_OVER
Definition: event_id.h:67
gnash::key::r
@ r
Definition: GnashKey.h:164
gnash::event_id::ROLL_OUT
@ ROLL_OUT
Definition: event_id.h:66
gnash::SWF::DefineButtonSoundTag::ButtonSound
Definition: DefineButtonSoundTag.h:45
gnash::renderer::opengl::for_each
void for_each(C &container, R(T::*pmf)(const A &), const A &arg)
Definition: Renderer_ogl.cpp:690
gnash::SWF::DefineButtonTag::loader
static void loader(SWFStream &in, TagType tag, movie_definition &m, const RunResources &r)
Load a DefineButtonTag.
Definition: DefineButtonTag.cpp:76
DefineButtonTag.h
gnash::event_id::RELEASE
@ RELEASE
Definition: event_id.h:63
gnash::key::codeMap
const unsigned char codeMap[KEYCOUNT][TYPES]
Definition: GnashKey.h:337
gnash::SWF::DEFINEBUTTON2
@ DEFINEBUTTON2
Definition: SWF.h:64
GnashAlgorithm.h
gnash::SWFStream::tell
unsigned long tell()
Return our current (byte) position in the input stream.
Definition: SWFStream.cpp:447
gnash::action_buffer::read
void read(SWFStream &in, unsigned long endPos)
Read action bytes from input stream up to but not including endPos.
Definition: action_buffer.cpp:48
gnash::Filters
std::vector< std::unique_ptr< BitmapFilter > > Filters
Definition: filter_factory.h:32
gnash::SWF::DefineButtonTag::addSoundTag
void addSoundTag(std::unique_ptr< SWF::DefineButtonSoundTag > soundTag)
Definition: DefineButtonTag.h:226
gnash::key::_1
@ _1
Definition: GnashKey.h:95
DefineButtonCxformTag.h
gnash::RunResources
Class to group together per-run and external resources for Gnash.
Definition: RunResources.h:54
gnash::event_id::PRESS
@ PRESS
Definition: event_id.h:62
gnash::SWF::ButtonRecord::ButtonRecord
ButtonRecord()
Definition: DefineButtonTag.h:58
gnash::SWFMatrix
Definition: SWFMatrix.h:54
gnash::SWF::DefineButton2Tag
A class for parsing a DefineButton2 tag.
Definition: DefineButtonTag.h:309
gnash::SWF::DefineButtonTag::forEachTrigger
void forEachTrigger(const event_id &ev, E &f) const
Invoke a functor for each action triggered by given event.
Definition: DefineButtonTag.h:256
gnash::readCxFormRGBA
SWFCxForm readCxFormRGBA(SWFStream &in)
Read a CxForm with alpha from the input stream.
Definition: TypesParser.cpp:354
gnash::SWF::ButtonRecord::ButtonRecord
ButtonRecord(ButtonRecord &&)=default
gnash::SWFStream::get_tag_end_position
unsigned long get_tag_end_position()
Return the file position of the end of the current tag.
Definition: SWFStream.cpp:497
gnash::Button::MouseState
MouseState
Definition: Button.h:67
gnash::Button
Button implements Flash buttons.
Definition: Button.h:45
filter_factory.h
IF_VERBOSE_PARSE
#define IF_VERBOSE_PARSE(x)
Definition: log.h:378
DisplayObject.h
gnash::log_parse
void log_parse(StringType msg, Args... args)
Definition: log.h:313
gnash::Button::MOUSESTATE_DOWN
@ MOUSESTATE_DOWN
Definition: Button.h:69
DefinitionTag.h
gnash::getObject
as_object * getObject(const DisplayObject *d)
Return the as_object associated with a DisplayObject if it exists.
Definition: DisplayObject.h:1160
gnash::key::f
@ f
Definition: GnashKey.h:152
SWF.h
namedStrings.h
gnash::Global_as
The Global object ultimately contains all objects in an ActionScript run.
Definition: Global_as.h:50
gnash::SWFStream::read_u16
std::uint16_t read_u16()
Read a aligned unsigned 16-bit value from the stream.
Definition: SWFStream.cpp:332
RunResources.h
gnash::getObjectWithPrototype
as_object * getObjectWithPrototype(Global_as &gl, const ObjectURI &c)
Definition: as_object.cpp:1109
gnash::SWF::DefineButtonTag::buttonRecords
ButtonRecords & buttonRecords()
Definition: DefineButtonTag.h:216
gnash::readCxFormRGB
SWFCxForm readCxFormRGB(SWFStream &in)
Read a RGB CxForm from the input stream.
Definition: TypesParser.cpp:322
gnash::SWF::ButtonAction
A class for parsing an ActionRecord.
Definition: DefineButtonTag.h:145
gnash::SWFStream::read_u8
std::uint8_t read_u8()
Read a aligned unsigned 8-bit value from the stream.
Definition: SWFStream.cpp:319
movie_definition.h
gnash::SWF::DefineButtonTag::trackAsMenu
bool trackAsMenu() const
Whether to track this button as a menu.
Definition: DefineButtonTag.h:245
gnash::DisplayObject
DisplayObject is the base class for all DisplayList objects.
Definition: DisplayObject.h:169
gnash::SWF::ButtonRecord
A class for parsing ButtonRecord, used by DefineButton and DefineButton2.
Definition: DefineButtonTag.h:54
gnash::action_buffer
A code segment.
Definition: action_buffer.h:50
gnash::SWF::DefineButtonTag::buttonRecords
const ButtonRecords & buttonRecords() const
Read-only access to the ButtonRecords directly.
Definition: DefineButtonTag.h:219
gnash::SWF::ButtonRecord::valid
bool valid() const
Return true if the ButtonRecord is valid.
Definition: DefineButtonTag.h:109
gnash::SWF::ButtonRecord::readRGBTransform
void readRGBTransform(SWFStream &in)
Read an RGB SWFCxForm for this record.
Definition: DefineButtonTag.h:91
gnash::NSV::CLASS_BUTTON
@ CLASS_BUTTON
Definition: namedStrings.h:202
gnash::SWFStream
SWF stream wrapper class.
Definition: SWFStream.h:59
gnash::event_id::RELEASE_OUTSIDE
@ RELEASE_OUTSIDE
Definition: event_id.h:64
gnash::SWF::DefineButtonTag::getSWFVersion
int getSWFVersion() const
Return version of the SWF containing this button definition.
Definition: DefineButtonTag.cpp:245
gnash::key::E
@ E
Definition: GnashKey.h:117
gnash::SWFStream::ensureBytes
void ensureBytes(unsigned long needed)
Ensure the requested number of bytes are available for an aligned read in the currently opened tag.
Definition: SWFStream.cpp:50
gnash::key::o
@ o
Definition: GnashKey.h:161
gnash::key::SWF
@ SWF
Definition: GnashKey.h:331
gnash::log_unimpl
void log_unimpl(StringType msg, Args... args)
Definition: log.h:289
gnash::isReferenceable
bool isReferenceable(const DisplayObject &d)
Returns true if the DisplayObject is referenceable in ActionScript.
Definition: DisplayObject.h:1149
gnash::SWF::DefineButtonTag::ButtonActions
boost::ptr_vector< ButtonAction > ButtonActions
Definition: DefineButtonTag.h:206
SWFStream.h
gnash::key::e
@ e
Definition: GnashKey.h:151
gnash::SWF::ButtonRecord::instantiate
DisplayObject * instantiate(Button *button, bool name=true) const
Create a DisplayObject from a ButtonRecord.
Definition: DefineButtonTag.cpp:319
gnash::event_id::KEY_PRESS
@ KEY_PRESS
Definition: event_id.h:69
gnash::event_id::keyCode
key::code keyCode() const
Return the keycode associated with this event_id.
Definition: event_id.h:138
gnash::SWF::ButtonAction::triggeredBy
bool triggeredBy(const event_id &ev) const
Return true if this action should be triggered by the given event.
Definition: DefineButtonTag.cpp:294
gnash::Button::MOUSESTATE_UP
@ MOUSESTATE_UP
Definition: Button.h:68
gnash::event_id::DRAG_OUT
@ DRAG_OUT
Definition: event_id.h:68
SWFMatrix.h