Created
December 2, 2015 14:29
-
-
Save awesomebytes/8fafe8583146c76fe26d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Licensed under the Apache License, Version 2.0 (the "License"); | |
| you may not use this file except in compliance with the License. | |
| You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| """ | |
| import remi.gui as gui | |
| from remi import start, App | |
| import math | |
| class JoystickWidget(gui.Svg): | |
| def __init__(self, width, height): | |
| super(JoystickWidget, self).__init__(width, height) | |
| self.width = width | |
| self.height = height | |
| # self.maxDragLength = min(width, height)/3.0*2.0 | |
| self.maxDragLength = min(width, height)/3.0 | |
| self.startPointIndicator = gui.SvgCircle(0, 0, 20) | |
| self.startPointIndicator.set_fill('rgb(255,200,50)') | |
| self.startPointIndicator.set_stroke(1, 'white') | |
| self.endPointIndicator = gui.SvgCircle(0, 0, 10) | |
| self.endPointIndicator.set_fill('rgb(200,255,50)') | |
| self.endPointIndicator.set_stroke(1, 'white') | |
| self.pathLine = gui.SvgLine(0, 0, 0, 0) | |
| self.startX = 0 | |
| self.startY = 0 | |
| self.append('path_line', self.pathLine) | |
| self.append('start_point', self.startPointIndicator) | |
| self.append('end_point', self.endPointIndicator) | |
| self.drag_state = False | |
| self.set_on_onmouseup_listener(self, 'mouseup') | |
| self.set_on_onmousedown_listener(self, 'mousedown') | |
| self.set_on_onmousemove_listener(self, 'mousemove') | |
| self.EVENT_ONMOVE = "ONJOYSTICKMOVE" | |
| self.reset_joystick(0, 0) | |
| def mousemove(self, x, y): | |
| if self.drag_state: | |
| self.endPointIndicator.set_position(x, y) | |
| self.pathLine.set_p2(x, y) | |
| moveX = min(abs(x-self.startX),self.maxDragLength)/self.maxDragLength * (-1 if x < self.startX else 1) | |
| moveY = min(abs(y-self.startY),self.maxDragLength)/self.maxDragLength * (1 if y < self.startY else -1) | |
| params = [moveX,moveY] | |
| return self.eventManager.propagate(self.EVENT_ONMOVE, params) | |
| def mousedown(self, x, y): | |
| self.drag_state = True | |
| #self.startPointIndicator.set_position(x, y) | |
| self.startPointIndicator.set_position(self.width/2, self.height/2) | |
| #self.pathLine.set_p1(x, y) | |
| self.pathLine.set_p1(self.width/2, self.height/2) | |
| # self.startX = x | |
| # self.startY = y | |
| self.startX = self.width/2 | |
| self.startY = self.height/2 | |
| def mouseup(self, x, y): | |
| self.reset_joystick(x, y) | |
| def reset_joystick(self, x, y): | |
| self.drag_state = False | |
| self.startPointIndicator.set_position(self.width/2, self.height/2) | |
| self.endPointIndicator.set_position(self.width/2, self.height/2) | |
| self.pathLine.set_coords(self.width/2, self.height/2, self.width/2, self.height/2) | |
| params = [0,0] | |
| return self.eventManager.propagate(self.EVENT_ONMOVE, params) | |
| def set_onmove_listener(self, listener, funcname): | |
| self.eventManager.register_listener(self.EVENT_ONMOVE, listener, funcname) | |
| class MyApp(App): | |
| def __init__(self, *args): | |
| super(MyApp, self).__init__(*args) | |
| def main(self, name='world'): | |
| self.wid = gui.Widget(620, 650, False, 10) | |
| self.infoLabel = gui.Label(300, 30, "coords") | |
| self.joystick = JoystickWidget(600, 600) | |
| self.joystick.style['background-color'] = 'rgb(255,255,255)' | |
| self.joystick.set_onmove_listener(self, "joystick_moved") | |
| self.wid.append('infolabel', self.infoLabel) | |
| self.wid.append('joystick', self.joystick) | |
| self.wid.set_on_onmousemove_listener(self.joystick, 'reset_joystick') | |
| # returning the root widget | |
| return self.wid | |
| def joystick_moved(self, x, y): # 0 <= x <= 1 0 <= y <= 1 | |
| self.infoLabel.set_text("move x:%s y:%s" % (x, y)) | |
| if __name__ == "__main__": | |
| start(MyApp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment