Skip to content

Commit

Permalink
- #1984 wollok-game: Set text on visual components.
Browse files Browse the repository at this point in the history
- #1985 wollok-game: Allow invisible components
  • Loading branch information
lgassman committed Jan 9, 2021
1 parent 1183b94 commit 3d9296d
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 5 deletions.
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)
}

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 @@ -48,6 +48,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 Down Expand Up @@ -75,6 +76,9 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
if (!declaration.writeable) {
constantsProperties.add(variableName)
}
else {
varProperties.add(variableName)
}
}
}

Expand All @@ -83,6 +87,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 @@ -102,7 +112,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 @@ -112,6 +122,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

0 comments on commit 3d9296d

Please sign in to comment.