summaryrefslogtreecommitdiff
path: root/Source/Main.hx
blob: 3146f894ecfb00e658273d91e72334f1083f7359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package;

import openfl.display.Sprite;
import openfl.geom.Point;
import openfl.events.MouseEvent;
import openfl.events.KeyboardEvent;
import openfl.events.Event;
import openfl.events.TimerEvent;
import openfl.ui.Keyboard;
import openfl.utils.Timer;
import openfl.display.SimpleButton;
import openfl.text.TextField;
import openfl.text.TextFormat;
import motion.Actuate;
import haxe.ds.Option;

using Lambda;

typedef PointType = { x:Float, y:Float};
typedef HasVelocity =  {velocity: PointType};
typedef Circle = PointType & {radius:Float};
typedef HasColor = {color: Int};

typedef MovingColoredCircle = Circle & HasVelocity & HasColor;


class Button extends SimpleButton
{
  static var overColor:Int = 0xdddddd;
  static var upColor:Int = 0xeeeeee;
  static var downColor:Int = 0xcccccc;

  static function textBox
  (text:String,
   bgColor:Int = 0xFFFFFF,
   ?textFormat:TextFormat,
   ?borderRadius:Float = 0.0,
   ?padding:Float = 40.0,
   ?borderThickness:Float = 0.0,
   ?borderColor:Int = 0
  ):Sprite
  {
    var tf = new TextField();
    tf.multiline = false;
    tf.autoSize = openfl.text.TextFieldAutoSize.CENTER;
    tf.selectable = false;
    tf.text = text;

    if (textFormat != null)
      tf.setTextFormat( textFormat );

    var s = new Sprite();
    s.graphics.beginFill( bgColor );

    if (borderThickness > 0)
      s.graphics.lineStyle( borderThickness, borderColor);

    s.graphics.drawRoundRect(0, 0,
                             tf.textWidth + padding,
                             tf.textHeight + padding,
                             borderRadius,
                             borderRadius );
    s.graphics.endFill();

    tf.x = (s.width - tf.textWidth) / 2;
    tf.y = (s.height - tf.textHeight) / 2;

    s.addChild( tf );
    return s;
  }
  
  public function new (text:String)
  {
    var textFormat = new TextFormat(null, 25);

    var over = textBox(text, overColor, textFormat, 10.0, 50, 2 );
    var up = textBox(text, upColor, textFormat, 10.0, 50, 2);
    var down = textBox(text, downColor, textFormat, 10.0, 50, 2);

    super( up , over, down, over);
    this.enabled = true;
  }

}

class Wiggler extends Sprite
{
  var path:Array<Point> = [];
  var circles:Array<MovingColoredCircle> = [];

  public function new (path:Array<Point>)
  {
    super();
    this.path = path;
  }
}

class DrawingScreen extends Sprite
{

  /* the prupose of which is to produce a path of a closed polygon
     suitable for passing in as the "skin" of a wiggler. */
  
  var path: Array<Point> = [];
  var holdPath:Bool = false;

  public function new ()
  {
    super();
    addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
    addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    addEventListener(Event.ADDED_TO_STAGE, maximizeHitArea);
  }

  /* Event Handling */
  
  static inline var sampleRate:Float = 0.01;
  static inline var sampleGap:Float = 5.0;
  static inline var bgColor:Int = 0xFFFFFF;

  var drawing = false;
  var timestamp:Float;

  function maximizeHitArea(e)
  {
    var hitBox = new Sprite();
    hitBox.graphics.beginFill(0);
    hitBox.graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight);
    hitBox.mouseEnabled = false;
    this.hitArea = hitBox;
  }

  function onMouseDown (e)
  {
    trace('onMouseDown');
    refresh();
    drawing = true;
    timestamp = haxe.Timer.stamp();
    path = [ new Point(e.localX, e.localY) ];

    graphics.lineStyle(3, 0);
    graphics.moveTo( path[0].x, path[0].y );
  }

  function refresh ()
  {
    Actuate.stop(this);
    graphics.clear();
    alpha = 1.0;
    visible = true;
    path = [];
    holdPath = false;
  }

  function fadeAndRefresh()
  {
    Actuate
      .tween(this, 0.5, {alpha: 0})
      .onComplete( refresh );
  }

  function onMouseUp (e)
  {
    drawing = false;
    if (!holdPath) fadeAndRefresh();
  }
  
  function onMouseOut (e)
  {
    drawing = false;
  }
  
  function clearAndRenderPath()
  {
    graphics.clear();
    graphics.lineStyle(3, 0);
    graphics.moveTo(path[0].x, path[0].y);
    for (i in 1...path.length)
      graphics.lineTo(path[i].x, path[i].y);
    graphics.lineTo(path[0].x, path[0].y);
  }

  function onMouseMove (e)
  {
    if (!drawing) return;

    var stamp = haxe.Timer.stamp();
    var pt = new Point( e.localX, e.localY);
    if ((stamp - timestamp > sampleRate) &&
        Point.distance(pt, path[path.length - 1]) >= sampleGap)
      {
        var intersectIndex = GeomTools.findSelfIntersection( path, pt );
        if (intersectIndex != null)
          {
            drawing = false;
            holdPath = true;
            var intersectionPt =
              GeomTools.linesIntersectAt( path[intersectIndex],
                                          path[intersectIndex + 1],
                                          path[path.length -1], pt);

            path = path.slice( intersectIndex );
            if (intersectionPt != null)
              {
                path.push(intersectionPt);
                graphics.lineTo( intersectionPt.x, intersectionPt.y);
              }
            else
              {
                graphics.lineTo( pt.x, pt.y );
              }

            clearAndRenderPath();
            return; // to return early
          }

        timestamp = stamp;
        path.push( pt );
        graphics.lineTo( pt.x, pt.y);
      }
  }
}

enum Line {
  Vertical(xVal:Float);
  Horizontal(yVal:Float);
  Sloped(slop:Float,yIntercept:Float);
}

class GeomTools
{

  public static function lineOfSegment ( a:Point, b:Point ): Null<Line>
  {
    if (a.equals( b )) return null;
    if (a.x == b.x) return Vertical(a.x);
    if (a.y == b.y) return Horizontal(a.y);

    var slope = (b.y - a.y) / (b.x - a.x);
    var yIntercept = a.y - slope * a.x;
    return Sloped(slope, yIntercept);
  }

  public static function isCounterClockwiseOrder(a:Point,b:Point,c:Point):Bool {
    return (b.x - a.x) * (c.y - a.y) > (b.y - a.y) * (c.x - a.x);
  }

  public static function segmentsIntersect(a:Point,b:Point,c:Point,d:Point):Bool
  {
    return (isCounterClockwiseOrder( a, c, d) != isCounterClockwiseOrder(b, c, d)) &&
      (isCounterClockwiseOrder( a ,b, c) != isCounterClockwiseOrder(a, b, d));
  }


  public static function pathIsCounterClockwise( path: Array<Point> ) : Bool
  {
    return path.length > 2 && isCounterClockwiseOrder(path[0], path[1], path[2]);
  }


  public static function linesIntersectAt(a:Point,b:Point,c:Point,d:Point):Null<Point>
  {
    var segments = [lineOfSegment(a, b), lineOfSegment(c, d)];
    trace(segments);

    switch (segments)
      {
      case [Sloped(m1,b1), Sloped(m2,b2)]:
        var x = (b2 - b1) / (m1 - m2);
        var y = m1 * x + b1;
        return new Point(x, y);

      case [Sloped(m,b), Horizontal(y)] | [Horizontal(y) , Sloped(m, b)]:
        var x = (y - b) / m;
        return new Point(x,y);

      case [Sloped(m,b), Vertical(x)] | [Vertical(x), Sloped(m,b)]:
        var y = m * x + b;
        return new Point(x, y);

      case [Horizontal(y), Vertical(x)] | [Vertical(x), Horizontal(y)]:
        return new Point(x, y);

      default:
        return null;
      }
  }

  // given a path and a point, check of the line between the last
  // point in tha path and the provided point intersects the path.  If
  // it does, the last index in the path checked is returned.
  public static function findSelfIntersection
  ( path:Array<Point>, pt:Point ) : Null<Int>
  {
    if (path != null && path.length > 0)
      {
        var last = path.length -1;
        for (i in 1...last)
          if ( segmentsIntersect( path[i-1], path[i], path[last], pt) )
            return i;
      }
    return null;
  }
}

class Main extends Sprite
{
  public function new()
  {
    super();
    addEventListener(Event.ADDED_TO_STAGE, onInit);
  }

  function onInit (e)
  {
    var screen = new DrawingScreen();
    addChild( screen );
  }
  
}