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

on-raw-key #262

Open
wants to merge 11 commits into
base: horizon
Choose a base branch
from
Open

on-raw-key #262

wants to merge 11 commits into from

Conversation

jpolitz
Copy link
Member

@jpolitz jpolitz commented Mar 11, 2018

A prototype of on-raw-key, which gives more control over keydown and keyup events, as well as information about meta keys

Copy link
Member

@blerner blerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this go along with a well-formed check that you only use either on-key or on-raw-key but not both?

runtime.getField(Reactors, "raw-key").app(
e.key,
e.type,
false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this false mean?


return rawJsworld.on_raw_key(
function(w, e, success) {
console.log("the key event: ", e)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray?

@jpolitz jpolitz temporarily deployed to pyret-horizon-pr-262 March 11, 2018 19:01 Inactive
@jpolitz jpolitz temporarily deployed to pyret-horizon-pr-262 March 11, 2018 19:22 Inactive
@jpolitz jpolitz temporarily deployed to pyret-horizon-pr-262 March 11, 2018 19:49 Inactive
@jpolitz jpolitz temporarily deployed to pyret-horizon-pr-262 March 11, 2018 20:09 Inactive
- app.json is only read when FIRST CREATED, future deploys do
  not read it
- All values must be strings
@jpolitz jpolitz temporarily deployed to pyret-horizon-pr-262 March 11, 2018 20:25 Inactive
@jpolitz jpolitz requested a deployment to pyret-horizon-pr-262 March 11, 2018 20:48 Abandoned
@jpolitz jpolitz temporarily deployed to pyret-horizon-pr-262 March 11, 2018 20:48 Inactive
@jpolitz
Copy link
Member Author

jpolitz commented Mar 11, 2018

Here's an example program that uses this for reference (not using a share link because the review app doesn't currently have Drive doing what I expect). You can run it by copy/pasting into

https://pyret-horizon-pr-262.herokuapp.com/editor

include image
include reactors

#|
   The Pong structure tracks whether each of the relevant keys is
   currently pushed down. W and S refer to the WASD setup for up
   and down, and up/down refer to the arrow keys. y1 and y2 correspond
   to the y positions of the two paddles
|#
data Pong:
  | pong(
      up :: Boolean,
      down :: Boolean,
      w :: Boolean,
      s :: Boolean,
      y1 :: Number,
      y2 :: Number)
end

DY = 3
paddle= rectangle(20, 50, "solid", "green")

draw-state :: Pong -> Image
fun draw-state(p):
  put-image(paddle, 20, p.y1,
    put-image(paddle, 480, p.y2,
      rectangle(500, 400, "solid", "black")))
end

next-state-tick :: Pong -> Pong
# This does all of the changing of actual positions - the two y values
# change according to the values in the up/down/w/s fields. The idea is
# that the up/down/w/s fields represent "moving right now", and all
# movement happens on ticks
fun next-state-tick(p):
  new-y1 =
    if p.s: p.y1 - DY
    else if p.w: p.y1 + DY
    else: p.y1
    end
  new-y2 =
    if p.up: p.y2 + DY
    else if p.down: p.y2 - DY
    else: p.y2 end
  pong(p.up, p.down, p.w, p.s, new-y1, new-y2)
end

next-state-raw-key :: Pong, Event -> Pong
#| This simply manipulates the up/down/w/s fields. The only two options
for the key-action field are "keydown" and "keyup" (this could potentially
be a boolean field on the Event). This means that on keyup, the
corresponding key state will become false, and on keydown it will become true,
and in all the intervening ticks it will be true.

The Event type is roughly what's specified in
 https://github.com/brownplt/code.pyret.org/issues/35#issuecomment-278417548
 
Currently, it's:
   
data Event:
  | time-tick
  | mouse(x :: Number, y :: Number, kind :: String)
  | keypress(key :: String)
  | raw-key(key :: String, key-action :: String, caps :: Boolean, shift :: Boolean, alt :: Boolean, command :: Boolean, control :: Boolean)
end

   and on-raw-key will _always_ receive a raw-key event. 
   
|#
fun next-state-raw-key(p, x):
  is-keydown = x.key-action == "keydown"
  if x.key == "s":
    pong(p.up, p.down, p.w, is-keydown, p.y1, p.y2)
  else if x.key == "w":
    pong(p.up, p.down, is-keydown, p.s, p.y1, p.y2)
  else if x.key == "ArrowUp":
    pong(is-keydown, p.down, p.w, p.s, p.y1, p.y2)
  else if x.key == "ArrowDown":
    pong(p.up, is-keydown, p.w, p.s, p.y1, p.y2)
  else:
    p
  end
end

r = reactor:
  init: pong(false, false, false, false, 200, 200),
  to-draw: draw-state,
  on-tick: next-state-tick,
  on-raw-key: next-state-raw-key
end

r.interact()

@jpolitz jpolitz temporarily deployed to pyret-horizon-pr-262 March 12, 2018 00:21 Inactive
@jpolitz jpolitz temporarily deployed to pyret-horizon-pr-262 March 12, 2018 03:27 Inactive
@schanzer schanzer mentioned this pull request Mar 31, 2018
@schanzer
Copy link

Where does this PR stand?

@L1Z3
Copy link

L1Z3 commented Dec 10, 2021

Is there any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants