Created
October 18, 2012 03:35
-
-
Save devilstower/3909730 to your computer and use it in GitHub Desktop.
CiderControls 1.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Frame | |
Frame = class() | |
-- Frame | |
-- ver. 1.5 | |
-- a simple rectangle for holding controls. | |
-- ==================== | |
function Frame:init(left, bottom, right, top) | |
self.left = left | |
self.right = right | |
self.bottom = bottom | |
self.top = top | |
end | |
function Frame:inset(dx, dy) | |
self.left = self.left + dx | |
self.right = self.right - dx | |
self.bottom = self.bottom + dy | |
self.top = self.top - dy | |
end | |
function Frame:offset(dx, dy) | |
self.left = self.left + dx | |
self.right = self.right + dx | |
self.bottom = self.bottom + dy | |
self.top = self.top + dy | |
end | |
function Frame:draw() | |
pushStyle() | |
rectMode(CORNERS) | |
rect(self.left, self.bottom, self.right, self.top) | |
popStyle() | |
end | |
function Frame:roundRect(r) | |
pushStyle() | |
insetPos = vec2(self.left + r,self.bottom + r) | |
insetSize = vec2(self:width() - 2 * r,self:height() - 2 * r) | |
rectMode(CORNER) | |
rect(insetPos.x, insetPos.y, insetSize.x, insetSize.y) | |
if r > 0 then | |
smooth() | |
lineCapMode(ROUND) | |
strokeWidth(r * 2) | |
line(insetPos.x, insetPos.y, | |
insetPos.x + insetSize.x, insetPos.y) | |
line(insetPos.x, insetPos.y, | |
insetPos.x, insetPos.y + insetSize.y) | |
line(insetPos.x, insetPos.y + insetSize.y, | |
insetPos.x + insetSize.x, insetPos.y + insetSize.y) | |
line(insetPos.x + insetSize.x, insetPos.y, | |
insetPos.x + insetSize.x, insetPos.y + insetSize.y) | |
end | |
popStyle() | |
end | |
function Frame:gloss(baseclr) | |
local i, t, r, g, b, y | |
pushStyle() | |
if baseclr == nil then baseclr = color(194, 194, 194, 255) end | |
fill(baseclr) | |
rectMode(CORNERS) | |
rect(self.left, self.bottom, self.right, self.top) | |
r = baseclr.r | |
g = baseclr.g | |
b = baseclr.b | |
for i = 1 , self:height() / 2 do | |
r = r - 1 | |
g = g - 1 | |
b = b - 1 | |
stroke(r, g, b, 255) | |
y = (self.bottom + self.top) / 2 | |
line(self.left, y + i, self.right, y + i) | |
line(self.left, y - i, self.right, y - i) | |
end | |
popStyle() | |
end | |
function Frame:shade(base, step) | |
pushStyle() | |
strokeWidth(1) | |
for y = self.bottom, self.top do | |
i = self.top - y | |
stroke(base - i * step, base - i * step, base - i * step, 255) | |
line(self.left, y, self.right, y) | |
end | |
popStyle() | |
end | |
function Frame:touched(touch) | |
if touch.x >= self.left and touch.x <= self.right then | |
if touch.y >= self.bottom and touch.y <= self.top then | |
return true | |
end | |
end | |
return false | |
end | |
function Frame:ptIn(x, y) | |
if x >= self.left and x <= self.right then | |
if y >= self.bottom and y <= self.top then | |
return true | |
end | |
end | |
return false | |
end | |
function Frame:overlaps(f) | |
if self.left > f.right or self.right < f.left or | |
self.bottom > f.top or self.top < f.bottom then | |
return false | |
else | |
return true | |
end | |
end | |
function Frame:width() | |
return self.right - self.left | |
end | |
function Frame:height() | |
return self.top - self.bottom | |
end | |
function Frame:midX() | |
return (self.left + self.right) / 2 | |
end | |
function Frame:midY() | |
return (self.bottom + self.top) / 2 | |
end | |
--# CheckBox | |
CheckBox = class(Frame) | |
-- CheckBox | |
-- ver. 1.0 | |
-- a two-state control | |
-- ==================== | |
function CheckBox:init(t, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.text = t | |
self.font = "ArialMT" | |
self.fontSize = 14 | |
self.background = color(30, 30, 30, 255) | |
self.foreground = color(255, 255, 255, 255) | |
self.highlight = color(255, 0, 26, 255) | |
self.selected = false | |
end | |
function CheckBox:draw() | |
pushStyle() | |
stroke(self.background) | |
fill(self.background) | |
self:roundRect(6) | |
self:offset(-3, 3) | |
stroke(self.foreground) | |
fill(self.foreground) | |
self:roundRect(6) | |
self:offset(3, -3) | |
stroke(self.highlight) | |
strokeWidth(5) | |
if self.selected then | |
line(self.left + 3, self:midY(), self:midX(), self.bottom + 4) | |
line(self:midX(), self.bottom + 4, self.right + 4, self.top) | |
end | |
fill(self.background) | |
textAlign(LEFT) | |
textMode(CORNER) | |
font(self.font) | |
fontSize(self.fontSize) | |
text(self.text, self.right + 8, self.bottom) | |
popStyle() | |
end | |
function CheckBox:initString() | |
return "CheckBox('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
function CheckBox:touched(touch) | |
if self:ptIn(touch.x, touch.y) then | |
if touch.state == BEGAN then | |
self.selected = not self.selected | |
end | |
return true | |
end | |
return false | |
end | |
--# DropList | |
DropList = class(Frame) | |
function DropList:init(s, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.text = s | |
self.font = "ArialMT" | |
self.fontSize = 14 | |
self.background = color(255, 255, 255, 255) | |
self.foreground = color(31, 31, 31, 255) | |
self.itemText = {} | |
self.open = false | |
self.selected = 1 | |
self:splitText() | |
end | |
function DropList:splitText() | |
local i, k | |
i = 0 | |
for k in string.gmatch(self.text,"([^;]+)") do | |
i = i + 1 | |
self.itemText[i] = k | |
end | |
end | |
function DropList:draw() | |
local i, t, h | |
pushStyle() | |
font(self.font) | |
fontSize(self.fontSize) | |
textMode(CENTER) | |
stroke(self.foreground) | |
fill(self.background) | |
rect(self.left, self.bottom, self:width(), self:height()) | |
fill(self.foreground) | |
if self.open then | |
for i, t in ipairs(self.itemText) do | |
text(t, self:midX(), self.top - i * 30 + 15) | |
end | |
strokeWidth(2) | |
stroke(243, 9, 9, 255) | |
line(self.left + 4, self.top - self.selected * 30, | |
self.right - 4, self.top - self.selected * 30) | |
line(self.left + 4, self.top - self.selected * 30 + 30, | |
self.right - 4, self.top - self.selected * 30 + 30) | |
else | |
text(self.itemText[self.selected], self:midX(), self:midY()) | |
end | |
popStyle() | |
end | |
function DropList:touched(touch) | |
local h | |
h = #self.itemText * 30 | |
if self:ptIn(touch.x, touch.y) then | |
if not self.open then | |
if touch.state == BEGAN then | |
self.open = true | |
self.bottom = self.top - h | |
end | |
else | |
self.selected = | |
math.floor((self.top - touch.y) / 30) | |
if self.selected < 1 then self.selected = 1 | |
elseif self.selected > #self.itemText then | |
self.selected = #self.itemText | |
end | |
end | |
end | |
if touch.state == ENDED then | |
self.open = false | |
self.bottom = self.top - 30 | |
end | |
end | |
function DropList:initString() | |
return "DropList('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
--# Dialog | |
Dialog = class(Frame) | |
function Dialog:init(t, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.text = t | |
self.inner = Frame(left + 4, bottom + 4, right - 4, top - 40) | |
self.background = color(255, 255, 255, 255) | |
self.foreground = color(20, 20, 20, 255) | |
self.highlight = color(95, 159, 172, 37) | |
end | |
function Dialog:draw() | |
pushMatrix() | |
pushStyle() | |
textMode(CENTER) | |
textAlign(CENTER) | |
fill(self.foreground) | |
stroke(self.foreground) | |
strokeWidth(2) | |
self:roundRect(8) | |
fill(self.background) | |
stroke(self.background) | |
text(self.text, self:midX(), self.top - 20) | |
self.inner:roundRect(10) | |
popStyle() | |
popMatrix() | |
end | |
function Dialog:touched(touch) | |
return self.inner:touched(touch) | |
end | |
function Dialog:initString() | |
return "Dialog('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
--# IconButton | |
IconButton = class(Frame) | |
-- IconButton | |
-- ver. 1.5 | |
-- a simple control that centers an image in a frame | |
-- ==================== | |
function IconButton:init(img, left, bottom, right, top) | |
Frame(self, left, bottom, right, top) | |
self.img = img | |
self.background = color(56, 56, 56, 255) | |
end | |
function IconButton:draw() | |
fill(self.background) | |
rect(self.left, self.bottom, self:width(), self:height()) | |
sprite(self.img, self:midX(), self:midY()) | |
end | |
--# Label | |
Label = class(Frame) | |
-- Label | |
-- ver. 1.5 | |
-- a control for basic text label | |
-- ==================== | |
function Label:init(s, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.text = s | |
self.font = "ArialMT" | |
self.fontSize = 14 | |
self.textAlign = CENTER | |
self.background = color(222, 222, 222, 255) | |
self.foreground = color(20, 20, 20, 255) | |
end | |
function Label:draw() | |
pushStyle() | |
font(self.font) | |
textMode(self.textAlign) | |
fontSize(self.fontSize) | |
fill(self.foreground) | |
text(self.text, self:midX(), self:midY()) | |
popStyle() | |
end | |
function Label:initString() | |
return "Label('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
--# Main | |
-- Cider Controls 1.3 | |
-- ===================== | |
-- Designed for use with the Cider interface designer | |
-- Use this file to test and demostrate controls in the library | |
-- ===================== | |
function setup() | |
btnTest = TextButton("Button", 100, HEIGHT - 100, 250, HEIGHT - 60) | |
txtTest = TextBox("Text Box", 100, HEIGHT - 160, 250, HEIGHT - 130) | |
txtTestC = TextBox("Text Box", 100, HEIGHT - 200, 250, HEIGHT - 170) | |
txtTestR = TextBox("Text Box", 100, HEIGHT - 240, 250, HEIGHT - 210) | |
txtLong = TextBox("Now is the winter of our discontent", | |
100, HEIGHT - 300, 250, HEIGHT - 250) | |
sldTest = Slider("Slider", 100, 100, 400, 170, 0, 111, 20) | |
swtTest = Switch("On;Off", 100, 200, 200, 230, 14, 48, 14) | |
mlbTest = MultiButton("Left;Center;Right", 100, 400, 450, 440) | |
lblTest = Label("Label Test", 100, 500, 350, 550) | |
dlgTest = Dialog("Dialog Test", 100, 300, WIDTH - 100, 600) | |
chkTest = CheckBox("Check box item", 100, 600, 130, 630) | |
txtTestC.textAlign = CENTER | |
txtTestR.textAlign = RIGHT | |
showDialog = false | |
end | |
-- This function gets called once every frame | |
function draw() | |
-- This sets a dark background color | |
background(181, 181, 181, 255) | |
-- This sets the line thickness | |
strokeWidth(5) | |
-- Do your drawing here | |
btnTest:draw() | |
txtTest:draw() | |
txtTestC:draw() | |
txtTestR:draw() | |
txtLong:draw() | |
swtTest:draw() | |
sldTest:draw() | |
mlbTest:draw() | |
lblTest:draw() | |
chkTest:draw() | |
if showDialog then dlgTest:draw() end | |
end | |
function touched(touch) | |
if btnTest:touched(touch) then | |
showDialog = true | |
end | |
txtTest:touched(touch) | |
txtTestC:touched(touch) | |
txtTestR:touched(touch) | |
txtLong:touched(touch) | |
if swtTest:touched(touch) then | |
CCActiveTextBox = nil | |
end | |
sldTest:touched(touch) | |
mlbTest:touched(touch) | |
lblTest:touched(touch) | |
chkTest:touched(touch) | |
end | |
function keyboard(key) | |
if CCActiveTextBox then | |
CCActiveTextBox:acceptKey(key) | |
end | |
end | |
--# MultiButton | |
MultiButton = class(Frame) | |
function MultiButton:init(s, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.text = s | |
self.font = "ArialMT" | |
self.fontSize = 14 | |
self.background = color(255, 255, 255, 255) | |
self.foreground = color(20, 20, 20, 255) | |
self.highlight = color(95, 159, 172, 37) | |
self.selected = 1 | |
self.itemText = {} | |
self:splitText() | |
end | |
function MultiButton:splitText() | |
local i, k | |
i = 0 | |
for k in string.gmatch(self.text,"([^;]+)") do | |
i = i + 1 | |
self.itemText[i] = k | |
end | |
end | |
function MultiButton:draw() | |
local w, i, b, h | |
pushStyle() | |
w = (self:width()) / #self.itemText | |
h = self:height() | |
strokeWidth(2) | |
fill(self.background) | |
stroke(self.foreground) | |
self:roundRect(6) | |
noStroke() | |
stroke(self.background) | |
self:inset(2, 2) | |
self:roundRect(6) | |
self:inset(-2, -2) | |
stroke(self.foreground) | |
textMode(CENTER) | |
font(self.font) | |
fontSize(self.fontSize) | |
for i, b in ipairs(self.itemText) do | |
fill(self.foreground) | |
strokeWidth(2) | |
if i < #self.itemText then | |
line(self.left + i * w, self.top, | |
self.left + i * w, self.bottom) | |
end | |
text(b, (self.left + i * w) - (w / 2), self:midY()) | |
noStroke() | |
fill(0, 0, 0, 22) | |
if i ~= self.selected then | |
h = self:height() | |
rect(self.left + i * w - w, self.bottom, | |
w, h * 0.6 ) | |
rect(self.left + i * w - w, self.bottom, | |
w, h * 0.4 ) | |
rect(self.left + i * w - w, self.bottom, | |
w, h * 0.2 ) | |
else | |
fill(self.highlight) | |
rect(self.left + i * w - w, self:midY() - h/4, | |
w, self:height() * 0.6) | |
rect(self.left + i * w - w, self:midY(), | |
w, self:height() * 0.4 ) | |
rect(self.left + i * w - w, self:midY() + h/4, | |
w, self:height() * 0.3 ) | |
end | |
end | |
popStyle() | |
end | |
function MultiButton:touched(touch) | |
if self:ptIn(touch.x, touch.y) then | |
if touch.state == BEGAN then | |
w = (self:width()) / #self.itemText | |
i = math.floor((touch.x - self.left) / w) + 1 | |
self.selected = i | |
end | |
return true | |
end | |
end | |
function MultiButton:initString() | |
return "MultiButton('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
--# PopMenu | |
PopMenu = class() | |
-- PopMenu | |
-- ver. 1.0 | |
-- a control that provides a simple menu | |
-- ==================== | |
function PopMenu:init(x, y) | |
self.x = x | |
self.y = y | |
self.items={} | |
self.frames = {} | |
end | |
function PopMenu:draw() | |
local h, w, x, i | |
pushStyle() | |
h = 10 | |
w = 100 | |
for i, item in ipairs(self.items) do | |
h = h + 60 | |
if string.len(item) * 20 > w then | |
w = string.len(item) * 20 | |
end | |
end | |
w = w + 20 | |
fill(0, 0, 0, 255) | |
rect(self.x, self.y, w, h) | |
textAlign(CENTER) | |
for i, item in ipairs(self.items) do | |
self.frames[i] = Frame(self.x + 10, self.y + i * 60 - 50, | |
self.x + w - 10, self.y + i * 60 ) | |
self.frames[i]:gloss(color(255, 255, 255, 255)) | |
x = self.x + w / 2 | |
text(item, x, self.y + i * 60 - 24) | |
end | |
popStyle() | |
end | |
function PopMenu:touched(touch) | |
local i | |
for i, frame in ipairs(self.frames) do | |
if frame:touched(touch) then | |
fill(255, 14, 0, 255) | |
frame:draw() | |
return i | |
end | |
end | |
return nil | |
end | |
--# Slider | |
Slider = class(Frame) | |
-- Slider | |
-- ver. 1.6 | |
-- a control that replicates the iparameter slider | |
-- ==================== | |
-- 1.6 bug fix on initString | |
function Slider:init(s, left, bottom, right, top, min, max, val) | |
Frame.init(self, left, bottom, right, top) | |
self.font = "ArialMT" | |
self.fontSize = 14 | |
self.text = s | |
self.background = color(238, 238, 238, 255) | |
self.foreground = color(31, 31, 31, 255) | |
self.highlight = color(132, 179, 190, 255) | |
self.min = min | |
self.max = max | |
self.val = val | |
end | |
function Slider:draw() | |
local x, y, scale | |
pushStyle() | |
font(self.font) | |
fontSize(self.fontSize) | |
stroke(self.foreground) | |
fill(self.background) | |
h, w = textSize(self.max) | |
scale = (self:width() - h * 2) / (self.max - self.min) | |
x = self.left + h + ((self.val - self.min) * scale) | |
y = self:midY() | |
strokeWidth(3) | |
line(self.left + h, y, self.right - h, y) | |
stroke(self.background) | |
line(self.left + h, y + 2, self.right - h, y + 2) | |
stroke(self.background) | |
line(self.left + h, y, self.right - h, y) | |
strokeWidth(1) | |
stroke(self.foreground) | |
fill(self.highlight) | |
ellipse(x, y, 20) | |
fill(self.foreground) | |
h, w = textSize("Slider") | |
textMode(CENTER) | |
textAlign(LEFT) | |
text(self.min, self.left, y) | |
textAlign(RIGHT) | |
text(self.max, self.right, y) | |
textAlign(CENTER) | |
text(self.text, self:midX(), y + h) | |
if self.val > self.min and self.val < self.max then | |
text(self.val, x, y + h / 2) | |
end | |
popStyle() | |
end | |
function Slider:touched(touch) | |
local x, scale | |
if touch.state == BEGAN or touch.state == MOVING then | |
if self:ptIn(touch.x, touch.y) then | |
x = touch.x - self.left - 10 | |
scale = ((self.right - self.left) - 20) / | |
(self.max - self.min) | |
self.val = math.floor(x / scale) + self.min | |
if self.val < self.min then | |
self.val = self.min | |
elseif self.val > self.max then | |
self.val = self.max | |
end | |
return true | |
end | |
end | |
end | |
function Slider:initString() | |
return "Slider('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..", "..self.min..", ".. | |
self.max..", "..self.val..")" | |
end | |
--# Switch | |
Switch = class(Frame) | |
-- Switch | |
-- ver. 1.5 | |
-- a control for displaying a two position switch | |
-- ==================== | |
-- notes | |
-- 1.6 cosmetic changes | |
-- 1.5 implemented inheritance from Frame | |
function Switch:init(s, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.text = s | |
self.font = "ArialMT" | |
self.fontSize = 14 | |
self.background = color(255, 255, 255, 255) | |
self.foreground = color(20, 20, 20, 255) | |
self.highlight = color(97, 159, 173, 255) | |
self.selected = true | |
self.itemText = {} | |
self:splitText() | |
end | |
function Switch:splitText() | |
local i, k | |
i = 0 | |
for k in string.gmatch(self.text,"([^;]+)") do | |
i = i + 1 | |
self.itemText[i] = k | |
end | |
end | |
function Switch:draw() | |
pushStyle() | |
font(self.font) | |
fontSize(self.fontSize) | |
strokeWidth(1) | |
if self.selected then | |
stroke(self.highlight) | |
fill(self.highlight) | |
h = self:height() | |
self:roundRect(h/2) | |
fill(255, 255, 255, 53) | |
rect(self.left + h/2, self.top - 10, self:width() - h, 8) | |
strokeWidth(1) | |
stroke(76, 76, 76, 255) | |
fill(227, 227, 227, 255) | |
ellipse(self.right - h/2, self:midY(), self:height()) | |
fill(self.foreground) | |
if #self.itemText > 0 then | |
text(self.itemText[1], self:midX(), self:midY()) | |
end | |
else | |
fill(self.background) | |
stroke(self.background) | |
h = self:height() | |
self:roundRect(h/2) | |
fill(0, 0, 0, 18) | |
rect(self.left + h, self.bottom + 2, self:width() - h*1.5, 8) | |
strokeWidth(1) | |
stroke(self.foreground) | |
fill(self.background) | |
ellipse(self.left + h/2, self:midY(), self:height()) | |
fill(self.foreground) | |
if #self.itemText > 1 then | |
text(self.itemText[2], self:midX(), self:midY()) | |
end | |
end | |
popStyle() | |
end | |
function Switch:touched(touch) | |
if self:ptIn(touch.x, touch.y) then | |
if touch.state == BEGAN then | |
self.selected = not self.selected | |
end | |
return true | |
else | |
return false | |
end | |
end | |
function Switch:initString() | |
return "Switch('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
--# TextBox | |
TextBox = class(Frame) | |
-- TextBox | |
-- ver. 2.6 | |
-- a control for basic string editing | |
-- ==================== | |
-- 2.6 added CCActiveTextBox for managing text box selection | |
-- cosmetic improvements | |
-- improved keyboard handling | |
CCActiveTextBox = nil | |
function TextBox:init(s, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.text = s | |
self.font = "ArialMT" | |
self.textAlign = LEFT | |
self.fontSize = 14 | |
self.background = color(255, 255, 255, 255) | |
self.foreground = color(20, 20, 20, 255) | |
self.highlight = color(124, 142, 194, 255) | |
self.blink = ElapsedTime | |
self.blinkstate = true | |
end | |
function TextBox:draw() | |
local x, w, h | |
pushStyle() | |
pushMatrix() | |
font(self.font) | |
textMode(CORNER) | |
textWrapWidth(self:width() - 8) | |
textAlign(self.textAlign) | |
fontSize(self.fontSize) | |
noStroke() | |
fill(self.foreground) | |
self:offset(-1, 1) | |
rect(self.left, self.bottom, self:width(), self:height()) | |
fill(self.background) | |
self:offset(1, -1) | |
w, h = textSize(self.text) | |
rect(self.left, self.bottom, self:width(), self:height()) | |
fill(self.foreground) | |
if self.textAlign == LEFT then | |
text(self.text, self.left + 4, self.bottom + 4) | |
x = self.left + w + 6 | |
elseif self.textAlign == CENTER then | |
text(self.text, self:midX() - w / 2, self.bottom + 4) | |
x = self:midX() + w / 2 + 2 | |
elseif self.textAlign == RIGHT then | |
text(self.text, self.right - w - 8, self.bottom + 4) | |
x = self.right - 4 | |
end | |
if self == CCActiveTextBox then | |
if self.blink < ElapsedTime - 0.3 then | |
self.blink = ElapsedTime | |
self.blinkstate = not self.blinkstate | |
end | |
if self.blinkstate then | |
strokeWidth(3) | |
stroke(self.highlight) | |
--x = self.left + w + 6 | |
line(x, self.bottom + 4, x, self.bottom + 4 + h) | |
end | |
end | |
popMatrix() | |
popStyle() | |
end | |
function TextBox:acceptKey(k) | |
if k ~= nil then | |
if string.byte(k) == nil then | |
if string.len(self.text) > 0 then | |
self.text = string.sub(self.text, | |
1, string.len(self.text) - 1) | |
end | |
end | |
self.text = self.text..k | |
end | |
end | |
function TextBox:initString() | |
return "TextBox('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
function TextBox:touched(touch) | |
if self:ptIn(touch.x, touch.y) then | |
CCActiveTextBox = self | |
if not isKeyboardShowing() then showKeyboard() end | |
return true | |
else | |
return false | |
end | |
end | |
--# TextButton | |
TextButton = class(Frame) | |
-- TextButton | |
-- ver. 1.6 | |
-- a control for displaying a simple button | |
-- ==================== | |
-- 1.6 cosmetic changes | |
function TextButton:init(s, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.text = s | |
self.font = "ArialMT" | |
self.fontSize = 14 | |
self.itemText = {} | |
self.background = color(0, 0, 0, 255) | |
self.foreground = color(253, 253, 253, 255) | |
self.highlight = color(123, 183, 197, 255) | |
self.status = false | |
end | |
function TextButton:draw() | |
pushStyle() | |
font(self.font) | |
fontSize(self.fontSize) | |
if not self.status then | |
stroke(self.background) | |
fill(self.background) | |
self:roundRect(5) | |
self:offset(-4, 4) | |
stroke(self.foreground) | |
fill(self.foreground) | |
self:roundRect(5) | |
self:offset(2, -2) | |
stroke(self.highlight) | |
fill(self.highlight) | |
self:roundRect(5) | |
self:offset(2, -2) | |
fill(255, 255, 255, 33) | |
rect(self.left + 12, self.top - 12, | |
self:width()- 24, 8) | |
fill(0, 0, 0, 36) | |
rect(self.left + 12, self.bottom + 4, | |
self:width()- 24, 4) | |
fill(self.background) | |
text(self.text, self:midX(), self:midY() + 2) | |
else | |
stroke(self.foreground) | |
fill(self.foreground) | |
self:roundRect(5) | |
self:offset(-4, 4) | |
stroke(self.background) | |
fill(self.background) | |
self:roundRect(5) | |
self:offset(2, -2) | |
stroke(self.highlight) | |
fill(self.highlight) | |
self:roundRect(5) | |
self:offset(2, -2) | |
fill(255, 255, 255, 33) | |
rect(self.left + 12, self.top - 12, | |
self:width()- 24, 8) | |
fill(0, 0, 0, 36) | |
rect(self.left + 12, self.bottom + 4, | |
self:width()- 24, 4) | |
fill(self.background) | |
text(self.text, self:midX() -2, self:midY() + 4) | |
end | |
popStyle() | |
end | |
function TextButton:touched(touch) | |
if self:ptIn(touch.x, touch.y) and touch.state ~= ENDED then | |
self.status = true | |
return true | |
else | |
self.status = false | |
return false | |
end | |
end | |
function TextButton:initString() | |
return "TextButton('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
--# Ttouch | |
Ttouch = class() | |
-- Translatable Touch | |
-- ver. 1.0 | |
-- maps fields of a touch but is easily modified. | |
-- ====================. | |
function Ttouch:init(touch) | |
self.x = touch.x | |
self.y = touch.y | |
self.state = touch.state | |
self.prevX = touch.prevX | |
self.prevY = touch.prevY | |
self.deltaX = touch.deltaX | |
self.deltaY = touch.deltaY | |
self.id = touch.id | |
self.tapCount = touch.tapCount | |
self.timer = 0 | |
end | |
function Ttouch:translate(x, y) | |
self.x = self.x - x | |
self.y = self.y - y | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment