summaryrefslogtreecommitdiff
path: root/designer.bmx
blob: a45034d0770afd7f1bac0e671494e5f6d8fd6c18 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
' Particle Pinch
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import noddybox.vector
Import noddybox.bitmapfont
Import noddybox.simplegui
Import noddybox.algorithm
Import "types.bmx"
Import "level.bmx"
Import "game.bmx"

Function LevelDesigner()
	DoDesigner()
End Function

Private

' **** Types
'
Type TDesObj Abstract
	Const SELSIZE:Int=3
	
	Function Create:TDesObj(x:Int, y:Int) Abstract
	Function CreateFromLevel:TDesObj(o:Object) Abstract
	Method Draw() Abstract
	Method DrawSelect() Abstract
	Method MouseOver:Int(x:Int, y:Int) Abstract
	Method Drag(x:Int, y:Int) Abstract
	Method Edit() Abstract
	Method Save(lev:TLevel) Abstract
	Method SetInfo(w:TLabel) Abstract
	Method Snap() Abstract

	Method DrawSelBox(x:Int, y:Int)
		Local x1:Int=x-SELSIZE
		Local y1:Int=y-SELSIZE
		Local x2:Int=x+SELSIZE
		Local y2:Int=y+SELSIZE
		
		SetColor(255,255,255)
		DrawLine(x1,y1,x2,y1)
		DrawLine(x2,y1,x2,y2)
		DrawLine(x2,y2,x1,y2)
		DrawLine(x1,y2,x1,y1)
	End Method
	
	Method InSelBox(px:Int, py:Int, x:Int, y:Int)
		Local x1:Int=x-SELSIZE
		Local y1:Int=y-SELSIZE
		Local x2:Int=x+SELSIZE
		Local y2:Int=y+SELSIZE

		Return px>=x1 And px<=x2 And py>=y1 And py<=y2
	End Method
	
	Method DrawCoord(x:Int, y:Int)
		GameGFX.font.Draw(x+","+y,0,0)
	End Method

	Method DoubleString:String(v:Double)
		Local s:String=v
		
		If s.Find(".")<>-1
			While s.length>1 And s[s.length-1]=Asc("0")
				s=s[..s.length-1]
			Wend
			
			While s.length>1 And s[s.length-1]=Asc(".")
				s=s[..s.length-1]
			Wend
		EndIf
		
		Return s
	End Method

	Method BoolString:String(b:Int)
		If b
			Return "Yes"
		Else
			Return "No "
		EndIf
	End Method
	
	Method CalcSnap:Int(p:Int)
		Return (p+Designer.GRID/2-1)/Designer.GRID*Designer.GRID-1
	End Method
End Type

Type TDesGrav Extends TDesObj
	Field g:TGravPoint
	
	Function Create:TDesObj(x:Int, y:Int)
		Local o:TDesGrav=New TDesGrav
		
		o.g=New TGravPoint
		o.g.x=x
		o.g.y=y
		o.g.friendly=False
		o.g.mass=25
		o.g.repel=False
		
		Return o
	End Function
	
	Function CreateFromLevel:TDesObj(o:Object)
		Local lp:TGravPoint=TGravPoint(o)
		Local no:TDesGrav=New TDesGrav
		
		no.g=New TGravPoint
		no.g.x=lp.x
		no.g.y=lp.y
		no.g.friendly=lp.friendly
		no.g.mass=lp.mass
		no.g.repel=lp.repel
		
		Return no
	End Function
	
	Method Draw()
		SetColor(255,255,255)
		If g.friendly
			DrawImage(GameGFX.collector,g.x,g.y)
		Else
			DrawImage(GameGFX.star,g.x,g.y)
		EndIf
	End Method
	
	Method DrawSelect()
		DrawSelBox(g.x,g.y)
	End Method
	
	Method MouseOver:Int(x:Int, y:Int)
		Return InSelBox(x,y,g.x,g.y)
	End Method
	
	Method Drag(x:Int, y:Int)
		DrawCoord(x,y)
		g.x=x
		g.y=y
	End Method
	
	Method Edit()
		Designer.md_friendly.checked = g.friendly
		Designer.md_invert.checked = g.repel
		Designer.md_mass.SetFloat(g.mass)

		If GUIDialog(Designer.mdialog,Designer.md_ok,Designer.md_cancel,GameGFX.pointer)
			g.friendly = Designer.md_friendly.checked
			g.repel = Designer.md_invert.checked
			g.mass = Designer.md_mass.text.ToDouble()
		EndIf
	End Method

	Method Save(lev:TLevel)
		lev.grav.AddLast(g)
	End Method

	Method SetInfo(w:TLabel)
		w.text="Friendly:" + BoolString(g.friendly) + "  Inverse gravity:" + BoolString(g.repel) + "  Mass: " + DoubleString(g.mass)
	End Method
	
	Method Snap()
		g.x=CalcSnap(g.x)
		g.y=CalcSnap(g.y)
	End Method

End Type

Type TDesPoint Extends TDesObj
	Field l:TPointLine
	Field over_p1:Int
	
	Function Create:TDesObj(x:Int, y:Int)
		Local o:TDesPoint=New TDesPoint
		
		o.over_p1=False

		o.l=New TPointLine
		o.l.x1=x
		o.l.y1=y
		o.l.y2=y
		
		If x<GraphicsWidth()/2
			o.l.x2=x+50
		Else
			o.l.x2=x-50
		EndIf
		
		o.l.gap=1
		o.l.v.x=0
		o.l.v.y=0
		
		Return o
	End Function
	
	Function CreateFromLevel:TDesObj(o:Object)
		Local lp:TPointLine=TPointLine(o)
		Local no:TDesPoint=New TDesPoint
		
		no.l=New TPointLine
		no.l.x1=lp.x1
		no.l.y1=lp.y1
		no.l.x2=lp.x2
		no.l.y2=lp.y2
		no.l.gap=lp.gap
		no.l.v.x=lp.v.x
		no.l.v.y=lp.v.y
		no.l.circle=lp.circle
		no.l.circvel=lp.circvel
		
		Return no
	End Function
	
	Method Draw()
		SetColor(255,128,128)

		If l.circle
			DrawLine(l.x1,l.y1,l.x2,l.y1)
			
			Local p:TAlgoPoint[]=DoCircle(l.x1,l.y1,Abs(l.x2-l.x1))
			
			For Local f:Int=0 To 359
				Local nf:Int=(f+1) Mod 360
				DrawLine(p[f].x,p[f].y,p[nf].x,p[nf].y)
			Next
		Else
			Local lp:TList=DoLine(l.x1,l.y1,l.x2,l.y2)
			
			For Local p:TAlgoPoint=EachIn lp
				Plot(p.x,p.y)
			Next
		EndIf
		
		Local lp:TList=CreateList()
		
		l.CreatePoints(lp)
		
		For Local p:TPoint=EachIn lp
			SetColor(0,255,0)
			DrawLine(p.x,p.y,p.x+p.v.x*10,p.y+p.v.y*10)
			SetColor(128,128,255)
			DrawImage(GameGFX.point,p.x,p.y)
		Next
	End Method
	
	Method DrawSelect()
		If over_p1
			DrawSelBox(l.x1,l.y1)
		Else
			DrawSelBox(l.x2,l.y2)
		EndIf
	End Method
	
	Method MouseOver:Int(x:Int, y:Int)
		If l.circle
			If InSelBox(x,y,l.x1,l.y1)
				over_p1=True
				Return True
			EndIf
			If InSelBox(x,y,l.x2,l.y1)
				over_p1=False
				Return True
			EndIf
		Else
			If InSelBox(x,y,l.x1,l.y1)
				over_p1=True
				Return True
			EndIf
			If InSelBox(x,y,l.x2,l.y2)
				over_p1=False
				Return True
			EndIf
		EndIf
		Return False
	End Method
	
	Method Drag(x:Int, y:Int)
		Local cx:Int=x
		Local cy:Int=y
		
		If l.circle
			If over_p1
				Local diff:Int=l.x2-l.x1
				l.x1=x
				l.y1=y
				l.x2=l.x1+diff
				l.y2=l.y1
			Else
				l.x2=x
				l.y2=l.y1
				cy=l.y2
			EndIf
		Else
			If over_p1
				l.x1=x
				l.y1=y
			Else
				l.x2=x
				l.y2=y
			EndIf
		EndIf

		DrawCoord(cx,cy)
	End Method
	
	Method Edit()
		Designer.pd_gap.text = l.gap
		Designer.pd_vx.SetFloat(l.v.x)
		Designer.pd_vy.SetFloat(l.v.y)
		Designer.pd_circle.checked = l.circle
		Designer.pd_circvel.SetFloat(l.circvel)

		If GUIDialog(Designer.pdialog,Designer.pd_ok,Designer.pd_cancel,GameGFX.pointer)
			l.gap = Max(1,Designer.pd_gap.text.ToInt())
			l.v.x = Designer.pd_vx.text.ToDouble()
			l.v.y = Designer.pd_vy.text.ToDouble()
			l.circle = Designer.pd_circle.checked
			l.circvel = Designer.pd_circvel.text.ToDouble()
		EndIf
	End Method

	Method Save(lev:TLevel)
		lev.point.AddLast(l)
	End Method

	Method SetInfo(w:TLabel)
		If l.circle
			w.text="Gap:" + l.gap + "  DX: " + DoubleString(l.v.x) + "  DY: " + DoubleString(l.v.y) + "  Angular velocity: " + DoubleString(l.circvel)
		Else
			w.text="Gap:" + l.gap + "  DX: " + DoubleString(l.v.x) + "  DY: " + DoubleString(l.v.y)
		EndIf
	End Method

	Method Snap()
		If l.circle
			If over_p1
				l.x1=CalcSnap(l.x1)
				l.y1=CalcSnap(l.y1)
			Else
				l.x2=CalcSnap(l.x2)
				l.y1=CalcSnap(l.y1)
				l.y2=l.y1
			EndIf
		Else
			If over_p1
				l.x1=CalcSnap(l.x1)
				l.y1=CalcSnap(l.y1)
			Else
				l.x2=CalcSnap(l.x2)
				l.y2=CalcSnap(l.y2)
			EndIf
		EndIf
	End Method

End Type

' **** Globals
'
' This type acts as a namespace for global variables
'
Type Designer
	Const TEXTX:Int=100
	Const GRID:Int=20

	Global init:Int=False
	
	Global obj:TList
	
	Global levelset:TLevelSet
	Global level:TLevel
	Global done:Int
	Global levelindex:Int
	
	Global gui:TGUIHandler
	
	Global fname_txt:TText
	Global fname_load:TButton
	Global fname_save:TButton
	Global fname_new:TButton
	
	Global setname_txt:TText
	
	Global levname_txt:TText
	Global levadd_but:TButton
	Global levins_but:TButton
	Global levdel_but:TButton
	Global levnum:TNumberInt
	
	Global info:TLabel
	
	Global hide_check:TCheckbox
	Global grid_check:TCheckbox
	
	Global validbut:TButton
	Global helpbut:TButton
	Global quitbut:TButton
	
	Global mdialog:TGUIHandler
	Global md_friendly:TCheckbox
	Global md_invert:TCheckbox
	Global md_mass:TText
	Global md_ok:TButton
	Global md_cancel:TButton

	Global pdialog:TGUIHandler
	Global pd_gap:TText
	Global pd_vx:TText
	Global pd_vy:TText
	Global pd_circle:TCheckbox
	Global pd_circvel:TText
	Global pd_ok:TButton
	Global pd_cancel:TButton

	Global ldialog:TGUIHandler
	Global ld_invert:TCheckbox
	Global ld_placefriend:TCheckbox
	Global ld_maxmass:TText
	Global ld_winpercent:TText
	Global ld_timer:TText
	Global ld_placemass:TText
	Global ld_ok:TButton
	Global ld_cancel:TButton

	Function Initialise()
		If Not init
			Local l:TLabel
			Local p:TPanel
			
			obj =			CreateList()

			levelset =		New TLevelSet
			level =			New TLevel
			levelindex =		0
			
			gui =			TGUIHandler.Create()
			
			TLabel.Create(gui,0,0,"File")
			fname_txt =		TText.Create(gui,TEXTX,0,"Default.ppinch",32)
			fname_load =		TButton.Create(gui,fname_txt.x+fname_txt.w+10,0,50,12,"Load",LoadCallback)
			fname_save =		TButton.Create(gui,fname_load.x+fname_load.w+10,0,50,fname_load.h,"Save",SaveCallback)
			fname_new =		TButton.Create(gui,fname_save.x+fname_save.w+10,0,50,fname_load.h,"New",NewCallback)
			
			TLabel.Create(gui,0,10,"Levelset name")
			setname_txt =		TText.Create(gui,TEXTX,10,"",32)
			
			TLabel.Create(gui,0,20,"Level name")
			levname_txt =		TText.Create(gui,TEXTX,20,"",32)
			levadd_but =		TButton.Create(gui,levname_txt.x+levname_txt.w+10,levname_txt.y,50,12,"Add",AddLevelCallback)
			levins_but =		TButton.Create(gui,levadd_but.x+levadd_but.w+10,levname_txt.y,50,12,"Insert",InsertLevelCallback)
			levdel_but =		TButton.Create(gui,levins_but.x+levins_but.w+10,levname_txt.y,50,12,"Delete",DeleteLevelCallback)

			l =				TLabel.Create(gui,0,35,"Level:")
			levnum =			TNumberInt.Create(gui,l.x+l.w+10,35,LevelNumberCallback)
			levnum.value =	0
			levnum.change =	1
			levnum.minval =	0
			levnum.maxval =	0
			
			info =			TLabel.Create(gui,0,50,"")
			
			hide_check =		TCheckbox.Create(gui,750,0,"Hide",HideCallback)
			grid_check =		TCheckbox.Create(gui,750,15,"Grid")
			grid_check.checked=True
			
			validbut =		TButton.Create(gui,650,570,49,29,"Check",CheckCallback)
			helpbut =			TButton.Create(gui,700,570,49,29,"Test",TestCallback)
			quitbut =			TButton.Create(gui,750,570,49,29,"Quit",QuitCallback)

			mdialog =			TGUIHandler.Create()
			p =				TPanel.Create(mdialog,-1,-1,400,100)
			md_friendly =		TCheckbox.Create(mdialog,p.x+5,p.y+10,"Friendly (scores for player)?")
			md_invert =		TCheckbox.Create(mdialog,p.x+5,p.y+30,"Inverse gravity?")
			l =				TLabel.Create(mdialog,p.x+5,p.y+50,"Mass:")
			md_mass =			TText.Create(mdialog,p.x+l.w+10,p.y+50,"",30,TText.NUMERIC|TText.POSITIVE)
			md_ok =			TButton.Create(mdialog,p.x+5,p.y+p.h-25,p.w/2-10,20,"OK",Null)
			md_cancel =		TButton.Create(mdialog,p.x+p.w/2+5,p.y+p.h-25,p.w/2-10,20,"Cancel",Null)

			pdialog =			TGUIHandler.Create()
			p =				TPanel.Create(pdialog,-1,-1,400,200)
			l =				TLabel.Create(pdialog,p.x+5,p.y+10,"Gap per point:")
			pd_gap =			TText.Create(pdialog,l.x+l.w+10,l.y,"",30,TText.NUMERIC|TText.INTEGER|TText.POSITIVE)
			l =				TLabel.Create(pdialog,p.x+5,p.y+30,"Initial dx:")
			pd_vx =			TText.Create(pdialog,l.x+l.w+10,l.y,"",30,TText.NUMERIC)
			l =				TLabel.Create(pdialog,p.x+5,p.y+50,"Initial dy:")
			pd_vy =			TText.Create(pdialog,l.x+l.w+10,l.y,"",30,TText.NUMERIC)
			pd_circle =		TCheckbox.Create(pdialog,p.x+5,p.y+70,"Circle?")
			l =				TLabel.Create(pdialog,p.x+5,p.y+90,"Circular velocity:")
			pd_circvel =		TText.Create(pdialog,l.x+l.w+10,l.y,"",30,TText.NUMERIC)
			pd_ok =			TButton.Create(pdialog,p.x+5,p.y+p.h-25,p.w/2-10,20,"OK",Null)
			pd_cancel =		TButton.Create(pdialog,p.x+p.w/2+5,p.y+p.h-25,p.w/2-10,20,"Cancel",Null)

			ldialog =			TGUIHandler.Create()
			p =				TPanel.Create(ldialog,-1,-1,400,200)
			ld_invert =		TCheckbox.Create(ldialog,p.x+5,p.y+10,"Inverse gravity for dropped masses?")
			ld_placefriend =	TCheckbox.Create(ldialog,p.x+5,p.y+30,"Dropped masses are collectors (friendly)?")
			l =				TLabel.Create(ldialog,p.x+5,p.y+50,"Max Dropped Masses:")
			ld_maxmass =		TText.Create(ldialog,l.x+l.w+10,l.y,"",2,TText.NUMERIC|TText.INTEGER|TText.POSITIVE)
			l =				TLabel.Create(ldialog,p.x+5,p.y+70,"Win percentage:")
			ld_winpercent =	TText.Create(ldialog,l.x+l.w+10,l.y,"",3,TText.NUMERIC|TText.INTEGER|TText.POSITIVE)
			l =				TLabel.Create(ldialog,p.x+5,p.y+90,"Timer (roughly seconds):")
			ld_timer =		TText.Create(ldialog,l.x+l.w+10,l.y,"",3,TText.NUMERIC|TText.INTEGER|TText.POSITIVE)
			l =				TLabel.Create(ldialog,p.x+5,p.y+110,"Placed mass:")
			ld_placemass =	TText.Create(ldialog,l.x+l.w+10,l.y,"",20,TText.NUMERIC|TText.POSITIVE)
			ld_ok =			TButton.Create(ldialog,p.x+5,p.y+p.h-25,p.w/2-10,20,"OK",Null)
			ld_cancel =		TButton.Create(ldialog,p.x+p.w/2+5,p.y+p.h-25,p.w/2-10,20,"Cancel",Null)

			levelset.level.AddLast(level)
			init=True
		EndIf
		
		done = False
		LoadLevel()
	End Function

	Function LoadLevel()
		setname_txt.text = levelset.name
		level = TLevel(ListAt(levelset.level,levelindex).Value())
		levname_txt.text = level.name
		
		obj.Clear()
		
		For Local o:Object=EachIn level.grav
			obj.AddLast(TDesGrav.CreateFromLevel(o))
		Next
		
		For Local o:Object=EachIn level.point
			obj.AddLast(TDesPoint.CreateFromLevel(o))
		Next
	End Function
	
	Function SaveLevel()
		levelset.name = setname_txt.text
		level.name = levname_txt.text
		level.grav.Clear()
		level.point.Clear()
		For Local o:TDesObj=EachIn obj
			o.Save(level)
		Next
	End Function

	Function EditLevelSettings()
		ld_invert.checked = level.invmass
		ld_placefriend.checked = level.placefriend
		ld_maxmass.text = level.maxmass
		ld_winpercent.text = level.winpercent
		ld_timer.text = level.timer
		ld_placemass.SetFloat(level.placemass)

		If GUIDialog(ldialog,ld_ok,ld_cancel,GameGFX.pointer)
			level.invmass = ld_invert.checked
			level.placefriend= ld_placefriend.checked
			level.maxmass = ld_maxmass.text.ToInt()
			level.winpercent = Max(0,Min(100,ld_winpercent.text.ToInt()))
			level.timer = ld_timer.text.ToInt()
			level.placemass = ld_placemass.text.ToDouble()
		EndIf
	End Function
End Type


' **** Main Loop
'
Function DoDesigner()
	Designer.Initialise()
	
	Designer.done=False
	
	Local sel:TDesObj=Null
	Local drag:Int=False
	
	While Not Designer.done
		Cls
		
		If Designer.grid_check.checked
			For Local x:Int=-400 To 400 Step 20
				If x=0
					SetColor(50,50,255)
				Else
					SetColor(50,50,128)
				EndIf
				DrawLine(399+x,0,399+x,599)
			Next
			For Local y:Int=-300 To 300 Step 20
				If y=0
					SetColor(50,50,255)
				Else
					SetColor(50,50,128)
				EndIf
				DrawLine(0,299+y,799,299+y)
			Next
		EndIf
		
		If Not drag
			Designer.gui.EventLoop()
		EndIf
		
		Local x:Int=MouseX()
		Local y:Int=MouseY()
		
		If Not drag
			sel=Null
		EndIf

		For Local o:TDesObj=EachIn Designer.obj
			o.Draw()
			
			If sel=Null And o.MouseOver(x,y)
				sel=o
			EndIf
		Next
		
		If sel<>Null
			sel.SetInfo(Designer.info)
			sel.DrawSelect()
		EndIf
		
		If drag
			sel.Drag(x,y)
		EndIf
		
		If KeyHit(KEY_MOUSERIGHT)
			If sel<>Null
				Select GUIMenu("Object Menu",["Edit","Snap to grid","Delete"],x,y,GameGFX.pointer)
					Case 0
						sel.Edit()
					Case 1
						sel.Snap()
					Case 2
						Designer.obj.Remove(sel)
				End Select
			Else
				Select GUIMenu("Create Menu",["Create Gravity Point","Create Particle Line","Edit Level Settings"],x,y,GameGFX.pointer)
					Case 0
						Designer.obj.AddLast(TDesGrav.Create(x,y))
					Case 1
						Designer.obj.AddLast(TDesPoint.Create(x,y))
					Case 2
						Designer.EditLevelSettings()
				End Select
			EndIf
			sel=Null
		EndIf
		
		If Not drag
			If KeyDown(KEY_MOUSELEFT) And sel<>Null
				drag=True
			EndIf
		Else
			If Not KeyDown(KEY_MOUSELEFT)
				drag=False
			EndIf
		EndIf

		SetColor(255,255,255)
		DrawImage(GameGFX.pointer,MouseX(),MouseY())
		
		Flip
		FlushMem
	Wend
End Function


' **** Utils
'
Function ListAt:TLink(l:TList, i:Int)
	Local tl:TLink=l.FirstLink()
	
	While i>0 And tl<>Null
		i:-1
		tl=tl.NextLink()
	Wend
	
	Return tl
End Function

' **** Callbacks
'
Function HideCallback(w:TWidget)
	Local c:TCheckbox=TCheckbox(w)
	Designer.gui.SetEnable(Not c.checked)
	c.enabled=True
End Function

Function QuitCallback(w:TWidget)
	Designer.done=GUIYesNo("Quit back to the|main menu of Particle Pinch?",GameGFX.pointer)
End Function

Function TestCallback(w:TWidget)
	Designer.SaveLevel()
	Local g:TGame=TGame.Create(Designer.level)
	Local res:Int=TGame.LEVEL_NOTOVER
	
	While res<>TGame.LEVEL_FINISHED And res<>TGame.LEVEL_CANCELLED
		Cls
		res=g.Play()
		SetColor(255,255,255)
		DrawImage(GameGFX.pointer,MouseX(),MouseY())
		FlushMem
		Flip
	Wend
	
	FlushKeys()
End Function

Function CheckCallback(w:TWidget)
	Designer.SaveLevel()
	
	Local s:String="The following problems were found:"
	Local ok:Int=True
	Local m:TList=CreateList()
	Local p:TList=CreateList()
	
	Designer.level.CreatePlayfield(m,p)
	
	If m.Count()+Designer.level.maxmass>MAX_GRAV
		ok=False
		s:+"|Too many masses (combining placed and dropped)"
	EndIf
	
	Local friends:Int=(Designer.level.placefriend And Designer.level.maxmass>0)
	
	If Not friends
		For Local gp:TMass=EachIn m
			If gp.friend
				friends=True
			EndIf
		Next
	EndIf
	
	If Not friends
		ok=False
		s:+"|No collector masses (no particles can be captured by player)"
	EndIf
	
	If p.Count()>MAX_POINT
		ok=False
		s:+"|Too many points (" + p.Count() + " -- maximum is " + MAX_POINT
	EndIf
	
	If p.Count()=0
		ok=False
		s:+"|No particles to collect!"
	EndIf
	
	If Not ok
		GUINotify(s,GameGFX.pointer)
	Else
		GUINotify("Level is valid",GameGFX.pointer)
	EndIf
End Function

Function LoadCallback(w:TWidget)
	Try
		If GUIYesNo("Overwrite current level set?",GameGFX.pointer)
			Local load:TLevelSet=TLevelSet.Load(Designer.fname_txt.text)
			Designer.levelset=load
			Designer.levelindex=0
			Designer.levnum.maxval=Designer.levelset.level.Count()-1
			Designer.levnum.value=0
			Designer.LoadLevel()
		EndIf
	Catch e:TLevelException
		GUINotify("Failed to load '" + Designer.fname_txt.text + "'||"+e.message,GameGFX.pointer)
	EndTry
End Function

Function SaveCallback(w:TWidget)
	Designer.SaveLevel()
	If Not Designer.levelset.Save(Designer.fname_txt.text)
		GUINotify("Failed to save '" + Designer.fname_txt.text + "'",GameGFX.pointer)
	Else
		GUINotify("'" + Designer.fname_txt.text + "' saved OK",GameGFX.pointer)
	EndIf
End Function

Function NewCallback(w:TWidget)
	If GUIYesNo("Lose current level set?",GameGFX.pointer)
		Designer.levelindex=0
		Designer.levnum.maxval=0
		Designer.levelset=New TLevelSet
		Designer.levelset.level.AddLast(New TLevel)
		Designer.fname_txt.text="Untitled.ppinch"
		Designer.LoadLevel()
	EndIf
End Function

Function LevelNumberCallback(w:TWidget)
	Local c:TNumberInt=TNumberInt(w)
	Designer.SaveLevel()
	Designer.levelindex=c.value
	Designer.LoadLevel()
End Function

Function AddLevelCallback(w:TWidget)
	Designer.levelset.level.AddLast(New TLevel)
	Designer.levnum.maxval=Designer.levelset.level.Count()-1
End Function

Function InsertLevelCallback(w:TWidget)
	Designer.SaveLevel()
	Local l:TLink=ListAt(Designer.levelset.level,Designer.levelindex)
	Designer.levelset.level.InsertBeforeLink(New TLevel,l)
	Designer.levnum.maxval=Designer.levelset.level.Count()-1
	Designer.LoadLevel()
End Function

Function DeleteLevelCallback(w:TWidget)
	If Designer.levelset.level.Count()<2
		GUINotify("Must have at least one level!",GameGFX.pointer)
		Return
	EndIf
	
	If GUIYesNo("Delete this level?||"+Designer.levname_txt.text,GameGFX.pointer)
		ListAt(Designer.levelset.level,Designer.levelindex).Remove()
		
		If Designer.levelindex>=Designer.levelset.level.Count()
			Designer.levelindex:-1
			Designer.levnum.value=Designer.levelindex
		EndIf
		Designer.levnum.maxval=Designer.levelset.level.Count()-1
		Designer.LoadLevel()
	EndIf
End Function