Skip to content

Commit

Permalink
elements: make right-click popup-menu to any state
Browse files Browse the repository at this point in the history
We have to use `(send this popup-menu …)` rather than
`(render-popup-menu …)` because
1. The latter requires a renderer, which would probably be the renderer
   for the root window of the application, and
2. The latter renders in coordinates specified by the root window of the
   renderer.
Since the canvases to which we apply the mixins _are_ gui windows
(https://docs.racket-lang.org/gui/windowing-overview.html#%28part._.Core_.Windowing_.Classes%29),
the mouse-click event is reported in coordinates of the canvas. When we
try to render the popup-menu via `(render-popup-menu …)` with a renderer
built from `(render (window … elements-cycler …))`, the menu ends up
being placed at the wrong coordinates.

In a diagram:
------------------------------
|                            |
| p                          |
|   -----   -----   -----    |
|   |   |   |   |   |   |    |
|   |   |   |   |   | x |    |
|   -----   -----   -----    |
|     A       B       C      |
|                            |
------------------------------

The 'x' marks the click. We want the popup to appear there, but the
coordinates we are given are in terms of the canvas 'C' (roughly (1,1)
here). When rendered on the outer window, that shows the popup-menu at
position 'p'!

So, we borrow the logic of `render-popup-menu` from
`racket/gui/easy/private/renderer`, but swapping in the canvas window
(_i.e._, `this`). That way, the coordinates are specified appropriately.
This is done by something of a hack: the `renderer%` class is not
available publicly from `racket/gui/easy` or from its internal private
modules, so to access it we use Racket's namespace reflection
capabilities.

If there were a way to translate the coordinates of the canvas to
coordinates of the parent window, we could probably use
`render-popup-menu` with the renderer for the parent window.
  • Loading branch information
benknoble committed Oct 31, 2022
1 parent b4e0306 commit 53c567c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
25 changes: 22 additions & 3 deletions gui/elements.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,31 @@
(define-values (states views) (element-cyclers es))
(values states (apply panel #:stretch '(#f #f) views)))

(define (handle-element-clicks cycle-element)
(define (handle-element-clicks cycle-element @state)
(define renderer%
(let ([mod 'racket/gui/easy/private/renderer])
(dynamic-require mod #f)
(define ns (module->namespace mod))
(eval 'renderer% ns)))
(define (gui:popup-menu pum)
(define r (new renderer% [tree pum]))
(begin0 (send r render #f)
(send r destroy)))
(mixin (canvas<%>) ()
(super-new)
(define/override (on-event e)
(case (send e get-event-type)
[(left-down) (cycle-element)]))))
[(left-down) (cycle-element)]
[(right-down)
(define pum
(gui:popup-menu
(popup-menu
(menu
"Transition to…"
(menu-item "Infused" (λ () (:= @state 'infused)))
(menu-item "Waning" (λ () (:= @state 'waning)))
(menu-item "Unfused" (λ () (:= @state 'unfused)))))))
(send this popup-menu pum (send e get-x) (send e get-y))]))))

(define (element-cycler e)
(define/obs @element-state 'unfused)
Expand All @@ -40,7 +59,7 @@
(pict-canvas @element-state
make-pict-for-canvas
#:min-size (list (+ 6 size) (+ 6 size))
#:mixin (handle-element-clicks action)))
#:mixin (handle-element-clicks action @element-state)))
(define cycler-view
(group
(element-pics-name e)
Expand Down
16 changes: 10 additions & 6 deletions scribblings/how-to-play.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ When you're finished adding monster groups, click "Next."
After adding monster groups, you'll reach the main scenario screen. It features
several elements, which we will examine in turn.


@subsection{Elements Tracker}

On the far left is the elements tracker. Each of the six (6) elements is
Expand All @@ -96,7 +95,11 @@ the element: click to infuse an element, and notice that the background of the
element fills up with color. Click again to manually wane the element; notice
the background is only half full. Click a third time to completely unfuse the
element---now, the background should be back to black. Alternately, you can
click the element to transition to the state indicated by the button.
click the element to transition to the state indicated by the button. You can
also right-click the element to transition to any state.

Note that elements are advanced automatically by the "Next Round" button as in
@secref{Advancing_the_Scenario}.

Here are the elements in each of the unfused, infused, and waning states:

Expand Down Expand Up @@ -139,10 +142,11 @@ two is used, and the better is discarded. The used card will appear next to
At the bottom of the right control group you will find two (2) buttons. Only
one will be available to you at a time.

The first button is "Next Round." After every group has taken their turn, use this
button to move to the next round. At this point, you would begin entering player
initiatives to prepare for the new round (see @secref{Player_Controls}). This
button also automatically wanes or unfuses active elements.
The first button is "Next Round." After every group has taken their turn, use
this button to move to the next round. At this point, you would begin entering
player initiatives to prepare for the new round as in @secref{Player_Controls}.
This button also automatically wanes or unfuses active elements as in
@secref{Elements_Tracker}.

The second button is "Draw Abilities." Use this button once all player
initiatives are entered to draw an ability card for each monster group and
Expand Down

0 comments on commit 53c567c

Please sign in to comment.