Permalink
Browse files

ui changes

  • Loading branch information...
zielmicha committed Dec 28, 2011
1 parent 2e35fc7 commit 3b2ff9d907461f1057c05a9da31f9aecaa45e1ee
Showing with 248 additions and 58 deletions.
  1. +1 −1 android/info.sh
  2. 0 build.sh
  3. +1 −1 lib/client/freeciv.py
  4. +7 −0 lib/features.py
  5. +18 −41 lib/main.py
  6. +149 −0 lib/menus.py
  7. +11 −2 lib/sync.py
  8. +25 −0 lib/test_glue.py
  9. +36 −13 lib/ui.py
View
@@ -5,5 +5,5 @@ PKG_PACKAGE="pl.org.zielinscy.freeciv"
PKG_ARG="--orientation landscape --permission INTERNET --icon $BASE/freeciv-client.png --presplash $BASE/presplash.png"
# --presplash $GFX/splashscreen.png --icon $GFX/logo.png
RELEASE_VERSION="1.0.4"
-RELEASE_VERSION_ID="1004" # remember to change civsync.ua in sync.py:38
+RELEASE_VERSION_ID="1005" # remember to change civsync.ua in sync.py:38
ANDROID_PATH="$HOME/apps/android-sdk-linux_x86/tools:$HOME/apps/android-ndk-r6b"
View
0 build.sh 100644 → 100755
No changes.
View
@@ -68,7 +68,7 @@ def _callback(funname, *args):
else:
print 'no function named %s %s' % (name, args)
except:
- traceback.print_exc()
+ traceback.print_exc(limit=200)
print 'Abort.'
_end_callbacks()
if hard_exit:
View
@@ -57,13 +57,20 @@ def parse_options():
args = sys.argv[1:]
unused = [sys.argv[0]]
for arg in args:
+ if arg == '-f:_list':
+ show_feature_list()
+ sys.exit(1)
if arg.startswith('-f:'):
arg = arg[len('-f:'):]
_parse_arg(arg)
else:
unused.append(arg)
sys.argv = unused
+def show_feature_list():
+ for key in sorted(feature_types):
+ print '%s = [%s] %s' % (key.rjust(35), feature_types[key].__name__, str(features[key]).ljust(35))
+
def _parse_arg(arg):
if '=' in arg:
key, val = arg.split('=', 1)
View
@@ -35,12 +35,10 @@
import features
import monitor
import options
+import menus
from sync import lzma
-def new_game():
- save.new_game()
-
features.add_feature('app.debug', default=True, type=bool)
features.add_feature('app.autoupdate', default=True, type=bool)
features.add_feature('app.forcesize')
@@ -60,24 +58,31 @@ def apply_hardexit(t):
pause_file = os.path.join(save.get_save_dir(), 'pause_options')
def client_main():
- action = sys.argv[1] if sys.argv[1:] else None
if try_resume():
ui.main()
return
+ ui.set(ui.Label('loading...'))
+ ui.execute_later.append(app_main)
+
+ if features.get('app.profile'):
+ profile_main()
+ else:
+ ui.main()
+
+def app_main():
+ action = sys.argv[1] if sys.argv[1:] else None
+
if action == 'load':
savename = sys.argv[2]
save.load_game(savename)
elif action == 'connect':
host, port = sys.argv[2:]
save.connect(host, int(port))
+ elif action == 'eval':
+ exec sys.argv[2]
else:
- show_main_menu()
-
- if features.get('app.profile'):
- profile_main()
- else:
- ui.main()
+ menus.main_menu()
def profile_main():
import cProfile
@@ -86,36 +91,18 @@ def profile_main():
p = pstats.Stats('profileout')
p.strip_dirs().sort_stats('time').print_stats()
-def show_main_menu():
- global main_menu
- main_menu = menu = ui.Menu()
-
- menu.add('New game', new_game)
- menu.add('Load game', save.load_dialog)
- menu.add('Feedback', feedback)
- menu.add('Options', options.show_options)
- if features.get('app.multiplayer'):
- menu.add('Connect', save.connect_dialog)
-
- ui.set(menu)
-
-def feedback():
- panel = ui.LinearLayoutWidget()
- panel.add(ui.Label('Leaving comments with logs will help me diagnose\npotential problems with game speed and\nunexpected behaviour.'))
- panel.add(ui.Button('Leave a comment with a log (preferred)', lambda: sync.comment(get_install_time(), True)))
- panel.add(ui.Button('Leave a comment without a log', lambda: sync.comment(get_install_time(), False)))
- ui.set(panel)
-
def start_autoupdate():
thread.start_new_thread(run_autoupdate, ())
def run_autoupdate():
- install_time = get_install_time()
+ install_time = sync.get_install_time()
try:
sync.client().updates(install_time)
except sync.civsync.UpdateRequiredError as err:
with ui.execute_later_lock:
ui.execute_later.append(lambda: notify_update(err.url))
+ except Exception as err:
+ print 'Failed to autoupdate:', err
def notify_update(url):
print 'update found at', url
@@ -164,16 +151,6 @@ def unpack_data():
os.remove('data.tgz')
progress.draw_frame('', 'starting...', 1)
-def get_install_time():
- path = os.path.join(save.get_save_dir(), 'install_time')
- try:
- return int(open(path).read())
- except (IOError, ValueError):
- install_time = int(time.time() * 1000)
- with open(path, 'w') as f:
- f.write('%d' % install_time)
- return install_time
-
def check_force_size():
if features.get('app.forcesize'):
return map(int, features.get('app.forcesize').split(','))
View
@@ -0,0 +1,149 @@
+# Copyright (C) 2011 Michal Zielinski (michal@zielinscy.org.pl)
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program 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 General Public License for more details.
+
+import ui
+import pygame
+
+import sync
+import save
+import features
+import options
+
+class SpacingLayoutWidget(ui.LayoutWidget):
+ def __init__(self, item1, item2, size):
+ ui.LayoutWidget.__init__(self)
+ self.item1 = item1
+ self.item2 = item2
+ self.items = (item1, item2)
+ self.size = (0,0)
+ self._asked_size = size
+
+ def get_positions(self):
+ assert sum(map(lambda a:0 if a==0 else 1, self._asked_size)) == 1, 'exactly one value in tuple `size` should be zero'
+ is_horizontal = self._asked_size[1] == 0
+ if is_horizontal:
+ w = self._asked_size[0]
+ self.size = (w, max(self.item1.size[1], self.item2.size[1]))
+ yield (0, 0)
+ yield (w - self.item2.size[0], 0)
+ else:
+ h = self._asked_size[1]
+ self.size = (max(self.item1.size[0], self.item2.size[0]), h)
+ yield (0, 0)
+ yield (0, h - self.item2.size[1])
+
+def create_aligned_widget(widget, align, size=None):
+ if not size:
+ size = ui.screen_size
+ if align & ui.RIGHT:
+ widget = SpacingLayoutWidget(ui.Spacing(0,0), widget, (size[0], 0))
+ if align & ui.BOTTOM:
+ widget = SpacingLayoutWidget(ui.Spacing(0,0), widget, (0, size[1]))
+ return widget
+
+class PrettyMenu(ui.AbsoluteLayoutWidget):
+ def __init__(self):
+ ui.AbsoluteLayoutWidget.__init__(self)
+
+ self.load_background()
+
+ self.left = ui.HorizontalLayoutWidget(spacing=10)
+ self.left.add(ui.Spacing(0, 0))
+ self.right = ui.HorizontalLayoutWidget(spacing=10)
+ self.cont = SpacingLayoutWidget(self.left, self.right, (ui.screen_width, 0))
+ self.add(create_aligned_widget(self.cont, ui.BOTTOM, (0, ui.screen_height - 10)), (0,0))
+
+ balloon_margin = 30
+
+ def show_balloon(self, over, baloon):
+ self.update_layout()
+ baloon.update_layout()
+ pos = self.get_position_of(over)
+ self.add(baloon, (pos[0], pos[1] - baloon.size[1] - self.balloon_margin))
+
+ def load_background(self):
+ img = ui.load_image('data/user/intro.jpg')
+ img_size = img.get_size()
+ surf_size = pygame.display.get_surface().get_size()
+ new_height = float(surf_size[0]) / img_size[0] * img_size[1]
+ self.background_margin_top = (new_height - surf_size[1]) / -2.5
+ self.background = pygame.transform.smoothscale(img, (surf_size[0], int(new_height)))
+
+ def draw(self, surf, (x, y)):
+ surf.blit(self.background, (x, y + self.background_margin_top))
+ super(PrettyMenu, self).draw(surf, (x, y))
+
+class MenuButton(ui.Button):
+ active_bg = (255, 255, 200, 20)
+ bg = (150, 150, 50, 20)
+ fg = (150, 150, 50)
+
+ def __init__(self, text, callback):
+ ui.Button.__init__(self, text, callback, force_width=100)
+
+ def set_text(self, label):
+ if '\n' not in label:
+ label += '\n '
+ ui.Button.set_text(self, label)
+
+class Balloon(ui.AbsoluteLayoutWidget):
+ margin = 10
+
+ def __init__(self):
+ ui.AbsoluteLayoutWidget.__init__(self)
+ self.content = ui.HorizontalLayoutWidget()
+ self.add(self.content, (self.margin, self.margin))
+
+ def draw(self, surf, pos):
+ rect = pos + (self.content.size[0] + self.margin*2, self.content.size[1] + self.margin*2)
+ tpos = (pos[0] + 10, pos[1] + self.content.size[1] + self.margin*2 - 1)
+ tw = th = 18
+
+ ui._round_rect(surf, (255,255,255,255), rect, 0, 10, 10)
+ pygame.gfxdraw.trigon(surf, tpos[0], tpos[1], tpos[0]+tw, tpos[1], tpos[0], tpos[1]+th, (255,255,255))
+ pygame.gfxdraw.filled_trigon(surf, tpos[0], tpos[1], tpos[0]+tw, tpos[1], tpos[0], tpos[1]+th, (255,255,255))
+ ui._round_rect(surf, (255,255,0,255), rect, 1, 10, 10)
+ ui.AbsoluteLayoutWidget.draw(self, surf, pos)
+
+class _D(ui.Label):
+ 'Label that prints its pos - for debugging'
+ def draw(self, surf, pos):
+ print pos, '(clip=%r)' % surf.get_clip()
+ ui.Label.draw(self, surf, pos)
+
+def main_menu():
+ menu = PrettyMenu()
+
+ menu.left.add(MenuButton('New\ngame', save.new_game))
+ load_game_button = MenuButton('Load\ngame', save.load_dialog)
+ menu.left.add(load_game_button)
+ if features.get('app.multiplayer'):
+ menu.left.add(MenuButton('Connect', save.connect_dialog))
+
+ menu.right.add(MenuButton('Feed\nback', feedback))
+ menu.right.add(MenuButton('Sett\nings', options.show_options))
+ menu.right.add(MenuButton('Exit', None))
+ menu.right.add(ui.Spacing(0, 0))
+
+ #balloon = Balloon()
+ #balloon.content.add(ui.Label('hello'))
+ #menu.show_balloon(load_game_button, balloon)
+
+ ui.replace(menu)
+
+
+def feedback():
+ panel = ui.LinearLayoutWidget()
+ panel.add(ui.Label('Leaving comments with logs will help me diagnose\npotential problems with game speed and\nunexpected behaviour.'))
+ panel.add(ui.Button('Leave a comment with a log (preferred)', lambda: sync.comment(sync.get_install_time(), True)))
+ panel.add(ui.Button('Leave a comment without a log', lambda: sync.comment(sync.get_install_time(), False)))
+ ui.set(panel)
View
@@ -33,7 +33,7 @@ def apply_user_agent_change(ua):
civsync.USER_AGENT = ua
features.set_applier('civsync.ua', apply_user_agent_change)
-features.set('civsync.ua', 'CivSyncAndroid/1004')
+features.set('civsync.ua', 'CivSyncAndroid/1005')
features.add_feature('civsync.enable', False, type=bool)
session = None
@@ -219,5 +219,14 @@ def sync_request(callback, name, args, kwargs):
with ui.execute_later_lock:
ui.execute_later.append(lambda: callback(result))
-#ui.execute_later.append()
+
+def get_install_time():
+ path = os.path.join(save.get_save_dir(), 'install_time')
+ try:
+ return int(open(path).read())
+ except (IOError, ValueError):
+ install_time = int(time.time() * 1000)
+ with open(path, 'w') as f:
+ f.write('%d' % install_time)
+ return install_time
View
@@ -0,0 +1,25 @@
+import sys, os
+
+sys.path.append(os.path.join(os.path.dirname(__file__), '../src'))
+
+import freecivclient
+import glob
+
+def callback(funname, *args):
+ if funname == 'add_function':
+ name, i = args
+ functions[name] = i
+ return
+ if funname == 'set_const':
+ return
+ return (5,)
+
+def call(name, *args):
+ freecivclient.call_f(functions[name], args)
+
+functions = {}
+
+freecivclient.set_callback(callback)
+
+while True:
+ call('call_callback', 5)
Oops, something went wrong.

0 comments on commit 3b2ff9d

Please sign in to comment.