Calculator Community > Lua

Updating WZGUILib

<< < (3/78) > >>

jwalker:
i was thinking something like that too. and i might change the circle to a square. the colors can be changed just by creating the dialog box or changing the color after it is created kind of like this:

--- Code: -------colors:
color = {
black = {0, 0, 0},
lightgreen = {0, 255, 0},}
-----------dialog code
dialog = class()

function dialog:init( x, y, text, title, isselected, backcolor, textcolor, closecolor, isdrawn)--add buttons and other features
self.x = x
self.y = y
self.text = text
self.title = title
self.width = 200
self.height = 150
self.cbw = 10
self.cbh = 10
self.cbx = self.x + 175
self.cby = self.y + 7
self.selected = isselected
self.backcolor = backcolor
self.textcolor = textcolor
self.closecolor = closecolor
self.isdrawn = isdrawn
self.moveable = false
table.insert(dialogTable, self)
end
function dialog:paint(gc)
if self.isdrawn then
gc:setFont("sansserif", "b", 12)
gc:setColorRGB(unpack(self.backcolor))
gc:fillRect( self.x, self.y, self.width, self.height)
gc:setColorRGB(unpack(color.white))
gc:fillRect( self.x + 5, self.y + 20, self.width - 10, self.height - 30)
gc:setColorRGB(unpack(self.textcolor))
if string.len(self.title) > 10 then
self.title = string.sub(self.title, 1, 10).."..."
end
gc:drawString(self.title, self.x + 10, self.y + 8, "middle")
gc:drawString(self.text, self.x + 9, self.y + 25, "middle")
gc:setColorRGB(unpack(self.closecolor))
gc:fillArc( self.cbx, self.cby, self.cbw, self.cbh, 0, 360)
end
end
function dialog:checkUnclick()
self.moveable = false
cursor.set("default")
self.selected = false
platform.window:invalidate()
end
function dialog:checkClick(x, y)
if self.isdrawn then
if (x >= self.cbx and x <= self.cbx + 10) and (y >= self.cby and y <= self.cby + 10) then
self:close()
elseif self.moveable == false and (x >= self.x and x <= self.x + self.width) and (y >=self.y and y <= self.y + (self.height - 130)) then
self:wasclicked(x, y)
else
self:checkUnclick()
end
end
end
function dialog:close()
self.selected = false
self.isdrawn = false
self.moveable = false
cursor.set("default")
platform.window:invalidate()
end
function dialog:wasclicked(x, y)
self.moveable = true
if self.selected ~= self then
for _, dlg in pairs(dialogTable) do
dlg.selected = false
end
self.selected = true
end
self.xvar = x - self.x
self.yvar = y - self.y
cursor.set("drag grab")
platform.window:invalidate()
end
function dialog:mouseMove(x, y)
if self.moveable and self.isdrawn and self.selected then
cursor.set("drag grab")
self.x = x - self.xvar
self.y = y - self.yvar
self.cbx = self.x + 175
self.cby = self.y + 7
platform.window:invalidate()
end
end
function drawDialog(gc)
for _, SD in pairs(dialogTable) do
if SD.isdrawn then
SD:paint(gc)
end
end
end
------------
---------------create the dialog box
function on.create()
dia1 = dialog(45, 23, "Color change", "Colors", false, color.lightgreen, color.red, color.red, true)
end
----------------------------say a function changes the color like this later on if it is called
function color_change()
dia1.backcolor = color.black
-----after that the color of the boarder would be black
end
--- End code ---
in the end its what the programmer decides, but i probably would tone the colors down just because they are hard to see on the calc :P
EDIT: @epic7 it is a small, easy to use gui library that offers quite a few features, im working on making a better use of the OOP that Nspire lua offers

jwalker:
Im trying to make a cursor for the text box but ive encountered a problem...
im not quite sure if the "gc:getStringWidth" command works properly
does anyone else have problems with it

Jim Bauwens:
It works fine for me. What is the problem ?

jwalker:
the problem is that with diffrent charachters like a and A it dosen't seem to recognize the diffrence in size:
example this code:

--- Code: ---textbox = class()

function textbox:init(x, y, text, selected, textcolor, tbcolor)
self.x = x
self.y = y
self.width = 156
self.height = 20
self.text = text
self.textcolor = textcolor
self.tbcolor = tbcolor
self.selected = isselected
self.curx = self.x + platform.gc():getStringWidth(self.text)
self.cury = y + 1
self.curh = (self.y + self.height) - 1
table.insert(textboxTable, self)
end
function textbox:paint(gc)
gc:setColorRGB(unpack(self.tbcolor))
gc:drawRect( self.x, self.y, self.width, self.height)
gc:setColorRGB(unpack(self.textcolor))
gc:setFont("serif", "r", 11)
gc:drawString( self.text, self.x + 4, self.y + 8, "middle")
gc:setColorRGB(unpack(color.black))
gc:drawLine(self.curx, self.cury, self.curx, self.curh)
end
function textbox:charIn(ch)
if self.selected then
if (self.width - 8) < platform.gc():getStringWidth(self.text) then
platform.window:invalidate()
else
self.text = self.text..ch
self.curx = self.x + platform.gc():getStringWidth(self.text) + 2
platform.window:invalidate()
end
end
end
function textbox:backspace()
if self.selected then
self.text = string.sub(self.text, 1, string.len(self.text) - 1)
self.curx = self.x + platform.gc():getStringWidth(self.text) + 2
platform.window:invalidate()
end
end

function textbox:click()
if self.selected then
self.selected = false
else
for _, tb in pairs(textboxTable) do
tb.selected = false
end
self.selected = true
end
platform.window:invalidate()
end

function textbox:checkClick(x, y)
if y >= self.y and y <= self.y + self.height and x >= self.x and x <= self.x + self.width then
self:click()
end
end
--- End code ---
the code is this: cury = self.x + platform.gc():getStringWidth(self.text)
it seems that if you continualy type a lowercase "a" the cursor goes farther ahead
and it seems that if you continualy type a capital "A" the cursor will fall behind
this is the same for most of the capitol and lowercase letters
also this dosent have a drawTB function because im working on a small screen manager

Adriweb:
Your problem might come from the fact that you're using platform.gc():getStringWidth()  instead of the normal  gc  you use when you draw stuff.
platform.gc() initialize a new  gc   and then doesn't know about the font properties (size etc.) of the one you want, so this is probably the source of problem...

Try either to :
  -  pass gc (the one you use to draw with) to the function where   getStringWidth()   is needed, then do that with this gc.
  -  calculate the width beforehand (then pass it to the function)... but that might be too heavy. It's better to use the 1st solution.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version