Created
December 31, 2014 19:29
-
-
Save edt11x/8dcd82b4d04331143aef to your computer and use it in GitHub Desktop.
Displacement Release v0.02 -Displacement Calculator
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
Displacement Tab Order Version: 0.02 | |
------------------------------ | |
This file should not be included in the Codea project. | |
#cDisplacement | |
#Dialog | |
#Displacement | |
#Input | |
#Main |
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
cDisplacement = class() | |
function cDisplacement:init(x) | |
-- you can accept and set parameters here | |
self.x = x | |
self.bore = 80.0 | |
self.stroke = 71.0 | |
self.cylinders = 3.0 | |
self.displacement = 0 | |
end | |
function cDisplacement:compute(bore,stroke,cylinders) | |
self.bore = bore | |
self.stroke = stroke | |
self.cylinders = cylinders | |
self.displacement = self.bore * self.bore * math.pi * self.stroke * self.cylinders / (4 * 1000) | |
return self.displacement | |
end |
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
Dialog = class() | |
function Dialog:init() | |
-- you can accept and set parameters here | |
end | |
function Dialog:draw() | |
-- Codea does not automatically call this method | |
background(100, 120, 163) | |
font("Georgia") | |
fontSize(20) | |
textWrapWidth(70) | |
w,h = textSize("Hello World!") | |
fill(120,0,40) | |
strokeWidth(2) | |
rectMode(CENTER) | |
rect(WIDTH/2,HEIGHT/2,w+15,h+15) | |
fill(255) | |
textMode(CENTER) | |
text("Hello World!",WIDTH/2, HEIGHT/2) | |
end | |
function Dialog:touched(touch) | |
-- Codea does not automatically call this method | |
end |
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
Displacement = class() | |
function Displacement:init(font) | |
-- you can accept and set parameters here | |
self.font = font | |
self.titleIndent = 60 | |
self.boxIndent = 240 | |
local w = 100 | |
local h = 25 | |
BoreStr = Input(self.boxIndent, HEIGHT-100, w, h, self.font, "80") | |
StrokeStr = Input(self.boxIndent, HEIGHT-160, w, h, self.font, "71") | |
CylindersStr = Input(self.boxIndent, HEIGHT-220, w, h, self.font, "3") | |
self.cDisp = cDisplacement() | |
end | |
function Displacement:draw() | |
-- Codea does not automatically call this method | |
background(40, 40, 50) | |
strokeWidth(5) | |
textMode(CORNER) | |
font(self.font) | |
fill(255) | |
fontSize(20) | |
text("Compute Displacement from Bore and Stroke", self.titleIndent, HEIGHT-50) | |
text("Bore", self.titleIndent, HEIGHT-100) | |
text("Stroke", self.titleIndent, HEIGHT-160) | |
text("Cylinders", self.titleIndent, HEIGHT-220) | |
text("Displacement", self.titleIndent, HEIGHT-280) | |
Input._draw() | |
Bore = tonumber(BoreStr.txt) | |
Stroke = tonumber(StrokeStr.txt) | |
Cylinders = tonumber(CylindersStr.txt) | |
Disp = self.cDisp:compute(BoreStr.txt, StrokeStr.txt, CylindersStr.txt) | |
text(Disp, self.boxIndent, HEIGHT-280) | |
end | |
function Displacement:touched(touch) | |
-- Codea does not automatically call this method | |
end |
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
--[[-- | |
GLOBAL FUNCTIONS | |
These global functions can be declared out of this file if required | |
--]]-- | |
function keyboard(key) | |
Input._put(key) | |
end | |
function touched(touch) | |
Input._touched(touch) | |
end | |
--[[ INPUT CLASS ]]-- | |
Input = class() | |
Input.instances = {} | |
local padding = 10 | |
--[[ Class functions ]]-- | |
function Input._draw() | |
for _,input in ipairs(Input.instances) do | |
input:draw() | |
end | |
end | |
function Input._touched(touch) | |
for _,input in ipairs(Input.instances) do | |
input:touched(touch) | |
end | |
end | |
function Input._put(key) | |
for _,input in ipairs(Input.instances) do | |
if input.focus then | |
input:put(key) | |
end | |
end | |
end | |
--[[ Instance functions ]]-- | |
function Input:init(x, y, w, h, fontName, txt, onEnter, target) | |
table.insert(Input.instances, self) | |
self.fontSize = h | |
self.fontName = fontName | |
font(self.fontName) | |
fontSize(self.fontSize) | |
self.x = x | |
self.y = y | |
self.w = w | |
self.h = h | |
if txt then | |
self.txt = txt | |
else | |
self.txt = "" | |
end | |
self.pos = string.len(self.txt) | |
self.winPos = 0 | |
self.cursor = Cursor(self.h+padding/2) | |
self:blur() | |
if onEnter == nil then | |
self.onEnter = function() print(self.txt) end | |
else | |
self.onEnter = onEnter | |
end | |
self.target = target | |
end | |
function Input:put(key) | |
if key == BACKSPACE then | |
self:backspace() | |
elseif key == RETURN then | |
self:enter() | |
else | |
self:insert(key) | |
end | |
end | |
function Input:backspace() | |
if self.pos > 0 then | |
local head,tail = self:splitAndDrop(1) | |
self.txt = head..tail | |
self.pos = self.pos - 1 | |
if self.pos == self.winPos and self.winPos > 0 then | |
self.winPos = self.winPos - 1 | |
end | |
end | |
end | |
function Input:insert(key) | |
local head,tail = self:splitAndDrop(0) | |
self.txt = head..key..tail | |
self.pos = self.pos + 1 | |
while self.pos > self:winSize() do | |
self.winPos = self.winPos + 1 | |
end | |
end | |
function Input:enter() | |
self:blur() | |
if self.target then | |
self.onEnter(self.target, self.txt) | |
else | |
self.onEnter(self.txt) | |
end | |
end | |
function Input:splitAndDrop(dropCount) | |
local head = string.sub(self.txt, 0, self.pos - dropCount) | |
local tail = string.sub(self.txt, self.pos + 1) | |
return head,tail | |
end | |
function Input:blur() | |
self.focus = false | |
hideKeyboard() | |
end | |
function Input:visiblePart() | |
local winSize = self:winSize() | |
return string.sub(self.txt, self.winPos + 1, self.winPos + winSize) | |
end | |
function Input:winSize() | |
font(self.fontName) | |
local winSize = string.len(self.txt) | |
local part = string.sub(self.txt, self.winPos, self.winPos + winSize) | |
local w,_ = textSize(part) | |
while w > self.w do | |
winSize = winSize - 1 | |
part = string.sub(self.txt, self.winPos, self.winPos + winSize) | |
w,_ = textSize(part) | |
end | |
return winSize | |
end | |
function Input:draw() | |
pushMatrix() | |
translate(self.x, self.y) | |
pushStyle() | |
self:showBox() | |
self:showText() | |
self:posCursor() | |
if self.focus then | |
self:showBorder() | |
self.cursor:draw() | |
end | |
popStyle() | |
popMatrix() | |
end | |
function Input:showBox() | |
fill(255, 255, 255, 255) | |
stroke(255, 255, 255, 255) | |
strokeWidth(padding) | |
rect(-padding, -padding, self.w+2*padding, self.h+2*padding) | |
end | |
function Input:showBorder() | |
noFill() | |
strokeWidth(2) | |
stroke(127, 127, 127, 255) | |
rect(-padding, -padding, self.w+2*padding, self.h+2*padding) | |
end | |
function Input:showText() | |
font(self.fontName) | |
textMode(CORNER) | |
fill(0, 0, 0, 255) | |
text(self:visiblePart(), 0, 0) | |
end | |
function Input:touched(touch) | |
if self:contains(touch) then | |
self.focus = true | |
if not isKeyboardShowing() then | |
showKeyboard() | |
end | |
if touch.state ~= ENDED then | |
self:moveCursor(touch.x) | |
end | |
else | |
self.focus = false | |
end | |
end | |
function Input:contains(touch) | |
return self.x <= touch.x | |
and touch.x <= (self.x + self.w) | |
and self.y <= touch.y | |
and touch.y <= (self.y + self.h) | |
end | |
function Input:moveCursor(x) | |
pushStyle() | |
font(self.fontName) | |
local min = math.huge | |
local part = self:visiblePart() | |
for i = 0,string.len(part) do | |
local sub = string.sub(part, 0, i) | |
local w,_ = textSize(sub) | |
local dist = math.abs(x - (self.x + w)) | |
if dist < min then | |
min = dist | |
self.pos = self.winPos + i | |
end | |
end | |
popStyle() | |
local rightEnd = self.winPos + self:winSize() | |
if self.pos == self.winPos and self.winPos > 0 then | |
self.winPos = self.winPos - 1 | |
elseif self.pos == rightEnd and rightEnd < string.len(self.txt) then | |
self.winPos = self.winPos + 1 | |
end | |
self:posCursor() | |
end | |
function Input:posCursor() | |
pushStyle() | |
font(self.fontName) | |
local part = self:visiblePart() | |
local relPos = self.pos - self.winPos | |
local sub = string.sub(part, 0, relPos) | |
self.cursor.x,_ = textSize(sub) | |
popStyle() | |
end | |
Cursor = class() | |
function Cursor:init(h) | |
self.h = h | |
self.x = 0 | |
self.show = true | |
tween.delay(0.4, Cursor._blink, self) | |
end | |
function Cursor:draw() | |
if self.show then | |
pushStyle() | |
strokeWidth(3) | |
stroke(0, 0, 0, 255) | |
line(self.x, 0, self.x, self.h) | |
popStyle() | |
end | |
end | |
function Cursor._blink(cursor) | |
cursor.show = not cursor.show | |
tween.delay(0.4, Cursor._blink, cursor) | |
end |
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
-- Displacement | |
--Compute Displacement, July 13, 2014 | |
-- | |
VERSION = "0.02" | |
PROJECTNAME = "Displacement" | |
-- Use this function to perform your initial setup | |
function setup() | |
autoGist = AutoGist(PROJECTNAME, "Displacement Calculator", VERSION) | |
autoGist:backup(true) | |
gFont = "CourierNewPSMT" | |
parameter.number("Bore", 1.0, 200.0, 50.) | |
parameter.number("Stroke", 1.0, 200.0, 50.0) | |
parameter.number("Cylinders", 1.0, 16.0, 3.0) | |
d = Dialog() | |
disp = Displacement(gFont) | |
end | |
-- This function gets called once every frame | |
function draw() | |
Bore = 80.0 | |
Stroke = 71.0 | |
Cylinders = 3.0 | |
Disp = 0 | |
disp:draw() | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment