Skip to content

Commit

Permalink
customize color
Browse files Browse the repository at this point in the history
  • Loading branch information
lgassman committed Dec 27, 2020
1 parent f96b27a commit d4f7b89
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import static extension org.uqbar.project.wollok.game.helpers.WollokConventionEx
import org.uqbar.project.wollok.interpreter.core.WollokProgramExceptionWrapper
import static extension org.uqbar.project.wollok.interpreter.nativeobj.WollokJavaConversions.*


@Accessors
class VisualComponent {
public static val MESSAGE_MAX_LENGTH = 50
Expand All @@ -23,28 +22,25 @@ class VisualComponent {
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
checkText()
this(object, object.position)
}

new(WollokObject object, WollokObject position) {
wObject = object
wPosition = position
checkText()
hasText = wObject.understands(TEXT_CONVENTION)
hasTextColor = wObject.understands(TEXT_COLOR_CONVENTION)
hasImage = wObject.understands(IMAGE_CONVENTION)
}

def checkText() {
try {
wObject.call(TEXT_CONVENTION)
hasText = true
}catch(WollokProgramExceptionWrapper e) {
}
}

def getAttributes() {
Expand Down Expand Up @@ -80,11 +76,17 @@ class VisualComponent {
}

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

def drawText(Window window) {
if(hasText) window.writeText(wObject.call(TEXT_CONVENTION).wollokToJava(String) as String, getPosition(), Color.BLACK)
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
@@ -1,17 +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 @@ -65,7 +65,7 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
def void addMember(WVariableDeclaration declaration, boolean initializeIt) {
val variableName = declaration.variable.name
instanceVariables.put(variableName, interpreter.performOnStack(declaration, this) [|
if (initializeIt) declaration.right?.eval else null
if(initializeIt) declaration.right?.eval else null
])
if (!declaration.writeable) {
constantReferences.add(variableName)
Expand All @@ -83,6 +83,13 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
// ******************************************
override getThisObject() { this }

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

}

override call(String message, WollokObject... parameters) {
val method = behavior.lookupMethod(message, parameters, false)
if (method === null) {
Expand Down Expand Up @@ -112,6 +119,10 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
override hasProperty(String variableName) {
properties.contains(variableName)
}

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

def throwMessageNotUnderstood(String methodName, Object... parameters) {
try {
Expand All @@ -120,7 +131,8 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
// this one is ok because calling messageNotUnderstood actually throws the exception!
throw e
} catch (RuntimeException e) {
throw new RuntimeException(NLS.bind(Messages.WollokInterpreter_errorWhileMessageNotUnderstood, e.message), e)
throw new RuntimeException(NLS.bind(Messages.WollokInterpreter_errorWhileMessageNotUnderstood, e.message),
e)
}
}

Expand All @@ -137,8 +149,8 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
val delegatedConstructor = constructor.wollokClass.resolveConstructorReference(other)
var EList<? extends EObject> arguments = ECollections.emptyEList
if (other.hasNamedParameters) {
arguments = ECollections.asEList(delegatedConstructor.parameters.map [ name ].map [
argName | other.argumentList.getArgument(argName)
arguments = ECollections.asEList(delegatedConstructor.parameters.map[name].map [ argName |
other.argumentList.getArgument(argName)
])
} else {
arguments = other.arguments
Expand Down Expand Up @@ -174,11 +186,10 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
this
else if (instanceVariables.containsKey(variableName)) {
getVariableValue(variableName)
}
else
} else
parentContext.resolve(variableName)
}

def getVariableValue(String variableName) {
val value = instanceVariables.get(variableName)
if (value instanceof LazyWollokObject) {
Expand All @@ -191,7 +202,7 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W

override setReference(String name, WollokObject value) {
if (name == SELF)
throw new RuntimeException(NLS.bind(Messages.WollokDslValidator_CANNOT_MODIFY_REFERENCE, SELF))
throw new RuntimeException(NLS.bind(Messages.WollokDslValidator_CANNOT_MODIFY_REFERENCE, SELF))
if (!instanceVariables.containsKey(name)) {
throw new UnresolvableReference('''Unrecognized variable "«name»" in object "«this.behavior.objectDescription»"''')
}
Expand All @@ -202,23 +213,23 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W

// query (kind of reflection api)
override allReferenceNames() {
instanceVariables.keySet.map [ variableName |
instanceVariables.keySet.map [ variableName |
val isConstantReference = constantReferences.contains(variableName)
val wollokObject = getVariableValue(variableName)
new WVariable(variableName, System.identityHashCode(wollokObject), false, isConstantReference)
] + #[SELF_VAR]
}

def simplifiedReferences() { behavior.fqn.equals(DATE) }

override allReferenceNamesForDynamicDiagram() {
if (simplifiedReferences) {
newArrayList
newArrayList
} else {
this.allReferenceNames.toList
}
}
}

def getProperties() {
properties
}
Expand All @@ -229,10 +240,10 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
else
behavior.methods
}

def toWollokString() {
val string = call("toString", #[])
(string.getNativeObject(STRING) as JavaWrapper<String>).wrapped //TODO: Usar las conversion extensions
(string.getNativeObject(STRING) as JavaWrapper<String>).wrapped // TODO: Usar las conversion extensions
}

override toString() {
Expand Down Expand Up @@ -311,14 +322,14 @@ class WollokObject extends AbstractWollokCallable implements EvaluationContext<W
throw throwMessageNotUnderstood(this, message, parameters)
}
}

method.call(parameters)
}

override showableInStackTrace() { true }

override variableShowableInDynamicDiagram(String name) { true }

}

/**
Expand Down

0 comments on commit d4f7b89

Please sign in to comment.