-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObject.py
executable file
·1098 lines (881 loc) · 31.9 KB
/
Object.py
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from random import randint
from copy import deepcopy
import localizer
# Class for generic game objects and upper class for all the other objects
class Object(object):
generalName = localizer.translate(
'classObject', 'solidObject')
generalNameAdessive = ""
#generalNameAdessive = localizer.translate(
# 'classObject', 'solidObjectAdessive')
# Generic attributes for objects
objectAttributes = {'object': {'music': ''}, 'className': 'Image'}
# Static method to create unique object ID
usedIds = []
def createUniqueId(newId=None):
if not (newId):
newId = str(randint(0, 1000000000))
failed = False
failCount = 0
originalId = newId
# Loop till unique ID found
while (True):
if not (newId in Object.usedIds):
if (failed):
print(
"Warning: Duplicate object ID '%s', new ID set as '%s'"
% (originalId, newId))
Object.usedIds.append(newId)
return newId
failCount += 1
failed = True
newId = "%s_duplicate_%i" % (originalId, failCount)
def __init__(self, parentView, objectId, images, objectAttributes):
if not (objectAttributes):
objectAttributes = Object.objectAttributes
if not (images):
images = [JSONImage.imageAttributes]
# Create image objects and objectId
self.images = []
if not (isinstance(self, JSONImage)):
for image in images:
self.images.append(
JSONImage(parentView, image, objectAttributes))
if (objectId):
self.id = Object.createUniqueId(objectId)
else:
self.id = Object.createUniqueId()
# JSONImage doesn't need an image or an ID check because
# images can have the same ID as their owners
else:
self.id = objectId
i = 0
for image in self.images:
i += 1
if(image.getID() == ""):
image.setID(self.id + "_" + str(i))
self.parentView = parentView
self.objectAttributes = objectAttributes
try:
self.texts = parentView.scenarioData.texts[self.id]
except KeyError:
self.texts = {}
print(
"Warning: Could not find texts.json entry for object '%s'"
% (self.id))
# Fill in attributes from objects that were missing during __init__
# Every item needs to implement this
def postInit(self, getGameObject):
return
def getImages(self):
return self.images
def getImage(self, imageId):
for image in self.images:
if (image.id == imageId):
return image
return None
def getClassname(self):
return self.objectAttributes["className"]
def getName(self):
return self.getRepresentingImage().getName()
def setName(self, name):
self.getRepresentingImage().setName(name)
def getPosition(self):
return self.getRepresentingImage().getCoordinates()
def setPosition(self, position):
#print ("Position set to: ", position.x(), position.y())
self.getRepresentingImage().setCoordinates(position.x(), position.y())
def initPosition(self):
self.getRepresentingImage().setCoordinates(0, 0)
# Returns of the most "representing" image for an item such as open door
# instead closed door image
# Every item needs to override this to act properly
def getRepresentingImage(self):
return self.images[0]
# Get the image activated by the given item
# Should be overriden by other objects
def getUseImage(self, useItem):
return self.images[0]
def getExamineText(self):
try:
return self.getRepresentingImage().texts["examine"]
except:
return
# Set item's examine (click) text
def setExamineText(self, examineText):
self.getRepresentingImage().texts["examine"] = examineText
# Remove this object
def removeObject(self):
self.parentView.removeObject(self)
# Set whether clicking the object game will end
# TODO: Set end layer too instead of having it hardcoded
def setIsEnding(self, isEnding):
if (isEnding):
self.objectAttributes["object"]["ending"] = "end_layer"
else:
try:
del self.objectAttributes["object"]["ending"]
except KeyError:
return
def getIsEnding(self):
if ("ending" in self.objectAttributes["object"]):
return True
return False
# Remove text with the given text key
def removeText(self, textKey):
newTexts = dict(self.texts)
try:
del newTexts[textKey]
except KeyError:
return
self.texts = newTexts
# Pickable item
class Item(Object):
generalName = localizer.translate('classItem', 'itemName')
generalNameAdessive = ""
# Generic attributes for items
objectAttributes = {
'className': 'Image',
'object': {
'consume': False,
'category': 'item',
'outcome': '',
'trigger': ''}
}
def __init__(self, parentView, itemId, images, objectAttributes):
if not (objectAttributes):
objectAttributes = Item.objectAttributes
if not (images):
images = [JSONImage.imageAttributes]
super(Item, self).\
__init__(parentView, itemId, images, objectAttributes)
# Handle these in postInit
self.trigger = None # Use on object
self.outcome = None
self.target = None # Can be any other object
self.goesInto = None
self.comesFrom = None
def postInit(self, getGameObject):
try:
self.trigger = getGameObject(
"object", self.objectAttributes["object"]["trigger"])
self.target = self.trigger
except KeyError:
pass
try:
self.outcome = getGameObject(
"object", self.objectAttributes["object"]["outcome"])
except KeyError:
pass
def getPickupText(self):
try:
return self.texts["pickup"]
except:
return
def getUse(self):
if (self.target):
return self.target
elif (self.goesInto):
return self.goesInto
elif (self.comesFrom):
return self.comesFrom
def setTarget(self, target):
self.target = target
def setGoesInto(self, target):
self.goesInto = target
self.comesFrom = None
self.trigger = None
self.target = target
self.outcome = None
def setComesFrom(self, target):
self.goesInto = None
self.comesFrom = target
self.trigger = None
self.target = target
self.outcome = None
def clearTrigger(self):
self.trigger = None
self.target = None
self.outcome = None
def clearTarget(self):
targetType = self.target.__class__.__name__
if (targetType in ("Door", "Container")):
self.target.setLocked(False)
elif (targetType in ("Item", "Obstacle")):
self.target.clearTrigger()
self.trigger = None
self.target = None
self.goesInto = None
self.comesFrom = None
self.outcome = None
# Set the item use text if target is defined
def setUseText(self, useText):
if (self.target):
self.texts[self.target.id] = useText
# Set item's pickup text
def setPickupText(self, pickupText):
if (pickupText == ""):
self.removeText("pickup")
else:
try:
self.texts["pickup"] = pickupText
except KeyError:
return
# Set item's default text
def setDefaultText(self, defaultText):
if (defaultText == ""):
self.removeText("default")
else:
self.texts["default"] = defaultText
def setInteractionText(self, targetId, interactionText):
if (interactionText == ""):
self.removeText(targetId)
else:
self.texts[targetId] = interactionText
def setOutcome(self, outcomeObject):
self.outcome = outcomeObject
def setConsume(self, isConsumed):
self.objectAttributes["object"]["consume"] = isConsumed
def getConsume(self):
return self.objectAttributes["object"]["consume"]
# Get the text displayed when this item is used on its target
def getUseText(self):
useImage = self.target.getUseImage(self)
try:
return self.texts[useImage.id]
except KeyError:
self.texts[useImage.id] = ""
return ""
# Get the image activated by the given item
def getUseImage(self, useItem):
return self.images[0]
# Set the object triggered by this item
# objectRole=0 act as a key, =1 act as inItem, =2, act as outItem
def setTargetObject(self, targetObject, objectRole=0):
if not (targetObject):
return
triggerType = targetObject.__class__.__name__
self.target = targetObject
if (triggerType in ("Object", "Item")):
self.trigger = targetObject
elif (triggerType in ("Door", "Container")):
if (objectRole == 1):
targetObject.setInItem(self)
self.goesIn = targetObject
elif (objectRole == 2):
targetObject.setOutItem(self)
self.comesFrom = targetObject
else:
targetObject.setKey(self)
elif (triggerType == "Obstacle"):
targetObject.setTrigger(self)
class Container(Object):
generalName = localizer.translate(
'classContainer', 'containerName')
generalNameAdessive = ""
# Generic attributes for containers
objectAttributes = {
'className': 'Image',
'object': {
'locked': False,
'full_image': '',
'state': 'empty',
'in': '',
'empty_image': '',
'out': '',
'category': 'container',
'blocked': False
}
}
def __init__(self, parentView, itemId, images, objectAttributes):
if not (objectAttributes):
objectAttributes = Container.objectAttributes
if not (images):
images = [JSONImage.imageAttributes]
super(Container, self).\
__init__(parentView, itemId, images, objectAttributes)
# Create the available image objects
try:
self.emptyImage = self.getImage(
objectAttributes["object"]["empty_image"])
except KeyError:
self.emptyImage = None
try:
self.lockedImage = self.getImage(
objectAttributes["object"]["locked_image"])
except KeyError:
self.lockedImage = None
try:
self.fullImage = self.getImage(
objectAttributes["object"]["full_image"])
except KeyError:
self.fullImage = None
self.texts = {}
try:
if (self.emptyImage):
self.texts.update(
parentView.scenarioData.texts[self.emptyImage.id])
if (self.lockedImage):
self.texts.update(
parentView.scenarioData.texts[self.lockedImage.id])
if (self.fullImage):
self.texts.update(
parentView.scenarioData.texts[self.fullImage.id])
except KeyError:
print(
"Warning: Could not find texts.json entry for object '%s'"
% (self.id))
# Handle these in postInit
self.key = None
self.inItem = None
self.outItem = None
def postInit(self, getGameObject):
try:
self.setKey(getGameObject(
"object", self.objectAttributes["object"]["key"]))
except KeyError:
pass
try:
self.inItem = getGameObject(
"object", self.objectAttributes["object"]["in"])
self.inItem.setGoesInto(self)
except KeyError:
pass
try:
self.outItem = getGameObject(
"object", self.objectAttributes["object"]["out"])
self.outItem.setComesFrom(self)
except KeyError:
pass
def getImages(self):
images = [self.emptyImage, self.lockedImage, self.fullImage]
return list(filter((None).__ne__, images))
def getRepresentingImage(self):
if self.emptyImage is None:
self.emptyImage = self.images[0]
return self.emptyImage
def setRepresentingImage(self, img):
self.emptyImage = img
# Get the image activated by the given item
def getUseImage(self, useItem):
if (self.key == useItem):
return self.lockedImage
elif (self.inItem == useItem):
return self.emptyImage
elif (self.outItem == useItem):
return self.fullImage
# Returns True if container is locked, otherwise False
def isLocked(self):
try:
if (self.objectAttributes["object"]["locked"] is True):
return True
except KeyError:
print(
"Warning: Attribute 'locked' not defined for\
container object '%s'"
% (self.id))
return False
def setIsLocked(self, isLocked):
self.objectAttributes["object"]["locked"] = isLocked
# Returns what unblocks the container
def getKey(self):
return self.key
def setKey(self, keyObject):
self.key = keyObject
self.key.setTarget(self)
def setInItem(self, inItemObject):
# Clear old inItem
if (self.inItem):
self.inItem.clearTarget()
# Set new inItem
self.inItem = inItemObject
if (self.inItem == self.key):
self.key = None
self.inItem.setGoesInto(self)
# Set it to object's attributes
self.objectAttributes["object"]["in"] = self.inItem.id
def clearInItem(self):
if (self.inItem):
self.inItem.clearTarget()
self.inItem = None
try:
del self.objectAttributes["object"]["in"]
except KeyError:
return
def setOutItem(self, outItemObject):
# Clear old outItem
if (self.outItem):
self.outItem.clearTarget()
# Set new outItem
self.outItem = outItemObject
if (self.outItem == self.key):
self.key = None
self.outItem.setComesFrom(self)
# Set it to object's attributes
self.objectAttributes["object"]["out"] = self.outItem.id
def clearOutItem(self):
if (self.outItem):
self.outItem.clearTarget()
self.outItem = None
try:
del self.objectAttributes["object"]["out"]
except KeyError:
return
# Set or remove locked state with images etc.
# When setting locked=True, other parameters can be given
def setLocked(self, setLocked, imagePath=None, keyObject=None):
if (self.key):
self.key = None
if (setLocked):
imageObject = JSONImage(
self.parentView, None, self.objectAttributes, imageId=self.id)
if (imagePath):
imageObject.setSource(imagePath)
# TODO: Put other attributes here too (?)
self.images.append(imageObject)
self.lockedImage = imageObject
self.objectAttributes["object"]["locked_image"] = imageObject.id
self.setIsLocked(True)
if (keyObject):
self.key = keyObject
self.key.setTarget(self)
self.objectAttributes["object"]["key"] = keyObject.id
else:
try:
del self.images[self.images.index(self.lockedImage)]
except ValueError:
pass
try:
self.objectAttributes["object"]["locked_image"]
except KeyError:
pass
self.lockedImage = None
self.setIsLocked(False)
self.clearKey()
# Nullify current key
def clearKey(self):
if (self.key):
self.key.clearTarget()
self.key = None
class Door(Object):
generalName = localizer.translate(
'classDoor', 'doorName')
generalNameAdessive = ""
# Generic attributes for doors
objectAttributes = {
'className': 'Image',
'object': {
'category': '',
'state': 'open',
'locked': False,
'transition': '',
'blocked': False,
'open_image': ''
}
}
def __init__(self, parentView, itemId, images, objectAttributes):
if not (objectAttributes):
objectAttributes = Door.objectAttributes
if not (images):
images = [JSONImage.imageAttributes]
super(Door, self).\
__init__(parentView, itemId, images, objectAttributes)
# Create the available image objects
try:
self.closedImage = self.getImage(
objectAttributes["object"]["closed_image"])
except KeyError:
self.closedImage = None
try:
self.lockedImage = self.getImage(
objectAttributes["object"]["locked_image"])
except KeyError:
self.lockedImage = None
try:
self.openImage = self.getImage(
objectAttributes["object"]["open_image"])
except KeyError:
self.openImage = None
try:
self.blockedImage = self.getImage(
objectAttributes["object"]["blocked_image"])
except KeyError:
self.blockedImage = None
self.texts = {}
try:
if (self.closedImage):
self.texts.update(
parentView.scenarioData.texts[self.closedImage.id])
if (self.lockedImage):
self.texts.update(
parentView.scenarioData.texts[self.lockedImage.id])
if (self.openImage):
self.texts.update(
parentView.scenarioData.texts[self.openImage.id])
if (self.blockedImage):
self.texts.update(
parentView.scenarioData.texts[self.blockedImage.id])
except KeyError:
print(
"Warning: Could not find texts.json entry for object '%s'"
% (self.id))
# Handle these in postInit
self.key = None
self.transition = None
def postInit(self, getGameObject):
try:
self.setKey(
getGameObject(
"object", self.objectAttributes["object"]["key"]))
except KeyError:
pass
try:
self.transition = getGameObject(
"room", self.objectAttributes["object"]["transition"])
except KeyError:
pass
# Set or remove locked state with images etc.
# When setting locked=True, other parameters can be given
def setLocked(self, setLocked, imagePath=None, keyObject=None):
if (self.key):
self.key = None
if (setLocked):
# Create locked image
imageObject = JSONImage(
self.parentView, None, self.objectAttributes, imageId=self.id)
if (imagePath):
imageObject.setSource(imagePath)
# TODO: Put other attributes here too
self.images.append(imageObject)
self.lockedImage = imageObject
self.objectAttributes["object"]["locked_image"] = imageObject.id
self.setIsLocked(True)
if (keyObject):
self.key = keyObject
self.key.setTarget(self)
self.objectAttributes["object"]["key"] = keyObject.id
else:
try:
del self.images[self.images.index(self.lockedImage)]
except ValueError:
pass
try:
self.objectAttributes["object"]["locked_image"]
except KeyError:
pass
self.lockedImage = None
self.setIsLocked(False)
self.clearKey()
# If closed, add closed image. If not closed, remove closed image
def setClosed(self, setClosed):
if (setClosed):
imageObject = JSONImage(
self.parentView, None, self.objectAttributes, imageId=self.id)
self.images.append(imageObject)
self.closedImage = imageObject
self.objectAttributes["object"]["closed_image"] = imageObject.id
else:
try:
del self.images[self.images.index(self.closedImage)]
except ValueError:
pass
try:
del self.objectAttributes["object"]["closed_image"]
except KeyError:
pass
self.closedImage = None
def getImages(self):
images = [
self.closedImage,
self.lockedImage,
self.openImage,
self.blockedImage
]
return list(filter((None).__ne__, images))
def getRepresentingImage(self):
if self.openImage is None:
self.openImage = self.images[0]
return self.openImage
def setRepresentingImage(self, img):
self.openImage = img
# Get the image activated by the given item
def getUseImage(self, useItem):
if (self.key == useItem):
return self.lockedImage
def setTransition(self, roomObject):
self.objectAttributes["object"]["transition"] = roomObject.id
self.transition = roomObject
# Returns True if door is locked, otherwise False
def isLocked(self):
try:
if (self.objectAttributes["object"]["locked"] is True):
return True
except KeyError:
print(
"Warning: Attribute 'locked' not defined for door object '%s'"
% (self.id))
return False
def setIsLocked(self, isLocked):
self.objectAttributes["object"]["locked"] = isLocked
def setKey(self, keyObject):
self.key = keyObject
self.key.setTarget(self)
# Nullify current key
def clearKey(self):
if (self.key):
self.key.clearTarget()
self.key = None
class Obstacle(Object):
generalName = localizer.translate(
'classObstacle', 'obstacleName')
generalNameAdessive = ""
# Generic attributes for obstacles
objectAttributes = {
'className': 'Image',
'object': {
'related': [],
'target': '',
'trigger': '',
'blocking_image': '',
'category': 'obstacle',
'blocking': True
}
}
def __init__(self, parentView, itemId, images, objectAttributes):
if not (objectAttributes):
objectAttributes = Obstacle.objectAttributes
if not (images):
images = [JSONImage.imageAttributes]
super(Obstacle, self).\
__init__(parentView, itemId, images, objectAttributes)
# Create the available image objects
try:
self.blockingImage = self.getImage(
objectAttributes["object"]["blocking_image"])
except KeyError:
self.blockingImage = None
try:
# TODO: To be implemented in kiigame
self.unblockingImage = self.getImage(
objectAttributes["object"]["unblocking_image"])
except KeyError:
self.unblockingImage = None
self.texts = {}
try:
if (self.blockingImage):
self.texts.update(
parentView.scenarioData.texts[self.blockingImage.id])
except KeyError:
print(
"Warning: Could not find texts.json entry for object '%s'"
% (self.id))
try:
if (self.unblockingImage):
self.texts.update(self.texts[self.unblockingText.id])
except KeyError:
print(
"Warning: Could not find texts.json entry for object '%s'"
% (self.id))
# Handle these in postInit
self.blockTarget = None
self.trigger = None
def postInit(self, getGameObject):
try:
self.blockTarget = getGameObject(
"object", self.objectAttributes["object"]["target"])
except KeyError:
pass
try:
self.setTrigger(getGameObject(
"object", self.objectAttributes["object"]["trigger"]))
except KeyError:
pass
def getImages(self):
images = [self.blockingImage, self.unblockingImage]
return list(filter((None).__ne__, images))
def getRepresentingImage(self):
if self.blockingImage is None:
self.blockingImage = self.images[0]
return self.blockingImage
def setRepresentingImage(self, img):
self.blockingImage = img
# Get the image activated by the given item
def getUseImage(self, useItem):
if (self.trigger == useItem):
return self.blockingImage
# Returns what unblocks the obstacle
def getKey(self):
return self.trigger
# Set which object blocks
def setTrigger(self, triggerObject):
self.trigger = triggerObject
self.trigger.setTarget(self)
def clearTrigger(self):
self.trigger.clearTarget()
self.trigger = None
def setBlockTarget(self, targetObject):
self.blockTarget = targetObject
self.objectAttributes["object"]["target"] = targetObject.id
def clearBlockTarget(self):
self.blockTarget = None
try:
del self.objectAttributes["object"]["target"]
except KeyError:
pass
# Image object representing what is in the JSON texts
class JSONImage(Object):
imageAttributes = {
'category': '',
'id': '',
'object_name': '',
'src': '',
'visible': False,
'x': 0,
'y': 0
}
generalName = localizer.translate(
'classJSONImage', 'jsonImageName')
generalNameAdessive = ""
# imageAttributes has to be dict, not a list as with other objects
# objectAttributes is a dict with object, attrs and className keys
def __init__(self, parentView, imageAttributes,
objectAttributes, imageId=None):
if not (imageAttributes):
imageAttributes = deepcopy(JSONImage.imageAttributes)
self.absoluteImagePath = None
if not (imageId):
imageId = imageAttributes["id"]
super(JSONImage, self).\
__init__(parentView, imageId, None, objectAttributes)
self.imageAttributes = imageAttributes
self.placeholderImage = PlaceholderImage(self)
if (imageAttributes):
self.absoluteImagePath = "%simages/%s"\
% (parentView.scenarioData.dataDir, self.getFileName())
def getRepresentingImage(self):
if (len(self.imageAttributes["src"]) == 0):
return self.placeholderImage
else:
return self
def getName(self):
try:
return self.texts["name"]
except KeyError:
return None
def setName(self, name):
self.texts["name"] = name
def getID(self):
try:
return self.id
except:
print("Warning: imageID not found.")
def setID(self, newID):
self.id = newID
def getFileName(self):
# TODO: self.getSource() returns None?
return self.imageAttributes["src"].split("/")[-1]
def getSource(self):
if (len(self.imageAttributes["src"]) == 0):
return self.placeholderImage.getSource()
return self.imageAttributes["src"]
def setSource(self, absoluteImagePath):
# Cut the plain filename out of the name
self.imageAttributes["src"] =\
"images/"+absoluteImagePath.split("/")[-1]
self.absoluteImagePath = absoluteImagePath
def setObjectName(self, objectName):
self.imageAttributes["object_name"] = objectName
def setCoordinates(self, x, y):
self.imageAttributes["x"] = x
self.imageAttributes["y"] = y
def getCoordinates(self):
#print(self.id, self.imageAttributes)
try:
return (self.imageAttributes["x"], self.imageAttributes["y"])
except KeyError:
return
def setCategory(self, category):
self.imageAttributes["category"] = category
def setObjectId(self, objectId):
self.imageAttributes["id"] = objectId
# Differentiate sequence images from normal images
class SequenceImage(JSONImage):
generalName = localizer.translate(
'classSequenceImage', 'imageSequenceName')
generalNameAdessive = ""
def __init__(self, parentView, imageAttributes,
objectAttributes, imageId=None):
super(SequenceImage, self).\
__init__(parentView, imageAttributes, objectAttributes, imageId)
# Get the display time for this image