Created
October 21, 2012 16:33
-
-
Save devilstower/3927497 to your computer and use it in GitHub Desktop.
Cider Controls 1.5
This file contains 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 | |
--# Control | |
Control = class(Frame) | |
-- Control 1.0 | |
-- ===================== | |
-- Designed for use with Cider controls | |
-- Used alone, and as a base class for other controls | |
-- ===================== | |
TOP = 1 | |
MIDDLE = 2 | |
BOTTOM = 3 | |
function Control:init(s, left, bottom, right, top) | |
Frame.init(self, left, bottom, right, top) | |
self.font = "ArialMT" | |
self.fontSize = 16 | |
self.textAlign = LEFT | |
self.vertAlign = TOP | |
self.text = s | |
self.background = color(63, 105, 149, 255) | |
self.foreground = color(14, 14, 14, 255) | |
self.highlight = color(112, 226, 78, 255) | |
self.selected = 1 | |
self.value = 1 | |
self.active = true | |
self.controlName = "Control" | |
end | |
function Control:draw() | |
local i, h, w, x | |
pushStyle() | |
noStroke() | |
fill(self.background) | |
Frame.draw(self) | |
fill(0, 0, 0, 3) | |
for i = 1,10 do | |
rect(self.left, self.bottom, self:width(), self:height()/(i)) | |
end | |
fill(self.foreground) | |
textAlign(LEFT) | |
textMode(CORNER) | |
font(self.font) | |
fontSize(self.fontSize) | |
w, h = textSize(self.text) | |
if self.textAlign == LEFT then | |
x = self.left + 4 | |
elseif self.textAlign == CENTER then | |
x = self:midX() - w / 2 | |
elseif self.textAlign == RIGHT then | |
x = self.right - w - 8 | |
end | |
if self.vertAlign == TOP then | |
text(self.text, x, self.top - h) | |
elseif self.vertAlign == MIDDLE then | |
text(self.text, x, self:midY()- h/2) | |
elseif self.vertAlign == BOTTOM then | |
text(self.text, x, self.bottom + 4) | |
end | |
popStyle() | |
end | |
function Control:initString() | |
return self.controlName.."('"..self.text.."', " .. | |
self.left..", "..self.bottom..", ".. | |
self.right..", "..self.top..")" | |
end | |
--# CheckBox | |
CheckBox = class(Control) | |
-- CheckBox 1.1 | |
-- ===================== | |
-- Designed for use with the Cider interface designer | |
-- A two-state inducator that alters state on touch | |
-- ===================== | |
function CheckBox:init(t, left, bottom, right, top) | |
Control.init(self, t, left, bottom, right, top) | |
self.controlName = "CheckBox" | |
end | |
function CheckBox:draw() | |
pushStyle() | |
noStroke() | |
fill(self.background) | |
ellipse(self.left + 20, self:midY(), 40) | |
if self.selected then | |
fill(self.highlight.r, self.highlight.g, self.highlight.b, 99) | |
ellipse(self.left + 20, self:midY(), 35) | |
fill(self.highlight) | |
ellipse(self.left + 20, self:midY(), 25) | |
fill(255, 255, 255, 53) | |
ellipse(self.left + 20, self:midY() + 5, 20, 10) | |
fill(0, 0, 0, 33) | |
ellipse(self.left + 20, self:midY() - 6, 20, 10) | |
else | |
fill(0, 0, 0, 80) | |
ellipse(self.left + 20, self:midY(), 25) | |
end | |
fill(self.foreground) | |
textAlign(LEFT) | |
textMode(CORNER) | |
font(self.font) | |
fontSize(self.fontSize) | |
text(self.text, self.left + 45, self:midY()- 8) | |
popStyle() | |
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 | |
--# Dialog | |
Dialog = class(Control) | |
-- Dialog 1.0 | |
-- ===================== | |
-- Designed for use with the Cider controls | |
-- A simple pop up window for other controls | |
-- ===================== | |
function Dialog:init(t, left, bottom, right, top) | |
Control.init(self, t, left, bottom, right, top) | |
self.controlName = "Dialog" | |
self.text = t | |
self.inner = Frame(left + 4, bottom + 4, right - 4, top - 40) | |
self.background = color(255, 255, 255, 255) | |
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 | |
--# ColorSelector | |
ColorSelector = class(Control) | |
function ColorSelector:init(x, y) | |
Control.init(self, "", x, y, x + 250, y + 250) | |
sldRed = Slider("", 70, 200, 220, 220, 0, 256, 0) | |
sldGreen = Slider("", 70, 160, 220, 180, 0, 256, 0) | |
sldBlue = Slider("", 70, 120, 220, 140, 0, 256, 0) | |
sldAlpha = Slider("", 70, 80, 220, 100, 0, 256, 0) | |
ctlSample = Control("", 70, 10, 220, 60) | |
self.background = color(203, 203, 203, 255) | |
end | |
function ColorSelector:setColor(c) | |
sldRed.val = c.r | |
sldGreen.val = c.g | |
sldBlue.val = c.b | |
sldAlpha.val = c.a | |
end | |
function ColorSelector:draw() | |
pushStyle() | |
pushMatrix() | |
self:offset(4, -4) | |
self.background = color(47, 47, 47, 99) | |
Control.draw(self) | |
self:offset(-4, 4) | |
self.background = color(206, 206, 206, 255) | |
Control.draw(self) | |
translate(self.left, self.bottom) | |
sldRed:draw() | |
sldGreen:draw() | |
sldBlue:draw() | |
sldAlpha:draw() | |
ctlSample:draw() | |
fill(21, 21, 21, 255) | |
textAlign(LEFT) | |
textMode(CORNER) | |
text("Red", sldRed.left - 60, sldRed.bottom) | |
text("Green", sldRed.left - 60, sldGreen.bottom) | |
text("Blue", sldRed.left - 60, sldBlue.bottom) | |
text("Alpha", sldRed.left - 60, sldAlpha.bottom) | |
popMatrix() | |
popStyle() | |
end | |
function ColorSelector:getColor() | |
local c | |
c = color(sldRed.val, sldGreen.val, sldBlue.val, sldAlpha.val) | |
return c | |
end | |
function ColorSelector:touched(touch) | |
local tt | |
if Control.touched(self, touch) then | |
tt = Ttouch(touch) | |
tt:translate(self.left, self.bottom) | |
sldRed:touched(tt) | |
sldGreen:touched(tt) | |
sldBlue:touched(tt) | |
sldAlpha:touched(tt) | |
ctlSample.background = self:getColor() | |
return true | |
else | |
return false | |
end | |
end | |
--# DropList | |
DropList = class(Control) | |
-- DropList 1.2 | |
-- ===================== | |
-- Designed for use with the Cider interface designer | |
-- drop down menu item list | |
-- ===================== | |
function DropList:init(s, left, bottom, right, top) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "DropList" | |
self.itemText = {} | |
self.open = false | |
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) | |
Frame.draw(self) | |
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 | |
--# IconButton | |
IconButton = class(Control) | |
-- IconButton | |
-- ver. 1.1 | |
-- a simple control that centers an image in a frame | |
-- ==================== | |
-- 1.1 derives from Control class, refactored | |
function IconButton:init(s, left, bottom, right, top, img) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "IconButton" | |
self.foreground = color(191, 191, 191, 255) | |
self.background = color(91, 91, 91, 255) | |
self.img = img | |
self.textAlign = CENTER | |
self.vertAlign = BOTTOM | |
end | |
function IconButton:draw() | |
local h, w | |
w, h = textSize(self.text) | |
pushStyle() | |
Control.draw(self) | |
sprite(self.img, self:midX(), self:midY() + h / 2) | |
popStyle() | |
end | |
--# Label | |
Label = class(Control) | |
-- Label | |
-- ver. 1.5 | |
-- a control for basic text label | |
-- ==================== | |
-- 1.5 derives from Control class, refactored | |
function Label:init(s, left, bottom, right, top) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "IconButton" | |
self.textAlign = LEFT | |
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 | |
--# Main | |
-- Cider Controls 1.5 | |
-- ===================== | |
-- Designed for use with the Cider interface designer | |
-- Use this file to test and demostrate controls in the library | |
-- ===================== | |
function setup() | |
ctlText = Control("Text Boxes", 20, HEIGHT - 300, 300, HEIGHT - 50) | |
ctlText.background = color(181, 141, 203, 255) | |
txtTest = TextBox("Text Box", 40, HEIGHT - 120, 280, HEIGHT - 90) | |
txtTestC = TextBox("Text Box", 40, HEIGHT - 160, 280, HEIGHT - 130) | |
txtTestR = TextBox("Text Box", 40, HEIGHT - 200, 280, HEIGHT - 170) | |
txtLong = TextBox("Now is the winter of our discontent", | |
70, HEIGHT - 280, 280, HEIGHT - 210) | |
txtTestC.textAlign = CENTER | |
txtTestR.textAlign = RIGHT | |
ctlSwitch = Control("Switches", 320, HEIGHT - 300, WIDTH - 20, | |
HEIGHT - 50) | |
ctlSwitch.background = color(255, 182, 0, 255) | |
swtTest = Switch("On;Off", 340, 900, 440, 930, 14, 48, 14) | |
chkTest = CheckBox("Check box", 340, 850, 440, 880) | |
ctlSlider = Control("Sliders", 20, HEIGHT - 500, | |
WIDTH - 20, HEIGHT - 330) | |
ctlSlider.background = color(223, 200, 168, 255) | |
sldTest = Slider("", 70, HEIGHT - 400, 400, HEIGHT - 370, | |
0, 111, 20) | |
ntsTest = NotchSlider("A;B;C", 90, HEIGHT - 460, 360, HEIGHT - 430) | |
ctlButton = Control("Buttons", 20, 170, WIDTH - 20, 480) | |
mlbTest = MultiButton("Left;Center;Right", 100, 400, 450, 440) | |
btnTest = TextButton("Button", 100, 330, 250, 370) | |
icbTest = IconButton("IconButton", 100, 200, 200, 300, | |
"Small World:Flag") | |
dlgTest = Dialog("Dialog Test", 100, 300, WIDTH - 100, 600) | |
showDialog = false | |
clrPicker = ColorSelector(100, 100) | |
showColor = 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 | |
ctlText:draw() | |
ctlSwitch:draw() | |
ctlSlider:draw() | |
ctlButton:draw() | |
btnTest:draw() | |
txtTest:draw() | |
txtTestC:draw() | |
txtTestR:draw() | |
txtLong:draw() | |
swtTest:draw() | |
sldTest:draw() | |
mlbTest:draw() | |
icbTest:draw() | |
chkTest:draw() | |
ntsTest:draw() | |
if showDialog then | |
sound(SOUND_JUMP, 24666) | |
dlgTest:draw() | |
end | |
if showColor then | |
clrPicker:draw() | |
end | |
end | |
function touched(touch) | |
if showColor then | |
showColor = clrPicker:touched(touch) | |
icbTest.background = clrPicker:getColor() | |
end | |
if btnTest:touched(touch) then | |
showDialog = true | |
end | |
if icbTest:touched(touch) then | |
showColor = true | |
end | |
if dlgTest:touched(touch) then | |
showDialog = false | |
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) | |
chkTest:touched(touch) | |
ntsTest:touched(touch) | |
end | |
function keyboard(key) | |
if CCActiveTextBox then | |
CCActiveTextBox:acceptKey(key) | |
end | |
end | |
--# MultiButton | |
MultiButton = class(Control) | |
-- MultiButton 1.2 | |
-- ===================== | |
-- Designed for use with the Cider Controls | |
-- Allows selecting between 2-x choices | |
-- ===================== | |
function MultiButton:init(s, left, bottom, right, top) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "MultiButton" | |
self.itemText = {} | |
self:splitText() | |
self.background = color(136, 168, 199, 255) | |
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 + 1, self.bottom + 3, | |
w - 2, self:height() - 6) | |
fill(0, 0, 0, 28) | |
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 ) | |
fill(self.foreground) | |
text(b, (self.left + i * w) - (w / 2), self:midY()) | |
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 | |
--# NotchSlider | |
NotchSlider = class(Control) | |
-- NotchSlider 1.0 | |
-- ===================== | |
-- Designed for use with Cider controls | |
-- offers option of sliding to preset positions | |
-- ===================== | |
function NotchSlider:init(s, left, bottom, right, top) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "NotchSlider" | |
self.itemText = {} | |
self:splitText() | |
end | |
function NotchSlider:splitText() | |
local i, k | |
i = 0 | |
for k in string.gmatch(self.text,"([^;]+)") do | |
i = i + 1 | |
self.itemText[i] = k | |
end | |
end | |
function NotchSlider:draw() | |
local x, i, scale | |
pushStyle() | |
font(self.font) | |
fontSize(self.fontSize) | |
textAlign(CENTER) | |
textMode(CENTER) | |
strokeWidth(20) | |
stroke(self.background) | |
fill(self.background) | |
line(self.left, self:midY(), self.right, self:midY()) | |
if #self.itemText > 1 then | |
x = self:width() / (#self.itemText - 1) | |
for i = 1, #self.itemText do | |
fill(self.background) | |
stroke(self.background) | |
ellipse(self.left + (i-1) * x, self:midY(), 35) | |
fill(self.foreground) | |
text(self.itemText[i], self.left + (i-1) * x, | |
self:midY() + 25) | |
end | |
x = self.left + (self.selected - 1) * x | |
else | |
x = self:midX() | |
end | |
fill(0, 0, 0, 16) | |
noStroke() | |
ellipse(x, self:midY() - 12, 25, 12) | |
ellipse(x, self:midY() - 10, 45, 30) | |
strokeWidth(12) | |
stroke(self.highlight) | |
ellipse(x, self:midY(), 40) | |
fill(255, 255, 255, 56) | |
noStroke() | |
ellipse(x, self:midY() + 12, 25, 12) | |
popStyle() | |
end | |
function NotchSlider:touched(touch) | |
local x, scale | |
if touch.state == BEGAN or touch.state == MOVING then | |
if self:ptIn(touch.x, touch.y) then | |
if #self.itemText > 1 then | |
scale = self:width() / (#self.itemText - 1) | |
x = touch.x - self.left + 20 | |
self.selected = math.floor(x / scale) + 1 | |
end | |
return true | |
end | |
end | |
end | |
--# PopMenu | |
PopMenu = class() | |
-- PopMenu | |
-- ver. 1.0 | |
-- a control that provides a simple menu | |
-- warning: not looked at in a LONG time | |
-- ==================== | |
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(Control) | |
-- Slider 1.3 | |
-- ===================== | |
-- Designed for use with Cider controls | |
-- offers option of sliding to 1-x values | |
-- ===================== | |
-- 1.3 refactored, based on Control class | |
function Slider:init(s, left, bottom, right, top, min, max, val) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "Slider" | |
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(15) | |
stroke(self.background) | |
line(self.left + h, y, self.right - h, y) | |
stroke(self.highlight) | |
line(self.left + h, y, x, y) | |
stroke(255, 255, 255, 29) | |
strokeWidth(7) | |
line(self.left + h, y +4, x, y + 4) | |
strokeWidth(3) | |
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(Control) | |
-- Switch 1.3 | |
-- ===================== | |
-- Designed for use with Cider controls | |
-- two-position selector | |
-- ===================== | |
-- 1.3 cosmetic changes, refactored, based on Control | |
function Switch:init(s, left, bottom, right, top) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "Switch" | |
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) | |
noStroke() | |
stroke(self.foreground) | |
fill(0, 0, 0, 39) | |
ellipse(self.right - h/2 + 4, self:midY(), self:height() - 4) | |
strokeWidth(1) | |
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) | |
noStroke() | |
stroke(self.foreground) | |
fill(0, 0, 0, 37) | |
ellipse(self.left + h/2 +4, self:midY(), self:height() - 4) | |
strokeWidth(1) | |
noFill() | |
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 | |
--# TextBox | |
TextBox = class(Control) | |
-- TextBox (numbers restarted, as this is a rewrite) | |
-- ver. 1.5 | |
-- a control for basic string editing | |
-- ==================== | |
-- 1.5 Refactored, based on Control | |
CCActiveTextBox = nil | |
function TextBox:init(s, left, bottom, right, top) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "TextBox" | |
self.background = color(255, 255, 255, 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) | |
w, h = textSize(self.text) | |
Control.draw(self) | |
if self.textAlign == LEFT then | |
x = self.left + w + 6 | |
elseif self.textAlign == CENTER then | |
x = self:midX() + w / 2 + 2 | |
elseif self.textAlign == RIGHT then | |
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) | |
line(x, self.top - h, x, self.top) | |
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: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(Control) | |
-- TextButton | |
-- ver. 1.7 | |
-- a control for displaying a simple button | |
-- ==================== | |
-- 1.7 derived from Control class, appearance changes | |
-- 1.6 cosmetic changes | |
function TextButton:init(s, left, bottom, right, top) | |
Control.init(self, s, left, bottom, right, top) | |
self.controlName = "TextButton" | |
self.pressed = false | |
self.background = color(119, 158, 199, 255) | |
self.highlight = color(64, 192, 66, 255) | |
end | |
function TextButton:draw() | |
pushStyle() | |
font(self.font) | |
fontSize(self.fontSize) | |
stroke(self.foreground) | |
fill(self.foreground) | |
self:roundRect(5) | |
self:inset(2, 2) | |
if self.pressed then | |
stroke(self.highlight) | |
fill(self.highlight) | |
else | |
stroke(self.background) | |
fill(self.background) | |
end | |
self:roundRect(5) | |
self:inset(-2, -2) | |
fill(255, 255, 255, 33) | |
noStroke() | |
rect(self.left, self:midY() - 4, self:width(), self:height() / 2) | |
rect(self.left, self:midY() + self:height() / 4 - 4, self:width(), | |
self:height() / 4) | |
fill(0, 0, 0, 37) | |
rect(self.left, self.bottom, self:width(), self:height() / 4 - 4) | |
fill(self.foreground) | |
text(self.text, self:midX(), self:midY()) | |
popStyle() | |
end | |
function TextButton:touched(touch) | |
if self:ptIn(touch.x, touch.y) and touch.state ~= ENDED then | |
self.pressed = true | |
return true | |
else | |
self.pressed = false | |
return false | |
end | |
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