1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-
# ---------------------------------------------------------------------------#
#
# Copyright (C) 1998-2003 Markus Franz Xaver Johannes Oberhumer
# Copyright (C) 2003 Mt. Hood Playing Card Co.
# Copyright (C) 2005-2009 Skomoroh
#
# 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 3 of the License, 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------------#
# kivy implementation:
# most of the code will not be used, but some important function have been
# emulated.
from __future__ import division
import logging
import os
from array import array
from kivy.core.image import Image as CoreImage
from kivy.core.text import Label as CoreLabel
from kivy.graphics.texture import Texture
from pysollib.kivy.LApp import LTopLevel0
# ************************************************************************
# * window manager util
# ************************************************************************
def wm_withdraw(window):
window.wm_withdraw()
def wm_map(window, maximized=0, fullscreen=0):
return
# ************************************************************************
# * window util
# ************************************************************************
def setTransient(window, parent, relx=None, rely=None, expose=1):
# Make an existing toplevel window transient for a parent.
#
# The window must exist but should not yet have been placed; in
# other words, this should be called after creating all the
# subwidget but before letting the user interact.
# not used in kivy (highly tk specific).
return
# ANM: werden ev. immer noch vom pysollib core benötigt/referenziert:
# (intern in kivy bitte nicht direkt benutzen).
def makeToplevel(parent, title=None):
print('tkutil: makeTopLevel')
# Create a Toplevel window.
#
window = LTopLevel0(parent, title)
# window = LTopLevelPopup(parent, title)
return window.content
def make_help_toplevel(app, title=None):
# Create an independent Toplevel window.
window = app.top
# from pysollib.winsystems import init_root_window
# window = Tkinter.Tk(className=TITLE)
# init_root_window(window, app)
return window
# ************************************************************************
# * bind wrapper - Tkinter doesn't properly delete all bindings
# ************************************************************************
__mfx_bindings = {}
__mfx_wm_protocols = ("WM_DELETE_WINDOW", "WM_TAKE_FOCUS", "WM_SAVE_YOURSELF")
def bind(widget, sequence, func, add=None):
# logging.info('tkutil: bind %s %s %s %s '
# % (widget, sequence, func, add))
# logging.info('tkutil: bind canvas = ' % str(widget.canvas))
if hasattr(widget, 'bindings'):
# logging.info('tkutil: bind %s %s %s %s '
# % (sequence, widget, func, add))
widget.bindings[sequence] = func
else:
# logging.info('tkutil: bind failed %s %s' % (sequence, widget))
pass
if (sequence == '<KeyPress-Left>'):
return
if (sequence == '<KeyPress-Right>'):
return
if (sequence == '<KeyPress-Prior>'):
return
if (sequence == '<KeyPress-Next>'):
return
if (sequence == '<KeyPress-Up>'):
return
if (sequence == '<KeyPress-Down>'):
return
if (sequence == '<KeyPress-Begin>'):
return
if (sequence == '<KeyPress-Home>'):
return
if (sequence == '<KeyPress-End>'):
return
if (sequence == '<KeyPress-Down>'):
return
if (sequence == '<4>'):
return
if (sequence == '<5>'):
return
if (sequence == '<1>'):
return
if (sequence == '<Motion>'):
return
if (sequence == '<ButtonRelease-1>'):
return
if (sequence == '<Control-1>'):
return
if (sequence == '<Shift-1>'):
return
if (sequence == '<Double-1>'):
return
if (sequence == '<3>'):
return
if (sequence == '<2>'):
return
if (sequence == '<Control-3>'):
return
if (sequence == '<Enter>'):
return
if (sequence == '<Leave>'):
return
if (sequence == '<Unmap>'):
return
if (sequence == '<Configure>'):
return
pass
def unbind_destroy(widget):
# logging.info('tkutil: unbind %s' % (widget))
widget.bindings = []
pass
# ************************************************************************
# * timer wrapper - Tkinter doesn't properly delete all commands
# ************************************************************************
def after(widget, ms, func, *args):
# print('tkutil: after(%s, %s, %s, %s)' % (widget, ms, func, args))
if (ms == 'demo'):
print('demo use')
from pysollib.kivy.LApp import LAfterAnimation
LAfterAnimation(func, 0.6)
return
elif (isinstance(ms, int)):
# print('ms: play timer (accounting)')
# Clock.schedule_once(lambda dt: func(), float(ms)/1000.0)
# makes not sense, drains battery!
pass
def after_idle(widget, func, *args):
# NOTE: This is called from the core in demo mode only.
# 'func' executes the next step in the game.
return after(widget, "demo", func, *args)
def after_cancel(t):
# print('tkutil: after_cancel()')
pass
# ************************************************************************
# * image handling
# ************************************************************************
# Wrappers
LCoreImage = CoreImage # noqa
class LImageInfo(object): # noqa
def __init__(self, arg):
if type(arg) is Texture:
self.filename = None
self.source = None
self.texture = arg
self.size = self.texture.size
if type(arg) is str:
self.filename = arg
self.source = arg
self.texture = LCoreImage(arg).texture
self.size = self.texture.size
# pysol core needs that:
def subsample(self, image):
return self
def width(self):
return self.size[0]
def height(self):
return self.size[1]
def getWidth(self):
return self.size[0]
def getHeight(self):
return self.size[1]
# ************************************************************************
# Interface to core.
def makeImage(file=None, data=None, dither=None, alpha=None):
if data is None:
assert file is not None
return LImageInfo(file)
else:
assert data is not None
return LImageInfo(data)
loadImage = makeImage # noqa - sorry flake8, aber das gehört zu oben dazu!
def copyImage(image, x, y, width, height):
# wird das überhaupt aufgerufen - ja bei SubSampleImage.
# aber: wo wird das gebraucht? - oder ist es eine altlast
# welche gar keine Relevanz mehr hat ? (Kann auch None
# zurückgeben, ohne dass etwas fehlt oder abstürzt).
tregion = image.texture.get_region(x, y, width, height)
return LImageInfo(tregion)
def fillTexture(texture, fill, outline=None, owidth=1):
# logging.info("fillImage: t=%s, f=%s o=%s, w=%s" %
# (texture, fill, outline, owidth))
# O.K. Kivy
if not fill and not outline:
return
width = texture.width
height = texture.height
ox = round(owidth)
ow = int(ox) # muss int sein!
if width <= 2 * ow or height <= 2 * ow:
fill = fill or outline
outline = None
if not fill:
fi0 = 0
fi1 = 0
fi2 = 0
fi3 = 0
else:
# wir erwarten Werte als '#xxxxxx' (color Werte in Tk notation)
# (optional mit transparenz)
if (fill[0] == '#'):
fill = fill[1:]
fi0 = int(fill[0:2], 16)
fi1 = int(fill[2:4], 16)
fi2 = int(fill[4:6], 16)
fi3 = 255
if len(fill) >= 8:
fi3 = int(fill[6:8], 16)
if not outline:
f = (fi0, fi1, fi2, fi3) * width
f = (f, ) * height
assert len(f) == height
f = sum(f, ())
assert len(f) == height * width * 4
arr = array('B', f)
texture.blit_buffer(arr, colorfmt='rgba', bufferfmt='ubyte')
else:
if (outline[0] == '#'):
outline = outline[1:]
ou0 = int(outline[0:2], 16)
ou1 = int(outline[2:4], 16)
ou2 = int(outline[4:6], 16)
ou3 = 255
if len(outline) >= 8:
ou3 = int(outline[6:8], 16)
l1 = (
ou0,
ou1,
ou2,
ou3,
) * width
l2 = (ou0, ou1, ou2, ou3, ) * ow + (fi0, fi1, fi2, fi3, ) * \
(width - 2 * ow) + (ou0, ou1, ou2, ou3, ) * ow
f = (l1, ) * ow + (l2, ) * (height - 2 * ow) + (l1, ) * ow
assert len(f) == height
f = sum(f, ())
assert len(f) == height * width * 4
arr = array('B', f)
texture.blit_buffer(arr, colorfmt='rgba', bufferfmt='ubyte')
def createImage(width, height, fill, outline=None, outwidth=1):
logging.info("createImage: w=%s, h=%s, f=%s, o=%s, ow=%s" %
(width, height, fill, outline, outwidth))
# test stellungen:
# if (fill==None):
# fill = '#00cc00'
# if (outline==None):
# outline = '#ff00ff'
# if (fill is None and (outline is None or outline == '')):
# outline = '#fff000'
# outwidth = 1
texture = Texture.create(size=(width, height), colorfmt='rgba')
fillTexture(texture, fill, outline, outwidth)
image = LImageInfo(texture)
# logging.info("createImage: LImageInfo create %s" % image)
return image
def createImagePIL(width, height, fill, outline=None, outwidth=1):
# Is this needed for Kivy?
createImage(width, height, fill, outline=outline, outwidth=outwidth)
# wird nur mit USE_PIL benutzt: nicht relevant. Der code wird mit
# Kivy nie durchlaufen.
def shadowImage(image, color='#3896f8', factor=0.3):
logging.info("shadowImage: ")
# TBD.
return None
# Kivy nicht benötigt. aber - was tut das ?
# wurde aufgerufen, als der erste König auf die Foundation
# gezogen wurde. (möglicherweise eine Gewonnen! - Markierung).
def markImage(image):
logging.info("markImage: ")
return None
def _createImageMask(texture, color):
col = 0
if (color == 'black'):
col = 0
if (color == 'white'):
col = 255
g = texture.pixels
arr = array('B', g)
for mx in range(int(len(arr) / 4)):
m = 4 * mx
if arr[m + 3] < 128:
arr[m + 3] = 0
arr[m] = arr[m + 1] = arr[m + 2] = 0
else:
arr[m + 3] = 32
arr[m] = arr[m + 1] = arr[m + 2] = col
mask = Texture.create(size=texture.size, colorfmt='rgba')
mask.blit_buffer(arr, colorfmt='rgba', bufferfmt='ubyte')
return mask
def _scaleTextureToSize(texture, size):
width = size[0]
height = size[1]
g = texture.pixels
ag = array('B', g)
gw, gh = texture.size
# print('size:',width,height)
# print('texture size:',gw,gh)
bb = array('B', [0 for x in range(width * height * 4)])
# print ('bb length: ',len(bb))
# print ('gg length: ',gw*gh*4)
scalex = width / gw
scaley = height / gh
# scale, x und y offset bestimmen.
scale = scaley
if (scalex < scaley):
scale = scalex
offx = (width - gw * scale) / 2
offy = (height - gh * scale) / 2
# print ('scale: ',scalex,'/',scaley,' -> ',scale)
# print ('offs: ',offx,'/',offy)
for bi in range(height):
bline = bi * width
if (bi >= offy) and (bi < (height - offy)):
# transfer
ai = gh - int((bi - offy) / scale) - 1
aline = ai * gw
for bk in range(width):
bpos = (bline + bk) * 4
if (bk >= offx) and (bk < (width - offx)):
# transfer
ak = int((bk - offx) / scale)
apos = (aline + ak) * 4
bb[bpos] = ag[apos]
bb[bpos + 1] = ag[apos + 1]
bb[bpos + 2] = ag[apos + 2]
bb[bpos + 3] = ag[apos + 3]
else:
# transparent
bb[bpos + 3] = 0
else:
# transparent
for bk in range(width):
bb[(bline + bk) * 4 + 3] = 0
stext = Texture.create(size=(width, height), colorfmt='rgba')
stext.blit_buffer(bb, colorfmt='rgba', bufferfmt='ubyte')
return stext
def _pasteTextureTo(texture, totexture):
g = texture.pixels
ag = array('B', g)
gw, gh = texture.size
t = totexture.pixels
at = array('B', t)
tw, th = totexture.size
if (tw != gw) or (th != gh):
return
for i in range(int(len(ag) / 4)):
i4 = i * 4
if ag[i4 + 3] > 128:
at[i4] = ag[i4]
at[i4 + 1] = ag[i4 + 1]
at[i4 + 2] = ag[i4 + 2]
at[i4 + 3] = ag[i4 + 3]
stext = Texture.create(size=(tw, th), colorfmt='rgba')
stext.blit_buffer(at, colorfmt='rgba', bufferfmt='ubyte')
return stext
def createBottom(image, color='white', backfile=None):
backfilebase = None
if backfile is not None:
backfilebase = os.path.basename(backfile)
logging.info("createBottom: %s | %s" % (color, backfilebase))
# print('createBottom:',image)
# th = 1 # thickness
# size = (w - th * 2, h - th * 2)
# original: zeichnet noch eine outline um die karte - können wir nicht.
tmp0 = _createImageMask(image.texture, color)
if backfile:
tmp1 = LCoreImage(backfile)
txtre = _scaleTextureToSize(tmp1.texture, image.texture.size)
tmp = _pasteTextureTo(txtre, tmp0)
else:
tmp = tmp0
img = LImageInfo(tmp)
img.size = (image.getWidth(), image.getHeight())
return img
'''
im = image._pil_image
th = 1 # thickness
sh = Image.new('RGBA', im.size, color)
out = Image.composite(sh, im, im)
w, h = im.size
size = (w - th * 2, h - th * 2)
tmp = Image.new('RGBA', size, color)
tmp.putalpha(60)
mask = out.resize(size, Image.ANTIALIAS)
out.paste(tmp, (th, th), mask)
if backfile:
back = Image.open(backfile).convert('RGBA')
w0, h0 = back.size
w1, h1 = im.size
a = min(float(w1) / w0, float(h1) / h0)
a = a * 0.9
w0, h0 = int(w0 * a), int(h0 * a)
back = back.resize((w0, h0), Image.ANTIALIAS)
x, y = (w1 - w0) / 2, (h1 - h0) / 2
out.paste(back, (x, y), back)
return PIL_Image(image=out)
'''
# ************************************************************************
# * font utils
# ************************************************************************
def get_text_width(text, font, root=None):
logging.info("get_text_width: %s %s" % (text, font))
label = CoreLabel()
label.text = text
label.refresh()
return label.content_width
# return Font(root=root, font=font).measure(text)
|