-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.lua
4623 lines (3688 loc) · 146 KB
/
main.lua
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
main = {}
config = {
title="NESBuilder",
width=850,
height=700,
cellWidth=24,
cellHeight=24,
buttonWidth=20,
buttonHeight=26,
buttonWidthSmall=4,
aboutURL = "https://github.com/SpiderDave/NESBuilder#readme",
bitcoinURL = "bitcoin:1ZdyKcXeqbbBp9yFxpEdpCMwSwccR2Cey",
moneroURL = "monero:831QMtQnoJJPik5Jtbz5wPhjDDM5JnMFVh5G7z834gTbLyHo4TcE83KBi4Co7FoBSi3V4j7VQpHWi5ZEckBZqhjCAA4BowV",
colors = {
bk='#202036',
bk2='#303046',
bk3='#404050', -- text background
bk4='#656570',
bk_hover='#454560',
fg = '#eef',
menuBk='#404056',
bk_menu_highlight='#606080',
bk_highlight='#404060',
tkDefault='#656570',
link='white',
linkHover='#88f',
borderLight='#56565a',
borderDark='#101020',
textInputBorder='#99a',
},
pluginFolder = "plugins", -- this one is for python
nRecentFiles = 12,
nRecentFiles_menu = 4,
defaultAssembler = 'sdasm',
}
config.buttonWidthNew = 20*7.5
config.buttonHeightNew = 26*7.5
config.pad = 6
config.left = config.pad*1.5
config.top = config.pad*1.5
config.launchText=[[
Created by SpiderDave
-------------------------------------------------------------------------------------------
NESBuilder is a NES development and romhacking tool made with
Python and Lua.
The goal of NESBuilder is to make NES development and romhacking
easier, helping to create a NES game from start to finish or
modify an existing game easily.
Features:
* Open source
* Integrated custom assembler
* Palette editor
* CHR Import/export and editing.
* Metatiles
* plugin system (Lua/Python)
]]
-- global stacks
stack, push, pop = NESBuilder:newStack()
recentProjects = NESBuilder:newStack{maxlen=config.nRecentFiles}
pad,left,top=config.pad, config.left, config.top
data = {}
data.projectTypes = {
{name = "dev", text = "NES Game", helpText="Create a new NES game."},
{name = "romhack", text = "Rom Hack", helpText="Modify an existing NES Rom."},
}
-- Override print with something custom.
_print = print
print = function(...) NESBuilder:print(...) end
makeHex = function(n)
if NESBuilder:cfgGetValue("main", "upperhex")==1 then
return string.format("%02X",n)
else
return string.format("%02x",n)
end
end
data.selectedColor = 0
data.palettes = {}
data.palettes = {
index=0,
[0]={0x0f,0x21,0x11,0x01},
{0x0f,0x26,0x16,0x06},
{0x0f,0x29,0x19,0x09},
{0x0f,0x30,0x10,0x00},
}
data.projectID = "project1" -- this determines the folder that it will load the project data from
data.folders = {
projects = "projects/",
tools = "tools/",
templates = "templates/",
plugins = config.pluginFolder.."/",
palettes = "palettes/",
}
data.project = {chr={}}
data.assemblers = {'sdasm','asm6','xkasplus'}
data.compression = {}
function init()
local x,y,pad
local control, b, c
local top,left
pad=6
top = config.top
left = config.left
x,y=left,top
print("init")
-- load default palette file if found
local f = NESBuilder:findFile(NESBuilder:cfgGetValue("main", "defaultPaletteFile"), list(NESBuilder:cfgGetValue("main", "defaultPaletteFolder")))
NESBuilder.palette.load(f)
local buttonWidth = config.buttonWidth*7.5
local buttonHeight = 27
-- local main = NESBuilder:getWindowQt()
-- main.tabParent.self.setTabsClosable(false)
-- main.update()
NESBuilder:incLua("Tserial")
util = NESBuilder:incLua("util")
ipairs_sparse = util.ipairs_sparse
-- make sure projects folder exists
--NESBuilder:makeDir(data.folders.projects)
statusBar=NESBuilder:makeLabelQt{x=x,y=y,text="Status bar"}
statusBar.setFont("Verdana", 10)
-- This dummy tab fixes an issue with create-on-demand tabs showing up
-- blank if all other tabs are closed.
-- We hide it at the end of this init()
local dummyTab = NESBuilder:makeTab{x=x,y=y,w=config.width,h=config.height,name="dummy",text="dummy", showInList = false}
if cfgGet('alphawarning')==1 then
control = NESBuilder:makeTab{x=x,y=y,w=config.width,h=config.height,name="Warning",text="Warning"}
end
control = NESBuilder:makeTab{x=x,y=y,w=config.width,h=config.height,name="tabLog",text="Log"}
control = NESBuilder:makeTab{x=x,y=y,w=config.width,h=config.height,name="Launcher",text="Launcher"}
control = NESBuilder:makeTab{x=x,y=y,w=config.width,h=config.height,name="Palette",text="Palette"}
control = NESBuilder:makeTab{x=x,y=y,w=config.width,h=config.height,name="Image",text="CHR"}
control = NESBuilder:makeTab{x=x,y=y,w=config.width,h=config.height,name="Symbols",text="Symbols"}
local items
-- The separator and Quit items will be added
-- in onReady() so plugins can add items before them.
items = {
{name="New", text="\u{1f4c1} New Project"},
{name="Open", text="\u{1f4c2} Open Project"},
{name="Save", text="\u{1f4be} Save Project"},
{name="SaveAs", text="\u{1f4be} Save As"},
{name="Preferences", text="\u{2699}\u{fe0f} Preferences"},
}
control = NESBuilder:makeMenuQt{name="menuFile", text="File", menuItems=items}
control = NESBuilder:makeMenuQt{name="menuEdit", text="Edit", menuItems={}}
--control.control.setEnable(false)
--local f = python.eval("lambda x: x[0]")
--print(control)
--print(f(control))
items = {
{name="Build", text="\u{27A0} Build"},
{name="BuildTest", text="\u{27A0} Build and Test"},
{name="TestRom", text="\u{27A5} Re-Test Last"},
}
control = NESBuilder:makeMenuQt{name="menuProject", text="Project", menuItems=items}
if cfgGet('alphawarning')==1 then
NESBuilder:setTabQt("Warning")
x,y=left,top
text=[[
WARNING:
This project is in alpha stage. It's going well but
some things may be broken or unstable. Make frequent
backups.
You can disable this warning tab in preferences.
--SpiderDave
]]
control=NESBuilder:makeLabelQt{x=x,y=y,text=text}
control.setFont("Verdana",12)
x=left+pad*8
y = y + control.height + pad*8
b=NESBuilder:makeButton{x=x,y=y,w=100,h=buttonHeight,name="buttonWarningClose",text="close"}
end
local items = {}
control = NESBuilder:makeMenuQt{name="menuView",text="View", menuItems=items}
NESBuilder:setTabQt("tabLog")
x,y = left, top
control = NESBuilder:makeConsole{x=x,y=y,w=750,h=600,name="log", text=""}
console = control
local oldPrint = print
print = function(...)
oldPrint(...)
console.print(NESBuilder:getPrintable(...))
end
handlePythonError = function(err, e)
print(string.rep('-', 79))
print(e)
print(string.rep('-', 79))
return true
end
handleLuaError = function(e)
print(string.rep('-', 80))
print("LuaError:\n")
print(e)
print()
print(string.rep('-', 80))
return true
end
--control=NESBuilder:makeButton{x=x,y=y,w=100,h=buttonHeight,name="testError",text="Test"}
NESBuilder:setTabQt("Image")
x,y=config.left, config.top
control=NESBuilder:makeCanvasQt{x=x,y=y,w=128,h=128,name="canvasQt", scale=3}
control.helpText = "Click to select a tile"
x=x+control.width
y=y+control.height+pad
control = NESBuilder:makeCanvasQt{x=x,y=y,w=8,h=8,name="canvasTile", scale=10}
control.helpText = "Left-click: apply color, right-click: select color"
control.setCursor('pencil')
-- right align it with the above canvas
control.move(x-control.width,y)
x=x-control.width
y=y+control.height + pad
local itemList = {
"Cut",
"Copy",
"Paste",
"\u{2194} Flip Tile Horizontally",
"\u{2195} Flip Tile Vertically",
"Shift Tile Up",
"Shift Tile Down",
"Shift Tile Left",
"Shift Tile Right",
}
control = NESBuilder:makeMenuBox{x=x,y=y, name="buttonCanvasTileActions", text="actions \u{25bc}", itemList = itemList}
control.helpText = "Tile actions"
x,y = left,top
NESBuilder:setTabQt("Palette")
NESBuilder:setDirection("h")
x,y=left,top
p = {[0]=0x0f,0x21,0x11,0x01}
control=NESBuilder:makePaletteControlQt{x=x,y=y,cellWidth=config.cellWidth,cellHeight=config.cellHeight, name="PaletteQt", palette=getNESPalette(), upperHex=cfgGet('upperhex')}
control.helpText = "Click to select a color"
palette = {}
for i=0,#p do
palette[i] = getNESPalette()[p[i]]
end
placeX = left
placeY = top
x=left
y=y+control.height+pad
local buttonHeight = config.buttonHeight
--control = NESBuilder:makeSideSpin{x=x,y=y,w=buttonHeight*3,h=buttonHeight, name="SpinChangePalette"}
--x = left
--y = y + control.height + pad
-- p = {[0]=0x0f,0x21,0x11,0x01}
-- palette = {}
-- for i=0,#p do
-- palette[i] = getNESPalette()[p[i]]
-- end
-- control=NESBuilder:makePaletteControlQt{x=x,y=y,cellWidth=config.cellWidth,cellHeight=config.cellHeight, name="PaletteEntryQt", palette=palette}
-- control.helpText = "Click to apply a color, right click to select a color"
--push(y+control.height+pad)
-- x=x+control.width+pad
-- c=NESBuilder:makeLabelQt{x=x,y=y+pad,name="PaletteEntryLabelQt",clear=true,text="foobar"}
-- c.setFont("Verdana", 10)
-- x=left
-- y=pop()
-- for i=0,#p do
-- palette[i] = getNESPalette()[p[i]]
-- end
y=y+pad
local itemList = {
"New",
"Rename",
"Delete",
"Move Up",
"Move Down",
"Duplicate",
}
control = NESBuilder:makeMenuBox{x=x,y=y, name="paletteSetActions", text="actions \u{25bc}", itemList = itemList}
control.helpText = "Palette set actions"
y = y + control.height + pad
control = NESBuilder:makeComboBox{x=x,y=y,w=400, name="paletteSet", itemList = {'set 0','set 1'}}
control.helpText = "Select a palette set"
y = y + control.height + pad
x = left
local scroll = NESBuilder:makeScrollFrame{x=x,y=y,w=400,h=400,name='scrollArea1'}
NESBuilder:setContainer(scroll.frame)
y=pad
x=left
paletteControl = {}
for i = 0,15 do
paletteControl[i] = {}
c = NESBuilder:makeLabelQt{x=x,y=y+pad,clear=true, text=string.format(' %02x', i), name=string.format('paletteLabelLeft%02x', i),functionName = 'paletteLabelLeft', index=i}
c.setFont("Verdana", 10)
x = x + c.width + pad
paletteControl[i].labelControlLeft = c
control=NESBuilder:makePaletteControlQt{x=x,y=y,cellWidth=config.cellWidth,cellHeight=config.cellHeight, name=string.format('Palette%02x',i), palette=palette}
paletteControl[i].control = control
paletteControl[i].index = i
x = x + control.width + pad
c = NESBuilder:makeLabelQt{x=x,y=y+pad,clear=true, text='00', name=string.format('paletteLabel%02x', i), functionName = 'paletteLabel', index=i}
c.setFont("Verdana", 10)
paletteControl[i].labelControl = c
y=y + control.height
x = left
main[control.name..'_cmd'] = function(t)
local f = main.Palette_cmd or Palette_cmd
if f then return f(t, i) end
end
end
if devMode() then
NESBuilder:setTabQt("Palette")
y = scroll.y() + scroll.height + pad
x = left
control = NESBuilder:makeButton{x=x,y=y,w=buttonWidth,name="test",text="test"}
y = y + control.height + pad
-- local control = pythonEval('QtDave.QComboBox')(NESBuilder:getTabQt())
-- control.move(x,y)
-- control.resize(buttonWidth, buttonHeight)
-- control.addItems(list('item 1', 'item 2', 'item 3'))
end
NESBuilder:setTabQt("Image")
p = {[0]=0x0f,0x21,0x11,0x01}
palette = {}
for i=0,#p do
palette[i] = getNESPalette()[p[i]]
end
x=pad
y=pad+128*3+pad*1.5
placeY = y
c=NESBuilder:makePaletteControlQt{x=x,y=y,cellWidth=config.cellWidth,cellHeight=config.cellHeight, name="CHRPalette", palette=palette}
x=left + 120
y=placeY
b=NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthSmall*7.5,name="ButtonPrevPaletteCHR",text="<"}
x=x+b.width+pad
y=placeY
b=NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthSmall*7.5,name="ButtonNextPaletteCHR",text=">"}
x=left
y = y + 32 + pad
b=NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthNew,name="LoadCHRImage",text="Load Image"}
y = y + b.height + pad
b=NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthNew,name="LoadCHRNESmaker",text="Load NESmaker Image"}
y = y + b.height + pad
b=NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthNew,name="LoadCHR",text="Load .chr"}
y = y + b.height + pad
b=NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthNew,name="ExportCHR",text="Export"}
y = y + b.height + pad
if devMode() then
b=NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthNew,name="test2",text="test"}
y = y + b.height + pad
end
x=128*3+pad*3
y=top
c=NESBuilder:makeLabelQt{x=x,y=y,name="CHRNumLabel",clear=true,text="CHR"}
y = y + buttonHeight
--y=top
x=128*3+pad*3
-- Yes, we're cheating with Python here.
local l = python.eval('["CHR {0:02n}".format(x) for x in range(8)]')
local menu = {
{name='add new', action = addCHR_cmd},
{name='rename', action = notImplemented},
{name='delete', action = delCHR},
}
control = NESBuilder:makeList{x=x,y=y,w=buttonWidth,h=buttonHeight*12, name="CHRList",list = l, contextMenuItems = menu}
control.helpText = "left-click: Select a CHR set, right-click: context menu"
push(y + control.height + pad, x)
x=x + control.width + pad*.5
b=NESBuilder:makeButton{x=x,y=y,w=30,name="addCHR",text="+"}
x,y = pop(2)
control = NESBuilder:makeLineEdit{x=x,y=y,w=control.width,h=inputHeight, name="CHRName"}
y = y + control.height + pad
NESBuilder:setTabQt("Symbols")
left = config.left
top = config.top
x,y = left,top
control = NESBuilder:makeTable{x=x,y=y,w=buttonWidth*5,h=buttonHeight*20, name="symbolsTable1",rows=100, columns=3}
control.setHorizontalHeaderLabels("Symbol", "Value", "Comment")
control.setColumnWidth(0,buttonWidth)
control.setColumnWidth(1,buttonWidth)
control.horizontalHeader().setStretchLastSection(true)
y = y + control.height + pad
NESBuilder:setTabQt("Launcher")
x,y = left,top
local startY
control = NESBuilder:makeLabelQt{x=x,y=y,w=buttonWidth, name="launcherRecentTitle",text="NESBuilder"}
control.setFont("Verdana", 28)
push(y+control.height+pad, x)
x = x + control.width+pad
y = y + pad*2
control = NESBuilder:makeLabelQt{x=x,y=y,w=buttonWidth, name="launcherProjectName", text="test"}
control.setFont("Verdana", 14)
x,y=pop(2)
--y = y + control.height + pad
push(y)
control = NESBuilder:makeButton{x=x,y=y,w=190,h=64, name="launcherButtonRecent",text="Recent Projects", image="icons/clock32.png", iconMod=true}
y = y + control.height + pad
control = NESBuilder:makeButton{x=x,y=y,w=190,h=64, name="launcherButtonOpen",text="Open Project", image="icons/folder32.png", iconMod=true}
y = y + control.height + pad
control = NESBuilder:makeButton{x=x,y=y,w=190,h=64, name="launcherButtonNew",text="New Project", image="icons/folderplus32.png", iconMod=true}
y = y + control.height + pad
control = NESBuilder:makeButton{x=x,y=y,w=190,h=64, name="launcherButtonTemplates",text="Templates", image="icons/folder32.png", iconMod=true}
y = y + control.height + pad
control = NESBuilder:makeButton{x=x,y=y,w=190,h=64, name="launcherButtonPreferences",text="Preferences", image="icons/gear32.png", iconMod=true}
y = y + control.height + pad
control = NESBuilder:makeButton{x=x,y=y,w=190,h=64, name="launcherButtonInfo",text="About", image="icons/note32.png", iconMod=true}
y = y + control.height + pad
x=x+control.width+pad*2
y=top
startY=pop()
data.launchFrames = {
recent = NESBuilder:makeFrame{x=x,y=startY,w=buttonWidth*5,h=config.height, name="frameRecentProjects"},
open = NESBuilder:makeFrame{x=x,y=startY,w=buttonWidth*5,h=config.height, name="frameOpenProjects"},
new = NESBuilder:makeFrame{x=x,y=startY,w=buttonWidth*5,h=config.height, name="frameNewProject"},
templates = NESBuilder:makeFrame{x=x,y=startY,w=buttonWidth*5,h=config.height, name="frameTemplates"},
pref = NESBuilder:makeFrame{x=x,y=startY,w=buttonWidth*5,h=config.height, name="framePreferences"},
about = NESBuilder:makeFrame{x=x,y=startY,w=buttonWidth*5,h=config.height, name="frameAbout"},
set = function(f)
for k,v in pairs(data.launchFrames) do
if k == "set" then
elseif k == f then
v.show()
else
v.hide()
end
end
end,
}
NESBuilder:setContainer(data.launchFrames.recent)
local columns = 3
local rows = 4
local iconWidth = 180
recentData = {}
for i = 1, config.nRecentFiles do
recentData[i-1]={}
x = ((i-1) % columns)*(iconWidth+15) + pad
y = math.floor((i-1)/columns)*130
control = NESBuilder:makeLauncherIcon{x=x,y=y,h=120, w=iconWidth, name="launcherRecentIcon", index=i-1}
recentData[i-1].icon = control
y=y+control.height-25
control = NESBuilder:makeLabelQt{x=x,y=y, name="launcherRecentLabel",text="", class="launcherText"}
control.width = iconWidth
control.height=20
recentData[i-1].label = control
end
NESBuilder:setContainer(data.launchFrames.new)
for i, item in ipairs(data.projectTypes) do
--recentData[i-1]={}
x = ((i-1) % columns)*(iconWidth+15) + pad
y = math.floor((i-1)/columns)*130
control = NESBuilder:makeLauncherIcon{x=x,y=y,h=120, w=iconWidth, name="launcherProjectType", index=i-1}
control.helpText = item.helpText
y=y+control.height-25
control = NESBuilder:makeLabelQt{x=x,y=y, name="launcherRecentLabel", text=item.text, class="launcherText"}
control.width = iconWidth
control.height=20
end
NESBuilder:setContainer(data.launchFrames.templates)
local i = 1
local baseFilename
local name
for f in python.iter(NESBuilder:files(data.folders.templates..'*.template.zip')) do
baseFilename = pathSplit(f)[1]
name = nil
local templateConfig = NESBuilder:getTextFromArchive(f, 'template.ini')
if templateConfig then
name = NESBuilder:parseTemplateData(templateConfig, 'main', 'name')
end
if not name then
name = pathSplit(f)[1]
name = rsplit(name, '.template.zip')[0]
end
x = ((i-1) % columns)*(iconWidth+15) + pad
y = math.floor((i-1)/columns)*130
control = NESBuilder:makeButton{x=x,y=y,w=iconWidth, h=120, name="launcherTemplate",text=name, index=i-1, class="templateButton", filename=baseFilename}
--control.setIconFromArchive(data.folders.templates..'smb.template.zip', 'templateicon.png')
control.setIconFromArchive(f, 'templateicon.png')
if templateConfig then
control.helpText = NESBuilder:parseTemplateData(templateConfig, 'main', 'helptext')
end
--control.helpText = item.helpText
y=y+control.height-25
i=i+1
end
NESBuilder:makeTab{name="Metatiles", text="Metatiles"}
NESBuilder:setTabQt("Metatiles")
x,y=left,top
push(y)
control = NESBuilder:makeCanvasQt{x=x,y=y,w=128,h=128,name="tsaCanvasQt", scale=2}
control.helpText = "Click to select a tile"
push(x+control.width+pad)
x = left
y=y + control.height + pad
control = NESBuilder:makeCanvasQt{x=x,y=y,w=8,h=8,name="tsaTileCanvasQt", scale=8, columns=1, rows=1}
control.helpText = "Selected tile"
y=y + control.height + pad
x = pop()
y = pop()
push(y)
local menu = {
{name='add new', action = addMTileSet},
{name='rename', action = renameMTileSet},
}
control = NESBuilder:makeList{x=x,y=y,w=buttonWidth,h=buttonHeight*12, name="mTileList", list = list(), contextMenuItems = menu}
control.helpText = "left-click: Select a metatile set, right-click: context menu"
y = y + control.height + pad
local newX = x + control.width + pad
control = NESBuilder:makeLabelQt{x=x,y=y, clear=true, text="Address:"}
control.setFont("Verdana", 11)
push(x)
x = x + control.width + pad
control = NESBuilder:makeLineEdit{x=x,y=y,w=buttonWidth/2,h=inputHeight, name="MTileAddress"}
control.helpText = "Address for metatile set (optional)"
y = y + control.height + pad
x = pop()
--control = NESBuilder:makeCheckbox{x=x,y=y,name="mTileIncludePointers", text="Include pointers", value=bool(0)}
control = NESBuilder:makeCheckbox{x=x,y=y,name="mTileIncludePointers", text="Include pointers"}
control.helpText="Include a table of pointers to metatiles for this set"
data.mTileTypes = {}
y = y + control.height + pad
control = NESBuilder:makeComboBox{x=x,y=y,w=buttonWidth, name="mTileTypeList"}
control.helpText="metatile type."
y = pop()
x = newX
push(x)
push(y)
control = NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthSmall*7.5, name="metatilePrev",text="<"}
control.helpText = "Previous metatile"
y=pop()
x = x + control.width+pad
push(y)
control=NESBuilder:makeLineEdit{x=x,y=y,w=24,h=buttonHeight, name="metatileNumber",text="0"}
control.helpText = "Current metatile index"
y=pop()
x= x + control.width+pad
control = NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthSmall*7.5, name="metatileNext",text=">"}
control.helpText = "Next metatile"
y = y + control.height + pad
x=pop()
push(x)
control = NESBuilder:makeNumberEdit{x=x,y=y,w=config.buttonWidthSmall*7.5,h=inputHeight, name="metatileW", value = 2}
control.helpText = "Metatile width"
x = x + control.width + pad
control = NESBuilder:makeNumberEdit{x=x,y=y,w=config.buttonWidthSmall*7.5,h=inputHeight, name="metatileH", value = 2}
control.helpText = "Metatile height"
x=x+control.width+pad
control = NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthSmall*7.5, name="metatileNew",text="Set"}
control.helpText = "Create a new metatile (WIP)"
y = y + control.height + pad
x=pop()
control = NESBuilder:makeLabelQt{x=x,y=y, name="metatileName", text="name"}
control.helpText = "Metatile name. Left-click: Edit"
control.setFont("Verdana", 10)
control.autoSize = false
control.width = buttonWidth * 2
-- control.height=20
y = y + control.height + pad
control = NESBuilder:makeLabelQt{x=x,y=y, name="metatileOffset", text="offset: (0, 0)"}
control.setFont("Verdana", 10)
control.autoSize = false
control.width = buttonWidth * 2
y = y + control.height + pad
control = NESBuilder:makeButton{x=x,y=y,w=config.buttonWidthSmall*7.5*3, name="metatileOffsetTest",text="set offset"}
control.helpText = "Toggle Offset mode"
y = y + control.height + pad
control = NESBuilder:makeCanvasQt{x=x,y=y,w=16,h=16,name="tsaCanvas2Qt", scale=6}
control.helpText = "Left-click: apply tile, right-click: select tile"
y = y + control.height + pad
--x = x + control.width + pad
ppInit()
createAboutTab()
data.launchFrames.set('recent')
loadSettings()
local tabParent = NESBuilder:getWindowQt().tabParent
tabParent.setTabVisible(tabParent.indexOf(dummyTab), false)
end
function onPluginsLoaded()
handlePluginCallback("onInit")
handlePluginCallback("onRegisterAssembler")
handlePluginCallback("onRegisterCompression")
-- This one is for compression plugins to update their controls
handlePluginCallback("onUpdateCompression")
getControl('mTileTypeList').clear()
local control = getControl('mTileTypeList')
control.addItem('(none)')
for k, v in iterItems(data.mTileTypes) do
control.addItem(v.name)
end
end
function addMTileType(t)
data.mTileTypes[t.name] = t
getControl('mTileTypeList').clear()
local control = getControl('mTileTypeList')
control.addItem('(none)')
for k, v in iterItems(data.mTileTypes) do
control.addItem(v.name)
end
end
function removeMTileType(n)
data.mTileTypes[n] = nil
getControl('mTileTypeList').clear()
local control = getControl('mTileTypeList')
control.addItem('(none)')
for k, v in iterItems(data.mTileTypes) do
control.addItem(v.name)
end
end
function onReady()
local k, control
local items = {}
-- add recent files
items[#items+1] = {text="-"}
for i,v in python.enumerate(reverseList(recentProjects.asList())) do
k = string.format("recentproject%d", i+1)
items[#items+1] = {name = k, text = v, index = i, action = function() recentproject_cmd({index=i}) end}
if i >= config.nRecentFiles_menu then break end
end
items[#items+1] = {text="-"}
items[#items+1] = {name="Quit", text="\u{274e} Exit"}
control = NESBuilder:makeMenuQt{name="menuFile", text="File", menuItems=items}
local items = {
{text="-"},
{name="openProjectFolder", text="\u{1f4c2} Open Project Folder"},
{name="projectProperties", text="\u{2699} Project Properties"},
}
control = NESBuilder:makeMenuQt{name="menuProject", text="Project", menuItems=items}
local items = {
{name="importAllChr", text="\u{1f4c4}\u{20d6} Import all CHR from .nes"},
{name="exportAllChr", text="\u{1f4c4}\u{20d7} Export all CHR to .nes"},
{name="importMultiChr", text="\u{1f4c4}\u{20d6} Import all CHR from .chr"},
}
control = NESBuilder:makeMenuQt{name="menuTools",text="Tools", menuItems=items}
local items = {
{name="About", text="\u{2753} About"},
}
control = NESBuilder:makeMenuQt{name="menuHelp", text="Help", menuItems=items}
local items = {}
local control = NESBuilder:getWindowQt()
for k, v in iterItems(control.tabs) do
table.insert(items, {name=k, text=v.title, action = function() toggleTab(k, nil, true) end, checked = true})
end
control = NESBuilder:makeMenuQt{name="menuView",text="View", menuItems=items}
handlePluginCallback("onReady")
LoadProject()
if cfgGet('autosave')==1 then
local main = NESBuilder:getWindowQt()
-- minimum auto save interval is 45 seconds
main.setTimer(math.max(cfgGet('autosaveinterval'), 1000*45), autoSave, true)
end
if getControl(data.project.savedTab) then
NESBuilder:switchTab(data.project.savedTab)
else
NESBuilder:switchTab("Launcher")
end
end
function toggleTab(n, visible, switchTo)
local control = NESBuilder:getWindowQt()
local tab = control.getTab(n)
if not tab then return end
if visible~=true and visible~=false then
visible = control.tabParent.isTabVisible(control.tabParent.indexOf(tab))
else
visible = not visible
end
if visible then
control.menus['menuView'].actions[n].setChecked(false)
control.tabParent.removeTab(control.tabParent.indexOf(tab))
else
control.menus['menuView'].actions[n].setChecked(true)
if control.tabParent.isTabVisible(control.tabParent.indexOf(tab)) then
-- already visible, no need to add
else
control.tabParent.insertTab(tab.index, tab, tab.title)
end
if switchTo then NESBuilder:switchTab(n) end
end
end
function handlePluginCallback(f, arg)
local keys={}
for k,v in pairs(plugins or {}) do
table.insert(keys,k)
end
table.sort(keys)
for _,n in pairs(keys) do
_getPlugin = function() return plugins[n] end
if plugins[n][f] then
if not plugins[n].hide then
print(string.format("(Plugin %s): %s",n,f))
if type(arg) == 'table' then
plugins[n][f](table.unpack(arg))
else
plugins[n][f](arg)
end
end
end
end
end
-- similar to handlePluginCallback but specific to compression
-- aslo note it returns the first one it can
function compress(method, arg)
local keys={}
arg = arg or {}
local f = arg.f or "compress" -- function name
for k,v in pairs(plugins or {}) do
table.insert(keys,k)
end
table.sort(keys)
for _,n in pairs(keys) do
_getPlugin = function() return plugins[n] end
if plugins[n][f] then
if not plugins[n].hide then
print(string.format("(Plugin %s): %s",n, f))
return plugins[n][f](method, arg)
-- if type(arg) == 'table' then
-- return plugins[n][f](method, table.unpack(arg))
-- else
-- return plugins[n][f](method, arg)
-- end
end
end
end
end
function decompress(method, arg)
arg = arg or {}
arg.f = "decompress" -- function name
return compress(method, arg)
end
function loadSettings()
data.projectID = NESBuilder:cfgGetValue("main", "project", data.projectID)
local dupCheck = {}
-- load recent projects list
local k,v
for i=1, config.nRecentFiles do
k = string.format("recentproject%d", i)
v = NESBuilder:cfgGetValue("main", k, false)
if not v then break end
if not dupCheck[v] then
recentProjects.push(v)
dupCheck[v] = true
end
end
end
function saveSettings()
local k
NESBuilder:cfgSetValue("main", "project", data.projectID)
-- save recent projects list
for i,v in python.enumerate(recentProjects.asList()) do
k = string.format("recentproject%d", i+1)
NESBuilder:cfgSetValue("main", k,v)
end
end
function doCommand(t)
if type(t) == 'string' then
print("**** doCommand "..t)
else
if t.anonymous then return false end
local functionName = t.functionName or t.name or false
pcall(function()
t.event.button = t.event.event.button()
end)
if not t.anonymous then
print("doCommand "..functionName)
end
if functionName then
if t.plugin then
if t.plugin[functionName.."_cmd"] then
t.plugin[functionName.."_cmd"](t)
return false
end
elseif functionName then
if main[functionName.."_cmd"] then
main[functionName.."_cmd"](t)
return false
end
end
end
end
return true
end
function PaletteQt_cmd(t)
local event = t.cell.event
local badColor = {}
for _,v in pairs({0x0d,0x0e,0x1d,0x1e,0x1f,0x2e,0x2f,0x3e,0x3f}) do
badColor[v] = 0x0f
end
if event.button == 1 or event.button == 2 then
data.selectedColor = badColor[t.cellNum] or t.cellNum
print(string.format("Selected palette %02x", data.selectedColor))
end
end
function CHRPalette_cmd(t)
local event = t.cell.event
local p = currentPalette()
if event.button == 1 then
data.selectedColor = p[t.cellNum+1]
data.selectedColorIndex = t.cellNum
print(string.format("Selected palette %02x",data.selectedColor))
--data.drawColorIndex = t.cellNum