kaorahi / lizgoban

Leela Zero & KataGo visualizer
GNU General Public License v3.0
169 stars 28 forks source link

Is it possible to support clicking on a step in the pv diagram to display it directly on the main board? #104

Closed qcgm1978 closed 9 months ago

qcgm1978 commented 9 months ago

Currently, there are no mouse events bound to the pv diagram. If you want to study the changes after a step on the pv diagram, you can only start from the first step of the suggest, which is rather cumbersome. I tried it as following code snippet and video:

const subgoban_mouse = {
    handle_mouse_on_goban: handle_mouse_on_sub_goban,
    read_only:false
}
const draw_pv = with_opts((...args) => {
    ...
}, subgoban_mouse)
function handle_mouse_on_sub_goban(canvas, coord2idx, read_only) { 
    const onmousedown = e => {
        const clicked_move = mouse2move(e, coord2idx)
        const variation = R[`game_${R.sequence_cursor}`][R.move_count].variation;
        const ind = variation.findIndex(h => h == clicked_move)
        if (!read_only && !R.attached) {
            for (let i = 0; i <= ind; i++) {
                const force_create = !Boolean(ind)
                const bool = play_here({ e, coord2idx, canvas, force_create,move:variation[i] })
                if (bool) {
                    set_showing_movenum_p(false)
                    hover_off(canvas)
                }
            }
        }
    }
    ...
}
function play_here({ e, coord2idx, canvas, move, force_create = true, }) {
    if (!move) {
        move = mouse2move(e, coord2idx); if (!move) { return true }
    }
    ...
}

屏幕录制2023-12-05 20.17.44.webm

kaorahi commented 9 months ago

This may be easier? (on 63eb172e5)

--- a/src/renderer.js
+++ b/src/renderer.js
@@ -411,7 +411,7 @@ const draw_pv = with_opts((...args) => {
         showing_branch_p() ? D.draw_goban_with_future_moves(...args) :
         already_showing_pv_p() ? draw_another(...args) :
         D.draw_goban_with_principal_variation(...args)
-}, ignore_mouse)
+})
 const draw_another = (...args) => {
     R.different_engine_for_white_p ?
         D.draw_goban_with_expected_variation(...args) :
--- a/src/draw_goban.js
+++ b/src/draw_goban.js
@@ -182,7 +182,7 @@ function draw_goban_with_variation(canvas, suggest, opts) {

 function draw_goban_with_principal_variation(canvas, options) {
     const opt = {draw_endstate_stdev_p: R.show_endstate, ...options}
-    draw_readonly_goban_with_variation(canvas, R.suggest[0] || {}, opt)
+    draw_goban_with_variation(canvas, R.suggest[0] || {}, opt)
 }

 function draw_goban_with_expected_variation(canvas, options) {

I explicitly set the "PV diagram board" read-only in early stage of development when I added the "mouse cursor" on the boards (498b4e6737). I further disabled mouse actions on it later (58f760f60). Though I cannot remember the reasons for them, I'd like to keep it unclickable. The subboard logic is already too complicated. I'm afraid that further actions might exceed my capability to manage, potentially leading to troublesome bugs.

Look at these terrible nested conditionals!

https://github.com/kaorahi/lizgoban/blob/915ed3269400c1a0559f3cf95cabc0d00e11378a/src/renderer.js#L409-L414

qcgm1978 commented 9 months ago

Since I have already changed the suggest moves of the pv diagram to dynamic display in my local code, your method seems to be able to display only the clicked step on the main board, not including the moves before that step. However, you are right, the code should be kept concise.