Skip to content

Commit

Permalink
Merge branch 'master' into features/python3
Browse files Browse the repository at this point in the history
  • Loading branch information
scotty007 committed Oct 15, 2020
2 parents 843ad84 + 9c4c2cf commit 965a6e0
Show file tree
Hide file tree
Showing 26 changed files with 384 additions and 57 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ project(libavg)
set(AVG_VERSION_MAJOR 2)
set(AVG_VERSION_MINOR 0)
set(AVG_VERSION_MICRO 0)
set(AVG_VERSION_EXTRA dev0) # optional
set(AVG_VERSION_EXTRA dev2) # optional
set(AVG_VERSION_RELEASE "${AVG_VERSION_MAJOR}.${AVG_VERSION_MINOR}.${AVG_VERSION_MICRO}")
if(DEFINED AVG_VERSION_EXTRA)
set(AVG_VERSION_RELEASE "${AVG_VERSION_RELEASE}.${AVG_VERSION_EXTRA}")
Expand Down
1 change: 1 addition & 0 deletions CreateVersionFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#define AVG_VERSION_BUILDER "{builder}"
#define AVG_VERSION_BUILDTIME "{buildtime}"
#define AVG_VERSION_REVISION "{revision}"
#define AVG_VERSION_BRANCH "{branch}"
#define AVG_VERSION_MAJOR "{major}"
#define AVG_VERSION_MINOR "{minor}"
#define AVG_VERSION_MICRO "{micro}"
Expand Down
27 changes: 24 additions & 3 deletions python/libavg/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import math

import libavg
from libavg import avg, Point2D, mtemu, player
from libavg import avg, player, Point2D, mtemu, clicktest

from . import settings
from .settings import Option
Expand Down Expand Up @@ -85,6 +85,7 @@ def __init__(self):
self._resolution = None
self._windowSize = None
self._mtEmu = None
self._clickTest = None

self._setupSettings()

Expand Down Expand Up @@ -118,11 +119,13 @@ def run(self, mainDiv, **kargs):
try:
self._runLoop()
except Exception as e:
self._stopClickTest()
self._teardownKeyboardManager()
raise

mainDiv.onExit()

self._stopClickTest()
self._teardownKeyboardManager()

return 0
Expand Down Expand Up @@ -216,7 +219,7 @@ def _setupLogging(self):
for catPair in catMap.split(' '):
cat, strLevel = catPair.split(':')
level = getattr(avg.logger.Severity, strLevel)

libavg.avg.logger.configureCategory(cat, level)

def _setupRootNode(self):
Expand Down Expand Up @@ -312,7 +315,7 @@ def _setupResolution(self):

def _applyResolution(self):
if self.settings.get('app_windowconfig') == '':

fullscreen = self.settings.getBoolean('app_fullscreen')

if fullscreen:
Expand Down Expand Up @@ -361,6 +364,12 @@ def _setupKeyboardManager(self):
help='Toggle multitouch emulation',
modifiers=libavg.avg.KEYMOD_CTRL)

keyboardmanager.bindKeyDown(
keyname='C',
handler=self._toggleClickTest,
help='Toggle click test',
modifiers=libavg.avg.KEYMOD_CTRL)

self.debugPanel.setupKeys()

def _toggleMtEmulation(self):
Expand Down Expand Up @@ -391,6 +400,18 @@ def _toggleMtEmulation(self):
del self._mtEmu
self._mtEmu = None

def _toggleClickTest(self):
if not self._clickTest:
self._clickTest = clicktest.ClickTest()
self._clickTest.start()
else:
self._stopClickTest()

def _stopClickTest(self):
if self._clickTest:
self._clickTest.stop()
self._clickTest = None

def _teardownKeyboardManager(self):
keyboardmanager.unbindAll()

Expand Down
202 changes: 202 additions & 0 deletions python/libavg/clicktest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# libavg - Media Playback Engine.
# Copyright (C) 2020 Thomas Schott, <scotty at c-base dot org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Current versions can be found at www.libavg.de

import logging
import random

from libavg import avg, player, Point2D

logger = logging.getLogger(__name__)


class ClickTest(object):
def __init__(self, mouse=False, maxTouches=10, probabilities=(0.5, 0.1, 0.2),
visualize=True):
self._mouse = bool(mouse)
self._maxTouches = max(0, maxTouches)
self._probabilities = tuple(min(1.0, max(0.0, p)) for p in probabilities)
self._visualize = bool(visualize)
self._contacts = None
self._frameHandlerID = None

def start(self):
if self._frameHandlerID is not None:
return

def onFrame():
for contact in self._contacts:
contact.update()

self._contacts = [
_TouchContact(self._probabilities, self._visualize)
for _ in xrange(self._maxTouches)
]
if self._mouse:
self._contacts.append(_MouseContact(self._probabilities, self._visualize))
self._frameHandlerID = player.subscribe(player.ON_FRAME, onFrame)
logger.info('click test started')

def stop(self):
if self._frameHandlerID is None or not player.isPlaying():
return

player.unsubscribe(self._frameHandlerID)
self._frameHandlerID = None
for contact in self._contacts:
contact.delete()
self._contacts = None
logger.info('click test stopped')


class _Contact(object):
def __init__(self, probabilities, visualize):
self._downProbability, self._upProbability, self._moveProbability = probabilities
rootNode = player.getRootNode()
self._maxX = rootNode.width - 1
self._maxY = rootNode.height - 1
self._pos = None
if visualize:
self._posNode = avg.CircleNode(
r=10, strokewidth=2, fillcolor='FF0000', fillopacity=0.5,
active=False, sensitive=False, parent=rootNode
)
self._lineNode = avg.PolyLineNode(
color='FF0000', active=False, sensitive=False, parent=rootNode
)
else:
self._posNode = None
self._lineNode = None
self._testHelper = player.getTestHelper()

def delete(self):
if self._pos:
self._up()
if self._posNode:
self._posNode.unlink(True)
self._lineNode.unlink(True)

def update(self):
if self._pos:
if random.random() <= self._upProbability:
self._up()
elif random.random() <= self._moveProbability:
self._move()
elif random.random() <= self._downProbability:
self._down()

def _down(self):
assert not self._pos
self._pos = self._getRandomPos()
if self._posNode:
self._posNode.pos = self._pos
self._lineNode.pos = [self._pos]
self._posNode.active = True

def _up(self):
assert self._pos
self._pos = None
if self._posNode:
self._posNode.active = False
self._lineNode.active = False

def _move(self):
assert self._pos
self._pos = self._getRandomPos()
if self._posNode:
self._posNode.pos = self._pos
self._lineNode.pos = self._lineNode.pos + [self._pos]
self._lineNode.active = True

def _getRandomPos(self):
return Point2D(random.randint(0, self._maxX), random.randint(0, self._maxY))


class _MouseContact(_Contact):
def _down(self):
super(_MouseContact, self)._down()
x, y = self._pos
self._testHelper.fakeMouseEvent(
avg.Event.CURSOR_DOWN, True, False, False, int(x), int(y), 1
)

def _up(self):
x, y = self._pos
super(_MouseContact, self)._up()
self._testHelper.fakeMouseEvent(
avg.Event.CURSOR_UP, True, False, False, int(x), int(y), 1
)

def _move(self):
super(_MouseContact, self)._move()
x, y = self._pos
self._testHelper.fakeMouseEvent(
avg.Event.CURSOR_MOTION, True, False, False, int(x), int(y), 0
)


class _TouchContact(_Contact):
__nextID = 0

@classmethod
def _getNextID(cls):
nextID = cls.__nextID
cls.__nextID += 1
return nextID

def __init__(self, *args):
super(_TouchContact, self).__init__(*args)
self._id = None

def _down(self):
assert self._id is None
super(_TouchContact, self)._down()
self._id = self._getNextID()
self._testHelper.fakeTouchEvent(
self._id, avg.Event.CURSOR_DOWN, avg.Event.TOUCH, self._pos
)

def _up(self):
assert self._id is not None
pos = self._pos
super(_TouchContact, self)._up()
self._testHelper.fakeTouchEvent(
self._id, avg.Event.CURSOR_UP, avg.Event.TOUCH, pos
)
self._id = None

def _move(self):
assert self._id is not None
super(_TouchContact, self)._move()
self._testHelper.fakeTouchEvent(
self._id, avg.Event.CURSOR_MOTION, avg.Event.TOUCH, self._pos
)


if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)

player.createMainCanvas(size=(800, 600))
ct = ClickTest(mouse=True, maxTouches=3)
ct.start()
player.setTimeout(5000, ct.stop)
player.play()
ct.stop()

0 comments on commit 965a6e0

Please sign in to comment.