|
| 1 | +<?xml version="1.0"?> |
| 2 | +<!-- |
| 3 | +license:CC0-1.0 |
| 4 | +--> |
| 5 | +<mamelayout version="2"> |
| 6 | + <view name="Touch-Enabled" showpointers="yes"> |
| 7 | + <screen id="screen" index="0"> |
| 8 | + <bounds left="0" top="0" right="4" bottom="3" /> |
| 9 | + </screen> |
| 10 | + </view> |
| 11 | + |
| 12 | + <script><![CDATA[ |
| 13 | + file:set_resolve_tags_callback( |
| 14 | + function () |
| 15 | + -- get the touchscreen I/O port fields |
| 16 | + local tsdev = file.device:subdevice('microtouch') |
| 17 | + local btn_field = tsdev:ioport('TOUCH'):field(0x01) |
| 18 | + local x_field = tsdev:ioport('TOUCH_X'):field(0x3fff) |
| 19 | + local y_field = tsdev:ioport('TOUCH_Y'):field(0x3fff) |
| 20 | +
|
| 21 | + -- for mapping coordinates |
| 22 | + local view = file.views['Touch-Enabled'] |
| 23 | + local scr_item = view.items['screen'] |
| 24 | + local floor = math.floor |
| 25 | + local l, r, t, b |
| 26 | + local x_scale, y_scale |
| 27 | +
|
| 28 | + -- pointer state |
| 29 | + local ptr_id = nil |
| 30 | + local inside = false |
| 31 | +
|
| 32 | + local function release_touch() |
| 33 | + btn_field:clear_value() |
| 34 | + x_field:clear_value() |
| 35 | + y_field:clear_value() |
| 36 | + end |
| 37 | +
|
| 38 | + local function recomputed() |
| 39 | + local bounds = scr_item.bounds |
| 40 | + l = bounds.x0 |
| 41 | + r = bounds.x1 |
| 42 | + t = bounds.y0 |
| 43 | + b = bounds.y1 |
| 44 | + x_scale = 0x3fff / bounds.width |
| 45 | + y_scale = 0x3fff / bounds.height |
| 46 | + end |
| 47 | +
|
| 48 | + local function check_pointer(x, y) |
| 49 | + if (x >= l) and (x < r) and (y >= t) and (y < b) then |
| 50 | + inside = true |
| 51 | + btn_field:set_value(1) |
| 52 | + x_field:set_value(floor((x - l) * x_scale)) |
| 53 | + y_field:set_value(floor((y - t) * y_scale)) |
| 54 | + elseif inside then |
| 55 | + inside = false |
| 56 | + release_touch() |
| 57 | + end |
| 58 | + end |
| 59 | +
|
| 60 | + local function forget_pointer() |
| 61 | + if inside then |
| 62 | + release_touch() |
| 63 | + end |
| 64 | + ptr_id = nil |
| 65 | + inside = false |
| 66 | + end |
| 67 | +
|
| 68 | + local function pointer_updated(type, id, dev, x, y, btn, dn, up, cnt) |
| 69 | + if ptr_id == id then |
| 70 | + if (btn & 0x01) == 0 then |
| 71 | + forget_pointer() |
| 72 | + else |
| 73 | + check_pointer(x, y) |
| 74 | + end |
| 75 | + elseif (not ptr_id) and ((dn & 0x01) ~= 0) then |
| 76 | + ptr_id = id |
| 77 | + check_pointer(x, y) |
| 78 | + end |
| 79 | + end |
| 80 | +
|
| 81 | + local function pointer_lost(type, id, dev, x, y, up, cnt) |
| 82 | + if ptr_id == id then |
| 83 | + forget_pointer() |
| 84 | + end |
| 85 | + end |
| 86 | +
|
| 87 | + -- attach callbacks to view |
| 88 | + view:set_recomputed_callback(recomputed) |
| 89 | + view:set_pointer_updated_callback(pointer_updated) |
| 90 | + view:set_pointer_left_callback(pointer_lost) |
| 91 | + view:set_pointer_aborted_callback(pointer_lost) |
| 92 | + view:set_forget_pointers_callback(forget_pointer) |
| 93 | + end) |
| 94 | + ]]></script> |
| 95 | +</mamelayout> |
0 commit comments