#!/usr/bin/env python # Copyright (C) 2010-2012 Sam Bull """ An example file demonstrating proper use of widgets. """ import os import sys import random import pygame from pygame.locals import * try: from OpenGL.GL import * except: pass pygame.display.init() pygame.font.init() sys.path.insert(0, "../..") import sgc from sgc.locals import * ver_no = "0.1.3" screen = sgc.surface.Screen((640,480)) #, flags=OPENGL clock = pygame.time.Clock() pygame.scrap.init() def clear(): """Clear input box when enter key is pressed.""" input_box.text = "" class MainMenu(sgc.widgets.Menu): """Create a subclass for custom functions.""" func_dict = lambda self: {"print_input": self.print_input, "remove": self.remove} def print_input(self): print self["input"].text sgc.Font.col = (150,150,150) # TODO Button font colour # Title title = sgc.widgets.Label(text="Simple Game Code " + ver_no, font=sgc.Font["title"], col=sgc.Font.col) title.rect.center = (screen.rect.w/2, 40) title.add() # Create input_box input_box = sgc.widgets.InputBox(label="Input Box", default="default text...") input_box.config(pos=(30,120)) input_box.add(0) # Change colour button, activate event caught in event loop button = sgc.widgets.Button(label="Change\ncolour", pos=(40,200)) # Create FPS counter fps = sgc.widgets.FPSCounter(clock=clock) fps.rect.midbottom = (screen.rect.w/2, screen.rect.h) fps.add() # Pass config file as argument, to have Menu parse file with open("menu") as menu_file: menu = MainMenu(menu=menu_file) # Display menu on button click, activate replaced through assignment btn_menu = sgc.widgets.Button(label="Menu", pos=(250,200)) btn_menu.activate = menu.add # Input_box for dialog window password_box = sgc.widgets.InputBox(label="Password", default="Enter password...") password_box.pos = (0,10) # Button for dialog window def print_pass(): print password_box.text dialogs[-1].remove() btn_ok = sgc.widgets.Button(label="OK", pos=(30,60)) btn_ok.activate = print_pass # Place widgets into a container dialog_container = sgc.widgets.Container(widgets=(password_box, btn_ok), border=10) # Display dialog window, activate replaced through inheritance dialogs = [] class BtnDialog(sgc.widgets.Button): def activate(self): dialogs.append(sgc.widgets.Dialog(widget=dialog_container, title="Window title here...")) dialogs[-1].rect.center = screen.rect.center dialogs[-1].add() btn_dialog = BtnDialog(label="Dialog", pos=(460,200)) box_btn = sgc.widgets.HBox(widgets=[button, btn_menu, btn_dialog], spacing=70) scroll_box = sgc.widgets.ScrollBox((300, box_btn.rect.h), widget=box_btn) scroll_box.rect.center = screen.rect.center scroll_box.add(1) # Radio Buttons radio1 = sgc.widgets.Radio(group="group1", label="Option 1", active=True) radio2 = sgc.widgets.Radio(group="group1", label="Option 2") radio3 = sgc.widgets.Radio(group="group1", label="Option 3") radio_box = sgc.widgets.VBox(widgets=(radio1, radio2, radio3), pos=(40,320)) radio_box.add(2) # Toggle Button toggle = sgc.widgets.Toggle(label="Toggle", pos=(200,320)) toggle.add(3) # Selectable Label label = sgc.widgets.Label(text="This is a selectable label", selectable=True) label.rect.midtop = title.rect.midbottom label.add() while True: time = clock.tick(30) for event in pygame.event.get(): # Send event to widgets sgc.widgets.event(event) if event.type == GUI: if event.widget_type is sgc.widgets.Button: print "Button event" if event.widget is button and event.gui_type == "activate": button.config(col=[random.randrange(1,200) for x in range(3)]) elif event.widget is input_box: clear() elif event.type == KEYDOWN: if event.key == K_f: fps.toggle() elif event.type == QUIT: exit() # Cleanup removed windows for widget in dialogs: if not widget.active(): dialogs.remove(widget) if not screen._opengl: screen.fill((0,0,255)) else: glClearColor(0,0,1,1) glClear(GL_COLOR_BUFFER_BIT) # Update the widgets once for each frame sgc.widgets.update(time) pygame.display.flip()