~ubuntu-branches/ubuntu/karmic/gnash/karmic

« back to all changes in this revision

Viewing changes to libcore/asobj/flash/geom/Point_as.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2008-10-13 14:29:49 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20081013142949-f6qdvnu4mn05ltdc
Tags: 0.8.4~~bzr9980-0ubuntu1
* new upstream release 0.8.4 (LP: #240325)
* ship new lib usr/lib/gnash/libmozsdk.so.* in mozilla-plugin-gnash
  - update debian/mozilla-plugin-gnash.install
* ship new lib usr/lib/gnash/libgnashnet.so.* in gnash-common
  - update debian/gnash-common.install
* add basic debian/build_head script to build latest CVS head packages.
  - add debian/build_head
* new sound architecture requires build depend on libsdl1.2-dev
  - update debian/control
* head build script now has been completely migrated to bzr (upstream +
  ubuntu)
  - update debian/build_head
* disable kde gui until klash/qt4 has been fixed; keep kde packages as empty
  packages for now.
  - update debian/rules
  - debian/klash.install
  - debian/klash.links
  - debian/klash.manpages
  - debian/konqueror-plugin-gnash.install
* drop libkonq5-dev build dependency accordingly
  - update debian/control
* don't install headers manually anymore. gnash doesnt provide a -dev
  package after all
  - update debian/rules
* update libs installed in gnash-common; libgnashserver-*.so is not available
  anymore (removed); in turn we add the new libgnashcore-*.so
  - update debian/gnash-common.install
* use -Os for optimization and properly pass CXXFLAGS=$(CFLAGS) to configure
  - update debian/rules
* touch firefox .autoreg in postinst of mozilla plugin
  - update debian/mozilla-plugin-gnash.postinst
* link gnash in ubufox plugins directory for the plugin alternative switcher
  - add debian/mozilla-plugin-gnash.links
* suggest ubufox accordingly
  - update debian/control
* add new required build-depends on libgif-dev
  - update debian/control
* add Xb-Npp-Description and Xb-Npp-File as new plugin database meta data
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Point_as.cpp:  ActionScript "Point" class, for Gnash.
 
2
//
 
3
//   Copyright (C) 2005, 2006, 2007, 2008 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
#include "Point_as.h"
 
21
#include "as_object.h" // for inheritance
 
22
#include "log.h"
 
23
#include "fn_call.h"
 
24
#include "smart_ptr.h" // for boost intrusive_ptr
 
25
#include "builtin_function.h" // need builtin_function
 
26
#include "GnashException.h" // for ActionException
 
27
#include "Object.h" // for AS inheritance
 
28
#include "VM.h" // for addStatics
 
29
#include "utility.h" // isFinite
 
30
 
 
31
#include <sstream>
 
32
 
 
33
namespace gnash {
 
34
 
 
35
static as_value Point_add(const fn_call& fn);
 
36
static as_value Point_clone(const fn_call& fn);
 
37
static as_value Point_equals(const fn_call& fn);
 
38
static as_value Point_normalize(const fn_call& fn);
 
39
static as_value Point_offset(const fn_call& fn);
 
40
static as_value Point_subtract(const fn_call& fn);
 
41
static as_value Point_toString(const fn_call& fn);
 
42
static as_value Point_length_getset(const fn_call& fn);
 
43
 
 
44
static as_value Point_distance(const fn_call& fn);
 
45
static as_value Point_interpolate(const fn_call& fn);
 
46
static as_value Point_polar(const fn_call& fn);
 
47
 
 
48
as_value Point_ctor(const fn_call& fn);
 
49
 
 
50
static void
 
51
attachPointInterface(as_object& o)
 
52
{
 
53
    int fl=0; // flags...
 
54
 
 
55
    o.init_member("add", new builtin_function(Point_add), fl);
 
56
    o.init_member("clone", new builtin_function(Point_clone), fl);
 
57
    o.init_member("equals", new builtin_function(Point_equals), fl);
 
58
    o.init_member("normalize", new builtin_function(Point_normalize), fl);
 
59
    o.init_member("offset", new builtin_function(Point_offset), fl);
 
60
    o.init_member("subtract", new builtin_function(Point_subtract), fl);
 
61
    o.init_member("toString", new builtin_function(Point_toString), fl);
 
62
    o.init_property("length", Point_length_getset, Point_length_getset, fl);
 
63
}
 
64
 
 
65
static void
 
66
attachPointStaticProperties(as_object& o)
 
67
{
 
68
    o.init_member("distance", new builtin_function(Point_distance), 0);
 
69
    o.init_member("interpolate", new builtin_function(Point_interpolate), 0);
 
70
    o.init_member("polar", new builtin_function(Point_polar), 0);
 
71
}
 
72
 
 
73
static as_object*
 
74
getPointInterface()
 
75
{
 
76
        static boost::intrusive_ptr<as_object> o;
 
77
 
 
78
        if ( ! o )
 
79
        {
 
80
                // TODO: check if this class should inherit from Object
 
81
                //       or from a different class
 
82
                o = new as_object(getObjectInterface());
 
83
                VM::get().addStatic(o.get());
 
84
 
 
85
                attachPointInterface(*o);
 
86
 
 
87
        }
 
88
 
 
89
        return o.get();
 
90
}
 
91
 
 
92
 
 
93
class Point_as: public as_object
 
94
{
 
95
public:
 
96
        Point_as()
 
97
            :
 
98
            as_object(getPointInterface())
 
99
        {}
 
100
            
 
101
};
 
102
 
 
103
 
 
104
static as_value
 
105
Point_add(const fn_call& fn)
 
106
{
 
107
        boost::intrusive_ptr<as_object> ptr = ensureType<as_object>(fn.this_ptr);
 
108
 
 
109
        as_value x, y;
 
110
        ptr->get_member(NSV::PROP_X, &x);
 
111
        ptr->get_member(NSV::PROP_Y, &y);
 
112
 
 
113
        as_value x1, y1;
 
114
 
 
115
        if ( ! fn.nargs )
 
116
        {
 
117
                IF_VERBOSE_ASCODING_ERRORS(
 
118
                log_aserror(_("%s: missing arguments"), "Point.add()");
 
119
                );
 
120
        }
 
121
        else
 
122
        {
 
123
                IF_VERBOSE_ASCODING_ERRORS(
 
124
                if ( fn.nargs > 1 )
 
125
                {
 
126
                        std::stringstream ss; fn.dump_args(ss);
 
127
                        log_aserror("Point.add(%s): %s", ss.str(), _("arguments after first discarded"));
 
128
                }
 
129
                );
 
130
                const as_value& arg1 = fn.arg(0);
 
131
                as_object* o = arg1.to_object().get();
 
132
                if ( ! o )
 
133
                {
 
134
                        IF_VERBOSE_ASCODING_ERRORS(
 
135
                        std::stringstream ss; fn.dump_args(ss);
 
136
                        log_aserror("Point.add(%s): %s", ss.str(), _("first argument doesn't cast to object"));
 
137
                        );
 
138
                }
 
139
                else
 
140
                {
 
141
                        if ( ! o->get_member(NSV::PROP_X, &x1) )
 
142
                        {
 
143
                                IF_VERBOSE_ASCODING_ERRORS(
 
144
                                std::stringstream ss; fn.dump_args(ss);
 
145
                                log_aserror("Point.add(%s): %s", ss.str(),
 
146
                                        _("first argument cast to object doesn't contain an 'x' member"));
 
147
                                );
 
148
                        }
 
149
                        if ( ! o->get_member(NSV::PROP_Y, &y1) )
 
150
                        {
 
151
                                IF_VERBOSE_ASCODING_ERRORS(
 
152
                                std::stringstream ss; fn.dump_args(ss);
 
153
                                log_aserror("Point.add(%s): %s", ss.str(),
 
154
                                        _("first argument cast to object doesn't contain an 'y' member"));
 
155
                                );
 
156
                        }
 
157
                }
 
158
        }
 
159
 
 
160
        x.newAdd(x1);
 
161
        y.newAdd(y1);
 
162
 
 
163
        boost::intrusive_ptr<as_object> ret = new Point_as;
 
164
        ret->set_member(NSV::PROP_X, x);
 
165
        ret->set_member(NSV::PROP_Y, y);
 
166
 
 
167
        return as_value(ret.get());
 
168
}
 
169
 
 
170
static as_value
 
171
Point_clone(const fn_call& fn)
 
172
{
 
173
        boost::intrusive_ptr<as_object> ptr = ensureType<as_object>(fn.this_ptr);
 
174
 
 
175
        as_value x, y;
 
176
        ptr->get_member(NSV::PROP_X, &x);
 
177
        ptr->get_member(NSV::PROP_Y, &y);
 
178
 
 
179
        boost::intrusive_ptr<as_object> ret = new Point_as;
 
180
        ret->set_member(NSV::PROP_X, x);
 
181
        ret->set_member(NSV::PROP_Y, y);
 
182
 
 
183
        return as_value(ret.get());
 
184
}
 
185
 
 
186
static as_value
 
187
Point_equals(const fn_call& fn)
 
188
{
 
189
        boost::intrusive_ptr<as_object> ptr = ensureType<as_object>(fn.this_ptr);
 
190
 
 
191
        if ( ! fn.nargs )
 
192
        {
 
193
                IF_VERBOSE_ASCODING_ERRORS(
 
194
                log_aserror(_("%s: missing arguments"), "Point.equals()");
 
195
                );
 
196
                return as_value(false);
 
197
        }
 
198
 
 
199
        const as_value& arg1 = fn.arg(0);
 
200
        if ( ! arg1.is_object() )
 
201
        {
 
202
                IF_VERBOSE_ASCODING_ERRORS(
 
203
                std::stringstream ss; fn.dump_args(ss);
 
204
                log_aserror("Point.equals(%s): %s", ss.str(), _("First arg must be an object"));
 
205
                );
 
206
                return as_value(false);
 
207
        }
 
208
        as_object* o = arg1.to_object().get();
 
209
        assert(o);
 
210
        if ( ! o->instanceOf(getFlashGeomPointConstructor()) )
 
211
        {
 
212
                IF_VERBOSE_ASCODING_ERRORS(
 
213
                std::stringstream ss; fn.dump_args(ss);
 
214
                log_aserror("Point.equals(%s): %s %s", ss.str(), _("First arg must be an instance of"), "flash.geom.Point");
 
215
                );
 
216
                return as_value(false);
 
217
        }
 
218
 
 
219
        as_value x, y;
 
220
        ptr->get_member(NSV::PROP_X, &x);
 
221
        ptr->get_member(NSV::PROP_Y, &y);
 
222
 
 
223
        as_value x1, y1;
 
224
        o->get_member(NSV::PROP_X, &x1);
 
225
        o->get_member(NSV::PROP_Y, &y1);
 
226
 
 
227
        return as_value(x.equals(x1) && y.equals(y1));
 
228
}
 
229
 
 
230
static as_value
 
231
Point_normalize(const fn_call& fn)
 
232
{
 
233
        boost::intrusive_ptr<Point_as> ptr = ensureType<Point_as>(fn.this_ptr);
 
234
 
 
235
        as_value argval;
 
236
 
 
237
        if ( ! fn.nargs )
 
238
        {
 
239
                IF_VERBOSE_ASCODING_ERRORS(
 
240
                log_aserror(_("%s: missing arguments"), "Point.normalize()");
 
241
                );
 
242
                return as_value();
 
243
        }
 
244
        else
 
245
        {
 
246
                IF_VERBOSE_ASCODING_ERRORS(
 
247
                if ( fn.nargs > 1 )
 
248
                {
 
249
                        std::stringstream ss; fn.dump_args(ss);
 
250
                        log_aserror("Point.normalize(%s): %s", ss.str(), _("arguments after first discarded"));
 
251
                }
 
252
                );
 
253
 
 
254
                argval = fn.arg(0);
 
255
        }
 
256
 
 
257
        // newlen may be NaN, and we'd still be updating x/y
 
258
        // see actionscript.all/Point.as
 
259
        double newlen = argval.to_number();
 
260
 
 
261
        as_value xval, yval;
 
262
        ptr->get_member(NSV::PROP_X, &xval);
 
263
        ptr->get_member(NSV::PROP_Y, &yval);
 
264
 
 
265
        double x = xval.to_number();
 
266
        if ( ! utility::isFinite(x) ) return as_value();
 
267
        double y = yval.to_number();
 
268
        if ( ! utility::isFinite(y) ) return as_value();
 
269
 
 
270
        if ( x == 0 && y == 0 ) return as_value();
 
271
 
 
272
        double curlen = std::sqrt(x*x+y*y);
 
273
        double fact = newlen/curlen;
 
274
 
 
275
 
 
276
        xval.set_double( xval.to_number() * fact );
 
277
        yval.set_double( yval.to_number() * fact );
 
278
        ptr->set_member(NSV::PROP_X, xval);
 
279
        ptr->set_member(NSV::PROP_Y, yval);
 
280
 
 
281
        return as_value();
 
282
}
 
283
 
 
284
static as_value
 
285
Point_offset(const fn_call& fn)
 
286
{
 
287
        boost::intrusive_ptr<Point_as> ptr = ensureType<Point_as>(fn.this_ptr);
 
288
 
 
289
        as_value x, y;
 
290
        ptr->get_member(NSV::PROP_X, &x);
 
291
        ptr->get_member(NSV::PROP_Y, &y);
 
292
 
 
293
        as_value xoff, yoff;
 
294
 
 
295
        if ( fn.nargs ) {
 
296
                xoff = fn.arg(0);
 
297
                if ( fn.nargs > 1 ) yoff = fn.arg(1);
 
298
        }
 
299
 
 
300
        x.newAdd(xoff);
 
301
        y.newAdd(yoff);
 
302
 
 
303
        ptr->set_member(NSV::PROP_X, x);
 
304
        ptr->set_member(NSV::PROP_Y, y);
 
305
 
 
306
        return as_value();
 
307
}
 
308
 
 
309
static as_value
 
310
Point_subtract(const fn_call& fn)
 
311
{
 
312
        boost::intrusive_ptr<Point_as> ptr = ensureType<Point_as>(fn.this_ptr);
 
313
 
 
314
        as_value x, y;
 
315
        ptr->get_member(NSV::PROP_X, &x);
 
316
        ptr->get_member(NSV::PROP_Y, &y);
 
317
 
 
318
        as_value x1, y1;
 
319
 
 
320
        if ( ! fn.nargs )
 
321
        {
 
322
                IF_VERBOSE_ASCODING_ERRORS(
 
323
                log_aserror(_("%s: missing arguments"), "Point.add()");
 
324
                );
 
325
        }
 
326
        else
 
327
        {
 
328
                IF_VERBOSE_ASCODING_ERRORS(
 
329
                if ( fn.nargs > 1 )
 
330
                {
 
331
                        std::stringstream ss; fn.dump_args(ss);
 
332
                        log_aserror("Point.add(%s): %s", ss.str(), _("arguments after first discarded"));
 
333
                }
 
334
                );
 
335
                const as_value& arg1 = fn.arg(0);
 
336
                as_object* o = arg1.to_object().get();
 
337
                if ( ! o )
 
338
                {
 
339
                        IF_VERBOSE_ASCODING_ERRORS(
 
340
                        std::stringstream ss; fn.dump_args(ss);
 
341
                        log_aserror("Point.add(%s): %s", ss.str(), _("first argument doesn't cast to object"));
 
342
                        );
 
343
                }
 
344
                else
 
345
                {
 
346
                        if ( ! o->get_member(NSV::PROP_X, &x1) )
 
347
                        {
 
348
                                IF_VERBOSE_ASCODING_ERRORS(
 
349
                                std::stringstream ss; fn.dump_args(ss);
 
350
                                log_aserror("Point.add(%s): %s", ss.str(),
 
351
                                        _("first argument casted to object doesn't contain an 'x' member"));
 
352
                                );
 
353
                        }
 
354
                        if ( ! o->get_member(NSV::PROP_Y, &y1) )
 
355
                        {
 
356
                                IF_VERBOSE_ASCODING_ERRORS(
 
357
                                std::stringstream ss; fn.dump_args(ss);
 
358
                                log_aserror("Point.add(%s): %s", ss.str(),
 
359
                                        _("first argument casted to object doesn't contain an 'y' member"));
 
360
                                );
 
361
                        }
 
362
                }
 
363
        }
 
364
 
 
365
        x.set_double(x.to_number() - x1.to_number());
 
366
        y.set_double(y.to_number() - y1.to_number());
 
367
 
 
368
        boost::intrusive_ptr<as_object> ret = new Point_as;
 
369
        ret->set_member(NSV::PROP_X, x);
 
370
        ret->set_member(NSV::PROP_Y, y);
 
371
 
 
372
        return as_value(ret.get());
 
373
}
 
374
 
 
375
static as_value
 
376
Point_toString(const fn_call& fn)
 
377
{
 
378
        boost::intrusive_ptr<Point_as> ptr = ensureType<Point_as>(fn.this_ptr);
 
379
 
 
380
        as_value x, y;
 
381
        ptr->get_member(NSV::PROP_X, &x);
 
382
        ptr->get_member(NSV::PROP_Y, &y);
 
383
 
 
384
        std::stringstream ss;
 
385
        ss << "(x=" << x.to_string()
 
386
                << ", y=" << y.to_string()
 
387
                << ")";
 
388
 
 
389
        return as_value(ss.str());
 
390
}
 
391
 
 
392
static as_value
 
393
Point_length_getset(const fn_call& fn)
 
394
{
 
395
        boost::intrusive_ptr<Point_as> ptr = ensureType<Point_as>(fn.this_ptr);
 
396
 
 
397
        if ( ! fn.nargs ) // getter
 
398
        {
 
399
                as_value xval, yval;
 
400
                ptr->get_member(NSV::PROP_X, &xval);
 
401
                ptr->get_member(NSV::PROP_Y, &yval);
 
402
                double x = xval.to_number();
 
403
                double y = yval.to_number();
 
404
 
 
405
                double l = std::sqrt(x*x+y*y);
 
406
                return as_value(l);
 
407
        }
 
408
        else // setter
 
409
        {
 
410
                IF_VERBOSE_ASCODING_ERRORS(
 
411
                log_aserror(_("Attempt to set read-only property %s"), "Point.length");
 
412
                );
 
413
                return as_value();
 
414
        }
 
415
}
 
416
 
 
417
static as_value
 
418
Point_distance(const fn_call& fn)
 
419
{
 
420
        if ( fn.nargs < 2 )
 
421
        {
 
422
                IF_VERBOSE_ASCODING_ERRORS(
 
423
                std::stringstream ss; fn.dump_args(ss);
 
424
                log_aserror("Point.distance(%s): %s", ss.str(), _("missing arguments"));
 
425
                );
 
426
                return as_value();
 
427
        }
 
428
 
 
429
        IF_VERBOSE_ASCODING_ERRORS(
 
430
        if ( fn.nargs > 2 )
 
431
        {
 
432
                std::stringstream ss; fn.dump_args(ss);
 
433
                log_aserror("Point.distance(%s): %s", ss.str(), _("arguments after first two discarded"));
 
434
        }
 
435
        );
 
436
 
 
437
        const as_value& arg1 = fn.arg(0);
 
438
        if ( ! arg1.is_object() )
 
439
        {
 
440
                IF_VERBOSE_ASCODING_ERRORS(
 
441
                std::stringstream ss; fn.dump_args(ss);
 
442
                log_aserror("Point.distance(%s): %s", ss.str(), _("First arg must be an object"));
 
443
                );
 
444
                return as_value();
 
445
        }
 
446
        as_object* o1 = arg1.to_object().get();
 
447
        assert(o1);
 
448
        if ( ! o1->instanceOf(getFlashGeomPointConstructor()) )
 
449
        {
 
450
                IF_VERBOSE_ASCODING_ERRORS(
 
451
                std::stringstream ss; fn.dump_args(ss);
 
452
                log_aserror("Point.equals(%s): %s %s", ss.str(), _("First arg must be an instance of"), "flash.geom.Point");
 
453
                );
 
454
                return as_value();
 
455
        }
 
456
 
 
457
        const as_value& arg2 = fn.arg(1);
 
458
        as_object* o2 = arg2.to_object().get();
 
459
        assert(o2);
 
460
        // it seems there's no need to check arg2 (see actionscript.all/Point.as)
 
461
 
 
462
        as_value x1val;
 
463
        o1->get_member(NSV::PROP_X, &x1val);
 
464
        double x1 = x1val.to_number();
 
465
        //if ( ! utility::isFinite(x1) ) return as_value(NaN);
 
466
 
 
467
        as_value y1val;
 
468
        o1->get_member(NSV::PROP_Y, &y1val);
 
469
        double y1 = y1val.to_number();
 
470
        //if ( ! utility::isFinite(y1) ) return as_value(NaN);
 
471
 
 
472
        as_value x2val;
 
473
        o2->get_member(NSV::PROP_X, &x2val);
 
474
        double x2 = x2val.to_number();
 
475
        //if ( ! utility::isFinite(x2) ) return as_value(NaN);
 
476
 
 
477
        as_value y2val;
 
478
        o2->get_member(NSV::PROP_Y, &y2val);
 
479
        double y2 = y2val.to_number();
 
480
        //if ( ! utility::isFinite(y2) ) return as_value(NaN);
 
481
 
 
482
        double hside = x2 - x1; // p1.x - p0.x;
 
483
        double vside = y2 - y1; // p1.y - p0.y;
 
484
 
 
485
        double sqdist = hside*hside + vside*vside;
 
486
        double dist = std::sqrt(sqdist);
 
487
 
 
488
        return as_value(dist);
 
489
}
 
490
 
 
491
static as_value
 
492
Point_interpolate(const fn_call& fn)
 
493
{
 
494
        as_value x0val;
 
495
        as_value y0val;
 
496
        as_value x1val;
 
497
        as_value y1val;
 
498
        as_value muval;
 
499
 
 
500
        if ( fn.nargs < 3 )
 
501
        {
 
502
                IF_VERBOSE_ASCODING_ERRORS(
 
503
                std::stringstream ss; fn.dump_args(ss);
 
504
                log_aserror("Point.interpolate(%s): %s", ss.str(), _("missing arguments"));
 
505
                );
 
506
        }
 
507
        else
 
508
        {
 
509
                IF_VERBOSE_ASCODING_ERRORS(
 
510
                if ( fn.nargs > 3 )
 
511
                {
 
512
                std::stringstream ss; fn.dump_args(ss);
 
513
                log_aserror("Point.interpolate(%s): %s", ss.str(), _("arguments after first three discarded"));
 
514
                }
 
515
                );
 
516
 
 
517
                const as_value& p0val = fn.arg(0);
 
518
                as_object* p0 = p0val.to_object().get();
 
519
                if ( ! p0 )
 
520
                {
 
521
                        IF_VERBOSE_ASCODING_ERRORS(
 
522
                        std::stringstream ss; fn.dump_args(ss);
 
523
                        log_aserror("Point.interpolate(%s): %s", ss.str(), _("first argument doesn't cast to object"));
 
524
                        );
 
525
                }
 
526
                else
 
527
                {
 
528
                        p0->get_member(NSV::PROP_X, &x0val);
 
529
                        p0->get_member(NSV::PROP_Y, &y0val);
 
530
                }
 
531
 
 
532
                const as_value& p1val = fn.arg(1);
 
533
                as_object* p1 = p1val.to_object().get();
 
534
                if ( ! p1 )
 
535
                {
 
536
                        IF_VERBOSE_ASCODING_ERRORS(
 
537
                        std::stringstream ss; fn.dump_args(ss);
 
538
                        log_aserror("Point.interpolate(%s): %s", ss.str(), _("second argument doesn't cast to object"));
 
539
                        );
 
540
                }
 
541
                else
 
542
                {
 
543
                        p1->get_member(NSV::PROP_X, &x1val);
 
544
                        p1->get_member(NSV::PROP_Y, &y1val);
 
545
                }
 
546
 
 
547
                muval = fn.arg(2);
 
548
        }
 
549
 
 
550
 
 
551
        double x0 = x0val.to_number();
 
552
        double y0 = y0val.to_number();
 
553
        double x1 = x1val.to_number();
 
554
        double y1 = y1val.to_number();
 
555
        double mu = muval.to_number();
 
556
 
 
557
        // newX = b.x + ( muval * (a.x - b.x) );
 
558
        // newY = b.y + ( muval * (a.y - b.y) );
 
559
 
 
560
        as_value xoff = mu * (x0 - x1);
 
561
        as_value yoff = mu * (y0 - y1);
 
562
 
 
563
        //log_debug("xoff:%s, yoff:%s, x1val:%s, y1val:%s", xoff, yoff, x1val, y1val);
 
564
 
 
565
        as_value x = x1val; // copy to avoid changing stack value
 
566
        x.newAdd(xoff);
 
567
        as_value y = y1val; // copy to avoid changing stack value
 
568
        y.newAdd(yoff);
 
569
 
 
570
        boost::intrusive_ptr<as_object> ret = new Point_as;
 
571
        ret->set_member(NSV::PROP_X, as_value(x));
 
572
        ret->set_member(NSV::PROP_Y, as_value(y));
 
573
 
 
574
        return as_value(ret.get());
 
575
}
 
576
 
 
577
static as_value
 
578
Point_polar(const fn_call& fn)
 
579
{
 
580
        as_value lval; // length
 
581
        as_value aval; // angle (radians)
 
582
 
 
583
        if ( fn.nargs )
 
584
        {
 
585
                lval=fn.arg(0);
 
586
                if ( fn.nargs > 1 ) aval=fn.arg(1);
 
587
                else
 
588
                {
 
589
                        IF_VERBOSE_ASCODING_ERRORS(
 
590
                        std::stringstream ss; fn.dump_args(ss);
 
591
                        log_aserror("Point.polar(%s): %s", ss.str(), _("missing arguments"));
 
592
                        );
 
593
                }
 
594
        }
 
595
        else
 
596
        {
 
597
                IF_VERBOSE_ASCODING_ERRORS(
 
598
                std::stringstream ss; fn.dump_args(ss);
 
599
                log_aserror("Point.polar(%s): %s", ss.str(), _("missing arguments"));
 
600
                );
 
601
        }
 
602
        
 
603
        double len = lval.to_number();
 
604
        double angle = aval.to_number();
 
605
 
 
606
        double x = len * std::cos(angle);
 
607
        double y = len * std::sin(angle);
 
608
 
 
609
        as_value xval(x);
 
610
        as_value yval(y);
 
611
        boost::intrusive_ptr<as_object> obj = new Point_as;
 
612
 
 
613
        obj->set_member(NSV::PROP_X, x);
 
614
        obj->set_member(NSV::PROP_Y, y);
 
615
        
 
616
        return as_value(obj.get());
 
617
}
 
618
 
 
619
 
 
620
as_value
 
621
Point_ctor(const fn_call& fn)
 
622
{
 
623
        boost::intrusive_ptr<as_object> obj = new Point_as;
 
624
 
 
625
        as_value x;
 
626
        as_value y;
 
627
 
 
628
        if ( ! fn.nargs )
 
629
        {
 
630
                x.set_double(0);
 
631
                y.set_double(0);
 
632
        }
 
633
        else
 
634
        {
 
635
                do {
 
636
                        x = fn.arg(0);
 
637
                        if ( fn.nargs < 2 ) break;
 
638
                        y = fn.arg(1);
 
639
                        if ( fn.nargs < 3 ) break;
 
640
                        IF_VERBOSE_ASCODING_ERRORS(
 
641
                                std::stringstream ss;
 
642
                                fn.dump_args(ss);
 
643
                                log_aserror("flash.geom.Point(%s): %s", ss.str(), _("arguments after the first two discarded"));
 
644
                        );
 
645
                } while(0);
 
646
        }
 
647
 
 
648
        obj->set_member(NSV::PROP_X, x);
 
649
        obj->set_member(NSV::PROP_Y, y);
 
650
 
 
651
        return as_value(obj.get()); // will keep alive
 
652
}
 
653
 
 
654
// extern 
 
655
as_function* getFlashGeomPointConstructor()
 
656
{
 
657
        static builtin_function* cl=NULL;
 
658
        if ( ! cl )
 
659
        {
 
660
                cl=new builtin_function(&Point_ctor, getPointInterface());
 
661
                VM::get().addStatic(cl);
 
662
                attachPointStaticProperties(*cl);
 
663
        }
 
664
        return cl;
 
665
}
 
666
 
 
667
static as_value get_flash_geom_point_constructor(const fn_call& /*fn*/)
 
668
{
 
669
        log_debug("Loading flash.geom.Point class");
 
670
 
 
671
        return getFlashGeomPointConstructor();
 
672
}
 
673
 
 
674
boost::intrusive_ptr<as_object> init_Point_instance()
 
675
{
 
676
    return boost::intrusive_ptr<as_object>(new Point_as);
 
677
}
 
678
 
 
679
// extern 
 
680
void Point_class_init(as_object& where)
 
681
{
 
682
        // Register _global.Point
 
683
        string_table& st = where.getVM().getStringTable();
 
684
        where.init_destructive_property(st.find("Point"), get_flash_geom_point_constructor);
 
685
}
 
686
 
 
687
} // end of gnash namespace