|
| 1 | +--[[ |
| 2 | +Gamepad demonstration |
| 3 | +--]] |
| 4 | + |
| 5 | + require 'gui/window' |
| 6 | + |
| 7 | + glSelf = obj.find('self') |
| 8 | + glPath = glSelf.workingPath |
| 9 | + |
| 10 | + local win = gui.window({ title='Gamepad Demo', insideWidth=600, insideHeight=400, center=true, enableControllers=true }) |
| 11 | + local viewport = win:clientViewport({ aspectRatio = ARF_MEET }) |
| 12 | + |
| 13 | + local text = viewport.new('vectortext', { |
| 14 | + x=win.margins.left, y=20, string='Use a gamepad to control the football.', face='Noto Sans', fontSize=20, fill='rgb(0,0,0)' |
| 15 | + }) |
| 16 | + |
| 17 | + local txtLeftStick = viewport.new('vectortext', { |
| 18 | + x=win.margins.left, y=50, string='Left Stick:', face='Noto Sans', fontSize=20, fill='rgb(0,0,0)' |
| 19 | + }) |
| 20 | + |
| 21 | + local txtRightStick = viewport.new('vectortext', { |
| 22 | + x=win.margins.left, y=80, string='Right Stick:', face='Noto Sans', fontSize=20, fill='rgb(0,0,0)' |
| 23 | + }) |
| 24 | + |
| 25 | + local txtButtons = viewport.new('vectortext', { |
| 26 | + x=win.margins.left, y=110, string='Buttons:', face='Noto Sans', fontSize=20, fill='rgb(0,0,0)' |
| 27 | + }) |
| 28 | + |
| 29 | + local ballVP = viewport.new('VectorViewport', { |
| 30 | + x = 260, y = 260, width = 60, height = 60 |
| 31 | + }) |
| 32 | + |
| 33 | + local svg = obj.new('svg', { target=ballVP, path=glPath .. 'data/football.svg' }) |
| 34 | + |
| 35 | + local MAX_SPEED = 3 |
| 36 | + local ctrl = obj.new('controller', { port=0 }) |
| 37 | + local err, controllerTimer = mSys.SubscribeTimer(1/60, function(Subscriber, Elapsed, CurrentTime) |
| 38 | + ctrl.acQuery() |
| 39 | + |
| 40 | + txtLeftStick.string = 'Left Stick: ' .. string.format('%.2f %.2f', ctrl.leftStickX, ctrl.leftStickY) |
| 41 | + txtRightStick.string = 'Right Stick: ' .. string.format('%.2f %.2f', ctrl.rightStickX, ctrl.rightStickY) |
| 42 | + txtButtons.string = 'Buttons: ' .. string.format('%.8x', ctrl.buttons) |
| 43 | + |
| 44 | + local speed = MAX_SPEED |
| 45 | + if bit.band(ctrl.buttons, CON_GAMEPAD_S) != 0 then |
| 46 | + speed = speed * 2 |
| 47 | + end |
| 48 | + |
| 49 | + ballVP.x = ballVP.x + (ctrl.leftStickX * speed) |
| 50 | + ballVP.y = ballVP.y - (ctrl.leftStickY * speed) |
| 51 | + |
| 52 | + ballVP.width = ballVP.width + ctrl.rightStickY |
| 53 | + ballVP.height = ballVP.height + ctrl.rightStickY |
| 54 | + |
| 55 | + viewport.scene.surface.mtScheduleRedraw() |
| 56 | + end) |
| 57 | + |
| 58 | + win:show() |
| 59 | + processing.sleep() |
0 commit comments