updated canvas helper

and migrated to new code
This commit is contained in:
Pavel Talanov
2012-02-07 15:37:19 +04:00
parent 6d27bde4cb
commit 01b3b13fa1
11 changed files with 67 additions and 74 deletions
+6 -2
View File
@@ -27,5 +27,9 @@ native
fun setInterval(callback : ()-> Unit) {}
library("collectionsMax")
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = f
native
open class DomElement() {
val offsetLeft = 0.0;
val offsetTop = 0.0;
val offsetParent : DomElement? = DomElement();
}
+4
View File
@@ -2,6 +2,10 @@ package java.util
import js.annotations.*;
library("collectionsMax")
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = f
library
public trait Comparator<T> {
fun compare(obj1 : T, obj2 : T) : Int;
+9 -5
View File
@@ -1,6 +1,7 @@
package html5
import js.annotations.native
import js.DomElement
native
class Context() {
@@ -11,6 +12,7 @@ class Context() {
fun rotate(angle : Double) {}
fun translate(x : Double, y : Double) {}
fun clearRect(x : Double, y : Double, w : Double, h : Double) {}
fun fillRect(x : Double, y : Double, w : Double, h : Double) {}
fun strokeRect(x : Double, y : Double, w : Double, h : Double) {}
@@ -46,6 +48,12 @@ class Context() {
fun measureText(text : String) : TextMetrics = TextMetrics();
}
native
class Canvas() : DomElement() {
val width = 0.0;
val height = 0.0;
}
native
class TextMetrics() {
val width : Int = 0
@@ -54,9 +62,5 @@ class TextMetrics() {
/*custom helpers*/
native
fun getContext() : Context = Context();
//native
//fun random() : Double = 0.0
native
fun getCanvasWidth() : Double = 0.0
native
fun getCanvasHeight() : Double = 0.0
fun getCanvas() : Canvas = Canvas();
+22 -4
View File
@@ -1,6 +1,7 @@
package jquery;
import js.annotations.*;
import js.DomElement
native
class JQuery() {
@@ -19,8 +20,16 @@ class JQuery() {
fun removeClass(className : String) = this
fun height() = 0
fun width() = 0
fun click() = this;
fun click(handler : DomElement.()->Unit) = this;
fun mousedown(handler : DomElement.(MouseEvent)->Unit) = this;
fun mouseup(handler : DomElement.(MouseEvent)->Unit) = this;
fun mousemove(handler : DomElement.(MouseEvent)->Unit) = this;
fun dblclick(handler : DomElement.(MouseClickEvent)->Unit) = this;
fun click(handler : DomElement.(MouseClickEvent)->Unit) = this;
fun append(str : String) = this;
fun ready(handler : ()->Unit) = this;
fun text(text : String) = this;
@@ -29,11 +38,20 @@ class JQuery() {
fun hover(handlerIn : DomElement.() -> Unit, handlerOut : DomElement.() -> Unit) = this;
}
open class DomElement() {
native
open class MouseEvent() {
val pageX : Double = 0.0;
val pageY : Double = 0.0;
fun preventDefault() {}
fun isDefaultPrevented() : Boolean = true;
}
//val document = object : DomElement() {}
native
class MouseClickEvent() : MouseEvent() {
val which : Int = 0;
}
native("$")
fun jq(selector : String) = JQuery();
@@ -19,10 +19,7 @@ import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.*;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getNamespaceName;
import static org.jetbrains.k2js.utils.JetFileUtils.createPsiFileList;
@@ -47,36 +44,32 @@ public final class K2JSTranslator {
writer.close();
}
public static void testTranslateFile(@NotNull String inputFile, @NotNull String outputFile) throws Exception {
public static void testTranslateFile(@NotNull String inputFile,
@NotNull String outputFile) throws Exception {
testTranslateFiles(Collections.singletonList(inputFile), outputFile);
}
public static void testTranslateFiles(@NotNull List<String> inputFiles,
@NotNull String outputFile) throws Exception {
K2JSTranslator translator = new K2JSTranslator(new TestConfig());
JetFile psiFile = JetFileUtils.loadPsiFile(inputFile, translator.getProject());
translator.generateAndSaveProgram(psiFile, outputFile);
List<JetFile> psiFiles = createPsiFileList(inputFiles, translator.getProject());
JsProgram program = translator.generateProgram(psiFiles);
saveProgramToFile(outputFile, program);
}
private static void saveProgramToFile(@NotNull String outputFile, @NotNull JsProgram program) throws IOException {
CodeGenerator generator = new CodeGenerator();
generator.generateToFile(program, new File(outputFile));
}
@NotNull
private Config config;
public K2JSTranslator(@NotNull Config config) {
this.config = config;
}
private void generateAndSaveProgram(@NotNull JetFile psiFile,
@NotNull String outputFile) throws IOException {
JsProgram program = generateProgram(Arrays.asList(psiFile));
CodeGenerator generator = new CodeGenerator();
generator.generateToFile(program, new File(outputFile));
}
public static void testTranslateFiles(@NotNull List<String> inputFiles, @NotNull String outputFile)
throws Exception {
K2JSTranslator translator = new K2JSTranslator(new TestConfig());
List<JetFile> psiFiles = createPsiFileList(inputFiles, translator.getProject());
JsProgram program = translator.generateProgram(psiFiles);
CodeGenerator generator = new CodeGenerator();
generator.generateToFile(program, new File(outputFile));
}
@NotNull
public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) {
JetFile file = JetFileUtils.createPsiFile("test", programText, getProject());
@@ -19,7 +19,7 @@ public abstract class AccessTranslator extends AbstractTranslator {
public static AccessTranslator getAccessTranslator(@NotNull JetExpression referenceExpression,
@NotNull TranslationContext context) {
assert ((referenceExpression instanceof JetReferenceExpression) ||
(referenceExpression instanceof JetDotQualifiedExpression));
(referenceExpression instanceof JetQualifiedExpression));
if (PropertyAccessTranslator.canBePropertyAccess(referenceExpression, context)) {
if (referenceExpression instanceof JetQualifiedExpression) {
return QualifiedExpressionTranslator.getAccessTranslator((JetQualifiedExpression) referenceExpression, context);
@@ -67,12 +67,6 @@ public final class OperatorOverloadingTest extends TranslationTest {
testFooBoxIsTrue("plusAssignNoReassign.kt");
}
public void testPlusAssignReassign() throws Exception {
testFooBoxIsTrue("plusAssignReassign.kt");
}
public void testNotOverload() throws Exception {
testFooBoxIsTrue("notOverload.kt");
}
@@ -81,7 +81,8 @@ public abstract class TranslationTest extends BaseTest {
}
protected void translateFile(String filename) throws Exception {
K2JSTranslator.testTranslateFile(getInputFilePath(filename), getOutputFilePath(filename));
K2JSTranslator.testTranslateFile(getInputFilePath(filename),
getOutputFilePath(filename));
}
protected void translateFilesInDir(String dirName) throws Exception {
@@ -92,7 +93,8 @@ public abstract class TranslationTest extends BaseTest {
fullFilePaths.add(getInputFilePath(dirName) + "\\" + fileName);
}
assert dir.isDirectory();
K2JSTranslator.testTranslateFiles(fullFilePaths, getOutputFilePath(dirName + ".kt"));
K2JSTranslator.testTranslateFiles(fullFilePaths,
getOutputFilePath(dirName + ".kt"));
}
protected List<String> generateFilenameList(String inputFile) {
+3 -11
View File
@@ -1,15 +1,7 @@
function getContext() {
return document.getElementsByTagName('canvas')[0].getContext('2d');
return getCanvas().getContext('2d');
}
function random() {
return Math.random();
}
function getCanvasWidth() {
return document.getElementsByTagName('canvas')[0].width;
}
function getCanvasHeight() {
return document.getElementsByTagName('canvas')[0].height;
function getCanvas() {
return document.getElementsByTagName('canvas')[0];
}
@@ -5,9 +5,7 @@ var a = MyInt()
class MyInt() {
var b = 0
fun inc() {
b = b + 1;
}
fun inc() = b + 1
}
@@ -1,16 +0,0 @@
package foo
class A(i : Int) {
val a = i
fun plusAssign(other : A) = A(this.a + other.a)
}
fun box() : Boolean {
var c = A(2)
val d = c
c += A(3)
return (c.a == 5) && (d.a == 2) && (d != c)
}