Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Horizontal material #98

Open
wants to merge 2 commits into
base: github
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 75 additions & 42 deletions tcl/board.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ proc ::board::new {w {psize 40} } {
set ::board::_evalbarHeight($w) 0
set ::board::_evalbarWidth($w) 0
set ::board::_evalbarScale($w) 1
set ::board::_matDiff(W,$w) 0
set ::board::_matDiff(B,$w) 0

set border $::board::_border($w)
set bsize [expr {$psize * 8 + $border * 9} ]
Expand Down Expand Up @@ -505,6 +507,14 @@ proc ::board::new {w {psize 40} } {
return $w
}

proc ::board::addMaterialBar { w } {
ttk_canvas $w.playerW.materialDiff -height 20 -width 0
grid $w.playerW.materialDiff -row 0 -column 3 -sticky e

ttk_canvas $w.playerB.materialDiff -height 20 -width 0
grid $w.playerB.materialDiff -row 0 -column 3 -sticky e
}

proc ::board::addNamesBar {w {varname}} {
ttk::frame $w.playerW -style fieldbg.TLabel
frame $w.playerW.color -background #EAE0C8 -width 6 -height 6
Expand All @@ -515,8 +525,8 @@ proc ::board::addNamesBar {w {varname}} {
grid $w.playerW.color -row 0 -column 0 -sticky news -padx 2 -pady 2
grid $w.playerW.name -row 0 -column 1 -sticky w
grid $w.playerW.elo -row 0 -column 2 -sticky w
grid $w.playerW.clock -row 0 -column 3 -sticky e
grid $w.playerW.tomove -row 0 -column 4 -sticky w -padx 4
grid $w.playerW.clock -row 0 -column 4 -sticky e
grid $w.playerW.tomove -row 0 -column 5 -sticky w -padx 4
grid columnconfigure $w.playerW 3 -weight 1
grid $w.playerW -row 16 -column 3 -columnspan 8 -sticky news -pady 4

Expand All @@ -529,8 +539,8 @@ proc ::board::addNamesBar {w {varname}} {
grid $w.playerB.color -row 0 -column 0 -sticky news -padx 2 -pady 2
grid $w.playerB.name -row 0 -column 1 -sticky w
grid $w.playerB.elo -row 0 -column 2 -sticky w
grid $w.playerB.clock -row 0 -column 3 -sticky e
grid $w.playerB.tomove -row 0 -column 4 -sticky w -padx 4
grid $w.playerB.clock -row 0 -column 4 -sticky e
grid $w.playerB.tomove -row 0 -column 5 -sticky w -padx 4
grid columnconfigure $w.playerB 3 -weight 1
grid $w.playerB -row 3 -column 3 -columnspan 8 -sticky news -pady 4
}
Expand Down Expand Up @@ -769,10 +779,10 @@ proc ::board::flipNames_ { {w} {white_on_top} } {
}

proc ::board::sideToMove_ { {w} {side} } {
if {![winfo exist $w.playerW] } { return }
if {![winfo exist $w.playerW.tomove] } { return }
if {$side == "w"} {
$w.playerB.tomove delete -tag tomove
$w.playerW.tomove create rectangle 0 0 100 100 -fill blue -tag tomove
$w.playerW.tomove create rectangle 0 0 100 100 -fill blue -tag tomove
} elseif {$side == "b"} {
$w.playerW.tomove delete -tag tomove
$w.playerB.tomove create rectangle 0 0 100 100 -fill blue -tag tomove
Expand Down Expand Up @@ -860,10 +870,8 @@ proc ::board::resize {w psize} {
$w.bd coords sq$pos $x1 $y1 $x2 $y2
}

# resize the material canvas
$w.mat configure -height $bsize

::board::coords $w $::board::_coords($w)
::board::configureMaterialBar $w
::board::update $w
::board::drawEvalBar_ $w

Expand Down Expand Up @@ -1742,15 +1750,29 @@ proc ::board::flip {w {newstate -1}} {
return $w
}
################################################################################
# ::board::configureMaterialBar
# if enough space between name/elo and Clock place material difference there
# else place them in a second row
################################################################################
proc ::board::configureMaterialBar { w } {
if { ! [winfo exists $w.playerW.materialDiff] } { return }
foreach i { W B } {
set width [expr $::board::_size($w) * 8 - [winfo width $w.player$i.name] - [winfo width $w.player$i.elo] - \
[winfo width $w.player$i.clock] - [winfo width $w.player$i.color] - [winfo width $w.player$i.tomove] ]
set len [$w.player$i.materialDiff cget -width]
if { $width < $len && $::board::_matDiff($i,$w) > 0 } {
grid $w.player$i.materialDiff -row 1 -column 1 -sticky e -columnspan 5
} else {
grid $w.player$i.materialDiff -row 0 -column 3 -sticky e -columnspan 1
}
}
}
################################################################################
# ::board::material
# displays material balance
################################################################################
proc ::board::material {w} {
set f $w.mat

$f delete material

set fen [lindex [sc_pos fen] 0]
set fen [string range $::board::_data($w) 0 63]
set p 0
set n 0
set b 0
Expand All @@ -1771,48 +1793,59 @@ proc ::board::material {w} {
Q {incr q}
}
}
set sum [expr abs($p) + abs($n) +abs($b) +abs($r) +abs($q) ]
set rank 0

set mat(W) ""
set mat(B) ""
set ::board::_matDiff(W,$w) 0
set ::board::_matDiff(B,$w) 0
$w.playerW.materialDiff delete material
$w.playerB.materialDiff delete material
foreach pType {q r b n p} {
set count [expr "\$$pType"]
set side ""
if {$count < 0} {
addMaterial $count $pType $f $rank $sum
incr rank [expr abs($count) ]
set count [expr abs($count)]
set side B
} elseif {$count > 0} {
set side W
}
}
foreach pType {q r b n p} {
set count [expr "\$$pType"]
if {$count > 0} {
addMaterial $count $pType $f $rank $sum
incr rank [expr abs($count) ]
if { $side ne "" } {
for {set i 0} {$i<$count} {incr i} {
append mat($side) $pType
incr ::board::_matDiff($side,$w)
}
}
}
::board::addMaterial $w.playerW.materialDiff $mat(W) w
::board::addMaterial $w.playerB.materialDiff $mat(B) w
configureMaterialBar $w
}
proc ::board::addMaterial {count piece parent rank sum} {
if {$count == 0} {return}
if {$count <0} {
set col "b"
set count [expr 0 - $count ]
} else {
set col "w"
}
set w [$parent cget -width]

proc ::board::addMaterial { parent pieces c} {
set count [string length $pieces]
set w [expr $count * 18]
set h [$parent cget -height]
set offset [expr ($h - ($sum * 20)) / 2]
if {$offset <0} { set offset 0 }
set x [expr $w / 2]
set y [expr $h / 2]
$parent configure -width $w
for {set i 0} {$i<$count} {incr i} {
set y [expr $rank * 20 +10 + $offset + $i * 20]
$parent create image $x $y -image $col${piece}20 -tag material
set x [expr $i * 18 + 9]
set piece [string index $pieces $i]
$parent create image $x $y -image $c${piece}20 -tag material
}
}
proc ::board::toggleMaterial {w} {
set ::board::_showmat($w) [expr {1 - $::board::_showmat($w)}]
if {$::board::_showmat($w)} {
grid $w.mat
if {!$::board::_showmat($w)} {
set ::board::_matDiff(W,$w) 0
set ::board::_matDiff(B,$w) 0
if { [winfo exists $w.playerW] } {
$w.playerW.materialDiff delete material
$w.playerB.materialDiff delete material
grid forget $w.playerW.materialDiff
grid forget $w.playerB.materialDiff
}
} else {
grid remove $w.mat
grid $w.playerW.materialDiff -row 0 -column 3 -sticky e
grid $w.playerB.materialDiff -row 0 -column 3 -sticky e
}
::board::update $w
return $::board::_showmat($w)
Expand Down
13 changes: 12 additions & 1 deletion tcl/main.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ proc updateMainGame {} {
set gamePlayers(eloB) [expr {$eloB == 0 ? "" : "($eloB)"}]
set gamePlayers(clockW) ""
set gamePlayers(clockB) ""
::board::configureMaterialBar .main.board
}

# updateTitle:
Expand Down Expand Up @@ -175,6 +176,7 @@ proc updateStatusBar {} {
regexp $clkExp $comment -> ::gamePlayers(clockW)
regexp $clkExp $prevCom -> ::gamePlayers(clockB)
}
::board::configureMaterialBar .main.board
}

if {[info exists ::guessedAddMove]} {
Expand Down Expand Up @@ -1152,6 +1154,8 @@ proc setPlayMode { callback } {
################################################################################
# In docked mode, resize board automatically
################################################################################
set ::mainboardWidth 0
set ::mainboardHigh 0
proc resizeMainBoard {} {
if { $::autoResizeBoard } {
update idletasks
Expand All @@ -1163,6 +1167,12 @@ proc resizeMainBoard {} {
if { [llength [pack slaves .main.tb]] != 0 } {
set availh [expr $availh - [winfo height .main.tb] ]
}
# ignore resize if size has not changed
if { $availw == $::mainboardWidth && $availh == $::mainboardHigh } {
return
}
set ::mainboardWidth $availw
set ::mainboardHigh $availh
set ::boardSize [::board::resizeAuto .main.board "0 0 $availw $availh"]
}
}
Expand Down Expand Up @@ -1193,10 +1203,11 @@ proc CreateMainBoard { {w} } {
options.persistent ::showEvalBar($w) 1
options.persistent ::showMainEvalBarArrow 1
if {$::showEvalBar($w)} { ::board::toggleEvalBar $w.board }
if {$::gameInfo(showMaterial)} { ::board::toggleMaterial $w.board }

::board::addNamesBar $w.board gamePlayers
::board::addInfoBar $w.board gameInfoBar
::board::addMaterialBar $w.board
if {$::gameInfo(showMaterial)} { ::board::toggleMaterial $w.board }

set ::gameInfoBar(tb_BD_Material) "set ::gameInfo(showMaterial) \[::board::toggleMaterial $w.board\]"
set ::gameInfoBar(tb_BD_Scorebar) [list apply {{w} {
Expand Down