Advertisement
Guest User

Untitled

a guest
Jun 16th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.13 KB | None | 0 0
  1. --------------------------------------------------------------
  2. ----------------- EQUATIONS D'OXYDOREDUCTION -----------------
  3. --------------------------------------------------------------
  4.  
  5. -- De Antoine Appia, TI-Planet/Omnimaga : AnToX98, antoineappia@hotmail.fr
  6. -- © Copyright 2014
  7.  
  8. --------------------------------------------------------------
  9.  
  10. local w, h, opt, input, Chembox, ww, hh, P, choice, firstequ, secequ, equ
  11.  
  12. function on.construction()
  13.     w, h = 318, 212
  14.     input = true
  15.     Chembox = { D2Editor.newRichText(), D2Editor.newRichText(), D2Editor.newRichText(), D2Editor.newRichText() }
  16.     ww, hh = 60, 23
  17.     Chembox[1]:resize(ww, hh) Chembox[2]:resize(ww, hh) Chembox[3]:resize(ww, hh) Chembox[4]:resize(ww, hh)
  18.     P = { 10, 40, 85, 40, 10, 83, 85, 83 }
  19.     for i = 1, #Chembox do
  20.         Chembox[i]:move(P[2 * i - 1], P[(2 * i)])
  21.         Chembox[i]:createChemBox()
  22.         Chembox[i]:setFontSize(10)
  23.         Chembox[i]:registerFilter{
  24.             charIn = function(char)
  25.                 local t, c, s = Chembox[i]:getExpressionSelection()
  26.                 return (c == 0 or c == t:ulen())
  27.             end,
  28.             backspaceKey = function()
  29.                 local t, c, s = Chembox[i]:getExpressionSelection()
  30.                 return (c == 7)
  31.             end,
  32.             enterKey = function()
  33.                 Chembox[i]:setFocus(false)
  34.                 on.enterKey()
  35.             end,
  36.             arrowKey = function(key)
  37.                 local t, c, s = Chembox[i]:getExpressionSelection()
  38.                 if key == "right" and c == t:ulen() then
  39.                     if i == 1 then Chembox[2]:setFocus(true)
  40.                     elseif i == 3 then Chembox[4]:setFocus(true)
  41.                     elseif i == 2 then Chembox[2]:setFocus(false) opt = 1
  42.                     elseif i == 4 then Chembox[4]:setFocus(false) opt = 2
  43.                     end
  44.                 elseif key == "left" and c == 0 then
  45.                     if i == 2 then Chembox[1]:setFocus(true)
  46.                     elseif i == 4 then Chembox[3]:setFocus(true)
  47.                     end
  48.                 elseif key == "up" then
  49.                     if i == 3 then Chembox[1]:setFocus(true)
  50.                     elseif i == 4 then Chembox[2]:setFocus(true)
  51.                     end
  52.                 elseif key == "down" then
  53.                     if i == 1 then Chembox[3]:setFocus(true)
  54.                     elseif i == 2 then Chembox[4]:setFocus(true)
  55.                     end
  56.                 end
  57.                 return false
  58.             end,
  59.             tabKey = function()
  60.                 if i <= 3 then
  61.                     Chembox[i + 1]:setFocus(true)
  62.                 else
  63.                     Chembox[4]:setFocus(false)
  64.                     opt = 1
  65.                 end
  66.             end,
  67.             escapeKey = function()
  68.                 local t, c, s = Chembox[i]:getExpressionSelection()
  69.                 return (c == 7)
  70.             end
  71.         }
  72.     end
  73.     choice = { 1, 1 }
  74.     opt = nil
  75.     Chembox[1]:setFocus(true)
  76. end
  77.  
  78. --------------------------------------------------------------
  79. function on.timer()
  80. end
  81.  
  82. --------------------------------------------------------------
  83. function on.arrowKey(key)
  84.     if key == "right" then
  85.     elseif key == "left" then
  86.         if opt == 1 then Chembox[2]:setFocus(true) opt = nil end
  87.         if opt == 2 then Chembox[4]:setFocus(true) opt = nil end
  88.     elseif key == "up" and opt == 2 then
  89.         opt = 1
  90.     elseif key == "down" and opt == 1 then
  91.         opt = 2
  92.     end
  93.     platform.window:invalidate()
  94. end
  95.  
  96. --------------------------------------------------------------
  97. function on.enterKey()
  98.     if input then
  99.         input = false
  100.         local table = {}
  101.         for i = 1, #Chembox do
  102.             table[#table + 1] = Chembox[i]:getExpressionSelection()
  103.         end
  104.         firstequ = Balance(table[1], table[2])
  105.         secequ = Balance(table[3], table[4])
  106.         platform.window:invalidate()
  107.     end
  108. end
  109.  
  110. function Balance(e1, e2)
  111.     local a
  112.     if choice[2] == 1 then a = "+ H^1" elseif choice[2] == 2 then a = "+ HO^-1" end
  113.     local str = e1:sub(8, #e1 - 1) .. "+^-1" .. "=" .. e2:sub(8, #e2 - 1)
  114.     str = replace(str)
  115.     str = math.evalStr('balance("' .. str .. '")')
  116.     if not str or str:sub(2, 3) == "Er" then
  117.         if e1:find("H₂O") or e2:find("H₂O") then
  118.             equ = e1:sub(8, #e1 - 1) .. a .. "+^-1" .. "=" .. e2:sub(8, #e2 - 1)
  119.         else
  120.             equ = e1:sub(8, #e1 - 1) .. a .. "+^-1" .. "=" .. e2:sub(8, #e2 - 1) .. "+H2O"
  121.         end
  122.         equ = replace(equ)
  123.         equ = math.evalStr('balance("' .. equ .. '")')
  124.     else
  125.         equ = str
  126.     end
  127.  
  128.     equ = equ:gsub("\"", "")
  129.  
  130.     equ = avoidNegatifCoeffs(equ)
  131.     equ = addElectron(equ)
  132.     return equ
  133. end
  134.  
  135. function replace(equ)
  136.     local tn = { "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "¹⁰" }
  137.     for i = 1, 10 do
  138.         equ = equ:gsub(tn[i] .. "⁻", "^-" .. tostring(i))
  139.         equ = equ:gsub(tn[i] .. "⁺", "^" .. tostring(i))
  140.     end
  141.     equ = equ:gsub("⁻", "^-1")
  142.     equ = equ:gsub("⁺", "^1")
  143.     local ti = { "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉" }
  144.     equ = equ:gsub("₀", "0")
  145.     for i = 1, 9 do
  146.         equ = equ:gsub(ti[i], i)
  147.     end
  148.     return equ
  149. end
  150.  
  151. function avoidNegatifCoeffs(equ)
  152.     local m = equ:ufind("→")
  153.     local left = equ:usub(1, m - 2)
  154.     local right = equ:usub(m + 2, equ:ulen())
  155.     local tleft, tright
  156.     for i = 1, left:ulen() do
  157.         if left:usub(i, i) == "−" then
  158.             for k = i, left:ulen() do
  159.                 if left:usub(k, k) == "+" or k == #left then
  160.                     tleft = { i, k }
  161.                     right = right .. " " .. left:usub(i, k - 1):gsub("−", "+ ")
  162.                 end
  163.             end
  164.         end
  165.     end
  166.     if tleft then
  167.         left = left:usub(1, tleft[1] - 1) .. left:usub(tleft[2] + 1, left:ulen())
  168.     end
  169.     for i = 1, right:ulen() do
  170.         if right:usub(i, i) == "−" then
  171.             for k = i, left:ulen() do
  172.                 if right:usub(k, k) == "+" or k == #right then
  173.                     tright = { i, k }
  174.                     left = left .. " " .. right:usub(i, k - 1):gsub("−", "+ ")
  175.                 end
  176.             end
  177.         end
  178.     end
  179.     if tright then
  180.         right = right:usub(1, tright[1] - 1) .. right:usub(tright[2] + 1, right:ulen())
  181.     end
  182.     return left .. " = " .. right
  183. end
  184.  
  185. --[[
  186. function addElectron(equ)
  187.     local m = equ:ufind("=")
  188.     local left = equ:usub(1, m - 2)
  189.     local right = equ:usub(m + 2, equ:ulen())
  190.  
  191.     for i = 1, left:ulen() do
  192.         if isCharge(left:usub(i, i)) == true then
  193.             for j = i, 1, -1 do
  194.                 if string.byte(left:usub(j, j)) >= 65 and string.byte(left:usub(j, j)) <= 90 or string.byte(left:usub(j, j)) >= 97 and string.byte(left:usub(j, j)) <= 122 or isNumb(left:usub(j, j)) == true then
  195.                     break
  196.                 end
  197.                 if isCharge(left:usub(j, j)) == false then
  198.                     electronpos = j
  199.                     break
  200.                 end
  201.                 if left:usub(j, j) == "+" then
  202.                     break
  203.                 end
  204.             end
  205.         end
  206.         if electronpos then
  207.             break
  208.         end
  209.     end
  210.     print(electronpos)
  211.     left = left:usub(1, electronpos - 1) .. "e" .. left:usub(electronpos + 1, left:ulen())
  212.     equ = left .. " = " .. right
  213.     return equ
  214. end
  215. --]]
  216.  
  217. function isCoeff(str)
  218.     local ti = { "1", "2", "3", "4", "5", "6", "7", "8", "9", " " }
  219.     for k = 1, #ti do
  220.         if ti[k] == str then
  221.             return true
  222.         end
  223.     end
  224.     return false
  225. end
  226.  
  227. function addElectron(equ)
  228.     for i = 1, equ:ulen() - 1 do
  229.         if isCoeff(equ:usub(i, i)) then
  230.             if equ:usub(i + 1, i + 1) == "⁻" then
  231.                 equ = equ:usub(1, i) .. "e" .. equ:usub(i + 1, equ:ulen())
  232.             end
  233.         end
  234.     end
  235.     return equ
  236. end
  237.  
  238. function isCharge(str)
  239.     local tn = { "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "⁻", "⁺" }
  240.     for k = 1, #tn do
  241.         if tn[k] == str then
  242.             return true
  243.         end
  244.     end
  245.     return false
  246. end
  247.  
  248. function isNumb(str)
  249.     local ti = { "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉", "₀" }
  250.     for k = 1, #ti do
  251.         if ti[k] == str then
  252.             return true
  253.         end
  254.     end
  255.     return false
  256. end
  257.  
  258. function string.ulen(s) -- count the number of non-continuing bytes
  259.     return select(2, s:gsub('[^\128-\193]', ''))
  260. end
  261.  
  262. function string.ufind(s)
  263.     for i = 1, str:ulen() do
  264.         if str:usub(i, i) == s then
  265.             return i
  266.         end
  267.     end
  268.     return nil
  269. end
  270.  
  271. --------------------------------------------------------------
  272. function on.tabKey()
  273.     if opt == 1 then opt = 2
  274.     elseif opt == 2 then Chembox[1]:setFocus(true) opt = nil
  275.     end
  276. end
  277.  
  278. --------------------------------------------------------------
  279. function on.escapeKey()
  280.     if choice[opt] == 1 then choice[opt] = 2
  281.     elseif choice[opt] == 2 then choice[opt] = 1
  282.     end
  283.     platform.window:invalidate()
  284. end
  285.  
  286. --------------------------------------------------------------
  287. function on.paint(gc)
  288.     gc:drawLine(0, 20, 318, 20)
  289.     gc:drawLine(73, 58, 80, 45)
  290.     gc:drawLine(73, 101, 80, 88)
  291.     gc:setColorRGB(0, 0, 200)
  292.     gc:setFont("serif", "b", 11)
  293.     gc:drawString("ÉQUATION D'OXYDORÉDUCTION", w / 2 - gc:getStringWidth("ÉQUATION D'OXYDORÉDUCTION") / 2, 0)
  294.     gc:setColorRGB(0, 150, 0)
  295.     for i = 1, 4 do
  296.         gc:drawRect(P[2 * i - 1] - 1, P[(2 * i)] - 1, ww + 1, hh + 1)
  297.     end
  298.     gc:setFont("sansserif", "b", 8)
  299.     gc:drawString("COUPLE 1", 10, 24)
  300.     gc:drawString("COUPLE 2", 10, 67)
  301.     gc:setColorRGB(0, 0, 0)
  302.     gc:setFont("sansserif", "b", 8)
  303.     gc:drawString("Reactifs", 210, 25)
  304.     gc:drawString("Milieu de la réaction", 188, 69)
  305.     gc:setColorRGB(200, 200, 200)
  306.     if opt == 1 then
  307.         gc:setColorRGB(200, 200, 255)
  308.     end
  309.     gc:fillRoundRect(230, 52, 100, 25, 10)
  310.     gc:setColorRGB(200, 200, 200)
  311.     if opt == 2 then
  312.         gc:setColorRGB(200, 200, 255)
  313.     end
  314.     gc:fillRoundRect(230, 96, 100, 25, 10)
  315.     gc:setColorRGB(0, 0, 0)
  316.     gc:drawRoundRect(230, 52, 100, 25, 10)
  317.     gc:drawRoundRect(230, 96, 100, 25, 10)
  318.     gc:setFont("serif", "b", 10)
  319.     gc:drawString("O₁/R₂", 195, 43)
  320.     gc:drawString("O₂/R₁", 236, 43)
  321.     gc:drawString("ACI", 197, 87)
  322.     gc:drawString("BAS", 238, 87)
  323.     gc:setColorRGB(150, 0, 0)
  324.     if choice[1] == 1 then
  325.         gc:drawRect(190, 44, 40, 16, 10)
  326.     else
  327.         gc:drawRect(230, 44, 40, 16, 10)
  328.     end
  329.     if choice[2] == 1 then
  330.         gc:drawRect(190, 88, 40, 16, 10)
  331.     else
  332.         gc:drawRect(230, 88, 40, 16, 10)
  333.     end
  334.     if input then
  335.         gc:setFont("sansserif", "r", 9)
  336.         gc:setColorRGB(0, 0, 0)
  337.         gc:drawString([[Appuyez sur [enter] pour valider vos entrées ou sur
  338.       [esc] pour changer  le type/mileu de réaction]], 20, 140)
  339.     else
  340.         gc:setFont("serif", "b", 10)
  341.         gc:setColorRGB(200, 0, 0)
  342.         gc:drawString("Réaction 1 : " .. firstequ, 10, 115)
  343.         gc:drawString("Réaction 2 : " .. secequ, 10, 135)
  344.         gc:setFont("serif", "b", 6)
  345.         gc:setColorRGB(0, 100, 0)
  346.         local str = [[Au cours d'une équation d'oxydoréduction, il y a autant d'électrons
  347.     captés par l'oxydant que d'électrons cédés par le réducteur. ]]
  348.         gc:drawString(str, 18, 155)
  349.         gc:setColorRGB(0, 0, 100)
  350.         gc:drawLine(32, 183, w - 32, 183)
  351.     end
  352. end
  353.  
  354. --------------------------------------------
  355. -- BetterLuaAPI, By Adriweb
  356. --------------------------------------------
  357. local function drawRoundRect(gc, x, y, wd, ht, rd) -- wd = width, ht = height, rd = radius of the rounded corner
  358.     local x = x - wd / 2 -- let the center of the square be the origin (x coord)
  359.     local y = y - ht / 2 -- same for y coord
  360.     if rd > ht / 2 then rd = ht / 2 end -- avoid drawing cool but unexpected shapes. This will draw a circle (max rd)
  361.     gc:drawLine(x + rd, y, x + wd - (rd), y)
  362.     gc:drawArc(x + wd - (rd * 2), y + ht - (rd * 2), rd * 2, rd * 2, 270, 90)
  363.     gc:drawLine(x + wd, y + rd, x + wd, y + ht - (rd))
  364.     gc:drawArc(x + wd - (rd * 2), y, rd * 2, rd * 2, 0, 90)
  365.     gc:drawLine(x + wd - (rd), y + ht, x + rd, y + ht)
  366.     gc:drawArc(x, y, rd * 2, rd * 2, 90, 90)
  367.     gc:drawLine(x, y + ht - (rd), x, y + rd)
  368.     gc:drawArc(x, y + ht - (rd * 2), rd * 2, rd * 2, 180, 90)
  369. end
  370.  
  371. local function fillRoundRect(gc, x, y, wd, ht, rd) -- wd = width, ht = height, rd = radius
  372.     if rd > ht / 2 then rd = ht / 2 end -- avoid drawing cool but unexpected shapes. This will draw a circle (max rd)
  373.     gc:fillPolygon({ (x - wd / 2), (y - ht / 2 + rd), (x + wd / 2), (y - ht / 2 + rd), (x + wd / 2), (y + ht / 2 - rd), (x - wd / 2), (y + ht / 2 - rd), (x - wd / 2), (y - ht / 2 + rd) })
  374.     gc:fillPolygon({ (x - wd / 2 - rd + 1), (y - ht / 2), (x + wd / 2 - rd + 1), (y - ht / 2), (x + wd / 2 - rd + 1), (y + ht / 2), (x - wd / 2 + rd), (y + ht / 2), (x - wd / 2 + rd), (y - ht / 2) })
  375.     local x = x - wd / 2 -- let the center of the square be the origin (x coord)
  376.     local y = y - ht / 2 -- same
  377.     gc:fillArc(x + wd - (rd * 2), y + ht - (rd * 2), rd * 2, rd * 2, 1, -91)
  378.     gc:fillArc(x + wd - (rd * 2), y, rd * 2, rd * 2, -2, 91)
  379.     gc:fillArc(x, y, rd * 2, rd * 2, 85, 95)
  380.     gc:fillArc(x, y + ht - (rd * 2), rd * 2, rd * 2, 180, 95)
  381. end
  382.  
  383. -- Credits to Jim Bauwens :)
  384. -- See http://inspired-lua.org/index.php/2013/05/how-to-add-your-own-functions-to-gc/
  385. function AddToGC(key, func)
  386.     local gcMetatable = platform.withGC(getmetatable)
  387.     gcMetatable[key] = func
  388. end
  389.  
  390. AddToGC("drawRoundRect", drawRoundRect)
  391. AddToGC("fillRoundRect", fillRoundRect)
  392.  
  393. --------------------------------------------
  394. function myErrorHandler(line, errMsg, callStack, locals)
  395.     print("Error handled ! ", errMsg)
  396.     return true -- let the script continue
  397. end
  398.  
  399. platform.registerErrorHandler(myErrorHandler)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement