Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1984 (texto en visual objects) y #1985 (objetos invisibles) #1986

Merged
merged 2 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.uqbar.project.wollok.game.gameboard.Window
import org.uqbar.project.wollok.interpreter.core.WollokObject

import static extension org.uqbar.project.wollok.game.helpers.WollokConventionExtensions.*
import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*

@Accessors
class VisualComponent {
Expand All @@ -18,20 +19,26 @@ class VisualComponent {

List<BalloonMessage> balloonMessages = newArrayList
boolean showAttributes = false

boolean hasText = false
boolean hasTextColor = false
boolean hasImage = false

WollokObject wObject
WollokObject wPosition

new(WollokObject object) {
wObject = object
wObject?.position // Force evaluate position when is added
this(object, object.position)
}

new(WollokObject object, WollokObject position) {
wObject = object
wPosition = position
hasText = wObject.understands(TEXT_CONVENTION)
hasTextColor = wObject.understands(TEXT_COLOR_CONVENTION)
hasImage = wObject.understands(IMAGE_CONVENTION)
}
Comment on lines 30 to 40
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hay que tener cuidado con esto. Este objeto tiene 2 constructores por las dos formas que tiene WG de indicar la posición de un objeto:

  • game.addVisual usa el primero, e indica que el objeto es quien conoce a su posición.
  • game.addVisualIn la posición es pasada por parámetro y el objeto no la conoce.

Si hacemos que uno delegue en el otro, CREO que el primer (y más usado) caso cachearía la posición inicial pero no se actualizaría a medida que el objeto cambie su posición.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sí, efectivamente acá había quedado ese bug, ahí lo arreglé en #1990 junto con otros detalles consecuencias de este PR) 😃


def getAttributes() {
wObject.printableVariables.filter[value !== null].map[key + ":" + value.toString].toList
}
Expand All @@ -58,13 +65,24 @@ class VisualComponent {
def void draw(Window window) {
window => [
drawMe
drawText
drawAttributesIfNecesary
drawBalloonIfNecesary
]
}

def drawMe(Window window) {
window.draw(image, position)
if(hasImage) {
window.draw(image, position)
}
}

def drawText(Window window) {
if(hasText) {
val text = wObject.call(TEXT_CONVENTION).wollokToJava(String) as String
val color = hasTextColor ? Color.valueOf(wObject.call(TEXT_COLOR_CONVENTION).wollokToJava(String) as String) : DEFAULT_TEXT_COLOR
window.writeText(text, getPosition(), color)
}
}

def drawAttributesIfNecesary(Window window) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class Window {
def doDraw(Texture texture, Position it, ImageSize size) {
batch.draw(texture, xinPixels, yinPixels, size.width(texture.width), size.height(texture.height))
}

def writeText(String text, Position position, Color color) {
write(text, color, position.xinPixels - 80, position.yinPixels + TEXT_SIZE)
}

def writeAttributes(String text, Position position, Color color) {
val lines = text.split("[\n|\r]").length
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package org.uqbar.project.wollok.game.helpers

import com.badlogic.gdx.graphics.Color
import org.uqbar.project.wollok.game.VisualComponent
import org.uqbar.project.wollok.interpreter.core.WollokObject
import org.uqbar.project.wollok.interpreter.core.WollokProgramExceptionWrapper

import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*


class WollokConventionExtensions {

public static val POSITION_CONVENTION = "position"
public static val TEXT_CONVENTION = "text"
public static val TEXT_COLOR_CONVENTION = "textColor"
public static val IMAGE_CONVENTION = "image"
public static val DEFAULT_IMAGE = "wko.png"
public static val DEFAULT_TEXT_COLOR = Color.BLUE


def static asVisual(WollokObject it) { new VisualComponent(it) }
def static asVisualIn(WollokObject it, WollokObject position) { new VisualComponent(it, position) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import org.uqbar.project.wollok.tests.injectors.WollokTestInjectorProvider
import wollok.lang.WDate

import static wollok.lang.WDate.*
import org.uqbar.project.wollok.WollokConstants
import org.uqbar.project.wollok.interpreter.core.WollokObject
import org.uqbar.project.wollok.interpreter.context.UnresolvableReference

/**
* Abstract base class for all interpreter tests cases.
Expand All @@ -35,6 +38,7 @@ abstract class AbstractWollokInterpreterTestCase extends Assert {
@Inject protected XtextResourceSet resourceSet
@Inject protected extension WollokInterpreter interpreter
public static val EXAMPLES_PROJECT_PATH = "../wollok-tests"
static val SYNTHETIC_PACKAGE = "p_" + WollokConstants.SYNTHETIC_FILE + "0"

@Before
def void setUp() {
Expand Down Expand Up @@ -200,4 +204,23 @@ abstract class AbstractWollokInterpreterTestCase extends Assert {
}

}

/**
* Find a global reference into interpreter.
* If failed try to find using synthetic package prefix.
*/
def WollokObject asWollokObject(String globalReference) {
try {
return interpreter.resolve(globalReference)
}
catch(UnresolvableReference originalException) {
try {
return interpreter.resolve(SYNTHETIC_PACKAGE + "." + globalReference)
}
catch(RuntimeException ex) {
throw originalException
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.uqbar.project.wollok.tests.interpreter.language

import org.junit.Test
import org.uqbar.project.wollok.interpreter.core.WollokObject
import org.uqbar.project.wollok.tests.interpreter.AbstractWollokInterpreterTestCase

class WollokObjectTest extends AbstractWollokInterpreterTestCase {

@Test
def void testUnderstand() {
'''
object myObject {

var privateVar = "private"
var property aVarProperty = "writable"
const property aConstProperty = "readOnly"

method aGetter() {
return "foo"
}

method aSetter(foo) {

}

method aMethod(foo, bar) {

}
}

//A program is necessary in order to evaluate and create the WollokObject
program abc {
myObject
}
'''.interpretPropagatingErrors

val myWollokObject = "myObject".asWollokObject

myWollokObject.assertProperties
myWollokObject.assertVarProperties
myWollokObject.assertConstantProperties
myWollokObject.assertUnderstands

}

def assertUnderstands(WollokObject myWollokObject) {
val zeroParams = #[] as WollokObject[]
val oneParam = #[myWollokObject] as WollokObject[]
val twoParams = #[myWollokObject , myWollokObject] as WollokObject[]

assertFalse(myWollokObject.understands("privateVar", zeroParams))
assertFalse(myWollokObject.understands("privateVar", oneParam))
assertFalse(myWollokObject.understands("privateVar", twoParams))

assertTrue(myWollokObject.understands("aVarProperty", zeroParams))
assertTrue(myWollokObject.understands("aVarProperty", oneParam))
assertFalse(myWollokObject.understands("aVarProperty", twoParams))

assertTrue(myWollokObject.understands("aConstProperty", zeroParams))
assertFalse(myWollokObject.understands("aConstProperty", oneParam))
assertFalse(myWollokObject.understands("aConstProperty", twoParams))

assertTrue(myWollokObject.understands("aGetter", zeroParams))
assertFalse(myWollokObject.understands("aGetter", oneParam))
assertFalse(myWollokObject.understands("aGetter", twoParams))

assertFalse(myWollokObject.understands("aSetter", zeroParams))
assertTrue(myWollokObject.understands("aSetter", oneParam))
assertFalse(myWollokObject.understands("aSetter", twoParams))

assertFalse(myWollokObject.understands("aMethod", zeroParams))
assertFalse(myWollokObject.understands("aMethod", oneParam))
assertTrue(myWollokObject.understands("aMethod", twoParams))

assertFalse(myWollokObject.understands("nonExistent", zeroParams))
assertFalse(myWollokObject.understands("nonExistent", oneParam))
assertFalse(myWollokObject.understands("nonExistent", twoParams))
}

def assertConstantProperties(WollokObject myWollokObject) {
assertFalse(myWollokObject.hasConstantProperty("privateVar"))
assertFalse(myWollokObject.hasConstantProperty("aVarProperty"))
assertTrue(myWollokObject.hasConstantProperty("aConstProperty"))
assertFalse(myWollokObject.hasConstantProperty("aGetter"))
assertFalse(myWollokObject.hasConstantProperty("aSetter"))
assertFalse(myWollokObject.hasConstantProperty("aMetthod"))
assertFalse(myWollokObject.hasConstantProperty("nonExistent"))
}

def assertVarProperties(WollokObject myWollokObject) {
assertFalse(myWollokObject.hasVarProperty("privateVar"))
assertTrue(myWollokObject.hasVarProperty("aVarProperty"))
assertFalse(myWollokObject.hasVarProperty("aConstProperty"))
assertFalse(myWollokObject.hasVarProperty("aGetter"))
assertFalse(myWollokObject.hasVarProperty("aSetter"))
assertFalse(myWollokObject.hasVarProperty("aMetthod"))
assertFalse(myWollokObject.hasVarProperty("nonExistent"))
}

def assertProperties(WollokObject myWollokObject) {
assertFalse(myWollokObject.hasProperty("privateVar"))
assertTrue(myWollokObject.hasProperty("aVarProperty"))
assertTrue(myWollokObject.hasProperty("aConstProperty"))
assertFalse(myWollokObject.hasProperty("aGetter"))
assertFalse(myWollokObject.hasProperty("aSetter"))
assertFalse(myWollokObject.hasProperty("aMetthod"))
assertFalse(myWollokObject.hasProperty("nonExistent"))
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
val EvaluationContext<WollokObject> parentContext
List<String> properties = newArrayList
List<String> constantsProperties = newArrayList
List<String> varProperties = newArrayList

new(IWollokInterpreter interpreter, WMethodContainer behavior) {
super(interpreter, behavior)
Expand All @@ -63,6 +64,9 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
if (!declaration.writeable) {
constantsProperties.add(variableName)
}
else {
varProperties.add(variableName)
}
}
}

Expand All @@ -81,6 +85,12 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
// ******************************************
override getThisObject() { this }

def understands(String message, WollokObject... parameters) {
behavior.lookupMethod(message, parameters, false) !== null ||
message.hasVarProperty && parameters.length() <= 1 ||
message.hasConstantProperty && parameters.empty
}

override call(String message, WollokObject... parameters) {
val method = behavior.lookupMethod(message, parameters, false)
if (method === null) {
Expand All @@ -100,7 +110,7 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
if (parameters.size > 1) {
throwMessageNotUnderstood(message, parameters)
}
if (constantsProperties.contains(message)) {
if (hasConstantProperty(message)) {
throw messageNotUnderstood(NLS.bind(Messages.WollokDslValidator_PROPERTY_NOT_WRITABLE, message))
}
setReference(message, parameters.head)
Expand All @@ -110,6 +120,15 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
override hasProperty(String variableName) {
properties.contains(variableName)
}

def hasConstantProperty(String variableName) {
constantsProperties.contains(variableName)
}

def hasVarProperty(String variableName) {
varProperties.contains(variableName)
}


def throwMessageNotUnderstood(String methodName, Object... parameters) {
try {
Expand Down