Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- class Recall:
- def __init__(self):
- self.pos = 0
- self.buffer = []
- def down(self):
- if self.pos > 0:
- self.pos -= 1
- text = self.buffer[self.pos]
- return text
- else:
- self.pos = -1
- def up(self):
- if self.pos < len(self.buffer) - 1:
- self.pos += 1
- return self.buffer[self.pos]
- def store(self, text):
- self.pos = -1
- if text not in self.buffer:
- if len(self.buffer) > 4:
- self.buffer = [text] + self.buffer[:4]
- else:
- self.buffer.insert(0, text)
- elif self.buffer[0] != text:
- self.buffer.remove(text)
- self.buffer.insert(0, text)
- class EntryBox:
- def __init__(self, font, rect, callback,
- textcolor = pygame.Color("white"),
- boxcolor = pygame.Color("gray44"),
- hovercolor = pygame.Color("aliceblue"),
- selectedcolor = pygame.Color("dodgerblue")):
- self.font = font
- self.rect = rect
- self.callback = callback
- self.boxcolor = boxcolor
- self.textcolor = textcolor
- self.hovercolor = hovercolor
- self.selectedcolor = selectedcolor
- self.buffer = []
- self.image = None
- w, h = self.font.size("|")
- self.image_rect = pygame.Rect(rect.x, rect.y + (rect.h - h) / 2, w, h)
- self.hover = False
- self.offset = [0, 0]
- self.selected = False
- self.recall = Recall()
- self.carrot_pos = 0
- self.carrot_left = self.rect.x
- self.carrot_show = True
- self.carrot_image = font.render("|", 1, textcolor)
- self.key_event = {
- pygame.K_BACKSPACE: self.keydown_backspace,
- pygame.K_DELETE: self.keydown_delete,
- pygame.K_DOWN: self.keydown_down,
- pygame.K_END: self.keydown_end,
- pygame.K_HOME: self.keydown_home,
- pygame.K_LEFT: self.keydown_left,
- pygame.K_RETURN: self.keydown_return,
- pygame.K_RIGHT: self.keydown_right,
- pygame.K_UP: self.keydown_up
- }
- def draw(self, surface):
- if self.selected:
- color = self.selectedcolor
- elif self.hover:
- color = self.hovercolor
- else:
- color = self.boxcolor
- surface.fill(color, self.rect)
- if self.image:
- surface.blit(self.image, self.image_rect)
- if self.selected:
- surface.blit(self.carrot_image, (self.carrot_left, self.image_rect.y))
- def keydown_backspace(self):
- if self.carrot_pos > 1:
- front = self.buffer[:self.carrot_pos - 1]
- back = self.buffer[self.carrot_pos:]
- self.buffer = front + back
- self.carrot_pos -= 1
- else:
- self.keydown_delete()
- def keydown_delete(self):
- self.buffer = []
- self.image = None
- self.carrot_pos = 0
- self.carrot_left = self.rect.x
- self.offset = [0, 0]
- def keydown_down(self):
- self.buffer = self.recall.down()
- if self.buffer:
- self.carrot_pos = len(self.buffer)
- else:
- self.keydown_delete()
- def keydown_end(self):
- self.carrot_pos = len(self.buffer)
- def keydown_home(self):
- self.carrot_pos = 0
- def keydown_left(self):
- if self.carrot_pos > 0:
- self.carrot_pos -= 1
- def keydown_return(self):
- self.recall.store(self.buffer)
- text = ''.join(self.buffer)
- self.keydown_delete()
- self.callback(text)
- def keydown_right(self):
- if self.carrot_pos < len(self.buffer):
- self.carrot_pos += 1
- def keydown_up(self):
- recall_buffer = self.recall.up()
- if recall_buffer:
- self.buffer = recall_buffer
- self.carrot_pos = len(self.buffer)
- def on_keydown(self, event):
- if self.selected:
- self.carrot_show = True
- ctrl = event.mod & pygame.KMOD_CTRL
- if ctrl == 0 and 31 < event.key < 127:
- self.buffer.insert(self.carrot_pos, event.unicode)
- self.carrot_pos += 1
- self.update_text()
- elif ctrl == 0:
- if self.key_event.get(event.key, False):
- self.key_event[event.key]()
- self.update_text()
- def on_mousemotion(self, event):
- self.hover = self.rect.collidepoint(event.pos)
- def update_text(self):
- if len(self.buffer) > 0:
- text = ''.join(self.buffer)
- width = self.rect.width - 4
- if self.carrot_pos > self.offset[1]:
- self.offset[1] = self.carrot_pos
- elif self.carrot_pos < self.offset[0]:
- self.offset[0] = self.carrot_pos
- while self.font.size(text[self.offset[0]:self.offset[1]])[0] < width and self.offset[0] > 0:
- self.offset[0] -= 1
- while self.font.size(text[self.offset[0]:self.offset[1]])[0] > width and self.offset[0] < self.carrot_pos:
- self.offset[0] += 1
- while self.font.size(text[self.offset[0]:self.offset[1]])[0] < width and self.offset[1] < len(self.buffer):
- self.offset[1] += 1
- while self.font.size(text[self.offset[0]:self.offset[1]])[0] > width:
- self.offset[1] -= 1
- t = text[self.offset[0]:self.offset[1]]
- self.image = self.font.render(t, 1, self.textcolor)
- self.image_rect.size = self.image.get_rect().size
- self.image_rect.centery = self.rect.centery
- t = text[self.offset[0]:self.carrot_pos]
- self.carrot_left = self.font.size(t)[0] + self.rect.x - 1
- else:
- self.keydown_delete()
- # Simple Scene Interface
- class Scene:
- def draw(self, surface, game): pass
- def event(self, event, game): pass
- class Game:
- def __init__(self, caption, width, height):
- # Basic pygame setup
- pygame.display.set_caption(caption)
- self.rect = pygame.Rect(0, 0, width, height)
- self.surface = pygame.display.set_mode(self.rect.size)
- self.clock = pygame.time.Clock()
- self.running = False
- self.fps = 30
- self.delta = 0
- # Scene Interface
- self.scene = Scene()
- Game.info = self
- def mainloop(self):
- self.running = True
- while self.running:
- for event in pygame.event.get():
- self.scene.event(event, self)
- self.scene.draw(self.surface, self)
- pygame.display.flip()
- self.delta = self.clock.tick(self.fps)
- class Example(Scene):
- def __init__(self):
- font = pygame.font.Font(None, 24)
- self.entry = EntryBox(font, pygame.Rect(20, 20, 200, 40), self.entry_return)
- self.background = pygame.Color('Black')
- def entry_return(self, text):
- print(text)
- def draw(self, surface, game):
- surface.fill(self.background)
- self.entry.draw(surface)
- def event(self, event, game):
- if event.type == pygame.QUIT:
- game.running = False
- elif event.type == pygame.KEYDOWN:
- self.entry.on_keydown(event)
- elif event.type == pygame.MOUSEMOTION:
- self.entry.on_mousemotion(event)
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if event.button == 1 and self.entry.hover:
- self.entry.selected = True
- else:
- self.entry.selected = False
- def main():
- pygame.init()
- game = Game("Entry Box Example", 800, 600)
- game.scene = Example()
- game.mainloop()
- pygame.quit()
- main()
Add Comment
Please, Sign In to add comment