admalledd Posted August 11, 2010 Share Posted August 11, 2010 so, my game is going OK, except i now have to develop the menus and buttons and stuff, and i have done as much as i can, i now ask - knees on ground - can anyone help develop a button GUI interface? im posting what i have, and then going to describe what i WANT to do, use or drop my code, i don't care... first "menu.py": (the one with the test code...) #!/usr/bin/python import pygame from pygame.locals import * import math from input import * def dist(rect_a,rect_b): '''rect_a == top block to snap to rect_b == bottom block to check''' return math.sqrt(((rect_a.midbottom[0]-rect_b.midtop[0])**2)+((rect_a.midbottom[1]-rect_b.midtop[1])**2)) class menu(object): def __init__(self): self.font = pygame.font.Font(None,48) self.clickable =[ (button((300,160) ,self.font.render('forward_spawn',True,(0,0,255)).convert_alpha() ,self.font.render('forward_spawn',True,(255,0,00)).convert_alpha() ,self.font.render('forward_spawn',True,(0,255,0)).convert_alpha() ) ,self.spawn_fwd ), (button((300,240) ,self.font.render('credits',True,(0,0,255)).convert_alpha() ,self.font.render('credits',True,(255,0,0)).convert_alpha() ,self.font.render('credits',True,(0,255,0)).convert_alpha() ) ,self.credits ) ] self.dragable = [(fwd_btn((300,300) ,self.font.render('drag me',True,(0,0,255)).convert_alpha() ,self.font.render('drag me',True,(255,0,0)).convert_alpha() ,self.font.render('drag me',True,(0,255,0)).convert_alpha() ) ) ] self.receptical = [dock_button((500,200) ,self.font.render('mount me',True,(0,0,255)).convert_alpha() ,self.font.render('mount me',True,(255,0,0)).convert_alpha() ,self.font.render('mount me',True,(0,255,0)).convert_alpha() ) ] self.dock_dist = 15 self.surf = pygame.Surface((800, 600)) def add_drag_btn(self, btn): self.dragable.append(btn) def remove_drag_btn(self,btn): self.dragable.remove(btn) del btn def events(self,events): '''TODO: drag/click through to ONLY one button...''' for btn,func in self.clickable: var = btn.events(events) if var: func(btn) for btn in self.dragable: if btn.events(events): self.dragged(btn) for event in events: ##debug code stuff, remove for production if event.type == MOUSEBUTTONUP and event.button == 1: print event.pos ## if event.type == KEYDOWN: if event.key == K_1: print self.dragable[0].dock print self.receptical[0].docked_btn else: print event def draw(self,screen): for btn,func in self.clickable: btn.draw(screen) for btn in self.dragable: btn.draw(screen) for btn in self.receptical: btn.draw(screen) def spawn_fwd(self,btn): print 'fwd spawned' b=fwd_btn((300,300) ,self.font.render('drag me',True,(0,0,255)).convert_alpha() ,self.font.render('drag me',True,(255,0,0)).convert_alpha() ,self.font.render('drag me',True,(0,255,0)).convert_alpha() ) self.add_drag_btn(b) def credits(self,btn): print 'credits clicked' def dragged(self,btn): print 'button dragged' for b in self.dragable: if b.docked_btn:#has been docked, dont do the math then... continue elif hasattr(b,'is_dock') and b.is_dock: if dist(b.rect,btn.rect) <= self.dock_dist: btn.rect.midtop=b.rect.midbottom print b print btn #flup... i have no idea... please help me... if b.dock(btn): return for r in self.receptical: if not r.docked_btn: if dist(r.rect,btn.rect) <= self.dock_dist: btn.rect.midtop=r.rect.midbottom r.dock(btn) return #d = dist(self.hub_rect,btn.rect)#find distance between midtop and midbottom #print d #if d < 15: # btn.rect.midtop=self.hub_rect.midbottom if __name__=='__main__': pygame.init() screen = pygame.display.set_mode((800, 600)) me = menu() while True: pygame.time.wait(10) screen.fill((0, 0, 0)) events = pygame.event.get() for event in events: if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() import sys sys.exit() me.events(events) me.draw(screen) pygame.display.flip() now for "input.py": import pygame from pygame.locals import * class button(object): def __init__(self,center,text_norm,text_high=None,text_click=None): self.text_norm = text_norm if text_high: self.text_high = text_high if text_click: self.text_click = text_click else: self.text_click = text_high else: self.text_high = text_norm self.surf = self.text_norm self.click_tmp=False self.clicked=False self.rect = self.text_norm.get_rect() self.rect.center = center self.is_dock = False def events(self,events): for event in events: if event.type == MOUSEMOTION:#hoverover button if self.rect.collidepoint(event.pos): if self.surf is self.text_norm: self.surf = self.text_high else: self.surf = self.text_norm elif event.type == MOUSEBUTTONDOWN:#click and hold over button if self.rect.collidepoint(event.pos): self.surf = self.text_click self.click_tmp=True else: self.click_tmp=False elif event.type == MOUSEBUTTONUP:#click,hold,and release over button if self.rect.collidepoint(event.pos): if self.click_tmp: self.clicked = True#variable for things to use to see if clicked... self.click_tmp=False else: self.click_tmp=False self.clicked=False if self.clicked == True: self.clicked = False return True def draw(self,surf): surf.blit(self.surf,self.rect) class dock_button(button): def __init__(self,center,text_norm,text_high=None,text_click=None): super(dock_button,self).__init__(center,text_norm,text_high,text_click) self.docked_btn = None self.is_dock = True def dock(self,btn): if self.docked_btn: return False else: self.docked_btn = btn self.docked_btn.dock = self def remove(self): '''to be defined, remove all docked items as well (eg we delete the entire dragged tree...)''' if hasattr(self.docked_btn,'remove'): self.docked_btn.remove() del self.dock del self def draw(self,surf): super(dock_button,self).draw(surf) ##TODO::: if self.docked_btn: self.docked_btn.draw(surf) class drag_button(button): def __init__(self,center,text_norm,text_high=None,text_click=None): super(drag_button,self).__init__(center,text_norm,text_high,text_click) self.dock=None def events(self,events): tmp = super(drag_button,self).events(events) for event in events: if event.type == MOUSEMOTION: if self.rect.collidepoint(event.pos): if self.click_tmp == True: self.rect.center= event.pos return tmp def draw(self,surf,dock_override=False): if self.dock: if not dock_override: return None surf.blit(self.surf,self.rect) pygame.draw.rect(surf, (255,255,255), self.rect, 1) class fwd_btn(drag_button , dock_button): pass now then: my problem(s) is(are): 1: crash when two buttons are docked to one another (click on fwd_spawn to make a new button...) 2: button disappear when mounted to "mount me" button dock... 3: my own incompetence with buttons and GUI's in general... what i WANT to do: 1: have a button that can be dragged and docked to another 2: a button that spawns more draggable(sp?) buttons 3: when dragging a button tree, move all of the sub-buttons with the head/master button. any and all questions are welcome, as well as any help. always makes me feel idiotic when i cant do simple GUI stuff... Link to comment Share on other sites More sharing options...
TheDoctor Posted August 11, 2010 Share Posted August 11, 2010 Ah, Python. This would be Mouses domain. See of you can catch him tomorrow. I'm begining to learn it now, but I won't be of much help. When all fails, sudo it. Link to comment Share on other sites More sharing options...
Eagle98 Posted August 11, 2010 Share Posted August 11, 2010 Eagle98, reporting in. How may I help you? Oh, wait. You mean you need help coding in? Forget it, then. Link to comment Share on other sites More sharing options...
Addictgamer Posted August 11, 2010 Share Posted August 11, 2010 Well, I have some c++ code that you could use to base menus and buttons off of... If that would help any bit... As Doc said, though, python is mouse's domain. Link to comment Share on other sites More sharing options...
admalledd Posted August 11, 2010 Author Share Posted August 11, 2010 Well, I have some c++ code that you could use to base menus and buttons off of... If that would help any bit... might this code be drag + drop stuff? because that is my major stumbling block right now, and i can understand c++, so it might help if it was drag + drop. otherwise, i can already to some basic menus and buttons, its the whole dragging and docking thing that has got me in a twist right now... *drops some cheese in hopes of a mouse...* Link to comment Share on other sites More sharing options...
Addictgamer Posted August 11, 2010 Share Posted August 11, 2010 Psudo code: if(mouse_click_detect) { if(button_clicked_and_dragged() [1] && dragable == true) { my_button.x = mouse.x; my_button.y = mouse.y; } } [1] - A function that checks if the mouse's position changes the whole time it is pressed down over the button. Link to comment Share on other sites More sharing options...
admalledd Posted August 11, 2010 Author Share Posted August 11, 2010 Psudo code: if(mouse_click_detect) { if(button_clicked_and_dragged() [1] && dragable == true) { my_button.x = mouse.x; my_button.y = mouse.y; } } [1] - A function that checks if the mouse's position changes the whole time it is pressed down over the button. that's reassuring.. it is almost identical to mine... def events(self,events): tmp = super(drag_button,self).events(events) for event in events: if event.type == MOUSEMOTION:###mouse has been moved... if self.rect.collidepoint(event.pos):###mouse is over the button... if self.click_tmp == True: ###mouse button is down (figured out in other code area...) self.rect.center= event.pos###the key line: set the button's center to the mouse location return tmp###(this is to see if the button was clicked, but not dragged...) *replaces old cheese that might be getting moldy with new fancy French cheese... * Link to comment Share on other sites More sharing options...
Anonymouse Posted August 11, 2010 Share Posted August 11, 2010 why three hashes for comments? One is enough IMO On topic, I'm not good with GUI either... Sorry... I used to use pygame too. I now tried pyglet. And realized it's much easier to use. Maybe you should try that.... I don't know though. Link to comment Share on other sites More sharing options...
admalledd Posted August 11, 2010 Author Share Posted August 11, 2010 well, i feel stupid right now... found out my problems, and fixed them. now to start working more on the GUI now that this works... for informational purposes; the reason that the button "disappeared" is that, for some reason, the button was no longer being drawn when it got docked... and the reason for the crashing when i dock two identical items is because of the fact that i'm mixing two classes together and they both define a thing as 'dock', one as a function, the other as a variable... so when i try to call the function i got the variable, and when i ask for the variable, i got the function... confusing... teach ME to use classes as they were meant to be used... you can check out the fixed code from my github page. Link to comment Share on other sites More sharing options...
Recommended Posts