This repository was archived by the owner on Mar 13, 2023. It is now read-only.
forked from teverse/teverse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkshop.lua
More file actions
1641 lines (1299 loc) · 45.2 KB
/
Copy pathworkshop.lua
File metadata and controls
1641 lines (1299 loc) · 45.2 KB
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Copyright (c) 2018 teverse.com
-- workshop.lua
-- This script has access to 'engine.workshop' APIs.
-- Contains everything needed to grow your own workshop.
-- TODO: tidy everything up
--
-- Undo/Redo History system
--
local history = {}
local dirty = {} -- Records changes made since last action
local currentPoint = 0 -- The current point in the history array that is used to undo
local goingBack = false -- Used to prevent objectChanged from functioning while undoing
local function objectChanged(property, value)
-- TODO: self is a reference to an event object
-- self.object is what the event is about
-- self:disconnect() is used to disconnect this handler
if goingBack then return end
if not dirty[self.object] then
dirty[self.object] = {}
end
if not dirty[self.object][property] then
-- mark the property as changed
dirty[self.object][property] = self.object[property]
end
end
local function savePoint()
local newPoint = {}
for object, properties in pairs(dirty) do
newPoint[object] = properties
end
if currentPoint < #history then
-- the user just undoed
-- lets overwrite the no longer history
local historySize = #history
for i = currentpoint+1, historySize do
table.remove(history, i)
end
end
table.insert(history, newPoint)
currentPoint = #history
dirty = {}
end
-- hook existing objects
for _,v in pairs(workspace.children) do
v:changed(objectChanged)
end
workspace:childAdded(function(child)
child:changed(objectChanged)
if not goingBack and dirty[child] then
dirty[child].new = true
end
end)
function undo()
if currentPoint == 0 then return end
currentPoint = currentPoint - 1
local snapShot = history[currentPoint]
if not snapShot then snapShot = {} end
goingBack = true
for object, properties in pairs(snapShot) do
for property, value in pairs(properties) do
object[property] = value
end
end
goingBack = false
end
function redo()
if currentPoint >= #history then
return print("Debug: can't redo.")
end
currentPoint = currentPoint + 1
local snapShot = history[currentPoint]
if not snapShot then return print("Debug: no snapshot found") end
goingBack = true
for object, properties in pairs(snapShot) do
for property, value in pairs(properties) do
object[property] = value
end
end
goingBack = false
end
--
-- UI
--
local normalFontName = "OpenSans-Regular"
local boldFontName = "OpenSans-Bold"
local themeColourWindow = colour:fromRGB(61, 66, 71)
local themeColourWindowText = colour(1, 1, 1)
local themeColourToolBar = colour:fromRGB(61, 66, 71)
local themeColourToolBarText = colour(1, 1, 1)
local themeColourToolSelected = colour(1, 1, 1)
local themeColourToolHovered = colour(0.9, 0.9, 0.9)
local themeColourToolDeselected = colour(0.6, 0.6, 0.6)
local themeColourButton = colour:fromRGB(78, 82, 86)
local themeColourButtonHighlighted = colour:fromRGB(96, 105, 114)
local themeColourButtonText = colour(1, 1, 1)
-- Menu Bar Creation
local menuBarTop = engine.guiMenuBar()
menuBarTop.size = guiCoord(1, 0, 0, 24)
menuBarTop.position = guiCoord(0, 0, 0, 0)
menuBarTop.parent = engine.workshop.interface
-- File Menu
local menuFile = menuBarTop:createItem("File")
local menuFileNew = menuFile:createItem("New Scene")
local menuFileOpen = menuFile:createItem("Open Scene")
local menuFileSave = menuFile:createItem("Save Scene")
local menuFileSaveAs = menuFile:createItem("Save Scene As")
-- Edit Menu
local menuEdit = menuBarTop:createItem("Edit")
local menuEditUndo = menuEdit:createItem("Undo")
local menuEditRedo = menuEdit:createItem("Redo")
local menuEditNote = menuEdit:createItem("! this doesnt work")
-- Insert Menu
local menuInsert = menuBarTop:createItem("Insert")
local menuInsertBlock = menuInsert:createItem("Block")
menuEditUndo:mouseLeftPressed(undo)
menuEditRedo:mouseLeftPressed(redo)
menuFileNew:mouseLeftReleased(function()
engine.workshop:newGame()
end)
menuFileOpen:mouseLeftReleased(function()
-- Tell the Workshop APIs to initate a game load.
engine.workshop:openFileDialogue()
end)
menuFileSave:mouseLeftReleased(function()
engine.workshop:saveGame() -- returns boolean
end)
menuFileSaveAs:mouseLeftReleased(function()
engine.workshop:saveGameAsDialogue()
end)
menuInsertBlock:mouseLeftPressed(function ()
local newBlock = engine.block("block")
newBlock.colour = colour(1,0,0)
newBlock.size = vector3(1,1,1)
newBlock.parent = workspace
local camera = workspace.camera
local lookVector = camera.rotation * vector3(0, 0, 1)
newBlock.position = camera.position - (lookVector * 10)
savePoint() -- for undo/redo
end)
-- Tool Bar Creation
local toolBarMain = engine.guiFrame()
toolBarMain.size = guiCoord(1, -240, 0, 30)
toolBarMain.position = guiCoord(0, 0, 0, 22)
toolBarMain.parent = engine.workshop.interface
toolBarMain.backgroundColour = themeColourToolBar
local activeTool = 0
local disableDefaultClickActions = false
local tools = {}
local function addTool(image, activate, deactivate)
local toolID = #tools + 1;
local toolButton = engine.guiImage()
toolButton.size = guiCoord(0, 24, 0, 24)
toolButton.position = guiCoord(0, 10 + (30 * #tools), 0, 3)
toolButton.parent = toolBarMain
toolButton.backgroundAlpha = 0
toolButton.imageColour = themeColourToolDeselected
toolButton.texture = image;
toolButton:mouseLeftReleased(function()
if tools[activeTool] then
tools[activeTool].gui.imageColour = themeColourToolDeselected
if deactivate then
deactivate(activeTool)
end
end
if activeTool == toolID then
activeTool = 0
else
activeTool = toolID
tools[toolID].gui.imageColour = themeColourToolSelected
if activate then
activate(toolID)
end
end
end)
table.insert(tools, {["gui"]=toolButton, ["data"]={}})
end
local windowProperties = engine.guiWindow()
windowProperties.size = guiCoord(0, 240, 0.6, -10)
windowProperties.position = guiCoord(1, -240, 0.4, 10)
windowProperties.parent = engine.workshop.interface
windowProperties.text = "Properties"
windowProperties.name = "windowProperties"
windowProperties.fontSize = 10
windowProperties.backgroundColour = themeColourWindow
windowProperties.textColour = themeColourWindowText
windowProperties.fontFile = normalFontName
windowProperties.guiStyle = enums.guiStyle.windowNoCloseButton
local scrollViewProperties = engine.guiScrollView("scrollView")
scrollViewProperties.size = guiCoord(1,-5,1,-20)
scrollViewProperties.parent = windowProperties
scrollViewProperties.position = guiCoord(0,0,0,16)
scrollViewProperties.guiStyle = enums.guiStyle.noBackground
local function generateLabel(text, parent)
local lbl = engine.guiTextBox()
lbl.size = guiCoord(1, 0, 0, 16)
lbl.position = guiCoord(0, 0, 0, 0)
lbl.fontSize = 9
lbl.guiStyle = enums.guiStyle.noBackground
lbl.fontFile = normalFontName
lbl.text = tostring(text)
lbl.wrap = false
lbl.align = enums.align.middleLeft
lbl.parent = parent or engine.workshop.interface
lbl.textColour = themeColourWindowText
return lbl
end
local function setReadOnly( textbox, value )
textbox.readOnly = value
if value then
textbox.backgroundAlpha = 0.8
else
textbox.backgroundAlpha = 1
end
end
local function generateInputBox(text, parent)
local lbl = engine.guiTextBox()
lbl.size = guiCoord(1, 0, 0, 21)
lbl.position = guiCoord(0, 0, 0, 0)
lbl.backgroundColour = themeColourButton
lbl.fontSize = 9
lbl.fontFile = normalFontName
lbl.text = tostring(text)
lbl.readOnly = false
lbl.wrap = true
lbl.multiline = false
lbl.align = enums.align.middle
if parent then
lbl.parent = parent
end
lbl.textColour = themeColourButtonText
return lbl
end
-- Selected Integer Text
local txtProperty = generateLabel("0 items selected", windowProperties)
txtProperty.name = "txtProperty"
txtProperty.textColour = themeColourWindowText
txtProperty.backgroundAlpha = 0.9
local event = nil -- stores the instance changed event so we can disconnect it
local showing = nil
local propertyThread = nil
--- ! POORLY OPTIMISED
--- TODO: REDO THIS METHOD
local function generateProperties( instance )
if instance == showing then return end
showing = instance
if propertyThread then
propertyThread:kill()
end
for _,v in pairs(scrollViewProperties.children) do
if v.name ~= "txtProperty" then
v:destroy() -- error
--v.visible = false
end
end
propertyThread = spawnThread(function()
local start = os.clock()
if event then
event:disconnect()
event = nil
end
if not instance then
scrollViewProperties.canvasSize = guiCoord(1,0,1,0)
return end
local destC = os.clock()
-- TODO: Add a way to properly verify an event exists.
if instance and instance.events and instance.events["changed"] then
event = instance:changed(function(key,value,oldValue)
for _,v in pairs(scrollViewProperties.children) do
if v.name == key then
local propertyType = type(value)
if propertyType == "vector2" then
v.x.text = tostring(value.x)
v.y.text = tostring(value.y)
elseif propertyType == "colour" then
v.r.text = tostring(value.r)
v.g.text = tostring(value.g)
v.b.text = tostring(value.b)
elseif propertyType == "vector3" then
v.x.text = tostring(value.x)
v.y.text = tostring(value.y)
v.z.text = tostring(value.z)
elseif propertyType == "guiCoord" then
v.scaleX.text = tostring(value.scaleX)
v.scaleY.text = tostring(value.scaleY)
v.offsetX.text = tostring(value.offsetX)
v.offsetY.text = tostring(value.offsetY)
elseif propertyType == "boolean" then
v.bool.selected = value
elseif isInstance(value) then
elseif propertyType == "number" then
v.number.text = tostring(value)
else
v.input.text = tostring(value)
end
end
end
end)
end
local eventsC = os.clock()
local members = engine.workshop:getMembersOfInstance( instance )
local y = 0
table.sort( members, function( a,b ) return a.property < b.property end ) -- alphabetical sort
local sortedC = os.clock()
for i, prop in pairs (members) do
if i % 8 == 0 then
wait()
end
local value = instance[prop.property]
local propertyType = type(value)
local readOnly = not prop.writable
if propertyType == "function" or propertyType == "table" then
-- Lua doesn't come with a "continue"
-- Teverse uses LuaJIT,
-- Here's a fancy functionality:
-- Jumps to the ::continue:: label
goto continue
end
local lblProp = generateLabel(prop.property, scrollViewProperties)
lblProp.position = guiCoord(0,3,0,y)
lblProp.size = guiCoord(0.47, -6, 0, 15)
lblProp.name = "lbl"..prop.property
if readOnly then
lblProp.backgroundAlpha = 0.5
end
local propContainer = engine.guiFrame()
propContainer.parent = scrollViewProperties
propContainer.name = prop.property
propContainer.size = guiCoord(0.54, -9, 0, 21) -- Compensates for the natural padding inside a guiWindow.
propContainer.position = guiCoord(0.45,0,0,y)
propContainer.backgroundAlpha = 0
if propertyType == "vector2" then
local txtProp = generateInputBox(value.x, propContainer)
txtProp.name = "x"
txtProp.position = guiCoord(0,1,0,0)
txtProp.size = guiCoord(0.5, -1, 1, 0)
setReadOnly(txtProp, readOnly)
local txtProp = generateInputBox(value.y, propContainer)
txtProp.name = "y"
txtProp.position = guiCoord(0.5,2,0,0)
txtProp.size = guiCoord(0.5, -1, 1, 0)
setReadOnly(txtProp, readOnly)
elseif propertyType == "colour" then
local colourPreview = engine.guiFrame()
colourPreview.name = "preview"
colourPreview.parent = propContainer
colourPreview.size = guiCoord(0.25, -10, 1, -12)
colourPreview.position = guiCoord(0.75, 7, 0, 6)
colourPreview.backgroundColour = value
local txtR = generateInputBox(value.r, propContainer)
txtR.name = "r"
txtR.position = guiCoord(0,1,0,0)
txtR.size = guiCoord(0.25, -1, 1, 0)
setReadOnly(txtR, readOnly)
txtR:textInput(function(value) -- Only fires when a user types in the box.
local col = instance[prop.property]
col.r = tonumber(value)
instance[prop.property] = col
colourPreview.backgroundColour = col
if tonumber(value) then savePoint() end
end)
local txtG = generateInputBox(value.g, propContainer)
txtG.name = "g"
txtG.position = guiCoord(0.25,1,0,0)
txtG.size = guiCoord(0.25, -1, 1, 0)
setReadOnly(txtG, readOnly)
txtG:textInput(function(value) -- Only fires when a user types in the box.
local col = instance[prop.property]
col.g = tonumber(value)
instance[prop.property] = col
colourPreview.backgroundColour = col
if tonumber(value) then savePoint() end
end)
local txtB = generateInputBox(value.b, propContainer)
txtB.name = "b"
txtB.position = guiCoord(0.5,1,0,0)
txtB.size = guiCoord(0.25, -1, 1, 0)
setReadOnly(txtB, readOnly)
txtB:textInput(function(value) -- Only fires when a user types in the box.
local col = instance[prop.property]
col.b = tonumber(value)
instance[prop.property] = col
colourPreview.backgroundColour = col
if tonumber(value) then savePoint() end
end)
elseif propertyType == "vector3" then
local txtX = generateInputBox(value.x, propContainer)
txtX.position = guiCoord(0,0,0,0)
txtX.name = "x"
txtX.size = guiCoord(1/3, -1, 1, 0)
setReadOnly(txtX, readOnly)
txtX:textInput(function(value) -- Only fires when a user types in the box.
local vec = instance[prop.property]
vec.x = tonumber(value)
instance[prop.property] = vec
if tonumber(value) then savePoint() end
end)
local txtY = generateInputBox(value.y, propContainer)
txtY.name = "y"
txtY.position = guiCoord(1/3,1,0,0)
txtY.size = guiCoord(1/3, -1, 1, 0)
setReadOnly(txtY, readOnly)
txtY:textInput(function(value) -- Only fires when a user types in the box.
local vec = instance[prop.property]
vec.y = tonumber(value)
instance[prop.property] = vec
if tonumber(value) then savePoint() end
end)
local txtZ = generateInputBox(value.z, propContainer)
txtZ.name = "z"
txtZ.position = guiCoord(2/3,2,0,0)
txtZ.size = guiCoord(1/3, -1, 1, 0)
setReadOnly(txtZ, readOnly)
txtZ:textInput(function(value) -- Only fires when a user types in the box.
local vec = instance[prop.property]
vec.z = tonumber(value)
instance[prop.property] = vec
if tonumber(value) then savePoint() end
end)
elseif propertyType == "quaternion" then
--quaternions are not suitable to be edited by hand
-- we'll allow people to edit it as an euler
local asEuler = value:getEuler()
local txtX, txtY, txtZ
local function quatHandler()
if not (tonumber(txtX.text) and tonumber(txtY.text) and tonumber(txtZ.text)) then return end
local vec = vector3(tonumber(txtX.text),tonumber(txtY.text),tonumber(txtZ.text))
local newRot = quaternion():setEuler(vec)
instance[prop.property] = newRot
if newRot then savePoint() end
end
txtX = generateInputBox(asEuler.x, propContainer)
txtX.position = guiCoord(0,0,0,0)
txtX.name = "x"
txtX.size = guiCoord(1/3, -1, 1, 0)
setReadOnly(txtX, readOnly)
txtX:textInput(quatHandler)
txtY = generateInputBox(asEuler.y, propContainer)
txtY.name = "y"
txtY.position = guiCoord(1/3,1,0,0)
txtY.size = guiCoord(1/3, -1, 1, 0)
setReadOnly(txtY, readOnly)
txtY:textInput(quatHandler)
txtZ = generateInputBox(asEuler.z, propContainer)
txtZ.name = "z"
txtZ.position = guiCoord(2/3,2,0,0)
txtZ.size = guiCoord(1/3, -1, 1, 0)
setReadOnly(txtZ, readOnly)
txtZ:textInput(quatHandler)
elseif propertyType == "guiCoord" then
local scaleX = generateInputBox(value.scaleX, propContainer)
scaleX.name = "scaleX"
scaleX.position = guiCoord(0,1,0,0)
scaleX.size = guiCoord(0.25, -1, 1, 0)
setReadOnly(scaleX, readOnly)
scaleX:textInput(function(value) -- Only fires when a user types in the box.
local coord = instance[prop.property]
coord.scaleX = tonumber(value)
instance[prop.property] = coord
if tonumber(value) then savePoint() end
end)
local offsetX = generateInputBox(value.offsetX, propContainer)
offsetX.name = "offsetX"
offsetX.position = guiCoord(0.25,1,0,0)
offsetX.size = guiCoord(0.25, -1, 1, 0)
setReadOnly(offsetX, readOnly)
offsetX:textInput(function(value) -- Only fires when a user types in the box.
local coord = instance[prop.property]
coord.offsetX = tonumber(value)
instance[prop.property] = coord
if tonumber(value) then savePoint() end
end)
local scaleY = generateInputBox(value.scaleY, propContainer)
scaleY.name = "scaleY"
scaleY.position = guiCoord(0.5,2,0,0)
scaleY.size = guiCoord(0.25, -1, 1, 0)
setReadOnly(scaleY, readOnly)
scaleY:textInput(function(value) -- Only fires when a user types in the box.
local coord = instance[prop.property]
coord.scaleY = tonumber(value)
instance[prop.property] = coord
if tonumber(value) then savePoint() end
end)
local offsetY = generateInputBox(value.offsetY, propContainer)
offsetY.name = "offsetY"
offsetY.position = guiCoord(0.75,2,0,0)
offsetY.size = guiCoord(0.25, -1, 1, 0)
setReadOnly(offsetY, readOnly)
offsetY:textInput(function(value) -- Only fires when a user types in the box.
local coord = instance[prop.property]
coord.offsetY = tonumber(value)
instance[prop.property] = coord
if tonumber(value) then savePoint() end
end)
elseif propertyType == "boolean" then
local boolProp = engine.guiButton()
boolProp.name = "bool"
boolProp.parent = propContainer
boolProp.position = guiCoord(0,0,0,2)
boolProp.size = guiCoord(1, 0, 1, 0)
boolProp.text = ""
boolProp.guiStyle = enums.guiStyle.checkBox
boolProp.selected = value
boolProp:mouseLeftPressed(function()
boolProp.selected = not boolProp.selected
instance[prop.property] = boolProp.selected
savePoint()
end)
elseif isInstance(value) then
--TODO: Allow user to select instance using explorer...
local placeholder = generateLabel(" . " .. propertyType .. " . ", propContainer)
placeholder.position = guiCoord(0,0,0,0)
placeholder.size = guiCoord(1, 0, 1, 0)
placeholder.align = enums.align.middle
placeholder.backgroundAlpha = 0.6
elseif propertyType == "number" then
local txtProp = generateInputBox(value, propContainer)
txtProp.name = "number"
txtProp.position = guiCoord(0,1,0,0)
txtProp.size = guiCoord(1, 0, 1, 0)
setReadOnly(txtProp, readOnly)
txtProp:textInput(function(value) -- Only fires when a user types in the box.
instance[prop.property] = tonumber(value)
if tonumber(value) then savePoint() end
end)
else
local txtProp = generateInputBox(value, propContainer)
txtProp.name = "input"
txtProp.position = guiCoord(0,1,0,0)
txtProp.size = guiCoord(1, 0, 1, 0)
setReadOnly(txtProp, readOnly)
txtProp:textInput(function(value) -- Only fires when a user types in the box.
instance[prop.property] = value
savePoint()
end)
end
y = y + 22
::continue::
end
scrollViewProperties.canvasSize = guiCoord(1,0,0,y+80)
end)
end
generateProperties(txtProperty)
--
-- Workshop Camera
-- Altered from https://wiki.teverse.com/tutorials/base-camera
--
-- The amount the camera moves when you use the scrollwheel
local zoomStep = 3
local rotateStep = -0.0045
local moveStep = 0.5 -- how fast the camera moves
local camera = workspace.camera
-- Setup the initial position of the camera
camera.position = vector3(11, 5, 10)
camera:lookAt(vector3(0,0,0))
-- Camera key input values
local cameraKeyEventLooping = false
local cameraKeyArray = {
[enums.key.w] = vector3(0, 0, -1),
[enums.key.s] = vector3(0, 0, 1),
[enums.key.a] = vector3(-1, 0, 0),
[enums.key.d] = vector3(1, 0, 0),
[enums.key.q] = vector3(0, -1, 0),
[enums.key.e] = vector3(0, 1, 0)
}
engine.input:mouseScrolled(function( input )
if input.systemHandled then return end
local cameraPos = camera.position
cameraPos = cameraPos + (camera.rotation * (cameraKeyArray[enums.key.w] * input.movement.y * zoomStep))
camera.position = cameraPos
end)
engine.input:mouseMoved(function( input )
if engine.input:isMouseButtonDown( enums.mouseButton.right ) then
local pitch = quaternion():setEuler(input.movement.y * rotateStep, 0, 0)
local yaw = quaternion():setEuler(0, input.movement.x * rotateStep, 0)
-- Applied seperately to avoid camera flipping on the wrong axis.
camera.rotation = yaw * camera.rotation;
camera.rotation = camera.rotation * pitch
--updatePosition()
end
end)
savePoint() -- Create a point.
--
-- Selection System
--
--testing purposes
local newBlock = engine.block("base")
newBlock.colour = colour(1,1,1)
newBlock.size = vector3(100,1,100)
newBlock.position = vector3(0,-1,0)
newBlock.parent = workspace
local newBlock = engine.block("block1")
newBlock.colour = colour(1,0,0)
newBlock.size = vector3(1,1,1)
newBlock.position = vector3(0,0,0)
newBlock.parent = workspace
local newBlock = engine.block("block2")
newBlock.colour = colour(0,1,0)
newBlock.size = vector3(1,1,1)
newBlock.position = vector3(0,1,0)
newBlock.parent = workspace
-- This block is used to show an outline around things we're hovering.
local outlineHoverBlock = engine.block("workshopHoverOutlineWireframe")
outlineHoverBlock.wireframe = true
outlineHoverBlock.static = true
outlineHoverBlock.physics = false
outlineHoverBlock.colour = colour(1, 1, 0)
outlineHoverBlock.opacity = 0
-- This block is used to outline selected items
local outlineSelectedBlock = engine.block("workshopSelectedOutlineWireframe")
outlineSelectedBlock.wireframe = true
outlineSelectedBlock.static = true
outlineSelectedBlock.physics = false
outlineSelectedBlock.colour = colour(0, 1, 1)
outlineSelectedBlock.opacity = 0
local selectedItems = {}
local function validateItems()
for i,v in pairs(selectedItems) do
if not v or v.isDestroyed then
table.remove(selectedItems, i)
end
end
end
local focusOnObjectInHierarchy, setHierarchyPartOpened, highlightInstanceInHierarchy;
local hierarchy = { }
local txtDebug = generateLabel("...", engine.workshop.interface)
txtDebug.name = "txtDebug"
txtDebug.position = guiCoord(0,0,1,-16)
local profile = 0
local lastframes = 0
local fps = 0
engine.graphics:frameDrawn(function(events, frameNumber)
local current = os.clock()
local timeSpent = current - profile
if timeSpent >= 1 then
fps = (frameNumber - lastframes)
profile = current
lastframes = frameNumber
end
txtDebug.text = "Lua Frame: " .. frameNumber .. " | Handling " .. events .. " events. Lua FPS: " .. fps
local mouseHit = engine.physics:rayTestScreen( engine.input.mousePosition ) -- accepts vector2 or number,number
if mouseHit then
outlineHoverBlock.size = mouseHit.object.size
outlineHoverBlock.position = mouseHit.object.position
outlineHoverBlock.opacity = 1
else
outlineHoverBlock.opacity = 0
end
end)
local renderHierarchy;
local function updateBounding( )
if #selectedItems > 1 then
outlineSelectedBlock.opacity = 1
local i = 1
while selectedItems[i].className ~= "block" and i <= #selectedItems do
i = i + 1
end
if i ~= #selectedItems then
-- used to calculate bounding box area...
local upper = selectedItems[i].position + (selectedItems[i].size/2) or vector3(0.1, 0.1, 0.1)
local lower = selectedItems[i].position - (selectedItems[i].size/2) or vector3(0.1, 0.1, 0.1)
for i, v in pairs(selectedItems) do
if type(v.size) == "vector2" and type(v.position) == "vector3" then
local topLeft = v.position + (v.size/2)or vector3(0.1, 0.1, 0.1)
local btmRight = v.position - (v.size/2)or vector3(0.1, 0.1, 0.1)
upper.x = math.max(topLeft.x, upper.x)
upper.y = math.max(topLeft.y, upper.y)
upper.z = math.max(topLeft.z, upper.z)
lower.x = math.min(btmRight.x, lower.x)
lower.y = math.min(btmRight.y, lower.y)
lower.z = math.min(btmRight.z, lower.z)
end
end
outlineSelectedBlock.position = (upper+lower)/2
outlineSelectedBlock.size = upper-lower
else
outlineSelectedBlock.opacity = 0
end
elseif #selectedItems == 1 and selectedItems[1].className == "block" then
outlineSelectedBlock.opacity = 1
outlineSelectedBlock.position = selectedItems[1].position
outlineSelectedBlock.size = selectedItems[1].size or vector3(0.1, 0.1, 0.1)
elseif #selectedItems == 0 then
print("op 0")
outlineSelectedBlock.opacity = 0
end
txtProperty.text = #selectedItems .. " item" .. (#selectedItems == 1 and "" or "s") .. " selected"
end
engine.input:mouseLeftReleased(function( input )
if input.systemHandled or disableDefaultClickActions then return end
validateItems()
local mouseHit = engine.physics:rayTestScreen( engine.input.mousePosition )
if not mouseHit then
-- User clicked empty space, deselect everything??
selectedItems = {}
outlineSelectedBlock.opacity = 0
txtProperty.text = "Nothing selected"
renderHierarchy()
generateProperties( nil )
--[[for btn,v in pairs(hierachy) do
if btn.btn.backgroundColour ~= themeColourButton then
btn.btn.backgroundColour = themeColourButton
end
end ]]
return
end
local doSelect = true
if not engine.input:isKeyDown(enums.key.leftShift) then
-- deselect everything and move on
selectedItems = {}
--[[for btn,v in pairs(hierachy) do
if btn.btn.backgroundColour ~= themeColourButton then
btn.btn.backgroundColour = themeColourButton
end
end ]]
else
for i,v in pairs(selectedItems) do
if v == mouseHit.object then
-- deselect
table.remove(selectedItems, i)
doSelect = false
end
end
end
if doSelect then
-- focusOnObjectInHierarchy(mouseHit)
table.insert(selectedItems, mouseHit.object)
highlightInstanceInHierarchy(mouseHit.object)
generateProperties(mouseHit.object)
end
updateBounding()
renderHierarchy()
end)
-- Output is currently here for ease of testing.
local windowOutput = engine.guiWindow()
windowOutput.size = guiCoord(0.7, -240, 0, 166)
windowOutput.position = guiCoord(0.3, 0, 1, -166)
windowOutput.parent = engine.workshop.interface
windowOutput.text = "Output Console"
windowOutput.name = "windowOutput"
windowOutput.draggable = true
windowOutput.fontSize = 10
windowOutput.guiStyle = enums.guiStyle.windowNoCloseButton
windowOutput.backgroundColour = themeColourWindow
windowOutput.fontFile = normalFontName
windowOutput.textColour = themeColourWindowText
local codeInputBox = engine.guiTextBox()
codeInputBox.parent = windowOutput
codeInputBox.size = guiCoord(1, 0, 0, 23)
codeInputBox.position = guiCoord(0, 0, 0, 0)
codeInputBox.backgroundColour = themeColourButton
codeInputBox.fontSize = 8
codeInputBox.fontFile = normalFontName
codeInputBox.text = " "
codeInputBox.readOnly = false
codeInputBox.wrap = true
codeInputBox.multiline = false
codeInputBox.align = enums.align.middleLeft
local outputLines = {}
local scrollViewOutput = engine.guiScrollView("scrollView")
scrollViewOutput.size = guiCoord(1,-20,1,-25)
scrollViewOutput.parent = windowOutput
scrollViewOutput.position = guiCoord(0,10,0,25)
scrollViewOutput.guiStyle = enums.guiStyle.noBackground
scrollViewOutput.canvasSize = guiCoord(1,0,0,110)
local lbl = engine.guiTextBox()
lbl.size = guiCoord(1, -10, 0, 50)
lbl.position = guiCoord(0, 0, 0, 0)
lbl.guiStyle = enums.guiStyle.noBackground
lbl.fontSize = 9
lbl.fontFile = normalFontName
lbl.text = "Test output, lacks stuff."
lbl.readOnly = true
lbl.wrap = true
lbl.multiline = true
lbl.align = enums.align.topLeft
lbl.parent = scrollViewOutput
lbl.textColour = colour(1, 1, 1)
local lastCmd = ""
codeInputBox:keyPressed(function(inputObj)
if inputObj.key == enums.key['return'] then
if (codeInputBox.text == "clear" or codeInputBox.text == " clear") then