{
"id": "104",
"prompt": "Distinguish tap from drag on a square: pressing on it arms a pending gesture and records the press origin; once the pointer moves more than 8px (manhattan distance is fine) it becomes a DRAG and the square follows with its grab offset; releasing without ever crossing the threshold counts as a TAP, increments a counter, and flashes the square white. Keep origin, mode, box position and counters in state slots.",
"solution_rl": "fn dif(a: i32, b: i32) -> i32 {\n if a > b { a - b } else { b - a }\n}\n\nfn frame(t: i32) {\n let w: i32 = host::display::width();\n let h: i32 = host::display::height();\n if host::display::state_get(10) == 0 {\n host::display::state_set(10, 1);\n host::display::state_set(3, w / 2 - 24);\n host::display::state_set(4, h / 2 - 24);\n }\n let down: i32 = host::display::pointer_down();\n let prev: i32 = host::display::state_get(5);\n let px: i32 = host::display::pointer_x();\n let py: i32 = host::display::pointer_y();\n let mut mode: i32 = host::display::state_get(2);\n let mut bx: i32 = host::display::state_get(3);\n let mut by: i32 = host::display::state_get(4);\n if down == 1 && prev == 0 && px >= bx && px < bx + 48 && py >= by && py < by + 48 {\n mode = 1;\n host::display::state_set(0, px);\n host::display::state_set(1, py);\n host::display::state_set(8, px - bx);\n host::display::state_set(9, py - by);\n }\n if mode == 1 && down == 1 {\n if dif(px, host::display::state_get(0)) + dif(py, host::display::state_get(1)) > 8 {\n mode = 2;\n }\n }\n if mode == 2 && down == 1 {\n bx = px - host::display::state_get(8);\n by = py - host::display::state_get(9);\n }\n let mut flash: i32 = host::display::state_get(7);\n if down == 0 && mode != 0 {\n if mode == 1 {\n host::display::state_set(6, host::display::state_get(6) + 1);\n flash = 15;\n }\n mode = 0;\n }\n if flash > 0 { flash = flash - 1; }\n host::display::state_set(2, mode);\n host::display::state_set(3, bx);\n host::display::state_set(4, by);\n host::display::state_set(5, down);\n host::display::state_set(7, flash);\n host::display::clear(0x101010);\n let mut c: i32 = 0x8844cc;\n if mode == 2 { c = 0xcc8800; }\n if flash > 0 { c = 0xffffff; }\n host::display::fill_rect(bx, by, 48, 48, c);\n host::display::draw_string(8, 8, \"TAPS\", 0x808080, 1);\n host::display::draw_number(50, 8, host::display::state_get(6), 0xffffff, 2);\n if mode == 2 {\n host::display::draw_string(8, 30, \"DRAGGING\", 0xcc8800, 1);\n }\n host::display::present();\n}\n",
"tags": [
"ui",
"pointer",
"state",
"gesture",
"drag"
]
}