kle2xy.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """ Original code from https://github.com/skullydazed/kle2xy
  2. """
  3. import hjson
  4. from decimal import Decimal
  5. class KLE2xy(list):
  6. """Abstract interface for interacting with a KLE layout.
  7. """
  8. def __init__(self, layout=None, name='', invert_y=True):
  9. super(KLE2xy, self).__init__()
  10. self.name = name
  11. self.invert_y = invert_y
  12. self.key_width = Decimal('19.05')
  13. self.key_skel = {
  14. 'decal': False,
  15. 'border_color': 'none',
  16. 'keycap_profile': '',
  17. 'keycap_color': 'grey',
  18. 'label_color': 'black',
  19. 'label_size': 3,
  20. 'label_style': 4,
  21. 'width': Decimal('1'), 'height': Decimal('1'),
  22. 'x': Decimal('0'), 'y': Decimal('0')
  23. }
  24. self.rows = Decimal(0)
  25. self.columns = Decimal(0)
  26. if layout:
  27. self.parse_layout(layout)
  28. @property
  29. def width(self):
  30. """Returns the width of the keyboard plate.
  31. """
  32. return (Decimal(self.columns) * self.key_width) + self.key_width/2
  33. @property
  34. def height(self):
  35. """Returns the height of the keyboard plate.
  36. """
  37. return (self.rows * self.key_width) + self.key_width/2
  38. @property
  39. def size(self):
  40. """Returns the size of the keyboard plate.
  41. """
  42. return (self.width, self.height)
  43. def attrs(self, properties):
  44. """Parse the keyboard properties dictionary.
  45. """
  46. # FIXME: Store more than just the keyboard name.
  47. if 'name' in properties:
  48. self.name = properties['name']
  49. def parse_layout(self, layout):
  50. # Wrap this in a dictionary so hjson will parse KLE raw data
  51. layout = '{"layout": [' + layout + ']}'
  52. layout = hjson.loads(layout)['layout']
  53. # Initialize our state machine
  54. current_key = self.key_skel.copy()
  55. current_row = Decimal(0)
  56. current_col = Decimal(0)
  57. current_x = 0
  58. current_y = self.key_width / 2
  59. if isinstance(layout[0], dict):
  60. self.attrs(layout[0])
  61. layout = layout[1:]
  62. for row_num, row in enumerate(layout):
  63. self.append([])
  64. # Process the current row
  65. for key in row:
  66. if isinstance(key, dict):
  67. if 'w' in key and key['w'] != Decimal(1):
  68. current_key['width'] = Decimal(key['w'])
  69. if 'w2' in key and 'h2' in key and key['w2'] == 1.5 and key['h2'] == 1:
  70. # FIXME: ISO Key uses these params: {x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25}
  71. current_key['isoenter'] = True
  72. if 'h' in key and key['h'] != Decimal(1):
  73. current_key['height'] = Decimal(key['h'])
  74. if 'a' in key:
  75. current_key['label_style'] = self.key_skel['label_style'] = int(key['a'])
  76. if current_key['label_style'] < 0:
  77. current_key['label_style'] = 0
  78. elif current_key['label_style'] > 9:
  79. current_key['label_style'] = 9
  80. if 'f' in key:
  81. font_size = int(key['f'])
  82. if font_size > 9:
  83. font_size = 9
  84. elif font_size < 1:
  85. font_size = 1
  86. current_key['label_size'] = self.key_skel['label_size'] = font_size
  87. if 'p' in key:
  88. current_key['keycap_profile'] = self.key_skel['keycap_profile'] = key['p']
  89. if 'c' in key:
  90. current_key['keycap_color'] = self.key_skel['keycap_color'] = key['c']
  91. if 't' in key:
  92. # FIXME: Need to do better validation, plus figure out how to support multiple colors
  93. if '\n' in key['t']:
  94. key['t'] = key['t'].split('\n')[0]
  95. if key['t'] == "0":
  96. key['t'] = "#000000"
  97. current_key['label_color'] = self.key_skel['label_color'] = key['t']
  98. if 'x' in key:
  99. current_col += Decimal(key['x'])
  100. current_x += Decimal(key['x']) * self.key_width
  101. if 'y' in key:
  102. current_row += Decimal(key['y'])
  103. current_y += Decimal(key['y']) * self.key_width
  104. if 'd' in key:
  105. current_key['decal'] = True
  106. else:
  107. current_key['name'] = key
  108. current_key['row'] = current_row
  109. current_key['column'] = current_col
  110. # Determine the X center
  111. x_center = (current_key['width'] * self.key_width) / 2
  112. current_x += x_center
  113. current_key['x'] = current_x
  114. current_x += x_center
  115. # Determine the Y center
  116. y_center = (current_key['height'] * self.key_width) / 2
  117. y_offset = y_center - (self.key_width / 2)
  118. current_key['y'] = (current_y + y_offset)
  119. # Tend to our row/col count
  120. current_col += current_key['width']
  121. if current_col > self.columns:
  122. self.columns = current_col
  123. # Invert the y-axis if neccesary
  124. if self.invert_y:
  125. current_key['y'] = -current_key['y']
  126. # Store this key
  127. self[-1].append(current_key)
  128. current_key = self.key_skel.copy()
  129. # Move to the next row
  130. current_x = 0
  131. current_y += self.key_width
  132. current_col = Decimal(0)
  133. current_row += Decimal(1)
  134. if current_row > self.rows:
  135. self.rows = Decimal(current_row)