-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbaize.lua
More file actions
executable file
·1710 lines (1550 loc) · 43.5 KB
/
baize.lua
File metadata and controls
executable file
·1710 lines (1550 loc) · 43.5 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
-- baize
local bitser = require 'bitser'
local log = require 'log'
require 'gradient' -- comment out to not use gradient
local Pile = require 'pile'
local Settings = require 'settings'
local Util = require 'util'
require 'cardfactory'
-- local UI = require 'ui'
---@class (exact) Baize
---@field seconds number
---@field status "virgin"|"complete"|"afoot"|"stuck"
---@field stroke table
---@field script table
---@field piles Pile[]
---@field cells Cell[]
---@field discards Discard[]
---@field foundations Foundation[]
---@field reserves Reserve[]
---@field stock Stock
---@field tableaux Tableau[]
---@field waste Pile
---@field cardHeight number
---@field cardWidth number
---@field undoStack table
---@field deck table []Card
---@field dragStart table {x, y}
---@field dragOffset table {x, y}
---@field ui table
---@field stats table
---@field moves integer
---@field fmoves integer
---@field recycles integer
---@field bookmark integer
---@field showMovable boolean
---@field percent number
---@field backgroundCanvas table
---@field cardTextureLibrary table
---@field cardBackTexture table
---@field cardShadowTexture table
---@field cardRadius number
---@field labelFont love.Font
---@field new function
---@field __index Baize
local Baize = {
}
Baize.__index = Baize
function Baize.new()
local o = {}
o.dragOffset = {x=0, y=0}
o.dragStart = {x=0, y=0}
o.undoStack = {}
o.recycles = 32767
o.bookmark = 0
o.status = 'virgin' -- afoot, stuck, collect, complete
o.percent = 0
o.showMovable = false
o.moves = 0
o.fmoves = 0
o.seconds = 0
return setmetatable(o, Baize)
end
---@return table
function Baize:getSavable()
local piles = {}
for _, pile in ipairs(self.piles) do
table.insert(piles, pile:getSavable())
end
return {recycles=self.recycles, bookmark=self.bookmark, seconds=self.seconds, piles=piles}
end
---@param obj table
---@return boolean
function Baize:isSavable(obj)
local function isSavablePile(svp)
if type(svp) == 'table' then
-- label might be nil
if type(svp.category) == 'string' then
if type(svp.cards) == 'table' then
return true
end
end
end
log.error('not a saved pile')
return false
end
if type(obj) == 'table' then
if type(obj.recycles) == 'number' then
if type(obj.bookmark) == 'number' then
if type(obj.piles) == 'table' then
if #obj.piles > 0 then
if isSavablePile(obj.piles[1]) then
return true
end
end
end
end
end
end
log.error('not a saved baize')
return false
end
function Baize:createCardTextures()
self.cardTextureLibrary, self.cardBackTexture, self.cardShadowTexture = _G.cardTextureFactory(self.cardWidth, self.cardHeight, self.cardRadius)
end
---@param vname string
function Baize:loadScript(vname)
-- for v, _ in pairs(_G.LSOL_VARIANTS) do
-- log.info(v)
-- end
local vinfo = _G.LSOL_VARIANTS[vname]
if not vinfo then
log.error('Unknown variant', vname)
return nil
end
local fname = 'variants/' .. vinfo.file
local info = love.filesystem.getInfo('variants', 'directory')
if not info then
log.error('no variants directory')
return nil
end
info = love.filesystem.getInfo(fname, 'file')
if not info then
log.error('no file called', fname)
return nil
end
local ok, chunk, result
ok, chunk = pcall(love.filesystem.load, fname) -- load the chunk safely
if not ok then
log.error(tostring(chunk))
return nil
else
ok, result = pcall(chunk) -- execute the chunk safely
end
if not ok then -- will be false if there is an error
log.error(tostring(result))
return nil
end
return result.new(vinfo)
end
function Baize:resetPiles()
self.piles = {}
-- are these weak tables?
self.cells = {}
self.discards = {}
self.foundations = {}
self.reserves = {}
self.stock = nil
self.tableaux = {}
self.waste = nil
end
function Baize:resetState()
self.undoStack = {}
self.bookmark = 0
self.recycles = 32767
self.seconds = 0
end
--[[
function Baize:allCards()
local co = coroutine.create(function()
for _,pile in ipairs(self.piles) do
for _,c in ipairs(pile.cards) do
coroutine.yield(c)
end
end
end)
return function() -- iterator for generic for loop
local result, c = coroutine.resume(co)
return result == true and c or nil
end
end
]]
---@return table[] list of {dst=<pile>, tail=<tail>}
function Baize:findAllMovableTails()
local allTails = {} -- {dst=<pile>, tail=<tail>}
for _, pile in ipairs(self.piles) do
local pileTails = pile:movableTails()
if pileTails and #pileTails > 0 then
for _, t in ipairs(pileTails) do
table.insert(allTails, t)
end
end
end
--[[
for _, t in ipairs(tails) do
local c = t.tail[1]
log.info(c.parent.category, tostring(c), 'to', t.dst.category)
end
]]
return allTails
end
-- countMoves, called by updateStatus, sets card.movable, baize.moves and baize.fmoves
-- remind me, why calc fmoves? so we know when to enable collect button
function Baize:countMoves()
self.moves, self.fmoves = 0, 0
for _, c in ipairs(self.deck) do
c.movable = 0
end
if #self.stock.cards == 0 then
if self.recycles > 0 then
self.moves = self.moves + 1
end
else
self.moves = self.moves + 1
self.stock:peek().movable = 3 -- TODO set according to dst
end
for _, tail in ipairs(self:findAllMovableTails()) do
-- list of {dst=<pile>, tail=<tail>}
local movable = true
local card = tail.tail[1]
local src = tail.tail[1].parent
local dst = tail.dst
-- moving an entire pile between empty pile slots is pointless
if #dst.cards == 0 and #tail.tail == #src.cards then
if src.label == dst.label then
if src.category == dst.category then
movable = false
end
end
end
if movable then
self.moves = self.moves + 1
if dst.category == 'Cell' then
card.movable = math.max(card.movable, 1)
elseif dst.category == 'Discard' then
card.movable = math.max(card.movable, 5)
elseif dst.category == 'Foundation' then
self.fmoves = self.fmoves + 1
card.movable = math.max(card.movable, 5)
elseif dst.category == 'Tableau' then
if #dst.cards == 0 then
if dst.label == '' then
card.movable = math.max(card.movable, 1)
else
card.movable = math.max(card.movable, 2)
end
else
if dst:peek().suit == card.suit then
card.movable = math.max(card.movable, 4)
else
card.movable = math.max(card.movable, 3)
end
end
end
end
end
end
---@param saved table
function Baize:updateFromSaved(saved)
if #saved.piles ~= #self.piles then
log.error('saved piles do not match')
return
end
Util.play('undo')
for i = 1, #self.piles do
local pile = self.piles[i]
local savedPile = saved.piles[i]
-- pearl from the mudbank: always update all piles,
-- even if you think they're the same
pile:updateFromSaved(savedPile)
end
self.bookmark = saved.bookmark
self.ui:updateWidget('gotobookmark', nil, self.bookmark ~= 0)
self:setRecycles(saved.recycles)
if saved.seconds then
self.seconds = saved.seconds
end
end
function Baize:updateStatus()
self.moves, self.fmoves = 0, 0
if #self.undoStack == 1 then
self.status = 'virgin'
elseif #self.undoStack > 1 then
self.status = 'afoot'
end
if self.script:complete() then
self.status = 'complete'
else
self:countMoves()
if self.moves == 0 then
self.status = 'stuck'
else
self.status = 'afoot'
end
end
self.percent = self.script:percentComplete()
end
--[[
or
and
< > <= >= ~= ==
..
+ -
* / %
not # - (unary)
^
]]
function Baize:updateUI()
local anyProneCards = function()
for _, c in ipairs(self.deck) do
if c.prone then
return true
end
end
return false
end
if self.moves == 0 or self.showMovable == true then
self.ui:updateWidget('hint', nil, false)
else
self.ui:updateWidget('hint', nil, true)
end
self.ui:updateWidget('collect', nil, self.fmoves > 0)
local undoable = #self.undoStack > 1 and self.status ~= 'complete'
self.ui:updateWidget('undo', nil, undoable)
self.ui:updateWidget('restartdeal', nil, #self.undoStack > 1 --[[self.status ~= 'virgin']])
self.ui:updateWidget('gotobookmark', nil, self.bookmark ~= 0)
if self.stock:offScreen() then
self.ui:updateWidget('stock', '')
else
if self.waste then
self.ui:updateWidget('stock', string.format('%d:%d', #self.stock.cards, #self.waste.cards))
else
self.ui:updateWidget('stock', string.format('%d', #self.stock.cards))
end
end
if _G.SETTINGS.debug then
self.ui:updateWidget('status', string.format('(%s) %d - %s', self.status, #self.undoStack - 1, Util.formatSeconds(self.seconds)))
else
self.ui:updateWidget('status', string.format('%d - %s', #self.undoStack - 1, Util.formatSeconds(self.seconds)))
end
if self.status == 'complete' then
self.ui:updateWidget('progress', 'COMPLETE')
else
self.ui:updateWidget('progress', string.format('%d%%', self.percent))
end
-- TODO this doesn't belong here
if self.status == 'complete' then
self.ui:showFAB{icon='star', baizeCmd='newDeal'}
self:startSpinning()
self.stats:recordWonGame(_G.SETTINGS.variantName, #self.undoStack - 1, self.seconds)
elseif self.status == 'stuck' then
self.ui:toast(_G.SETTINGS.variantName .. ' stuck', 'blip')
self.ui:showFAB{icon='star', baizeCmd='newDeal'}
elseif (self.fmoves > 0) and (self.percent == 100) and (not anyProneCards()) then
self.ui:showFAB{icon='done_all', baizeCmd='collect'}
else
self.ui:hideFAB()
end
-- log.trace('updateUI', debug.getinfo(2).name)
end
function Baize:undoPush()
-- TODO might need to determine status here and save it into the undo stack
local saved = self:getSavable()
table.insert(self.undoStack, saved)
end
function Baize:undoPeek()
if #self.undoStack == 0 then
return nil
end
return self.undoStack[#self.undoStack]
end
---@return table
function Baize:undoPop()
return table.remove(self.undoStack)
end
function Baize:undo()
if #self.undoStack < 2 then
self.ui:toast('Nothing to undo', 'blip')
return
end
if self.status == 'complete' then
self.ui:toast('Cannot undo a completed game', 'blip')
return
end
self:unhint()
local _ = self:undoPop() -- remove current state
local saved = self:undoPop()
if not saved then
log.error('undoPop returned nil')
else
self:updateFromSaved(saved)
end
self:undoPush() -- replace current state
self:updateStatus()
self:updateUI()
end
local savedUndoStackFname = 'undoStack.bitser'
function Baize:loadUndoStack()
local ok, undoStack
local info = love.filesystem.getInfo(savedUndoStackFname)
if type(info) == 'table' and type(info.type) == 'string' and info.type == 'file' then
ok, undoStack = pcall(bitser.loadLoveFile, savedUndoStackFname)
if not ok then
-- undoStack is now an error message
log.error('error loading', savedUndoStackFname, undoStack)
undoStack = nil
end
end
love.filesystem.remove(savedUndoStackFname) -- delete it even if error
--[[
undoStack will be an array of objects created by Baize:getSavable()
piles (table, array of objects created by Pile:getSavable())
recycles (number)
bookmark (number)
--]]
if undoStack and #undoStack > 0 then
if not self:isSavable(undoStack[1]) then
undoStack = nil
end
end
--[[
if undoStack then
log.info(string.format('undo stack loaded, depth %d', #undoStack))
else
log.info('undo stack not loaded')
end
]]
self.undoStack = undoStack -- it's ok for this to be nil
end
function Baize:saveUndoStack()
self:undoPush()
bitser.dumpLoveFile(savedUndoStackFname, self.undoStack)
-- log.info('undo stack saved')
end
function Baize:rmUndoStack()
if love.filesystem.remove(savedUndoStackFname) then
log.info('removed', savedUndoStackFname)
else
log.info(savedUndoStackFname, 'not removed')
end
end
function Baize:toggleMenuDrawer()
self.ui:toggleMenuDrawer()
end
function Baize:showVariantTypesDrawer()
self.ui:showVariantTypesDrawer()
end
function Baize:showVariantsDrawer(vtype)
self.ui:showVariantsDrawer(vtype)
end
function Baize:showStatsDrawer()
self.ui:showStatsDrawer(self.stats:strings(_G.SETTINGS.variantName))
end
function Baize:showSettingsDrawer()
self.ui:showSettingsDrawer()
end
function Baize:showColorDrawer()
self.ui:showColorDrawer()
end
function Baize:showAniSpeedDrawer()
self.ui:showAniSpeedDrawer()
end
function Baize:colorBackground()
self.ui:showColorPickerDrawer('baizeColor')
end
function Baize:colorCardFace()
self.ui:showColorPickerDrawer('cardFaceColor')
end
function Baize:colorCardBack()
self.ui:showColorPickerDrawer('cardBackColor')
end
function Baize:colorClub()
self.ui:showColorPickerDrawer('clubColor')
end
function Baize:colorDiamond()
self.ui:showColorPickerDrawer('diamondColor')
end
function Baize:colorHeart()
self.ui:showColorPickerDrawer('heartColor')
end
function Baize:colorSpade()
self.ui:showColorPickerDrawer('spadeColor')
end
function Baize:colorHint()
self.ui:showColorPickerDrawer('hintColor')
end
function Baize:modifySetting(tbl)
-- log.info(tbl.setting, ':=', tbl.value)
if _G.SETTINGS[tbl.setting] ~= nil then
_G.SETTINGS[tbl.setting] = tbl.value
Settings.save(_G.SETTINGS)
self.backgroundCanvas = nil
self:createCardTextures()
end
end
function Baize:showAboutDrawer()
local strs = {
love.filesystem.getIdentity(),
string.format('Version %d %s', _G.LSOL_VERSION, _G.LSOL_VERSION_DATE),
'',
'https://github.com/oddstream/lsol#readme',
'https://love2d.org',
'',
'This program comes with no warranty',
}
self.ui:showAboutDrawer(strs)
end
function Baize:resetStats()
local pressedButton = love.window.showMessageBox('Are you sure?', 'Reset statistics for ' .. _G.SETTINGS.variantName .. '?', {'Yes', 'No', escapebutton = 2}, 'warning')
if pressedButton == 1 then
self.stats:reset(_G.SETTINGS.variantName)
self.ui:toast(string.format('Statistics for %s have been reset', _G.SETTINGS.variantName))
end
end
function Baize:toggleCheckbox(var)
-- log.info('toggle', var)
_G.SETTINGS[var] = not _G.SETTINGS[var]
Settings.save(_G.SETTINGS)
if var == 'simpleCards' then
self:createCardTextures()
self:buildPileBoxesAndRefan()
elseif var == 'autoColorCards' then
self:createCardTextures()
elseif var == 'gradientShading' then
self:createCardTextures()
self.backgroundCanvas = nil
elseif var == 'cardScrunching' then
self:buildPileBoxesAndRefan()
-- powerMoves
elseif var == 'mirrorBaize' then
if self.status == 'complete' then
self.ui:toast('Cannot mirror a completed game', 'blip')
else
self:undoPush()
self:resetPiles()
self.script:buildPiles()
if _G.SETTINGS.mirrorBaize then
self:mirrorSlots()
end
self:layout()
-- BUG when doing this with completed/spinning game, cannot undo completed game
self:undo()
end
-- muteSounds
elseif var == 'allowOrientation' then
-- could have used https://love2d.org/wiki/love.window.updateMode
love.event.quit('restart')
end
end
function Baize:toggleRadio(radio)
-- a radio button has been pressed, and should be toggled ON
-- all other radio buttons with the same var should be toggled OFF
-- radio.var will be the _G.SETTINGS variable
-- which should be set to radio.val
-- the drawer will be closed, and repainted when reopened
_G.SETTINGS[radio.var] = radio.val
Settings.save(_G.SETTINGS)
end
--[[
function Baize:buttonPressed(text)
-- for k,v in pairs(love.handlers) do
-- print(k, v)
-- end
log.trace('Baize:buttonPressed(', text, ')')
love.event.push('permissionButton', text)
end
function Baize:getPermission(text)
_G.BAIZE.ui:showModalDialog({
text=text,
buttons={'Yes','No'}
})
local eventName, result
repeat
eventName, result = love.event.wait()
until eventName == 'permissionButton'
return result == 'Yes'
end
]]
local function resignGameAreYouSure()
-- local pressedButton = love.window.showMessageBox('Are you sure?', 'The current game will count as a loss. Continue?', {'Yes', 'No', escapebutton = 2}, 'warning')
-- return pressedButton == 1
return true
end
---@param vname string
function Baize:changeVariant(vname)
-- log.trace('changing variant from', _G.SETTINGS.variantName, 'to', vname)
if vname == _G.SETTINGS.variantName then
return
end
local newScript = _G.BAIZE:loadScript(vname)
if newScript then
if #self.undoStack > 1 then
if self.status ~= 'complete' then
if not resignGameAreYouSure() then
return
end
self.stats:recordLostGame(_G.SETTINGS.variantName, self.percent)
end
end
--
_G.SETTINGS.variantName = vname
Settings.save(_G.SETTINGS)
self.script = newScript
self:resetPiles()
self.script:buildPiles()
if _G.SETTINGS.mirrorBaize then
self:mirrorSlots()
end
self:layout()
self:resetState()
self.ui:toast('Starting a new game of ' .. _G.SETTINGS.variantName, 'deal')
self.script:startGame()
self:undoPush()
self:unhint()
self:updateStatus()
self:updateUI()
self.ui:updateWidget('title', vname)
self:createCardTextures()
else
self.ui:toast('Do not know how to play ' .. vname, 'blip')
end
end
function Baize:newDeal()
if #self.undoStack > 1 then
if self.status ~= 'complete' then
if not resignGameAreYouSure() then
return
end
self.stats:recordLostGame(_G.SETTINGS.variantName, self.percent)
end
end
self:stopSpinning()
self.ui:hideFAB()
for _, p in ipairs(self.piles) do
p.faceFanFactor = Util.maxFanFactor()
p.cards = {}
end
for _, c in ipairs(self.deck) do
self.stock:push(c)
end
self.stock:shuffle()
self:resetState()
self.ui:toast('Starting a new game of ' .. _G.SETTINGS.variantName, 'deal')
self.script:startGame()
self:undoPush()
self:unhint()
self:updateStatus()
self:updateUI()
end
function Baize:restartDeal()
local saved
while #self.undoStack > 0 do
saved = self:undoPop()
end
self:updateFromSaved(saved)
self:undoPush()
self:unhint()
self:updateStatus()
self:updateUI()
end
function Baize:setBookmark()
-- no point setting a bookmark if virgin or complete, but we allow it
self.bookmark = #self.undoStack
local saved = self:undoPeek()
saved.bookmark = self.bookmark
saved.recycles = self.recycles
self.ui:updateWidget('gotobookmark', nil, self.bookmark ~= 0)
self.ui:toast('Position bookmarked')
end
function Baize:gotoBookmark()
if self.bookmark == 0 then
self.ui:toast('No bookmark', 'blip')
return
end
local saved
while #self.undoStack + 1 > self.bookmark do
saved = self:undoPop()
end
self:updateFromSaved(saved)
self:undoPush()
self:updateStatus()
self:updateUI()
end
function Baize:createBackgroundCanvas()
local ww, wh, _ = love.window.getMode()
local ww2, wh2 = ww / 2, wh / 2
local canvas = love.graphics.newCanvas(ww, wh)
love.graphics.setCanvas({canvas, stencil=true}) -- direct drawing operations to the canvas
if love.gradient and _G.SETTINGS.gradientShading then
local frontColor, backColor = Util.getGradientColors('baizeColor', 'darkGreen', 0.2)
love.gradient.draw(
function()
love.graphics.rectangle('fill', 0, 0, ww, wh)
end,
'radial', -- gradient type
ww2, wh2, -- center of shape
ww2, ww2, -- width of shape
backColor, -- back color
frontColor) -- front color
else
Util.setColorFromSetting('baizeColor')
love.graphics.rectangle('fill', 0, 0, ww, wh)
end
love.graphics.setCanvas()
self.backgroundCanvas = canvas
end
function Baize:buildPileBoxesAndRefan()
--[[
piles with fanType == FAN_DOWN, FAN_RIGHT or FAN_LEFT have a 'box'
within which, all the cards of that pile must fit
if the cards start to spill outside the box
then the fan factor is decreased and the cards are refanned
for example, consider a pile with fanType == FAN_DOWN:
the box will start at the x,y position of the pile, and be the same width as a card
the bottom of the box will either be the bottom of the baize,
or the top of another pile that is directly below this pile
NB pile.cards will be an empty table when called; it would be nice to check if
boundary pile was empty, but this isn't the place
]]
if _G.SETTINGS.cardScrunching then
for _, pile in ipairs(self.piles) do -- run another loop because x,y will have been set
pile.faceFanFactor = Util.maxFanFactor()
if pile.fanType == 'FAN_DOWN' then
pile.box = {
x = pile.x,
y = pile.y,
width = self.cardWidth,
}
if pile.boundaryPile --[[and #pile.boundaryPile.cards > 0]] then
pile.box.height = pile.boundaryPile.y - pile.y
else
pile.box.height = -1 -- signal to use Baize height
end
elseif pile.fanType == 'FAN_RIGHT' then
pile.box = {
x = pile.x,
y = pile.y,
height = self.cardHeight
}
if pile.boundaryPile --[[and #pile.boundaryPile.cards > 0]] then
pile.box.width = pile.boundaryPile.x - pile.x
else
pile.box.width = -1 -- signal to use Baize width
end
elseif pile.fanType == 'FAN_LEFT' then
pile.box = {
x = 0,
y = pile.y,
width = pile.x + self.cardWidth,
height = self.cardHeight
}
end
pile:refan()
end
else
-- version of the above that only honors boundaryPile, not baize
for _, pile in ipairs(self.piles) do -- run another loop because x,y will have been set
pile.faceFanFactor = Util.maxFanFactor()
if pile.boundaryPile --[[and #pile.boundaryPile.cards > 0]] then
if pile.fanType == 'FAN_DOWN' then
pile.box = {
x = pile.x,
y = pile.y,
width = self.cardWidth,
height = pile.boundaryPile.y - pile.y
}
elseif pile.fanType == 'FAN_RIGHT' then
pile.box = {
x = pile.x,
y = pile.y,
width = pile.boundaryPile.x - pile.x,
height = self.cardHeight
}
elseif pile.fanType == 'FAN_LEFT' then
pile.box = {
x = 0,
y = pile.y,
width = pile.x + self.cardWidth,
height = self.cardHeight
}
else
pile.box = nil
end
else
pile.box = nil
end
pile:refan()
end
end
end
function Baize:layout()
local oldCardWidth, oldCardHeight = self.cardWidth, self.cardHeight
local maxSlotX = 0
for _, pile in ipairs(self.piles) do
if not (pile.slot.x < 0 or pile.slot.y < 0) then
if pile.slot.x > maxSlotX then
-- Duchess rule
if pile.fanType == 'FAN_RIGHT3' or pile.fanType == 'FAN_RIGHT' then
maxSlotX = pile.slot.x + 2
else
maxSlotX = pile.slot.x
end
end
end
end
local safex, safey, safew, safeh = love.window.getSafeArea()
-- values returned are in DPI-scaled units (the same coordinate system as most other window-related APIs), not in pixels
-- safex = love.window.toPixels(safex)
-- safey = love.window.toPixels(safey)
-- safew = love.window.toPixels(safew)
-- safeh = love.window.toPixels(safeh)
-- if _G.SETTINGS.debug then
-- local amt = 50
-- local ww, wh, _ = love.window.getMode()
-- safex, safey, safew, safeh = amt, amt, ww - (amt*2), wh - (amt*2)
-- log.info(ww, wh, ':=', safex, safey, safew, safeh)
-- end
_G.UI_SAFEX = safex
_G.UI_SAFEY = safey
_G.UI_SAFEW = safew
_G.UI_SAFEH = safeh
local landscape = safew > safeh -- add a one-card-width border either side
local slotWidth, slotHeight
if landscape then
-- 2.8 gives equal margin left/right
-- and debug vertical line appears in middle of Freecell tableaux
-- why 2.8? currently a mystery
-- cardRatioLandscape is 1.39 but 1.39*2=2.78 does not center tableaux
slotWidth = safew / (maxSlotX + 2.8)
slotHeight = slotWidth * _G.SETTINGS.cardRatioLandscape
else
slotWidth = safew / (maxSlotX + 1) -- +1 gives a 0.5 card width gap either side
slotHeight = slotWidth * _G.SETTINGS.cardRatioPortrait
end
local pilePaddingX = slotWidth / 10
self.cardWidth = slotWidth - pilePaddingX
local pilePaddingY = slotHeight / 10
self.cardHeight = slotHeight - pilePaddingY
local leftMargin
if landscape then
leftMargin = safex + self.cardWidth / 2 + pilePaddingX + self.cardWidth
else
leftMargin = safex + self.cardWidth / 2 + pilePaddingX -- - (pilePaddingX * 4) to have smaller border
end
local topMargin = safey + _G.TITLEBARHEIGHT + pilePaddingY
self.cardRadius = self.cardWidth / 10 -- _G.SETTINGS.cardRoundness
if self.cardWidth ~= oldCardWidth or self.cardHeight ~= oldCardHeight then
self.labelFont = love.graphics.newFont(_G.ORD_FONT, self.cardWidth * 0.7)
self:createCardTextures()
-- _G.consoleLog(string.format('card %d %d', self.cardWidth, self.cardHeight))
-- log.info('card width, height', self.cardWidth, self.cardHeight)
end
for _, pile in ipairs(self.piles) do
pile:setBaizePos(
-- slots are 1-based, graphics coords are 0-based
leftMargin + ((pile.slot.x - 1) * (self.cardWidth + pilePaddingX)),
topMargin + ((pile.slot.y - 1) * (self.cardHeight + pilePaddingY))
)
end
self:buildPileBoxesAndRefan()
self.ui:layout()
end
function Baize:mirrorSlots()
--[[
0 1 2 3 4 5
5 4 3 2 1 0
0 1 2 3 4
4 3 2 1 0
]]
local minX = 32767
local maxX = 0
for _, p in ipairs(self.piles) do
if p.slot.x > 0 and p.slot.y > 0 then -- ignore hidden piles
if p.slot.x < minX then
minX = p.slot.x
end
if p.slot.x > maxX then
maxX = p.slot.x
end
end
end
for _, p in ipairs(self.piles) do
if p.slot.x > 0 and p.slot.y > 0 then -- ignore hidden piles
p.slot.x = maxX - p.slot.x + minX
if p.fanType == 'FAN_RIGHT' then
p.fanType = 'FAN_LEFT'
elseif p.fanType == 'FAN_LEFT' then
p.fanType = 'FAN_RIGHT'
elseif p.fanType == 'FAN_LEFT3' then
p.fanType = 'FAN_RIGHT3'
elseif p.fanType == 'FAN_RIGHT3' then
p.fanType = 'FAN_LEFT3'
end
end
end
end
function Baize:stateSnapshot()