Hello everyone…
So I’m making a chat client using buttons for the messages inside a directscrolledframe.
I don’t set the framesize of the button, I let the wordwrap handle that. So the size of the button is determined by the text inside it.
Then, I will vertically position the buttons on the directscrolledframe using getBounds() on the button, basing the vertical offset on the height of the button.
The problem is, the button with multiple text lines returns a pretty large number, hence a huge gap between the muti-lined button and the single lined button.
Is there a reason that multi-lined button returns a large digit, much higher than the visual size of the button?
I hope I explained clearly. If not, here’s the code.
(I’m new at python, so sorry for the messy and inefficient code…)
# file: chat_client_jap.py
# -*- coding: utf-8 -*-
import direct.directbase.DirectStart
from math import ceil
from pandac.PandaModules import *
loadPrcFileData("", "win-size 1024 768")
from direct.showbase.DirectObject import DirectObject
from direct.showbase.PythonUtil import clampScalar
from direct.gui.DirectGui import *
from direct.gui.OnscreenText import OnscreenText
from direct.gui.OnscreenImage import OnscreenImage
from types import IntType
from direct.task import Task
class PChatClass(DirectObject):
def __init__(self,parent = None):
#Chat Window Size
self.frameWidth = 1.2
self.frameHeight = .8
#Button array
self.messageList = []
#Array offset
self.numMessage = -1
self.currentCanvasHeight = 0
self.chatFrame = DirectScrolledFrame(parent=parent,
#pos=(-self.frameWidth*.5,0,self.frameHeight*.5),
relief=DGG.GROOVE,
borderWidth=(0,0),
frameColor=(0,0,0,.7),
frameSize=(0,self.frameWidth,-self.frameHeight,0),
canvasSize=(0,self.frameWidth,-self.frameHeight,0),
manageScrollBars=False,
autoHideScrollBars=True,
verticalScroll_resizeThumb=False,
verticalScroll_thumb_frameSize=(0,.01,-.01,0))
self.chatBox=self.chatFrame.getCanvas().attachNewNode('myCanvas')
def addMessage(self, textMessage):
newMessage = DirectButton(parent = self.chatBox,
scale = .055,
#pos = (0,0,self.currentCanvasHeight),
relief = DGG.GROOVE,
pad=(0,0),
borderWidth=(0.01,0.01),
text = textMessage,
text_wordwrap = 15,
text_align = TextNode.ALeft,
text_fg = (1,1,1,1),
enableEdit = False,
suppressMouse = False)
self.messageList.append(newMessage)
self.numMessage += 1
self.updateCanvasHeight()
def updateCanvasHeight(self):
self.updateMessages()
self.chatFrame['canvasSize'] = (0,self.frameWidth,self.currentCanvasHeight,0)
def updateMessages(self):
messageSpacing = -0.07
for i in range(0,self.numMessage+1):
self.messageList[i].setZ(messageSpacing)
l,r,b,t = self.messageList[i].getBounds()
messageSpacing += b
self.currentCanvasHeight = messageSpacing
chat = PChatClass()
chat.addMessage('2')
chat.addMessage('The quick brown fox jumps over the lazy dog.')
chat.addMessage('4')
chat.addMessage('5')
chat.addMessage('6')
chat.addMessage('7')
chat.addMessage('8')
run()