Text = button?

hi again,

i wonder if we can make a text, a DirectButton…

am i right to write this following code to make a text (like “New game, Load game, ect:.”) a direct button?

import direct.directbase.DirectStart
from panda3d.core import NodePath,TextNode
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.interval.SoundInterval import SoundInterval
from direct.gui.DirectSlider import DirectSlider
from direct.gui.DirectButton import DirectButton

import sys

title = OnscreenText(text="Game Title",
               style=1,fg(1,1,1,1), pos=(50.63, 40.54)

command_list = [New Game, Load Game, Option, Quit]

def text_is_a_button (self):

    self.command_list = DirectButton(pos=(.345, 5.45)
    scale=.07, rolloverSound = None, ClickSound = ("sound/gui/guiClick"))

so… possible :confused: ?

thanks in advence for any help


This kind of topic rather goes in Scripting Issues, not in General Discussion.

What you are looking for is more like this (the following is non-working pseudo-code):

command_list = [New Game, Load Game, Option, Quit]

for i in range(len(command_list)):
  DirectButton(text=command_list[i], text_font=loader.loadFont("cms12"), pos=(0,0,0-i*0,05), scale=0.075, text_fg=(1,1,1,1), frameColor=(1,1,1,0.5), command=self.function, extraArgs=command_list[i])

range(len(command_list)) means, that: len(command_list) is 4. range(4) = 0,1,2,3.

The pos=(0,0,0-i*0,05) means that i is 0,1,2 or 3. Multiply that by 0,05 and the buttons are spaced out a little: 0,0,0; 0,0,-0.05; 0,0,-0.1; 0,0,-0.15.

Command is the function you want to fire when a button is pressed. It’s just a pointer, so there’s no ().

text=command_list[i] means that the list of commands you created is being used from 0 to i, i.e. command_list[2] = Option. Notice that you have to use strings " ".

oh, sorry about posting in the wrong board.

but thanks about your reply :slight_smile:

This is my code of a main menu that uses text-only buttons.
You should be able to extract what you need, i guess.

class MainMenu(object):
    def __init__(self):
        mainmenu = [("Adventure", lambda: self.p("adventure")),
                    ("Quick Game", lambda: self.p("quick game")),
                    ("Credits", lambda: self.p("not yet implemented")),
                    ("Exit", sys.exit)]
        #self.font = loader.loadFont("fonts/aranea.ttf")
        self.parent_node = aspect2d.attachNewNode("main menu")
        #self.bg = OnscreenImage(image="backdrops/menubg.jpg",
                                scale=(1.333333,1,1), parent=self.parent_node)
        self.t = OnscreenText("Your Game", pos=(-0.6, 0.7), font=self.font,
                              fg=(1,1,1,1), scale=0.3)
        margin = 0.0
        button_options = {
                "text_fg":(1,1,1,1), "text_font":self.font, "text_scale":0.1,
                "relief":None, "rolloverSound":None, "clickSound":None,
                "pressEffect":0, "frameVisibleScale":(0.1,0.1), "sortOrder":2,
                "text_wordwrap":7, "parent":self.parent_node}
        self.buttons = []
        lengths = 0
        for caption, function in mainmenu:
            b = DirectButton(text=caption, command=function, **button_options)
            lengths += b.getWidth()
        space = (2 - margin * 2 - lengths) / (len(self.buttons) - 1)
        pos = -1 + margin
        for b in self.buttons:
            pos -= b.node().getFrame()[0]
            b.setPos(pos, 0, -0.7)
            pos += b.node().getFrame()[1] + space

    def p(self, arg):
        """Temporary convenience function."""
        print arg

    def destroy(self):
        for b in self.buttons: